llms-from-scratch-rs

Library Listed

Rust (candle) code for Build a LLM From Scratch by Sebastian Raschka

Checked · Sources

Downloads
3
Dependents
0
Stars
340
Forks
43
Contributors
4
Commits
511
Latest release
0.1.5 ·

About

From the project README ·

LLMs from scratch - Rust

This project aims to provide Rust code that follows the incredible text, Build An LLM From Scratch by Sebastian Raschka. The book provides arguably the most clearest step by step walkthrough for building a GPT-style LLM. Listed below are the titles for each of the 7 Chapters of the book.

  1. Understanding large language models
  2. Working with text data
  3. Coding attention mechanisms
  4. Implementing a GPT model from scratch to generate text
  5. Pretraining an unlabeled data
  6. Fine-tuning for classification
  7. Fine-tuning to follow instructions

The code (see associated github repo) provided in the book is all written in PyTorch (understandably so). In this project, we translate all of the PyTorch code into Rust code by using the Candle crate, which is a minimalist ML Framework.

Usage

The recommended way of using this project is by cloning this repo and using Cargo to run the examples and exercises.

# SSH
git clone git@github.com:nerdai/llms-from-scratch-rs.git

# HTTPS
git clone https://github.com/nerdai/llms-from-scratch-rs.git
Read the full README

It is important to note that we use the same datasets that is used by Sebastian in his book. Use the command below to download the data in a subfolder called data/ which will eventually be used by the examples and exercises of the book.

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'
Navigating the code

Users have the option of reading the code via their chosen IDE and the cloned repo, or by using the project's docs.

NOTE: The import style used in all of the examples and exercises modules are not by convention. Specifically, relevant imports are made under the main() method of every Example and Exercise implementation. This is done for educational purposes to assist the reader of the book in knowing precisely what imports are needed for the example/exercise at hand.

Running Examples and Exercises

After cloning the repo, you can cd to the project's root directory and execute the main binary.

# Run code for Example 05.07
cargo run example 05.07

# Run code for Exercise 5.5
cargo run exercise 5.5

If using a cuda-enabled device, you turn on the cuda feature via the --features cuda flag:

# Run code for Example 05.07
cargo run --features cuda example 05.07

# Run code for Exercise 5.5
cargo run --features cuda exercise 5.5
Listing Examples

To list the Examples, use the following command:

cargo run list --examples

A snippet of the output is pasted below.

EXAMPLES:
+-------+----------------------------------------------------------------------+
| Id    | Description                                                          |
+==============================================================================+
| 02.01 | Example usage of `listings::ch02::sample_read_text`                  |
|-------+----------------------------------------------------------------------|
| 02.02 | Use candle to generate an Embedding Layer.                           |
|-------+----------------------------------------------------------------------|
| 02.03 | Create absolute postiional embeddings.                               |
|-------+----------------------------------------------------------------------|
| 03.01 | Computing attention scores as a dot product.                         |
...
|-------+----------------------------------------------------------------------|
| 06.13 | Example usage of `train_classifier_simple` and `plot_values`         |
|       | function.                                                            |
|-------+----------------------------------------------------------------------|
| 06.14 | Loading fine-tuned model and calculate performance on whole train,   |
|       | val and test sets.                                                   |
|-------+----------------------------------------------------------------------|
| 06.15 | Example usage of `classify_review`.                                  |
+-------+----------------------------------------------------------------------+
Listing Exercises

One can similarly list the Exercises using:

cargo run list --exercises
# first few lines of output
EXERCISES:
+-----+------------------------------------------------------------------------+
| Id  | Statement                                                              |
+==============================================================================+
| 2.1 | Byte pair encoding of unknown words                                    |
|     |                                                                        |
|     | Try the BPE tokenizer from the tiktoken library on the unknown words   |
|     | 'Akwirw ier' and print the individual token IDs. Then, call the decode |
|     | function on each of the resulting integers in this list to reproduce   |
|     | the mapping shown in figure 2.11. Lastly, call the decode method on    |
|     | the token IDs to check whether it can reconstruct the original input,  |
|     | 'Akwirw ier.'                                                          |
|-----+------------------------------------------------------------------------|
| 2.2 | Data loaders with different strides and context sizes                  |
|     |                                                                        |
|     | To develop more intuition for how the data loader works, try to run it |
|     | with different settings such as `max_length=2` and `stride=2`, and     |
|     | `max_length=8` and `stride=2`.                                         |
|-----+------------------------------------------------------------------------|
...
|-----+------------------------------------------------------------------------|
| 6.2 | Fine-tuning the whole model                                            |
|     |                                                                        |
|     | Instead of fine-tuning just the final transformer block, fine-tune the |
|     | entire model and assess the effect on predictive performance.          |
|-----+------------------------------------------------------------------------|
| 6.3 | Fine-tuning the first vs. last token                                   |
|     |                                                                        |
|     | Try fine-tuning the first output token. Notice the changes in          |
|     | predictive performance compared to fine-tuning the last output token.  |
+-----+------------------------------------------------------------------------+

