Rate this Page

torch.onnx.verification#

Created On: Mar 18, 2025 | Last Updated: Mar 18, 2025

The ONNX verification module provides a set of tools to verify the correctness of ONNX models.

torch.onnx.verification.verify_onnx_program(onnx_program, args=None, kwargs=None, compare_intermediates=False)[source]#

Verify the ONNX model by comparing the values with the expected values from ExportedProgram.

Parameters
  • onnx_program (_onnx_program.ONNXProgram) – The ONNX program to verify.

  • args (tuple[Any, ...] | None) – The input arguments for the model.

  • kwargs (dict[str, Any] | None) – The keyword arguments for the model.

  • compare_intermediates (bool) – Whether to verify intermediate values. This is going to take longer time, so it is disabled by default.

Returns

VerificationInfo objects containing the verification information for each value.

Return type

list[VerificationInfo]

class torch.onnx.verification.VerificationInfo(name, max_abs_diff, max_rel_diff, abs_diff_hist, rel_diff_hist, expected_dtype, actual_dtype)#

Verification information for a value in the ONNX program.

This class contains the maximum absolute difference, maximum relative difference, and histograms of absolute and relative differences between the expected and actual values. It also includes the expected and actual data types.

The histograms are represented as tuples of tensors, where the first tensor is the histogram counts and the second tensor is the bin edges.

Variables
  • name (str) – The name of the value (output or intermediate).

  • max_abs_diff (float) – The maximum absolute difference between the expected and actual values.

  • max_rel_diff (float) – The maximum relative difference between the expected and actual values.

  • abs_diff_hist (tuple[torch.Tensor, torch.Tensor]) – A tuple of tensors representing the histogram of absolute differences. The first tensor is the histogram counts and the second tensor is the bin edges.

  • rel_diff_hist (tuple[torch.Tensor, torch.Tensor]) – A tuple of tensors representing the histogram of relative differences. The first tensor is the histogram counts and the second tensor is the bin edges.

  • expected_dtype (torch.dtype) – The data type of the expected value.

  • actual_dtype (torch.dtype) – The data type of the actual value.

asdict()[source]#

Convert the VerificationInfo object to a dictionary.

Returns

A dictionary representation of the VerificationInfo object.

Return type

dict[str, Any]

classmethod from_tensors(name, expected, actual)[source]#

Create a VerificationInfo object from two tensors.

Parameters
Returns

The VerificationInfo object.

Return type

VerificationInfo

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)[source]#

Verify model export to ONNX against original PyTorch model.

Deprecated since version 2.7: Consider using torch.onnx.export(..., dynamo=True) and use the returned ONNXProgram to test the ONNX model.

Parameters
Raises
  • AssertionError – if outputs from ONNX model and PyTorch model are not equal up to specified precision.

  • ValueError – if arguments provided are invalid.

Deprecated#

The following classes and functions are deprecated.

class torch.onnx.verification.check_export_model_diff[source]#
class torch.onnx.verification.GraphInfo[source]#
class torch.onnx.verification.GraphInfoPrettyPrinter[source]#
class torch.onnx.verification.OnnxBackend[source]#
class torch.onnx.verification.OnnxTestCaseRepro[source]#
class torch.onnx.verification.VerificationOptions[source]#
torch.onnx.verification.find_mismatch()[source]#
torch.onnx.verification.verify_aten_graph()[source]#