torch.onnx.verification¶
ONNX 驗證模組提供了一套工具,用於驗證 ONNX 模型的正確性。
- torch.onnx.verification.verify_onnx_program(onnx_program, args=None, kwargs=None, compare_intermediates=False)[原始碼]¶
透過將 ONNX 模型的值與來自 ExportedProgram 的預期值進行比較來驗證 ONNX 模型。
- class torch.onnx.verification.VerificationInfo(name, max_abs_diff, max_rel_diff, abs_diff_hist, rel_diff_hist, expected_dtype, actual_dtype)¶
ONNX program 中某個值的驗證資訊。
此類包含最大絕對差值、最大相對差值以及預期值與實際值之間絕對差值和相對差值的直方圖。它還包括預期和實際資料型別。
直方圖表示為張量的元組,其中第一個張量是直方圖計數,第二個張量是 bin 邊界。
- 變數
name (str) – 值的名稱(輸出或中間值)。
max_abs_diff (float) – 預期值與實際值之間的最大絕對差值。
max_rel_diff (float) – 預期值與實際值之間的最大相對差值。
abs_diff_hist (tuple[torch.Tensor, torch.Tensor]) – 表示絕對差值直方圖的張量元組。第一個張量是直方圖計數,第二個張量是 bin 邊界。
rel_diff_hist (tuple[torch.Tensor, torch.Tensor]) – 表示相對差值直方圖的張量元組。第一個張量是直方圖計數,第二個張量是 bin 邊界。
expected_dtype (torch.dtype) – 預期值的資料型別。
actual_dtype (torch.dtype) – 實際值的資料型別。
- torch.onnx.verification.verify(model, input_args, input_kwargs=None, do_constant_folding=True, dynamic_axes=None, input_names=None, output_names=None, training=<TrainingMode.EVAL: 0>, opset_version=None, keep_initializers_as_inputs=True, verbose=False, fixed_batch_size=False, use_external_data=False, additional_test_inputs=None, options=None)[原始碼][原始碼]¶
驗證模型匯出到 ONNX 是否與原始 PyTorch 模型一致。
自版本 2.7 起已棄用: 考慮使用
torch.onnx.export(..., dynamo=True)並使用返回的ONNXProgram來測試 ONNX 模型。- 引數
model (_ModelType) – 參見
torch.onnx.export()。input_args (_InputArgsType) – 參見
torch.onnx.export()。input_kwargs (_InputKwargsType | None) – 參見
torch.onnx.export()。do_constant_folding (bool) – 參見
torch.onnx.export()。dynamic_axes (Mapping[str, Mapping[int, str] | Mapping[str, Sequence[int]]] | None) – 參見
torch.onnx.export()。input_names (Sequence[str] | None) – 參見
torch.onnx.export()。output_names (Sequence[str] | None) – 參見
torch.onnx.export()。training (_C_onnx.TrainingMode) – 參見
torch.onnx.export()。opset_version (int | None) – 參見
torch.onnx.export()。keep_initializers_as_inputs (bool) – 參見
torch.onnx.export()。verbose (bool) – 參見
torch.onnx.export()。fixed_batch_size (bool) – 遺留引數,僅用於 RNN 測試用例。
use_external_data (bool) – 明確指定是否使用外部資料匯出模型。
additional_test_inputs (Sequence[_InputArgsType] | None) – 元組列表。每個元組是一組要測試的輸入引數。當前僅支援
*args。options (VerificationOptions | None) – 一個控制驗證行為的 VerificationOptions 物件。
- 觸發
AssertionError – 如果 ONNX 模型和 PyTorch 模型的輸出在指定精度內不相等。
ValueError – 如果提供的引數無效。
已棄用¶
以下類和函式已棄用。