Roadmap / Case Study: OLMo 3

✓

Count OLMo 3's Parameters

easycase-studyolmo-3transformer

The problem

The released config.json file of a model lists its sizes: the number of layers, the model dimension, the number of heads, and so on. That is enough to count its parameters exactly, and a good test that you understand the architecture: if your count matches the published checkpoint to the last digit, you have not forgotten any matrix.

Implement count_params(cfg), where cfg is a dict with the keys of the Hugging Face configuration of OLMo 3:

  • vocab_size (VV), the number of tokens;
  • hidden_size (DD), the model dimension;
  • num_hidden_layers (LL), the number of layers;
  • num_attention_heads (HH) and num_key_value_heads (GG);
  • intermediate_size (FF), the hidden size of the feed-forward part;
  • tie_word_embeddings: whether the input and output layers share one matrix.

The head size is d=D/Hd = D / H. OLMo 3 has no bias anywhere.

What to count

Once for the whole model:

  • the input embedding, V×DV \times D, and the output layer, another V×DV \times D unless tie_word_embeddings is true (then they share one matrix);
  • a final RMSNorm (root mean square layer normalization) gain of size DD.

In each of the LL layers:

  • attention projections: WqW_q is D×HdD \times Hd, WkW_k and WvW_v are D×GdD \times Gd, WoW_o is Hd×DHd \times D;
  • QK-norm gains: QK-norm normalizes the queries and the keys before attention, with a learned gain per feature: one of size HdHd for the queries and one of size GdGd for the keys;
  • the SwiGLU (Swish-gated linear unit) MLP (multilayer perceptron): gate and up projections of D×FD \times F each, and a down projection of F×DF \times D;
  • two RMSNorm gains of size DD (one after attention, one after the MLP).

Example. A toy configuration: V=10V = 10, D=8D = 8, L=2L = 2, H=2H = 2, G=1G = 1, F=16F = 16, not tied. Then d=4d = 4.

part count
attention: 8⋅8+2⋅(8⋅4)+8⋅88 \cdot 8 + 2 \cdot (8 \cdot 4) + 8 \cdot 8 192192
QK-norm: 2⋅4+1⋅42 \cdot 4 + 1 \cdot 4 1212
MLP: 3⋅8⋅163 \cdot 8 \cdot 16 384384
two norms: 2⋅82 \cdot 8 1616
per layer 604604

Total: 2⋅10⋅82 \cdot 10 \cdot 8 (two embedding matrices) +  2⋅604+ \; 2 \cdot 604 (layers) +  8+ \; 8 (final norm) =160+1208+8=1376= 160 + 1208 + 8 = 1376.

What to implement

count_params(cfg): the total number of parameters.

Requirements

  • Return a Python int.
  • The tests compare against the parameter counts of the published checkpoints: 7,298,011,136 for the 7B and 32,233,522,176 for the 32B.

Hints

  • With grouped-query attention, WkW_k, WvW_v and the key norm depend on GG, not on HH. If your count does not change when you change num_key_value_heads, one of them uses the wrong size.
  • With tie_word_embeddings false, the model has two V×DV \times D matrices: forgetting the second is the most common mistake.
solution.py
Python idle

Press Run (⌘/Ctrl + Enter) to test your code.