Repository navigation
Add centralized tolerance registry and integrate with Tester harness #20014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RiyaP2508
wants to merge
2
commits into
pytorch:main
Choose a base branch
from
RiyaP2508:rpunia/tolerance-registry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from .tester import Tester | ||
| from .tolerance import get_tolerance, ToleranceConfig | ||
|
|
||
| __all__ = ["Tester"] | ||
| __all__ = ["Tester", "ToleranceConfig", "get_tolerance"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import unittest | ||
|
|
||
| import torch | ||
| from executorch.backends.test.harness.tolerance import ( | ||
| _dtype_to_key, | ||
| BACKEND_TOLERANCES, | ||
| get_tolerance, | ||
| ToleranceConfig, | ||
| ) | ||
|
|
||
|
|
||
| class TestToleranceConfig(unittest.TestCase): | ||
| def test_default_values(self): | ||
| config = ToleranceConfig() | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
| self.assertEqual(config.qtol, 0) | ||
|
|
||
| def test_custom_values(self): | ||
| config = ToleranceConfig(atol=1e-2, rtol=5e-2, qtol=2) | ||
| self.assertEqual(config.atol, 1e-2) | ||
| self.assertEqual(config.rtol, 5e-2) | ||
| self.assertEqual(config.qtol, 2) | ||
|
|
||
| def test_frozen(self): | ||
| config = ToleranceConfig() | ||
| with self.assertRaises(AttributeError): | ||
| config.atol = 0.5 # type: ignore[misc] | ||
|
|
||
| def test_with_quantization_scale(self): | ||
| config = ToleranceConfig(atol=1e-3, rtol=1e-3, qtol=2) | ||
| scale = 0.05 | ||
| adjusted = config.with_quantization_scale(scale) | ||
|
|
||
| self.assertAlmostEqual(adjusted.atol, 1e-3 + 0.05 * 2) | ||
| self.assertEqual(adjusted.rtol, 1e-3) | ||
| self.assertEqual(adjusted.qtol, 2) | ||
|
|
||
| def test_with_quantization_scale_zero_qtol(self): | ||
| config = ToleranceConfig(atol=1e-3, rtol=1e-3, qtol=0) | ||
| adjusted = config.with_quantization_scale(0.05) | ||
|
|
||
| self.assertEqual(adjusted.atol, 1e-3) | ||
|
|
||
| def test_with_quantization_scale_returns_new_instance(self): | ||
| config = ToleranceConfig(atol=1e-3, rtol=1e-3, qtol=1) | ||
| adjusted = config.with_quantization_scale(0.1) | ||
|
|
||
| self.assertIsNot(config, adjusted) | ||
| self.assertEqual(config.atol, 1e-3) | ||
|
|
||
| def test_equality(self): | ||
| a = ToleranceConfig(atol=1e-3, rtol=1e-3, qtol=0) | ||
| b = ToleranceConfig(atol=1e-3, rtol=1e-3, qtol=0) | ||
| self.assertEqual(a, b) | ||
|
|
||
| def test_inequality(self): | ||
| a = ToleranceConfig(atol=1e-3, rtol=1e-3, qtol=0) | ||
| b = ToleranceConfig(atol=1e-2, rtol=1e-3, qtol=0) | ||
| self.assertNotEqual(a, b) | ||
|
|
||
|
|
||
| class TestDtypeToKey(unittest.TestCase): | ||
| def test_quantized_always_wins(self): | ||
| self.assertEqual(_dtype_to_key(torch.float32, quantized=True), "quantized") | ||
| self.assertEqual(_dtype_to_key(torch.float16, quantized=True), "quantized") | ||
| self.assertEqual(_dtype_to_key(torch.bfloat16, quantized=True), "quantized") | ||
|
|
||
| def test_fp16(self): | ||
| self.assertEqual(_dtype_to_key(torch.float16, quantized=False), "fp16") | ||
|
|
||
| def test_bf16(self): | ||
| self.assertEqual(_dtype_to_key(torch.bfloat16, quantized=False), "bf16") | ||
|
|
||
| def test_fp32_default(self): | ||
| self.assertEqual(_dtype_to_key(torch.float32, quantized=False), "default") | ||
|
|
||
| def test_fp64_default(self): | ||
| self.assertEqual(_dtype_to_key(torch.float64, quantized=False), "default") | ||
|
|
||
| def test_int_types_default(self): | ||
| self.assertEqual(_dtype_to_key(torch.int32, quantized=False), "default") | ||
| self.assertEqual(_dtype_to_key(torch.int64, quantized=False), "default") | ||
|
|
||
|
|
||
| class TestGetTolerance(unittest.TestCase): | ||
| # --- Known backends --- | ||
|
|
||
| def test_xnnpack_default(self): | ||
| config = get_tolerance("xnnpack") | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
|
|
||
| def test_xnnpack_fp16(self): | ||
| config = get_tolerance("xnnpack", dtype=torch.float16) | ||
| self.assertEqual(config.atol, 2e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
|
|
||
| def test_xnnpack_quantized(self): | ||
| config = get_tolerance("xnnpack", quantized=True) | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
| self.assertEqual(config.qtol, 1) | ||
|
|
||
| def test_vulkan_default(self): | ||
| config = get_tolerance("vulkan") | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-1) | ||
|
|
||
| def test_vulkan_fp16(self): | ||
| config = get_tolerance("vulkan", dtype=torch.float16) | ||
| self.assertEqual(config.atol, 1e-2) | ||
| self.assertEqual(config.rtol, 1e-2) | ||
|
|
||
| def test_coreml_quantized(self): | ||
| config = get_tolerance("coreml", quantized=True) | ||
| self.assertEqual(config.atol, 5e-2) | ||
| self.assertEqual(config.rtol, 5e-2) | ||
|
|
||
| def test_metal_bf16(self): | ||
| config = get_tolerance("metal", dtype=torch.bfloat16) | ||
| self.assertEqual(config.atol, 1e-2) | ||
| self.assertEqual(config.rtol, 1e-2) | ||
|
|
||
| def test_metal_fp32(self): | ||
| config = get_tolerance("metal") | ||
| self.assertEqual(config.atol, 1e-5) | ||
| self.assertEqual(config.rtol, 1e-5) | ||
|
|
||
| def test_qnn_quantized(self): | ||
| config = get_tolerance("qnn", quantized=True) | ||
| self.assertEqual(config.atol, 1e-1) | ||
| self.assertEqual(config.rtol, 1.0) | ||
|
|
||
| def test_arm_quantized(self): | ||
| config = get_tolerance("arm", quantized=True) | ||
| self.assertEqual(config.qtol, 1) | ||
|
|
||
| def test_arm_bf16(self): | ||
| config = get_tolerance("arm", dtype=torch.bfloat16) | ||
| self.assertEqual(config.atol, 1e-2) | ||
|
|
||
| def test_nxp_default(self): | ||
| config = get_tolerance("nxp") | ||
| self.assertEqual(config.atol, 1e-8) | ||
| self.assertEqual(config.rtol, 1e-5) | ||
|
|
||
| def test_nxp_quantized(self): | ||
| config = get_tolerance("nxp", quantized=True) | ||
| self.assertEqual(config.atol, 1.0) | ||
|
|
||
| def test_mlx_fp16(self): | ||
| config = get_tolerance("mlx", dtype=torch.float16) | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
|
|
||
| def test_mlx_bf16(self): | ||
| config = get_tolerance("mlx", dtype=torch.bfloat16) | ||
| self.assertEqual(config.atol, 1e-2) | ||
| self.assertEqual(config.rtol, 1e-2) | ||
|
|
||
| # --- Fallback behavior --- | ||
|
|
||
| def test_unknown_backend_returns_global_default(self): | ||
| config = get_tolerance("nonexistent_backend") | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
| self.assertEqual(config.qtol, 0) | ||
|
|
||
| def test_unknown_dtype_falls_back_to_backend_default(self): | ||
| config = get_tolerance("xnnpack", dtype=torch.float64) | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
|
|
||
| def test_samsung_no_quantized_falls_back_to_default(self): | ||
| config = get_tolerance("samsung", quantized=True) | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
|
|
||
| def test_webgpu_fp16_falls_back_to_default(self): | ||
| config = get_tolerance("webgpu", dtype=torch.float16) | ||
| self.assertEqual(config.atol, 1e-3) | ||
| self.assertEqual(config.rtol, 1e-3) | ||
|
|
||
| # --- Op parameter (reserved for future use) --- | ||
|
|
||
| def test_op_parameter_currently_ignored(self): | ||
| config_with_op = get_tolerance("xnnpack", op="aten.exp") | ||
| config_without_op = get_tolerance("xnnpack") | ||
| self.assertEqual(config_with_op, config_without_op) | ||
|
|
||
| # --- Registry completeness --- | ||
|
|
||
| def test_all_backends_have_default_key(self): | ||
| for backend_name, backend_config in BACKEND_TOLERANCES.items(): | ||
| self.assertIn( | ||
| "default", | ||
| backend_config, | ||
| f"Backend '{backend_name}' is missing a 'default' tolerance entry", | ||
| ) | ||
|
|
||
| def test_all_tolerance_values_are_non_negative(self): | ||
| for backend_name, backend_config in BACKEND_TOLERANCES.items(): | ||
| for dtype_key, config in backend_config.items(): | ||
| self.assertGreaterEqual( | ||
| config.atol, | ||
| 0, | ||
| f"{backend_name}/{dtype_key} has negative atol", | ||
| ) | ||
| self.assertGreaterEqual( | ||
| config.rtol, | ||
| 0, | ||
| f"{backend_name}/{dtype_key} has negative rtol", | ||
| ) | ||
| self.assertGreaterEqual( | ||
| config.qtol, | ||
| 0, | ||
| f"{backend_name}/{dtype_key} has negative qtol", | ||
| ) | ||
|
|
||
| def test_registry_has_expected_backends(self): | ||
| expected = { | ||
| "xnnpack", | ||
| "vulkan", | ||
| "coreml", | ||
| "mps", | ||
| "metal", | ||
| "qnn", | ||
| "arm", | ||
| "cortex_m", | ||
| "samsung", | ||
| "mlx", | ||
| "openvino", | ||
| "nxp", | ||
| "cadence", | ||
| "cuda", | ||
| "webgpu", | ||
| } | ||
| self.assertEqual(set(BACKEND_TOLERANCES.keys()), expected) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lookup always uses the default
dtype=torch.float32andquantized=False. Withbackend="xnnpack", fp16 comparisons therefore getatol=0.001instead of0.002, and quantized comparisons getqtol=0instead of1.Could we pass the output dtype and quantization state into the lookup? A test through
Tester.run_method_and_compare_outputs()would catch this; the current tests only exercise the registry directly.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rascani yeah you're right -- it's always hitting "default" right now. The fix should be straightforward since quantization_scale and reference_output.dtype are already available inside the loop:
Same quantization_scale signal that's already used at line 514. I'll add a test through run_method_and_compare_outputs() for the fp16/quantized paths too.
Happy to adjust if you have a better approach in mind!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deriving the dtype/quantization from a output dtype assumes that output metadata represents the computation's numerical behavior. Its fairly common for outputs to be dequantized. I'd prefer the backend/test configuration select the intended precision and quantization profile.