Skip to content

array API: add positive, logical_xor, trunc, count_nonzero, diff, full_like#3730

Open
katlun-lgtm wants to merge 2 commits into
ml-explore:mainfrom
katlun-lgtm:array-api-new-ops
Open

array API: add positive, logical_xor, trunc, count_nonzero, diff, full_like#3730
katlun-lgtm wants to merge 2 commits into
ml-explore:mainfrom
katlun-lgtm:array-api-new-ops

Conversation

@katlun-lgtm

Copy link
Copy Markdown
Contributor

Summary

Focused split from #3684. Adds six new ops required by the Python array API standard that have substantive implementations (not pure aliases).

Op Implementation
positive mx::astype(a, a.dtype()) — copy with same dtype (shallow copy semantics per @zcbenz's comment on #3684)
logical_xor not_equal(bool(a), bool(b))
trunc where(a < 0, ceil(a), floor(a)) — truncate toward zero
count_nonzero sum(not_equal(a, 0).astype(int32)) with axis/keepdims support
diff n-th discrete difference via slice subtraction; supports prepend/append
full_like mx::full(a.shape(), vals) with optional dtype override

Files changed

  • python/src/ops.cpp — C++ lambda registrations in init_ops()
  • docs/src/python/ops.rst — alphabetical entries
  • python/tests/test_ops.pytest_array_api_elementwise, test_diff, test_array_api_creation (full_like part)

Related

Split from #3684 per reviewer feedback from @zcbenz.

@katlun-lgtm katlun-lgtm mentioned this pull request Jun 20, 2026
4 tasks

@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.

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.
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