-
Notifications
You must be signed in to change notification settings - Fork 27.7k
Support Array Interface (__array_interface__ attribute) #54138
Copy link
Copy link
Open
Labels
enhancementNot as big of a feature, but technically not a bug. Should be easy to fixNot as big of a feature, but technically not a bug. Should be easy to fixmodule: numpyRelated to numpy support, and also numpy compatibility of our operatorsRelated to numpy support, and also numpy compatibility of our operatorstriagedThis 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
enhancementNot as big of a feature, but technically not a bug. Should be easy to fixNot as big of a feature, but technically not a bug. Should be easy to fixmodule: numpyRelated to numpy support, and also numpy compatibility of our operatorsRelated to numpy support, and also numpy compatibility of our operatorstriagedThis 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
Implement
torch.Tensor.__array_interface__attribute as defined by Array InterfaceMotivation
The array interface allows no-copy sharing of data buffers in between different array-like objects.
PyTorch currently implements
torch.Tensor.__cuda_array_interface__attribute defined by CUDA Array Interface for conveniently sharing data stored in CUDA devices while sharing data in CPU memory is currently possible via NumPy ndarray object:Dependence on NumPy for sharing data buffers between PyTorch and other array-like objects is unnecessary.
Pitch
Add a new attribute
__array_interface__to atorch.Tensorobject that__cuda_array_interface__attribute availability (that is enabled only when the Tensor object uses CUDA storage).torch.Tensor.__array__()method becausenumpy.asarrayprefers to use__array_interface__protocol over the__array__protocol.Alternatives
__array_struct__as an alternative way to share data buffers.__array_struct__can be considered obsolete via PEP 3118- Buffer Protocol.Additional context
This feature request is a follow-up to #51156 and https://pearu.github.io/array_interface_pytorch.html discussions.
cc @mruberry @rgommers @heitorschueroff