Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Onnx Onnx Version Converter Convert Version

From Leeroopedia


Knowledge Sources
Domains Versioning, Model_Transformation
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for converting ONNX models between opset versions, wrapping a C++ version conversion engine.

Description

The convert_version function converts a ModelProto from its current opset version to a specified target opset version within the default domain. It is a Python wrapper around the C++ version converter (onnx_cpp2py_export.version_converter). The function serializes the model to bytes, passes it to the C++ converter engine which applies the appropriate version adapters, and deserializes the result.

The C++ engine maintains a registry of adapters in convert.h (962 lines) that handle version transitions for individual operators. The BaseConverter.h class provides the abstract interface for these adapters.

Usage

Import this function when you need to convert a model to a different opset version. The function raises ConvertError if the conversion is not supported (e.g., if an operator has no registered adapter for the requested version transition).

Code Reference

Source Location

  • Repository: onnx
  • File: onnx/version_converter.py
  • Lines: 17-40

Signature

def convert_version(model: ModelProto, target_version: int) -> ModelProto:
    """Convert opset version of the ModelProto.

    Args:
        model: Model to convert.
        target_version: Target opset version for the default domain.

    Returns:
        Converted model with operators adapted to the target opset.

    Raises:
        RuntimeError: When a necessary conversion is not supported.
        TypeError: If model is not ModelProto or target_version is not int.
    """

Import

from onnx import version_converter

I/O Contract

Inputs

Name Type Required Description
model ModelProto Yes Source model to convert
target_version int Yes Target opset version for the default domain (ai.onnx)

Outputs

Name Type Description
return ModelProto New model with operators converted to the target opset version

Usage Examples

Upgrade Opset Version

import onnx
from onnx import version_converter

# Load a model with opset 13
model = onnx.load_model("model_opset13.onnx")
print(f"Current opset: {model.opset_import[0].version}")

# Convert to opset 17
converted = version_converter.convert_version(model, 17)
print(f"Converted opset: {converted.opset_import[0].version}")

# Validate and save
onnx.checker.check_model(converted)
onnx.save_model(converted, "model_opset17.onnx")

Downgrade Opset Version

import onnx
from onnx import version_converter

# Load a model with latest opset
model = onnx.load_model("model.onnx")

# Downgrade to opset 11 for older runtime compatibility
try:
    converted = version_converter.convert_version(model, 11)
    onnx.save_model(converted, "model_opset11.onnx")
except RuntimeError as e:
    print(f"Conversion not supported: {e}")

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment