Topic 5 of 6 · 6 min read
Reading an open model's config file
Goal: Given a model's config.json, estimate its parameter count and memory needs and identify its architecture choices.
A model's config.json is a complete architecture spec: from about ten fields you can estimate the parameter count, the memory the weights need, the KV cache per token, and which design choices the model uses. Read it before downloading 16 GB of weights.
Check yourself · 1 of 3
A config has num_attention_heads 32 and num_key_value_heads 8. What attention variant is this?
Why it matters
Model cards are marketing; configs are code. A config tells you whether a model is dense or MoE, whether it uses GQA, how long its context is meant to be, and how big its vocabulary is. It also catches mistakes: a quantised copy that needs more memory than you expected, or a "long-context" variant with no scaling entry.
The fields to read
{
"hidden_size": 4096,
"num_hidden_layers": 32,
"num_attention_heads": 32,
"num_key_value_heads": 8,
"intermediate_size": 14336,
"vocab_size": 128256,
"max_position_embeddings": 8192,
"rope_theta": 500000.0,
"tie_word_embeddings": false
}
- hidden_size, num_hidden_layers: the width and depth of the stack.
- num_attention_heads vs num_key_value_heads: equal means MHA; smaller means GQA (here groups of 4).
- intermediate_size: feed-forward width. With SwiGLU there are three matrices.
- vocab_size: sets the embedding table: 128256 * 4096 = 525M parameters.
- tie_word_embeddings: false means the output head is a second table of the same size.
- num_local_experts / num_experts_per_tok: present only in MoE models.
- rope_theta, rope_scaling, max_position_embeddings: the position setup and its stated limit.
Estimating parameters
Per layer: attention = 2 * 4096 * 4096 + 2 * 4096 * 1024 = 41.9M (queries and output, plus keys and values at 8 heads of 128). Feed-forward = 3 * 4096 * 14336 = 176.2M. One layer is 218.1M, times 32 is 6.98B. Embeddings plus untied head add 1.05B. Total: about 8.03B.
Memory for weights is parameters times bytes: 8.03B * 2 bytes (bf16) is about 16 GB; 4-bit is roughly a quarter of that.
Before and after
Weak: "It is an 8B model, so 8 GB." That assumes one byte per parameter. Better: 16 GB in bf16, plus KV cache at 128 KiB per token, plus runtime overhead.
What goes wrong
- Forgetting the untied output head or counting the vocabulary once.
- Treating
max_position_embeddingsas proven quality at that length. - Reading
num_attention_headsas the KV head count. - Ignoring
torch_dtypeand assuming the files are in the precision you want.
Examples
Estimate from a config in code
d, L, ff, v, kv, hd = 4096, 32, 14336, 128256, 8, 128
attn = 2*d*d + 2*d*(kv*hd)
mlp = 3*d*ff
total = L*(attn+mlp) + 2*v*d # untied head
print(total/1e9) # about 8.03
Spotting a MoE
{"num_local_experts": 8, "num_experts_per_tok": 2, "intermediate_size": 14336}
The feed-forward width belongs to each expert. Multiply by 8 for the stored total and by 2 for the active share per token.
Key points
- Ten or so config fields are enough to estimate parameters, weight memory and KV cache per token.
- Compare num_attention_heads with num_key_value_heads to see MHA or GQA and the group size.
- Vocabulary times hidden size gives the embedding table; an untied head adds a second table.
- Weight memory is parameters times bytes per parameter: 2 for bf16, about 0.5 for 4-bit.
- Expert fields in the config mean MoE, so total and active parameters differ.
Common mistakes
- Equating parameter count in billions with gigabytes of memory.
- Counting the embedding table once when the head is untied.
- Reading max_position_embeddings as a quality guarantee.
- Skipping the KV head field and sizing the cache from attention heads.
Check yourself · 2 of 3
A model has 7B parameters in bf16. About how much memory do the weights need?
Check yourself · 3 of 3
vocab_size 32000, hidden_size 4096, tie_word_embeddings false. How many parameters sit in the embedding table and output head together?
Practice items, not counted towards your mastery.
Start here
- The Big LLM Architecture Comparison (Sebastian Raschka) (opens in a new tab)ArticleFree
Side-by-side walkthrough of recent open-weight designs: attention variants, MoE layouts, norm placement.
- RoFormer: Enhanced Transformer with Rotary Position Embedding (opens in a new tab)PaperFree
The original rotary embedding paper: how rotating queries and keys encodes relative position.
- YaRN: Efficient Context Window Extension of Large Language Models (opens in a new tab)PaperFree
A scaling method that stretches rotary models to longer contexts with little extra fine-tuning.
More for this topic
- Llama model documentation (Hugging Face Transformers) (opens in a new tab)DocsFree
Documents every config.json field of a Llama-style model, the format most open models copy.