Skip to content

Fix ValueError in dpnp.bincount for empty input arrays - #3018

Open
antonwolfy wants to merge 2 commits into
masterfrom
fix/bincount-empty-input
Open

Fix ValueError in dpnp.bincount for empty input arrays#3018
antonwolfy wants to merge 2 commits into
masterfrom
fix/bincount-empty-input

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

dpnp.bincount raised a ValueError when given an empty input array, while NumPy returns an empty (or zero-filled) array. This PR short-circuits the empty-input case so dpnp.bincount matches NumPy.

The Python layer determines the binning edges by reducing over the input with dpnp.max/dpnp.min. Those reductions have no identity for zero-size input, so an empty array failed before ever reaching the kernel:

>>> import dpnp
>>> dpnp.bincount(dpnp.array([], dtype="i8"))
ValueError: reduction cannot be performed over zero-size axes

The C++ kernel already guards sample.get_size() == 0, but that code path is unreachable because the max/min reduction runs first.

The fix assumes to return early for empty input with an intp array of zeros of length minlength, before any reduction is performed.

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

@antonwolfy antonwolfy added this to the 0.21.0 release milestone Aug 11, 2026
@antonwolfy antonwolfy self-assigned this Aug 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

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

@antonwolfy
antonwolfy force-pushed the fix/bincount-empty-input branch from 73993ba to 8e90fd3 Compare August 11, 2026 17:35
dpnp.bincount raised a ValueError on an empty input array because the
Python layer reduces over the array with dpnp.max/dpnp.min to determine
binning edges, and those reductions have no identity for zero-size input.

Short-circuit empty input and return an intp array of zeros of length
minlength, matching NumPy behavior (which returns intp even when weights
are provided).
@antonwolfy
antonwolfy force-pushed the fix/bincount-empty-input branch from 8e90fd3 to e5966bc Compare August 11, 2026 17:37
for_dtypes already skips dtypes the default device cannot represent
natively (float64/complex128 without fp64 support, float16 without fp16
support), but for_dtypes_combination did not, so combination tests could
attempt to allocate an unsupported-dtype array and fail with a device
ValueError instead of exercising the intended code path. This surfaced in
the new bincount empty-with-weights test.

Factor the per-dtype check into a shared helper and apply it in both
decorators so combination tests skip such dtypes consistently.
@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.402% (-0.01%) from 78.414% — fix/bincount-empty-input into master

@antonwolfy
antonwolfy marked this pull request as ready for review August 11, 2026 21:48
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