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:
proto – model to translate
single_line – as a single line or not
api – API to export into, default is “light” and this is handle by class
onnx_array_api.translate_api.light_emitter.LightEmitter
, another value is “onnx” which is the inner API implemented in onnx package, “builder” follows the syntax for the classonnx_array_api.graph_api.GraphBuilder
- 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#
EventType#
InnerEmitter#
LightEmitter#
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.