How many parameters does a multi-head self-attention layer with dmodel=512 and h=8 heads have (with biases)? Does the number of heads change the count? How many does a full transformer encoder layer with feed-forward width dff=2048 have?

Sequence models and transformers, Attention
Level L2 Elementary. Length Short. Nature Mat Mathematical.

What's being asked

Whether you know the shapes of the per-head projections, and that heads split dmodel rather than multiply it.

Key idea

Each of WQ,WK,WV,WO (the query, key, value and output projections) is d×d with a bias of size d, where d=dmodel. The h heads, each of size dk=d/h, are just slices of these matrices. So the count is 4(d2+d), independent of h.

Steps

  1. Per head: WQ(i),WK(i),WV(i)∈R512×64, since dk=512/8=64. Concatenating 8 heads gives 512×512 for each of Q,K,V.
  2. Output projection: WO∈R512×512.
  3. Total: 4(5122+512)=4(262,144+512)=1,050,624.
  4. Changing h changes how the 512 dimensions are split, not the count.
  5. Encoder layer: the feed-forward network (two linear layers, 512→2048→512) has 2⋅512⋅2048+2048+512=2,099,712 parameters. The two layer norms have 2⋅2⋅512=2048 (a γ and a β each). Total: 1,050,624+2,099,712+2048=3,152,384, about 12d2.

Common mistakes

How long your answer should be

4–5 lines of arithmetic.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: What are the time and memory costs of one self-attention layer for sequence length n and model dimension d? For n=4096 and d=1024, which part dominates?
Next in Sequence models and transformers: A language model assigns probabilities 0.5, 0.25, 0.125 and 0.5 to the four tokens of a test sentence (each conditioned on the tokens before it). What is the perplexity? What is the perplexity of a model that is uniform over a vocabulary of size V?