LaurentMazare/tch-rs
73.6
Strong · 13 September 2026
10.9k
lines of production code
Rust
primary language
1
measurement over time
What this system is
This system is a Rust library (tch) that provides bindings to the PyTorch C++ API, enabling tensor operations, neural network layer definitions, and model training directly within Rust. It supports a wide range of deep learning tasks through extensive examples, including computer vision, natural language processing, reinforcement learning, and generative models like Stable Diffusion and LLaMA. The library facilitates interoperability with Python via PyO3 and handles model serialization in formats such as Safetensors and TorchScript.
How it got here
2019 — Library rewrite and feature expansion
26 changes.
The project underwent a major architectural overhaul, renaming to 'tch' and replacing low-level C bindings with a structured, idiomatic Rust API featuring modular components for tensors, neural networks, and vision. This period established the core library foundation, including PyTorch 2.13 support, comprehensive error handling, and data iteration utilities, while simultaneously expanding capabilities with pre-trained models and diverse training examples.
2020–2023 — expansion of example ecosystem
10 changes.
This period focused on significantly expanding the project's example suite to cover diverse use cases, including custom optimizers, JIT-compiled models, quantized inference, and large language model inference. It also introduced a dedicated pyo3-tch package to facilitate seamless interoperability between Rust and Python PyTorch tensors.
Features
Add CIFAR-10 ResNet training example
A new example demonstrating how to train a ResNet model on the CIFAR-10 dataset has been added. The code shows how to use sequential layers, 2D convolutions, batch normalization, and data augmentation to achieve approximately 95.4% accuracy.
examples/cifar · high confidence
Add JIT quantized model inference example
Added a new example demonstrating how to load and run quantized PyTorch models (INT8) in Rust. The example includes a Python script to export a quantized ResNet-18 model and a Rust program that loads the TorchScript module, allowing users to select the FBGEMM or QNNPACK quantization engine for inference on resource-constrained devices.
examples/jit-quantized · high confidence
Add LLaMA inference example with checkpoint conversion
Added a new LLaMA example in the examples/llama directory, including a Rust implementation for inference and a Python script to convert LLaMA checkpoints into the .safetensors format. The Rust code implements the model architecture (including RMSNorm, MLP, and causal self-attention with rotary embeddings) and a SentencePiece tokenizer, allowing users to run inference on converted LLaMA weights.
examples/llama · high confidence
Add MNIST example suite with linear, neural network, and convolutional models
The examples/mnist directory now contains a complete set of MNIST training examples, including a linear classifier, a simple neural network, and a convolutional neural network. These examples demonstrate how to load MNIST data, define models using the tch-rs API, and train them with optimizers like Adam or manual gradient descent. A main.rs entry point allows users to select which model to run via command-line arguments.
examples/mnist · high confidence
Add Python extension example using tch and PyO3
A new example has been added to demonstrate how to create a Python extension using the \tch\ crate and PyO3. This sample crate provides a Rust implementation of a Python module (\tch\_ext\) that manipulates PyTorch tensors, including a build script for linking and a test script to verify functionality.
examples/python-extension · high confidence
Add Relativistic DCGAN example
The examples/gan directory now includes a new Relativistic DCGAN implementation that demonstrates training a generator and discriminator using relativistic loss calculations, batch normalization, and configurable hyperparameters for image generation.
examples/gan · high confidence
Add Variational Auto-Encoder example for MNIST
A new example demonstrating a Variational Auto-Encoder (VAE) trained on the MNIST dataset has been added to the examples directory. This sample application implements the VAE architecture, including encoding, decoding, and loss calculation (reconstruction plus KL divergence), and provides functionality to train the model and generate visual samples of reconstructed digits.
examples/vae · high confidence
Add YOLOv3 object detection example
The \examples/yolo\ directory now contains a complete YOLOv3 implementation, including a Darknet configuration parser (\darknet.rs\), COCO class labels (\coco\_classes.rs\), and a main entry point (\main.rs\) that loads pre-trained weights, processes images, and outputs annotated results with bounding boxes using non-maximum suppression.
examples/yolo · high confidence
Add character-level RNN language model example
A new char-rnn example has been added to the examples directory, implementing a character-level language model inspired by Karpathy's char-rnn. The example demonstrates training an LSTM model on text data (such as the tiny Shakespeare dataset) to predict the next character in a sequence, and includes code for generating sample text using the trained model. Users can run the example via \cargo run --example char-rnn\ after placing their input text file in \data/input.txt\.
examples/char-rnn · high confidence
Add custom sparse Adam optimizer example
The \examples/custom-optimizer\ directory now includes a demonstration of a custom Sparse Adam optimizer implementation. This example shows how to handle both sparse and dense gradients, which is particularly useful for large embedding matrices where only a small portion of the matrix is updated, leading to significant training speed-ups. The implementation uses \index\_select\ and \index\add\ for sparse updates and \addcdiv\\/\addcmul\_\ for dense updates, and is validated on a MNIST classification task.
examples/custom-optimizer · high confidence
Add example for loading PyTorch JIT models in Rust
A new example demonstrates how to load and run a PyTorch TorchScript model (specifically a pre-trained ResNet-18) from Rust. The example includes a Python script to export the model using \torch.jit.trace\ and a Rust program that loads the resulting \.pt\ file via \tch::CModule::load\, performs inference on an image, and prints the top 5 predicted classes with their probabilities.
examples/jit · high confidence
Add minGPT-rs character-level language model example
A new Rust example implementing a character-level language model based on minGPT has been added to the examples directory. Users can now train the model on text files (such as the tiny Shakespeare dataset) to predict the next character in a sequence, and generate sample text using the trained weights. The example includes a custom linear layer implementation to apply weight decay selectively to weight matrices while excluding biases, and supports both training and prediction modes via command-line arguments.
examples/min-gpt · high confidence
Add neural style transfer example
The examples/neural-style-transfer directory now contains a Rust implementation of the neural style transfer algorithm, demonstrating how to apply the artistic style of one image to the content of another using a pre-trained VGG-16 model. The example supports GPU acceleration via CUDA, loads model weights from a specified file, and optimizes an input image using the Adam optimizer to minimize a combined style and content loss.
examples/neural-style-transfer · high confidence
Add pretrained-models example for vision classification
A new example application (\examples/pretrained-models/main.rs\) demonstrates how to load and run inference with pre-trained vision models. Users can now classify images using a wide range of supported architectures, including ResNet (18, 34), DenseNet (121), VGG (13, 16, 19), SqueezeNet (1.0, 1.1), AlexNet, Inception-v3, MobileNet-v2, EfficientNet (b0–b7), Convmixer, and DINOv2. The example handles image loading, resizing to 224x224, model instantiation, weight loading, and outputs the top 5 ImageNet classification probabilities.
examples/pretrained-models · high confidence
Add seq2seq translation example with attention
Introduces a new sequence-to-sequence translation example in the \examples/translation\ directory that translates between French and English using a GRU-based encoder-decoder architecture with attention. The example includes dataset loading and language tokenization utilities, implements teacher forcing during training, and provides prediction capabilities for generated translations.
examples/translation · high confidence
Add transfer learning example using pretrained ResNet
A new example demonstrating transfer learning has been added to the examples/transfer-learning directory. It shows how to load a pretrained ResNet-18 model, freeze its weights to extract features from an ants-vs-bees dataset, and train a simple linear classifier on top of those features using stochastic gradient descent.
examples/transfer-learning · high confidence
Added JIT tracing example for serializing and loading TorchScript models
A new example in \examples/jit-trace\ demonstrates how to train a neural network in Rust using the \tch\ crate and serialize it as a TorchScript program via JIT tracing. The example includes a Rust script (\main.rs\) that trains a CNN on the MNIST dataset and saves the traced model to \model.pt\, along with a Python script (\test.py\) that loads and evaluates the serialized model in a Python environment.
examples/jit-trace · high confidence
Initial release of the libtch C++ binding layer
This change introduces the core C++ implementation for the \torch-sys/libtch\ crate, establishing the foundational bridge between Rust and PyTorch. The new files include a CMake build configuration, a generated header and source file exposing a comprehensive set of PyTorch tensor operations (such as arithmetic, pooling, and convolution), and a manual API layer handling device management, tensor creation, and data copying. Additionally, the \stb\_image\, \stb\_image\_write\, and \stb\_image\_resize\ libraries are bundled to enable image loading, saving, and resizing capabilities directly within the binding layer.
torch-sys/libtch · high confidence
Initial release of the low-level Rust bindings for PyTorch
This change introduces the \src/wrappers\ module, providing the foundational Rust bindings for the PyTorch C++ API. It adds support for core tensor operations, including creation, shape/stride inspection, and arithmetic via generated \tensor.rs\ and \tensor\_generated.rs\ files. The update exposes device management for CPU, CUDA, MPS, and Vulkan backends, along with JIT scripting capabilities through the \IValue\ type and \jit.rs\ module. Additionally, it includes utilities for image I/O, optimizer implementations (Adam, AdamW, SGD, RMSProp), and scalar handling, enabling users to build and run PyTorch models directly from Rust.
src/wrappers · high confidence
Initial torch-sys FFI bindings and module structure
The \torch-sys\ crate now provides the foundational Rust bindings to the PyTorch C++ API, introducing a new modular structure with \lib.rs\ defining core types like \C\_tensor\ and \C\_scalar\, \c\_generated.rs\ containing auto-generated FFI declarations for tensor operations, \cuda.rs\ for GPU device management, and \io.rs\ for custom stream-based I/O. This change establishes the low-level interface for tensor creation, manipulation, and device handling, serving as the base layer for higher-level Rust PyTorch integration.
torch-sys/src · high confidence
Introduce OCaml bindings generator for PyTorch C++ API
The \gen\ directory now contains the source code for an OCaml-based tool that automatically generates C++ to Rust bindings for PyTorch. This generator reads a \Descriptions.yaml\ file (produced during PyTorch source builds) and produces Rust wrapper code, handling specific exclusions, optional scalar arguments, and various tensor types to ensure compatibility with the underlying C++ API.
gen · high confidence
Introduce dedicated pyo3-tch package for PyTorch tensor interoperability
A new pyo3-tch package has been added to provide a dedicated bridge between Rust's tch-rs and Python. This package exposes a PyTensor wrapper that implements PyO3's FromPyObject and IntoPyObject traits, enabling seamless conversion of tch-rs tensors to and from Python torch.Tensor objects. A build script is also included to handle linking against libtorch on Linux and Windows, ensuring the native library is correctly resolved at runtime.
pyo3-tch · high confidence
Introduce modular neural network layer library
This change introduces a new \src/nn\ module providing a PyTorch-like API for building neural networks. It includes core layer implementations such as Linear, Conv1D/2D/3D, BatchNorm, LayerNorm, GroupNorm, Embedding, and Recurrent layers (LSTM/GRU), along with transposed convolutions. The module also provides configuration structs for these layers, variable initialization schemes (Kaiming, Orthogonal, etc.), optimizers (SGD, Adam, AdamW, RMSProp), and utilities for managing variable stores and sequential model composition.
src/nn · high confidence
New example for training Torch Script models in Rust
Added a new \jit-train\ example that demonstrates how to load a PyTorch Torch Script model (serialized as \model.pt\) into Rust, train it on the MNIST dataset using the \TrainableCModule\ API, and save the updated weights. The example includes a Python script (\resnet.py\) to generate the initial Torch Script file and Rust code (\main.rs\) to handle the training loop and evaluation.
examples/jit-train · high confidence
New reinforcement learning examples for Atari and continuous control
The repository now includes a new \examples/reinforcement-learning\ directory featuring runnable implementations of several reinforcement learning algorithms. Users can train and sample from Advantage Actor-Critic (A2C) and Proximal Policy Optimization (PPO) agents on Atari games (e.g., Space Invaders) using a vectorized Python Gym environment wrapped in Rust, as well as a Policy Gradient example on CartPole and a Deep Deterministic Policy Gradient (DDPG) example for continuous control. The examples rely on the \tch\ library for tensor operations and \rust-cpython\ to interface with OpenAI Gym, with specific Python wrappers provided for Atari preprocessing.
examples/reinforcement-learning · high confidence
New tensor utility and memory stress test examples; updated basic example
The examples directory now includes a new \tensor-tools\ utility that allows users to list and convert tensors between formats including NPY, NPZ, safetensors, and the native OT format. A new \memory\_test\ example has been added to help detect memory leaks by creating large numbers of tensors on CPU or GPU. The \basics\ example has been updated to use the modern \tch\ API, demonstrating gradient computation, device availability checks (CUDA, cuDNN, MPS, Vulkan), and basic tensor operations.
examples · high confidence
New vision module with pre-trained models and dataset utilities
The \src/vision\ module introduces a comprehensive set of computer vision capabilities, including implementations of popular architectures such as AlexNet, ResNet (18/34/50/101/152), DenseNet, EfficientNet, MobileNet V2, Inception V3, VGG, SqueezeNet, ConvMixer, and DINOv2. It also provides dataset loaders for MNIST, CIFAR-10, and ImageNet, along with image utility functions for loading, saving, resizing, and data augmentation (flipping, cropping, cutout). Python scripts are included to export pre-trained weights from PyTorch models into the safetensors format for use with the Rust library.
src/vision · high confidence
Stable Diffusion example added and relocated
A new Stable Diffusion example has been added to the repository, providing a Rust implementation using libtorch to generate images from text prompts. The example includes a README with instructions for downloading and converting model weights. However, the README explicitly notes that this example has been moved to a separate repository, diffusers-rs, which now hosts the primary implementation along with additional features like inpainting and pre-packaged weights.
examples/stable-diffusion · high confidence
Tensor module refactoring and expanded conversion support
The tensor module has been reorganized into dedicated submodules (convert, display, index, iter, npy, ops, safetensors) to improve code structure. This change introduces robust conversion traits (TryFrom/TryInto) allowing tensors to be converted to and from standard Rust types like Vec, ndarray arrays, and primitive scalars (including f16 and bf16). It also adds support for reading and writing NumPy (.npy/.npz) and Safetensors formats, implements a flexible indexing API with the \i\ operator, and provides improved pretty-printing for tensor debugging.
src/tensor · high confidence
Removals
Removal of C++ PyTorch API bindings
The C++ source files providing the low-level PyTorch API bindings (torch\_api.cpp, torch\_api.h, and the generated wrappers) have been deleted. This removes the C++ interface layer that exposed tensor operations, optimizer management, and model loading to the rest of the system.
c · high confidence
Behavioural changes
Major API overhaul: new error handling, data iterators, and modular structure
The library has been refactored to replace the previous low-level, unsafe C bindings with a structured, idiomatic Rust API. Error handling now uses a dedicated \TchError\ enum (powered by \thiserror\) instead of raw pointers or panics, providing specific variants for I/O, shape mismatches, and Torch API failures. Data loading is now handled via new iterator types: \Iter2\ for batching pairs of tensors and \TextData\ for character-level text datasets, both offering configurable batch sizes and device placement. The public API is reorganized into distinct modules (\data\, \tensor\, \wrappers\, \nn\, \vision\), exposing high-level components like \COptimizer\, \CModule\, and \IValue\ while removing the previous \extern\ block and manual memory management.
src · high confidence
New build script for PyTorch 2.13.0 with optional automatic download
The torch-sys crate now includes a new build.rs script that targets PyTorch version 2.13.0. This script introduces a feature-gated automatic download mechanism (enabled by default via the 'download-libtorch' feature) which fetches the appropriate libtorch binaries using the ureq library and extracts them via the zip crate. It supports locating system-wide installations via the LIBTORCH environment variable or Python environments via LIBTORCH\_USE\_PYTORCH=1, and includes version checking to ensure compatibility with the expected PyTorch release.
torch-sys · high confidence
Switch to system-wide libtorch linking and update Python environment
The build script (build.rs) has been rewritten to link against a system-wide libtorch installation by default, removing the previous requirement for the LIBTORCH environment variable and the manual compilation of C++ wrappers via the cc crate. This change aligns with the project's support for system-wide libtorch installations. Additionally, the project now specifies Python 3.12 as the target version via .python-version and introduces a uv.lock file that pins dependencies such as cuda-bindings 13.2.0 and filelock 3.20.3.
(repo-wide) · high confidence
Test coverage
Added test data generation for linear layer validation; Expanded test coverage for core library features.
Dependencies
Library renamed to tch with PyTorch 2.13 bindings and new Python extension support
The Rust PyTorch bindings library has been renamed from 'torchr' to 'tch' and updated to version 0.26.0, aligning with PyTorch 2.13.0 (specified in pyproject.toml). This release introduces a dedicated 'pyo3-tch' crate and a Python extension example, enabling users to manipulate PyTorch tensors from Python extensions via PyO3. The dependency stack has been modernized, including updates to ndarray (0.16.1), rand (0.8), and the adoption of the Rust 2021 edition across the workspace.
(dependencies) · high confidence
Written by watchdog.canine.dev from the codebase's own history, inside the signed delivery this page is composed from.
How this codebase got here
Baseline
- First survey — no prior run to compare against. CAI 74.
Lenses
- Code Health 93
- Architecture 100
- Maturity 67
- Readiness 73
- Security 75
- Domain Modelling 100
Changes since last survey
- 300 commits — 248 feature/other, 52 fixes
By area
- (repo) — 74 commits
- (root) — 57 commits
- src/tensor — 25 commits
- src/wrappers — 21 commits
- src/nn — 16 commits
- torch-sys/build.rs — 16 commits
- src/vision — 14 commits
- third_party/pytorch — 12 commits
- torch-sys/libtch — 12 commits
- examples/python-extension — 11 commits
- examples/stable-diffusion — 6 commits
- examples/llama — 5 commits
- .github/workflows — 4 commits
- torch-sys/Cargo.toml — 4 commits
- examples/reinforcement-learning — 3 commits
- examples/yolo — 3 commits
- tests/tensor_tests.rs — 3 commits
- examples/basics.rs — 2 commits
- tests/var_store.rs — 2 commits
- .cargo/config.toml — 1 commit
Notable commits
- fix: Another fix.
- fix: Clippy fixes for 1.78.
- fix: Clippy fixes.
- fix: Clippy fixes.
- fix: Clippy fixes.
- fix: Clippy fixes.
- fix: Clippy fixes.
- fix: Convert to a c_char rather than an i8, fix for #567.
- fix: Couple fixes.
- fix: Documentation typo fix
- fix: Fix a bunch of clippy lint errors.
- fix: Fix more clippy lints.
- fix: Fix more clippy lints.
- fix: Fix number of element calculation for Vec<T> conversion (#693)
- fix: Fix rustfmt + minor tweaks.
- fix: Fix rustfmt.
- fix: Fix some clippy lint.
- fix: Fix some clippy lint.
- fix: Fix some clippy lints for rust 1.66.
- fix: Fix some clippy lints in the tests.
- …and 280 more
Written by watchdog.canine.dev from the codebase's own history, inside the signed delivery this page is composed from.
Survey your own repository
LaurentMazare/tch-rs was measured the same way every project in this corpus was: the same rubric, at a pinned commit, with the result published in full. Point a surveyor at a repository you know and see whether you agree with it.
About this page
- The score is its most recent published measurement, taken on 13 September 2026 at a pinned commit. It is not a live figure and does not change until the project is measured again.
- Measured at commit 4227b89b72059b0651ff83a38637693574e0a2d6 — the exact code this score is about.
- Scored under rubric-2026.09.10 — the same rubric and the same method as every other entry in this index.
- Measured by watchdog.canine.dev using codehealth-analyzer preprod-7a6aa1c33003.