A transformer holds two kinds of knowledge. Attention reads what is in the context. The weights hold
what is not: that a thermostat takes a temperature, that living room is a place and
warm white is a colour. In a standard transformer those facts live in the feed-forward
layers, which act as key-value memories, and every token multiplies through all of them.
Needle replaced its feed-forward layers with the Hadamard MLP, so the facts need another
home. The engram is that home: hashed n-gram tables that a token reads by index.
Two kinds of parameter
A parameter in a matrix is read by every token. A parameter in a table is read by the tokens that need it.
For a matrix W∈Rm×n and a token x, the
product Wx costs mn multiply-adds and moves mn weights from memory, every token, whatever the
token is. For a table T∈RS×c read at row i,
the fetch T[i] costs c numbers moved and no multiply-adds, and it costs the same for any S:
cost(Wx)=mn per token,cost(T[i])=c per token, for any S.
Capacity in a matrix costs every token. Capacity in a table costs only the tokens that read it. So
the design is to keep the matrices small and let the tables be large. The engram is the table: a
conditional memory of hashed n-gram rows, in Needle's case sized for a phone.
Two kinds of parameter. Each token sweeps through the whole matrix on the left. On the right the
token picks six rows and reads only those. The table could be a thousand times taller and the token
would still read six rows.
The lookup
There are two ways to find a row in a memory. The first is to search for it. Attention does this,
and so do product-key memories, a design where a large table of rows is searched by comparing the
token against a learned key for every row. The token is turned into a query, the query is compared
with every key, and the best matches are read. The comparison is a matrix product whose cost grows
with the number of rows, and the keys are parameters that every token multiplies through, so a
search is a matrix in disguise and pays the matrix's price.
The second way is to compute an address. A token is a word or a piece of a word, and the vocabulary
is the fixed list of tokens the model knows, each with its own integer id. Fix a function of those
ids, with no learned part, that maps any short run of tokens to a row number. A function like that
is a hash. It scrambles its input into one number in a fixed range, so that similar inputs land on
unrelated rows.
With a hash the row is known before the model has done any arithmetic. Finding it costs a handful of
integer operations however many rows there are, and the table can grow without the lookup getting
slower.
The engram takes the second way. Its price is that the address is fixed, so the model cannot decide
which n-grams share a row and which do not. An n-gram is a run of n consecutive tokens, so a
bigram is two tokens in a row and a trigram is three. The rest of this section and the next are
about how the model lives with that, and there is a bit of arithmetic to get through first.
A table needs a row index, and the index has to come from the tokens with no learned computation in
the way. The engram hashes the last few tokens. For a position n with tokens tn,tn−1,… and an order o, the row is
io(n)=mix(tn,tn−1,…,tn−o+1)modS.
mix is the FNV hash, a few integer operations per token. Start from a seed a,
then for each token in the window:
a←(a⊕tj)⋅16777619(mod232),then a←a⊕(a≫15).
FNV is the Fowler-Noll-Vo hash, and its two operations do two different jobs. The xor, written ⊕,
combines two binary numbers bit by bit, a bit being one binary digit, and gives a one wherever just
one of them has a one. It folds the next token id into the running state, so every token in the
window leaves its mark.
The multiplication by 16777619, the standard 32-bit FNV prime, carries the low bits upward across the
whole word, so two token ids that differ in one bit end up in states that differ nearly everywhere.
The final step xors the state with itself shifted down by 15 bits, written a≫15. That mixes the
well-scrambled high bits back into the low ones, so the reduction modS, which keeps only the remainder on
division by the table size, inherits the scrambling.
The result is that neighbouring n-grams land in rows that have nothing to do with each other, which
is what you want when many of them must share a table. The seed a is what differs between the
three heads of an order. A different seed gives a different, equally scrambled, map from n-grams to
rows. So two n-grams that land on the same row in one head, which is a collision, almost never land
together in another.
Needle uses orders 2 and 3: one row indexed by the last two tokens, one by the last three. Each
order has three heads with different seeds, so a site has six tables, each with S=18,432 rows of 128
numbers. The six rows a token fetches are concatenated into one vector
e=[T1[i1],T2[i2],…,T6[i6]]∈R768,
which is exactly the model width. A row that would reach past the start of the sequence is zeroed.
The lookup. The last two tokens and the last three tokens are each hashed to a row of a table.
Different heads use different seeds, so a collision in one table is not a collision in another. The
rows are fetched by index, and nothing is multiplied to find them.
The vocabulary has 8,192 tokens, so there are 67 million possible bigrams and a table has 18,432
rows. Most rows are shared by many n-grams. Three things make that work:
the rows are learned, so a row becomes whatever the n-grams that land on it need, and the common n-grams win;
the three heads of an order have different seeds, so two n-grams that collide in one head almost
never collide in the other two, and the concatenated e still tells them apart;
the gate below decides, per token, how much of the fetched row to believe.
This is the hashing trick applied to n-grams, close to hash embeddings with several
hash functions per key, and the table read is the gather that product-key memories also rely
on, without the learned key search.
The gate
The fetched vector e is projected to a key and a value, two ordinary 768×768 matrices:
k=Wke,v=Wve.
The value is smoothed over earlier positions with four learned taps at stride 3, so the value at a
position also carries the rows fetched three, six and nine tokens back:
vn←∑j=03τj⊙vn−3j.
Two words from above need unpacking. A transformer is a stack of blocks, and each block reads one
running vector per token, adds its own contribution, and passes the vector on. That running vector
is the residual stream, and by the middle of the stack it is the model's summary of everything it
has worked out about the context so far. A site is one point in the stack where an engram is wired
in and adds to that stream.
Now the question is how much of that value to trust. The natural test is whether the fetched row
fits the context the model is already in, and the residual stream x at the site is
that summary. Compare the row's key with the
stream as two directions in R768, the space
of vectors with 768 numbers. If they point the same way the row is about what the stream is about.
If they point away from each other the row is about something else, which is what a hash collision
looks like from inside the model.
The quantity that measures this is the cosine of the angle between the two vectors, one when they
point the same way, zero when they are at right angles, and minus one when they oppose.
Rms-normalising both is how to get at it. The rms of a vector is the root mean square of its
entries, meaning you square every entry, average the squares and take the square root. Dividing by
it rescales the vector to length d without
changing its direction. After that x^⋅k^=dcosθ, and dividing by d leaves
dcosθ, about 28cosθ at d=768.
A sigmoid squashes any number into the range zero to one, near one for large positive inputs and
near zero for large negative ones. A sigmoid of dcosθ is therefore near one when the
directions agree and near zero when they oppose, and the factor of d makes the
switch between the two happen over a small change of angle rather than a gentle slope. The gate is
that sigmoid.
Then the gate compares the key with the residual stream x of the block the site belongs to, and
adds the value in proportion:
α=σ(dx^⋅k^),x←x+αv,x^=rms(x)x,k^=rms(k)k.
If the key points the way the stream already points, α is near one and the value is written in.
If it does not, α is small and the row is mostly ignored. A hash collision is a key that does not
agree with the stream, and a gate that stays closed. The model never has to know which rows are
shared; it learns keys that only open for the right context.
The gate. Each fetched row is projected to a key and a value. The key is compared with the residual
stream, and the closer they point the more of the value is added. A wrong row, fetched because two
n-grams share a slot, is a key that does not agree, and the gate stays closed.
Where the sites sit
Needle 3 has five sites, at layers 3, 7, 11, 15 and 19 of its 20 blocks. Every site has its own six
tables and its own Wk,
Wv and
taps. The hash indices depend only on the tokens, so they are the same at all five sites; what
differs between sites is the rows, and what the stream looks like when the key is compared with it.
A site fires at the start of its block, before that block's attention, so the attention and the
mixer of the block work on a stream that already contains the fetched facts.
The sites belong to blocks, and the intelligence ladder keeps them with their blocks.
needle build --layers 8 keeps the sites whose layers survive and drops the other tables
from the file, so a shallower model is also a smaller file.
The cost
Per site, with d=768:
per site
parameters
multiply-adds per token
numbers moved per token
six tables, 6×18,432×128
14.16M
0
768
Wk and Wv
1.18M
1.18M
1.18M
taps and gate
3K
4K
3K
Over five sites the engram is 76.7M parameters, 70.8M of them in tables, for 5.9M multiply-adds per
token. The dense feed-forward layers it replaces, twenty of them at d=768, would be 94M parameters
for 94M multiply-adds per token. The tables hold 92% of the engram's parameters and cost
nothing to multiply; a token reads six rows of the 110,592 in a site, 768 numbers out of 14 million.
Results
The runs below train on a mixture built around SYNTH, a corpus where every document has a query, a
reasoning trace and an answer. The loss is cross-entropy, the model's average surprise at the true
next token, measured in nats. A model that gives the true token probability p is surprised by
−logp nats, so a gap of 0.01 nats means one model gives the true token about 1% more probability than
the other.
Four questions, each answered with one run per setting on the SYNTH mixture, validation
cross-entropy in nats per token, general text and tool calls scored separately.
Does it help. The same model trained twice for 4,000 steps, once with two memory
sites of 8,192 rows and once without, sizes at 16 bits:
file size
added
validation CE
loss change
without the engram
71 MB
2.204
with the engram
90 MB
+27%
2.162
−1.9%
1.9% of the loss for 27% more file, almost all of it table rows that no token multiplies.
How many rows. An 18-layer, d=1024 model with sites at layers 2 and 10, trained for
3,000 steps, tables of three sizes:
rows per table
file size
added
text CE
change
tool-call CE
change
8,192
185 MB
2.022
0.615
18,432
227 MB
+23%
1.989
−1.6%
0.601
−2.3%
28,672
269 MB
+45%
1.977
−2.2%
0.595
−3.3%
Where the first site sits. The same model with the second site fixed at layer 10
and the first moved:
first site at layer
text CE
tool-call CE
1
2.037
0.623
2
2.022, 2.026
0.615, 0.619
3
2.030
0.619
4
2.036
0.621
5
2.034
0.622
Layer 2 was run twice; the two runs differ by 0.004, 0.2% of the loss, which is the noise floor for
this series. Layer 1 is the only clearly bad position, 0.7% above layer 2; from layer 2 on, the
differences are within that noise.
Both panels are the 18-layer model at 3,000 steps. On the left, more rows per table keeps paying,
2.2% of the loss from 8,192 to 28,672 rows. On the right, which layer holds the first site moves the
loss by 0.7% at most, and two identical runs at layers 2 and 10 differ by 0.2%. Layer 1 is the only
position to avoid.
Sites against rows. At 20 layers, two layouts of the tables:
sites × rows
file size
added
text CE
change
tool-call CE
change
5 × 28,672
472 MB
1.976
0.746
10 × 18,432
576 MB
+22%
1.966
−0.5%
0.675
−9.6%
More sites with fewer rows each beat fewer sites with more rows, on tool calls by a wide margin, for
a file 22% larger. Needle 3 ships five sites of 18,432 rows, where the tables at 2 bits are already
two thirds of a 29 MB file; doubling the sites would add about 19 MB.
What the bytes buy, against the other knobs in the same 18-layer series. From 8,192 to 28,672 rows
adds 45% to the file for 2.2% less loss. Widening the value heads from 64 to 128 adds 46%
for 6.5% less loss, nearly three times as much for the same bytes. But the value bytes are
multiplied by every token, and the table bytes are not. Table rows are the cheapest bytes to run,
not the cheapest bytes to store, and Needle spends its byte budget on both.
On the device
In the shipped needle3.cact the five table tensors are 2-bit: 110,592 rows of 128 each,
17.7 MB of the 29 MB file. A token reads 30 rows, 3,840 numbers, 960 bytes at 2 bits, 32 bytes per
row. Each row is decoded on fetch through a 4-entry codebook and the six rows are concatenated
straight into e. The hash is a few integer operations per table. Wk and
Wv are
2-bit as well, and the engine runs both projections from one quantised copy of e. The projected
value is kept in an int8 cache per site, so the taps read the values three, six and nine tokens back
instead of recomputing them.
Two words in that paragraph need unpacking. At 2 bits each number in a table is stored as one of
four levels, and the codebook is the list of the four values those levels stand for, so decoding a
row is looking each code up in a list of four. An int8 cache holds numbers as 8-bit integers, which
gives 256 levels, enough for a value that is only going to be added to the stream.
The dense feed-forward layer, had it stayed, would have been 94M weights moved per token. The engram
moves 5.9M through its projections and 3,840 through its tables. The rest of the tables stay where
they are until a token asks for them.
What it gives up
A row is indexed by the surface form of an n-gram. set the lights and turn on the
lights are different rows; a paraphrase is a different key, and the model has to learn the
fact twice or let attention carry it.
Rows collide. The gate and the multiple heads make collisions cheap, not free; a rare n-gram that
shares a row with a common one mostly reads the common one's content.
The tables are two thirds of the file. On a device where flash is the limit and the model is never
sliced, that is the cost of capacity that costs no arithmetic.
The projections are ordinary matrices. 8% of the engram's parameters still cost 5.9M
multiply-adds per token.
The tables hold what the training data put in them. The attention-only study found that what
a small feed-forward layer contributes is mostly parameter count, and the engram is the same bet
made in a cheaper place; whether that holds for knowledge-heavy text at larger scale is the open
question it left.