fix(metal): preserve subnormal float values when casting to bool (#4205) - #4224
Open
reckylurker wants to merge 2 commits into
Open
fix(metal): preserve subnormal float values when casting to bool (#4205)#4224reckylurker wants to merge 2 commits into
reckylurker wants to merge 2 commits into
Conversation
zcbenz
reviewed
Aug 15, 2026
| /////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| template <typename U, typename T> | ||
| inline U mlx_cast(T val) { |
Member
There was a problem hiding this comment.
In cuda backend there is a similar utility named cast_to, I think we can use the same name here.
|
|
||
| template <> | ||
| inline bool mlx_cast<bool, half>(half val) { | ||
| return (as_type<uint16_t>(val) & 0x7FFF) != 0; |
Member
There was a problem hiding this comment.
float16 does not seem to need this, should add a test.
Contributor
Author
There was a problem hiding this comment.
yes, looks like half values are not affected in MSL. Thanks for the suggestion.
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.
Fixes #4205
Proposed changes
In Metal Shading Language (MSL), casting a floating-point number directly to
boolviastatic_cast<bool>(x)evaluates in float registers, which flushes subnormal numbers to zero in hardware. Sostatic_cast<bool>(subnormal)ends up evaluating as0.0f != 0.0f->false.This was affecting
.astype(mx.bool_)(incopy.h) as well asany(),all(), and other reductions wheneverU = bool.To fix this, introduced a
mlx_cast<U, T>helper inutils.hthat checks the raw bit pattern for floats (ignoring the sign bit, e.g.(as_type<uint32_t>(val) & 0x7FFFFFFF) != 0). This checks the exponent and fraction bits as raw integers so subnormals don't get flushed by Metal hardware units, while still keeping-0.0asFalse.updated
copy.h,reduce_all.h,reduce_col.h, andreduce_row.hto usemlx_cast<U>.Added
test_subnormal_bool_castinpython/tests/test_ops.pyto checkastype(bool),any(), andall()onfloat32andbfloat16subnormals. All Python and C++ tests pass!Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes