{"id":3188,"library":"onnx-ir","title":"ONNX Intermediate Representation (IR)","description":"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.","status":"active","version":"0.2.0","language":"en","source_language":"en","source_url":"https://github.com/onnx/ir-py","tags":["onnx","ir","intermediate representation","ml","deep learning","graph optimization"],"install":[{"cmd":"pip install onnx-ir","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for ONNX model manipulation.","package":"onnx","optional":false},{"reason":"Used for tensor data handling.","package":"numpy","optional":false},{"reason":"Added in v0.2.0 for symbolic shape inferencing infrastructure.","package":"sympy","optional":false}],"imports":[{"symbol":"Graph","correct":"from onnx_ir import Graph"},{"symbol":"Node","correct":"from onnx_ir import Node"},{"symbol":"Value","correct":"from onnx_ir import Value"},{"symbol":"Type","correct":"from onnx_ir import Type"},{"symbol":"Shape","correct":"from onnx_ir import Shape"},{"symbol":"TensorElementDataType","correct":"from onnx_ir import TensorElementDataType"}],"quickstart":{"code":"import onnx_ir as ir\nimport numpy as np\n\n# Create input values with specific types and shapes\ninput_a = ir.Value(\"input_a\", ir.Type(ir.Shape([2, 2]), ir.TensorElementDataType.FLOAT))\ninput_b = ir.Value(\"input_b\", ir.Type(ir.Shape([2, 2]), ir.TensorElementDataType.FLOAT))\n\n# Define the output value for the Add node\noutput_c = ir.Value(\"output_c\") \n\n# Create an 'Add' node with inputs and outputs\nadd_node = ir.Node(\"Add\", inputs=[input_a, input_b], outputs=[output_c])\n\n# Assemble a graph from inputs, outputs, and nodes\ngraph = ir.Graph(\n    [input_a, input_b], # Graph inputs\n    [output_c],         # Graph outputs\n    [add_node],         # Nodes in the graph\n    \"simple_add_graph\"  # Name of the graph\n)\n\nprint(f\"Created graph: {graph.name}\")\nprint(f\"Graph has {len(graph.nodes)} node(s).\")\nprint(f\"Input 'input_a' shape: {input_a.type.shape.dims}\")","lang":"python","description":"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`."},"warnings":[{"fix":"Replace `ir.Input` with `ir.val`. Update node attribute definitions to use tuples for repeating attributes as specified in the updated API documentation.","message":"In `v0.1.9`, the `ir.Input` class was deprecated in favor of `ir.val`. Additionally, attribute signatures for nodes were simplified, requiring tuples for repeating attributes. Code using the old `ir.Input` or the previous attribute signature format will break.","severity":"breaking","affected_versions":">=0.1.9"},{"fix":"After calling `Value.replace_all_uses_with` with `replace_graph_outputs=True`, ensure the replacement `Value` has its `.name` attribute set to the original output's name if graph signature preservation is desired.","message":"When using the `Value.replace_all_uses_with` method (introduced in `v0.1.12`) with the `replace_graph_outputs` option, users are responsible for manually assigning the original output name to the replacement value if they wish to preserve the graph's signature. Failing to do so can lead to a modified graph signature.","severity":"gotcha","affected_versions":">=0.1.12"},{"fix":"Ensure your environment is prepared for the additional `sympy` dependency. No code changes are required unless you were explicitly trying to avoid `sympy`.","message":"The `v0.2.0` release introduced `sympy` as a new dependency for symbolic shape inferencing. This will increase the total install size and might slightly extend the installation time for new environments.","severity":"gotcha","affected_versions":">=0.2.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}