How to replace onnx by onnx-light#

This guide covers the Python interface. For the other language, see How to replace onnx by onnx-light.

onnx-light mirrors the public onnx API in both Python and C++, so an existing project can switch to it by changing a few imports, includes and build targets. This page shows the Python code before and after the replacement.

Python#

The onnx_light.onnx module exposes the same protobuf message types (ModelProto, TensorProto, …) and submodules (helper, numpy_helper, reference, backend). Only the imports change; the rest of the code (load, save, oh.make_node, ReferenceEvaluator, …) stays the same.

import onnx
import onnx.helper as oh
import onnx.numpy_helper as onh
from onnx.reference import ReferenceEvaluator

model = onnx.load("model.onnx")
node = oh.make_node("Add", ["a", "b"], ["c"])
ref = ReferenceEvaluator(model)
import onnx_light.onnx as onnx
import onnx_light.onnx.helper as oh
import onnx_light.onnx.numpy_helper as onh
from onnx_light.onnx.reference import ReferenceEvaluator

model = onnx.load("model.onnx")
node = oh.make_node("Add", ["a", "b"], ["c"])
ref = ReferenceEvaluator(model)

See also#