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(), the number of tokens;hidden_size(), the model dimension;num_hidden_layers(), the number of layers;num_attention_heads() andnum_key_value_heads();intermediate_size(), the hidden size of the feed-forward part;tie_word_embeddings: whether the input and output layers share one matrix.
The head size is . OLMo 3 has no bias anywhere.
What to count
Once for the whole model:
- the input embedding, , and the output layer, another unless
tie_word_embeddingsis true (then they share one matrix); - a final RMSNorm (root mean square layer normalization) gain of size .
In each of the layers:
- attention projections: is , and are , is ;
- QK-norm gains: QK-norm normalizes the queries and the keys before attention, with a learned gain per feature: one of size for the queries and one of size for the keys;
- the SwiGLU (Swish-gated linear unit) MLP (multilayer perceptron): gate and up projections of each, and a down projection of ;
- two RMSNorm gains of size (one after attention, one after the MLP).
Example. A toy configuration: , , , , , , not tied. Then .
part count attention: QK-norm: MLP: two norms: per layer Total: (two embedding matrices) (layers) (final norm) .
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, , and the key norm depend on , not on . If your count does not change when you change
num_key_value_heads, one of them uses the wrong size. - With
tie_word_embeddingsfalse, the model has two matrices: forgetting the second is the most common mistake.