ONNX Intermediate Representation (IR)

0.2.0 · active · verified Sat Apr 11

onnx-ir provides an efficient in-memory representation for ONNX graphs, allowing for programmatic creation, manipulation, and optimization of ONNX models in Python. It is currently at version 0.2.0 and has a frequent release cadence, often seeing multiple patch releases per month, indicating active development.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple ONNX graph representing an 'Add' operation using `onnx-ir`'s core components: `Value`, `Type`, `Shape`, `Node`, and `Graph`.

import onnx_ir as ir
import numpy as np

# Create input values with specific types and shapes
input_a = ir.Value("input_a", ir.Type(ir.Shape([2, 2]), ir.TensorElementDataType.FLOAT))
input_b = ir.Value("input_b", ir.Type(ir.Shape([2, 2]), ir.TensorElementDataType.FLOAT))

# Define the output value for the Add node
output_c = ir.Value("output_c") 

# Create an 'Add' node with inputs and outputs
add_node = ir.Node("Add", inputs=[input_a, input_b], outputs=[output_c])

# Assemble a graph from inputs, outputs, and nodes
graph = ir.Graph(
    [input_a, input_b], # Graph inputs
    [output_c],         # Graph outputs
    [add_node],         # Nodes in the graph
    "simple_add_graph"  # Name of the graph
)

print(f"Created graph: {graph.name}")
print(f"Graph has {len(graph.nodes)} node(s).")
print(f"Input 'input_a' shape: {input_a.type.shape.dims}")

view raw JSON →