Repository navigation
Support boolean input in dpnp.positive - #3128
Conversation
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.
|
View rendered docs @ https://intelpython.github.io/dpnp/index.html |
|
|
||
| # dpnp.tensor.positive has no boolean loop, abs is identity on bool | ||
| if src.dtype.char == "?": | ||
| return ti._abs(src, dst, sycl_queue, depends) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
If it's verified there is no problem with the previous comment, LGTM
|
Array API standard conformance tests for dpnp=0.21.0dev16=np2py314h8d9cdd5_14 ran successfully. |
NumPy 2.6 defines
numpy.positiveand the unary+operator for boolean arrays: they return a boolean copy of the input instead of raisingTypeError. This PR alignsdpnp.positiveand+ondpnp.ndarraywith that behavior.dpnp.tensor.positivehas no boolean loop, so for boolean inputdpnp.positiveruns thedpnp.tensorabskernel, which returns boolean values unchanged. NumPy reuses itsabsoluteloop for this case the same way. Other dtypes still use thepositivekernel.dpnp.tensor.positiveand+onusm_ndarrayare unchanged.The acceptance function that raised
TypeErrorfor boolean input is removed, since boolean input now has its own loop and never goes through type promotion.