Skip to content

Fix matcher has_value tolerance for low precision types - #5190

Open
CharlieL7 wants to merge 5 commits into
developfrom
matcher_has_value_tolerance
Open

Fix matcher has_value tolerance for low precision types#5190
CharlieL7 wants to merge 5 commits into
developfrom
matcher_has_value_tolerance

Conversation

@CharlieL7

Copy link
Copy Markdown
Collaborator

Motivation

  • has_value matcher uses a set epsilon multiple that only makes sense for fp32 types.
  • Make different tolerances for low precision types that are more reasonable.

Technical Details

  • Set smaller multiple for fp16 and fp8 types.

Changelog Category

Add a CHANGELOG.md entry for any option other than Not Applicable

    • Added: New functionality.
    • Changed: Changes to existing functionality.
    • Removed: Functionality or support that has been removed. (Compared to a previous release)
    • Optimized: Component performance that has been optimized or improved.
    • Resolved Issues: Known issues from a previous version that have been resolved.
    • Not Applicable: This PR is not to be included in the changelog.

Follow the LLVM AI Tool Use Policy for contributions using AI.

@CharlieL7
CharlieL7 requested a review from a team as a code owner August 25, 2026 19:04
Copilot AI lite review requested due to automatic review settings August 25, 2026 19:04
@CharlieL7
CharlieL7 requested a review from causten as a code owner August 25, 2026 19:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the match::has_value literal matcher to use type-specific tolerance windows so that comparisons remain meaningful for low-precision floating types (notably fp8), while preserving the historical behavior for float. This fits into MIGraphX’s matcher infrastructure by making constant-pattern matching more robust across literal element types.

Changes:

  • Introduces per-shape::type_t default tolerance multipliers and refactors the has_value implementation to apply them.
  • Updates has_value comparison logic to compute the difference in double, avoiding extra rounding in narrow types.
  • Adds new matcher tests covering fp8 and bf16 neighbor/rounding cases, and documents the behavior change in the changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/include/migraphx/matcher.hpp Adds per-type default tolerances and refactors has_value matching logic to avoid overly-wide windows for fp8/bf16/half.
test/matcher.cpp Adds regression tests ensuring has_value doesn’t match neighboring fp8/bf16 representable values and preserves float behavior.
CHANGELOG.md Documents the behavioral fix to has_value for narrow types and its downstream impact on passes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1243 to +1247
// A literal views const data, so drop the qualifier or numeric_limits will miss the
// specialization for the narrow types and report an epsilon of zero.
using type = std::remove_cv_t<typename decltype(v)::value_type>;
auto eps = static_cast<double>(std::numeric_limits<type>::epsilon());
auto window = eps * (atol + rtol * std::fabs(target));
Comment on lines +1185 to +1186
double atol_mult = 0;
double rtol_mult = 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we name this without the _mult postfix:

Suggested change
double atol_mult = 0;
double rtol_mult = 0;
double atol = 0;
double rtol = 0;

/// The window has_value uses when the caller does not name one. Every enumerator is listed so that
/// a type added to shape::type_t has to name its own window here rather than silently picking up a
/// window sized for float.
inline value_tolerance default_value_tolerance(shape::type_t t)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this into the value_tolerance class and rename it to get_defaults.

Comment on lines +1234 to +1235
optional<double> atol_mult,
optional<double> rtol_mult)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pass the value_tolerance. Probably need to make them optional<double> in the struct.


/// As above with the epsilon multiples given explicitly. Both zero requires an exact match.
template <class T>
inline auto has_value(T x, double atol_mult, double rtol_mult)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should take the value_tolerance struct. Since its an aggregate type it will help make it clearer which value is being set: has_value(x, {.atol = 1e-3, .rtol = 1e-3})

@gh-app-migraphx-bot-pr-write

