Replace hand-rolled axis normalization with normalize_axis_index in split/unstack/partition/topk - #4288
Merged
Conversation
zcbenz
approved these changes
Aug 16, 2026
…plit/unstack/partition/topk split, unstack, partition, argpartition and topk each duplicated the same axis < 0 ? axis + ndim : axis pattern followed by a manual bounds check and custom error message, instead of using normalize_axis_index which already does both in one call and is used for this exact purpose elsewhere in ops.cpp (squeeze, expand_dims, flip, concatenate, stack, repeat). Behavior is unchanged: valid axes (including negative ones) still resolve to the same value, and out-of-range axes still raise std::invalid_argument. Only the exact wording of the error message changes; no test in the tree asserts on that text.
zcbenz
force-pushed
the
refactor-axis-normalization
branch
from
August 16, 2026 11:02
4078e53 to
f6bdf31
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.
Fixes #4287.
Proposed changes
split,unstack,partition,argpartitionandtopkeach duplicated the same axis normalization pattern:with slightly different bounds-check expressions at each site.
normalize_axis_indexalready does this — normalize and validate in one call — and is already used for exactly this purpose elsewhere inops.cpp(squeeze,expand_dims,flip,concatenate,stack,repeat). #4118 fixed the same duplication intake_along_axis/scatter_axis/linalg::cross; this covers the remaining call sites inops.cpp.This is a pure refactor, not a behavior change:
std::invalid_argument(ValueErrorin Python).[topk] Axis -100 is out of bounds for array with 3 dimensions.instead of[topk] Received invalid axis -100 for array with 3 dimensions.); no test in the tree asserts on that text, only that the correct exception type is raised.In
split(a, num_splits, axis, s)the normalized axis is now also reused for the subsequenta.shape(...)lookup and the recursivesplit(a, indices, ax, s)call instead of re-deriving it, avoiding the redundant re-normalization that happened implicitly througharray::shape(int).Net diff removes more than it adds (54 lines changed, 8 added / 46 removed).
Tests
No behavior change, so no new tests were added. Verified with a build of
libmlx.a(cmake --build cmake_build --target mlx) and the full C++ test suite (cmake --build cmake_build --target tests && ./cmake_build/tests/tests): 263 test cases / 3578 assertions, all passing, includingops_tests.cppcoverage ofsplit,unstack,partition,argpartitionandtopk.pre-commit run --all-filesto format my code and installed pre-commit prior to committing changes