Model or dataset
nerdai/llms-from-scratch-rs avatar
nerdai/llms-from-scratch-rs

llms-from-scratch-rs: Raschka's LLM book, translated into Rust with Candle

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

334 stars41 forksRustMIT

At a glance

What is it?
A chapter-by-chapter Rust port of the code in Build a Large Language Model (From Scratch), built on the Candle crate. It is a study companion for readers who want to see GPT internals in Rust, not a training framework.
Who is it for?
Adopt it if you are working through Raschka's book and want the same steps expressed in Rust with Candle, or if you want a reading reference for attention, tokenization and GPT blocks in typed code. Skip it if you need a production training or serving stack, a maintained tokenizer pipeline, or anything with a documented rollback and upgrade path; the README describes no training run at scale and no deployment story.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 4 days ago.
What is it written in?
Mainly Rust, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 15, 2026, and from our analysis. They are not legal advice.

DEEP OPEN-SOURCE ANALYSIS

What llms-from-scratch-rs actually ports, and for whom

Sebastian Raschka's book builds a GPT-style model step by step, and its companion code is PyTorch. This repository restates that code in Rust using Candle, the minimalist ML framework from Hugging Face. The README lists the book's seven chapters, from understanding large language models through text data, attention mechanisms, a GPT model that generates text, pretraining, classification fine-tuning, and instruction fine-tuning, and says the project translates all of the PyTorch code into Rust.

The audience is narrow and that is a feature. If you learn by reading source, and PyTorch's dynamic style leaves the tensor shapes and buffer handling implicit for you, Rust with Candle forces those details into the open: allocations, dtypes, device selection, and error propagation are all visible in the signatures. The README is explicit that this is educational: imports in every example and exercise are placed inside the main method rather than at module level, which it calls non-conventional, so a reader can see exactly which imports a given example needs. That choice will irritate anyone using the crate as a library, and it is deliberate.

What it is not: a library you depend on. The README's recommended usage is cloning the repository and running the examples and exercises through Cargo. There is no published workflow for embedding this in another service, and the Cargo.toml marks the package license as MIT while excluding the data directory from the published artifact.

How the examples and exercises are wired

The repository is organised around two catalogues. Examples map to code listings in the book, numbered like 02.01 and 05.07, and exercises map to the book's practice problems, numbered like 2.1 and 5.5. A single binary dispatches to them: the README shows `cargo run example 05.07` and `cargo run exercise 5.5`, so the first positional argument selects the catalogue and the second selects the entry.

Both catalogues are discoverable from the command line rather than from a directory listing. `cargo run list --examples` prints a table with an Id column and a Description column, and the README pastes a fragment of that output showing entries such as 02.02, described as using Candle to generate an embedding layer, and 02.03, for absolute positional embeddings. The exercises listing shows an Id column and a Statement column, and the README's excerpt includes exercise 2.1 on byte pair encoding of unknown words with the tiktoken library, and exercise 6.3 on fine-tuning the first versus the last token.

The dependencies tell you what the mechanism rests on. Candle supplies the tensor and neural network primitives, tiktoken-rs and tokenizers cover tokenization, hf-hub and reqwest handle fetching, polars and plotly appear to support the data and plotting examples, and ndarray is present alongside Candle's own tensor type. Candle is pulled from a git URL at version 0.9.1 rather than from crates.io, which matters for reproducible builds.

Installing it and running a first example

There is no package to install. The README's recommended path is to clone the repository and use Cargo. Either the SSH or HTTPS form works; the HTTPS form is the one that needs no key setup.

bash
git clone https://github.com/nerdai/llms-from-scratch-rs.git
cd llms-from-scratch-rs

The examples expect the book's dataset to be present locally. The README gives a command that creates a data directory and downloads the-verdict.txt into it, which is the text used by the early chapter examples.

bash
mkdir -p 'data/'
wget 'https://raw.githubusercontent.com/rabst/LLMs-from-scratch/main/ch02/01_main-chapter-code/the-verdict.txt' -O 'data/the-verdict.txt'

Before running anything, list what is available. This prints a table of example IDs and their descriptions, so you can confirm the build works and pick a target.

bash
cargo run list --examples

The README's own example invocation runs listing 05.07. Expect a compile of the full dependency graph on first run, including Candle from git, which is the slowest part of the process.

bash
cargo run example 05.07

If you have a CUDA-capable device, the README documents a cuda feature that you enable on the command line. The same flag applies to exercises.

bash
cargo run --features cuda example 05.07
cargo run --features cuda exercise 5.5

For exercises, swap the subcommand and use the exercise ID from `cargo run list --exercises`, for example `cargo run exercise 5.5`. The README notes that the project also publishes API documentation on docs.rs, so you can read the modules in a browser instead of an IDE.

Where this repository stops being the right tool

