bugger.

LLM architectures

Work through the math inside a transformer, then compare current models and find benchmarks that match your task.

Animated walkthroughs

Compute one attention head

0:43

What can “lantern” learn before it sees “glows”?

Step 1 of 4Every column keeps the same token from projection to output.

Read the worked example
  1. Project Q, K, and V

    Each hidden vector is projected by three learned matrices. For token 2, multiply [1, 2] by the query matrix’s columns [1, 0] and [−1, 1] to get query [1, 1]. The three keys are [1, 0], [1, 1], and [2, 1]. Their values are [2, 0], [0, 2], and [2, 2].

  2. Score the keys

    Take the dot product of the query with each key: 1, 2, and 3. Divide all three by the square root of the key dimension, 2, to get about 0.71, 1.41, and 2.12. These are pre-softmax logits.

  3. Mask and normalize

    Token 3 is in the future for query 2. Set its logit to negative infinity before softmax, so it gets weight zero even though its raw score was highest. Exponentiate the remaining logits and divide each by their sum: the weights are about 0.33 and 0.67.

  4. Mix the values

    Multiply the value vectors by their weights and add: 0.33 × [2, 0] + 0.67 × [0, 2] + 0 × [2, 2] gives about [0.66, 1.34]. That is this head’s output for token 2. The model combines the outputs of all heads through a learned output projection.

Change one part of the block.

Change the mask, route a token, grow the context, or give a block another pass.

Which keys can each query see?
Q ↓ / K →Thecatsatonthemat
VisibleMaskedMaskedMaskedMaskedMasked
VisibleVisibleMaskedMaskedMaskedMasked
VisibleVisibleVisibleMaskedMaskedMasked
VisibleVisibleVisibleVisibleMaskedMasked
VisibleVisibleVisibleVisibleVisibleMasked
VisibleVisibleVisibleVisibleVisibleVisible

Who can see whom?

Choose an attention mask, then select a query on the left side of the grid.

4 of 6 keys visible“on” can see “The”, “cat”, “sat”, “on”.

Three ways to build a decoder

Dense transformer

Attention
Full attention in every layer
FFN
One dense feed-forward network

GPT-2, Llama 2

KV cache and FLOPs grow with context.

Sparse MoE

Attention
Full attention with MLA or GQA
FFN
Many experts, top-k routing

DeepSeek-V3, gpt-oss

Attention is still quadratic; all-to-all becomes the new cost.

Hybrid sparse stack

Attention
Linear or recurrent layers plus sparse full attention
FFN
Fine-grained or latent MoE

Kimi K3, DeepSeek V4, Qwen3.5, Nemotron 3

Lossy memory, routing complexity, immature kernels.

Interview prep