Reading notes · Inference Engineering · §2.2.2 Transformer Blocks

One token's trip through the transformer

The body of an LLM is three kinds of layers: an embedding layer that turns tokens into vectors, a tall stack of transformer blocks that refine them, and an output layer that turns the final vector into a prediction. Below, we follow one real token through one full decode pass.

“Within the transformer blocks, there are sublayers for attention, a feed-forward neural network, and normalization.” — Inference Engineering, §2.2.2

01 · The walkthrough

Follow the token

The prompt is the book's own sentence, cut off mid-thought: “I decided to write a book because I thought it would be…” The model's job in this decode pass is to produce the next token. Step through the pipeline with the buttons; the map shows where you are, and the teal bar tracks what shape the data has at each point.

02 · Inside the attention sublayer

Multi-head attention, on the book's sentence

Attention relates the current token to every earlier one. Each head is a full, independent attention operation running in parallel, and different heads learn different kinds of relationships — the book gives subject–verb agreement and co-reference (figuring out what “it” refers to) as examples. Hover any word to see where that token's query lands; switch heads to see the same token asking a different question.

Hover a word. Arcs point only backwards — the causal mask hides every future token, which is what makes this a causal language model.

Under the hood each head computes the book's Figure 2.8: the current token's query (Q) is dot-producted against the keys (K) of all prior tokens, scores are softmaxed into weights like the arcs above, and those weights blend the prior tokens' values (V) into an update. Because every token looks at every earlier token, the raw cost grows quadratically with sequence length — the KV cache (built during prefill, §5.3) is what keeps decode linear by storing every K and V instead of recomputing them. These weights are hand-drawn for illustration; real heads are learned and messier — one famous quirk being that many heads dump spare attention on the first token, an “attention sink.”

03 · Where the weights live

The FFN is the heavyweight

The book notes the feed-forward sublayers hold the majority of an LLM's weights, attention is second, and normalization is “a rounding error.” Here is that claim made concrete with Llama-3-8B's actual dimensions (32 blocks, hidden size 4,096, FFN width 14,336, vocabulary 128,256):

Feed-forward (×32)5.64 B · 70.2%
Attention (×32)1.34 B · 16.7%
Embedding + LM head1.05 B · 13.1%
All normalization~0.3 M · 0.003%

Parameter counts computed from the architecture's config.json dimensions (§2.2.1). Yet the hardest part to run fast is attention, not the FFN — matmuls are the GPU's favorite food, while attention's data movement is what needed inventions like FlashAttention and the KV cache.

04 · Reading the diagram like an engineer

Details worth noticing