Repolode
LaurentMazare

LaurentMazare/tch-rs

Rust bindings for the C++ API of PyTorch (libtorch), letting Rust code build, train and run deep learning models.

Stars
5,516
Forks
463
Open issues
276
Last commit
3 months ago

What is it?

tch-rs provides the `tch` crate, a set of thin wrappers around the C++ PyTorch API (libtorch). It stays close to the original C++ interface, so developers can call tensor operations, automatic differentiation, and neural network modules from Rust. More idiomatic Rust APIs can be built on top of it.

The crate covers tensor creation and arithmetic, gradient-based training through a VarStore and optimizers such as SGD and Adam, and TorchScript JIT model loading. It also includes vision utilities such as MNIST loading, ImageNet preprocessing, and pretrained ResNet and VGG models, and can import weights exported from PyTorch in the safetensors format.

The crate requires libtorch v2.13.0, which can be taken from a system install, a manual download, a local Python PyTorch installation, or fetched automatically via the download-libtorch feature. Static linking is also supported.

Key features

  • 01Thin wrappers over the libtorch C++ API with a tensor type and common tensor operations.
  • 02Automatic differentiation with optimizers such as SGD and Adam via VarStore.
  • 03Loading TorchScript JIT models and importing PyTorch weights in safetensors format.
  • 04Vision utilities including MNIST loading, ImageNet preprocessing, and pretrained ResNet and VGG models.
  • 05Build options for system libtorch, manual install, Python PyTorch, or automatic download.

Use cases

  • Writing or training deep learning models in Rust while relying on PyTorch's kernels.
  • Running inference with pretrained vision models or TorchScript models from Rust services.
  • Porting PyTorch model code to Rust while keeping the same libtorch backend.
  • Building higher-level Rust machine learning libraries on top of libtorch.

Quick start

  1. 1

    Install libtorch v2.13.0 (system-wide, or download it from the PyTorch website)

  2. 2

    Point the build to your libtorch directory with the LIBTORCH variable

    export LIBTORCH=/path/to/libtorch
  3. 3

    Run an example to check the setup

    cargo run --example basics

Things to know

  • Requires a separate libtorch v2.13.0 install, which adds setup effort.
  • Windows users should use the MSVC toolchain, since MinGW has PyTorch compatibility issues.
  • Static linking requires manually building libtorch from source.

Topics

FAQ

What is tch-rs?
Rust bindings for the C++ API of PyTorch (libtorch), letting Rust code build, train and run deep learning models. tch-rs provides the `tch` crate, a set of thin wrappers around the C++ PyTorch API (libtorch). It stays close to the original C++ interface, so developers can call tensor operations, automatic differentiation, and neural network modules from Rust. More idiomatic Rust APIs can be built on top of it.
Is tch-rs free to use? What license does it have?
tch-rs is open source under the Apache-2.0 license. Check the license terms before using it in commercial products.
Is tch-rs still maintained?
Updated within 6 months. The last commit to tch-rs was on Aug 23, 2026.
What can I use tch-rs for?
Writing or training deep learning models in Rust while relying on PyTorch's kernels. Running inference with pretrained vision models or TorchScript models from Rust services. Porting PyTorch model code to Rust while keeping the same libtorch backend. Building higher-level Rust machine learning libraries on top of libtorch.
How do I install tch-rs?
Install libtorch v2.13.0 (system-wide, or download it from the PyTorch website) Point the build to your libtorch directory with the LIBTORCH variable: export LIBTORCH=/path/to/libtorch Run an example to check the setup: cargo run --example basics
Where can I get tch-rs?
The source code and installation instructions are on GitHub: https://github.com/LaurentMazare/tch-rs

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects