#pytorch #deep-learning #machine-learning

tch-ext

Sample Python extension using tch to interact with PyTorch

4 releases

0.2.0 Oct 17, 2024
0.1.2 Feb 5, 2024
0.1.1 May 30, 2023
0.1.0 May 18, 2023

#406 in Machine learning

Download history 6/week @ 2024-07-25 1/week @ 2024-08-01 11/week @ 2024-09-19 16/week @ 2024-09-26 7/week @ 2024-10-03 150/week @ 2024-10-17 3/week @ 2024-10-24 1/week @ 2024-10-31

154 downloads per month

MIT/Apache

3.5MB
96K SLoC

Rust 78K SLoC // 0.0% comments C++ 18K SLoC // 0.0% comments OCaml 830 SLoC // 0.0% comments Python 32 SLoC

Python extensions using tch

This sample crate shows how to use tch to write a Python extension that manipulates PyTorch tensors via PyO3.

This is currently experimental hence requires some unsafe code until this has been stabilized.

In order to build the extension and test the plugin, run the following in a Python environment that has torch installed from the root of the github repo.

LIBTORCH_USE_PYTORCH=1 cargo build -p tch-ext && cp -f target/debug/libtch_ext.so tch_ext.so
python examples/python-extension/main.py

It is recommended to run the build with LIBTORCH_USE_PYTORCH set, this will result in using the libtorch C++ library from the Python install in tch and will ensure that this is at the proper version (having tch using a different libtorch version from the one used by the Python runtime may result in segfaults).

Colab Notebook

tch based plugins can easily be used from colab (though it might be a bit slow to download all the crates and compile), see this example notebook.

Dependencies

~10–17MB
~231K SLoC