Skip to reading
AI.LLM.Transformer AI / Modern ML

Transformers and attention — the architecture behind LLMs

Node 9 of 12 in AI

Objectives

  • Identify the key rules and §§ that apply to transformers and attention — the architecture behind llms.
  • Apply the AI.LLM.Transformer knowledge element in a typical exam scenario.
  • Recognize common distractors and partial-credit answers.

Read

AI.LLM.Transformer

Transformers and attention — the architecture behind LLMs

AI Era · Transformer Architecture Attention, MHA, KV cache, positional encoding

**Scaled dot-product attention (Vaswani et al. 2017):**

Attn(Q,K,V) = softmax(QKᵀ / √d_k) V

Q (queries) and K (keys) are projected from hidden states; V (values) is also projected. √d_k scaling prevents softmax saturation as dimensionality grows.

**Multi-head attention (MHA):** h parallel attention heads on dim d_model/h each. Concat and project back to d_model. Allows heads to specialize on different relational patterns. **GQA (Grouped-Query Attention, Ainslie et al. 2023)** and **MQA** share K,V across groups of heads — reduce KV cache memory ~4-8× at small quality cost; standard in Llama 3, Claude, Mistral.

**Decoder-only transformer block:** RMSNorm (or LayerNorm) → self-attention (causal mask) → residual → RMSNorm → MLP (SwiGLU or GELU) → residual. Stacked N times.

**Positional encoding:** • **Sinusoidal** (original) — fixed; limited extrapolation. • **Learned absolute** — positions 0..max_len. • **RoPE** (Rotary PE, Su et al. 2021) — rotates Q,K in 2D planes by position. Used by Llama, Mistral, most modern LLMs. Extendable via NTK/YaRN/LongRoPE. • **ALiBi** — linear distance bias on attention scores.

**KV cache:** during autoregressive generation, cache computed K,V tensors for past tokens. Memory scales O(L × d × layers) per request — the dominant memory cost for long context. Paged attention (vLLM, Kwon et al. 2023), KV quantization (Int4/Int8), and context-parallel serving are active areas.

**FlashAttention (Dao et al. 2022, v2 2023, v3 2024):** IO-aware attention that tiles computation to fit in SRAM, avoiding materialization of the N×N attention matrix. 2-4× speedup and enables long context. Standard in production training/inference stacks.

**Encoder-decoder** transformers (T5, BART) remain strong for translation/summarization; decoder-only dominates general chat/coding.

Vaswani et al. (2017) Attention Is All You Need, NeurIPS Dao (2023) FlashAttention-2 Su et al. (2021) RoFormer (RoPE)

Check yourself

3 quick questions — no score kept, just formative feedback.

  1. Q1 · AI.LLM.Transformer

    In scaled dot-product attention (Vaswani et al. 2017), the attention scores QKᵀ are divided by √d_k before softmax primarily to:

    Answer choices
  2. Q2 · AI.LLM.Training

    DPO (Direct Preference Optimization, Rafailov et al. 2023) differs from RLHF-PPO in that DPO:

    Answer choices
  3. Q3 · AI.LLM.Transformer

    Rotary Position Embedding (RoPE) encodes token positions by:

    Answer choices

Tutor

Scoped to AI.LLM.Transformer .

  1. Ask questions about this passage. Answers cite the specific corpus chunk and regulation. The tutor will never reproduce real exam items.