Skip to content

Add dpnp.top_k - #3133

Merged
ndgrigorian merged 9 commits into
masterfrom
feature/add-dpnp-topk
Nov 3, 2026
Merged

ndgrigorian merged 9 commits into
masterfrom
feature/add-dpnp-topk

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

This PR proposes the addition of dpnp.top_k, which will be available in NumPy 2.6

dpnp.top_k reuses tensor.top_k, but adds NumPy-aligned functionality, including a sorted keyword

Also deprecates behavior in dpnp.tensor.top_k which was part of the initial array API plan that changed during its addition to the spec

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

ndgrigorian added a commit that referenced this pull request Oct 30, 2026
@ndgrigorian ndgrigorian changed the title Add dpnp.top_k Add dpnp.top_k Oct 30, 2026
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev0=np2py314h8d9cdd5_22 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/index.html

@ndgrigorian
ndgrigorian marked this pull request as ready for review October 30, 2026 08:39
@coveralls

coveralls commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.376% (+0.01%) from 84.364% — feature/add-dpnp-topk into master

@antonwolfy antonwolfy added this to the 0.22.0 release milestone Oct 30, 2026
Base automatically changed from feature/implement-radix-select-topk to master October 31, 2026 11:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved
Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved
Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the new dpnp.top_k API.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved
Comment thread dpnp/tests/test_sort.py Resolved
Comment thread dpnp/tests/test_sycl_queue.py Resolved
Comment thread dpnp/tests/test_usm_type.py Resolved
Comment thread dpnp/dpnp_iface_sorting.py Resolved
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.
ndgrigorian and others added 2 commits November 2, 2026 21:49
`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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the new top_k API and tensor-axis deprecation.

No open issues 🎉

Review effort: Balanced

@antonwolfy antonwolfy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thank you!

Comment thread dpnp/dpnp_iface_sorting.py Resolved
@antonwolfy

antonwolfy commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

One more follow-up is about an ASV benchmark for dpnp.top_k: I'd suggest not adding one in this PR and covering it in a follow-up instead.

  • The benchmark suite has no sorting module yet, so adding one here would mean designing it around a single function.
  • dpnp.top_k wraps dpnp.tensor.top_k, so a benchmark would mainly time the existing kernel.
  • For the "checked performance impact" item, a quick timing in the PR description is enough (e.g. top_k vs sort + slice for a few k values).

Follow-up proposal: a new benchmarks/benchmarks/bench_sorting.py covering sort, argsort, partition and top_k together, none of which is benchmarked today:

  • the shared _SIZES_1D and _FLOAT_DTYPES axes from _utils.py
  • a k axis with a few small values and one large one
  • sorted=True/False for top_k
  • sizes and parameter combinations kept moderate, so the Jenkins ASV job doesn't get noisier

@ndgrigorian
ndgrigorian merged commit 58c2370 into master Nov 3, 2026
88 of 91 checks passed
@ndgrigorian
ndgrigorian deleted the feature/add-dpnp-topk branch November 3, 2026 21:55
github-actions Bot added a commit that referenced this pull request Nov 3, 2026
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.

4 participants