The Intermediate Representation (IR) is where all modifications to the graph are made. It can also be used to
create new graphs from scratch. The IR involves three components: Tensors,
Nodes, and Graphs.
Nearly all of the member variables of each component can be freely modified. For details on the various
attributes of these classes, you can view the help output using help(<class_or_instance>) in an
interactive shell, or using print(help(<class_or_instance>)) in a script, where <class_or_instance>
is an ONNX GraphSurgeon type, or an instance of that type.
Tensor
Tensors are divided into two subclasses: Variable and Constant.
A Constant is a tensor whose values are known upfront, and can be retrieved as a NumPy array and modified.
Note: The values property of a Constant is loaded on-demand. If the property is not accessed, the values willnot be loaded as a NumPy array.
A Variable is a tensor whose values are unknown until inference-time, but may contain information about data type and shape.
The inputs and outputs of Tensors are always Nodes.
A Node defines an operation in the graph. A node may specify attributes; attribute values can be any
Python primitive types, as well as ONNX GraphSurgeon Graphs or Tensors
The inputs and outputs of Nodes are always Tensors
In this case, the node has no attributes. Otherwise, attributes are displayed as an OrderedDict.
A Note On Modifying Inputs And Outputs
The inputs/outputs members of nodes and tensors have special logic that will update the inputs/outputs of all
affected nodes/tensors when you make a change. This means, for example, that you do not need to update the inputs
of a Node when you make a change to the outputs of its input tensor.
A Graph contains zero or more Nodes and input/output Tensors.
Intermediate tensors are not explicitly tracked, but are instead retrieved from the nodes contained within the graph.
The Graph class exposes several functions. A small subset is listed here:
cleanup(): Removes unused nodes and tensors in the graph
toposort(): Topologically sorts the graph.
tensors(): Returns a Dict[str, Tensor] mapping tensor names to tensors, by walking over all the tensors in the graph.
This is an O(N) operation, and so may be slow for large graphs.
To see the full Graph API, you can see help(onnx_graphsurgeon.Graph) in an interactive Python shell.
Exporters
Exporters are used to export the ONNX GraphSurgeon IR to ONNX or other types of graphs.
The exporter interface is defined in base_exporter.py.
Using models with externally stored data with ONNX-GraphSurgeon is almost the same as working with
ONNX models without external data. Refer to the
official ONNX documentation
for details on how to load such models. To import the model into ONNX-GraphSurgeon, you can use the
import_onnx function as normal.
During export, you just need to take one additional step:
Export the model from ONNX-GraphSurgeon as normal:
model = gs.export_onnx(graph)
Update the model so that it writes its data to the external location. If the location is not
specified, it defaults to the same directory as the ONNX model:
from onnx.external_data_helper import convert_model_to_external_data
convert_model_to_external_data(model, location="model.data")
Then you can save the model as usual:
onnx.save(model, "model.onnx")
Same project
Also published from nvidia/tensorrt.
tensorrt-cu12A high performance deep learning inference library