Git Product home page Git Product logo

pydlpack's Introduction

Python package Conda Version

pydlpack

PyDLPack is a Python library for exchanging data between different array libraries using DLPack: Open In Memory Tensor Structure. 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:

Array Interface Protocol, version 2 support can be provided on request.

Currently, the package is tested with the following consumers

using the following provider objects with devices:

Install

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()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.