Model Width, Head Count, and Head Width

Model width, head count, query-key width, and value width are separate design choices. Derive every projection shape, identify the common equal-width convention, and distinguish mathematical requirements from framework restrictions.

Multi-head attention uses several widths with different jobs:

  • dmodeld_{model} is the feature width entering and leaving the layer;
  • hh is the number of attention heads;
  • dkd_k is the query and key width inside each head;
  • dvd_v is the value and head-output width inside each head.

These symbols describe an architecture. They are not automatically linked by one equation.

The Common Equal-Split Configuration

Many Transformers choose

dk=dv=dmodelh.d_k=d_v=\frac{d_{model}}{h}.

Then the concatenated head width is

hdv=dmodel,hd_v=d_{model},

and dmodeld_{model} must be divisible by hh. For example, with dmodel=512d_{model}=512 and h=8h=8, each head commonly uses dk=dv=64d_k=d_v=64.

This is a design convention, not the definition of multi-head attention. A valid layer may use hdvdmodelhd_v\ne d_{model} and let WOW_O map the concatenated width back to dmodeld_{model}. Query-key width and value width may also differ.

Separate Projection Shapes

For each head rr,

WQ(r),WK(r):dmodel×dk,WV(r):dmodel×dv.W_Q^{(r)},W_K^{(r)}:d_{model}\times d_k,\qquad W_V^{(r)}:d_{model}\times d_v.

The head result has shape

Z(r):B×T×dv.Z^{(r)}:B\times T\times d_v.

Concatenating hh results along their feature axes gives

Z:B×T×(hdv),Z:B\times T\times(hd_v),

so the output projection has shape

WO:(hdv)×dmodel.W_O:(hd_v)\times d_{model}.

Compare Two Valid Configurations

Configurationdmodeld_{model}hhdkd_kdvd_vConcatenated widthWOW_O
equal split123441212 by 12
wider total values123261818 by 12

Both are mathematically valid. The second uses a larger combined value width and therefore a larger WVW_V total projection and output projection. Calling both “three-head attention” does not make their parameter counts equal.

Divisibility Depends on the Implementation Choice

If an implementation reshapes one dmodeld_{model}-wide vector directly into hh equal heads, then dmodel/hd_{model}/h must be an integer. If it first projects into an explicit total width hdkhd_k, only that total projected width needs to split evenly under the chosen equal-head convention.

Framework APIs often impose the common equal split for convenience. When an API rejects a configuration, distinguish a mathematical incompatibility from an interface constraint.

Q1. Derive the joined value width

A layer has h=6h=6 heads and value width dv=10d_v=10 per head. Its model width is 48. What shape must WOW_O have, ignoring bias vectors?

Choose one

Select one choice, then check.

Hint
The input width of WOW_O is hdvhd_v.
Solution
hdv=6(10)=60hd_v=6(10)=60, so WOW_O has shape (60,48)(60,48).
Not attempted
Review

Not marked done.

Write the Configuration Before the Shapes

A shape trace is ambiguous if h,dk,dvh,d_k,d_v, projection convention, and axis order are missing. State those choices first. Only then is a reshape such as (B,T,hdk)(B,T,h,dk)(B,T,hd_k)\to(B,T,h,d_k) justified.

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.