Count Parameters and Attention Work

Under a fixed total head width, changing head count does not multiply every parameter and compute cost by the number of heads. Count projections, score entries, arithmetic terms, and stored tensors under explicit assumptions.

The phrase “more heads” does not by itself determine parameter count or computation. We must also know the per-head widths, total projected widths, bias convention, sequence length, and which tensors are materialized.

General Bias-Free Parameter Count

With equal widths across heads, the projection matrices contain

WQ:dmodel(hdk),WK:dmodel(hdk),WV:dmodel(hdv),WO:(hdv)dmodel.\begin{aligned} W_Q &: d_{model}(hd_k),\\ W_K &: d_{model}(hd_k),\\ W_V &: d_{model}(hd_v),\\ W_O &: (hd_v)d_{model}. \end{aligned}

The total is

2dmodelhdk+2dmodelhdv.2d_{model}hd_k+2d_{model}hd_v.

Separate per-head matrices and combined matrices have the same number of entries when they represent the same widths. Combining is an implementation arrangement, not parameter sharing among heads.

Fixed Total Width Makes the Standard Count Independent of Head Count

Under the common convention

hdk=hdv=dmodel=d,hd_k=hd_v=d_{model}=d,

the count becomes

d2+d2+d2+d2=4d2.d^2+d^2+d^2+d^2=4d^2.

Changing hh while reducing each head width to keep the total widths fixed does not change this projection count. For d=512d=512, one head of width 512 and eight heads of width 64 both use 4(512)2=1,048,5764(512)^2=1{,}048{,}576 bias-free projection parameters.

They do not compute the same function: the one-head version has one softmax distribution, while the eight-head version has eight.

If an implementation uses projection biases, add their exact lengths. Under one common convention, Q, K, V, and output biases add hdk+hdk+hdv+dmodelhd_k+hd_k+hd_v+d_{model} entries. Some models omit selected biases, so the convention must be stated rather than assumed.

Dense Scores Depend on Head Count and Sequence Length

Each head forms T2T^2 scores per batch item. Across BB items and hh heads, the score tensor contains

BhT2BhT^2

entries. The approximate multiply-add work for scores and weighted values is

O(BhT2dk)+O(BhT2dv).O(BhT^2d_k)+O(BhT^2d_v).

When hdkhd_k and hdvhd_v remain fixed, these expressions are approximately independent of how the total width is split among heads. The number of separate score matrices still grows with hh, which can affect kernels, memory layout, and overhead even when the leading arithmetic count is similar.

Compare Counts Under Explicit Conventions

Let dmodel=12d_{model}=12, T=100T=100, B=2B=2, with no biases.

hhdk=dvd_k=d_vTotal Q/K/V/O parametersScore entries
11257620,000
3457660,000

The score entry count triples because there are three separate attention maps. Each map's score dot products are only one third as wide, so the leading score arithmetic remains comparable under this fixed-total-width setup. Entry count, arithmetic, memory, and wall-clock time are related but not interchangeable. The first row uses 4(12)2=5764(12)^2=576 projection parameters and 2(1)(100)2=20,0002(1)(100)^2=20{,}000 score entries. Replacing one head with three gives 2(3)(100)2=60,0002(3)(100)^2=60{,}000 score entries while leaving the projection count fixed.

Q1. Count standard projection parameters

A bias-free layer uses dmodel=16d_{model}=16, h=4h=4, and dk=dv=dmodel/h=4d_k=d_v=d_{model}/h=4. How many entries are in all Q, K, V, and output projection matrices?

Compute it first, then check your number.

Hint
Each combined projection is 16×1616\times16, and there are four of them.
Solution
4(162)=4(256)=1,0244(16^2)=4(256)=1{,}024 entries.
Not attempted
Review

Not marked done.

Never Compare Head Counts Without Widths

“Eight heads cost eight times as much as one” is false under the common fixed total width. “Eight heads are free” is also false: eight dense maps are stored, and implementation overhead changes. A useful comparison states parameter widths, tensor entries, arithmetic, and measured runtime separately.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.