Node¶
- class onnx_ir.Node(domain, op_type, inputs, attributes=(), *, overload='', num_outputs=None, outputs=None, version=None, graph=None, name=None, doc_string=None, metadata_props=None, device_configurations=())¶
IR Node.
Tip
For a more convenient way (that supports Python objects as attributes) to create a node, use the
onnx_ir.node()constructor.If
graphis provided, the node will be added to the graph. Otherwise, the user is responsible for callinggraph.append(node)(or other mutation methods inGraph) to add the node to the graph.After the node is initialized, it will add itself as a user of its input values.
The output values of the node are created during node initialization and are immutable. To change the output values, create a new node and, for each use of the old outputs (
output.uses()), replace the input in the consuming node by callingreplace_input_with(). You can also use thereplace_all_uses_with()method to replace all uses of the output values.Note
When the
domainis"ai.onnx", it is normalized to"".- Parameters:
- device_configurations¶
- doc_string¶
- append(nodes)[source]¶
Insert a node after this node in the list of nodes in the graph.
It is the same as calling
graph.insert_after(self, nodes).Example:
Before: previous_node -> self previous_node' -> node -> next_node' After: previous_node -> self -> node previous_node' -> next_node'
- property attributes: Attributes¶
The attributes of the node as
dict[str, Attr]with additional access methods.Use it as a dictionary with keys being the attribute names and values being the
Attrobjects.Use
node.attributes.add(attr)to add an attribute to the node. Usenode.attributes.get_int(name, default)to get an integer attribute value. Refer to theAttributesfor more methods.
- display(*, page=False)[source]¶
Pretty print the node.
This method is used for debugging and visualization purposes.
- Parameters:
page (bool)
- Return type:
None
- property graph: Graph | None¶
The graph that the node belongs to.
If the node is not added to any graph, this property is None.
- property inputs: Sequence[Value | None]¶
The input values of the node.
The inputs are immutable. To change the inputs, create a new node and replace the inputs of the using nodes of this node’s outputs by calling
replace_input_with()on the using nodes of this node’s outputs.
- property meta: MetadataStore¶
The metadata store for intermediate analysis.
Write to the
metadata_propsif you would like the metadata to be serialized to the ONNX proto.
- property metadata_props: dict[str, str]¶
The metadata properties of the node.
The metadata properties are used to store additional information about the node. Unlike
meta, this property is serialized to the ONNX proto.
- op_identifier()[source]¶
Return the operator identifier of the node.
The operator identifier is a tuple of the domain, op_type and overload.
- property outputs: Sequence[Value]¶
The output values of the node.
The outputs are always attached to this node once initialized (immutable), except that the list can be resized to remove or add outputs.
Use
resize_outputs()to change the number of outputs of the node.
- predecessors()[source]¶
Return the predecessor nodes of the node, deduplicated, in a deterministic order.
- prepend(nodes)[source]¶
Insert a node before this node in the list of nodes in the graph.
It is the same as calling
graph.insert_before(self, nodes).Example:
Before: previous_node -> self previous_node' -> node -> next_node' After: previous_node -> node -> self previous_node' -> next_node'
- resize_inputs(new_size, /)[source]¶
Resize the inputs of the node.
If the new size is greater than the current size, new inputs are added as None. If the new size is less than the current size, the extra inputs are removed.
After
inputsis resized, you can usereplace_input_with()to set the new inputs.Added in version 0.1.13.
- Parameters:
new_size (int) – The new number of inputs.
- Return type:
None
- resize_outputs(new_size, /)[source]¶
Resize the outputs of the node.
If the new size is greater than the current size, new output values are created. If the new size is less than the current size, the extra output values are removed. The removed output values must not have any uses.
Added in version 0.1.13.
- Parameters:
new_size (int) – The new number of outputs.
- Raises:
ValueError – If the new size is less than the current size and the removed outputs have uses.
- Return type:
None
- set_pipeline_stage(configuration, stage)[source]¶
Assign this node to a pipeline
stageforconfiguration.This expresses pure device placement / pipeline parallelism — for example putting a contiguous block of decoder layers on one device. Unlike
shard(), it attaches no sharding spec; the node is placed as a whole.If the node already has a
NodeDeviceConfigurationforconfiguration(for example one created byshard()), itspipeline_stageis updated in place; otherwise a new placement-only configuration is created. Calling this method again replaces the stage.How a stage maps to a physical device is by convention: a common choice is
stage == device indexintoconfiguration.device_names.Added in version 1.0.0.
- Parameters:
configuration (ModelConfiguration) – The
ModelConfigurationthe stage belongs to.stage (int) – The pipeline stage index. Must be
>= 0.
- Raises:
ValueError – If
stageis negative.- Return type:
None
- shard(value, *, configuration, axis, num_shards, device_indices=(), pipeline_stage=None)[source]¶
Record a sharding of
valuealongaxisfor this node.This is a convenience wrapper that builds and attaches the appropriate multi-device configuration metadata in
device_configurations. The sharding is bound tovalueandconfigurationby object identity, so it follows renames and survives as long as the objects do.Calling this method repeatedly for the same
valueandconfigurationbut different axes builds a singleShardingSpecwith oneShardedDimper axis (the canonical representation for sharding a tensor across a multi-axis device mesh).device_indicesare unioned across those calls.Added in version 1.0.0.
- Parameters:
value (Value) – The input or output value to shard. Must be one of this node’s own inputs or outputs.
configuration (ModelConfiguration) – The
ModelConfigurationthe sharding belongs to.axis (int) – The axis along which
valueis sharded. May be negative to count from the back (in the range[-rank, rank)following the ONNX convention), when the rank ofvalueis known.num_shards (int) – The number of shards along
axis. Must be>= 1.device_indices (Sequence[int]) – Optional indices (into
configuration.device_names) of the devices the tensor is placed on. These are device indices, not the device names passed toModel.add_device_configuration().pipeline_stage (int | None) – Optional pipeline stage for the configuration. Must be
>= 0and must not conflict with a stage already set on the configuration.
- Raises:
ValueError – If
valueis not an input or output of the node, ifnum_shards < 1, ifpipeline_stageis negative, ifaxisis out of range when the rank ofvalueis known, ifvalueis already sharded alongaxisfor thisconfiguration, or ifpipeline_stageconflicts with the configuration’s existing stage.- Return type:
None
- sharding_of(value)[source]¶
Return all sharding specs on this node that target
value.Matching is by object identity, so this returns the live specs that reference exactly
value.Added in version 1.0.0.
- Parameters:
value (Value)
- Return type:
tuple[ShardingSpec, …]
- successors()[source]¶
Return the successor nodes of the node, deduplicated, in a deterministic order.
- property version: int | None¶
Opset version of the operator called.
If
None, the version is unspecified and will follow that of the graph. This property is special to ONNX IR to allow mixed opset usage in a graph for supporting more flexible graph transformations. It does not exist in the ONNX serialization (protobuf) spec.