Fix python float scalars rounding through float32 - #4181
Draft
AKnassa wants to merge 1 commit into
Draft
Conversation
A python float used as an operand of a float64 array, or as the fill value of full/full_like, was cast to a C float before the array was constructed. The result had dtype float64 but carried only float32 precision, so mx.array([1.0], mx.float64) * 0.1 gave 0.10000000149011612 rather than 0.1. to_array now casts to double when the target dtype is float64, which mirrors the conversion already used in convert.cpp. Every other dtype keeps rounding through float exactly as before. Fixes ml-explore#4159 Fixes ml-explore#4160
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.
Proposed changes
Fixes #4159 and Fixes #4160 — both come from the same line of code.
When a plain Python float is used together with a
float64array, the value isturned into a C
floatbefore the array is built. The dtype comes out correct,but the number has already lost precision:
Both should be exact.
This looks like a missed call site rather than a deliberate choice. #2861 fixed
this same pattern in
python/src/convert.cpp, butto_arrayinpython/src/utils.cppwas never updated. This change mirrors that fix: cast todoublewhen the target dtype isfloat64, and keep casting tofloatforevery other dtype.
Since
to_arrayis the path every Python-scalar operand takes, this also fixesthe same rounding in
mx.maximum,mx.minimum,mx.where,mx.clip,full_like, and the in-place operators on float64 arrays. Nothing changes forfloat16, bfloat16 or float32.
Testing
Extended
test_double_keeps_precision(added by #2861) to cover the operand andthe
full/full_likepaths. It fails before this change and passes after.Full Python suite on a CPU-only build (
MLX_BUILD_METAL=OFF): 819 tests, no newfailures.
float64is CPU-only in MLX, so that is where this behaviour lives,but note the GPU suite was not run on my machine.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes