checker.h#
Validation utilities for ONNX graphs and models, including
onnx::checker::CheckerContext,
onnx::checker::LexicalScopeContext, and the public
onnx::checker::check_model() entry points.
Declares ONNX model and graph validation entry points.
This header exposes the checker context and lexical scope helpers used to validate ONNX protobuf structures, as well as the public check_model() overloads used by the C++ API.
Defines
-
fail_check(...)#
Throws a ValidationError with a message built from the variadic arguments.
Arguments are concatenated using ONNX_LIGHT_NAMESPACE::MakeString.
-
namespace ONNX_LIGHT_NAMESPACE#
-
namespace checker#
Typedefs
-
using IR_VERSION_TYPE = decltype(Version::IR_VERSION)#
Alias for the integral type of the ONNX IR version enum.
Functions
-
void check_value_info(const ValueInfoProto &value_info, const CheckerContext&)#
Validates a ValueInfoProto entry.
- Parameters:
value_info – Value information to validate.
ctx – Provides checker settings such as the active IR version.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_tensor(const TensorProto &tensor, const CheckerContext&)#
Validates a TensorProto, including type, shape, and raw/external data.
- Parameters:
tensor – Tensor to validate.
ctx – Provides checker settings such as the model base directory.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_sparse_tensor(const SparseTensorProto &sparse_tensor, const CheckerContext&)#
Validates a SparseTensorProto and its indices/values consistency.
- Parameters:
sparse_tensor – Sparse tensor to validate.
ctx – Provides checker settings.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_sequence(const SequenceProto &sequence, const CheckerContext&)#
Validates a SequenceProto and the consistency of its elements.
- Parameters:
sequence – Sequence to validate.
ctx – Provides checker settings.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_map(const MapProto &map, const CheckerContext&)#
Validates a MapProto, including key and value type consistency.
- Parameters:
map – Map to validate.
ctx – Provides checker settings.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_optional(const OptionalProto &opt, const CheckerContext&)#
Validates an OptionalProto wrapper.
- Parameters:
opt – Optional value to validate.
ctx – Provides checker settings.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_attribute(const AttributeProto &attr, const CheckerContext&, const LexicalScopeContext&)#
Validates an AttributeProto against the schema of its enclosing node.
- Parameters:
attr – Attribute to validate.
ctx – Provides checker settings and schema lookup configuration.
lex_ctx – Lexical scope used to validate subgraph references.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_node(const NodeProto &node, const CheckerContext&, const LexicalScopeContext&)#
Validates a NodeProto against the operator schema selected by the active opset imports.
- Parameters:
node – Node to validate.
ctx – Provides checker settings and schema lookup configuration.
lex_ctx – Lexical scope used to resolve input names.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_graph(const GraphProto &graph, const CheckerContext&, const LexicalScopeContext&)#
Validates a GraphProto, including its inputs, initializers, and nodes.
- Parameters:
graph – Graph to validate.
ctx – Provides checker settings and schema lookup configuration.
parent_lex – Lexical scope from the parent graph, when applicable.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_function(const FunctionProto &function, const CheckerContext&, const LexicalScopeContext&)#
Validates a FunctionProto and the bodies it defines.
- Parameters:
function – Function to validate.
ctx – Provides checker settings and schema lookup configuration.
parent_lex – Lexical scope visible to the function body.
- Throws:
ValidationError – Thrown when validation fails.
-
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)#
Determines whether a node remains schema-compatible across two opset versions.
Compatibility means that both imported versions resolve to the same schema evolution point (the same since_version), so function-local and model-level imports do not disagree for the node’s operator.
- Parameters:
node – Identifies the operator node to validate.
ctx – Provides checker settings and schema lookup configuration.
func_opset_imports – Contains opset imports from the enclosing function.
model_opset_imports – Contains opset imports from the parent model.
- Throws:
ValidationError – Thrown when compatibility checks fail.
-
void check_model_local_functions(const ModelProto &model, const CheckerContext &ctx, const LexicalScopeContext &parent_lex)#
Validates all model-local functions declared in a model.
- Parameters:
model – Supplies the model containing local functions.
ctx – Provides checker settings and schema lookup configuration.
parent_lex – Provides the lexical scope visible to local functions.
- Throws:
ValidationError – Thrown when function validation fails.
-
void check_function_call_cycles(const ModelProto &model)#
Detects cycles in the model-local function call graph.
- Parameters:
model – Supplies the model containing local functions.
- Throws:
ValidationError – Thrown when a function directly or indirectly references itself.
-
void check_model(const ModelProto &model, bool full_check = false, bool skip_opset_compatibility_check = false, bool check_custom_domain = false)#
Validates an in-memory model protobuf.
- Parameters:
model – Model to validate.
full_check – When true, enables additional shape inference checks after structural validation.
skip_opset_compatibility_check – When true, skips schema compatibility checks.
check_custom_domain – When true, enables checks on custom op domains.
- Throws:
ValidationError – Thrown when validation fails.
-
void check_model(const std::string &model_path, bool full_check = false, bool skip_opset_compatibility_check = false, bool check_custom_domain = false)#
Validates a serialized model located at a filesystem path.
- Parameters:
model_path – UTF-8 path to a serialized ModelProto.
full_check – When true, enables additional shape inference checks after structural validation.
skip_opset_compatibility_check – When true, skips schema compatibility checks.
check_custom_domain – When true, enables checks on custom op domains.
- Throws:
ValidationError – Thrown when validation fails.
-
std::filesystem::path resolve_external_data_location(const std::string &base_dir, const std::string &location, const std::string &tensor_name)#
Resolves and validates an external tensor data location relative to a model.
- Parameters:
base_dir – Provides the model base directory used for resolution.
location – Provides the external data location from TensorProto.
tensor_name – Provides the tensor name used in error messages.
- Throws:
ValidationError – Thrown when the location is invalid or unsafe.
- Returns:
The resolved filesystem path.
-
int64_t open_external_data(const std::string &base_dir, const std::string &location, const std::string &tensor_name, bool read_only)#
Opens external tensor data and returns a CRT file descriptor.
The caller owns the descriptor and must close it.
- Parameters:
base_dir – Provides the model base directory used for resolution.
location – Provides the external data location from TensorProto.
tensor_name – Provides the tensor name used in error messages.
read_only – Selects read-only mode when true.
- Throws:
ValidationError – Thrown when location validation fails.
std::runtime_error – Thrown when the file cannot be opened.
- Returns:
The opened CRT file descriptor.
-
class CheckerContext#
- #include <checker.h>
Stores checker configuration shared across recursive validation calls.
Public Functions
-
inline int get_ir_version() const#
Returns the IR version currently being validated.
-
inline void set_ir_version(int v)#
Sets the IR version used during validation.
-
inline const std::unordered_map<std::string, int> &get_opset_imports() const#
Returns the opset imports (domain to version) configured for the model.
-
inline void set_opset_imports(std::unordered_map<std::string, int> imps)#
Sets the opset imports (domain to version) to use during validation.
-
inline bool is_main_graph() const#
Returns true when the graph being validated is the model main graph.
-
inline void set_is_main_graph(bool is_main_graph)#
Selects whether the next graph to validate is the main graph.
-
inline void set_schema_registry(const ISchemaRegistry *schema_registry)#
Overrides the schema registry used to look up operator definitions.
The default registry is OpSchemaRegistry::Instance().
-
inline const ISchemaRegistry *get_schema_registry() const#
Returns the schema registry used to look up operator definitions.
-
inline void set_model_dir(const std::string &model_dir)#
Sets the directory used to resolve relative external data locations.
- Parameters:
model_dir – Filesystem path containing the loaded model.
-
inline std::string get_model_dir() const#
Returns the directory used to resolve relative external data locations.
-
inline bool skip_opset_compatibility_check() const#
Returns true when schema-version compatibility checks are skipped.
-
inline void set_skip_opset_compatibility_check(bool value)#
Enables or disables schema-version compatibility checks.
-
inline bool check_custom_domain() const#
Returns true when checks on operators from custom domains are enabled.
-
inline void set_check_custom_domain(bool value)#
Enables or disables checks on operators from custom domains.
-
explicit CheckerContext() = default#
Constructs a CheckerContext with default validation options.
-
inline int get_ir_version() const#
-
class LexicalScopeContext#
- #include <checker.h>
Tracks values visible in the current graph and in parent lexical scopes.
Public Functions
-
LexicalScopeContext() = default#
Constructs an empty lexical scope with no parent.
-
~LexicalScopeContext() = default#
-
inline LexicalScopeContext(const LexicalScopeContext &parent_context)#
Constructs an instance referencing the lexical scope of a parent graph.
Enables lookup of names from the parent scope via this_or_ancestor_graph_has(). The caller must ensure
parent_contextremains valid for the entire lifetime of the new instance. Alternatively, if that cannot be guaranteed, create an instance with the default constructor and populate ::output_names with the values from the parent scope so the values are copied instead.
-
inline LexicalScopeContext &operator=(const LexicalScopeContext &parent_context)#
Re-binds this scope to a different parent context.
- Parameters:
parent_context – Parent scope whose lifetime must outlive this scope.
-
LexicalScopeContext(LexicalScopeContext&&) = delete#
-
LexicalScopeContext &operator=(LexicalScopeContext&&) = delete#
Public Members
Private Members
-
const LexicalScopeContext *parent_context_ = {nullptr}#
-
LexicalScopeContext() = default#
-
class ValidationError : public std::runtime_error#
- #include <checker.h>
Exception type thrown by checker routines when validation fails.
The error message can be augmented after construction with AppendContext() so that callers higher in the recursion can prepend contextual information (such as the offending node or graph) to the underlying error string.
Public Functions
-
inline const char *what() const noexcept override#
Returns the error message, including any appended context.
- Returns:
The expanded message when present, otherwise the original std::runtime_error message.
-
inline const char *what() const noexcept override#
-
using IR_VERSION_TYPE = decltype(Version::IR_VERSION)#
-
namespace checker#