Skip to content

[CPU] Fix fp4 tie rounding - #4845

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
eyupcanakman:fix/fp4-cpu-tie-rounding
Nov 24, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
eyupcanakman:fix/fp4-cpu-tie-rounding

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

Fixes #4753

In fp4 rounding, the CPU and GPU disagree when a number is exactly halfway between two stored values.
The CPU uses the smaller value, but the GPU uses a rule called "round to even", and that's the correct version.

Instead of using its own rule, the CPU now uses the GPU's rule, following zcbenz's suggested approach.
Testing found more problems, and I fixed them too:

  • one NaN could break a whole group of numbers
  • a single nvfp4 case divided by zero
  • an all-zero group stored the wrong scale byte

Added a new test to cover ties, a NaN and an all-zero group.
The fix made fp4 about 4x faster and mxfp8 about 1.5x slower because of the new NaN skipping when finding each group's maximum.

Median float32 CPU quantize times on an Apple M5 Pro, in milliseconds.

Mode Shape Before After
mxfp4 1024x1024 26 6
mxfp4 4096x4096 415 97.5
nvfp4 1024x1024 26 6.5
nvfp4 4096x4096 423 105
mxfp8 1024x1024 0.6 1.0
mxfp8 4096x4096 10.3 15.8
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Claude Code wrote the code, and Claude and GPT (Codex) reviewed it.

@zcbenz
zcbenz force-pushed the fix/fp4-cpu-tie-rounding branch from 6646b79 to 5257c17 Compare November 20, 2026 10:12
Comment thread mlx/ops.cpp Resolved
Comment thread python/tests/test_quantized.py Resolved
@zcbenz zcbenz added await response This pull request is waiting for response from the author. and removed await response This pull request is waiting for response from the author. labels Nov 23, 2026
@zcbenz
zcbenz force-pushed the fix/fp4-cpu-tie-rounding branch from e7a1c29 to 9169e68 Compare November 23, 2026 23:37
@zcbenz zcbenz changed the title Fix CPU fp4 tie rounding [CPU] Fix fp4 tie rounding Nov 24, 2026
@zcbenz
zcbenz merged commit b0df275 into ml-explore:main Nov 24, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
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.

[BUG] FP4 quantization on CPU rounds ties toward zero while Metal rounds to even

2 participants