tch-rs: Rust bindings for the PyTorch C++ API providing tensor operations and neural network training

tch-rs: Rust bindings for the PyTorch C++ API providing tensor operations and neural network training

What it solves

tch-rs provides Rust bindings for the PyTorch C++ API (libtorch), allowing developers to use PyTorch's powerful tensor operations, automatic differentiation, and neural network modules within the Rust programming language. It bridges the gap for those who want the performance and safety of Rust while leveraging the established ecosystem of PyTorch.

How it works

The project provides thin wrappers around the C++ PyTorch API. It is designed to stay as close as possible to the original C++ API to ensure consistency. The library links against libtorch, which can be provided via a system-wide installation, a Python PyTorch installation, or downloaded automatically during the build process.

Who it’s for

It is intended for Rust developers who want to build, train, and deploy machine learning models using PyTorch's functionality without needing to write Python code.

Highlights

  • Tensor Operations: Support for basic tensor manipulation and mathematical operations.
  • Automatic Differentiation: Ability to train models via gradient descent using nn::VarStore and optimizers like SGD and Adam.
  • Neural Network API: Tools to build architectures with linear layers and activation functions (e.g., ReLU).
  • Pre-trained Model Support: Ability to load weights from PyTorch files or the safetensors format for immediate inference.
  • TorchScript JIT: Support for deploying and running models trained in Python via TorchScript.
  • Cross-Platform: Support for Linux, macOS, and Windows, with CUDA support for GPU acceleration.

Sources