nerdai/llms-from-scratch-rs

A comprehensive Rust translation of the code from Sebastian Raschka's Build an LLM from Scratch book.

What it solves

Sebastian Raschka's book Build An LLM From Scratch teaches how to build a GPT-style large language model step by step, but all the code is written in PyTorch (Python). This project solves the problem for Rust developers: it translates the entire book's code into Rust, so you can follow the same tutorial and learn LLM internals without using Python at all.

How it works

The project mirrors the book's 7 chapters, covering topics like text data handling, attention mechanisms, implementing a GPT model, pretraining on unlabeled data, fine-tuning for classification, and fine-tuning to follow instructions. It uses the Candle crate (a minimalist ML framework from Hugging Face) to write equivalent Rust code. The repo provides runnable "examples" and "exercises" for each chapter. You clone the repository, download the same dataset used in the book (the-verdict.txt), and then run specific examples or exercises with cargo commands like cargo run example 05.07 or cargo run exercise 5.5. You can also list all available items with cargo run list --examples or cargo run list --exercises. The code is structured so that each example/exercise shows exactly which imports are needed, which is helpful for learning. If you have a CUDA-enabled GPU, you can enable the cuda feature to speed up computation.

Who it's for

This is for Rust developers who want to understand how LLMs like GPT work under the hood by following a proven, step-by-step tutorial. It's also useful for readers of the book who want to see Rust implementations, and for anyone interested in a non-Python approach to building a transformer-based LLM.

Highlights

  • Complete Rust translation of the book's chapters, all built with the Candle ML framework.
  • Runnable examples and exercises that mirror the book, so you can practice the key concepts.
  • Uses the exact same dataset as the book, making it a drop-in companion.
  • Supports CUDA for accelerated execution on compatible GPUs.
  • Published on crates.io, so you can install it with cargo install and run it anywhere.

Related

  • Project
  • Project
  • Project
  • Project