Workflow:Onnx Onnx Model Validation
| Knowledge Sources | |
|---|---|
| Domains | ML_Infrastructure, Model_Validation |
| Last Updated | 2026-02-10 02:30 GMT |
Overview
End-to-end process for loading an ONNX model, validating its structural correctness, and enriching it with inferred type and shape information.
Description
This workflow covers the standard procedure for ingesting and verifying ONNX models. It loads a serialized model from disk (supporting binary protobuf, text protobuf, JSON, and ONNX text formats), runs the ONNX checker to validate structural and semantic correctness against the ONNX specification, applies shape inference to propagate tensor types and shapes through the computation graph, and optionally inspects the enriched model metadata. The process handles both standard models and large models exceeding 2GB that use external data storage.
Usage
Execute this workflow when you need to:
- Verify that an ONNX model exported from a framework (PyTorch, TensorFlow, etc.) is spec-compliant
- Diagnose validation errors before deploying a model to an ONNX runtime
- Enrich a model with intermediate tensor shape information for optimization or analysis tools
- Validate models received from external sources before integration into a pipeline
Execution Steps
Step 1: Load the Model
Deserialize the ONNX model from its on-disk representation into an in-memory ModelProto object. The loader automatically detects the serialization format from the file extension and resolves external tensor data if present in the same directory.
Key considerations:
- For models with external data in a different directory, disable automatic external data loading and load it separately with an explicit directory path
- For models larger than 2GB, use the file path directly rather than loading into memory (protobuf has a 2GB limit for in-memory messages)
- Supported formats include .onnx (binary protobuf), .onnxtxt (text syntax), .json, and .textproto
Step 2: Run the Model Checker
Execute the ONNX checker against the loaded model to validate structural and semantic correctness. The checker verifies IR version compatibility, opset import declarations, node operator schemas, type constraints, attribute validity, graph connectivity, and metadata consistency.
Key considerations:
- The checker raises a ValidationError with a descriptive message if any issue is found
- For large models (>2GB), pass the file path string directly to the checker instead of the in-memory ModelProto
- The skip_opset_compatibility_check parameter can bypass opset validation for custom or experimental operators
- The check_custom_domain parameter extends validation to non-standard operator domains
Step 3: Apply Shape Inference
Run shape inference to propagate type and shape information from model inputs through all intermediate tensors to the outputs. The inference engine uses each operator's registered shape inference function to compute output types and shapes from input types and shapes.
Key considerations:
- Shape inference adds ValueInfoProto entries to the graph's value_info field for intermediate tensors
- The strict_mode parameter causes inference to raise errors rather than silently stopping on failures
- The data_prop parameter enables limited constant propagation for operators like Shape, Size, and Gather
- For models larger than 2GB, use infer_shapes_path which operates on the file directly
Step 4: Inspect the Enriched Model
Examine the model's metadata, graph structure, and inferred shape information. This includes reviewing opset imports, checking input/output specifications, inspecting intermediate value_info entries added by shape inference, and examining individual node configurations.
Key considerations:
- The model's graph.value_info field contains the shapes added by inference
- Use the ONNX printer module to convert the model to a human-readable text format for inspection
- The helper module provides utility functions for extracting type and shape information from TypeProto objects
Step 5: Save the Validated Model
Persist the validated and shape-enriched model back to disk. The enriched model with inferred shapes is useful for downstream tools that benefit from complete type information.
Key considerations:
- Saving after shape inference preserves the inferred value_info in the output file
- Choose the appropriate serialization format based on the downstream consumer's requirements