Fix dpnp.linspace returning NaN for equal infinite endpoints - #3043
Fix dpnp.linspace returning NaN for equal infinite endpoints#3043antonwolfy wants to merge 2 commits into
dpnp.linspace returning NaN for equal infinite endpoints#3043Conversation
Backport numpy#31620: equal endpoints must produce a zero step instead of evaluating inf - inf (or inf * 0), which yields NaN. - dpnp_linspace: fill directly for equal scalar endpoints and zero the delta where array endpoints coincide - LinearSequenceAffineFunctor: short-circuit to a constant sequence when start == stop, fixing dpnp.tensor.linspace at its root
|
Array API standard conformance tests for dpnp=0.21.0dev7=py314ha0e2e8e_15 ran successfully. |
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3043/index.html |
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
LGTM
Thank you @antonwolfy
One separate issue for a follow-up
dpnp.linspace(-dpnp.inf, dpnp.inf, num) returns NaN for the last value instead of stop
In [4]: dpnp.linspace(-dpnp.inf, dpnp.inf, 4)
Out[4]: array([nan, nan, nan, nan])
In [6]: numpy.linspace(-numpy.inf, numpy.inf, 4)
/home/sdp/miniforge3/envs/dpnp_conda/lib/python3.14/site-packages/numpy/_core/function_base.py:163: RuntimeWarning: invalid value encountered in multiply
y *= step
/home/sdp/miniforge3/envs/dpnp_conda/lib/python3.14/site-packages/numpy/_core/function_base.py:173: RuntimeWarning: invalid value encountered in add
y += start
Out[6]: array([nan, nan, nan, inf])
|
Just to note, NumPy behavior also doesn't seem correct, since the result should be: Since I believe the only way to do that in dpnp/dpnp.tensor now is to have a special handling for the first and last elements of the interval. |
|
It seems the same is applicable to similar use cases: |
dpnp.linspacereturnedNaNinstead of the endpoint value whenstartandstopare equal and infinite — e.g.dpnp.linspace(inf, inf, 4)gave[nan inf inf nan].The bug had two independent sources:
dpnp_linspacecomputeddelta = stop - start, soinf - inf = NaNpropagated through the whole result.dpnp.tensor.linspace, whose affine kernelLinearSequenceAffineFunctorevaluatesstart * w + stop * wc; at the endpoints one weight is0, andinf * 0 = NaN.The PR proposes to fix all the places.
Note, the tests compare against NumPy on versions that include the fix (NumPy >= 2.6.0) and fall back to explicit expected values on older NumPy.