Skip to content

Dedupe all-axes boilerplate in ops.cpp with all_axes() helper - #4290

Open
Adityaj0 wants to merge 1 commit into
ml-explore:mainfrom
Adityaj0:dedupe-all-axes-reduce-helper
Open

Dedupe all-axes boilerplate in ops.cpp with all_axes() helper#4290
Adityaj0 wants to merge 1 commit into
ml-explore:mainfrom
Adityaj0:dedupe-all-axes-reduce-helper

Conversation

@Adityaj0

Copy link
Copy Markdown
Contributor

Fourteen no-axes overloads (flip, all, any, sum, count_nonzero, mean, median, var, std, prod, max, min, logsumexp, softmax) each repeated the same std::iota-based boilerplate to build the "all axes" list before delegating to their axes-taking overload.

Extracted a shared all_axes(int ndim) helper into the existing anonymous namespace alongside compute_reduce_shape, and collapsed each call site to a one-liner. Net diff: 21 insertions / 42 deletions, single file (mlx/ops.cpp). Left pad's default-axes logic and the argmin/argmax keepdims expansion untouched since they build a different range.

Behavior-preserving — verified with the full C++ test suite (ops_tests.cpp, notably test flip and test reduction ops): 263 test cases, 3578 assertions, all passing. pre-commit run (clang-format) also passed clean.

Closes #4289 (originally filed against this repo by mistake, refiled at Adityaj0#1)

Fourteen reduce/elementwise ops (flip, all, any, sum, count_nonzero,
mean, median, var, std, prod, max, min, logsumexp, softmax) each
repeated the same std::iota block to build a 0..ndim-1 axis list for
their no-axes overload. Factor this into a single all_axes(ndim)
helper alongside the existing compute_reduce_shape helper.
@zcbenz

zcbenz commented Aug 16, 2026

Copy link
Copy Markdown
Member

Can you rebase on latest main?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicated all-axes boilerplate across reduction/elementwise ops in ops.cpp

2 participants