Rust bindings for PyTorch C++ API (libtorch)
Top 10.2% on sourcepulse
This Rust crate provides low-level bindings to the C++ PyTorch (libtorch) API, enabling Rust developers to leverage PyTorch's deep learning capabilities. It aims to mirror the C++ API closely, serving as a foundation for higher-level Rust abstractions.
How It Works
The tch-rs
crate acts as a thin wrapper around libtorch
, the C++ backend of PyTorch. It utilizes code generation derived from ocaml-torch
to create bindings for the C API of libtorch
. This approach ensures a direct mapping to PyTorch's core functionalities, allowing for efficient tensor operations, automatic differentiation, and neural network construction within a Rust environment.
Quick Start & Requirements
libtorch
version v2.7.0. Can be linked to a system-wide installation, a Python PyTorch installation (LIBTORCH_USE_PYTORCH=1
), or downloaded via the download-libtorch
feature. CUDA support requires TORCH_CUDA_VERSION
to be set (e.g., cu117
).LIBTORCH
environment variable to the libtorch directory.LIBTORCH_STATIC=1
and compile libtorch manually.Highlighted Details
nn
), optimizers, and utilities for vision tasks (e.g., MNIST, ResNet).safetensors
.Maintenance & Community
Licensing & Compatibility
Limitations & Caveats
torch-sys
rebuilds; environment variables like LIBTORCH
need to be correctly configured for IDEs like rust-analyzer.LD_LIBRARY_PATH
or DYLD_LIBRARY_PATH
.1 day ago
1 day