Session 9 - Recap of the core ideas
Session 9
From tokens to a defensible system: the eight sessions in one hour
Today: one part per session, the few ideas to keep, and a self-test at the end.
Session 1 - a model reads tokens and predicts the next one
Decoder-only: each token sees only the tokens before it. Training scores all positions in parallel; generation repeats this loop one token at a time. The weights hold the knowledge, the context window is the working memory.
The tokenizer fixes the sequence length before the model runs, and you pay per token: about 4 characters per token in English, 6.6× more tokens for Russian on an English-centric tokenizer.
\[\text{Attention}(Q,K,V) = \text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V\]
Future scores are set to \(-\infty\) before the softmax, so their weights are exactly 0. Without it, training leaks the answer and generation produces garbage.
Session 2 - how a model is trained and what compute buys
\[L = -\frac{1}{T}\sum_{t} \log P(x_t \mid x_{<t}) \qquad \text{perplexity} = e^{L}\]
Start from bytes, repeatedly merge the most frequent adjacent pair, stop at the target vocabulary size \(V\). The tokenizer is frozen before training.
Training FLOPs ≈ 6 × parameters × tokens (2 forward + 4 backward per parameter per token); serving costs about \(2N\) FLOPs per generated token.
Session 3 - from a text completer to an assistant
SFT imitates demonstrations, then a reward model and RL optimise preferences; DPO collapses the second part into one step.
Same cross-entropy as pretraining, scored on answer tokens only (prompt labels set to -100), including the end-of-turn token. SFT teaches format and style, not reliable new facts; safety trained this way is shallow.
The reward model is a proxy: push it too far and true quality drops (Goodhart’s law).
DPO turns the KL-regularised optimum into a classification loss on log-probability ratios: no reward model, no sampling, loss \(= \ln 2\) at step 0. It is stable and simple, but offline. RL with verifiable rewards (maths, code) and GRPO go beyond it.
Freeze \(W_0\), learn \(\Delta W = BA\) with rank \(r\) (8 to 64). Rank 8 on a 4096 × 4096 matrix adds 65,536 parameters. LoRA learns less and forgets less than full fine-tuning.
Session 4 - reliable, structured, safe outputs
Stable text first so it can be cached; long documents first and the question last; untrusted data inside tags. Prompts are code: judge them on a held-out set.
The server masks tokens that would break the schema before sampling, so the output is valid by construction: schema adherence goes from under 40% to 100%. Valid is not correct.
Instructions and data share one token stream, so injection can only be reduced and measured (attack success rate). Layers: separate, constrain, validate, limit the blast radius. Untrusted input + sensitive data + external actions together can leak data: remove one leg.
Session 5 - ground answers in your own documents
Offline: parse, chunk (300 to 800 tokens, 10 to 20% overlap), embed, index. Online: query, retrieve, rerank, generate. Update the index, not the weights.
BM25 wins on rare words (codes, acronyms), dense embeddings win on meaning. Fuse by rank: score = Σ 1 / (60 + rank), so score scales never need to match.
A bi-encoder encodes query and chunk separately (fast, coarse); a cross-encoder reads them together (precise, slow) and only sees the shortlist. Often the largest single quality gain.
Session 6 - let the model act
The model writes a thought and a structured tool call; your code runs it and appends the observation; repeat until final_answer. Tool errors go back as observations, they never crash the run.
Pick the lowest rung that solves the task, and climb only when an evaluation shows a gain.
Per-step reliability \(p\) over \(n\) steps gives \(p^n\): 0.95 over 20 steps is about 36%. Five stop conditions: goal reached, step limit, budget, repeated call, needs a human. Always report why it stopped.
Session 7 - generate and read images
Forward: add Gaussian noise step by step (\(T = 1000\)). Training: predict the added noise, \(\|\epsilon - \epsilon_\theta(x_t, t, c)\|^2\). Reverse: start from noise and denoise. Latent diffusion runs this on a VAE-compressed image, 8× smaller per side.
CFG pushes away from “any image” and towards “this prompt”. Past a sweet spot, too much guidance burns the image.
The image is cut into patches, embedded by a vision encoder, projected, and spliced into the token sequence: the LLM never sees pixels. The loss is still next-token prediction. Cost grows with the number of patches.
Session 8 - from “the demo looks great” to “we can defend this”
Cheap deterministic checks run always, costly ones (LLM judge, human) run rarely: climb one level only when the cheaper one cannot answer.
Each stage is something you learned to build, and each stage needs its own metric.
✅ Answers
✅ Answers
Albert School - Generative AI I, course wrap-up