Skip to content

Support boolean input in dpnp.positive - #3128

Merged
antonwolfy merged 2 commits into
masterfrom
support-boolean-positive
Oct 28, 2026
Merged

antonwolfy merged 2 commits into
masterfrom
support-boolean-positive

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

NumPy 2.6 defines numpy.positive and the unary + operator for boolean arrays: they return a boolean copy of the input instead of raising TypeError. This PR aligns dpnp.positive and + on dpnp.ndarray with that behavior.

>>> import dpnp as np
>>> x = np.array([True, False])
>>> +x
array([ True, False])

dpnp.tensor.positive has no boolean loop, so for boolean input dpnp.positive runs the dpnp.tensor abs kernel, which returns boolean values unchanged. NumPy reuses its absolute loop for this case the same way. Other dtypes still use the positive kernel. dpnp.tensor.positive and + on usm_ndarray are unchanged.

The acceptance function that raised TypeError for boolean input is removed, since boolean input now has its own loop and never goes through type promotion.

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

NumPy 2.6 defines `positive` and the unary `+` operator for boolean
arrays, returning a boolean copy instead of raising `TypeError`.
Align `dpnp.positive` by running the `abs` kernel for boolean input,
which is the identity on bool, as NumPy reuses its `absolute` loop.

`dpnp.tensor.positive` is unchanged.
@antonwolfy antonwolfy added this to the 0.22.0 release milestone Oct 25, 2026
@antonwolfy antonwolfy self-assigned this Oct 25, 2026
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

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

@coveralls

coveralls commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 82.649% (+0.004%) from 82.645% — support-boolean-positive into master


# dpnp.tensor.positive has no boolean loop, abs is identity on bool
if src.dtype.char == "?":
return ti._abs(src, dst, sycl_queue, depends)

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.

We recently had the issues with casting to and from bool with non-zero bytes and alignment with NumPy (#3055), will it be an issue here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, it shouldn't be an issue.

The boolean input is dispatched to the abs kernel, and since #3055 every path AbsFunctor<bool> can take (contiguous sub-group, scalar fallback and strided) reads the input through normalize_bool, so any non-zero byte is treated as True and the result always holds a canonical 0x00/0x01 byte.

I also checked it with bytes [0, 1, 2, 255, 0x80, 0x10, 0, 3] viewed as bool, for contiguous, strided, reversed and in-place (out=) inputs, and the result bytes matched NumPy's in every case.

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

If it's verified there is no problem with the previous comment, LGTM

@github-actions

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev16=np2py314h8d9cdd5_14 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@antonwolfy
antonwolfy merged commit b6652df into master Oct 28, 2026
110 of 113 checks passed
@antonwolfy
antonwolfy deleted the support-boolean-positive branch October 28, 2026 16:55
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