Knowledge editing in n-gram space

Writing knowledge into Qwen4's n-gram memory without gradients

Qwen4's n-gram memory is a table of 20 billion parameters in which every pair and triple of tokens has its own entry. A linear fit over the entries of n-grams the model already knows gives a vector for a property such as “Olympic athlete”. Adding that vector to another entry makes the model treat that n-gram as an Olympic athlete.

The table is a promising place to edit knowledge. Finding an n-gram's entry takes one hash of its tokens, which is $O(1)$. There's no searching through the network for where a fact is stored. Most bigrams and trigrams refer to one thing, like “Alan Turing”, so changing an entry only affects text that mentions it. The table is also read before the second layer, and every layer after that treats the edited entry like any other.

The n-gram table in Qwen4

Qwen4-35B-A3B has an n-gram table. At every position it takes the last two tokens and the last three, turns each into row numbers with a fixed hash, and reads those rows from a table of 160 million rows, more than half of the model's 35 billion parameters. What it reads is added to the model's state before its second layer.1

The n-gram table at one token. Hover or tap a token. “Schwarzenegger” is three tokens, so the trigram at “egger” is the whole surname. The 8 rows of the bigram and the 8 rows of the trigram are joined into one vector, which gives a key and a value. The key is compared with the hidden state to give a gate, and the value times the gate is added to the hidden state. Real values from Qwen4-35B-A3B.1

Knowledge editing

Write $\boldsymbol r_p$ for the entry of a pair of tokens $p$, its 8 bigram rows joined into one vector. A two-token name such as “Alan Turing” is one pair, so it has one entry.

1. Find the vector for a property. Take names the model knows, some with the property and some without. For each name $n$, $\boldsymbol r_n$ is its entry, and $z_n$ is 1 if it has the property and 0 if not. Pick a vector $\boldsymbol b$ for all names and a vector $\boldsymbol k$ for names with the property, so that $\boldsymbol b + z_n \boldsymbol k$ is as close as possible to each entry:

$$ \min_{\boldsymbol b,\,\boldsymbol k} \; \sum_n \big\lVert \boldsymbol r_n - \boldsymbol b - z_n \boldsymbol k \big\rVert^2 $$

For “Olympic athlete” the names with the property are Olympic champions, and the rest are other famous people. The solution makes $\boldsymbol b$ the average entry of the other famous people, and $\boldsymbol k$ the average entry of the champions minus $\boldsymbol b$. So $\boldsymbol k$ is what the champions share that other famous people don't.

2. Add it to an entry. Add $\boldsymbol k$, times a strength $\alpha$, to the entry and write the rows back:

$$ \boldsymbol r_p \;\leftarrow\; \boldsymbol r_p \;+\; \alpha\, \boldsymbol k $$

There's no training involved. The vector is fitted from rows the model already has and added to one entry, and the model only reads that entry when the pair comes up in text.

Results

Related work

Engram. The table is the Engram design from DeepSeek (Cheng et al., 2026). Three recent papers write new knowledge into Engram tables: EngramEdit, User as Engram, and Writability and the Pathway. All three get the new rows by gradient descent on target answers.

Activation steering. ActAdd (Turner et al., 2023) and CAA (Rimsky et al., 2023) also add a vector found without training, but they add it to the hidden state at every position, so it changes everyone the model talks about. With the billionaire vector used this way the model says “Barack Obama is the co-founder and CEO of Microsoft.”

Model editing. ROME and MEMIT (Meng et al., 2022, 2023) also edit one subject at a time. They first have to find where the subject is stored and then optimise the new value. In the table, where an n-gram is stored follows from its tokens.

Token embeddings. They can be edited by the same addition, but their rows are per token. Bruno Mars, a Mars bar and the Mars rover all read the row for “ Mars”, so an edit to one reaches all of them.

  1. The table has 16 heads, 8 for bigrams and 8 for trigrams, each with about 10 million rows of 128 numbers and its own hash. Many n-grams share a row in any one head, but two n-grams almost never share rows in all 8, so the 8 rows of a bigram, 1,024 numbers, work as its own entry. The hash only looks at token ids, so an entry can be found, read and rewritten without running the model. The row number is each token id multiplied by a fixed odd constant, combined by XOR and taken modulo the head's size, a different prime for every head. Each row passes through two learned linear maps, a value (what to add) and a key (when to add it). The gate is a sigmoid of how well the key matches the hidden state. The hidden state is split into 4 lanes with one gate each, and the figure shows the largest of the four. A short causal convolution then mixes the gated values of nearby positions. ↩