Module 2 · Interactive companion
Inside a Transformer
One vector per token. Two decoder blocks. Follow how attention updates each position.
Component buttons provide every explanation and shape. The 3D scene is an optional spatial companion to the same components.
Start from 2.3 · MHA / GQA / MQA; review 2.1 · Self-attention or 2.2 · Multiple heads. Residual paths, normalization, FFN and memory details are optional previews.
Layout and selected query
Advanced: model size
Changing query count or head width changes model width: this is a different model architecture. Start with 8 queries, width 64 and 3 tokens. To compare scale alone, hold heads fixed and compare 3 with 8,192 tokens; raw KV storage grows linearly with sequence length.
Schematic model: 2 layers · 8 query heads · 8 KV heads · head width 64 · model width 512.
Causal decoder · pre-norm · schematic
Loading local 3D renderer… Component buttons below work independently of 3D.
Drag to orbit · wheel / pinch to zoom · use component buttons with keyboard or touch. Arrows on focused 3D view rotate; + / − zoom; Home resets.
Components and views
Architecture note
Follow one token through the model
Choose a component to inspect its role, formula and dimensions. Open a decoder block, then its attention module, to see the head structure.
One sequence; axes shown as token × head × component where applicable. Shapes are exact for the selected schematic configuration. No learned weights or attention values are simulated.
Read the head-sharing derivation →Optional · What changes in the KV cache?
One sequence, two layers, BF16 / FP16 storage (2 bytes per element). Same query count and head width for every comparison; GQA reference uses one KV head per four queries (two per group when there are only four queries). Changing layout describes a different model architecture, not a lossless runtime switch for an existing checkpoint.
Raw K/V payload only. Excludes weights, activations, allocator waste, cache metadata and temporary repetitions of shared K/V. Fewer KV bytes do not guarantee a proportional speedup or equal model quality. This view counts storage for all selected positions, even when inspecting an earlier position.
Optional next: 2.4 · Cache internals · 2.5 · Memory and performance
Architecture summary, notation and sources
Token embeddings + learned position embeddings → decoder block 1 → decoder block 2 → final LayerNorm → vocabulary projection → logits. In each pre-norm block: LayerNorm → causal attention → add incoming vector → LayerNorm → FFN → add the vector after attention. The FFN uses Linear → GELU → Linear with 4× hidden width. Other decoder families can use rotary positions, RMSNorm and gated FFNs. There is no encoder or cross-attention here.
Attention: O = softmax(QKᵀ / √D + M)V. Each future position receives mask −∞, so its weight is exactly zero. Softmax is applied over key positions for each query row. Selected query position can attend itself. Causal visibility does not mean every allowed position gets equal weight.
- T
- Token positions in the selected sequence.
- Hq / HKV
- Query heads / key-value heads; equal groups require divisibility.
- D
- Width of one query, key or value head in this example.
- dmodel
- Model width; query heads × head width here.
- L / B
- Attention layers / bytes per stored element (2 / 2 here).
Primary references: Attention Is All You Need, GPT-2 pre-norm implementation, Multi-query attention, Grouped-query attention. This page combines these ideas into a teaching schematic; it is not a specific pretrained model.