Domain

Attention & Transformers

The transformer architecture: tokenization, self-attention, positional encodings and transformer blocks, with calculations of attention and its memory cost at long context.

10 notebooks10 demos
Preview the recommended route
First notebookScaled Dot-Product Attention & Transformer Layers

Start here. Make one prediction, then note what carries over to the next notebook.

q_ik_jsoftmaxΣ α_ij v_j
Open first notebook
QuestionWhat carries over from Scaled Dot-Product Attention & Transformer Layers into Positional Encoding?
PredictionBefore opening the first demo, predict which quantity changes first.
Demos10 interactive demos in this domain
Before moving onSay in one sentence how the idea works before going to the next notebook.
LearnerWhich notebook should I read first?

Read the first notebook, make one prediction in its demo, then go on to the next.

Open the first notebook

Recommended route

Read these in order: each notebook comes after the notebooks in this domain that it builds on, and the label beside it gives its level. A prerequisite with no page here yet is marked as outside reading. A notebook is left out of the route only when a prerequisite has neither a page here nor a named outside reading; the full list below marks it.

  1. 01
    Scaled Dot-Product Attention & Transformer Layers

    How a query scores keys with scaled dot products, turns the scores into softmax weights and mixes the values; with masks, several queries at once and the transformer layer around them.

    Intermediate18 mincodedemoafter Maximum Likelihood, Dot Product
  2. 02
    Positional Encoding

    How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.

    Introductory12 mincodedemoafter Scaled Dot-Product Attention & Transformer Layers

    Positional Encoding builds directly on Scaled Dot-Product Attention & Transformer Layers.

  3. 03
    Residual Connections & Skip Connections

    A residual connection adds a layer's output to its input, giving the input a direct path through the network while the layer learns an update.

    Intermediate14 mincodedemoafter Scaled Dot-Product Attention & Transformer Layers
  4. 04
    Rotary Position Embeddings (RoPE)

    A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.

    Intermediate14 mincodedemoafter Scaled Dot-Product Attention & Transformer Layers
  5. 05
    Layer Normalization & RMSNorm

    Normalizing each token's vector across its features: LayerNorm subtracts the mean and divides by the standard deviation; RMSNorm only divides by the root mean square.

    Intermediate24 mincodedemoafter Scaled Dot-Product Attention & Transformer Layers, Residual Connections & Skip Connections
  6. 06
    Tokenization & Vocabulary Design

    How text becomes token IDs: segmentation, BPE/unigram tokenizers, and the tradeoffs that shape cost and capability.

    Intermediate14 mincodedemoafter Maximum Likelihood, Representation Learning & Embedding Geometry
  7. 07
    SwiGLU: Gated MLP Blocks in Transformers

    Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.

    Advanced16 mincodedemoafter Scaled Dot-Product Attention & Transformer Layers, Linear Transformations (outside reading)
  8. 08
    Efficient Attention at Scale: KV Cache, GQA & FlashAttention

    How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.

    Advanced26 mincodedemoafter Scaled Dot-Product Attention & Transformer Layers, Rotary Position Embeddings (RoPE), Efficiency: Quantization, Distillation, LoRA & Sparse MoE
  9. 09
    Long Context Engineering: RoPE Scaling, KV Compression & Memory Optimization

    How models handle long inputs: keeping positions within the range seen in training (RoPE scaling), sliding windows, and cutting KV-cache memory with GQA, paging, quantization and compression.

    Advanced22 mincodedemoafter Scaled Dot-Product Attention & Transformer Layers, Rotary Position Embeddings (RoPE), Efficient Attention at Scale: KV Cache, GQA & FlashAttention

    Long Context Engineering builds directly on Efficient Attention at Scale.

  10. 10
    FlashAttention: IO-Aware Attention

    Exact attention computed tile by tile with a running softmax, so the full T × T matrix is never written to GPU main memory; in the paper's benchmarks the reduced memory traffic made attention faster.

    Advanced30 mincodedemoafter Efficient Attention at Scale: KV Cache, GQA & FlashAttention, Long Context Engineering: RoPE Scaling, KV Compression & Memory Optimization

    FlashAttention builds directly on Long Context Engineering.

All notebooks

Every published notebook in this domain.

Scaled Dot-Product Attention & Transformer Layers

How a query scores keys with scaled dot products, turns the scores into softmax weights and mixes the values; with masks, several queries at once and the transformer layer around them.

Intermediate18 mindemo

Positional Encoding

How transformers represent order: sinusoidal encodings, learned embeddings, and relative-position methods like RoPE.

Introductory12 mindemo

Residual Connections & Skip Connections

A residual connection adds a layer's output to its input, giving the input a direct path through the network while the layer learns an update.

Intermediate14 mindemo

Rotary Position Embeddings (RoPE)

A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.

Intermediate14 mindemo

Layer Normalization & RMSNorm

Normalizing each token's vector across its features: LayerNorm subtracts the mean and divides by the standard deviation; RMSNorm only divides by the root mean square.

Intermediate24 mindemo

Tokenization & Vocabulary Design

How text becomes token IDs: segmentation, BPE/unigram tokenizers, and the tradeoffs that shape cost and capability.

Intermediate14 mindemo

SwiGLU: Gated MLP Blocks in Transformers

Why modern transformer MLPs often use a learned multiplicative gate: one projection proposes a token-local write while another projection controls how much of each channel reaches the residual stream.

Advanced16 mindemo

Efficient Attention at Scale: KV Cache, GQA & FlashAttention

How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.

Advanced26 mindemo

Long Context Engineering: RoPE Scaling, KV Compression & Memory Optimization

How models handle long inputs: keeping positions within the range seen in training (RoPE scaling), sliding windows, and cutting KV-cache memory with GQA, paging, quantization and compression.

Advanced22 mindemo

FlashAttention: IO-Aware Attention

Exact attention computed tile by tile with a running softmax, so the full T × T matrix is never written to GPU main memory; in the paper's benchmarks the reduced memory traffic made attention faster.

Advanced30 mindemo