Use cast_to for fused AsType in compiled Metal kernels - #4351
Open
katlun-lgtm wants to merge 1 commit into
Open
Conversation
…als to bool false Follow-up to ml-explore#4224: the fused path in compiled.cpp still emitted a raw static_cast for AsType, so .astype(mx.bool_) inside a compiled graph of two or more fusable ops flushed float32/bfloat16 subnormals to False. cast_to is already in the generated preamble via metal::utils().
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.
Follow-up to #4224 (#4205). The compiled/fused path still flushes float32 and bfloat16 subnormals to
Falseon.astype(mx.bool_):build_kernelinmlx/backend/metal/compiled.cppemits a rawstatic_castforis_static_castprimitives, so a fusedAsTypenever reachescast_to. This swaps it forcast_to, which is already in every generated kernel's preamble viametal::utils().Repro — needs at least two fusable ops; a single-op
mx.compile(lambda x: x.astype(mx.bool_))doesn't fuse and falls back to the already-correct copy kernel:broadcast_tois deliberate: the fused broadcast preserves the input bits (0x00000001in and out), so what fails is the cast, not FTZ arithmetic. Same failure on bfloat16. float16 is unaffected — Metal'sstatic_cast<bool>on half is already correct (probed directly withmx.fast.metal_kernel).Out of scope, and not claimed fixed:
!=,>,==on subnormals still diverge from the CPU backend after this. Eagerx + 0.0zeroes the bits too, so that's ordinary Metal FTZ arithmetic, a different class from the cast. I also checked the otherstatic_castin this file (constant-input loads around line 121): it casts the printed constant to its own dtype, so there is nothing to fix there.Tested on an M3 Max: the new test in
test_compile.pyfails on current main and passes with the change; fullpython/testssuite is green (801 passed, 51 skipped, 11293 subtests).Investigation and test runs were AI-assisted; I've reviewed and understand the change.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes