Fix CPU gather transposing column-contiguous slices#3647
Open
tillahoffmann wants to merge 2 commits into
Open
Conversation
zcbenz
approved these changes
Jun 10, 2026
zcbenz
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the fix, I think it works correctly, but without background knowledge I would need more reviews on this.
The gather "fast copy" path used the row/col contiguous flags to decide whether a per-index slice could be copied as a single contiguous block. For a column-contiguous source the slice is contiguous in memory but in column-major order, while the output is written in row-major order, so a multi-dimensional slice came out transposed. This surfaced on the CPU backend via chained `take` through size-1 axes (which yield a col-contiguous intermediate) and via a direct `take` from a transposed source; the GPU backend was correct. Replace the flag-based heuristic with a direct check that the slice is row-major contiguous within the source (each non-singleton slice dim's source stride equals the product of the inner slice sizes), which is the exact precondition for the contiguous copy. Falls back to the strided iterator otherwise. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
e5b97b3 to
8e20a28
Compare
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
mx.take(gather) on the CPU backend returns a transposed / wrong-stride result when the source is column-contiguous and the gathered slice spans more than one non-singleton dimension. The GPU backend is correct.The
gather"fast copy" path inmlx/backend/cpu/indexing.cppdecided whether each gathered slice could be copied as a single contiguous block based on the source'srow_contiguous/col_contiguousflags. The gather output is always written in row-major order, but a column-contiguous source is contiguous in memory in column-major order — so copying a multi-dimensional slice as a raw block transposes it.This surfaces via:
takethrough size-1 axes, which produces a column-contiguous intermediate, andtakefrom a transposed (column-contiguous) source.Repro
Before this fix (disagrees with NumPy / the GPU backend):
After this fix both match NumPy.
Originally found while differential-testing a CPU reference against the GPU/MPS path; a downstream consumer reaches this through a
dynamic_update_slicelowering that expands to a chained-take+ mask +where.Fix
Replace the flag-based heuristic with a direct check that the slice is genuinely row-major contiguous within the source: each non-singleton slice dimension's source stride must equal the product of the slice sizes of the dimensions inside it (size-1 dimensions are skipped, since their stride is irrelevant). This is the exact precondition for the contiguous
std::copy; everything else falls back to the strided iterator. The check is also strictly more correct than the previous row-contiguous branch.Testing
test gather contiguityC++ regression case (tests/ops_tests.cpp) covering both scenarios, forced onto the CPU device.🤖 Generated with Claude Code