{"id":7729,"library":"sng4onnx","title":"Simple ONNX Name Generator","description":"sng4onnx is a Python library and CLI tool designed to automatically generate and assign an operation (OP) name to each operation within an ONNX (Open Neural Network Exchange) file, particularly useful for older format models lacking explicit OP names. The library is currently active, with version 2.0.1, and maintains a frequent release cadence, often rolling out updates for bug fixes and minor feature enhancements.","status":"active","version":"2.0.1","language":"en","source_language":"en","source_url":"https://github.com/PINTO0309/sng4onnx","tags":["onnx","model-processing","neural-networks","machine-learning"],"install":[{"cmd":"pip install -U onnx sng4onnx","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for ONNX model manipulation.","package":"onnx","optional":false}],"imports":[{"note":"The primary function for processing ONNX models.","symbol":"generate","correct":"from sng4onnx import generate"}],"quickstart":{"code":"import onnx\nfrom onnx import helper, TensorProto\nimport numpy as np\nfrom sng4onnx import generate\nimport os\n\n# 1. Create a dummy ONNX model for demonstration\ndef create_dummy_onnx(path):\n    # Define graph inputs\n    X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 2, 3])\n    Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 2, 3])\n\n    # Define graph outputs\n    Z = helper.make_tensor_value_info('Z', TensorProto.FLOAT, [1, 2, 3])\n\n    # Create a node (Mul operator)\n    node_def = helper.make_node(\n        'Add',\n        ['X', 'Y'],\n        ['Z'],\n        name='MyAddOperation' # Can be empty in an 'old format' model\n    )\n\n    # Create the graph\n    graph_def = helper.make_graph(\n        [node_def],\n        'simple_graph',\n        [X, Y],\n        [Z]\n    )\n\n    # Create the model\n    model_def = helper.make_model(graph_def, producer_name='dummy-model')\n\n    # Save the model\n    onnx.save(model_def, path)\n\ninput_onnx_path = 'input_model.onnx'\noutput_onnx_path = 'output_model_renamed.onnx'\ncreate_dummy_onnx(input_onnx_path)\n\n# 2. Use sng4onnx to generate/assign OP names\nprint(f\"Processing {input_onnx_path}...\")\nrenamed_model = generate(\n    input_onnx_file_path=input_onnx_path,\n    output_onnx_file_path=output_onnx_path,\n    non_verbose=False\n)\n\n# 3. Verify the output (optional)\nif os.path.exists(output_onnx_path):\n    print(f\"Successfully generated {output_onnx_path}\")\n    loaded_model = onnx.load(output_onnx_path)\n    print(f\"Nodes in renamed model: {[node.name for node in loaded_model.graph.node]}\")\nelse:\n    print(\"Error: Output model not found.\")\n\n# Clean up dummy files\nos.remove(input_onnx_path)\nos.remove(output_onnx_path)","lang":"python","description":"This quickstart demonstrates how to use `sng4onnx` to process an ONNX model. It begins by programmatically creating a simple ONNX model, then passes it to the `generate` function to automatically assign operation names. Finally, it saves and verifies the processed model."},"warnings":[{"fix":"Explicitly install `onnx_graphsurgeon` if your workflow requires it: `pip install -U onnx_graphsurgeon --index-url https://pypi.ngc.nvidia.com`.","message":"Version 2.0.0 removed the `onnx_graphsurgeon` dependency. If your project or other tools implicitly relied on `sng4onnx` installing `onnx_graphsurgeon` or expected its presence, you will encounter `ModuleNotFoundError`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade to `sng4onnx` version 1.0.5 or newer to ensure correct handling of external data during shape inference.","message":"Older versions (prior to 1.0.5) had a bug where running `onnx.shape_inference.infer_shapes()` on ONNX models with external data could corrupt the model file.","severity":"gotcha","affected_versions":"<1.0.5"},{"fix":"Ensure you are using `sng4onnx` version 1.0.2 or later to correctly preserve ONNX graph metadata.","message":"Versions prior to 1.0.2 had issues preserving critical ONNX model metadata, such as `domain` and `ir_version`, during processing, potentially leading to models that behave unexpectedly or are incompatible with other tools.","severity":"gotcha","affected_versions":"<1.0.2"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `onnx_graphsurgeon` explicitly: `pip install -U onnx_graphsurgeon --index-url https://pypi.ngc.nvidia.com`.","cause":"From version 2.0.0, `sng4onnx` no longer includes `onnx_graphsurgeon` as a direct dependency. If your code or other tools you use alongside `sng4onnx` still require `onnx_graphsurgeon`, it must be installed separately.","error":"ModuleNotFoundError: No module named 'onnx_graphsurgeon'"},{"fix":"Double-check the file name and its full path. Ensure the file exists and is accessible from where the script is being executed.","cause":"The path provided for the input ONNX file is incorrect, or the file does not exist at the specified location.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'your_model.onnx'"},{"fix":"Validate the ONNX model using `onnx.checker.check_model(model_path)` to get specific errors. Consider re-exporting the model from its original framework, ensuring all input and output shapes are explicitly defined. You might also need to use other ONNX manipulation tools to inspect and fix the model's graph structure.","cause":"These errors indicate issues with the structure or completeness of the input ONNX model itself, such as missing shape information for tensors or incorrect tensor ranks, which might stem from how the model was originally exported.","error":"The model is invalid: Field 'shape' of 'type' is required but missing. / [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: ..."}]}