评价此页

torch.onnx.verification#

创建于: 2025年3月18日 | 最后更新于: 2025年6月10日

ONNX 验证模块提供了一系列用于验证 ONNX 模型正确性的工具。

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

通过比较 ExportedProgram 的预期值来验证 ONNX 模型。

参数
  • onnx_program (_onnx_program.ONNXProgram) – 要验证的 ONNX 程序。

  • args (tuple[Any, ...] | None) – 模型的输入参数。

  • kwargs (dict[str, Any] | None) – 模型的关键字参数。

  • compare_intermediates (bool) – 是否验证中间值。这将花费更长的时间,因此默认禁用。

返回

包含每个值的验证信息的 VerificationInfo 对象。

返回类型

list[VerificationInfo]

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

ONNX 程序中值的验证信息。

此类包含预期值和实际值之间的最大绝对差、最大相对差以及绝对差和相对差的直方图。它还包括预期的和实际的数据类型。

直方图表示为张量元组,其中第一个张量是直方图计数,第二个张量是分箱边缘。

变量
  • name (str) – 值的名称(输出或中间值)。

  • max_abs_diff (float) – 预期值和实际值之间的最大绝对差。

  • max_rel_diff (float) – 预期值和实际值之间的最大相对差。

  • abs_diff_hist (tuple[torch.Tensor, torch.Tensor]) – 一个表示绝对差直方图的张量元组。第一个张量是直方图计数,第二个张量是分箱边缘。

  • rel_diff_hist (tuple[torch.Tensor, torch.Tensor]) – 一个表示相对差直方图的张量元组。第一个张量是直方图计数,第二个张量是分箱边缘。

  • expected_dtype (torch.dtype) – 预期值的数据类型。

  • actual_dtype (torch.dtype) – 实际值的实际数据类型。

asdict()[source]#

将 VerificationInfo 对象转换为字典。

返回

VerificationInfo 对象的字典表示。

返回类型

dict[str, Any]

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

从两个张量创建 VerificationInfo 对象。

参数
返回

VerificationInfo 对象。

返回类型

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]#

验证模型导出到 ONNX 与原始 PyTorch 模型的一致性。

已弃用,版本 2.7 起:请考虑使用 torch.onnx.export(..., dynamo=True) 并使用返回的 ONNXProgram 来测试 ONNX 模型。

参数
引发
  • AssertionError – 如果 ONNX 模型和 PyTorch 模型的输出在指定精度下不相等。

  • ValueError – 如果提供的参数无效。

已弃用#

以下类和函数已弃用。

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[源代码]#
class torch.onnx.verification.OnnxTestCaseRepro[源代码]#
class torch.onnx.verification.VerificationOptions[源代码]#
torch.onnx.verification.find_mismatch()[源代码]#
torch.onnx.verification.verify_aten_graph()[源代码]#