pydlpack
PyDLPack is a Python library for exchanging data between different array libraries using [DLPack: Open In Memory Tensor Structure]((https://github.com/dmlc/dlpack/). The provider library does not need to implement the DLPack support, it will be sufficent if the provider library implements one of the following protocols:
- dmlc/dlpack
- Array Interface Protocol, version 3
- CUDA Array Interface, version 3
- Python Buffer protocol
Array Interface Protocol, version 2 support can be provided on request.
Currently, the package is tested with the following consumers
- NumPy,
numpy.from_dlpack - PyTorch,
torch.from_dlpack - CuPy,
cupy.from_dlpack - Jax,
jax.numpy.from_dlpack - Tensorflow,
tf.experimental.dlpack.from_dlpack - cuDF,
cudf.from_dlpack
using the following provider objects with devices:
- Numpy
ndarray, CPU - Torch
Tensor, CPU and CUDA - CuPy
ndarray, CUDA - Numba
DeviceNDArray, CUDA - Jax
Array, CPU and CUDA - Tensorflow
Tensor, CPU - Python
bytes, CPU - Python
bytearray, CPU - Python
array, CPU - Numpy
memmap, CPU - Python
mmap, CPU
Install
-
conda install pydlpack
-
pip install pydlpack
Basic usage
>>> from dlpack import asdlpack
>>> import torch
>>> dl = asdlpack(b"Hello!")
>>> torch.from_dlpack(dl)
tensor([ 72, 101, 108, 108, 111, 33], dtype=torch.uint8)
that is, the Python package dlpack provides a function asdlpack
that input can be any object that implements one of the above
mentioned protocols and it will return a light-weight DLPackObject
instance which implements the DLPack protocol methods
__dlpack__(stream=None) and __dlpack_device__(). This
DLPackObject instance can be used as an argument to a
consumer.from_dlpack(obj) function of any DLPack-compatible consumer
library (a partial list of such libraries is listed above). For
example:
>>> from dlpack import asdlpack
>>> import numba.cuda
>>> import numpy
>>> arr = numba.cuda.to_device(numpy.array([[1, 2], [3, 4]]))
>>> arr
<numba.cuda.cudadrv.devicearray.DeviceNDArray object at 0x7fbed9c548b0>
>>> dl = asdlpack(arr)
>>> import torch
>>> torch.from_dlpack(dl)
tensor([[1, 2],
[3, 4]], device='cuda:0')
>>> import jax
>>> jax.numpy.from_dlpack(dl)
Array([[1, 2],
[3, 4]], dtype=int32)
>>> import cupy
>>> cupy.from_dlpack(dl)
array([[1, 2],
[3, 4]])
that is, the DLPackObject instance can be efficiently used for
exchanging the CUDA buffer created using Numba to_device
functionality with different consumer objects such as torch.Tensor,
jax.Array, and cupy.ndarray while all these array objects share the
same CUDA memory.
Testing
It is a non-trivial task to install all dlpack-compatible libraries
into the same environment. Therefore, dlpack tests are included the
dlpack package so that one can import dlpack and run the tests on
DLPack-compatible objects that are available in a particular
environment. For example:
>>> import dlpack.tests
>>> dlpack.tests.run()
Metadata
Release files for pydlpack 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydlpack-0.2.1.tar.gz | 22.2 kB | Without provenance | Details for pydlpack-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydlpack-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for pydlpack-0.2.1-py3-none-any.whl |
Total release size: 41.6 kB
Release files / pydlpack-0.2.1.tar.gz
| Download URL | pydlpack-0.2.1.tar.gz |
|---|---|
| Size | 22.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b3b1b0dd2292fac72a020b8c191fdaf32be6f9471ec696c88f753fbc042c28f
|
|
BLAKE2b-256 checksum How to use checksums |
872348c707f166468bf116740b2922b5ff745fdfd72bb40304a0358ce495d4b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / pydlpack-0.2.1-py3-none-any.whl
| Download URL | pydlpack-0.2.1-py3-none-any.whl |
|---|---|
| Size | 19.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b42ef189511e5ab4e1eafa42c5f0825d2b4349518020a3a9c33390890deb7cb2
|
|
BLAKE2b-256 checksum How to use checksums |
3df8ae765c0f3fac5f10119d51c9b82002ea709c1899190c4743e0b124e82149
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for pydlpack 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.