numpy icon indicating copy to clipboard operation
numpy copied to clipboard

BUG: scalars don't have dlpack support

Open arogozhnikov opened this issue 1 year ago • 4 comments

Describe the issue:

reduction (e.g. xp.sum) are expected to produce arrays, but in numpy they produce scalars, that don't follow array api specifications (e.g. I can't use dlpack on those).

In numpy v1, np.array_api correctly returned arrays in this case

Reproduce the code example:

import numpy as np
x = np.zeros([3, 4])

type(x.__array_namespace__().sum(x))
# outputs: numpy.float64 expected: numpy.ndarray

Python and NumPy Versions:

2.0.1
3.11.9 (main, Apr  6 2024, 17:59:24) [GCC 11.4.0]

arogozhnikov avatar Aug 08 '24 00:08 arogozhnikov

Duplicate of https://github.com/numpy/numpy/issues/26850. Is there any particular you're problem with getting a scalar here? If it's a duck typing issue we should fix the issue in the scalars like in that issue.

ngoldbaum avatar Aug 08 '24 16:08 ngoldbaum

particular problem for me was missing dlpack functionality on scalars

arogozhnikov avatar Aug 08 '24 16:08 arogozhnikov

Maybe we could add a __dlpack__ implementation (in C) to the scalars that converts to a 0-D array and delegates to the ndarray __dlpack__?

ngoldbaum avatar Aug 08 '24 16:08 ngoldbaum

That's maintainers' decision.

From my user perspective I was quite happy that array api removes different corner cases in favor of uniformity (e.g. returning arrays), but I have no insight about potential performance issues.

arogozhnikov avatar Aug 08 '24 16:08 arogozhnikov