onnx_verify.h#
Schema-free structural validation for onnx_proto messages.
The functions declared here validate that a protobuf is internally consistent on its own terms: required fields are set, names are unique, the graph is in single static assignment (SSA) form and topologically sorted, tensor payload sizes match the declared shape/dtype, and so on. None of these checks needs an operator-schema registry: nothing here depends on the semantics of a specific op_type.
For schema-aware validation (operator input/output arity, attribute constraints, type/shape inference), use onnx_lib::checker::check_model() instead (declared in onnx_lib/checker.h).
Every function below throws std::invalid_argument (via EXT_THROW_INVALID / EXT_ENFORCE_INVALID) with a descriptive message on the first violation found.
-
namespace onnx_light
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when
lib_onnx_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.Functions
- ONNX_LIGHT_PROTO_API void VerifyValueInfo (const ValueInfoProto &value_info, bool is_main_graph=true)
Validates a ValueInfoProto.
- ONNX_LIGHT_PROTO_API void VerifyTensor (const TensorProto &tensor)
Validates a TensorProto: data_type is set, at most one payload field is populated, and the populated field matches the declared data_type.
- ONNX_LIGHT_PROTO_API void VerifySparseTensor (const SparseTensorProto &sparse_tensor)
Validates a SparseTensorProto:
valuesandindicesare individually valid tensors andindicesuses the required INT64 data type.- Parameters:
sparse_tensor – Sparse tensor to validate.
- Throws:
std::invalid_argument – Thrown when validation fails.
- ONNX_LIGHT_PROTO_API void VerifyAttribute (const AttributeProto &attribute, bool in_function_body, const std::unordered_set< std::string > &scope)
Validates an AttributeProto: exactly one value field is populated and it matches the declared
type; nested graphs/tensors are recursively validated.- Parameters:
attribute – Attribute to validate.
in_function_body – True when the attribute belongs to a node inside a FunctionProto body, in which case
ref_attr_nameis permitted.scope – Names visible to the attribute’s nested subgraph (if any), i.e. every name defined so far in the enclosing graph. Used to validate that control-flow body subgraphs (Attribute.g / Attribute.graphs) can legally reference outer-scope values.
- Throws:
std::invalid_argument – Thrown when validation fails.
- ONNX_LIGHT_PROTO_API void VerifyNode (const NodeProto &node, bool in_function_body, const std::unordered_set< std::string > &scope)
Validates a NodeProto:
op_typeis set, the node has at least one input or output, attribute names are unique, and each attribute is valid.- Parameters:
node – Node to validate.
in_function_body – True when
nodebelongs to a FunctionProto body.scope – Names visible to the node, forwarded to VerifyAttribute() for nested subgraph validation.
- Throws:
std::invalid_argument – Thrown when validation fails.
- ONNX_LIGHT_PROTO_API void VerifyGraph (const GraphProto &graph, bool is_main_graph=true, bool in_function_body=false, const std::unordered_set< std::string > *outer_scope=nullptr)
Validates a GraphProto: inputs/outputs/initializers have unique names, the graph is in SSA form, nodes are topologically sorted (every input is produced by a prior node, initializer, graph input, or outer scope), and every declared output is actually produced.
- Parameters:
graph – Graph to validate.
is_main_graph – When false, relaxes the ValueInfoProto
typerequirement on graph inputs/outputs (subgraphs may omit it).in_function_body – True when
graphis nested inside a FunctionProto body.outer_scope – Names visible from an enclosing graph (used when validating a control-flow body subgraph); may be null for the main graph.
- Throws:
std::invalid_argument – Thrown when validation fails.
- ONNX_LIGHT_PROTO_API void VerifyFunction (const FunctionProto &function)
Validates a FunctionProto:
nameis set, inputs are uniquely named, nodes are topologically sorted with respect to the function’s inputs, and every declared output is produced.- Parameters:
function – Function to validate.
- Throws:
std::invalid_argument – Thrown when validation fails.
- ONNX_LIGHT_PROTO_API void VerifyModel (const ModelProto &model)
Validates an in-memory ModelProto without requiring an operator-schema registry:
graphis present, at least one opset is imported with unique domains, the main graph is structurally valid, and model-local functions are individually valid and uniquely identified by (domain, name, overload).This performs IR-level structural checks only; it does not check operator input/output arity or attribute constraints, and it does not run shape inference. Use
onnx_lib::checker::check_model()for that.- Parameters:
model – Model to validate.
- Throws:
std::invalid_argument – Thrown when a structural inconsistency is found.