labs / lab-c-rmsnorm
Next task →
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 p(xt∣x<t)p(x_t\mid x_{<t}) and compute PPL=exp⁡(−1n∑tlog⁡p(xt∣x<t))\mathrm{PPL}=\exp(-\frac1n\sum_t\log p(x_t\mid x_{<t})).

Instructions

  1. 01Read section 7 of the Normalization Layers: LayerNorm and RMSNorm learning page and write its governing equation on paper.
  2. 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.
  3. 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
  4. 04Run the experiment with temperature or draft length and record perplexity and tokens/s.
  5. 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)]))