Skip to content

[CPU] Support float64 in to_fp8 and from_fp8 - #4858

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
siddhantchaudhary036:cpu-fp8-float64
Nov 22, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
siddhantchaudhary036:cpu-fp8-float64

Conversation

@siddhantchaudhary036

Copy link
Copy Markdown
Contributor

ConvertFP8::eval_cpu had no float64 case, so float64 fell through to the float32 default: to_fp8 read the input buffer as float32 and from_fp8 wrote float32 values into a float64 output.

Narrowing double to float32 before encoding can also land a value exactly on an fp8 tie and round it the wrong way (1.0625 + 2^-30 encoded to 1.0 instead of 1.125), so narrow with round-to-odd first.

Fixes #4752

  • Added "float64" cases to both the "to_fp8" and "from_fp8" branches in "ConvertFP8::eval_cpu"

  • Added "narrow_round_to_odd" helper in "unary_ops.h" to prevent double-rounding errors when narrowing "double -> float32" before encoding

  • I then replaced the "default:" fallbacks using "float32"/"float64" cases that throw for unknown types, this is because we dont want a future dtype to silently corrupt data again

  • Also finished adding "test_to_from_fp8_float64" covering round trip correctness, the tie rounding edge case, saturation and NaN

  • ☑️ I understand it is strictly prohibited to use AI to write PR description

  • AI usage disclosure: I used an AI assistant to help investigate the root cause, worked with it to build the round-to-odd approach and I completely understand the produced code :)

@siddhantchaudhary036

Copy link
Copy Markdown
Contributor Author

Hi @zcbenz, could you approve the CI workflow and take a look when you get a chance? This fixes #4752 (float64 in to_fp8/from_fp8 on CPU) and is related to the fp8 NaN fix in #4756. I ran pre-commit and python/tests/run.py locally and everything passes. Thanks!

ConvertFP8::eval_cpu had no float64 case, so float64 fell through to the float32 default: to_fp8 read the input buffer as float32 and from_fp8 wrote float32 values into a float64 output.

Narrowing double to float32 before encoding can also land a value exactly on an fp8 tie and round it the wrong way (1.0625 + 2^-30 encoded to 1.0 instead of 1.125), so narrow with round-to-odd first.

Fixes ml-explore#4752
@zcbenz
zcbenz merged commit 3013bd8 into ml-explore:main Nov 22, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
Ported: the fork's Highway backend (quantized_highway_backend.cpp, used
when MLX_USE_HIGHWAY_KERNELS is set) has its own
fast::ConvertFP8::eval_cpu. It gets the same float32 and float64 cases
and the same error for other types. Before, it used float for every
other type, so from_fp8 to float64 wrote floats into a buffer of
doubles.

(cherry picked from commit 3013bd8)
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] mx.{to,from}_fp8 w/ float64 returns incorrect results on CPU

2 participants