[Alternative Usage] Installing from crates.io

Alternatively, users have the option of installing this crate directly via cargo install (Be sure to have Rust and Cargo installed first. See here for installation instructions.):

cargo install llms-from-scratch-rs

Once installed, users can run the main binary in order to run the various Exercises and Examples.

# Run code for Example 05.07
cargo run example 05.07

# Run code for Exercise 5.5
cargo run exercsise 5.5
Runs on
CUDA
What shows it
  • CUDA: llms-from-scratch-rs feature cuda
Native code
None found
  • No native code found
Python bindings
No Python binding crate found
License
MIT

Compare with similar projects

Order: strongest tier first, then most downloads, then name, with unmeasured values last. A library counts downloads over 30 days. An app counts binary downloads per release. An app with none counts its crate downloads over 30 days. ML Rust runs this catalogue, so its closest project always gets a row.

llms-from-scratch-rs beside the projects that share a topic with it
ProjectDownloadsDependentsStarsLatest releaseRuns on (GPU)Native codeLicense
llms-from-scratch-rs This page Listed

Rust (candle) code for Build a LLM From Scratch by Sebastian Raschka

3 downloads over 30 days 0 340 0.1.5 CUDA No native code found MIT
tch-rs Featured

Rust wrappers for the PyTorch C++ api (libtorch).

Python bindings

1,190,464 downloads over 30 days 124 5,518 0.26.0 CUDA Binds native code: tch, torch-sys. Build uses a native toolchain: cc Apache-2.0 OR MIT
mlx-rs Featured

Unofficial rust wrapper for Apple's mlx machine learning library.

33,398 downloads over 30 days 36 389 0.34.0 Metal Binds native code: mach-sys, mlx, mlx-sys. Build uses a native toolchain: bindgen, cc, cmake Apache-2.0 OR MIT
aprender Listed

Next-generation ML framework in pure Rust — `cargo install aprender` for the `apr` CLI

15,442 downloads over 30 days 31 140 0.72.3 CUDA, WebGPU Binds native code: libsqlite3-sys, zstd-sys, tree-sitter (feature full), alsa-sys (feature audio-alsa), nvml-wrapper-sys (feature nvml) and 1 more. Build uses a native toolchain: bindgen (feature bindgen) MIT
mpsgraph-rs Listed Dormant

Modern Rust bindings for Apple's Metal Performance Shaders Graph framework

398 downloads over 30 days 2 Not measured 0.2.0 Metal No native code found MIT
oxidizr App Listed

Trains GPT-style language models from a YAML config and a tokenized dataset: dense GQA transformers, Mamba 1, 2 and 3, MLA and MoE layers on CPU or CUDA. Runs the training loop, optimizer, checkpoints, evaluation and metrics on boostr, and builds speech corpora through voxr.

27 downloads over 30 days Not applicable 8 0.1.1 CUDA Binds native code: nvml-wrapper-sys (feature cuda) Apache-2.0
RustTensor App Listed Dormant

A learning-focused, high-performance tensor computation library built from scratch in Rust, featuring automatic differentiation and CPU/CUDA backends.

Not measured Not applicable 79 Not measured Not measured Not measured MIT
Why ListedNo archive, New or Featured rule places it, so it is Listed.

What it lacks for Featured

  • It has 0 active dependents and 0 new dependents in 365 days. Featured needs 11 active dependents or 9 new dependents in 365 days. The repository route reads 340 stars, 43 forks and 4 contributors. It needs 4,539 stars, 434 forks and 79 contributors together.