The most concrete limitation is that this is a translation of teaching code. The book's model is small by design, sized so a reader can train it on a single machine within a chapter. Nothing in the README describes distributed training, checkpoint resumption, gradient accumulation policies, or a serving path, and the Makefile's targets are formatting, linting, checking and testing, not training pipelines. If your goal is to pretrain or fine-tune a model you intend to deploy, this repository gives you the concepts and none of the operational machinery.

A second constraint is dependency drift. Candle is declared from a git URL at version 0.9.1, and Candle's API has moved over time. A git dependency with a version requirement is not the same as a locked release, so a build months from now may resolve differently than the one the author used. If you pin Cargo.lock, you inherit the author's resolution; if you do not, you own the breakage.

The import style is a third friction point. Imports inside main are helpful when reading alongside the book and unhelpful when you want to lift a function into your own crate, because you have to relocate every use statement by hand. The README states this is intentional for educational purposes, so treat it as a design decision rather than an oversight, but plan for the extra work.

Finally, the README does not document rollback, versioning policy, or what changes between releases. The release history shows v0.1.3 in January 2025, v0.1.4 in February 2025, and v0.1.5 in June 2025, with the package version in Cargo.toml matching 0.1.5. The repository's last push was on 2026-09-14, so activity continues, but the README offers no compatibility statement for the 0.x line.

The PyTorch original, and what changes when you switch

The obvious alternative is the source of truth: Raschka's own LLMs-from-scratch repository, which the README links and which holds the PyTorch code the book is built around. The difference is not cosmetic. In PyTorch you get eager execution, a large ecosystem of pretrained weights, and error messages that point at the offending line of Python. You also get the book's text matching the code line for line, which this project cannot promise because it is a translation.

In the Rust version, the trade runs the other way. Types constrain what a function can do to a tensor, ownership makes buffer reuse explicit, and Candle's API is small enough to read end to end. For someone who has bounced off PyTorch's implicit broadcasting, seeing the same attention computation written with explicit shapes and dtypes can be the thing that makes it click. The cost is that you cannot copy a snippet from the book and expect it to compile, and you cannot fall back on the wider PyTorch ecosystem when an example needs a pretrained checkpoint.

A second alternative, if your interest is Rust rather than this specific book, is to use Candle directly and follow its own examples. That skips the chapter numbering and the book's narrative, but it also skips the educational import placement and gives you a framework whose documentation is maintained as a framework rather than as a teaching aid.

Maintenance, licence and the cost of upgrading

The repository is not archived, and its last push was on 2026-09-14. That is recent enough that the project is not abandoned, but the README does not describe a release cadence, a deprecation policy, or a migration guide between versions. The three releases on record are v0.1.3, v0.1.4 and v0.1.5, and the Cargo.toml version matches v0.1.5, so versioning is at least consistent between the manifest and the tags.

Upgrade cost is dominated by the Candle dependency, not by this repository's own code. Because Candle is referenced through a git URL, a fresh build after a Candle change can fail in ways the project's own commits did not cause. Committing Cargo.lock is the cheap defence, and the repository does include one at the top level. If you fork and update, expect to spend your time on Candle API changes rather than on the chapter examples.

The licence is MIT, stated in Cargo.toml and present as a LICENSE file at the repository root. MIT is permissive, so reuse in closed work is generally permitted, but the book's text and the datasets are separate works with their own terms, and the README points at the dataset hosted in the Python repository rather than shipping it here. The Cargo.toml excludes data from the published package, which reflects that. This is a description of what the repository states, not legal advice; if you plan to redistribute the book's text or data, check their terms separately.

Editorial conclusion

Adopt it if you are working through Raschka's book and want the same steps expressed in Rust with Candle, or if you want a reading reference for attention, tokenization and GPT blocks in typed code. Skip it if you need a production training or serving stack, a maintained tokenizer pipeline, or anything with a documented rollback and upgrade path; the README describes no training run at scale and no deployment story. Before committing time, verify three things yourself: that the pinned Candle git dependency resolves on your toolchain, that `cargo run list --examples` prints the table shown in the README, and that the example IDs you care about actually appear in that listing rather than in the book's Python repository.

Frequently asked questions

How are LLMs built from scratch?

The README frames the process through the seven chapters of Raschka's book: understanding large language models, working with text data, coding attention mechanisms, implementing a GPT model that generates text, pretraining on unlabeled data, fine-tuning for classification, and fine-tuning to follow instructions. This repository expresses each of those steps in Rust with Candle rather than PyTorch.

How to learn LLMs from scratch?

The README's recommended path is to clone the repository, download the-verdict.txt into a data directory, and work through the examples and exercises with Cargo, reading each one alongside the corresponding book chapter. It notes that imports are deliberately placed inside main so a reader can see exactly what each example needs.

How do I build an LLM from scratch?

In this project you build it by running the numbered examples and exercises rather than by calling a library. `cargo run list --examples` shows the available listings, and `cargo run example 05.07` runs one of them, with a cuda feature available for CUDA-capable devices.

Official sources

  1. License: MIT
  2. nerdai/llms-from-scratch-rs on GitHub
  3. Project website
  4. README
  5. Releases
Community notes

Community notes