Add flip and unstack#3683
Open
katlun-lgtm wants to merge 1 commit into
Open
Conversation
zcbenz
reviewed
Jun 14, 2026
zcbenz
left a comment
Collaborator
There was a problem hiding this comment.
Can you add C++ versions in mlx/ops.cpp and then export them in mlx/python/src/ops.cpp?
bd3d0e6 to
e13fa16
Compare
Contributor
Author
|
Done — moved both to |
zcbenz
reviewed
Jun 15, 2026
e13fa16 to
814493a
Compare
Contributor
Author
|
Addressed both:
|
zcbenz
reviewed
Jun 15, 2026
Adds `flip` and `unstack` as core C++ ops in mlx/ops.{h,cpp} (so they
are available from C++ as well), and exports them through the Python
bindings:
- `flip(a, axis=None)` reverses element order along the given axis (or
all axes), via a negative-stride `slice`.
- `unstack(x, axis=0)` splits an array into a sequence of arrays along
an axis with that axis removed (the inverse of `stack`), via
`split` + `squeeze`.
Adds C++ tests (tests/ops_tests.cpp) and Python tests (test_ops.py),
plus ops documentation.
814493a to
4b0158f
Compare
Contributor
Author
|
Good call — switched the test to compare against |
This was referenced Jun 15, 2026
zcbenz
approved these changes
Jun 15, 2026
zcbenz
left a comment
Collaborator
There was a problem hiding this comment.
Looks good to me, thanks!
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.
Proposed changes
Adds two array API manipulation functions to
mlx.core:flip(a, /, axis=None)— reverse the order of elements along the given axis, or all axes whenaxisisNone. Implemented with a negative-strideslice(the same mechanism that backsa[::-1]), so no new primitive is needed.unstack(x, /, *, axis=0)— split an array into a sequence of arrays along an axis, with that axis removed (the inverse ofstack). Built onsplit+squeeze. Returns a list, consistent withsplit.Added to the ops docs and tested in
test_ops.py. Part of #3484.Checklist
clang-formatandblack(the formatters configured in.pre-commit-config.yaml) report no changes on the modified filestest_ops.py::TestOps::test_flip,test_unstack)test_flip,test_unstack, and the existingtest_splitpass