28/42
Part 5 of 6 · The check and the projectionLearn

Raise ValueError when C doesn't split into H heads

The constructor has one job before it builds anything: check the head count.

if n_embd % n_head:
    raise ValueError("n_embd must be divisible by n_head")

The head size d=C/Hd = C / H has to be a whole number, and PyTorch won't check it for you. So check it first thing in __init__.

Raise ValueError, not assert. The test expects a ValueError, and python -O strips asserts out.