Skip to content

Create a DLPack tensor in plain C library, wrap DLPack structures in Python, import DLpack tensor into NumPy / PyTorch

Notifications You must be signed in to change notification settings

vadimkantorov/pydlpack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Create a DLPack tensor in plain C library, wrap DLPack structures in Python, import DLpack tensor into NumPy / PyTorch

Topics

Resources

Stars

Watchers

Forks