Skip to content
INDIE / MACHINE
MATHLESSON 3 OF 67 MIN

Hashing n-grams into tables

The multiplicative-XOR hash, why each head's table has a prime size, why several heads, and how big the tables get.

STEP 1 / 7QUIZ 0 / 2
01 /

Too many n-grams for an exact table

N-Grammer could afford an exact table because it paired a few hundred cluster ids. Engram keys on real tokens. With about 98,600 compressed ids:

98,627² ≈ 9.7 billion bigrams, 98,627³ ≈ 9.6 × 10¹⁴ trigrams
Almost all of them never occur. An exact table would be mostly empty rows.

So Engram hashes. Every n-gram gets squeezed into a fixed table of M rows. Two different n-grams can land on the same row - a collision - and the design is about making collisions rare and survivable.

02 /

The hash

Each layer draws one odd 64-bit multiplier per position in the window. For the n-gram ending at token t:

mix = t₀·m₀ ⊕ t₋₁·m₁ ⊕ … ⊕ t₋₍ₙ₋₁₎·mₙ₋₁
⊕ is bitwise XOR. In the demo the products stay inside int64: multipliers are below (2⁶³ − 1) / vocab.
row = mix mod pⱼ for head j
Before the first token, the missing history is the pad id.

The bigram and trigram at a position share the first two terms of the XOR: the trigram is the bigram's mix XOR one more product, which is why the code builds them in one loop.

03 /

Watch it collide

Real tables have about 646,000 rows per head. These have 31 to 43, so collisions show up in a single sentence.

INTERACTIVE / MULTI-HEAD HASHING
Click an n-gram below to see its hash.
HEAD 0 · 31 ROWS
HEAD 1 · 37 ROWS
“the cat” at position 1
mix = 429·7919 ⊕ 866·104729
    = 89403409
head 0: 89403409 mod 31 = row 29
head 1: 89403409 mod 37 = row 13

No two different n-grams here collide in both heads.

Two hash heads per order with tiny prime tables (31 and 37 rows for bigrams, 41 and 43 rows for trigrams), so collisions are easy to see. A cell with two different n-grams is a collision in that head; the layer still reads the other head's row, so an n-gram only loses its memory completely when every head collides.
04 /

Why several heads, why primes

Each order has K heads (8 in the paper and the demo), and each head has its own table with a different prime number of rows. The demo picks them by walking up from the target size and skipping any prime already taken, across both orders and every Engram layer.

A row of one head can be shared by several n-grams, but the memory vector concatenates all heads, so two n-grams only read identical memory if they collide in every head at once. If the heads behaved like independent uniform hashes, two given n-grams would share all K rows with probability 1 / (p₁·p₂·…·p_K). Different moduli are what keep the heads from colliding on the same pairs: with equal table sizes, two mixes that differ by a multiple of p would collide in every head together.

global row = row + p₀ + p₁ + … + pⱼ₋₁
Head 3's rows start where head 2's end. Offsets are a fixed buffer, so the layer is one embedding gather.
05 / CHECK YOURSELF

Quick check

Bigram ids (3, 5), multipliers m₀ = 2, m₁ = 7, head prime 11. Which row? (current token 5, previous 3)

06 /

How big are the tables?

The demo config: 2 orders × 8 heads, each table a prime just above 5 × 129,280 = 646,400 rows, each row 512 / 8 = 64 numbers. Hyperfield's prime search gives the same sizes as the demo's:

DEMO LAYERSMALLEST / LARGEST PRIMEROWSPARAMETERS (×64)
1646,403 / 646,61910,344,164662,026,496
15646,631 / 646,87910,348,242662,287,488

Each token reads 16 rows of 64 numbers per layer - 1,024 values out of 662 million. That ratio is the whole pitch: parameters that cost memory, but almost no compute.

07 / CHECK YOURSELF

Quick check

Two different trigrams collide in head 0 but not in head 1. What does the layer read for them?

LESSON 3 COMPLETE

Up next: The gate and the short conv