6.11 — Putting it together: the full decoder-only Transformer + AIAYN read
Goal: Draw the full decoder-only Transformer end-to-end (embedding → N pre-LN blocks → final LN → LM head); count its parameters; then read Vaswani et al. (2017) section-by-section and identify what we have built and what differs.
1. Why this session exists
Sessions 6.1 through 6.10 have built every piece of one Transformer block:
- Attention (6.1 → 6.2 → 6.3 → 6.4 → 6.5 → 6.6 → 6.7): scaled dot-product, multi-head, causal-masked, position-aware via RoPE.
- FFN (6.8): position-wise two-layer MLP with .
- Residuals (6.9): identity skips that break the multiplicative chain rule.
- Pre-LN (6.10): LayerNorm inside the residual branch, depth-independent gradient flow.
This session puts them together. We draw the full decoder-only Transformer — the architecture of GPT-2, GPT-3, LLaMA, Mistral, Qwen, the model class behind ~every modern LLM — and count its parameters. Then we read Attention Is All You Need (Vaswani et al., 2017)[2] end-to-end with the explicit goal of "underline three things that now feel obvious that did not before."
After this session you will be able to write the architecture in lines of pseudocode and read the original paper without confusion.[1, 3]
2. The full decoder-only architecture
Let be the vocabulary size, the maximum context length, the residual-stream width (a.k.a. d_model, d_embed), the number of blocks, the number of attention heads. A decoder-only Transformer maps an input token sequence to a sequence of next-token logits .
2.1 The forward pass, end-to-end
Each line corresponds to one block of computation we have already covered:
- Embedding . A matrix; row is the vector for token . (5.2)
- RoPE. Applied inside MHA, to and only, before the dot product. (6.7) No additive PE row anywhere; RoPE is the only positional information the model sees.
- MHA. Multi-head causal self-attention. (6.4 + 6.5) Output projection included.
- FFN. Position-wise with . (6.8)
- LN. Pre-norm: norm inside each residual branch, plus one at the top. (3.8 + 6.10)
- LM head. Project the final hidden state back to vocabulary logits. Weight-tied: the LM-head matrix is the transpose of the embedding — same parameters, used at both ends. (5.2)
That is the entire model. Pseudocode:
def transformer_forward(t): # t : (T,) token ids
X = E[t] # (T, d)
for ell in range(L):
# attention sub-layer
X = X + MHA(LN1[ell](X)) # (T, d)
# FFN sub-layer
X = X + FFN(LN2[ell](X)) # (T, d)
Z = LN_final(X) # (T, d)
logits = Z @ E.T # (T, V)
return logits
Twelve lines. The hidden complexity sits inside MHA and FFN, which we have already opened up in 6.4 and 6.8.
2.2 The training objective
The model is trained by causal next-token cross-entropy, the language-modelling objective of 5.3. For a target sequence with (next-token target),
The causal mask of 6.5 is what makes depend only on . There is no separate "encoder" or "decoder" in a decoder-only model; one stack does everything.
3. Parameter count
We have the per-block formulas from 6.4 and 6.8:
Plus two LayerNorms per block at each ( vectors, each in ), and a final . Per block:
For the full model with blocks plus the embedding/output:
The embedding and LM-head are tied, so they count once. Biases and norm scales contribute — negligible at scale.
3.1 Sanity-check known model sizes
| Model | | | | | Predicted | Reported | |---|---:|---:|---:|---:|---:|---:| | GPT-2 small | 768 | 12 | 12 | 50,257 | M | 124M | | GPT-2 medium | 1024 | 24 | 16 | 50,257 | M | 355M | | GPT-3 (175B) | 12,288 | 96 | 96 | 50,257 | B | 175B | | LLaMA-7B | 4096 | 32 | 32 | 32,000 | B | 6.7B | | LLaMA-70B | 8192 | 80 | 64 | 32,000 | B | 70B |
The term dominates at scale: GPT-3 has parameters from blocks alone, against from the embedding. Embeddings matter for small models, blocks dominate for large ones.
3.2 What is not counted here
- Biases. Modern LLMs (LLaMA, Mistral, Qwen) drop biases entirely; the formula is exact in that case modulo the LN scales.
- SwiGLU. Replaces the FFN's with two parallel matrices (6.8 Aside, 8.4), giving instead of . To hold parameter count constant, LLaMA uses .
- GQA/MQA. Reduces the K, V projection sizes; saves a fraction of the attention term. (8.3, with the KV-cache motivation in 6.14.)
- RoPE base frequencies. Free; no parameters.
These are second-order; the block term is the right first-order story for any decoder-only model.
4. The "residual stream" recap
6.9 §5 introduced the residual stream — the single -vector that flows from input embedding to the final LN, modified by every sub-layer's correction. With pre-LN (6.10) the picture is exact:
Each block adds two corrections (one from MHA, one from FFN). Everything the model "knows" lives in this stream; everything that comes out of a sub-layer is added to it, never replacing it. This is the entire interpretability story of Chapter 11 (Anthropic Transformer Circuits, Olsson et al. 2022)[4].
5. Read: Attention Is All You Need (Vaswani et al., 2017)
Time to sit with the paper. Open arXiv:1706.03762 and read it section by section. You should now find every section either obvious (because we covered it more carefully than the paper does) or historically interesting (because the field has moved on).
Reading guide (≈30 min):
- Abstract + §1 (Introduction). Position the paper: replace recurrence and convolutions with attention only. Note the original motivation was machine translation, not language modelling.
- §2 (Background). Cites Extended Neural GPU, ByteNet, ConvS2S — all attempts to parallelise sequence modelling without recurrence. None of them are used today; the Transformer obsoleted them.
- §3.1 (Encoder and Decoder Stacks). Two stacks of blocks. Decoder has an extra cross-attention sub-layer (6.3) on top of the masked self-attention. The decoder-only architecture (GPT family) discards the encoder; modern LLMs are §3.1's decoder side, alone.
- §3.2.1 (Scaled Dot-Product Attention). The single most-cited formula of the paper. We derived it in 6.2.
- §3.2.2 (Multi-Head Attention). Why parallel heads of dimension . We derived it in 6.4; the modern frontier uses at .
- §3.3 (Position-wise Feed-Forward). Three-line FFN definition. We spent a whole session (6.8) on it.
- §3.4 (Embeddings and Softmax). Notes weight tying between embedding and output projection —
tied_weightsin your pseudocode. Mentions the scaling on embeddings (idiosyncratic, mostly dropped in modern LLMs). - §3.5 (Positional Encoding). The original sinusoidal PE. Now in our Appendix 6.A; main-path replaced by RoPE.
- §4 (Why Self-Attention). Three columns: complexity per layer ( for attention vs for recurrence), parallelisability, maximum path length between any two positions. This is the strategic argument; we revisit complexity in detail in 6.12.
- §5 (Training). WMT2014 English-German, 8 P100 GPUs, 4-day training. Adam with the inverse-square-root warmup schedule (3.7 § warmup) at 4000 steps. Label smoothing .
- §6 (Results). SOTA BLEU on EN-DE and EN-FR translation. A small ablation grid in Table 3.
- §7 (Conclusion). Foreshadows future work in non-translation tasks.
5.1 Three things that should now feel obvious
After reading, write down three statements from the paper that now feel intuitive but would have been hard to motivate a week ago. The exact three are personal; here are common ones:
- "Why in the softmax." §3.2.1 motivates it with one line about variance. We derived it carefully in 6.2 §4.
- "Why multiple heads in parallel." §3.2.2 says "allows the model to jointly attend to information from different representation subspaces." We made the case in 6.4 §3: one projection vs smaller ones gives the same FLOP count but specialised heads.
- "Why residual + LN around every sub-layer." §3.1 says "We employ a residual connection around each of the two sub-layers, followed by layer normalization." We spent a session on each (6.9, 6.10).
5.2 What the paper does not contain
Worth knowing what is missing in AIAYN that we have, post-hoc, made canonical:
- Pre-norm. AIAYN is post-norm. Pre-norm (6.10) was added by GPT-2 and is the modern default.
- RoPE. AIAYN uses sinusoidal additive PE; RoPE (6.7) was introduced by Su et al. (2021) and is the modern default.
- SwiGLU. AIAYN uses ReLU FFN; SwiGLU (Cap 8.4) is the modern default.
- RMSNorm. AIAYN uses LayerNorm; RMSNorm (3.8) is the modern default in LLaMA/Mistral.
- GQA / KV cache. AIAYN has full MHA and no inference-side cache; both (6.14, 8.3) came later.
In other words: AIAYN is the load-bearing skeleton; everything since has been "what muscles to hang on it." The 2017 paper got the skeleton right.
6. The decoder-only family tree
For a sense of how this one architecture has scaled:
| Year | Model | Architectural diffs from AIAYN | |---|---|---| | 2018 | GPT-1 | Decoder-only; learned PE; standard MHA, ReLU FFN, post-LN. | | 2019 | GPT-2 | + pre-LN; otherwise same. | | 2020 | GPT-3 | Same as GPT-2, scaled to 175B. | | 2022 | PaLM | + SwiGLU; + RoPE; + parallel attention/FFN. | | 2023 | LLaMA-1, 2 | + RoPE; + SwiGLU; + RMSNorm; pre-norm. | | 2023 | Mistral 7B | LLaMA-2-style + sliding-window attention (6.13). | | 2023 | LLaMA-2-70B | + grouped-query attention (8.3). | | 2024 | LLaMA-3 | + tied embeddings only at smaller sizes; very deep. | | 2024 | DeepSeek-V2 | + multi-head latent attention (Cap 8.5). | | 2024–26 | Frontier LLMs | All decoder-only, all pre-norm, almost all RoPE, almost all GQA/MLA. |
All of these are the same skeleton — embedding → N pre-LN blocks → final LN → LM head — with substitutions in (FFN non-linearity, PE scheme, KV-head sharing, norm type). The skeleton has been frozen since 2019.
7. What we keep going forward
Sixty lines of pseudocode (with MHA and FFN opened up). parameters in the blocks. Trained by causal next-token cross-entropy. Augmented by RoPE inside attention. This is the architecture of every frontier LLM in 2026.
Next session opens up the compute and memory cost — where attention's dominates, where the FFN's dominates, and what this implies for context-length scaling.
(a) For GPT-2 small (), compute the total parameter count from the formula . Check against the headline number 124M.
(b) Write the forward pass of a decoder-only Transformer in 12 lines of pseudocode (as in §2.1). Mark, with comments, where: (i) RoPE applies, (ii) the causal mask applies, (iii) the residual additions happen.
(c) Read AIAYN §3 (about 4 pages). Underline three statements that you now find obvious that you would have struggled to motivate a week ago.
(d) AIAYN uses post-LN; GPT-2 onward use pre-LN. Why did this single change make scaling to 100+ layers feasible? (Recap 6.10 in two sentences.)
(e) A decoder-only model and an encoder-only BERT-style model differ in two ways: (i) the attention mask, (ii) the training objective. State each difference in one sentence and explain why decoder-only is preferred for generative LLMs.
References
- [1]
Raschka, S. (2024). Build a Large Language Model (From Scratch), Chapters 3–4. Manning. — Self-attention implementation (6.3, 6.4), causal masking (6.5), pre-norm block (6.10–6.11).
↩ - [2]
Vaswani, A., Shazeer, N., Parmar, N., et al. (2017). "Attention is all you need". NeurIPS. arXiv:1706.03762 — The Transformer paper. Scaled dot-product attention (6.2), multi-head attention (6.4), encoder–decoder cross-attention (6.3), sinusoidal positional encoding (Appendix 6.A), the FFN sub-layer (6.8), post-LN block diagram (6.10), full architecture read (6.11).
↩ - [3]
Radford, A., Wu, J., Child, R., et al. (2019). "Language models are unsupervised multitask learners" (GPT-2). OpenAI technical report. — Learned absolute positional encoding (Appendix 6.B), pre-LN variant (6.10), the architectural skeleton of every modern decoder-only LLM (6.11).
↩ - [4]
Olsson, C., Elhage, N., Nanda, N., et al. (2022). "In-context learning and induction heads". Anthropic. transformer-circuits.pub — Induction heads as the mechanism behind in-context learning (6.4 §4); residual-stream view (6.11 §4).
↩