onnx_array_api.translate_api#

Main API#

translate#

onnx_array_api.translate_api.translate(proto: ModelProto, single_line: bool = False, api: str = 'light') str[source]#

Translates an ONNX proto into a code using Light API for ONNX: everything in one line to describe the ONNX graph.

Parameters:
Returns:

code

<<<

from onnx_array_api.light_api import start
from onnx_array_api.translate_api import translate

onx = (
    start()
    .vin("X")
    .reshape((-1, 1))
    .Transpose(perm=[1, 0])
    .rename("Y")
    .vout()
    .to_onnx()
)
code = translate(onx)
print(code)

>>>

    (
        start(opset=21)
        .cst(np.array([-1, 1], dtype=np.int64))
        .rename('r')
        .vin('X', elem_type=TensorProto.FLOAT)
        .bring('X', 'r')
        .Reshape()
        .rename('r0_0')
        .bring('r0_0')
        .Transpose(perm=[1, 0])
        .rename('Y')
        .bring('Y')
        .vout(elem_type=TensorProto.FLOAT)
        .to_onnx()
    )

The inner API from onnx package is also available.

<<<

from onnx_array_api.light_api import start
from onnx_array_api.translate_api import translate

onx = (
    start()
    .vin("X")
    .reshape((-1, 1))
    .Transpose(perm=[1, 0])
    .rename("Y")
    .vout()
    .to_onnx()
)
code = translate(onx, api="onnx")
print(code)

>>>

    opset_imports = [
        make_opsetid('', 21),
    ]
    inputs = []
    outputs = []
    nodes = []
    initializers = []
    sparse_initializers = []
    functions = []
    initializers.append(
        from_array(
            np.array([-1, 1], dtype=np.int64),
            name='r'
        )
    )
    inputs.append(make_tensor_value_info('X', TensorProto.FLOAT, shape=[]))
    nodes.append(
        make_node_extended(
            'Reshape',
            ['X', 'r'],
            ['r0_0']
        )
    )
    nodes.append(
        make_node_extended(
            'Transpose',
            ['r0_0'],
            ['Y'],
            perm=[1, 0]
        )
    )
    outputs.append(make_tensor_value_info('Y', TensorProto.FLOAT, shape=[]))
    graph = make_graph(
        nodes,
        'light_api',
        inputs,
        outputs,
        initializers,
        sparse_initializer=sparse_initializers,
    )
    model = make_model(
        graph,
        functions=functions,
        opset_imports=opset_imports
    )

The GraphBuilder API returns this:

<<<

from onnx_array_api.light_api import start
from onnx_array_api.translate_api import translate

onx = (
    start()
    .vin("X")
    .reshape((-1, 1))
    .Transpose(perm=[1, 0])
    .rename("Y")
    .vout()
    .to_onnx()
)
code = translate(onx, api="builder")
print(code)

>>>

    
    def light_api(
        op: "GraphBuilder",
        X: "FLOAT[]",
    ):
        r = np.array([-1, 1], dtype=np.int64)
        r0_0 = op.Reshape(X, r)
        Y = op.Transpose(r0_0, perm=[1, 0])
        op.Identity(Y, outputs=["Y"])
        return Y
    
    g = GraphBuilder({'': 21})
    g.make_tensor_input("X", TensorProto.FLOAT, ())
    light_api(g.op, "X")
    g.make_tensor_output("Y", TensorProto.FLOAT, ())
    model = g.to_onnx()

make_helper#

onnx_array_api.translate_api.make_helper.make_node_extended(op_type: str, inputs: Sequence[str], outputs: Sequence[str], name: str | None = None, doc_string: str | None = None, domain: str | None = None, **kwargs: Any) NodeProto[source]#

Constructs a NodeProto.

Parameters:
  • op_type – The name of the operator to construct

  • inputs – list of input names

  • outputs – list of output names

  • name – optional unique identifier for NodeProto

  • doc_string – optional documentation string for NodeProto

  • domain – optional domain for NodeProto. If it’s None, we will just use default domain (which is empty)

  • kwargs – the attributes of the node.

Returns:

node proto

onnx_array_api.translate_api.make_helper.make_ref_attribute(key: str, attr_type: int, ref_attr_name: str | None = None) AttributeProto[source]#

Creates an attribute.

Parameters:
  • key – atttribute name

  • attr_type – attribute type

  • ref_attr_name – if not None, link this attribute to a function attribute

Returns:

attribute

Classes for the Translater#

BaseEmitter#

class onnx_array_api.translate_api.base_emitter.BaseEmitter[source]#
render_attribute_value(value: Any) Tuple[List[str], str][source]#

Renders an attribute value into a string.

Parameters:

value – value to converter

Returns:

rows to append before, actual value

EventType#

class onnx_array_api.translate_api.base_emitter.EventType(value)[source]#

An enumeration.

InnerEmitter#

class onnx_array_api.translate_api.inner_emitter.InnerEmitter[source]#

Converts event into proper code.

join(rows: List[str], single_line: bool = False) str[source]#

Returns the separators. single_line is unused.

render_attribute_value(value: Any) Tuple[List[str], str][source]#

Renders an attribute value into a string.

Parameters:

value – value to converter

Returns:

rows to append before, actual value

LightEmitter#

class onnx_array_api.translate_api.light_emitter.LightEmitter[source]#

Converts event into proper code.

join(rows: List[str], single_line: bool = False) str[source]#

Join the rows

Translater#

class onnx_array_api.translate_api.translate.Translater(proto: ModelProto | FunctionProto | GraphProto, emitter: LightEmitter | None = None)[source]#

Translates an ONNX graph into a code following the light API.

export(as_str, single_line: bool = False) str | List[str][source]#

Exports into a code.

Parameters:
  • as_str – as a single string or by rows

  • single_line – tries to compress the output into a single line

Returns:

list of instructions

extract_attributes(node: NodeProto) Dict[str, Tuple[AttributeProto, Any]][source]#

Extracts all atributes of a node.

Parameters:

node – node proto

Returns:

dictionary