Implementation:Onnx Onnx BaseConverter Class
| Knowledge Sources | |
|---|---|
| Domains | Version Conversion, Model Compatibility |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for defining the base class and adapter registry infrastructure for ONNX opset version conversion, provided by the ONNX library.
Description
The BaseVersionConverter class is the abstract foundation of the ONNX version conversion system. It implements the adapter pattern for transforming ONNX operator nodes between different opset versions, enabling both forward (upgrade) and backward (downgrade) compatibility.
The class maintains two key data structures:
adapters is a nested unordered_map organized as {op_name: {from_domain$from_version: {to_domain$to_version: adapter}}}. This three-level map allows efficient lookup of the appropriate transformation adapter for a given operator moving from one version to another.
all_schemas is a nested map organized as {op_name: {domain: {version: schema}}} that stores operator schemas across all versions, providing reference information needed during conversion.
The class provides three primary methods:
adapter_lookup locates the correct registered adapter for a given Node being converted from an initial version to a target version. It navigates the adapters map and asserts with descriptive error messages if no adapter is found for the operator, source version, or target version.
convert_version is a pure virtual method that must be implemented by derived classes (such as DefaultVersionConverter) to define the overall version conversion strategy for a complete model.
registerAdapter has two overloads: one accepting a unique_ptr<Adapter> directly, and one accepting an operator name, source version, target version, and a NodeTransformerFunction that creates a GenericAdapter. Both overloads insert the adapter into the registry indexed by operator name, source version string, and target version string.
Usage
This class is not used directly by end users. Instead, it serves as the base for the DefaultVersionConverter that implements the full model conversion pipeline. Use this class when extending the version conversion system with custom conversion strategies. The registerAdapter methods are called during initialization of derived converter classes to populate the adapter registry with transformations for each operator version transition.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/version_converter/BaseConverter.h
Signature
class BaseVersionConverter {
protected:
std::unordered_map<
std::string,
std::unordered_map<std::string, std::unordered_map<std::string, std::unique_ptr<Adapter>>>>
adapters;
std::unordered_map<
std::string,
std::unordered_map<std::string, std::map<int64_t, const OpSchema*>>>
all_schemas;
public:
BaseVersionConverter() = default;
virtual ~BaseVersionConverter() = default;
const Adapter& adapter_lookup(
const Node* op,
const OpSetID& initial_version,
const OpSetID& target_version) const;
virtual ModelProto convert_version(
const ModelProto& mp_in,
const OpSetID& initial_version,
const OpSetID& target_version) const = 0;
void registerAdapter(std::unique_ptr<Adapter> a_ptr);
void registerAdapter(
const char* op,
int64_t from,
int64_t to,
const NodeTransformerFunction& transformer);
};
Import
#include "onnx/version_converter/BaseConverter.h"
I/O Contract
Inputs (adapter_lookup)
| Name | Type | Required | Description |
|---|---|---|---|
| op | const Node* | Yes | The computation graph node to find an adapter for |
| initial_version | const OpSetID& | Yes | The source opset version (domain and version number) |
| target_version | const OpSetID& | Yes | The target opset version (domain and version number) |
Inputs (convert_version)
| Name | Type | Required | Description |
|---|---|---|---|
| mp_in | const ModelProto& | Yes | The input ONNX model to convert |
| initial_version | const OpSetID& | Yes | The source opset version |
| target_version | const OpSetID& | Yes | The desired target opset version |
Inputs (registerAdapter - unique_ptr)
| Name | Type | Required | Description |
|---|---|---|---|
| a_ptr | std::unique_ptr<Adapter> | Yes | The adapter object to register, providing name, initial_version, and target_version |
Inputs (registerAdapter - transformer)
| Name | Type | Required | Description |
|---|---|---|---|
| op | const char* | Yes | The operator name this adapter applies to |
| from | int64_t | Yes | The source opset version number |
| to | int64_t | Yes | The target opset version number |
| transformer | const NodeTransformerFunction& | Yes | The function that performs the node transformation |
Outputs
| Name | Type | Description |
|---|---|---|
| adapter | const Adapter& | Reference to the matching registered adapter (from adapter_lookup) |
| model | ModelProto | The converted model at the target opset version (from convert_version) |
Usage Examples
#include "onnx/version_converter/BaseConverter.h"
using namespace ONNX_NAMESPACE::version_conversion;
// Derived converter class
class MyConverter : public BaseVersionConverter {
public:
MyConverter() {
// Register adapters for specific operator version transitions
registerAdapter("Relu", 6, 14, [](Node* node) -> Node* {
// Transform Relu from opset 6 to opset 14
return node;
});
registerAdapter("BatchNormalization", 9, 15, [](Node* node) -> Node* {
// Transform BatchNormalization from opset 9 to opset 15
return node;
});
}
ModelProto convert_version(
const ModelProto& mp_in,
const OpSetID& initial_version,
const OpSetID& target_version) const override {
// Implement conversion strategy
ModelProto mp_out(mp_in);
// ... apply adapters ...
return mp_out;
}
};
// Look up an adapter for a specific conversion
MyConverter converter;
const Adapter& adapter = converter.adapter_lookup(node, OpSetID(6), OpSetID(14));