What else the check found

  • The last activity is on 29 Jul 2026, 103 days ago. The rules expect an update within 450 days. Dormancy rule

Exact rules: Listed, Featured library. The check on applied thresholds version 9.

Every rule outcome, as the check records it
  • expected-interval: version 0.1.5 base 450: E = 450 days
  • dormancy: last activity 2026-07-29, 103 days ago, E = 450: active
  • featured: not featured: active_users 0 < 11 and new_adopters 0 < 9 and repository route stars 340 (needs 4539), forks 43 (needs 434), contributors 4 (needs 79)
  • listed: listed: no archive, new or featured rule applies

Measurements and sources

Each signal row shows the raw value, its source and the fetch date. The Sources list gathers every distinct source on this page. Movement needs more history.

Sources6 sources
Adoption0 active dependents, 0 new dependents in 365 days1 not measured
Adoption measurements
SignalValueSourceFetchedCounts
Active dependents0crates.io database dumpYes
New dependents (365 days)0crates.io database dumpYes
Peak active dependents0crates.io database dumpYes
Active dependents as a share of the peak1crates.io database dumpYes
Binary downloads per releaseNot measuredYes
MaintenanceLatest human commit: 3 months ago3 not measured
Maintenance measurements
SignalValueSourceFetchedCounts
Latest human commit2026-07-29GitHub repositoryYes
Latest release2025-06-05crates.io database dumpYes
First release2024-12-13crates.io database dumpYes
Latest version0.1.5crates.io database dumpYes
Days the oldest outside issue waits for a reply0GitHub repositoryYes
Maintainer replies over 365 days4GitHub repositoryYes
Repository archivedfalseGitHub repositoryYes
Repository gonefalseGitHub repositoryYes
Release commit or manifest in the repotruecrates.io packageYes
Published docstruecrates.io database dumpYes
Latest release fails cargo checkNot measuredYes
Build check dateNot measuredYes
Build check toolchainNot measuredYes
SecurityUnpatched vulnerabilities: none
Security measurements
SignalValueSourceFetchedCounts
Unpatched vulnerabilities past the grace windownoneRustSec advisory databaseYes
RustSec unmaintained advisorynoneRustSec advisory databaseYes
Deprecation noticenonecrates.io database dumpYes
Deprecated requirementnonecrates.io database dumpYes
Distribution11 releases, 3 downloads over 30 days5 not measured
Distribution measurements
SignalValueSourceFetchedCounts
Releases11crates.io database dumpYes
Downloads over 30 days3crates.io database dumpYes
Binary downloads over the last 3 releasesNot measuredYes
Homebrew installs on request over 365 daysNot measuredYes
Repology repository familiesNot measuredYes
Docker pull growth over 90 daysNot measuredYes
Docker pulls at the growth baselineNot measuredYes
Every version a prereleasefalsecrates.io database dumpYes
Every version 0.0.xfalsecrates.io database dumpYes
Every version yankedfalsecrates.io database dumpYes
Crate deleted from crates.iofalsecrates.io database dumpYes
Repository340 stars, 43 forks, 4 contributors, 511 commits. Stars alone never qualify a project.
Repository measurements
SignalValueSourceFetchedCounts
GitHub stars340GitHub repositoryYes — one part of the repository route
GitHub forks43GitHub repositoryYes — one part of the repository route
Contributors with a GitHub account4GitHub contributorsYes — one part of the repository route
Commits on the default branch511GitHub repositoryNo
Repository's main crate left out of the entrynonecrates.io database dumpYes
Facts that never count
Facts that never count measurements
SignalValueSourceFetchedCounts
LicenseMITcrates.io database dumpNo
Runs on, from listed crates and their direct dependenciesCUDAcrates.io database dumpNo
What shows each backendCUDA: llms-from-scratch-rs feature cudacrates.io database dumpNo
Native code, from listed crates and their dependenciesNo native code foundcrates.io database dumpNo
Python bindings, from direct dependencies of listed cratesNo Python binding crate foundcrates.io database dumpNo
Crates that depend on a listed crate directly0crates.io database dumpNo

Corrections and review history

Report a correction

No correction applies to this entry.

Judgments

A judgment enters only through an approved issue and a merged pull request.

No judgment applies to this entry.