onnx_ir.tapeΒΆ

Taping module to facilitate building IR graphs.

The onnx_ir.tape module provides utilities for recording nodes and initializers to construct computational graphs or functions.

The Tape classΒΆ

The Tape class is a recorder that collects nodes and initializers created during the construction of a graph or function. It supports creating nodes with single or multiple outputs and registering initializers.

class onnx_ir.tape.Tape(graph_like=None)ΒΆ

Tape class.

A tape is a recorder that collects nodes and initializers that are created so that they can be used for creating a graph.

Example:

>>> import onnx_ir as ir

>>> tape = ir.tape.Tape()
>>> a = tape.initializer(ir.tensor([1.0, 2.0, 3.0], name="a"))
>>> b: ir.Value = ir.val("b", dtype=ir.DataType.FLOAT, shape=(3,))
>>> c: ir.Value = ir.val("c", dtype=ir.DataType.FLOAT, shape=(3,))
>>> x = tape.op("Add", [a, b])
>>> y = tape.op("Elu", [x, c], attributes={"alpha": 2.0})
>>> y.shape = ir.Shape((3,))
>>> y.dtype = ir.DataType.FLOAT
>>> model = ir.Model(
...     ir.Graph(
...         inputs=[b, c],
...         outputs=[y],
...         nodes=tape.nodes,
...         initializers=tape.initializers,
...         opset_imports={"": 20},
...         name="main_graph",
...     ),
...     ir_version=10,
... )
>>> print(model)  
<
    ir_version=10,
    opset_imports={'': 20},
    producer_name=None,
    producer_version=None,
    domain=None,
    model_version=None,
>
graph(
    name=main_graph,
    inputs=(
        %"b"<FLOAT,[3]>,
        %"c"<FLOAT,[3]>
    ),
    outputs=(
        %"val_1"<FLOAT,[3]>
    ),
    initializers=(
        %"a"<FLOAT,[3]>{Tensor<FLOAT,[3]>(array([1., 2., 3.], dtype=float32), name='a')}
    ),
) {
    0 |  # node_Add_0
         %"val_0"<?,?> ⬅️ ::Add(%"a"{[1.0, 2.0, 3.0]}, %"b")
    1 |  # node_Elu_1
         %"val_1"<FLOAT,[3]> ⬅️ ::Elu(%"val_0", %"c") {alpha=2.0}
    return %"val_1"<FLOAT,[3]>
}
Parameters:

graph_like (ir.Graph | ir.Function | None)

graph_likeΒΆ

The graph to append the new nodes and initializers to. When it is None, the nodes and initializers are creating without owned by a graph. Initializers will not be added to functions because it is not supported by ONNX.

initializer(tensor, name=None)[source]ΒΆ
Parameters:
Return type:

Value

property initializers: Sequence[Value]ΒΆ
property nodes: Sequence[Node]ΒΆ
op(op_type, inputs, attributes=None, *, domain='', overload='', version=None, graph=None, name=None, doc_string=None, metadata_props=None, output=None)[source]ΒΆ
Parameters:
Return type:

Value

op_multi_out(op_type, inputs, attributes=None, *, num_outputs=None, outputs=None, domain='', overload='', version=None, graph=None, name=None, doc_string=None, metadata_props=None)[source]ΒΆ
Parameters:
Return type:

Sequence[Value]

property used_opsets: set[tuple[str, int | None]]ΒΆ