Code Monkey home page Code Monkey logo

pydlpack's Introduction

This is an example of:

  1. Create and allocate a DLPack tensor in a plain C library. Memory is supposed to also be freed in C with a custom deallocator.
  2. Wrap DLPack structures in Python with ctypes. dlpack.py should in theory be reusable as is outside this example project. Hopefully, in future there will be official ctypes DLPack bindings
  3. (experimentally) Consume the DLPack tensor in NumPy. Hopefully, in future there will be an official method for this. The main difficulty is absence of memory leaks and segfaults related to freeing the memory.
  4. Demonstrate of importing DLPack tensor into PyTorch

IMPORTANT Currently the sort-of-supported semantics of consuming DLPack tensors is move semantics. So consume the DLPack tensor only once. Also please monitor memory, I do not guarantee absence of leaks.

A more complete example of reading an audio file with ffmpeg library in C and consuming it via DLPack is available at https://github.com/vadimkantorov/readaudio

DLPack header is taken from https://github.com/dmlc/dlpack/blob/3efc489b55385936531a06ff83425b719387ec63/include/dlpack/dlpack.h

License for my code: dlpack.c and dlpack.py

MIT

Usage

# dump golden.bin with some test data
make dlpack
./dlpack golden.bin

# dump numpy.bin and torch.bin
make libdlpack.so
python3 dlpack.py numpy.bin
python3 dlpack.py torch.bin

# check that both ways of importing DLPack tensors work
diff golden.bin numpy.bin
diff golden.bin torch.bin

pydlpack's People

Contributors

vadimkantorov avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

judahrand

pydlpack's Issues

License?

Hi,

What license is your code shared under? Would you be willing to consider GPL-3?

Alternatively, specifying any license at all will help people be able to work off your code, reuse it, and make contributions back. They can judge whether doing the work will meet their intended uses. Without a license, one can't know what the future holds.

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.