array API: add positive, logical_xor, trunc, count_nonzero, diff, full_like#3730
Open
katlun-lgtm wants to merge 2 commits into
Open
array API: add positive, logical_xor, trunc, count_nonzero, diff, full_like#3730katlun-lgtm wants to merge 2 commits into
katlun-lgtm wants to merge 2 commits into
Conversation
4 tasks
zcbenz
requested changes
Jun 21, 2026
zcbenz
left a comment
Collaborator
There was a problem hiding this comment.
ops that are not pure aliases should have C++ versions and then exposed in python
…f, full_like New elementwise and utility ops required by the Python array API standard (https://data-apis.org/array-api/latest/): - positive: unary plus, returns a copy (mx::astype to same dtype) - logical_xor: element-wise XOR via not_equal(bool(a), bool(b)) - trunc: truncate toward zero (where(a < 0, ceil, floor)) - count_nonzero: count non-zero elements; returns int32; supports axis/keepdims - diff: n-th discrete difference along an axis, with optional prepend/append - full_like: fill an array shaped like the input; optional dtype override Docs and tests included. Part of the array API split from ml-explore#3684.
Per zcbenz review: ops that are not pure aliases must have C++ implementations exposed through the usual mlx/ops.h + mlx/ops.cpp path, not inline Python-binding lambdas. - positive: array copy (like __copy__) - logical_xor: not_equal(astype(a,bool_), astype(b,bool_)) - trunc: where(less(a,0), ceil, floor); integers returned as-is - count_nonzero: sum(not_equal(a,0).astype(int32)) with axis overloads - diff: n-th slice subtraction with optional prepend/append - full_like: delegates to existing mx::full_like C++ overload Python bindings in python/src/ops.cpp now call the C++ functions instead of duplicating the logic inline.
a091726 to
9ceb055
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.
Summary
Focused split from #3684. Adds six new ops required by the Python array API standard that have substantive implementations (not pure aliases).
positivemx::astype(a, a.dtype())— copy with same dtype (shallow copy semantics per @zcbenz's comment on #3684)logical_xornot_equal(bool(a), bool(b))truncwhere(a < 0, ceil(a), floor(a))— truncate toward zerocount_nonzerosum(not_equal(a, 0).astype(int32))with axis/keepdims supportdiffslicesubtraction; supportsprepend/appendfull_likemx::full(a.shape(), vals)with optional dtype overrideFiles changed
python/src/ops.cpp— C++ lambda registrations ininit_ops()docs/src/python/ops.rst— alphabetical entriespython/tests/test_ops.py—test_array_api_elementwise,test_diff,test_array_api_creation(full_like part)Related
Split from #3684 per reviewer feedback from @zcbenz.