Repository navigation
Add dpnp.top_k - #3133
Add dpnp.top_k#3133
dpnp.top_k#3133Conversation
|
Array API standard conformance tests for dpnp=0.22.0dev0=np2py314h8d9cdd5_22 ran successfully. |
|
View rendered docs @ https://intelpython.github.io/dpnp/index.html |
e55889f to
9cf4054
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The sorted=False test compares identical gathers rather than checking returned values against the indexed input, allowing incorrect indices to pass.
1 open issue
What changed in this PR
Adds dpnp.top_k as a NumPy-aligned public wrapper around dpnp.tensor.top_k, returning selected values and their indices.
Changes:
- Supports largest/smallest selection along an axis, with optional sorted output.
- Adds tests for dtypes, axes, NaNs, input types, and errors.
- Exports the function and adds API documentation and a changelog entry.
| File | Description |
|---|---|
dpnp/tests/test_sort.py |
Adds selection, compatibility, and validation tests. |
dpnp/dpnp_iface_sorting.py |
Implements and documents top_k, including output sorting. |
dpnp/__init__.py |
Exports top_k in the public namespace. |
doc/reference/sort.rst |
Lists top_k in the sorting reference. |
CHANGELOG.md |
Records the new function and sorting option. |
Review effort: Balanced
Return a `TopKResult` namedtuple from `dpnp.top_k`, document that an empty axis with `k > 0` raises, and say "non-negative" in the `dpnp.tensor.top_k` error for a negative `k`. Make the tests reach the re-sort path with NaN, along every axis of a 3-D input and in the queue and USM type tests, add empty-input tests and keep the tests that don't need NumPy 2.5 running on older NumPy.
`dpnp.tensor.top_k` defaults to `axis=None` and searches the flattened array, as in the first revision of data-apis/array-api#722. The default was changed to `axis=-1` before that PR was merged, and the 2026.12 draft of the standard has `axis=-1`. Omitting `axis` for an input that is not 1-D now emits a `DeprecationWarning` and keeps flattening. An explicit `axis=None` and 1-D input, for which both defaults give the same result, don't warn.
|
One more follow-up is about an ASV benchmark for
Follow-up proposal: a new
|
Add `dpnp.top_k` 58c2370

This PR proposes the addition of
dpnp.top_k, which will be available in NumPy 2.6dpnp.top_kreusestensor.top_k, but adds NumPy-aligned functionality, including asortedkeywordAlso deprecates behavior in
dpnp.tensor.top_kwhich was part of the initial array API plan that changed during its addition to the spec