Learn/Course 14
Transformers · weeks 17–19

Build a transformer from scratch

Domain autocomplete needs a model whose every component the team can explain.

Opening production brief

A measurable problem, before a model.

Users

Operators, product owners, and people affected by the transformer from scratch decision.

Data

tokenization, embeddings, RoPE, QKV projections with version, owner, event time, and availability contracts.

Constraints

Domain autocomplete needs a model whose every component the team can explain. Every solution must state latency, cost, capacity, and fallback limits.

Success

Offline evidence plus a deployable resolution: Tokenizer, attention, RoPE, residual stack, training loop, generation, and KV cache.

CHAPTER 1

Frame and baseline

Turn the production problem into explicit data, metric, and baseline contracts.

  1. 01

    Text to tensors: tokenization, embeddings, position

    Tokenizer and position scheme are not preprocessing trivia; together they define what symbols and order the transformer can represent.

    65–90 min · checkpoint · deterministic lab
  2. 02

    Scaled dot-product self-attention by hand

    Attention is a masked, normalized lookup: queries score keys, softmax allocates influence, and values carry the information.

    65–90 min · checkpoint · deterministic lab
CHAPTER 2

Build and stress

Formalize the machinery, test counterexamples, and expose system limits.

  1. 03

    Heads, norms, residuals, and MLPs

    Attention mixes tokens, the MLP mixes channels, and normalized residual paths let both transformations accumulate without losing the original stream.

    65–90 min · checkpoint · deterministic lab
  2. 04

    Train a causal language model

    Causal LM training is next-token alignment plus a no-looking-ahead proof; everything else scales that contract.

    65–90 min · checkpoint · deterministic lab
CHAPTER 3

Resolve and operate

Join model behavior to architecture, observability, rollout, and rollback.

  1. 05

    Sampling, KV cache, and efficient generation

    Efficient generation couples a declared sampling policy with a position-correct KV cache whose logits match the uncached model.

    65–90 min · checkpoint · deterministic lab
  2. HW

    Decoder-only transformer from scratch

    Specify tokenizer, attention blocks, causal training, unit tests, sampling, and KV-cache parity without a transformer module.

    Rubric · staged hints · reference resolution
Prerequisite guidance

3 concepts make this faster.

Prerequisites are guidance, never hard gates.

C09Neural networksA small network built from forward pass through backpropagation and optimization.C11Sequence modelsA masked sequence model with BPTT, recurrent gates, and attention precursors.C12GPU trainingA profiled, mixed-precision, memory-budgeted training loop.
Ready to work the problem?

Text to tensors: tokenization, embeddings, position

Start lesson 01