Part 1 of 6 · Inference Engineering

Attention

How tokens read each other, one guess at a time — Query, Key, Value, the softmax that blends them, and the O(n²) cost that the whole engine is built to tame.

Dims everything but the section you're reading.
Color key — each role keeps its own hue Green = where you are / progress Blue = keywords Violet = math Coral = analogy
01 / 06 Tokens read each other
  1. 01 Tokens read each other
  2. 02 Query, Key, Value
  3. 03 The scaled dot-product formula
  4. 04 Causal mask & many heads
  5. 05 The O(n²) cost
  6. 06 On your cluster
01

Attention lets tokens read each other

TL;DR · Attention is the step where each token reaches back and pulls in information from the earlier tokens that matter to it.

A token on its own knows only itself. Attention is how "it" finds out what "it" refers to — the one operation that lets context actually flow between positions.

The whole point: mixing context

Attention enables each token to read information from earlier tokens in the sequence. Without it, every position would be processed in isolation; with it, a word can absorb meaning from anything that came before.

It's selective, not a blur

A token doesn't soak up all earlier tokens equally. It scores how relevant each earlier token is, then pulls hardest from the ones that matter — a pronoun reaching back to its noun, a verb reaching back to its subject.

This is what "transformer" means

Stacking many of these attention steps is the heart of the transformer. Everything in the next lesson — the block, the layers — is built around repeating this read-from-the-past operation.

Keywords — tap to unfold the plain meaning

Analogy Back at the line, a cook reading the current order item glances back over everything already written on the ticket and asks "which earlier lines change what I do now?" — the "no nuts" note, the "table is in a hurry" note. They don't re-cook every earlier line; they just let the relevant ones colour the next move. Attention is that glance back over the ticket.

Attention is the one operation that lets a token read from earlier tokens — selectively, weighting the relevant ones most.

02

Query, Key, Value: the three roles

TL;DR · Each token projects into three vectors — a Query (what it asks), a Key (what it advertises), and a Value (what it carries).

All of attention is built from three little vectors per token. Get what each one is for and the formula in the next station reads itself.

Query — the question this item asks

The Query (Q) is the question the current token poses: "what am I looking for?" It's the vector the token uses to go shopping among everything before it.

Key — the label each item advertises

The Key (K) is the label each earlier token advertises: "here's what I'm about." A Query matches strongly against Keys that answer its question.

Value — the content each item carries

The Value (V) is the actual content each token hands over once it's been matched: "if you pick me, here's what you get." The output is a blend of Values.

The recipe in one line

Score every earlier token by Query·Key, softmax those scores into weights that add up to 1, and output the weighted sum of their Values. That sentence is the entire mechanism.

Keywords — tap to unfold the plain meaning

