VKVlad KulikovPublic / lectures
Modern LLMs6 / 20

Module 6. Normalization, activations, and the anatomy of a Transformer block

Try each exercise first. Expand worked answers or project guidance when needed; their open state is shared between RU/EN.

Download course files (.zip) 2588.1 KB

The archive preserves the source folder structure, both languages, and supporting files.

E1. Exact block accounting

Compute the matrix-only and full counts for the Llama-3.1-8B block.

Show answer and solution

Solution. In GQA, QQ and OO are d×dd\times d, while K/V width is one quarter of dd. The two K/V matrices therefore contribute 2d(d/4)=0.5d22d(d/4)=0.5d^2, giving

Pattn=2d2+0.5d2=2.5d2.P_{attn}=2d^2+0.5d^2=2.5d^2.

SwiGLU has gate, up, and down projections. With dff=3.5dd_{ff}=3.5d, their count is 3ddff=10.5d23dd_{ff}=10.5d^2. Thus

Pmatrix=13d2=13⋅40962=218,103,808.P_{matrix}=13d^2=13\cdot4096^2=218{,}103{,}808.

Two RMSNorm scales add 2d=81922d=8192, yielding 218,112,000. Biases are excluded by this architecture contract.

E2. Epsilon and gain

Compare the RMSNorm multipliers at input RMS 10−310^{-3} for ϵ=10−6\epsilon=10^{-6} and 10−510^{-5}.

Show answer and solution

Solution. RMSNorm divides by RMS⁡(x)2+ϵ\sqrt{\operatorname{RMS}(x)^2+\epsilon}. Here the signal-square term is 10−610^{-6}, so

g10−6=1/2⋅10−6≈707.1,g_{10^{-6}}=1/\sqrt{2\cdot10^{-6}}\approx707.1, g10−5=1/11⋅10−6≈301.5.g_{10^{-5}}=1/\sqrt{11\cdot10^{-6}}\approx301.5.

The larger epsilon more than halves the gain because it dominates this small signal. Epsilon must therefore reflect reduction precision and expected activation scale, not dtype machine epsilon alone.

E3. The negative gpt-oss branch

Explain why the up-branch clip still changes the z=−9z=-9 case.

Show answer and solution

Solution. Use g=min⁡(z,7)g=\min(z,7), u=clip⁡(z,−7,7)u=\operatorname{clip}(z,-7,7), and y=gσ(1.702g)(u+1)y=g\sigma(1.702g)(u+1):

zzgguuyy
-9-9-70.000012019
-2-2-20.064341377
0000
2225.806975869
77755.999625026
97755.999625026

At z=−9z=-9, the gate is not lower-clipped and its sigmoid nearly suppresses it, but the up branch is still changed to −7-7 and enters as u+1=−6u+1=-6. The output is small because of the gate, not because the clip is irrelevant. At 7 and 9, both used branches are identical, so the outputs match.

E4 [project]

Compare RMSNorm, DyT, and DyISRU on one small language model with matched seeds, optimizer, data order, and a learning-rate sweep. Do not extrapolate one run to 100B scale.

Project demos and guidance

Demo for this project exercise: English project-demo notebook for Module 6, section “Demo 1”. It shows a compact reproducible example on small inputs, not a completed solution to the full-scale project.

E5 [project]

Compare Q/K score distributions for two open models with different stabilization mechanisms. Record the exact code path; do not merge L2 normalization, Q/K RMSNorm, and logit soft-capping into one label.

Project demos and guidance

Demo for this project exercise: English project-demo notebook for Module 6, section “Demo 2”. It shows a compact reproducible example on small inputs, not a completed solution to the full-scale project.

E6. Scaling and reduction axes

Why does fixed positive epsilon break exact scale invariance? How does changing one head affect per-head and whole-projection RMSNorm?

Show answer and solution

Solution. For a positive scale cc, factor it out of the denominator:

cxc2mean⁡(x2)+ϵ=xmean⁡(x2)+ϵ/c2.\frac{cx}{\sqrt{c^2\operatorname{mean}(x^2)+\epsilon}} =\frac{x}{\sqrt{\operatorname{mean}(x^2)+\epsilon/c^2}}.

Keeping ϵ>0\epsilon>0 fixed therefore changes the normalization. With a single value x=10−3x=10^{-3}, ϵ=10−6\epsilon=10^{-6}, and unit learned scale, the output is 1/2≈0.70711/\sqrt2\approx0.7071. Doubling the input gives 2/5≈0.89442/\sqrt5\approx0.8944. The stabilizer is comparable to the squared signal, so the difference is substantial.

When RMS is reduced over all heads together, scaling one head changes the shared denominator and hence the untouched heads too. Separate head-wise reductions keep their denominators independent. Predict which heads will change before comparing the two notebook calculations.

E7. Doubly stochastic does not mean identity

Verify the example in §12. Does satisfying both sum constraints imply AX=XAX=X?

Show answer and solution

Solution. Each row and column of the matrix in §12 sums to 0.75+0.25=10.75+0.25=1. Its first output is 0.75⋅2+0.25⋅6=30.75\cdot2+0.25\cdot6=3; its second is 0.25⋅2+0.75⋅6=50.25\cdot2+0.75\cdot6=5. Thus AX=(3,5)⊤≠XAX=(3,5)^\top\ne X.

Nonnegative entries and unit row sums make each output a convex combination, bounded between 2 and 6. The output range can shrink: here it is 3 to 5. Unit column sums additionally preserve the sum across streams: 3+5=2+63+5=2+6. Neither property preserves each stream separately. After a finite number of Sinkhorn–Knopp iterations, check both sum residuals before treating these properties as exact.