-
Notifications
You must be signed in to change notification settings - Fork 46
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
RFC: add a way to convert back to Python (tolist
)
#710
Comments
There're some difficulties that NumPy folks are discussing: numpy/numpy#24989 |
Well, maybe the standard should introduce a new name then, such as |
The semantic issues with Would one actually need this for arrays of arbitrary dimensionality? Some real-world examples would be good to see. |
I don't know which dunder method would that be. I think currently My use case was for 0D arrays, more similar to NumPy's |
For 0-D arrays, |
I think there was a misunderstanding... I do not want a list returned for 0D arrays, but a dtype- independent way to convert them to a Python object that can hold them, that works also for non-standard dtypes. |
What you need to use is NumPy has the As I said on the NumPy issue, maybe raveling would be the more useful default behavior, although I am not sure... Loosing dimensions is also surprising! Whatever the solution, maybe a new name is fine, maybe one should just keep the tolist name but make |
This issue made me wonder about converting from one namespace to another. Say from PyTorch to Numpy. This works: x = array_api_compat.torch.asarray([1,2,3])
array_api_compat.numpy.asarray(x) # -> array([1, 2, 3]) The reason I was thinking about this was that it would be nice to have a consistent way of converting things. Of course, there is no |
We did discuss a possibility to standardize bringing data from any array object to Python. It would make sense to have a function that would transfer content of array into another type that exposes Python buffer protocol. From here the content could be converted to NumPy, or passed to |
Non-standard dtypes may not have a pure Python equivalent, so that's quite tricky clearly. Things like def convert_0D_arrays(x):
if not x.ndim == 0:
raise ValueError('...')
if xp.isdtype(x, 'real floating'):
return float(x)
elif xp.isdtype(x, 'complex floating'):
return complex(x)
# etc. Static typing is also easier outside of a magical do-it-all function, because you can add the overloads for the different return types.
This can be done with |
The thing is that if the arrays implement additional dtypes apart from the standard ones (something allowed in the standard, as far as I know), this is not so easy to do from the user side. Consider for example the
So, what can a user do to retrieve the object? And note that although it is valid to say "this is not a problem for the standard, as it only requires numerical types with dunder methods", I still see the value in standardizing at least the naming and interface of a function similar to NumPy |
There's a reasonable amount of consensus, among both NumPy devs and devs from other array libraries, that NumPy's scalars were a design mistake. They add a large amount of complexity, and we'd remove them from NumPy if we could (but, backwards compat). So I don't think this is going to fly.
There is only one library that supports datetime dtypes, namely NumPy. So you can explicitly handle that case with a NumPy function. |
Just thinking out loud... If we agree that a "0D list" is an ill-defined construct, perhaps we can at least have a clean
|
That seems reasonable @leofang. However, there are going to be other exceptions aside from 0-D arrays, because leaving array land isn't always possible. E.g., what about non-CPU devices or detaching from an autograd graph? |
In case that this is implemented I would rather have the natural implementation for 0D arrays: returning a non-list (either a Python representation of the scalar value itself or the array unchanged) so that |
Adding to
and
remarks, should we use a separate issue that covers inter-namespace conversion specifically rather than tolist ? I want to emphasize with this usecase I have when trying to adapt code for Array API compliance. There is some code that can't compromise on numerical accuracy and absolutly requires at least float64 precision, but I could use an integrated GPU that supports at most float32 (e.g using For this usecase an intermediate object that enable inter-device and inter-namespace conversion surely would be practical.
wouldn't it be practical to add to the Array API a conversion to numpy, e.g |
Related discussion #626 |
tolist
)tolist
)
I think (correct me if I am mistaken) that currently the only way to convert an array object back to a Python representation is to call
float
,int
,bool
, etc on 0D arrays. This requires that the user knows the appropriate function to call and does not offer any standard way to retrieve the underlying Python object when the library has additional dtypes, such asobject
in NumPy.Moreover, as there is no
tolist
in the standard, it is also not possible to obtain a list representation of the array (from which the Python object could be retrieved).I propose to add
tolist
to the standard, as defined in NumPy and Pytorch to deal with these cases. Although the name is a bit misleading (because for 0D arrays there is no list at all), I think that prior art justifies reusing that name.The text was updated successfully, but these errors were encountered: