Index/rustlab_llm — Lesson Site

rustlab_llm — Lesson Site

A self-contained tutorial series for building a GPT-style language model from first principles, using rustlab as the scripting and visualisation environment. Each lesson is a single executable Markdown notebook — derivations, code, and plots in one file.

What you see here is the rendered output of the source notebooks: prose, executed rustlab blocks, captured print() output, and inline SVG plots. GitHub displays it directly.

The source notebooks live at ../notebooks/<slug>.md. This book/ tree is regenerated by make notebooks from the repo root; only book/README.md (this file) is hand-written.

Reading order

Each lesson depends on the ones before it, and each closes with an Engineering Lenses section (Signals · Systems · Information) whose claims are labelled Exact, Model, or Analogy and backed by executed code. See ../PLAN.md for phase status and ../docs/proposal-2026-09-27-ee-controls-it-revision.md for the design of the revision.

Lesson 00 — Orientation

#LessonFocus
00The LLM as a Systemthe signal-flow map, the three lenses, notation, your toolkit mapped to lessons

Phase 1 — Foundations

#LessonFocus
01Tokens & Text Encodingcharacter vocabulary, integer indices, one-hot encoding
02Probability & Softmaxdiscrete distributions, softmax, temperature scaling
03Cross-Entropy Losscomparing predicted vs. true distributions; MLE connection
04Embeddings & Similaritydense vectors, cosine similarity, geometry of meaning

Phase 2 — First Working Model

#LessonFocus
05The Bigram Language Modelcount-based next-token prediction; train/sample loop
06Linear Layers & Gradient Descent\(y = Wx + b\); loss landscape; gradient step

Phase 3 — Attention

#LessonFocus
07Context & Naive Averagingbag-of-tokens averaging; the causal mixing matrix
08Scaled Dot-Product Attention\(Q, K, V\); attention scores; causal masking
09Multi-Head Attentionparallel heads, concatenation, output projection

Phase 4 — Transformer Components

#LessonFocus
10Positional Encodingsinusoidal encoding; why attention is order-blind
11Feed-Forward Blockposition-wise MLP; GELU
12LayerNorm & Residualsnormalisation; the residual stream

Phase 5 — Full GPT Architecture

#LessonFocus
13The Transformer BlockPre-LN block: MHA → residual → FFN → residual
14Full GPT Architectureembeddings + N blocks + LM head; parameter count

Phase 6 — Training

#LessonFocus
15Backpropagationchain rule through attention and MLP; gradient flow
16AdamW Optimizermomentum, adaptive steps, decoupled weight decay
17Learning-Rate Schedulingwarmup + cosine decay
18The Training Loopforward → loss → backward → step; diagnostics

Phase 7 — Tokenisation & Evaluation

#LessonFocus
19Byte Pair Encodingiterative merges; subword vocabulary
20Perplexity & EvaluationPPL = exp(loss); train/val; overfitting

Phase 8 — Generation, Backprop, Capstone

#LessonFocus
21Sampling and Generationautoregressive loop; greedy / temperature / top-K / top-P; KV cache
22Full Backprop Through the Blockthe complete analytical backward pass; gradient check; the shared library
23Putting It All Togetherchar → BPE → train the full transformer → sample at checkpoints

Phases 9–10 — Extensions

#LessonFocus
24Modern Architectural VariantsRoPE, RMSNorm, SwiGLU, GQA
25Fine-Tuning — SFT and DPOloss-masked SFT and catastrophic forgetting; DPO with a frozen reference
26Quantization and Fixed-Point Inferenceb-bit weights, 6 dB per bit, perplexity vs bits, KV-cache memory and the bandwidth bound

Local interactive view

For Plotly hover/zoom/pan and KaTeX-rendered math, run make html from the repo root and open index.html (right next to this file) in a browser. The HTML build includes an auto-generated entry page with prev/next navigation between notebooks. The index.html and per-notebook *.html files are gitignored — regenerate them locally any time.

How this is organized

The pattern (sources flat in notebooks/, rendered output committed to a top-level book/, standalone .rlab scripts in lessons/<slug>/, shared code in lib/) is documented at ../rustlab/docs/lesson-site-pattern.md and shared across the rustlab course family (rustlab_em uses the same layout).