Analogy Picture the pantry behind the line. The current dish asks a question — "I need something acidic" (that's the Query). Every jar on the shelf wears a label — "vinegar," "lemon," "sugar" (those are the Keys). The cook matches the question against the labels, finds the jars that fit, and blends what's actually inside them — the contents are the Values. Question, label, contents: Query, Key, Value.
The current token emits a Query, which is matched against the Keys of earlier tokens; the resulting weights blend the earlier tokens' Values into the output. one token's Query shops among the earlier tokens Query (Q) current token asks Key₁ · Value₁ Key₂ · Value₂ Key₃ · Value₃ earlier tokens advertise Keys, carry Values output weighted Σ of Values
The thick match line is the strongest Query·Key score — that earlier token's Value dominates the blended output. Thinner lines contribute less.

Query asks, Key advertises, Value carries. Match Q against the Keys, weight by the match, blend the Values.

03

The scaled dot-product formula

TL;DR · One compact equation — softmax(QKᵀ/√d_k)V — packs the whole "score, normalize, blend" recipe, with a √d_k divide to keep the scores tame.

Don't let the symbols scare you. You already know what they mean from Station 02 — this is just that one sentence written in math.

Dot product = the relevance score

A dot product multiplies two vectors' paired numbers and adds them. It's larger when the vectors point the same way — so Q·K is a natural "how well does this Key answer my Query" score.

Why divide by √d_k

When the vectors are long (high d_k, the per-head dimension), dot products grow large and softmax gets razor-sharp. Dividing by √d_k rescales the scores so softmax stays smooth and trainable.

Softmax then weighted sum

Softmax squashes the raw scores into positive weights that add up to 1. Multiplying those weights by V produces the weighted sum of Values — the token's attention output.

Math, decoded — scaled dot-product attention

Attention(Q,K,V) = softmax(QKTdk) V
  • Qthe Queries — what each token is asking for
  • Kthe Keys — the label each earlier token advertises
  • Vthe Values — the content each earlier token carries
  • QKᵀevery Query dotted with every Key: the raw relevance scores, one per pair
  • √d_ksquare root of the per-head dimension; divides the scores so softmax doesn't get too peaky
  • softmax(·)turns those scores into positive weights that sum to 1
  • (…)Vweights times Values = the blended output the token walks away with

Read it left to right: score every token-pair with QKᵀ, shrink by √d_k, softmax into weights, then blend the Values. It's exactly the one-line recipe — Query·Key, softmax, weighted sum of Values — written in symbols.

Keywords — tap to unfold the plain meaning

softmax(QKᵀ/√d_k)V — score with the dot product, tame it with √d_k, normalize with softmax, blend the Values.

04

The causal mask & many heads

TL;DR · A causal mask blocks any token from peeking at the future; multi-head attention runs many of these reads in parallel, each learning a different pattern.

Two refinements turn raw attention into the thing that actually powers an LLM: one stops it cheating, the other lets it specialize.

Causal mask — no peeking ahead

The causal mask means a token may only attend to tokens before it, so it can't peek at the future it's trying to predict. Next-token prediction would be trivial — and useless — if a token could read the answer.

Multi-head — many reads at once

Multi-head attention runs several attention computations in parallel, each with its own separate Q/K/V projections. Each head learns a different relationship — one for syntax, one for referential links, and so on — then their notes are combined.

Keywords — tap to unfold the plain meaning

Analogy Several specialist cooks each scan the ticket for a different pattern — one watches for allergies, one for timing, one for sauces — then they combine notes before the dish goes out. Each specialist is a head. And the causal rule is simple kitchen discipline: you can read every line written above the current one, but never a line that hasn't been written yet.

Causal mask = read only the past. Multi-head = many parallel reads, each a specialist, notes combined.

05

The O(n²) cost — and the three fixes

TL;DR · Every token attends to every earlier token, so attention costs O(n²) in sequence length — and the KV cache, GQA, and FlashAttention all exist to beat that down.

Attention's superpower — letting every token see every other — is also its bill. That n-squared is the single biggest reason serving LLMs is hard, and the next several lessons are all about taming it.

Quadratic in the sequence length

Because each of n tokens scores against all n tokens, attention complexity is O(n²) in the sequence length. Double the context and the attention work roughly quadruples.

Fix 1 — the KV cache

The KV cache stores the unchanging Keys and Values from past tokens so each new step doesn't recompute them. You'll meet it in depth later; here it's the headline fix for repeated work.

Fix 2 — GQA

Grouped-Query Attention (GQA) lets multiple query heads share fewer K/V heads, shrinking how much Key/Value data has to be stored and moved.

Fix 3 — FlashAttention

FlashAttention computes the scores in fast on-chip SRAM instead of materializing the full score matrix in slow memory — the same answer, far less memory traffic.

A curve rising as the square of sequence length n, showing attention work growing quadratically; labels mark the KV cache, GQA, and FlashAttention as the three optimizations that bring it down. attention work ∝ n² — every token attends to every token sequence length n → work → O(n²) KV cache GQA FlashAttention three fixes that flatten the curve
The curve is the cost; the three blue tags are the tools the inference engine reaches for to bend it back down — each covered in its own later lesson.

Keywords — tap to unfold the plain meaning

Math, decoded

pairs scored = n × n = n2  →  cost ∈ O(n2)
  • nthe number of tokens in the sequence (the context length)
  • n × neach token scores against every token, so the score matrix has n² entries
  • O(n²)work grows with the square of the sequence — the cost the fixes attack

Every Query meets every Key, so the score matrix is n by n. That squared term is why long contexts get expensive fast — and why KV cache, GQA, and FlashAttention exist.

Analogy If every cook on the line had to walk to one faraway pantry and back for every ingredient, the trips — not the cooking — would set the pace. That long walk is memory bandwidth: the answer is fast to compute but slow to fetch. The KV cache keeps the labelled jars right at the station instead of across the kitchen, so each step is one short reach, not a full lap.

Attention is O(n²) because every token attends to every token. KV cache, GQA, and FlashAttention are the three tools built to flatten that cost.

06

On your cluster

TL;DR · Qwen3.6 runs 24 query heads sharing just 4 KV heads (GQA), shrinking the KV cache 6× — and uses cheap linear attention most layers, full attention every 4th.

The theory shows up directly in the config you serve. Peek at the model on the box and you can read GQA and hybrid attention straight off the head counts.

GQA in the config

The example model uses 24 query heads with only 4 KV heads (head_dim 256). Because 6 query heads share each K/V head, the KV cache is 6× smaller than it would be with one K/V head per query head.

Hybrid attention layers

It also mixes attention types: cheap linear attention on most layers, with full attention every 4th layer. The full layers preserve quality where it matters; the linear ones keep the bulk of the network cheap.

Inspect the heads on the 4×H100 box

curl localhost:8000/v1/models # Qwen3.6-27B-FP8 on 4×H100
# config.json reports:
  "num_attention_heads": 24,
  "num_key_value_heads": 4,    # GQA → 6× smaller KV cache
  "head_dim": 256
Cluster note Read off the Qwen3.6-27B-FP8 config on the 4×H100 box: 24 query heads / 4 KV heads at head_dim 256 means each K/V head is shared by 6 query heads — a 6× smaller KV cache, directly easing the memory pressure from the O(n²) attention you saw in Station 05. The hybrid schedule (linear attention most layers, full attention every 4th) trims it further.

Keywords — tap to unfold the plain meaning

Check yourself

  1. What are the three roles a token projects into, and what does each one do?
  2. What does the √d_k in softmax(QKᵀ/√d_k)V protect against, and what does the causal mask forbid?
  3. Why is attention O(n²), and which three optimizations are built to flatten that cost?

Your served model wears the theory on its sleeve: 24 query / 4 KV heads is GQA cutting the cache 6×, and every-4th-layer full attention is the hybrid trick in action.

Reached the end — nice. This lesson now counts toward your progress.