Simple ONNX Name Generator
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.
Common errors
-
ModuleNotFoundError: No module named 'onnx_graphsurgeon'
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.fixInstall `onnx_graphsurgeon` explicitly: `pip install -U onnx_graphsurgeon --index-url https://pypi.ngc.nvidia.com`. -
FileNotFoundError: [Errno 2] No such file or directory: 'your_model.onnx'
cause The path provided for the input ONNX file is incorrect, or the file does not exist at the specified location.fixDouble-check the file name and its full path. Ensure the file exists and is accessible from where the script is being executed. -
The model is invalid: Field 'shape' of 'type' is required but missing. / [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: ...
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.fixValidate 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.
Warnings
- breaking 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`.
- gotcha 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.
- gotcha 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.
Install
-
pip install -U onnx sng4onnx
Imports
- generate
from sng4onnx import generate
Quickstart
import onnx
from onnx import helper, TensorProto
import numpy as np
from sng4onnx import generate
import os
# 1. Create a dummy ONNX model for demonstration
def create_dummy_onnx(path):
# Define graph inputs
X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 2, 3])
Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 2, 3])
# Define graph outputs
Z = helper.make_tensor_value_info('Z', TensorProto.FLOAT, [1, 2, 3])
# Create a node (Mul operator)
node_def = helper.make_node(
'Add',
['X', 'Y'],
['Z'],
name='MyAddOperation' # Can be empty in an 'old format' model
)
# Create the graph
graph_def = helper.make_graph(
[node_def],
'simple_graph',
[X, Y],
[Z]
)
# Create the model
model_def = helper.make_model(graph_def, producer_name='dummy-model')
# Save the model
onnx.save(model_def, path)
input_onnx_path = 'input_model.onnx'
output_onnx_path = 'output_model_renamed.onnx'
create_dummy_onnx(input_onnx_path)
# 2. Use sng4onnx to generate/assign OP names
print(f"Processing {input_onnx_path}...")
renamed_model = generate(
input_onnx_file_path=input_onnx_path,
output_onnx_file_path=output_onnx_path,
non_verbose=False
)
# 3. Verify the output (optional)
if os.path.exists(output_onnx_path):
print(f"Successfully generated {output_onnx_path}")
loaded_model = onnx.load(output_onnx_path)
print(f"Nodes in renamed model: {[node.name for node in loaded_model.graph.node]}")
else:
print("Error: Output model not found.")
# Clean up dummy files
os.remove(input_onnx_path)
os.remove(output_onnx_path)