Skip to content
INDIE / MACHINE
MATHLESSON 4 OF 66 MIN

Bigram ids and the embedding table

How (previous id, id) pairs become exact rows in one embedding table, the start-of-sequence row, and how many parameters the table costs.

STEP 1 / 6QUIZ 0 / 3
01 /

Look one token back

After snapping, every position has H ids. For a bigram we pair each id with the id at the previous position, in the same head. Shift the ids right by one:

prev[t] = ids[t − 1], prev[0] = K
Position 0 has no previous token. It uses the extra id K, which no real centroid has.

This is why the table has K + 1 possible previous ids but only K current ids.

02 /

From a pair to a row

All heads share one table. Head h owns a contiguous block of (K+1)·K rows, and inside it the pair picks the row:

row = (h·(K+1) + prev)·K + id
In code: prev * K + ids + h * (K + 1) * K - the same number, rearranged.
INTERACTIVE / BIGRAM ROW FINDER

row = (h·(K+1) + prev)·K + id = (1·5 + 2)·4 + 3 = 31

HEAD 0 · ROWS 0-19
id 0id 1id 2id 3prev 00123prev 14567prev 2891011prev 312131415prev ∅16171819
HEAD 1 · ROWS 20-39
id 0id 1id 2id 3prev 020212223prev 124252627prev 228293031prev 332333435prev ∅36373839

Total: 2 × 5 × 4 = 40 rows.

Each head owns a block of (K+1)·K rows: one per (previous id, id) pair, plus K rows for “no previous token” (∅). The highlighted cell is the row this head reads.
03 / CHECK YOURSELF

Quick check

K = 4. Head 1, previous id 2, current id 3. Which row?

04 / CHECK YOURSELF

Quick check

Why does each head need (K+1)·K rows rather than K·K?

05 /

How big is the table?

Each row has dh = w / H numbers and there are H·(K+1)·K rows, so the H cancels: the table costs (K+1)·K·w parameters no matter how many heads you use.

INTERACTIVE / TABLE SIZE
ROWS = heads·(K+1)·K
1,052,672
ROW WIDTH = w / heads
16
PARAMETERS = (K+1)·K·w
16,842,752

A plain token-bigram table at the same width would need vocab² · w = 8192² · 256 = 17,179,869,184 parameters - about 1,020× more. Notice the head count cancels out: more heads means more, narrower rows.

The preset is Yumon cp1's real config (8 layers, width 256, vocab 8192, one N-Grammer layer with K = 256 and 16 heads). The whole model is 27.2M parameters.

For Yumon cp1 that is 16,842,752 parameters, about 62% of the 27.2M model - yet each token only reads 16 rows of 16 numbers. Sparse lookups like this are cheap at inference time; the paper's pitch is exactly that trade: lots of parameters, little compute.

06 / CHECK YOURSELF

Quick check

K = 4, H = 2, w = 8. How many rows does the table have?

LESSON 4 COMPLETE

Up next: Coding it: PyTorch to Hyperfield