Skip to content

Fix abort in top_k for k=0 - #3022

Open
antonwolfy wants to merge 2 commits into
masterfrom
fix-empty-topk
Open

Fix abort in top_k for k=0#3022
antonwolfy wants to merge 2 commits into
masterfrom
fix-empty-topk

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

dpnp.tensor.top_k(x, 0) might aborted the process on an assertions-enabled SYCL runtime:

import dpnp.tensor as dpt
dpt.top_k(dpt.arange(12).reshape(3, 4), 0)   # k = 0

k == 0 is a valid request whose result is empty, and it passes validation (k < 0 is rejected, k > size is rejected, but k == 0 is allowed). py_topk then dispatched to the kernel, whose output-writing step (write_out_impl) builds an nd_range from nelems = iter_nelems * k. A zero-sized nd_range is a silent no-op on runtimes built with NDEBUG, but aborts on an assertions-enabled SYCL runtime.

The existing guard covered iter_nelems == 0 and axis_nelems == 0, but not k == 0.

This PR proposes to extend the early-return guard in py_topk to also cover k == 0.

  • 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?

top_k(x, 0) is a valid request whose result is empty, but py_topk still
dispatched to the kernel, which builds an nd_range with a zero global
size (nelems = iter_nelems * k == 0) and a non-zero local size. That is a
silent no-op on runtimes built with NDEBUG, but aborts on an
assertions-enabled SYCL runtime (adjustNDRangePerKernel asserts
NDR.LocalSize[0] == 0 when GlobalSize is 0).

Extend the early-return guard to cover k == 0. The vals/inds outputs are
already allocated with a zero-length result axis, so there is nothing to
compute or fill.
@antonwolfy antonwolfy added this to the 0.21.0 release milestone Aug 12, 2026
@antonwolfy antonwolfy self-assigned this Aug 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.402% (-0.01%) from 78.412% — fix-empty-topk into master

@antonwolfy
antonwolfy marked this pull request as ready for review August 12, 2026 15:37
@github-actions

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev3=py314h509198e_41 ran successfully.
Passed: 1376
Failed: 1
Skipped: 5

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.

3 participants