All courses

The modern LLaMA-style architecture

Name the changes modern LLMs make to the original GPT design: RMSNorm, pre-norm, SwiGLU, RoPE, no bias terms, untied embeddings. Give the reason for each.

Builds on: The decoder-only transformer, end to end, Rotary position embeddings (RoPE)

▶ Watch this lesson Start the whole course

What the animation says

Same skeleton, six changes

GPT from 2018 and LLaMA from 2023 share one skeleton: a decoder-only stack of attention and MLP blocks. What changed is a handful of details, and nearly every open model since copies them. Pre-norm, RMSNorm, no biases, SwiGLU, RoPE, and an untied output matrix. For each one, ask what it buys: stable training, less work, or better quality.

Pre-norm: keep the stream a plain sum

Each block reads the residual stream, computes something, and adds it back. The original post-norm design normalized the stream itself after every addition. Deep stacks of it often diverged unless training started very gently. Pre-norm normalizes only the input to each sublayer. The stream stays r plus f of norm of r. So gradients flow back to early layers through the additions, untouched. Your turn: tap where pre-norm puts the norm.

RMSNorm and no biases

LayerNorm subtracts the mean, divides by the spread, then applies a gain and a bias. RMSNorm keeps only the rescale. Take four numbers: four, zero, minus two, four. Square them and average: thirty-six over four is nine. The root is three. Divide by three, then multiply by a learned gain. No mean, no bias: fewer operations, same quality. LLaMA also removes the plus b from every linear layer. Quality barely moves, and training gets simpler and more stable.

RoPE and an untied output

RoPE rotates the queries and keys in every attention layer, by an angle set by each token's position. Scores then see position only as the offset between tokens. Nothing is added to the stream, and no position table limits length. At the output, LLaMA uses its own matrix instead of reusing the embedding: reading a token and predicting one are different jobs. In LLaMA seven B, that extra matrix is about a hundred thirty-one million weights. Roughly two percent. GPT-2 small is much narrower. What would untying cost there?

SwiGLU: a gate and three matrices

Thirty-one percent. That's why small models often keep the embedding tied. Now the MLP. A GELU MLP has two matrices: up to a wide layer, then back down. SwiGLU adds a third. One projection, passed through SiLU, gates a second one, element by element. The product goes back down through the third matrix, and at equal size it reliably gets lower loss. LLaMA seven B has width four thousand ninety-six. Its GELU MLP would be sixteen thousand wide. With three matrices, what hidden width keeps the same weight count?

The whole LLaMA block

Two matrices sixteen thousand wide hold as many weights as three at two-thirds of that: eight-thirds of the model width. LLaMA rounds to eleven thousand and eight. So here is one block: the stream plus RoPE attention on the RMS-normed stream, then plus SwiGLU on the RMS-normed result. No biases anywhere. After the last block, one more RMSNorm, then the separate output matrix. Pre-norm, RMSNorm and no biases buy stable, simple training. SwiGLU and RoPE buy quality and relative positions.

What did LLaMA change, and why?

The original GPT (2018) and LLaMA (2023) are both decoder-only stacks of attention + MLP blocks. Nearly every open model since LLaMA keeps the same handful of changes to that original design: - pre-norm (already adopted in GPT-2) - RMSNorm - SwiGLU - RoPE - no biases - untied embeddings What exactly does each change, and what does it buy: **stable training**, **less work**, or **better quality**?

Changes for stability and simplicity

**Pre-norm**: normalize only each sublayer's *input*, $r + f(\text{norm}(r))$. The stream stays a plain sum, so gradients reach early layers through the additions untouched, and deep stacks train stably. The original post-norm design, which normalizes the stream itself after every addition, often diverged in deep stacks unless training was started very gently. **RMSNorm**: drop LayerNorm's mean-subtraction and bias. Keep only the rescale-by-RMS and a gain. It does fewer operations with the same quality. **No biases**: remove $+b$ from every linear layer. Quality barely changes, and training is simpler and more stable.

Changes for quality and flexibility

**SwiGLU MLP**: in a gated MLP, one projection decides how much of another passes through. It reliably gets lower loss than a GELU MLP of the same size. **RoPE**: rotate $q, k$ in every attention layer. Scores then depend on relative offset. Nothing is added to the stream, and there is no learned position table with a hard length limit. **Untied $W_U$**: reading a token in and predicting the next token are different jobs. At large $d$ the extra matrix costs little.

One LLaMA-style block, precisely

$$\tilde r = r + \text{Attn}_{\text{RoPE}}(\text{RMSNorm}(r)),\quad r' = \tilde r + \text{MLP}(\text{RMSNorm}(\tilde r))$$ $$\text{RMSNorm}(x)=\frac{x}{\sqrt{\tfrac1d\sum_i x_i^2+\epsilon}}\odot g$$ $$\text{MLP}(x)=\big(\text{SiLU}(xW_1)\odot xW_3\big)W_2$$ Here: - $W_1,W_3\in\mathbb R^{d\times d_{ff}}$ and $W_2\in\mathbb R^{d_{ff}\times d}$. - SiLU$(u)=u\,\sigma(u)$. - $\odot$ is elementwise multiplication. - $g$ is a learned gain. - $\epsilon$ is a tiny constant that avoids division by zero. There is no $+b$ anywhere. The output is $z=\text{RMSNorm}(r^{(L)})\,W_U$, with $W_U\neq W_E^\top$.

Numbers: SwiGLU width and untying cost

Take LLaMA-7B: $d=4096$. A GELU MLP with $d_{ff}=4d$ has $2\cdot4096\cdot16384\approx134$M weights. SwiGLU uses 3 matrices, so set $d_{ff}=8d/3\approx10923$: $3\cdot4096\cdot10923\approx134$M, the same size. LLaMA rounds this to $11008$. Untying adds $V d = 32000\cdot4096\approx131$M, about 2% of 6.7B. For GPT-2 small ($V=50257$, $d=768$), $Vd\approx38.6$M, about 31% of 124M. That is why small models often keep tying.

Remember

A LLaMA-style block is $r + f(\text{RMSNorm}(r))$ with no biases. Its MLP is SwiGLU with $d_{ff}\approx 8d/3$, and positions enter only as RoPE rotations of $q,k$. The output matrix $W_U$ is separate from $W_E$. Pre-norm, RMSNorm and dropping biases make training **stable and simple**. SwiGLU and RoPE buy **quality and relative positions**. Untying is cheap once the model is large.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.