-
Notifications
You must be signed in to change notification settings - Fork 27.7k
[Feature Request] Support tensor creation from objects that implement the __array__ interface #51156
Copy link
Copy link
Open
Labels
function requestA request for a new function or the addition of new arguments/modes to an existing function.A request for a new function or the addition of new arguments/modes to an existing function.module: numpyRelated to numpy support, and also numpy compatibility of our operatorsRelated to numpy support, and also numpy compatibility of our operatorsneeds designWe want to add this feature but we need to figure out how firstWe want to add this feature but we need to figure out how firsttriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Metadata
Metadata
Assignees
Labels
function requestA request for a new function or the addition of new arguments/modes to an existing function.A request for a new function or the addition of new arguments/modes to an existing function.module: numpyRelated to numpy support, and also numpy compatibility of our operatorsRelated to numpy support, and also numpy compatibility of our operatorsneeds designWe want to add this feature but we need to figure out how firstWe want to add this feature but we need to figure out how firsttriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
🚀 Feature
It would be great for
torch.tensor(obj),torch.from_numpy(obj), and other tensor-creation functions to checkobj.__array__()before raising an error thatobjis of the wrong type.I'd be happy to give this a shot.
Motivation
NumPy provides a standard way for a class to expose a
numpy.ndarrayso that NumPy functions likeasarraycan know how to cast it to an array. This standard is for the class to implement an__array__()method that returns a standardnumpy.ndarray.This standard makes it really convenient for non-ndarray classes to nonetheless interface nicely with NumPy, xarray, and other array-based libraries.
We can make a ndarray out of
my_data...my_datacan even take the form of an xarray-DataArrayBut
my_datacannot easily be made a tensor!Pitch
I would be happy to take a swing at this as long as this feature can be implemented without leaving the pure-Python side of things.
cc @mruberry @rgommers @heitorschueroff