Skip to content

Add flip and unstack#3683

Open
katlun-lgtm wants to merge 1 commit into
ml-explore:mainfrom
katlun-lgtm:flip-unstack
Open

Add flip and unstack#3683
katlun-lgtm wants to merge 1 commit into
ml-explore:mainfrom
katlun-lgtm:flip-unstack

Conversation

@katlun-lgtm

Copy link
Copy Markdown
Contributor

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 when axis is None. Implemented with a negative-stride slice (the same mechanism that backs a[::-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 of stack). Built on split + squeeze. Returns a list, consistent with split.

Added to the ops docs and tested in test_ops.py. Part of #3484.

Checklist

  • I have read the CONTRIBUTING document
  • clang-format and black (the formatters configured in .pre-commit-config.yaml) report no changes on the modified files
  • Added tests (test_ops.py::TestOps::test_flip, test_unstack)
  • Built from source and ran the tests locally: test_flip, test_unstack, and the existing test_split pass

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add C++ versions in mlx/ops.cpp and then export them in mlx/python/src/ops.cpp?

@katlun-lgtm

Copy link
Copy Markdown
Contributor Author

Done — moved both to mlx/ops.{h,cpp} (flip/unstack as core C++ ops) and made the Python bindings thin wrappers. Added C++ tests in tests/ops_tests.cpp (alongside the existing Python tests). Thanks for the review!

Comment thread python/tests/test_ops.py Outdated
Comment thread mlx/ops.cpp Outdated
@katlun-lgtm

Copy link
Copy Markdown
Contributor Author

Addressed both:

  • Reordered ops.cpp so flip/unstack appear in the same order as in ops.h (flip now right after expand_dims, unstack after split).
  • Reworked the Python tests to assert against NumPy (np.flip and per-axis np.take).

Comment thread python/tests/test_ops.py Outdated
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.
@katlun-lgtm

Copy link
Copy Markdown
Contributor Author

Good call — switched the test to compare against numpy.unstack directly (consistent with the np.flip check).

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks!

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.

2 participants