Copy link
Copy Markdown
Test Batch New Rate (d1d375) Old Rate (3a503c)* Diff Status
torchvision-resnet50 64 3,322.36 3,264.92 1.76%
torchvision-resnet50_fp16 64 7,874.46 7,548.67 4.32%
torchvision-densenet121 32 2,490.25 2,483.99 0.25%
torchvision-densenet121_fp16 32 5,022.07 5,004.24 0.36%
torchvision-inceptionv3 32 2,070.96 2,058.51 0.60%
torchvision-inceptionv3_fp16 32 4,444.70 4,416.99 0.63%
cadene-inceptionv4 16 817.76 820.61 -0.35%
cadene-resnext64x4 16 782.56 782.78 -0.03%
slim-mobilenet 64 8,381.66 8,386.36 -0.06%
slim-nasnetalarge 64 228.31 228.86 -0.24%
slim-resnet50v2 64 3,238.18 3,180.91 1.80%
bert-mrpc-onnx 8 1,170.21 1,168.84 0.12%
bert-mrpc-tf 1 499.69 498.63 0.21%
pytorch-examples-wlang-gru 1 488.75 473.35 3.25%
pytorch-examples-wlang-lstm 1 406.70 384.83 5.68% 🔆
torchvision-resnet50_1 1 1,055.62 1,046.63 0.86%
cadene-dpn92_1 1 448.38 437.32 2.53%
cadene-resnext101_1 1 366.77 365.89 0.24%
onnx-taau-downsample 1 845.97 844.09 0.22%
dlrm-criteoterabyte 1 32.27 32.42 -0.47%
dlrm-criteoterabyte_fp16 1 51.60 51.80 -0.39%
agentmodel 1 14,684.31 9,209.12 59.45% 🔆
unet_fp16 2 58.31 58.80 -0.83%
resnet50v1_fp16 1 1,441.94 1,366.11 5.55% 🔆
resnet50v1_int8 1 1,839.44 1,883.96 -2.36%
bert_base_cased_fp16 64 1,097.48 1,098.16 -0.06%
bert_large_uncased_fp16 32 345.92 345.59 0.10%
bert_large_fp16 1 205.89 206.59 -0.34%
distilgpt2_fp16 16 2,082.67 2,092.89 -0.49%
yolov5s 1 562.06 558.33 0.67%
tinyllama 1 45.84 45.83 0.01%
vicuna-fastchat 1 44.24 44.20 0.08%
whisper-tiny-encoder 1 412.91 411.87 0.25%
whisper-tiny-decoder 1 410.63 408.48 0.53%
llama2_7b 1 20.86 20.84 0.10%
qwen1.5-7b 1 23.69 23.58 0.45%
phi3-3.8b 1 28.38 26.72 6.22% 🔆
llama3-8b 1 22.72 21.80 4.22%
whisper-large-encoder 1 10.17 10.18 -0.07%
whisper-large-decoder 1 107.71 105.30 2.28%
mistral-7b 1 23.78 23.78 0.02%
FLUX.1-schnell 1 784.13 755.22 3.83%

Check flagged results 🔆

* No develop baseline was found for this PR's branch point; compared against the latest available develop run instead.

@gh-app-migraphx-bot-pr-write

Copy link
Copy Markdown
Test Status Result
bert-mrpc-onnx PASSED: MIGraphX meets tolerance
bert-mrpc-tf ERROR - check error output
traceback
Traceback (most recent call last):
File "/src/AMDMIGraphX/tools/accuracy/accuracy_checker.py", line 377, in
main()
File "/src/AMDMIGraphX/tools/accuracy/accuracy_checker.py", line 313, in main
import tensorflow as tf
File "/usr/local/lib/python3.12/dist-packages/tensorflow/init.py", line 40, in
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow # pylint: disable=unused-import
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 37, in
self_check.preload_check()
File "/usr/local/lib/python3.12/dist-packages/tensorflow/python/platform/self_check.py", line 63, in preload_check
from tensorflow.python.platform import _pywrap_cpu_feature_guard
ImportError: libnuma.so.1: cannot open shared object file: No such file or directory
pytorch-examples-wlang-gru PASSED: MIGraphX meets tolerance
pytorch-examples-wlang-lstm PASSED: MIGraphX meets tolerance
dlrm-criteoterabyte PASSED: MIGraphX meets tolerance
agentmodel PASSED: MIGraphX meets tolerance
unet PASSED: MIGraphX meets tolerance
resnet50v1 PASSED: MIGraphX meets tolerance
bert_base_cased_fp16 PASSED: MIGraphX meets tolerance
bert_large_uncased_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
bert_large PASSED: MIGraphX meets tolerance
yolov5s PASSED: MIGraphX meets tolerance
tinyllama PASSED: MIGraphX meets tolerance
vicuna-fastchat PASSED: MIGraphX meets tolerance
whisper-tiny-encoder PASSED: MIGraphX meets tolerance
whisper-tiny-decoder PASSED: MIGraphX meets tolerance
distilgpt2_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
llama2_7b PASSED: MIGraphX meets tolerance
qwen1.5-7b PASSED: MIGraphX meets tolerance
phi3-3.8b PASSED: MIGraphX meets tolerance
llama3-8b PASSED: MIGraphX meets tolerance
whisper-large-encoder PASSED: MIGraphX meets tolerance
whisper-large-decoder PASSED: MIGraphX meets tolerance
mistral-7b PASSED: MIGraphX meets tolerance
FLUX.1-schnell PASSED: MIGraphX meets tolerance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants