Fix shapeless compile eliding reductions on size-1 dimensions#3202
Open
ghstrider wants to merge 1 commit intoml-explore:mainfrom
Open
Fix shapeless compile eliding reductions on size-1 dimensions#3202ghstrider wants to merge 1 commit intoml-explore:mainfrom
ghstrider wants to merge 1 commit intoml-explore:mainfrom
Conversation
During shapeless (dynamic) tracing, compute_reduce_shape() reported is_noop=true when a reduced dimension happened to be size 1 at trace time. This caused sum/mean/max/min/prod/all/any/argmin/argmax to skip creating the Reduce primitive entirely—returning just astype() or the input directly. On replay with a larger dimension, the missing Reduce meant the graph went straight from GatherAxis (correctly producing [n] elements) to Squeeze (dropping the dimension to scalar), and only the first element was visible in the output. The fix disables the is_noop optimisation when in_dynamic_tracing() is true, following the same pattern already used for no-op broadcast elision (lines 1489, 1543 of ops.cpp). Fixes ml-explore#3201 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Fixes #3201 —
mx.compile(shapeless=True)returns stale values when a reduction (sum,mean,max, etc.) operates on a dynamically-shaped input fromtake/gather.Root Cause
compute_reduce_shape()reportsis_noop=truewhen a reduced dimension is size 1 at trace time. This causessum()et al. to skip creating theReduceprimitive entirely. On replay with a larger dimension, the missing reduction means the graph goes fromGatherAxis→Squeezewith no reduction in between, and only the first element survives.Fix
Disable the
is_noopoptimisation duringin_dynamic_tracing(), following the same pattern already used for no-op broadcast elision (lines 1489, 1543 ofops.cpp).One-line change in
compute_reduce_shape():This fixes all 8 affected reduction operations (
sum,prod,min,max,all,any,argmin,argmax) in one place.Test Plan
test_shapeless_compile_reduce_after_gathercoveringsum,mean, andmaxwith varying index sizes starting from size 1test_shapeless_compile_with_reductioncontinues to pass (it doesn't trigger the bug because its reduced dimensions are never size 1 at trace time)