RustingBrain
A deep-learning library in Rust, from a two-layer XOR network up to a 300M-parameter transformer language model trained on one desktop GPU.
No Python in the loop, no C++ build step, no framework runtime. The CUDA kernels are compiled at startup by NVRTC and the rest is Rust.
Speed
Training tokens per second for the same 101.7M-parameter transformer, mixed
precision on every side, torch.compile for PyTorch and XLA for TensorFlow, on
one idle RTX 3060 12 GB:
| batch × seq | RustingBrain | TensorFlow | PyTorch |
|---|---|---|---|
| 1 × 512 | 13071 | 15560 | 9607 |
| 1 × 1024 | 16352 | 15652 | 12235 |
| 4 × 512 | 22045 | 22412 | 17993 |
| 4 × 1024 | 21787 | 20063 | 20544 |
| 8 × 512 | 22926 | 23906 | 21366 |
Single GPU, one architecture family. Other shapes are in docs/baseline.md.
Features
- Language models: decoder-only transformers with grouped-query attention, rotary positions, RMSNorm, SwiGLU, flash attention and mixture-of-experts layers.
- Training: mixed precision, gradient accumulation, clipping, schedules, LoRA, resumable checkpoints, corpora larger than memory.
- Inference: KV-cached and streaming generation, samplers, int8 weights.
- Images and video: FLUX, FLUX.2, Stable Diffusion 1.x to 3 and Wan 2.2, loaded from the published safetensors directory.
- Dense networks: regression and classification with CSV,
.npy, IDX and image-folder loaders. - Backends: CPU, CUDA, and Metal for dense networks.
- Interop: ONNX import and export, Hugging Face tokenizers and checkpoints.
Install
The crate builds and tests with no GPU present. Driver and toolkit setup is in
INSTALL_CUDA.md. Other features: onnx, images, metal.
A language model
use ;
A dense network
let mut model = builder
.input_size
.dense
.dense
.loss
.optimizer
.build;
model.fit?;
println!;
Text to image
let mut pipeline = load?;
let image = pipeline.generate?;
save_png?;
Learn more
The tutorials are an eighteen-chapter course with runnable code for each chapter.
| Topic | Chapter |
|---|---|
| Loading and preparing data | 4 |
| Training loop, evaluation, saving | 7, 8, 9 |
| CUDA | 11 |
| ONNX import and export | 12 |
| Tokenizers, generation, sampling | 14 |
| Mixture of experts | 15 |
| Long runs, resuming, instruction tuning | 16 |
| LoRA | 17 |
| Text to image | 18 |
Examples
The rest are in examples/.
Scope
One machine, one GPU. Encoder-decoder models, multi-GPU and distributed training are not implemented and are not planned.
License
RustingBrain License 1.1: use, modify and distribute it, and sell projects built with it, but do not sell RustingBrain itself. See LICENSE.md.