Implementation:Onnx Onnx Helper Make Model
| Knowledge Sources | |
|---|---|
| Domains | Model_Construction, Intermediate_Representation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for constructing complete ONNX model protobuf messages provided by the ONNX helper module.
Description
The make_model function constructs a ModelProto protobuf message from a GraphProto and optional keyword arguments. It automatically sets the IR version to the current onnx.IR_VERSION and, if no opset imports are specified, adds a default import for the latest ai.onnx opset version. The companion make_opsetid function creates OperatorSetIdProto messages for custom opset imports.
Usage
Import this function after assembling a GraphProto via make_graph. The resulting ModelProto is ready for validation with checker.check_model and serialization with save_model. Use opset_imports to pin a specific opset version rather than relying on the default.
Code Reference
Source Location
- Repository: onnx
- File: onnx/helper.py
- Lines: 294-326 (make_model), 243-255 (make_opsetid)
Signature
def make_model(graph: GraphProto, **kwargs: Any) -> ModelProto:
"""Construct a ModelProto.
Args:
graph: GraphProto from make_graph.
**kwargs: Model-level attributes including:
opset_imports (Sequence[OperatorSetIdProto]): Opset version declarations.
functions (Sequence[FunctionProto]): Model-local function definitions.
ir_version (int): Override IR version (default: onnx.IR_VERSION).
producer_name (str): Producer name metadata.
model_version (int): Model version number.
doc_string (str): Model documentation.
"""
def make_opsetid(domain: str, version: int) -> OperatorSetIdProto:
"""Construct an OperatorSetIdProto.
Args:
domain: The domain of the operator set (e.g., "" for ai.onnx).
version: Version of the operator set.
"""
Import
from onnx import helper
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| graph | GraphProto | Yes | The computation graph from make_graph |
| opset_imports | Sequence[OperatorSetIdProto] | No | Opset version declarations (default: latest ai.onnx) |
| functions | Sequence[FunctionProto] | No | Model-local function definitions |
| ir_version | int | No | ONNX IR version (default: onnx.IR_VERSION) |
| producer_name | str | No | Name of the producing tool |
| model_version | int | No | Model version number |
Outputs
| Name | Type | Description |
|---|---|---|
| return | ModelProto | Complete ONNX model protobuf ready for validation and serialization |
Usage Examples
Default Opset
from onnx import helper
# Build model with default opset (latest)
model = helper.make_model(graph)
print(f"IR version: {model.ir_version}")
print(f"Opset: {model.opset_import[0].version}")
Explicit Opset Version
from onnx import helper
# Pin to opset 17 for compatibility
model = helper.make_model(
graph,
opset_imports=[helper.make_opsetid("", 17)],
producer_name="my_tool",
model_version=1,
)
Multiple Domains
from onnx import helper
# Model using both standard and ML domain operators
model = helper.make_model(
graph,
opset_imports=[
helper.make_opsetid("", 21), # ai.onnx domain
helper.make_opsetid("ai.onnx.ml", 5), # ML domain
],
)