{"library":"onnx-graphsurgeon","title":"ONNX GraphSurgeon","description":"ONNX GraphSurgeon is a Python library used for programmatically manipulating ONNX graphs. It provides a high-level API to import, inspect, modify, and export ONNX models, often used in conjunction with NVIDIA TensorRT for optimization and deployment workflows. The current PyPI version is 0.6.1, though its development is closely tied to TensorRT's release cadence, with newer versions often bundled with TensorRT distributions.","language":"python","status":"active","last_verified":"Sat May 16","install":{"commands":["pip install onnx-graphsurgeon==0.6.1"],"cli":null},"imports":["import onnx_graphsurgeon as gs","from onnx_graphsurgeon.ir.graph import Graph","from onnx_graphsurgeon.ir.node import Node","from onnx_graphsurgeon.ir.tensor import Tensor"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import onnx\nimport onnx_graphsurgeon as gs\nfrom onnx import helper, TensorProto\nimport numpy as np\nimport os\n\n# 1. Create a dummy ONNX model for demonstration\ndef create_dummy_model():\n    X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 3, 16, 16])\n    Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 3, 16, 16])\n    \n    const_val_tensor_1 = helper.make_tensor('const_add_val_1', TensorProto.FLOAT, [], [1.0])\n    \n    # Model: X -> Add (with const_add_val_1) -> Add_Output -> Identity -> Y\n    node_add = helper.make_node('Add', ['X', 'const_add_val_1'], ['Add_Output'])\n    node_identity = helper.make_node('Identity', ['Add_Output'], ['Y'])\n\n    graph_def = helper.make_graph(\n        [node_add, node_identity],\n        'simple_graph',\n        [X],\n        [Y],\n        [const_val_tensor_1]\n    )\n    model = helper.make_model(graph_def, producer_name='dummy-model', opset_imports=[helper.make_opsetid(\"\", 13)])\n    return model\n\n# Save the dummy model to a file\ndummy_model = create_dummy_model()\nonnx.save(dummy_model, \"dummy_model.onnx\")\nprint(\"Original model saved to dummy_model.onnx\")\n\n# 2. Load the model with ONNX GraphSurgeon\ngraph = gs.import_onnx(onnx.load(\"dummy_model.onnx\"))\n\n# 3. Modify the graph: Replace the Identity node with a second Add node\nidentity_node = None\nfor node in graph.nodes:\n    if node.op == \"Identity\":\n        identity_node = node\n        break\n\nif identity_node:\n    input_tensor = identity_node.inputs[0]  # Output of the first Add node\n    output_tensor = identity_node.outputs[0] # The graph's final output tensor 'Y'\n\n    # Remove the old Identity node\n    graph.nodes.remove(identity_node)\n\n    # Create a new Constant for the second Add op\n    const_val_tensor_2 = gs.Constant(name=\"const_add_val_2\", values=np.array([2.0], dtype=np.float32))\n\n    # Create a new Add node to replace Identity\n    new_add_node = gs.Node(\n        op=\"Add\",\n        inputs=[input_tensor, const_val_tensor_2], # Input from first Add, plus new constant\n        outputs=[output_tensor] # Re-use the original output tensor 'Y'\n    )\n\n    # Add the new node to the graph\n    graph.nodes.append(new_add_node)\n\n# Always cleanup and topological sort after graph modifications\ngraph.cleanup().toposort()\n\n# 4. Save the modified model\nmodified_model = gs.export_onnx(graph)\nonnx.save(modified_model, \"modified_dummy_model.onnx\")\nprint(\"Modified model saved to modified_dummy_model.onnx (Identity replaced with Add)\")\n\n# Optional: Verify the modified model with ONNX checker\ntry:\n    onnx.checker.check_model(modified_model)\n    print(\"Modified model check successful!\")\nexcept Exception as e:\n    print(f\"Modified model check failed: {e}\")\n\n# Cleanup created files\nos.remove(\"dummy_model.onnx\")\nos.remove(\"modified_dummy_model.onnx\")","lang":"python","description":"This quickstart demonstrates how to load an ONNX model, find a specific node (Identity), remove it, and replace it with a new operation (an 'Add' node with a new constant input), and then save the modified model. It also includes `onnx.checker.check_model` for basic graph validation.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-16","installed_version":"0.6.1","pypi_latest":"0.6.1","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":50,"avg_install_s":6.7,"avg_import_s":0.62,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"onnx-graphsurgeon==0.6.1","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"onnx-graphsurgeon==0.6.1","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":6.9,"import_time_s":0.42,"mem_mb":14.5,"disk_size":"191M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"onnx-graphsurgeon==0.6.1","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"onnx-graphsurgeon==0.6.1","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":6.4,"import_time_s":0.64,"mem_mb":15.9,"disk_size":"200M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"onnx-graphsurgeon==0.6.1","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"onnx-graphsurgeon==0.6.1","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":6.2,"import_time_s":0.92,"mem_mb":17.6,"disk_size":"188M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"onnx-graphsurgeon==0.6.1","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"onnx-graphsurgeon==0.6.1","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":6.2,"import_time_s":0.5,"mem_mb":12.9,"disk_size":"188M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"onnx-graphsurgeon==0.6.1","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"onnx-graphsurgeon==0.6.1","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":7.8,"import_time_s":0.61,"mem_mb":15,"disk_size":"201M"}]}}