Count a model's parameters from its config file
Multiply out the shapes in config.json and add them up. Qwen3-8B comes to exactly 8,190,735,360. Plus MoE totals, memory and the traps.
You're choosing an open-weight model to run on your own GPU. The model card for Qwen3-8B says "8.2B", and its config.json says hidden_size: 4096, num_hidden_layers: 36 and a dozen other numbers. Those numbers are the whole recipe: every weight matrix in the model has a shape they set. Once you can turn the config into a count, you can check a model card, size a GPU, and see what a mixture-of-experts model's "30B" means, all before downloading a single weight.
The short answer
A typical dense language model's parameters are an embedding matrix, an output matrix (unless the two are tied), a final norm, and a stack of identical layers, each holding four attention projections, the MLP's matrices and a few normalisation vectors. Every one of those shapes comes from config.json, so the count is multiplication and addition. For Qwen3-8B it comes to 8,190,735,360, exactly the number Hugging Face reports, and two-thirds of it sits in the MLPs.
What is a parameter, and where do they live?
A parameter is one learned number in one of the model's weight matrices or vectors. A matrix that maps a 4,096-wide vector to a 1,024-wide one holds 4,096 × 1,024 = 4,194,304 of them. A transformer is almost entirely matrices like that, plus a few vectors, so counting one is a four-step job: list the matrices, read their shapes from the config, multiply, add.
Attention in one diagram followed a token through one layer. Here is the same model drawn as what it stores, with each box labelled by the config fields that set its shape:
The arrows show the order the parts are applied in. What they leave out holds no parameters at all: the residual adds, which carry each token's vector around attention and around the MLP, are plain additions. The activation functions and the softmax are fixed arithmetic too, and so is RoPE, the rotary position scheme most current models use: it rotates queries and keys by angles computed from the position, with no learned table.
The config fields that matter
Eight fields decide almost the whole count. The table gives Qwen3-8B's values from its config.json; the symbols are the ones the formulas below use.
| field | what it sets | symbol | Qwen3-8B |
|---|---|---|---|
vocab_size | rows of the embedding and output matrices | 151,936 | |
hidden_size | width of the vector each token carries between layers | 4,096 | |
num_hidden_layers | how many identical layers are stacked | 36 | |
num_attention_heads | query heads per layer | 32 | |
num_key_value_heads | key and value heads, each shared by a group of query heads | 8 | |
head_dim | width of each head | 128 | |
intermediate_size | hidden width of the MLP | 12,288 | |
tie_word_embeddings | whether the output head reuses the embedding matrix | false |
The rest of the recipe is in the architecture, named by the architectures field (Qwen3ForCausalLM). The Qwen3 Technical Report (section 2) spells it out: a SwiGLU MLP, RMSNorm before each sublayer, QK-norm in attention, and no biases. When the paper and your count disagree, the model's source code in Transformers is the final word on which matrices exist.
Counting one layer
Attention
Attention has four projections. The query projection turns the hidden vector into one query per head, so it's = 4,096 × 4,096. The output projection maps the joined heads back to the hidden size, so it's the same size.
The key and value projections are where most current models save. With grouped-query attention (GQA, Ainslie et al., 2023), a group of query heads shares one key head and one value head. Qwen3-8B has 32 query heads and 8 key/value heads, so groups of four share, and and are only wide.
Qwen3 adds QK-norm: an RMSNorm applied to every query head and every key head before the position rotation and the scores. All query heads share one learned 128-wide scale vector, and all key heads share another, so that's 256 more, for 41,943,296.
MLP
The MLP works on each token's vector on its own. Qwen3 uses SwiGLU (Shazeer, 2020): a gate projection and an up projection, both from to , multiplied element by element, then a down projection back to . That's three matrices of :
Older models such as GPT-2 use a plain MLP with two matrices and biases, so their formula is plus .
Norms
Each layer has two RMSNorms, one before attention and one before the MLP. An RMSNorm is a single learned scale vector of width , so the pair is 8,192. (LayerNorm, in older models, also has a shift vector, so it's each.)
One Qwen3-8B layer is therefore 41,943,296 + 150,994,944 + 8,192 = 192,946,432 parameters, 78% of them in the MLP. 3Blue1Brown's chapter on the MLP shows why those matrices are so large and counts them for GPT-3:
How might LLMs store facts | Deep Learning Chapter 7
3Blue1Brown · segment of 22:42
An 11-minute segment that animates the up-projection, nonlinearity and down-projection, then counts the MLP's parameters across a whole model.
Worked example: Qwen3-8B to the last parameter
Add the vocabulary matrices and the final norm to 36 layers:
| component | formula | parameters |
|---|---|---|
| embedding | 151,936 × 4,096 | 622,329,856 |
| 36 layers | 36 × 192,946,432 | 6,946,071,552 |
| final RMSNorm | 4,096 | 4,096 |
| output head (not tied) | 4,096 × 151,936 | 622,329,856 |
| total | 8,190,735,360 |
That's the "8.2B" on the model card. Take out the two vocabulary matrices and 6,946,075,648 remain, the card's "6.95B non-embedding". Across the whole model the MLPs hold 66%, attention 18%, the embedding and output head 15%, and the norms under 0.01%.
The same sum in Python, reading the config file directly:
import json
c = json.load(open("config.json"))
d, L, V = c["hidden_size"], c["num_hidden_layers"], c["vocab_size"]
heads, kv_heads = c["num_attention_heads"], c["num_key_value_heads"]
head_dim = c.get("head_dim") or d // heads
d_ff = c["intermediate_size"]
attention = (
d * heads * head_dim # W_Q
+ 2 * d * kv_heads * head_dim # W_K and W_V (narrow with GQA)
+ heads * head_dim * d # W_O
+ 2 * head_dim # QK-norm (Qwen3); drop for most other models
)
mlp = 3 * d * d_ff # SwiGLU: gate, up, down
norms = 2 * d # RMSNorm before attention and before the MLP
layer = attention + mlp + norms
embeddings = V * d
output_head = 0 if c.get("tie_word_embeddings") else V * d
total = embeddings + L * layer + d + output_head # + d for the final norm
print(f"per layer {layer:>15,} (attention {attention:,}, MLP {mlp:,})")
print(f"embeddings {embeddings:>15,}")
print(f"output head {output_head:>15,}")
print(f"total {total:>15,}")per layer 192,946,432 (attention 41,943,296, MLP 150,994,944)
embeddings 622,329,856
output head 622,329,856
total 8,190,735,360To check it against the real implementation without downloading 16 GB of weights, build the model on PyTorch's meta device, which gives every tensor its shape but allocates no memory:
import torch
from transformers import AutoConfig, AutoModelForCausalLM
config = AutoConfig.from_pretrained("Qwen/Qwen3-8B")
with torch.device("meta"): # shapes only: no download, no memory
model = AutoModelForCausalLM.from_config(config)
print(f"{model.num_parameters():,}")8,190,735,360When we ran it, the same formula reproduced Hugging Face's published totals to the parameter for Qwen3-4B as it stands, and for Qwen2.5-7B and Mistral-7B-v0.3 with QK-norm dropped (and Qwen2.5's query, key and value biases added). Try your own model's numbers:
- Attention per layer
- 41,943,296
- MLP per layer (SwiGLU)
- 150,994,944
- Total parameters
- 8,190,735,360
- MLP share of the total
- 66%
- Weights in bf16
- 16.4GB
Starts at Qwen3-8B's config.json. It assumes a SwiGLU MLP, RMSNorm and no biases, like most current open-weight models; set QK-norm to 0 for families without it.
Mixture of experts: total and active parameters
A mixture-of-experts (MoE) model replaces each layer's single MLP with many smaller ones, the experts, plus a router: a small matrix that scores the experts for each token and sends the token to the top few. Qwen3-30B-A3B's config has three fields that matter for the count beyond the ones above: num_experts: 128, num_experts_per_tok: 8 and moe_intermediate_size: 768, with a hidden size of 2,048 and 48 layers.
| per layer | formula | parameters |
|---|---|---|
| one expert | 3 × 2,048 × 768 | 4,718,592 |
| all 128 experts | 128 × 4,718,592 | 603,979,776 |
| router | 2,048 × 128 | 262,144 |
| attention (32 query heads, 4 KV heads, QK-norm) | 18,874,624 |
Add the embedding, output head and norms and the model totals 30,532,122,624 parameters. Count only the 8 experts each token passes through and it's 3,353,032,704. Those are the model card's "30.5B in total and 3.3B activated", and the "A3B" in the name.
The same config also has intermediate_size: 6144, which no layer uses, because every layer is MoE. Run the dense script or the calculator on it and you get 3,340,449,792: close enough to "3.3B active" to look right, and wrong for both numbers. Always check num_experts before trusting a dense count.
The two numbers answer different questions. Total parameters decide memory: any token can be routed to any expert, so all 128 have to be loaded, about 61 GB in bf16. Active parameters decide the compute each token costs, which here is close to a 3B dense model's. Against the dense Qwen3-8B, this model needs nearly four times the memory but does less than half the arithmetic per token.
What the count tells you
Weight memory is parameters × bytes per parameter. In bf16 (2 bytes) Qwen3-8B's weights are 16.4 GB (15.3 GiB), before any memory for the KV cache. Quantised checkpoints don't shrink evenly. Hugging Face's per-type counts for Qwen/Qwen3-8B-FP8 show 1,244,967,936 parameters still in bf16 (the embedding, output head and norms) and 6,945,767,424 in FP8, about 9.4 GB rather than the 8.2 GB that "one byte per parameter" suggests.
Compute per token follows from the count too. Kaplan et al. (2020) estimate a forward pass at about floating-point operations per token, where is the non-embedding parameter count: one multiply and one add per weight. A term that grows with context length comes on top. For Qwen3-8B that's about 13.9 GFLOPs per token; for Qwen3-30B-A3B, counting only active parameters, about 5.5. The output head's matrix multiply () is extra in both, because Kaplan's leaves out the vocabulary matrices. Simpler estimates use 2 × total parameters instead (16.4 GFLOPs for Qwen3-8B); the gap between the two conventions is just those matrices.
A shortcut from the same paper: for a classic transformer, . It assumes every head has its own keys and values and a two-matrix MLP four times as wide as the hidden size. For Qwen3-8B it gives 7.25B against the real 6.95B, 4.3% high: smaller attention (GQA) and a bigger MLP (SwiGLU at three times the hidden size) partly cancel. Good enough for a sanity check, not for a GPU budget.
Traps that throw the count off
Every row below is a real case we checked against the published files.
| trap | example | how far off |
|---|---|---|
assuming head_dim = hidden_size ÷ heads | Qwen3-0.6B: 1,024 ÷ 16 = 64, but the config says 128 | 88,083,968 too few |
| assuming every head has its own keys and values | Qwen3-8B counted as if it had 32 key/value heads | 905,969,664 too many |
| counting a tied output head | Qwen3-0.6B ties it: 596,049,920 in Transformers | 155,582,464 too many |
| biases the config doesn't mention | Qwen2.5-7B adds biases to Q, K and V in its model code | 129,024 too few |
| counting buffers as weights | GPT-2's file stores a 1,024 × 1,024 causal mask per layer | 12,582,912 too many |
| taking the vocabulary from the tokenizer | Qwen3's tokenizer has 151,669 entries; the matrices have 151,936 rows | 2,187,264 too few (Qwen3-8B) |
You can list them without downloading the weights. A safetensors file starts with 8 bytes giving the length of a JSON header, and the header lists every tensor's name, type and shape. Two small range requests per file fetch it; a checkpoint split across several files lists them in model.safetensors.index.json.
When to count by hand, and when not to
| you want to | do this |
|---|---|
| check a model card, or compare models before downloading | the config sum above, or the calculator |
| size a GPU | the count × bytes per parameter for each type in the checkpoint, then add the KV cache |
| get the exact number in code | model.num_parameters() on the meta device |
| read an MoE model | compare its total with a dense model's count for memory, and its active count for compute per token |
| trust the Hub's figure | fine for most models; check tied embeddings and stored buffers when it looks off |
Memory is where the count earns its keep. GPU sizing arithmetic turns it into a full budget: weights, KV cache, activations and headroom, on the cards you're likely to rent.
Questions people ask
Why is an "8B" model 8.19 billion parameters?
Names round. Qwen3-8B is 8,190,735,360 parameters, its card says 8.2B and its name says 8B. When the exact number matters, count from the config.
What's the difference between total and active parameters?
In a mixture-of-experts model, total counts every expert and decides memory; active counts only the experts one token uses and decides compute per token. Qwen3-30B-A3B is 30.5B total and 3.3B active.
Does the parameter count include the embeddings?
The headline number usually does. Scaling-law papers often quote non-embedding parameters instead, which for Qwen3-8B is 6.95B of the 8.19B.
How much memory does an 8B model need?
About 2 bytes per parameter in bf16, so 16.4 GB for Qwen3-8B's weights, plus the KV cache and working memory. An FP8 checkpoint is about 9.4 GB, because the embedding and output head stay in bf16.
Sources & further reading
- Qwen3 Technical ReportPaper · Yang et al. · Qwen Team · 2025The running example's architecture. Section 2 lists what each layer contains (GQA, SwiGLU, RoPE, RMSNorm, QK-norm, no QKV bias); Tables 1 and 2 give layers, heads, tied embeddings and experts for every model in the family.
- Scaling Laws for Neural Language ModelsPaper · Kaplan et al. · OpenAI · 2020Section 2.1 and Table 1 give the 12·L·d² shortcut for non-embedding parameters, the 2N FLOPs per token estimate, and why the authors leave embeddings out of N.
- GQA: Training Generalized Multi-Query Transformer Models from Multi-Head CheckpointsPaper · Ainslie et al. · Google Research · 2023EMNLP 2023. Grouped-query attention, the reason the key and value projections in most current models are a fraction of the query projection's size.
- GLU Variants Improve TransformerPaper · Noam Shazeer · Google · 2020Two pages introducing SwiGLU, the three-matrix MLP behind most of an open-weight model's parameters.
- Qwen3-8B config.jsonDocs · Qwen TeamThe file this post counts from. Open it next to the worked example; the model card on the model's main page gives the 8.2B and 6.95B figures we check against.
- Qwen3 model configurationDocs · Hugging FaceWhat each config field means in the Transformers implementation, including the default for head_dim and how num_key_value_heads selects MHA, GQA or MQA.
- safetensorsDocs · Hugging FaceThe Format section: an 8-byte header length, then a JSON header listing every tensor's name, dtype and shape. It is how you can count a checkpoint without downloading its weights.
