#223GPTConfig.head_size with the divisibility checkEasy

GPTConfig.head_size with the divisibility check

Background

GPT-2's config exposes a derived head_size — the per-head dimension — computed from the embedding width and the number of heads:

head_size=n_embdn_head\text{head\_size} = \frac{n\_embd}{n\_head}

It must divide evenly (the heads concatenate back to n_embd), so the config asserts n_embd % n_head == 0. For GPT-2 small: 768 / 12 = 64.

Problem statement

Implement head_size(n_embd, n_head) returning n_embd // n_head, but raising ValueError when n_head does not divide n_embd.

Input

  • n_embdint, embedding/hidden dimension.
  • n_headint, number of attention heads.

Output

Returns an int n_embd // n_head; raises ValueError if n_embd % n_head != 0.

Examples

Example 1 — GPT-2 small

Input:  n_embd = 768, n_head = 12
Output: 64

Example 2 — invalid

Input:  n_embd = 768, n_head = 5
Raises: ValueError

Constraints

  • Validate n_embd % n_head == 0; otherwise raise ValueError(...).
  • Return the integer head size.

Notes

  • This mirrors the @property head_size on GPTConfig — one number that every attention module reads, so the check belongs here.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • GPT-2 small 768/12 (example)
  • Single head returns n_embd (reference)
  • Non-divisible raises ValueError (sample)