Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove __array__ #69

Merged
merged 6 commits into from
Nov 2, 2024
Merged

Remove __array__ #69

merged 6 commits into from
Nov 2, 2024

Conversation

asmeurer
Copy link
Member

This makes it raise an exception, since it isn't supported by the standard (if
we just leave it unimplemented, then np.asarray() returns an object dtype
array, which is not good).

There is one issue here from the test suite, which is that this breaks the
logic in asarray() for converting lists of array_api_strict 0-D arrays. I'm
not yet sure what to do about that.

Fixes #67.

This is built on #68, which can and should be merged separately (it isn't controversial).

This makes it raise an exception, since it isn't supported by the standard (if
we just leave it unimplemented, then np.asarray() returns an object dtype
array, which is not good).

There is one issue here from the test suite, which is that this breaks the
logic in asarray() for converting lists of array_api_strict 0-D arrays. I'm
not yet sure what to do about that.

Fixes data-apis#67.
@betatim
Copy link
Member

betatim commented Oct 21, 2024

In scikit-learn's tests we convert array API arrays to Numpy arrays in order to compare their values to some reference array. How would we do that if we remove __array__ here? Other libraries have .tonumpy() and the like, but I think array-api-strict doesn't? Should we add something like that as part of this removal?

@asmeurer
Copy link
Member Author

Testing is a valid use-case. You could use dlpack if you wanted to stay within the standard. Although I'm still not clear if this is a good idea or not.

@betatim
Copy link
Member

betatim commented Oct 22, 2024

Good point. We already have custom code to convert from the various array libraries to Numpy. Using

x = xp.asarray([1,2,3], device=...)
np.from_dlpack(x)

would maybe simplify that code and remove the need for __array__.

Copy link

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

Although I'm still not clear if this is a good idea or not.

I think this is a good idea for array-api-strict. Libraries that need to do any array conversion should only depend on __dlpack__.

(Although this PR is breaking tests in this repo)

@asmeurer
Copy link
Member Author

To clarify a note from the OP: this has stopped working

>>> import array_api_strict as xp
>>> xp.asarray([[xp.asarray(1), xp.asarray(2)]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/aaronmeurer/Documents/array-api-strict/array_api_strict/_creation_functions.py", line 97, in asarray
    res = np.array(obj, dtype=_np_dtype, copy=copy)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aaronmeurer/Documents/array-api-strict/array_api_strict/_array_object.py", line 138, in __array__
    raise ValueError("Conversion from an array_api_strict array to a NumPy ndarray is not supported")
ValueError: Conversion from an array_api_strict array to a NumPy ndarray is not supported

because it was implicitly relying on np.asarray() calling __array__ on nested array_api_strict arrays.

@asmeurer asmeurer merged commit b9e64e5 into data-apis:main Nov 2, 2024
21 checks passed
@asmeurer
Copy link
Member Author

asmeurer commented Nov 6, 2024

Thinking about dlpack, should we be doing anything with the device emulation in __dlpack__? https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.html#dlpack Maybe we should disable __dlpack__ when the device isn't the default CPU_DEVICE? @betatim

@betatim
Copy link
Member

betatim commented Nov 7, 2024

I thought dlpack now allows you to move arrays from one device to another? For example to support the common case of "i would like to convert this array from library foo to a numpy array". Is that right? If yes, it could be cool to support this for the "not CPU devices" here. If only because (in scikit-learn) we convert things back to Numpy arrays during tests.

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.

Should we be implementing __array__?
3 participants