Dedupe all-axes boilerplate in ops.cpp with all_axes() helper - #4290
Open
Adityaj0 wants to merge 1 commit into
Open
Dedupe all-axes boilerplate in ops.cpp with all_axes() helper#4290Adityaj0 wants to merge 1 commit into
Adityaj0 wants to merge 1 commit into
Conversation
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
approved these changes
Aug 16, 2026
Member
|
Can you rebase on latest main? |
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.
Fourteen no-axes overloads (
flip,all,any,sum,count_nonzero,mean,median,var,std,prod,max,min,logsumexp,softmax) each repeated the samestd::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 alongsidecompute_reduce_shape, and collapsed each call site to a one-liner. Net diff: 21 insertions / 42 deletions, single file (mlx/ops.cpp). Leftpad's default-axes logic and theargmin/argmaxkeepdims expansion untouched since they build a different range.Behavior-preserving — verified with the full C++ test suite (
ops_tests.cpp, notablytest flipandtest 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)