Skip to content

Accuracy: Round to nearest, ties to even for generic_float - #5193

Open
CharlieL7 wants to merge 6 commits into
developfrom
rne_for_generic_float
Open

Accuracy: Round to nearest, ties to even for generic_float#5193
CharlieL7 wants to merge 6 commits into
developfrom
rne_for_generic_float

Conversation

@CharlieL7

Copy link
Copy Markdown
Collaborator

Motivation

  • Was hitting accuracy error with generic_float when trying to compare with allclose().

Technical Details

  • Implements RNE for generic_float so that the reference values are correctly rounded when compared to the target results.

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.

Copilot AI lite review requested due to automatic review settings August 26, 2026 16:34
@CharlieL7
CharlieL7 requested review from a team and causten as code owners August 26, 2026 16:34

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

Updates MIGraphX’s generic_float (notably migraphx::half and migraphx::bf16) float32 host conversion to use round-to-nearest, ties-to-even (RNE), aligning with typical hardware conversion behavior and improving numeric agreement with allclose()-style comparisons.

Changes:

  • Implemented RNE rounding in generic_float float32→target conversion, including correct carry/overflow behavior (to infinity when appropriate).
  • Updated unit tests and ONNX verify gold data to reflect RNE results and overflow-to-inf behavior.
  • Adjusted rewrite_resize_test comparisons to use an elementwise tolerance for narrow types to avoid 1-ULP mismatches.

Reviewed changes

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

Show a summary per file
File Description
src/include/migraphx/generic_float.hpp Implements RNE rounding logic for float32 conversions used by half/bf16.
test/half.cpp Adds/updates tests to pin RNE rounding behavior and overflow/subnormal corner cases for half.
test/bf16.cpp Adds/updates tests to pin RNE rounding behavior and overflow/subnormal corner cases for bf16.
test/rewrite_resize_test.cpp Uses elementwise allclose tolerance for narrow output types where interpolation precision differs.
test/onnx/verify/gelu_default_test.cpp Updates expected outputs to match ORT CPU EP rounding behavior.
test/onnx/verify/gelu_default_bf16_test.cpp Refreshes bf16 expectations and comments for the verify test.
test/onnx/verify/group_query_attention_test.cpp Updates expected values/comments to reflect more precise ORT-sourced gold data.
CHANGELOG.md Documents the behavior change for float32→half/bf16 host conversion.

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

Comment thread CHANGELOG.md
* Allow for 1 arg slicing over a dynamic dimension. (#5015)
* Route convolutions and dot operations through rocMLIR when MIOpen or GEMM libraries are disabled at build time (#5059).
* Rejected symbolic input shapes in the multi-input `slice` calls, and pointed both symbolic `slice` errors at `dyn_slice` (#5088).
* Changed host conversion of float32 to `migraphx::half` and `migraphx::bf16` to round to nearest with ties to even instead of truncating, matching the hardware conversions these types emulate. Values above the target type's overflow threshold, including `std::numeric_limits<float>::max()`, now convert to infinity rather than to the largest finite value, so fp16 and bf16 model output can shift by up to one ULP (#5193).
constexpr const int diff = float32_parts::exponent_bias() - exponent_bias();
constexpr const int drop = int(float32_parts::mantissa_width() - MantissaSize);

if(f.exponent != 0 and f.exponent != float32_parts::max_exponent() and

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.

So fp32 and bf16 uses round_normal. Is this an optimization? So are we doing the same RNE algorithm and just taking advantage that it has the same exponent as float to do this automatically?

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

Copy link
Copy Markdown
Test Batch New Rate (63a772) Old Rate (3a503c)* Diff Status
torchvision-resnet50 64 3,301.69 3,264.92 1.13%
torchvision-resnet50_fp16 64 7,887.80 7,548.67 4.49%
torchvision-densenet121 32 2,491.07 2,483.99 0.28%
torchvision-densenet121_fp16 32 5,021.67 5,004.24 0.35%
torchvision-inceptionv3 32 2,059.93 2,058.51 0.07%
torchvision-inceptionv3_fp16 32 4,439.72 4,416.99 0.51%
cadene-inceptionv4 16 817.60 820.61 -0.37%
cadene-resnext64x4 16 784.83 782.78 0.26%
slim-mobilenet 64 8,377.53 8,386.36 -0.11%
slim-nasnetalarge 64 229.30 228.86 0.19%
slim-resnet50v2 64 3,230.35 3,180.91 1.55%
bert-mrpc-onnx 8 1,170.44 1,168.84 0.14%
bert-mrpc-tf 1 502.96 498.63 0.87%
pytorch-examples-wlang-gru 1 373.92 473.35 -21.01% 🔴
pytorch-examples-wlang-lstm 1 556.95 384.83 44.73% 🔆
torchvision-resnet50_1 1 1,054.96 1,046.63 0.80%
cadene-dpn92_1 1 448.99 437.32 2.67%
cadene-resnext101_1 1 364.28 365.89 -0.44%
onnx-taau-downsample 1 846.17 844.09 0.25%
dlrm-criteoterabyte 1 32.26 32.42 -0.50%
dlrm-criteoterabyte_fp16 1 51.54 51.80 -0.51%
agentmodel 1 14,407.31 9,209.12 56.45% 🔆
unet_fp16 2 58.40 58.80 -0.68%
resnet50v1_fp16 1 1,460.31 1,366.11 6.90% 🔆
resnet50v1_int8 1 1,802.32 1,883.96 -4.33%
bert_base_cased_fp16 64 1,099.47 1,098.16 0.12%
bert_large_uncased_fp16 32 347.33 345.59 0.50%
bert_large_fp16 1 206.52 206.59 -0.04%
distilgpt2_fp16 16 2,101.91 2,092.89 0.43%
yolov5s 1 550.92 558.33 -1.33%
tinyllama 1 45.85 45.83 0.03%
vicuna-fastchat 1 44.32 44.20 0.28%
whisper-tiny-encoder 1 413.47 411.87 0.39%
whisper-tiny-decoder 1 409.25 408.48 0.19%
llama2_7b 1 20.88 20.84 0.20%
qwen1.5-7b 1 23.70 23.58 0.50%
phi3-3.8b 1 28.35 26.72 6.12% 🔆
llama3-8b 1 22.76 21.80 4.37%
whisper-large-encoder 1 10.17 10.18 -0.09%
whisper-large-decoder 1 108.05 105.30 2.61%
mistral-7b 1 23.79 23.78 0.06%
FLUX.1-schnell 1 783.10 755.22 3.69%

Regressions detected 🔴

* 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 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
pytorch-examples-wlang-lstm 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
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