Skip to content

Preserve FP16 rounding before explicit consumer Casts - #34012

Open
Asuka (akasakariko) wants to merge 1 commit into
microsoft:mainfrom
akasakariko:fix/fp16-matmul-cast-rounding
Open

Asuka (akasakariko) wants to merge 1 commit into
microsoft:mainfrom
akasakariko:fix/fp16-matmul-cast-rounding

Conversation

@akasakariko

Copy link
Copy Markdown
Contributor

Description

  • Treat inserted FP32-to-FP16 Casts as narrowing when checking whether a model-authored consumer Cast can bypass them
  • Keep the precision-loss exemption for inserted consumer Casts so adjacent CPU FP32 fallback operators can still share FP32 intermediates
  • Add a numerical regression that forces the MatMul fallback path even on CPUs with native FP16 support and checks both FP32 and FP16 outputs with optimizations disabled and extended optimizations enabled

Motivation and Context

Fixes #33998

The duplicate-Cast remover treated inserted downcasts as precision-free, allowing an explicit FP16-to-FP32 Cast to read the unrounded FP32 fallback result

For FP16 inputs representing 0.7, the FP32 output was 0.49027347564697266 instead of the FP16-rounded 0.490234375

Validation

  • Reproduced the issue attachment on Linux x86_64 with ONNX Runtime 1.31.1 under both ORT_DISABLE_ALL and ORT_ENABLE_EXTENDED
  • Built onnxruntime_test_all in Release mode on macOS ARM64
  • Confirmed TransformerTest.Fp16MatMulFallbackPreservesRoundingBeforeCast fails on the unchanged implementation at both optimization levels and passes with this fix
  • ./onnxruntime_test_all --gtest_filter='TransformerTest.*' — all 31 tests passed
  • lintrunner onnxruntime/core/optimizer/insert_cast_transformer.cc onnxruntime/test/framework/insert_cast_transformer_test.cc — passed
  • git diff --check — passed
  • Full test suite and Linux x86_64 source build not run locally

Copilot AI balanced review requested due to automatic review settings November 9, 2026 15:16

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

This branch has not been deployed

No deployments
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.

CPU: FP16 MatMul followed by Cast to FP32 bypasses FP16 rounding

2 participants