onnxscript.ir¶
In-memory intermediate representation for ONNX graphs.
- class onnxscript.ir.ArrayCompatible(*args, **kwargs)¶
Protocol for array-like objects.
An example of an array-like object is a numpy ndarray or a PyTorch Tensor. Read more at https://numpy.org/devdocs/user/basics.interoperability.html
- class onnxscript.ir.Attr(name: str, type: AttributeType, value: Any, *, doc_string: str | None = None)¶
Base class for ONNX attributes.
- as_tensor() TensorProtocol [source]¶
Get the attribute value as a tensor.
- as_tensors() Sequence[TensorProtocol] [source]¶
Get the attribute value as a sequence of tensors.
- doc_string¶
- name¶
- type¶
- value¶
- onnxscript.ir.AttrFloat32(name: str, value: float, doc_string: str | None = None) Attr ¶
Create a float attribute.
- onnxscript.ir.AttrFloat32s(name: str, value: Sequence[float], doc_string: str | None = None) Attr ¶
Create a float sequence attribute.
- onnxscript.ir.AttrGraph(name: str, value: Graph, doc_string: str | None = None) Attr ¶
Create a graph attribute.
- onnxscript.ir.AttrGraphs(name: str, value: Sequence[Graph], doc_string: str | None = None) Attr ¶
Create a graph sequence attribute.
- onnxscript.ir.AttrInt64(name: str, value: int, doc_string: str | None = None) Attr ¶
Create an int attribute.
- onnxscript.ir.AttrInt64s(name: str, value: Sequence[int], doc_string: str | None = None) Attr ¶
Create an int sequence attribute.
- onnxscript.ir.AttrSparseTensor(name: str, value: SparseTensorProtocol, doc_string: str | None = None) Attr ¶
Create a sparse tensor attribute.
- onnxscript.ir.AttrSparseTensors(name: str, value: Sequence[SparseTensorProtocol], doc_string: str | None = None) Attr ¶
Create a sparse tensor sequence attribute.
- onnxscript.ir.AttrString(name: str, value: str, doc_string: str | None = None) Attr ¶
Create a str attribute.
- onnxscript.ir.AttrStrings(name: str, value: Sequence[str], doc_string: str | None = None) Attr ¶
Create a string sequence attribute.
- onnxscript.ir.AttrTensor(name: str, value: TensorProtocol, doc_string: str | None = None) Attr ¶
Create a tensor attribute.
- onnxscript.ir.AttrTensors(name: str, value: Sequence[TensorProtocol], doc_string: str | None = None) Attr ¶
Create a tensor sequence attribute.
- onnxscript.ir.AttrTypeProto(name: str, value: TypeAndShape, doc_string: str | None = None) Attr ¶
Create a type attribute.
- onnxscript.ir.AttrTypeProtos(name: str, value: Sequence[TypeAndShape], doc_string: str | None = None) Attr ¶
Create a type sequence attribute.
- class onnxscript.ir.AttributeProtocol(*args, **kwargs)¶
Protocol for ONNX attributes.
- type¶
The type of the attribute.
- Type:
_enums.AttributeType
- value¶
The value of the attribute.
- Type:
Any
- type: _enums.AttributeType¶
- value: Any¶
- class onnxscript.ir.AttributeType(value)¶
Enum for the types of ONNX attributes.
- FLOAT = 1¶
- FLOATS = 6¶
- GRAPH = 5¶
- GRAPHS = 10¶
- INT = 2¶
- INTS = 7¶
- SPARSE_TENSOR = 11¶
- SPARSE_TENSORS = 12¶
- STRING = 3¶
- STRINGS = 8¶
- TENSOR = 4¶
- TENSORS = 9¶
- TYPE_PROTO = 13¶
- TYPE_PROTOS = 14¶
- UNDEFINED = 0¶
- class onnxscript.ir.DLPackCompatible(*args, **kwargs)¶
Protocol for objects that can support dlpack.
Computation backends can call __dlpack__ to obtain the underlying data in a tensor without copying the data. This allows use to use tensorflow tensors etc. without copying the data.
- class onnxscript.ir.DataType(value)¶
Enum for the data types of ONNX tensors, defined in
onnx.TensorProto
.- BFLOAT16 = 16¶
- BOOL = 9¶
- COMPLEX128 = 15¶
- COMPLEX64 = 14¶
- DOUBLE = 11¶
- FLOAT = 1¶
- FLOAT16 = 10¶
- FLOAT4E2M1 = 23¶
- FLOAT8E4M3FN = 17¶
- FLOAT8E4M3FNUZ = 18¶
- FLOAT8E5M2 = 19¶
- FLOAT8E5M2FNUZ = 20¶
- INT16 = 5¶
- INT32 = 6¶
- INT4 = 22¶
- INT64 = 7¶
- INT8 = 3¶
- STRING = 8¶
- UINT16 = 4¶
- UINT32 = 12¶
- UINT4 = 21¶
- UINT64 = 13¶
- UINT8 = 2¶
- UNDEFINED = 0¶
- class onnxscript.ir.ExternalTensor(location: PathLike | str, offset: int | None, length: int | None, dtype: DataType, *, shape: Shape, name: str, doc_string: str | None = None, metadata_props: dict[str, str] | None = None, base_dir: PathLike | str = '')¶
An immutable concrete tensor with its data store on disk.
This class uses memory mapping to avoid loading the tensor into memory, when the data type is supported by numpy. Otherwise, the tensor is loaded into memory lazily when accessed.
Calling
shape
does not incur IO. Checking shape before loading the tensor is recommended if IO overhead and memory usage is a concern.To obtain an array, call
numpy()
. To obtain the bytes, calltobytes()
.The
location
must be a relative path conforming to the ONNX specification. Given the correctbase_dir
, thepath
is computed to be the full path to the data file. Users should expect that thepath
always leads to the correct file. At initialization, paths are not checked. It is the user’s responsibility to ensure the paths are valid and accessible.- location¶
The location of the data file. It is the path relative to the base directory.
- base_dir¶
The base directory for the external data. It is used to resolve relative paths. At serialization, only the
location
is serialized into the “location” field of theTensorProto
.
- offset¶
The offset in bytes from the start of the file.
- length¶
The length of the data in bytes.
- dtype¶
The data type of the tensor.
- shape¶
The shape of the tensor.
- name¶
The name of the tensor. It must be specified.
- doc_string¶
The documentation string.
- metadata_props¶
The metadata properties.
- doc_string¶
- 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.
- name¶
- numpy() ndarray [source]¶
Return the tensor as a numpy array.
The data will be memory mapped into memory and will not taken up physical memory space.
- raw¶
- class onnxscript.ir.Function(domain: str, name: str, overload: str = '', *, graph: Graph, attributes: Sequence[Attr], metadata_props: dict[str, str] | None = None)¶
IR functions.
Like a graph, a function can have nodes that are not topologically sorted. 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 Function. The Function can be seen as a Sequence of nodes and should be used as such. For example, to obtain all nodes as a list, calllist(function)
.- name¶
The function name.
- domain¶
The domain this function is defined in.
- overload¶
The overload name when the function is overloaded.
- inputs¶
The input values of the function.
- attributes¶
The attributes this function defines.
- outputs¶
The output values of the function.
- opset_imports¶
Opsets imported by the function.
- doc_string¶
Documentation string.
- metadata_props¶
Metadata that will be serialized to the ONNX file.
- meta¶
Metadata store for graph transform passes.
- property attributes: OrderedDict[str, Attr]¶
- extend(nodes: Iterable[Node], /) None [source]¶
Extend the function with the given nodes in O(#new_nodes) time.
- insert_after(node: Node, new_nodes: Iterable[Node], /) None [source]¶
Insert new nodes after the given node in O(#new_nodes) time.
- insert_before(node: Node, new_nodes: Iterable[Node], /) None [source]¶
Insert new nodes before the given node in O(#new_nodes) time.
- 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.
- remove(nodes: Node | Iterable[Node], /, safe: bool = False) None [source]¶
Remove nodes from the graph in O(#num of nodes) 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.
- class onnxscript.ir.FunctionProtocol(*args, **kwargs)¶
Protocol for ONNX functions.
Like a graph, a function can have nodes that are not topologically sorted. 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 Function. The Function can be seen as a Sequence of nodes and should be used as such. For example, to obtain all nodes as a list, calllist(function)
.- inputs¶
The input values of the function.
- Type:
Sequence[ValueProtocol]
- attributes¶
The attributes this function defines.
- Type:
OrderedDict[str, AttributeProtocol]
- outputs¶
The output values of the function.
- Type:
Sequence[ValueProtocol]
- append(node: NodeProtocol, /) None [source]¶
Append a node to the function.
- attributes: OrderedDict[str, AttributeProtocol]¶
- extend(nodes: Iterable[NodeProtocol], /) None [source]¶
Extend the function with the given nodes.
- inputs: Sequence[ValueProtocol]¶
- insert_after(node: NodeProtocol, new_nodes: Iterator[NodeProtocol], /) None [source]¶
Insert new nodes after the given node.
- insert_before(node: NodeProtocol, new_nodes: Iterator[NodeProtocol], /) None [source]¶
Insert new nodes before the given node.
- outputs: Sequence[ValueProtocol]¶
- remove(node: NodeProtocol, /) None [source]¶
Remove a node from the function.
- class onnxscript.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.
- 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.
- 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.
- 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.
- name¶
- 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.
- 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.
Referece: https://github.com/madelson/MedallionTopologicalSort#stable-sort
- Raises:
ValueError – If the graph contains a cycle, making topological sorting impossible.
- class onnxscript.ir.GraphProtocol(*args, **kwargs)¶
Protocol for graphs.
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)
.- inputs¶
The input values of the graph.
- Type:
MutableSequence[ValueProtocol]
- outputs¶
The output values of the graph.
- Type:
MutableSequence[ValueProtocol]
- initializers¶
The initializers in the graph.
- Type:
MutableMapping[str, ValueProtocol]
- append(node: NodeProtocol, /) None [source]¶
Append a node to the graph.
- extend(nodes: Iterable[NodeProtocol], /) None [source]¶
Extend the graph with the given nodes.
- initializers: MutableMapping[str, ValueProtocol]¶
- inputs: MutableSequence[ValueProtocol]¶
- insert_after(node: NodeProtocol, new_nodes: Iterator[NodeProtocol], /) None [source]¶
Insert new nodes after the given node.
- insert_before(node: NodeProtocol, new_nodes: Iterator[NodeProtocol], /) None [source]¶
Insert new nodes before the given node.
- outputs: MutableSequence[ValueProtocol]¶
- remove(node: NodeProtocol, /) None [source]¶
Remove a node from the graph.
- class onnxscript.ir.GraphView(inputs: Sequence[Value], outputs: Sequence[Value], *, nodes: Iterable[Node], initializers: Sequence[ValueProtocol] = (), doc_string: str | None = None, opset_imports: dict[str, int] | None = None, name: str | None = None, metadata_props: dict[str, str] | None = None)¶
A read-only view on a graph.
The GraphView is useful for analysis of a subgraph. It can be initialized with a subset of nodes from a
Graph
. Creating GraphView does not change the ownership of the nodes, and so it is possible to create multiple GraphViews that contain the same nodes. If the underlying nodes / connections are mutated, the mutation will be reflected in all views as well.The graph view can be serialized to ONNX:
graph_proto = ir.serde.serialize_graph(graph_view)
It can also be used to create a model:
model = ir.Model(graph_view, ir_version=8) model_proto = ir.serde.serialize_model(model)
The model created with a GraphView will have a fixed topology, and its graph will remain read-only as a GraphView. No copying will be done during the initialization process.
- 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.
- doc_string¶
- initializers¶
- inputs¶
- 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.
- name¶
- nodes¶
- opset_imports¶
- outputs¶
- class onnxscript.ir.GraphViewProtocol(*args, **kwargs)¶
Protocol for a read-only view on a graph.
The GraphView is useful for analysis of a subgraph. It can be initialized with a subset of nodes from a
Graph
. Creating GraphView does not change the ownership of the nodes, and so it is possible to create multiple GraphViews that contain the same nodes.- inputs¶
The input values of the graph.
- Type:
Sequence[ValueProtocol]
- outputs¶
The output values of the graph.
- Type:
Sequence[ValueProtocol]
- initializers¶
The initializers in the graph.
- Type:
Mapping[str, ValueProtocol]
- initializers: Mapping[str, ValueProtocol]¶
- inputs: Sequence[ValueProtocol]¶
- outputs: Sequence[ValueProtocol]¶
- onnxscript.ir.Input(name: str | None = None, shape: Shape | None = None, type: TypeProtocol | None = None, doc_string: str | None = None) Value ¶
Create an input of a Graph or a Function.
This is equivalent to calling
Value(name=name, shape=shape, type=type, doc_string=doc_string)
.
- class onnxscript.ir.MapTypeProtocol(*args, **kwargs)¶
Protocol for ONNX map types.
TODO: This protocol is not yet implemented in the ONNX IR.
- key_type: Literal[_enums.DataType.STRING, _enums.DataType.INT64, _enums.DataType.INT32, _enums.DataType.INT16, _enums.DataType.INT8, _enums.DataType.UINT64, _enums.DataType.UINT32, _enums.DataType.UINT16, _enums.DataType.UINT8]¶
- value_type: _enums.DataType¶
- class onnxscript.ir.Model(graph: Graph, *, ir_version: int, producer_name: str | None = None, producer_version: str | None = None, domain: str | None = None, model_version: int | None = None, doc_string: str | None = None, functions: Sequence[Function] = (), meta_data_props: dict[str, str] | None = None)¶
- doc_string¶
- domain¶
- graph¶
- ir_version¶
- 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.
- model_version¶
- producer_name¶
- producer_version¶
- class onnxscript.ir.ModelProtocol(*args, **kwargs)¶
Protocol for models.
A model is a container for a graph and metadata. It is the top-level object that represents an ONNX model.
- graph¶
The graph of the model.
- Type:
- functions¶
The functions defined in the model.
- Type:
MutableMapping[str, FunctionProtocol]
- functions: MutableMapping[str, FunctionProtocol]¶
- graph: GraphProtocol¶
- class onnxscript.ir.Node(domain: str, op_type: str, inputs: Iterable[Value | None], attributes: Iterable[Attr | RefAttr] = (), *, overload: str = '', num_outputs: int | None = None, outputs: Sequence[Value] | None = None, version: int | None = None, graph: Graph | None = None, name: str | None = None, doc_string: str | None = None, metadata_props: dict[str, str] | None = None)¶
IR Node.
If the
graph
is provided, the node will be added to the graph. Otherwise, user is responsible to callgraph.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 the 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 replace the each of the inputs of
output.uses()
with the new output values by callingreplace_input_with()
on the using nodes of this node’s outputs.- append(nodes: Node | Iterable[Node]) None [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'
- Parameters:
nodes – A node or a sequence of nodes to put after this node.
- property attributes: OrderedDict[str, Attr | RefAttr]¶
- display(*, page: bool = False) None [source]¶
Pretty print the object.
- Parameters:
page – Whether to page the output.
- doc_string¶
- 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.
- prepend(nodes: Node | Iterable[Node]) None [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'
- Parameters:
nodes – A node or a sequence of nodes to put before this node.
- class onnxscript.ir.NodeProtocol(*args, **kwargs)¶
Protocol for nodes.
A node represents an invocation of an operation on the
Value
s in the computational graph.A node can be optionally named. A name should typically be assigned when the node is added to a graph.
domain
,op_type
, andoverload
together uniquely identify the operator, and are always strings. For ONNX operators,domain
andoverload
are both empty strings.inputs
andoutputs
are the input and output values of the node.attributes
are the attributes of the node. The attributes are stored in an ordered dictionary to preserve the order of the attributes. This is a deviation from the current ONNX spec where attributes are unordered, but it is helpful for tools that rely on the order of the attributes, e.g. those converting to and from Python function keyword arguments.version
is unique to the IR and is not specified in the ONNX spec. This allows the IR to represent a graph with mixed opset versions. Deserializers should decide how to reconcile the different versions within the graph. A typical graph will have a single version, declared in theGraph
object and the nodes will haveNone
as the version.- inputs¶
Input values.
- Type:
Sequence[ValueProtocol]
- outputs¶
Output values.
- Type:
Sequence[ValueProtocol]
- attributes¶
The attributes of the operator.
- Type:
OrderedDict[str, AttributeProtocol | ReferenceAttributeProtocol]
- attributes: OrderedDict[str, AttributeProtocol | ReferenceAttributeProtocol]¶
- inputs: Sequence[ValueProtocol]¶
- outputs: Sequence[ValueProtocol]¶
- class onnxscript.ir.OptionalType(elem_type: TypeProtocol, *, denotation: str | None = None)¶
A type that represents an optional element.
- class onnxscript.ir.RefAttr(name: str, ref_attr_name: str, type: AttributeType, *, doc_string: str | None = None)¶
Reference attribute.
- property type: AttributeType¶
- class onnxscript.ir.ReferenceAttributeProtocol(*args, **kwargs)¶
Protocol for a reference attribute.
A reference attribute can only appear inside the definition body of a function.
- type¶
The type of the attribute.
- type: AttributeType¶
- class onnxscript.ir.SequenceType(elem_type: TypeProtocol, *, denotation: str | None = None)¶
A type that represents a sequence of elements.
- class onnxscript.ir.Shape(dims: Iterable[int | SupportsInt | SymbolicDim | str | None], /, denotations: Iterable[str | None] | None = None, frozen: bool = False)¶
-
- property dims: tuple[int | SymbolicDim, ...]¶
All dimensions in the shape.
This property is read-only. Use __getitem__ and __setitem__ to modify the shape or create a new shape.
- get_denotation(index: int) str | None [source]¶
Return the denotation of the dimension at the index.
- Parameters:
index – The index of the dimension.
- Returns:
The denotation of the dimension.
- class onnxscript.ir.ShapeProtocol(*args, **kwargs)¶
Protocol for ONNX shapes.
A shape is a sequence of dimensions.
- dims¶
The dimensions of the shape.
- Type:
Sequence[int | SymbolicDimProtocol]
- dims: Sequence[int | SymbolicDimProtocol]¶
- class onnxscript.ir.SparseTensorProtocol(*args, **kwargs)¶
-
- indices: TensorProtocol¶
- values: TensorProtocol¶
- class onnxscript.ir.SparseTensorType(dtype: DataType, *, denotation: str | None = None)¶
A type that represents a sparse tensor.
- class onnxscript.ir.StringTensor(value: Sequence[bytes] | npt.NDArray[np.bytes_], *, shape: Shape | None = None, name: str | None = None, doc_string: str | None = None, metadata_props: dict[str, str] | None = None)¶
Multidimensional array of strings (as binary data to match the string_data field in TensorProto).
- doc_string¶
- 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.
- name¶
- class onnxscript.ir.SymbolicDimProtocol(*args, **kwargs)¶
Value of a single symbolic/dynamic dimension in a shape.
- class onnxscript.ir.Tensor(value: TArrayCompatible, dtype: DataType | None = None, *, shape: Shape | None = None, name: str | None = None, doc_string: str | None = None, metadata_props: dict[str, str] | None = None)¶
An immutable concrete tensor.
This class is a wrapper around the raw tensor data. The raw tensor data can be a numpy array compatible object (e.g.
np.ndarray
,torch.Tensor
) or aDLPack
compatible object. The tensor is immutable and the data is not copied at initialization.To create a tensor from a numpy array:
>>> import numpy as np >>> array = np.array([1, 2, 3]) >>> tensor = Tensor(array) >>> # The tensor itself can be treated as a numpy array because it implements the __array__ method >>> np.allclose(tensor, array) True
To get a numpy array from the tensor, call
numpy()
. To convert the tensor to a byte string for serialization, calltobytes()
.It is recommended to check the size of the tensor first before accessing the underlying data, because accessing the data may be expensive and incur IO overhead.
Subclass this class to efficiently handle different types of tensors from different frameworks.
- name¶
The name of the tensor.
- shape¶
The shape of the tensor.
- dtype¶
The data type of the elements of the tensor. It is an
ir.DataType
enum.
- doc_string¶
Documentation string.
- raw¶
The raw data behind this tensor. It can be anything.
- size¶
The number of elements in the tensor.
- nbytes¶
The number of bytes in the tensor.
- metadata_props¶
Metadata that will be serialized to the ONNX file.
- meta¶
Metadata store for graph transform passes.
- doc_string¶
- 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.
- name¶
- numpy() ndarray [source]¶
Return the tensor as a numpy array.
When the data type is not supported by numpy, the dtypes from the
ml_dtype
package are used. The values can be reinterpreted as bit representations using the.view()
method.
- property raw: TArrayCompatible¶
Backing data of the tensor. Immutable.
- class onnxscript.ir.TensorProtoTensor(proto: TensorProto)¶
A tensor initialized from a tensor proto.
- 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.
- numpy() ndarray [source]¶
Return the tensor as a numpy array.
This is an improved version of onnx.numpy_helper.to_array. It first reads the data using the dtype corresponding to the tensor proto data field, then converts it to the correct dtype and shape. Special cases are bfloat16, complex and int4 where we need to reinterpret the data. Other types can simply be casted.
When the data type is not supported by numpy, the dtypes from the
ml_dtype
package are used. The values can be reinterpreted as bit representations using the.view()
method.When the data type is a string, this method returns a numpy array of bytes instead of a numpy array of strings, to follow the ONNX specification.
External tensors are not supported by this class. Use
onnxscript.ir.ExternalTensor
instead.- Raises:
ValueError – If the data type is UNDEFINED.
- property raw: TensorProto¶
- tobytes() bytes [source]¶
Return the tensor as a byte string conformed to the ONNX specification, in little endian.
- Raises:
ValueError – If the tensor is a string tensor or an external tensor.
ValueError – If the tensor is of UNDEFINED data type.
- class onnxscript.ir.TensorProtocol(*args, **kwargs)¶
Concrete tensor backed by data.
The protocol does not specify how the data is stored. That data is exposed through the
raw
attribute for examination, but accessingraw
is typically not needed.To use the tensor as a numpy array, call
numpy()
. To convert the tensor to a byte string for serialization, calltobytes()
.It is recommended to check the size of the tensor first before accessing the underlying data, because accessing the data may be expensive and incur IO overhead.
- shape¶
The shape of the tensor.
- Type:
- dtype¶
The data type of the elements of the tensor. It is an
ir.DataType
enum.- Type:
_enums.DataType
- raw¶
The raw data behind this tensor. It can be anything.
- Type:
Any
- size¶
The number of elements in the tensor.
- nbytes¶
The number of bytes in the tensor.
- dtype: _enums.DataType¶
- raw: Any¶
- shape: ShapeProtocol¶
- class onnxscript.ir.TensorType(dtype: DataType, *, denotation: str | None = None)¶
A type that represents a tensor.
- class onnxscript.ir.TypeAndShape(type: TypeProtocol | None, shape: Shape | None)¶
Type and shape.
Useful for constructing a type proto.
- type: TypeProtocol | None¶
- class onnxscript.ir.TypeProtocol(*args, **kwargs)¶
Protocol for ONNX tensors, Sequence tensors, Optional tensors and Sparse tensors.
These three types of tensors share the same attribute “elem_type” so they are merged in the same interface. Unlike the ONNX TensorProto, shapes are not included in the type and should be stored in the
Value
.- denotation¶
An optional denotation can be used to denote the whole type with a standard semantic description as to what is stored inside. Refer to https://github.com/onnx/onnx/blob/main/docs/TypeDenotation.md#type-denotation-definition for pre-defined type denotations.
- Type:
str | None
- elem_type¶
The type of its elements for nested types like Sequence[Optional] tensors. Or the DataType if the type is not nested.
- dtype¶
The data type of the tensor or the nested tensor.
- Type:
- elem_type: TypeProtocol | DataType¶
- class onnxscript.ir.Value(producer: Node | None = None, *, index: int | None = None, name: str | None = None, shape: Shape | None = None, type: TypeProtocol | None = None, doc_string: str | None = None, const_value: TensorProtocol | None = 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
ValueInfoProto
in the ONNX specification.A
Value
is 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.producer
andindex
areNone
.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()
.To check if the value is an output of a graph, call
is_graph_output()
.- name¶
The name of the value. A value is always named when it is part of a graph.
- shape¶
The shape of the value.
- type¶
The type of the value.
- metadata_props¶
Metadata.
- property const_value: TensorProtocol | None¶
A concrete value.
The value can be backed by different raw data types, such as numpy arrays. The only guarantee is that it conforms TensorProtocol.
- doc_string¶
- 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.
- producer() Node | None [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.
- 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.dtype
or convenientlydtype
.
- class onnxscript.ir.ValueProtocol(*args, **kwargs)¶
Protocol for values.
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
ValueInfoProto
in the ONNX specification.A
Value
is 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.producer
andindex
areNone
.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()
.To check if the value is an output of a graph, call
is_graph_output()
.- shape¶
The shape of the value.
- Type:
ShapeProtocol | None
- type¶
The type of the value.
- Type:
TypeProtocol | None
- const_value¶
The constant tensor is the value constant.
- Type:
TensorProtocol | None
- const_value: TensorProtocol | None¶
- producer() NodeProtocol | None [source]¶
The node that produces this value.
- shape: ShapeProtocol | None¶
- type: TypeProtocol | None¶
- uses() Collection[tuple[NodeProtocol, int]] [source]¶
The set of (node, input_index) with node being those that use this value as an input.
- onnxscript.ir.from_proto(proto: ModelProto | GraphProto | NodeProto | TensorProto | AttributeProto | ValueInfoProto | TypeProto | FunctionProto) Any ¶
Deserialize an ONNX proto message to an IR object.
- onnxscript.ir.load(path: str | PathLike, format: str | None = None) Model ¶
Load an ONNX model from a file.
- Parameters:
path – The path to the ONNX file.
format – The format of the file (e.g. protobuf, textproto, json, etc.). If None, the format is inferred from the file extension.
- Returns:
The loaded model.
- onnxscript.ir.save(model: Model, path: str | PathLike, format: str | None = None) None ¶
Save an ONNX model to a file.
- Parameters:
model – The model to save.
path – The path to save the model to.
format – The format of the file (e.g. protobuf, textproto, json, etc.). If None, the format is inferred from the file extension.
- onnxscript.ir.tensor(value: npt.ArrayLike | onnx.TensorProto | _protocols.DLPackCompatible | _protocols.ArrayCompatible, dtype: _enums.DataType | None = None, name: str | None = None, doc_string: str | None = None) _protocols.TensorProtocol ¶
Create a tensor value from an ArrayLike object or a TensorProto.
The dtype must match the value. Reinterpretation of the value is not supported, unless if the value is a plain Python object, in which case it is converted to a numpy array with the given dtype.
:param:`value` can be a numpy array, a plain Python object, or a TensorProto.
Example:
>>> from onnxscript import ir >>> import numpy as np >>> import ml_dtypes >>> import onnx >>> ir.tensor(np.array([1, 2, 3], dtype=np.int16)) Tensor<INT16,[3]>(array([1, 2, 3], dtype=int16), name=None) >>> ir.tensor([1, 2, 3], dtype=ir.DataType.BFLOAT16) Tensor<BFLOAT16,[3]>(array([1, 2, 3], dtype=bfloat16), name=None) >>> tp_tensor = ir.tensor(onnx.helper.make_tensor("tensor", onnx.TensorProto.FLOAT, dims=[], vals=[0.5])) >>> tp_tensor.numpy() array(0.5, dtype=float32)
- Parameters:
value – The numpy array to create the tensor from.
dtype – The data type of the tensor.
name – The name of the tensor.
doc_string – The documentation string of the tensor.
- Returns:
A tensor value.
- Raises:
ValueError – If the dtype does not match the value when value is not a plain Python object like
list[int]
.
- onnxscript.ir.to_proto(ir_object: ModelProtocol | GraphProtocol | NodeProtocol | ValueProtocol | AttributeProtocol | ReferenceAttributeProtocol | TensorProtocol | TypeProtocol | GraphViewProtocol | FunctionProtocol) Any ¶
Serialize an IR object to a proto.