Value¶
- class onnx_ir.Value(producer=None, *, index=None, name=None, shape=None, type=None, doc_string=None, const_value=None, metadata_props=None)¶
IR Value.
A value is a named entity that can be used to represent an input or output of a graph, a function, or a node. The information it stores generalizes over
ValueInfoProtoin the ONNX specification.A
Valueis always not owned or owned by exactly one node. When the value is not owned, it must be an input of a graph or a function.producerandindexareNone.When the value is owned by a node, it is an output of the node. The node that produces the value can be accessed with
producer(). The index of the output of the node that produces the value can be accessed withindex().To find all the nodes that use this value as an input, call
uses(). Consuming nodes can be obtained withconsumers().To check if the value is an is an input, output or initializer of a graph, use
is_graph_input(),is_graph_output()oris_initializer().Use
graphto get the graph that owns the value.- Parameters:
- doc_string¶
- property const_value: TensorProtocol | None¶
The backing constant tensor for the value.
If the
Valuehas aconst_valueand is part of a graph initializers dictionary, the value is an initialized value. Itsconst_valuewill appear as aninitializerin the GraphProto when serialized.If the
Valueis not part of a graph initializers dictionary, theconst_valuefield will be ignored during serialization.const_valuecan be backed by different raw data types, such as numpy arrays. The only guarantee is that it conforms TensorProtocol.
- display(*, page=False)¶
Pretty print the object.
- Parameters:
page (bool) – Whether to page the output.
- Return type:
None
- property graph: Graph | None¶
Return the graph that defines this value.
When the value is an input/output/initializer of a graph, the owning graph is that graph. When the value is an output of a node, the owning graph is the graph that the node belongs to. When the value is not owned by any graph, it returns
None.
- 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 value.
The metadata properties are used to store additional information about the value. Unlike
meta, this property is serialized to the ONNX proto.
- producer()[source]¶
The node that produces this value.
When producer is
None, the value does not belong to a node, and is typically a graph input or an initializer. You can usegraph`()to find the graph that owns this value. Useis_graph_input(),is_graph_output()oris_initializer()to check if the value is an input, output or initializer of a graph.- Return type:
Node | None
- replace_all_uses_with(replacement, /, replace_graph_outputs=False)[source]¶
Replace all uses of this value with another value.
Tip
Handling graph outputs
To also replace graph outputs that reference the values being replaced, either set
replace_graph_outputsto True, or manually update the graph outputs before calling this function to avoid an error being raised whenreplace_graph_outputs=False.Be careful when a value appears multiple times in the graph outputs - this is invalid. An identity node will need to be added on each duplicated outputs to ensure a valid ONNX graph.
You may also want to assign the name of this value to the replacement value to maintain the name when it is a graph output.
To replace usage of a sequence of values with another sequence of values, consider using
onnx_ir.convenience.replace_all_uses_with().Added in version 0.1.12.
- Parameters:
- Raises:
ValueError – When
replace_graph_outputsis False && when the value to replace is a graph output.- Return type:
None
- property type: TypeProtocol | None¶
The type of the tensor.
Example types can be
TensorType,SparseTensorType,SequenceType,OptionalType. To obtain the data type of the tensor, usetype.dtypeor convenientlydtype.
- uses()[source]¶
Return a set of uses of the value.
The set contains tuples of
(Node, index)where the index is the index of the input of the node. For example, ifnode.inputs[1] == value, then the use is(node, 1).- Return type:
Collection[Usage]