validation
orchard.export.validation
¶
Export Numerical Validation.
Verifies that the ONNX graph produces outputs numerically equivalent to the
original PyTorch model. validate_export runs N random forward passes
through both runtimes (PyTorch on CPU, onnxruntime CPUExecutionProvider)
and asserts that the maximum absolute deviation stays below a configurable
threshold (default 1e-4). Called automatically by the export pipeline when
validate=True.
validate_export(pytorch_model, onnx_path, input_shape, num_samples=10, max_deviation=0.0001, label='ONNX')
¶
Validate ONNX export against PyTorch model.
Compares outputs from PyTorch and ONNX models on random inputs to ensure numerical consistency after export.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pytorch_model
|
Module
|
Original PyTorch model (with loaded weights) |
required |
onnx_path
|
Path
|
Path to exported ONNX model |
required |
input_shape
|
tuple[int, int, int]
|
Input tensor shape (C, H, W) |
required |
num_samples
|
int
|
Number of random samples to test |
10
|
max_deviation
|
float
|
Maximum allowed absolute difference |
0.0001
|
label
|
str
|
Display label for log header (e.g. "ONNX", "Quantized") |
'ONNX'
|
Returns:
| Type | Description |
|---|---|
bool | None
|
True if validation passes, False if outputs diverge, |
bool | None
|
None if skipped (onnxruntime not installed). |
Raises:
| Type | Description |
|---|---|
OrchardExportError
|
If the ONNX file does not exist. |
Example
model.load_state_dict(torch.load("checkpoint.pth")) valid = validate_export(model, Path("model.onnx"), input_shape=(3, 224, 224)) if valid: ... print("Export validated successfully!")
Source code in orchard/export/validation.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |