LLMs n-gram - new way to augment inference with knowledge

So far, everything around augmenting LLMs with knowledge revolves around RAG: embeddings, knowledge graphs, or a combination of both. Retrieval happens outside the model, and the results are glued back in as context.

This time there is something actually new — an approach that works at the inference level itself, transformer-level to be precise.

Qwen3.8 next-generation architecture: n-gram augmentation at the transformer inference level

Qwen calls this N-gram Embedding. DeepSeek calls its more elaborate version Engram / conditional memory.

At the moment of writing this there are two technologies - both open-weigh which support this: Qwen 3.8 Flash Next and Deepseek v4.1 Flash.

To put simply it's key-value list, with proper value injected into the transformer's attention layer which can be quickly looked up and is computionally cheap.

What key to lookup are currently so called bigram and trigram - literally last two and three tokens of the input sequence.

How is the n-gram learned? It's learned from the training data, like usually. The main point is that training starts with KV cache seemingly initialized with random vectors for values. During training optimizer keeps track if injection has helped or not and adjusts the KV cache so its information is more helpful in predicting proper tokens. It's like saving predictions what could be said afterward.

What I consider another cool trick which could propagate to other areas is how they approached the KEY part of KV.

So we have 3 or in case of Deepseek even 4 (or in theory more) tokens of the input sequence used as the key.

This would lead to absurdly large amount of keys (let's say 250,000 tokens possible so 250,000^3. To reduce this to just millions they used multiple independent hash heads, instead of trusting one hash. DeepSeek's original Engram paper specifically uses deterministic multi-head hashing and describes a lightweight multiplicative-XOR hash.

Despite these tricks sometimes system may lead to wrong results for this reason or because tokens just accidently matched n-gram but nothing useful to our context is about to be injected. Here both Deepseek and Qwen added gating to the n-gram system based on current transformer's hidden state.

Conclusion

This is not exactly replacement for RAG as done to dae, but a way to increase performance on lower-end hardware and scale parameters in a different way. This could be big for future local LLM models.

What is cool this ngram can be offloaded to just slower RAM or even NVMe disk with neglibible performance loss.

Also so people can remain calm - using SSD for a lot of reads does not affect SSDs longevity, what is killer to SSDs is writes so avoid that.

I have been experimenting myself with embedding and graphs for the past couple years myself, and put a lot of thought into memory system for my project, and while it is possible to squeeze a lot from these methods, it is definitely work of art to get perfect for some applications. Unfortunately this new ngram approach does not make it any easier for me, but still this was very interesting and I could not wait for actual model since the day Deepseek published the paper about it.