Practical Labs/Lab: Normalization Layers: LayerNorm and RMSNorm
Level 3 · generative-ai
Lab: Normalization Layers: LayerNorm and RMSNorm
Build a minimal, verifiable implementation that demonstrates Normalization Layers: LayerNorm and RMSNorm. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.
Mathematical basis
For Normalization Layers: LayerNorm and RMSNorm, implement and compute .
Instructions
- 01Read section 7 of the Normalization Layers: LayerNorm and RMSNorm learning page and write its governing equation on paper.
- 02Implement the core operation in PyTorch / Transformers on TinyStories or a toy token sequence. Do not call a library function that solves the whole task.
- 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
- 04Run the experiment with temperature or draft length and record perplexity and tokens/s.
- 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.
Starter code
import numpy as np
def perplexity(logprobs):
lp = np.asarray(logprobs, float)
return float(np.exp(-lp.mean()))
assert abs(perplexity([np.log(0.5), np.log(0.5)]) - 2.0) < 1e-9
print(perplexity([np.log(0.5), np.log(0.5)]))