Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Array API - np.sum returns a scalar instead of an array/missing .device attribute #26850

@betatim

Description

@betatim

With Numpy 2 I observed the following behaviour. I think it makes sense that an array now has a .device to fit in with the array API. It makes sense that a Numpy scalar doesn't have that attribute as the array API doesn't have scalars. It makes sense that np.sum returns a scalar. However, in the array API it says that sum should return an array.

I found this because we have a test in scikit-learn that checks that the .device attribute of the input and output of sum match.

In [1]: import numpy as np

In [2]: a = np.asarray([1.,2,3,4])

In [3]: a.device
Out[3]: 'cpu'

In [4]: np.sum(a)
Out[4]: np.float64(10.0)

In [5]: np.sum(a).device
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[5], line 1
----> 1 np.sum(a).device

AttributeError: 'numpy.float64' object has no attribute 'device'

Not sure how to resolve this. The individual "facts" make sense but the combination somehow doesn't.

Is the most pragmatic thing to add the .device attribute to Numpy scalars?

Metadata

Metadata

Assignees

Labels

40 - array API standardPRs and issues related to support for the array API standard

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions