onnx_light.tools.translate#

Translate an ONNX model or graph into Python code that rebuilds it.

Two output flavours (api) are supported:

  • "onnx-compact" – a single nested expression building the model with onnx_light.onnx.helper (oh.make_model(oh.make_graph([...], ...))), mirroring the onnx-compact API of yet-another-onnx-builder.

  • "builder" – a plain Python script that rebuilds the same model with the incremental onnx_light.onnx_core.graph_builder.GraphBuilder (g.inp(...), g.init(...), g.op.<operator>(...), g.out(...), g.to_onnx(...)).

  • "cpp" – a C++ function that rebuilds the model with core::builder::GraphBuilder.

All flavours only rely on the attributes of the standard ONNX message types (ModelProto, GraphProto, NodeProto, ValueInfoProto, TensorProto, AttributeProto and TensorShapeProto); they therefore work both with messages built by onnx_light and with messages built by the upstream onnx package.

Example:

from onnx_light.tools import translate, translate_header

code = translate_header("onnx-compact") + translate(model, api="onnx-compact")
print(code)
onnx_light.tools.translate.translate(proto: Any, api: str = 'onnx-compact') → str#

Translates an ONNX model or graph into Python code that rebuilds it.

Parameters:
  • proto – a ModelProto or GraphProto (or a file path to load).

  • api – target flavour, "onnx-compact" (default), "builder" or "cpp".

Returns:

The generated Python or C++ code as a string (without the matching header, see translate_header()).

onnx_light.tools.translate.translate_header(api: str = 'onnx-compact') → str#

Returns the import header required by the code produced by translate().

Parameters:

api – target flavour, "onnx-compact", "builder" or "cpp".

Returns:

The import header as a string ending with a trailing newline.