Description
Feature
Motivation
Currently, any library which needs to exchange data with NumPy needs to have it as a dependency. This issue will allow moving away from that approach to a more Pythonic standards-based approach leveraging the DLPack Library, as described in the Array API standard. This is also mentioned in and a prerequisite for NEP 47, although it can be discussed and integrated independently of it as well, the only caveat being that if the NEP is accepted; adopting DLPack is a given.
DLPack is a small C header-only library with a stable ABI.
Changes needed to NumPy
The numpy.ndarray
type will need to gain two new methods:
__dlpack__
: This will return aPyCapsule
with a DLPack struct.__dlpack_device__
: This one will always return the CPU device for NumPy.
And the NumPy namespace will gain one extra function:
from_dlpack
: This will consume aPyCapsule
containing a DLPack struct and create anumpy.ndarray
based on that. It will raise aRuntimeError
on all unsupported configurations of the object.
Relevant issues/discussion:
- [RFC] Adopt DLPack as cross-language C ABI stable data structure for array exchange data-apis/consortium-feedback#1
- Array API docs
- Links to existing implementations: [RFC] Adopt DLPack as cross-language C ABI stable data structure for array exchange data-apis/consortium-feedback#1 (comment)
- PyTorch in-progress PR: Adds DLPack support pytorch/pytorch#57110
- Discussion on the DLPack issue tracker: Import DLPack tensors directly into NumPy (without going via PyTorch or TF) dmlc/dlpack#55
Edit: Previously this issue said from_dlpack
was a method, that has been corrected.