Repository navigation
DOC: Added explanation document on interoperability - #20185
Conversation
|
Read this now—as a newbie on the existing interop features, I think the page does a great job in both outlining the avenues available and going through how other libraries actually use them. It took me a second to appreciate how |
|
Thanks @honno - indeed I think that's valuable, will do! |
rossbar
left a comment
There was a problem hiding this comment.
This is an excellent document, very nicely done @melissawm !
One high-level thing that stood out to me in the discussion surrounding __array_ufunc__ vs. __array_function__. I feel the discussion here assumes that the reader has a firm grasp on what the difference between a ufunc and a "non-ufunc" numpy function is, which is perhaps not a safe assumption. One possible remedy could be to use the np.mean(np.exp) example to point out this distinction more explicitly (i.e. exp is a ufunc but mean is not).
Other than that I only had nits, mostly related to preferring internal sphinx/intersphinx links to external links for futureproofing. Definitely not a blocker though!
Definitely not a safe assumption:) |
rgommers
left a comment
There was a problem hiding this comment.
Thanks @melissawm, this is a very nice overview. I added some high level comments. One big picture comment on the whole doc: it would be good to make a better conceptual distinction between:
- Methods of turning a foreign object into an ndarray (buffer protocol et al.)
- Methods of deferring execution from a NumPy function to another array library (
__array_ufunc__et al.)
Those are very different things, and right now they are all called "protocol" and mixed together.
Finally, it would be appropriate to mention __array_wrap__, __array_finalize__ & co. That is a third type of feature set, meant to (a) use the numpy function implementation, and (b) then turn the return value back into an instance of the foreign object.
| array-like object; | ||
| - ``__array__()``: a method returning the NumPy ndarray view of an array-like | ||
| object; | ||
| - ``__array_struct__``: a ``PyCapsule`` containing a pointer to a |
There was a problem hiding this comment.
__array_struct__ is basically obsolete, I'd prefer to either remove it or otherwise to at least add in a note that it's legacy and should not be used for new code. Use the buffer protocol instead.
There was a problem hiding this comment.
Maybe it would work better to add this note in the main array interface protocol page page, and not even mention it here.
| The ``__array__`` protocol | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The ``__array__`` protocol acts as a dispatch mechanism and ensures that any |
There was a problem hiding this comment.
It's not a dispatch mechanism I'd say. Dispatching means something like "deferring execution to an implementation elsewhere". That is not the case here. Instead let's just say it's a way to request the object to turn itself into a np.ndarray instance. The original object, and any attributes/behavior it may have had, is lost.
There was a problem hiding this comment.
OK - I rephrased it, let me know if it reads better.
|
Great - thanks for the reviews, folks! I will address those shortly.
I'll do a little research but they didn't turn up in my initial readings. Do you know of any sources for that besides the API docs? |
|
79b6702 to
b4aff73
Compare
|
cc: @kmaehashi @emcastillo for vis |
b4aff73 to
c55b507
Compare
|
I think this is ready for a second round of reviews. |
rgommers
left a comment
There was a problem hiding this comment.
The description of __array_wrap__ is a little misleading (that's the fault of the docs you reference). It focuses on "subtypes", which is a word to avoid anyway because with type annotations in Python 3 it now has a different meaning. But really subclasses/subtypes are not needed for this. Any object can implement this and it'll be called at the end - allowing "resetting" the output type to the correct container. A small demo to demonstrate:
>>> import torch
>>> t = torch.arange(4)
>>> np.abs(t)
tensor([0, 1, 2, 3])
>>>
>>> wrap = lambda self, array: np.asarray(array)
>>> torch.Tensor.__array_wrap__ = wrap
>>> t = torch.arange(4)
>>> np.abs(t)
array([0, 1, 2, 3])So PyTorch implements __array_wrap__ to be able to get tensors back. And if we modify it, we see that it's a simple Python method that does whatever you put inside. And torch.Tensor is not a subclass/subtype of numpy.ndarray.
|
Ah - thanks for that note, makes sense, I'll update accordingly. Do you think the current structure makes the distinctions between the groups of features that you had mentioned before clearer? Happy to re-work if this is not yet sufficient. |
I think that looks quite good. Overall this is close to ready I'd say. |
| >>> t = torch.arange(4) | ||
| >>> np.abs(t) | ||
| tensor([0, 1, 2, 3]) | ||
| >>> wrap = lambda self, array: np.asarray(array) |
There was a problem hiding this comment.
From this line on it's best to delete the example (only keep until tensor([0, 1, 2, 3])). I included it to show you that it really was __array_wrap__ that is responsible for creating the tensor return type, but now the example looks like this is somehow normal usage.
There was a problem hiding this comment.
Should the docstring get a pragma to be ignored in doctests? I don't think we want to import tensorflow
There was a problem hiding this comment.
I have added this page to the refguide exclude list as none of it is very useful to run as doctests.
| .. note:: | ||
|
|
||
| :obj:`__array_struct__` is considered legacy and should not be used for new | ||
| code. Use the :py:doc:`buffer protocol <c-api/buffer>` instead. |
There was a problem hiding this comment.
Can we deprecate it? I think also ndarray.dlpack could be mentioned as an alternative, although I see it is not part of the documentation ...
Checking for uses of __array_struct__ I found only pygame, and opened pygame/pygame#2949 to ask about removing it
There was a problem hiding this comment.
Should I add a deprecation note here? Or wait until it is actually marked for deprecation?
| protocol, it defines a way to access the contents of a NumPy array from other | ||
| C extensions. | ||
| 3. The ``__array__()`` method, which asks an arbitrary object to convert | ||
| itself into an array. |
There was a problem hiding this comment.
Should we add the __dlpack__() method
There was a problem hiding this comment.
sorry, that is in a different section
There was a problem hiding this comment.
Do you mean adding a mention of __dlpack()__ in the arrays.interface doc? I can at least add a link to https://data-apis.org/array-api/latest/design_topics/data_interchange.html#dlpack-support
There was a problem hiding this comment.
Maybe leave it out for now. I thought we had a tracking issue for finishing dlpack, including documentation.
There was a problem hiding this comment.
So long we don't have first-class consumer API, it doesn't really make sense to expose it any case. This should be added as part of making that consumer API first-class.
|
Let's put this in as-is and iterate on it as the 1.23 release progresses |
|
Thanks @melissawm |
|
Is there a place where the dev docs (the |
This document is an explanation on current interoperability features for NumPy, including examples with Pandas, CuPy, Dask and PyTorch.
It is very likely that this is incomplete or inaccurate, as I was learning while writing it, so feel free to suggest changes. I have added this document to the skip list in
refguide_check.pybecause of the code involving other libraries which are not dependencies to our documentation (and don't need to be).EDIT: rendered page here