Skip to content

Support Array Interface (__array_interface__ attribute) #54138

@pearu

Description

@pearu

🚀 Feature

Implement torch.Tensor.__array_interface__ attribute as defined by Array Interface

Motivation

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:

>>> import torch
>>> t = torch.tensor([1, 2, 3, 4, 5])
>>> import numpy
>>> numpy.asarray(t).__array_interface__
{'data': (94135211131264, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (5,), 'version': 3}

Dependence on NumPy for sharing data buffers between PyTorch and other array-like objects is unnecessary.

Pitch

Add a new attribute __array_interface__ to a torch.Tensor object that

  • is a dictionary containing data buffer data as defined by the Array Interface.
  • is enabled only when the Tensor object uses CPU storage. This behavior is in line with the __cuda_array_interface__ attribute availability (that is enabled only when the Tensor object uses CUDA storage).
  • is backward compatible to existing torch.Tensor.__array__() method because numpy.asarray prefers to use __array_interface__ protocol over the __array__ protocol.

Alternatives

  • The alternative is to wrap PyTorch Tensor object with NumPy ndarray as described above.
  • Array Interface defines __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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNot 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 operatorstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions