onnx_ir.convenienceΒΆ

onnx_ir.convenience.convert_attribute(name: str, attr: str | int | float | Sequence[int] | Sequence[float] | Sequence[str] | TensorProtocol | TensorProto | Attr | GraphProtocol | Sequence[GraphProtocol] | GraphProto | TypeProtocol | Sequence[TypeProtocol] | None, attr_type: AttributeType | None = None) AttrΒΆ

Convert a Python object to a _core.Attr object.

This method is useful when constructing nodes with attributes. It infers the attribute type based on the type of the Python value.

Parameters:
  • name – The name of the attribute.

  • attr – The value of the attribute.

  • attr_type – The type of the attribute. This is required when attr is None. When provided, it overrides the inferred type.

Returns:

A Attr object.

Raises:
  • ValueError – If attr is None and attr_type is not provided.

  • TypeError – If the type of the attribute is not supported.

onnx_ir.convenience.convert_attributes(attrs: Mapping[str, str | int | float | Sequence[int] | Sequence[float] | Sequence[str] | TensorProtocol | TensorProto | Attr | GraphProtocol | Sequence[GraphProtocol] | GraphProto | TypeProtocol | Sequence[TypeProtocol] | None]) list[Attr]ΒΆ

Convert a dictionary of attributes to a list of _core.Attr objects.

It infers the attribute type based on the type of the value. The supported types are: int, float, str, Sequence[int], Sequence[float], Sequence[str], _core.Tensor, and _core.Attr:

>>> import onnx_ir as ir
>>> import onnx
>>> import numpy as np
>>> attrs = {
...     "int": 1,
...     "float": 1.0,
...     "str": "hello",
...     "ints": [1, 2, 3],
...     "floats": [1.0, 2.0, 3.0],
...     "strings": ["hello", "world"],
...     "tensor": ir.Tensor(np.array([1.0, 2.0, 3.0])),
...     "tensor_proto":
...         onnx.TensorProto(
...             dims=[3],
...             data_type=onnx.TensorProto.FLOAT,
...             float_data=[1.0, 2.0, 3.0],
...             name="proto",
...         ),
...     "graph": ir.Graph([], [], nodes=[], name="graph0"),
...     "graphs": [ir.Graph([], [], nodes=[], name="graph1"), ir.Graph([], [], nodes=[], name="graph2")],
...     "type_proto": ir.TensorType(ir.DataType.FLOAT),
...     "type_protos": [ir.TensorType(ir.DataType.FLOAT), ir.TensorType(ir.DataType.FLOAT)],
... }
>>> convert_attributes(attrs)
[Attr('int', INT, 1), Attr('float', FLOAT, 1.0), Attr('str', STRING, 'hello'), Attr('ints', INTS, [1, 2, 3]), Attr('floats', FLOATS, [1.0, 2.0, 3.0]), Attr('strings', STRINGS, ['hello', 'world']), Attr('tensor', TENSOR, Tensor<DOUBLE,[3]>(array([1., 2., 3.]), name=None)), Attr('tensor_proto', TENSOR, TensorProtoTensor<FLOAT,[3]>(array([1., 2., 3.], dtype=float32), name='proto')), Attr('graph', INTS, Graph(
    name='graph0',
    inputs=(

    ),
    outputs=(

    ),
    len()=0
)), Attr('graphs', GRAPHS, [Graph(
    name='graph1',
    inputs=(

    ),
    outputs=(

    ),
    len()=0
), Graph(
    name='graph2',
    inputs=(

    ),
    outputs=(

    ),
    len()=0
)]), Attr('type_proto', TYPE_PROTO, Tensor(FLOAT)), Attr('type_protos', TYPE_PROTOS, [Tensor(FLOAT), Tensor(FLOAT)])]
Parameters:

attrs – A dictionary of {<attribute name>: <python objects>} to convert.

Returns:

A list of _core.Attr objects.

onnx_ir.convenience.replace_all_uses_with(values: ValueProtocol | Sequence[ValueProtocol], replacements: ValueProtocol | Sequence[ValueProtocol]) NoneΒΆ

Replace all uses of the given values with the replacements.

This is useful when nodes in the graph are replaced with new nodes, where the old users need to be updated to use the outputs of the new nodes.

For example, suppose we have the following graph:

A -> {B, C}

We want to replace the node A with a new node D:

>>> import onnx_ir as ir
>>> input = ir.Input("input")
>>> node_a = ir.Node("", "A", [input])
>>> node_b = ir.Node("", "B", node_a.outputs)
>>> node_c = ir.Node("", "C", node_a.outputs)
>>> node_d = ir.Node("", "D", [input])
>>> replace_all_uses_with(node_a.outputs, node_d.outputs)
>>> len(node_b.inputs)
1
>>> node_b.inputs[0].producer().op_type
'D'
>>> len(node_c.inputs)
1
>>> node_c.inputs[0].producer().op_type
'D'
>>> len(node_a.outputs[0].uses())
0

When values and replacements are sequences, they are zipped into pairs. All users of the first value is replaced with the first replacement, and so on.

Note

You still need to update the graph outputs if any of the values being replaced are part of the graph outputs. Be sure to remove the old nodes from the graph using graph.remove() if they are no longer needed.

Parameters:
  • values – The value or values to be replaced.

  • replacements – The new value or values to use as inputs.

onnx_ir.convenience.replace_nodes_and_values(graph_or_function: Graph | Function, /, insertion_point: Node, old_nodes: Sequence[Node], new_nodes: Sequence[Node], old_values: Sequence[Value], new_values: Sequence[Value]) NoneΒΆ

Replaces nodes and values in the graph or function.

Parameters:
  • graph_or_function – The graph or function to replace nodes and values in.

  • insertion_point – The node to insert the new nodes after.

  • old_nodes – The nodes to replace.

  • new_nodes – The nodes to replace with.

  • old_values – The values to replace.

  • new_values – The values to replace with.

onnx_ir.convenience.create_value_mapping(graph: Graph) dict[str, Value]ΒΆ

Return a dictionary mapping names to values in the graph.

The mapping does not include values from subgraphs.

Parameters:

graph – The graph to extract the mapping from.

Returns:

A dictionary mapping names to values.