Implementation:Onnx Onnx Checker Cpp API
| Knowledge Sources | |
|---|---|
| Domains | Validation, Model Checking |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for validating ONNX protobuf structures provided by the ONNX library's C++ checker API.
Description
The onnx/checker.h header defines the public C++ API and core infrastructure for ONNX model validation. It provides exception types, context classes for tracking validation state, and function declarations for checking all ONNX protobuf structures.
The header defines three key classes:
ValidationError is a custom exception class derived from std::runtime_error. It supports accumulating context information through its AppendContext() method, which appends additional context strings to the error message. The fail_check(...) macro provides a convenient shorthand for throwing ValidationError with a formatted message.
CheckerContext stores all configuration needed during validation. It tracks the IR version, opset imports (a map of domain to version), whether the current graph is the main graph, the schema registry, the model directory (for resolving external data), and flags for skipping opset compatibility checks or checking custom domains. All members are accessed through getter/setter methods.
LexicalScopeContext manages variable name scoping for validating references in nested graph structures. It maintains an output_names set for the current graph and optionally links to a parent context for ancestor lookups via this_or_ancestor_graph_has(). The copy constructor creates a child scope that references the parent for hierarchical name resolution.
The header declares validation functions for every ONNX protobuf type: check_value_info, check_tensor, check_sparse_tensor, check_sequence, check_map, check_optional, check_attribute, check_node, check_graph, check_function, check_opset_compatibility, check_model_local_functions, and check_model (with overloads for both ModelProto and file path).
Usage
Use check_model() to validate an entire ONNX model, either from a ModelProto object or from a file path. Set full_check=true for complete validation. Use the individual check functions (check_node, check_graph, etc.) when validating specific protobuf components. Create a CheckerContext to customize validation behavior, such as skipping opset compatibility checks or enabling custom domain validation.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/checker.h
Signature
namespace ONNX_NAMESPACE {
namespace checker {
class ValidationError final : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
const char* what() const noexcept override;
void AppendContext(const std::string& context);
};
class CheckerContext final {
public:
int get_ir_version() const;
void set_ir_version(int v);
const std::unordered_map<std::string, int>& get_opset_imports() const;
void set_opset_imports(std::unordered_map<std::string, int> imps);
bool is_main_graph() const;
void set_is_main_graph(bool is_main_graph);
void set_schema_registry(const ISchemaRegistry* schema_registry);
const ISchemaRegistry* get_schema_registry() const;
void set_model_dir(const std::string& model_dir);
std::string get_model_dir() const;
bool skip_opset_compatibility_check() const;
void set_skip_opset_compatibility_check(bool value);
bool check_custom_domain() const;
void set_check_custom_domain(bool value);
};
class LexicalScopeContext final {
public:
void add(const std::string& name);
bool this_graph_has(const std::string& name) const;
bool this_or_ancestor_graph_has(const std::string& name) const;
std::unordered_set<std::string> output_names;
};
ONNX_API void check_model(const ModelProto& model, bool full_check = false,
bool skip_opset_compatibility_check = false, bool check_custom_domain = false);
ONNX_API void check_model(const std::string& model_path, bool full_check = false,
bool skip_opset_compatibility_check = false, bool check_custom_domain = false);
void check_node(const NodeProto& node, const CheckerContext&, const LexicalScopeContext&);
void check_graph(const GraphProto& graph, const CheckerContext&, const LexicalScopeContext&);
void check_tensor(const TensorProto& tensor, const CheckerContext&);
void check_function(const FunctionProto& function, const CheckerContext&, const LexicalScopeContext&);
ONNX_API void check_opset_compatibility(const NodeProto& node, const CheckerContext& ctx,
const std::unordered_map<std::string, int>& func_opset_imports,
const std::unordered_map<std::string, int>& model_opset_imports);
ONNX_API bool check_is_experimental_op(const NodeProto& node);
} // namespace checker
} // namespace ONNX_NAMESPACE
Import
#include "onnx/checker.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | const ModelProto& or const std::string& | Yes | The ONNX model object or file path to validate |
| full_check | bool | No | When true, performs complete validation including shape inference (defaults to false) |
| skip_opset_compatibility_check | bool | No | When true, skips opset compatibility validation (defaults to false) |
| check_custom_domain | bool | No | When true, validates operators in custom domains (defaults to false) |
| ctx | const CheckerContext& | Yes (for individual checks) | Validation configuration including IR version and opset imports |
| lex_ctx | const LexicalScopeContext& | Yes (for node/graph/function checks) | Tracks variable names in scope |
Outputs
| Name | Type | Description |
|---|---|---|
| (none / exception) | void | Functions return normally on success; throw ValidationError on failure |
| is_experimental | bool | check_is_experimental_op returns whether a node uses an experimental operator |
Usage Examples
#include "onnx/checker.h"
// Validate a model from a file path
try {
ONNX_NAMESPACE::checker::check_model("model.onnx", /*full_check=*/true);
} catch (const ONNX_NAMESPACE::checker::ValidationError& e) {
std::cerr << "Validation failed: " << e.what() << std::endl;
}
// Validate a model proto with custom options
ONNX_NAMESPACE::checker::check_model(model_proto,
/*full_check=*/false,
/*skip_opset_compatibility_check=*/true,
/*check_custom_domain=*/false);
// Validate a single node with context
ONNX_NAMESPACE::checker::CheckerContext ctx;
ctx.set_ir_version(8);
ctx.set_opset_imports({{"", 17}});
ONNX_NAMESPACE::checker::LexicalScopeContext lex_ctx;
lex_ctx.add("input_tensor");
ONNX_NAMESPACE::checker::check_node(node, ctx, lex_ctx);