GitXplorerGitXplorer
L

tch-ext

public
34 stars
0 forks
3 issues

Commits

List of commits on branch main.
Unverified
aa2a358353c0970367a2c5521e3a8023a4c5cd00

Bump the pytorch version used in the CI.

LLaurentMazare committed a year ago
Unverified
b9a5f285fad2a815fbc2ef828ebf740152df7d3e

Bump the tch version.

LLaurentMazare committed a year ago
Unverified
bfe28d08e87cab542b716445e70ba906a978f80c

Readme update.

LLaurentMazare committed 2 years ago
Verified
e4d325c44b89bdf760d0eeac56de5af0bd5bf78c

Minor tweaks

LLaurentMazare committed 2 years ago
Verified
3147ea9636a00d9672a017ef9510ba7c3bacbc43

Update README.md

LLaurentMazare committed 2 years ago
Unverified
6037aa39cdb4925d89c3c55fb5f4e8bd6850a7d8

Bump the crate version.

LLaurentMazare committed 2 years ago

README

The README file for this repository.

Python extensions using tch to interact with PyTorch

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

There is a single function exposed by the Python extension which adds one to the input tensor. The relevant code is as follows:

#[pyfunction]
fn add_one(tensor: PyTensor) -> PyResult<PyTensor> {
    let tensor = tensor.f_add_scalar(1.0).map_err(wrap_tch_err)?;
    Ok(PyTensor(tensor))
}

It is recommended to use the f_ methods so that potential errors in the tch crate do not result in a crash of the Python interpreter.

Compiling the Extension

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

LIBTORCH_USE_PYTORCH=1 cargo build && cp -f target/debug/libtch_ext.so tch_ext.so
python test.py

Setting LIBTORCH_USE_PYTORCH results in using the libtorch C++ library from the Python install in tch and ensures 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.