Principle:PeterL1n BackgroundMattingV2 ONNX validation
| Knowledge Sources | |
|---|---|
| Domains | Model_Deployment, Testing |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
A numerical verification technique that compares ONNX model outputs against the original PyTorch model to ensure export correctness within a specified tolerance.
Description
ONNX validation verifies that the exported ONNX model produces outputs numerically close to the original PyTorch model. This is critical because the ONNX export process can introduce subtle numerical differences due to operator implementation variations, precision handling, and graph optimizations.
The validation procedure:
- Generates test inputs with different dimensions than the export dummy inputs (to test dynamic axes)
- Runs inference with the original PyTorch model
- Runs inference with the exported ONNX model via ONNX Runtime
- Computes the maximum absolute difference per output tensor
- Passes if all differences are below a threshold (0.005)
Usage
Use this principle after every ONNX export to verify correctness. Enable via the --validate flag. Failed validation indicates an incompatibility between the chosen patch crop/replace methods and the ONNX runtime.
Theoretical Basis
The validation metric is the maximum absolute element-wise difference:
The threshold e_max < 0.005 accounts for floating-point arithmetic differences between PyTorch and ONNX Runtime implementations.
Pseudo-code:
# Abstract ONNX validation
pytorch_outputs = pytorch_model(test_src, test_bgr)
onnx_session = load_onnx(exported_path)
onnx_outputs = onnx_session.run({'src': test_src, 'bgr': test_bgr})
for pytorch_out, onnx_out in zip(pytorch_outputs, onnx_outputs):
error = max_abs_difference(pytorch_out, onnx_out)
assert error < 0.005, "Validation failed"