Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:PeterL1n BackgroundMattingV2 ONNX validation

From Leeroopedia


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:

  1. Generates test inputs with different dimensions than the export dummy inputs (to test dynamic axes)
  2. Runs inference with the original PyTorch model
  3. Runs inference with the exported ONNX model via ONNX Runtime
  4. Computes the maximum absolute difference per output tensor
  5. 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:

emax=maxi|yPyTorch(i)−yONNX(i)|

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"

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment