onnx_light.tools.translate#

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

Two output flavours (api) are supported:

Both flavours are pure Python and 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) or "builder".

Returns:

The generated Python code as a string (without the import 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" or "builder".

Returns:

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