Skip to content

Fix floating-point constant precision in compiled kernels - #4511

Open
Ryan11c wants to merge 3 commits into
ml-explore:mainfrom
Ryan11c:fix-compiled-float-precision
Open

Ryan11c wants to merge 3 commits into
ml-explore:mainfrom
Ryan11c:fix-compiled-float-precision

Conversation

@Ryan11c

@Ryan11c Ryan11c commented Sep 15, 2026

Copy link
Copy Markdown

Fixes #4503

Captured floating-point constants in compiled kernels are currently printed using digits10 + 1 digits. For some values this isn't enough precision to round-trip back to the same float which can make the compiled result slightly different from eager execution.

Changed print_float_constant to use max_digits10 instead. This gives enough precision for the value to round-trip correctly when the generated kernel source is compiled.

Also added tests with float32 constants that were affected by this issue. This includes 1 / 3, 128**-0.5, and 0.7071067811865476. I also updated the float64 test to use math.nextafter(1.0, 2.0) so it catches the same issue for doubles. The tests use mx.array_equal to make sure the eager and compiled results match.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: I used GPT 5.6 Sol to help me understand the repo and how the issue is present in this repo. Fixed the bug myself and used agent to review/clean up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mx.compile inlines float scalar constants with 7 significant digits, so compiled results differ from eager by 1 ulp

2 participants