Graph¶
- class onnx_ir.Graph(inputs: Sequence[Value], outputs: Sequence[Value], *, nodes: Iterable[Node], initializers: Sequence[Value] = (), doc_string: str | None = None, opset_imports: dict[str, int] | None = None, name: str | None = None, metadata_props: dict[str, str] | None = None)¶
IR Graph.
Graph represents a computation graph. In addition to the ONNX specification specified fields, it also contains a mapping of
opset_imports
. This allows different subgraphs to import different opsets. It is the responsibility of the deserializer to reconcile the different opsets.The nodes are not guaranteed to be topologically sorted. But the iteration order should be deterministic across different runs. It is the responsibility of the user to maintain a topological order of the nodes.
Note that there is not a
node
attribute in the Graph. The Graph can be seen as a Sequence of nodes and should be used as such. For example, to obtain all nodes as a list, calllist(graph)
.- name¶
The name of the graph.
- inputs¶
The input values of the graph.
- outputs¶
The output values of the graph.
- initializers¶
The initializers in the graph.
- doc_string¶
Documentation string.
- opset_imports¶
Opsets imported by the graph.
- metadata_props¶
Metadata that will be serialized to the ONNX file.
- meta¶
Metadata store for graph transform passes.
- name¶
- append(node: Node, /) None [source]¶
Append a node to the graph in O(1) time.
Unique names will be assigned to the node and its values if any name is
None
.- Parameters:
node – The node to append.
- Raises:
ValueError – If the node belongs to another graph.
- count(value) integer -- return number of occurrences of value ¶
- display(*, page: bool = False) None ¶
Pretty print the object.
- Parameters:
page – Whether to page the output.
- extend(nodes: Iterable[Node], /) None [source]¶
Extend the graph with the given nodes in O(#new_nodes) time.
Unique names will be assigned to the node and its values if any name is
None
.- Parameters:
nodes – The nodes to extend the graph with.
- Raises:
ValueError – If any node belongs to another graph.
- index(value[, start[, stop]]) integer -- return first index of value. ¶
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- property initializers: MutableMapping[str, Value]¶
- property inputs: MutableSequence[Value]¶
- insert_after(node: Node, new_nodes: Iterable[Node] | Node, /) None [source]¶
Insert new nodes after the given node in O(#new_nodes) time.
Unique names will be assigned to the node and its values if any name is
None
.- Parameters:
node – The node to insert after.
new_nodes – The new nodes to insert.
- Raises:
ValueError – If any node belongs to another graph.
- insert_before(node: Node, new_nodes: Iterable[Node] | Node, /) None [source]¶
Insert new nodes before the given node in O(#new_nodes) time.
Unique names will be assigned to the node and its values if any name is
None
.- Parameters:
node – The node to insert before.
new_nodes – The new nodes to insert.
- Raises:
ValueError – If any node belongs to another graph.
- property meta: MetadataStore¶
The metadata store for intermediate analysis.
Write to the
metadata_props
if you would like the metadata to be serialized to the ONNX proto.
- node(index_or_name: int | str, /) Node [source]¶
Get a node by index or name.
This is an O(n) operation. Getting nodes on the ends of the graph (0 or -1) is O(1).
Note
If you need repeated random access, consider turning it into a list with
list(graph)
. Or a dictionary for repeated access by name:{node.name for node in graph}
.When a name is provided and if there are multiple nodes with the same name, the first node with the name is returned.
- Parameters:
index_or_name – The index or name of the node.
- Returns:
The node if found.
- Raises:
IndexError – If the index is out of range.
ValueError – If the node with the given name is not found.
- num_nodes() int [source]¶
Get the number of nodes in the graph in O(1) time.
Note that this method returns the number of nodes this graph directly contains. It does not count nodes in subgraphs.
This is an alias for
len(graph)
. Use this if you prefer a more descriptive name for readability.
- property outputs: MutableSequence[Value]¶
- register_initializer(value: Value) None [source]¶
Register an initializer to the graph.
This is a convenience method to register an initializer to the graph with checks.
- Parameters:
value – The
Value
to register as an initializer of the graph. It must have its.const_value
set.- Raises:
ValueError – If a value of the same name that is not this value is already registered.
ValueError – If the value does not have a name.
ValueError – If the initializer is produced by a node.
ValueError – If the value does not have its
.const_value
set.
- remove(nodes: Node | Iterable[Node], /, safe: bool = False) None [source]¶
Remove nodes from the graph in O(#num of nodes to remove) time.
If any errors are raise, to ensure the graph is not left in an inconsistent state, the graph is not modified.
- Parameters:
nodes – The node to remove.
safe –
If True, performs the following actions before removal:
1. It checks to make sure there are no users of the node that are not to be removed before removing it. 2. It checks the node does not contribute to any graph outputs. 3. It removes references to all inputs so it is no longer a user of other nodes.
- Raises:
ValueError – If any node to remove does not belong to this graph.
ValueError – (When
safe=True
) If the node does not belong to this graph or if there are users of the node.ValueError – (When
safe=True
) If the node is still being used by other nodes not to be removed.
- sort() None [source]¶
Perform a topological sort of this graph and all subgraphs in O(#nodes + #values) time.
This sort is stable. It preserves the original order as much as possible.
Reference: https://github.com/madelson/MedallionTopologicalSort#stable-sort
- Raises:
ValueError – If the graph contains a cycle, making topological sorting impossible.