Repository navigation
[CPU] Fix FP4 tie rounding on CPU - #4805
Open
projekt11th wants to merge 2 commits into
Open
projekt11th wants to merge 2 commits into
projekt11th wants to merge 2 commits into
Conversation
The CPU fallback in fp_quantize used argmin, which selects the smaller value on exact ties. Update it to use the same rounding thresholds as Metal's fp4_e2m1, ensuring consistent FP4 quantization across backends. Also changes CPU NaN quantization from code 0 to code 7. Tests: - python3 python/tests/run.py - pre-commit run --all Fixes ml-explore#4753
zcbenz
requested changes
Nov 3, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
This code feels very inefficient, can you try the algorithm used in https://github.com/ROCm/AMDMIGraphX/blob/4bba8e7bf4ab80ed198a06949f00175bd13d2a34/src/include/migraphx/fp4_casts.hpp#L83?
It should be possible to use searchsorted to replace the for loop, and there is a sign op.
Replace the iterative FP4 rounding loop with searchsorted-based tie detection and ties-to-even rounding. Use sign(wq) to determine the sign instead of the view-as-int approach. CPU produces code 0 for exact -0.0, while Metal produces code 8. Both decode to zero.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The CPU fallback in fp_quantize used argmin, which selects the smaller value on exact ties. Update it to use the same rounding thresholds as Metal's fp4_e2m1, ensuring consistent FP4 quantization across backends.
Also changes CPU NaN quantization from code 0 to code 7.
Tests:
Fixes #4753