← 20 Session 22 - Architectural lookup - Mon 30 Nov →
All decks
  1. 1 Why retrieval nowMon 31 Aug
  2. 2 The inverted indexWed 02 Sep
  3. 3 Index construction and ranking foundationsMon 07 Sep
  4. 4 BM25: finish probabilistic rankingWed 09 Sep
  5. 5 Evaluation and the changing goals of retrieval benchmarksMon 14 Sep
  6. 6 Coarse retrieval and rerankingWed 16 Sep
  7. 7 LLM embeddings and rerankingMon 28 Sep
  8. 9 Hard queries: reasoning and ambiguityWed 07 Oct
  9. 10 Approximate nearest neighbour search and search systemsMon 12 Oct
  10. 12 Agentic memory - introduction, desiderata, and evaluationMon 26 Oct
  11. 14 Agentic memory - approaches and handcrafted methodsMon 02 Nov
  12. 16 Agentic memory - RL methods and wrap-upMon 09 Nov
  13. 18 Retrieval from the parametersMon 16 Nov
  14. 20 An explicit index of the training dataMon 23 Nov
  15. 22 Architectural lookupMon 30 Nov
  16. 24 Retrieval from the context - attention and the KV cacheMon 07 Dec
  17. 25 Retrieval from the context - which tokens are readWed 09 Dec
  18. 26 Course wrap-upMon 14 Dec
Contents
  1. 1Architectural lookup
  2. 2Upcoming sessions
  3. 3Today
  4. 4From datastore back to weights
  5. 5In this section
  6. 6Memory Decoder imitates the datastore
  7. 7A large memory lifts a small model above a larger one
  8. 8MLP Memory applies the same imitation to the pretraining data
  9. 9MeMo trains a separate memory model
  10. 10MeMo answers questions that span documents
  11. 11The distilled memory trades deletion and attribution for speed
  12. 12Addressability means a computable address, isolated slots, and…
  13. 13Memory inside the network
  14. 14In this section
  15. 15The first matrix holds keys and the second holds values
  16. 16A router sends each token to the top k experts
  17. 17Mixtral activates 13B of its 47B parameters per token
  18. 18Mixtral's experts follow syntax rather than topic
  19. 19An expert is a coarse address with no record of its data
  20. 20A memory layer is a sparse feed-forward layer with a huge hidde…
  21. 21A memory layer reads like attention over fixed keys
  22. 22Product keys find the best of a million keys in a small corner
  23. 23A memory layer beats a model twice as deep at almost twice the…
  24. 24Quick quiz: how much does the product-key search compute?
  25. 25A memory layer passes the address and isolation tests but not p…
  26. 26Scaled memory layers beat denser models on factual QA
  27. 27Engram computes the address from the text itself
  28. 28Inside an Engram block
  29. 29A text-based address lets the table sit in CPU memory
  30. 30Engram complements experts under a fixed budget
  31. 31Engram names its rows by text rather than by documents
  32. 32Updating and tracing memory
  33. 33In this section
  34. 34Finetuning on a new fact disturbs old ones
  35. 35Update only the slots that are specific to the new fact
  36. 36Forgetting falls from 89% to 11%
  37. 37Memory blocks keyed by document clusters can be traced
  38. 38Finer addresses pass more tests, and cost storage and sharing
  39. 39Next time

The recording

1

Architectural lookup

Information retrieval for AI

30 November 2026

Seong Joon Oh - KAIST AI

2

Upcoming sessions

2 December - Weights or an explicit store

The anchor paper is LMLM (Zhao et al., ICLR 2026). Preparation is due 1 December, 18:00 KST.

7 December and 9 December - recorded lectures, no live class. Retrieval from the context, parts 1 and 2.

Roles and readings are in the course schedule.

3

Today

Session 20 covered the two ends of one axis.

  • A datastore is traced and updated by lookup. Every token needs a nearest-neighbour search, and the store grows with the corpus.
  • A dense network runs regular, parallel arithmetic. It is traced approximately, through influence functions.

The middle could keep the cost of the dense end and the addresses of the datastore. Today asks which designs do.

dense FFNreads all MoE2 of 8 experts memory layer32 per head of 262k+ slots Engramhashed n-gram rows kNN-LMneighbours in a datastore session 20session 20today low storage, dense computation high storage, sparse computation
  1. From datastore back to weights
  2. Memory inside the network
  3. Updating and tracing memory
4

From datastore back to weights

5

In this section

  • Memory Decoder: train a module to imitate the datastore.
  • MeMo trains a separate memory model and queries it in text.
  • The distilled module gains speed and loses deletion and attribution.
  • Three tests for any store inside the network: address, isolation, and provenance.
6
Cao et al. (2025), Fig. 3. Memory Decoder learns the retriever distribution in pretraining and runs beside the LLM at inference.
Cao et al. (2025), Fig. 3. Memory Decoder learns the retriever distribution in pretraining and runs beside the LLM at inference.

Memory Decoder imitates the datastore

The datastore costs a nearest-neighbour search on every token, and it grows with the corpus. Memory Decoder moves it to the dense end. A small network gives the datastore’s output without the search.

A small transformer decoder learns to match the kNN distribution over a domain corpus. The loss is

At inference it runs beside the frozen model, and the two distributions are interpolated. There is no search.

Across biomedicine, finance, and law, perplexity falls by 6.17 points on average. The overhead is 1.28 times the base model, against 2.17 for kNN-LM.

Cao et al., 2025, abstract, §3, and §4.

7
Memory Decoder at Scale (2026), Fig. 1. Average score over 17 tasks against total parameters, for base models and base models with memory.
Memory Decoder at Scale (2026), Fig. 1. Average score over 17 tasks against total parameters, for base models and base models with memory.

A large memory lifts a small model above a larger one

In Memory Decoder at Scale, a 6.9B memory beside Pythia-410M raises its average over 17 benchmarks from 29.86 to 37.34. Pythia-12B scores 37.24.

Memory Decoder at Scale, 2026, abstract and Fig. 1.

8
Wei et al. (2025), Fig. 1 right. Time to first token and tokens per second for a 7B base model, with RAG, kNN-LM, and a 1B MLP Memory. kNN-LM uses dimension reduction and the Wikipedia-2021 corpus.
Wei et al. (2025), Fig. 1 right. Time to first token and tokens per second for a 7B base model, with RAG, kNN-LM, and a 1B MLP Memory. kNN-LM uses dimension reduction and the Wikipedia-2021 corpus.

MLP Memory applies the same imitation to the pretraining data

Same loss as Memory Decoder, from the same group. A 1B MLP imitates the retriever over the pretraining data.

  • Like kNN-LM, it mixes the retriever’s distribution into the output. On Mistral-7B, QA accuracy over five benchmarks rises 12.3% relative to the base model.
  • For 5B tokens, a 40 TB datastore becomes a 4 GB MLP, in the paper’s example.
  • Time to first token is 27 ms, against 22 for the base and 151 for kNN-LM.

Wei et al., 2025, §1, §3, Fig. 1, and Table 1.

9

MeMo trains a separate memory model

Training new corpus generator LLMfrozen question-answerpairs memory modelfinetuned Inference question LLM, open or closedfrozen memory model sub-question in text answer in text final answer

The LLM reads the memory model only through text, so it needs no access to the LLM’s weights or logits. Retrieval cost does not grow with the corpus.

Like Memory Decoder, it has no entry to delete and no source to cite.

Quek et al., arXiv 2026, abstract, §1, and Fig. 1.

10

MeMo answers questions that span documents

The memory model reads the whole corpus in training, so one answer can combine facts from many documents. RAG passes the LLM separate passages. Memory Decoder mixes output distributions, so it needs the LLM’s logits.

RAGMemory DecoderMeMo
LLM weights unchanged✓✓✓
No index to search✗✓✓
Works with a closed LLM✓✗✓

On NarrativeQA with Gemini-3-Flash, MeMo scores 53.58. The best retrieval baseline, NV-Embed-V2, scores 26.62, and perfect retrieval 60.41. On BrowseComp-Plus, where the answer must be read from the evidence, graph-based RAG is level or ahead.

Quek et al., arXiv 2026, Table 1, Table 2, and §5.1.

11

The distilled memory trades deletion and attribution for speed

Explicit datastoreDistilled memory
Cost per tokenSearch over stored keysOne extra forward pass
Delete a documentDrop its entriesRetrain or edit the module
Name the sourceRead the retrieved entriesNo entries to read
12

Addressability means a computable address, isolated slots, and a trace to the data

  1. Address. Can you compute which parameters a token reads?
  2. Isolation. Can you change one slot without disturbing the rest?
  3. Provenance. Can you trace a slot to the training data that wrote it?

A datastore passes these tests. Memory Decoder and MeMo fail them: the distilled module has no entry to change and no source to cite.

13

Memory inside the network

14

In this section

  • A feed-forward layer is a key-value memory read densely.
  • Mixture of experts routes each token to a few experts (e.g. Mixtral).
  • A memory layer reads a few slots from a table of hundreds of thousands.
  • Memory Layers at Scale scales the table.
  • Engram computes the address from the text itself.

The designs follow the spectrum from left to right.

dense FFNreads all MoE2 of 8 experts memory layer32 per head of 262k+ slots Engramhashed n-gram rows kNN-LMneighbours in a datastore session 20session 20today low storage, dense computation high storage, sparse computation
15

The first matrix holds keys and the second holds values

Mixture of experts and memory layers each replace this layer.

Rows of are keys, rows of are values, and weights each key.

x keys K (all scored)values V (all mixed) every key scored, every value mixed: slots = hidden size

Every token reads every slot, so more slots cost more computation, and the address test fails.

Geva et al., EMNLP 2021. Replacement of the FFN: Lample et al. 2019, Fig. 3; Berges et al. 2024, §1.

16

A router sends each token to the top k experts

Only the top experts run, so adding experts adds parameters but not expert computation per token.

token router + output .

The first step from the dense end. One address selects a whole expert, thousands of parameters.

Shazeer et al., ICLR 2017.

17
Jiang et al. (2024), Fig. 3. Benchmark scores against active parameters for Mistral 7B, Mixtral 8x7B, and Llama 2 7B, 13B, and 70B.
Jiang et al. (2024), Fig. 3. Benchmark scores against active parameters for Mistral 7B, Mixtral 8x7B, and Llama 2 7B, 13B, and 70B.

Mixtral activates 13B of its 47B parameters per token

Each layer has 8 feed-forward experts. The router selects 2 for every token at every layer.

Each token can use 47B parameters but uses 13B active parameters.

With 5 times fewer active parameters, Mixtral matches or beats Llama 2 70B on most of the reported benchmarks.

Jiang et al., arXiv 2024, abstract, §3, and Fig. 3.

18
Jiang et al. (2024), Fig. 7. Share of tokens sent to each of the 8 experts, per Pile domain, at layers 0, 15, and 31. The dashed line marks 1/8.
Jiang et al. (2024), Fig. 7. Share of tokens sent to each of the 8 experts, per Pile domain, at layers 0, 15, and 31. The dashed line marks 1/8.

Mixtral’s experts follow syntax rather than topic

Expert assignment on The Pile is very similar for ArXiv, PubMed Abstracts, and PhilPapers, at the first, middle, and last layers. DM Mathematics differs slightly.

The router shows structure in syntax instead. self in Python and indentation tokens go to the same experts. Consecutive tokens often share an expert.

In Mixtral’s analysis, expert assignment does not map to topics.

Jiang et al., arXiv 2024, §5, Figures 7-8 and Table 5.

19

An expert is a coarse address with no record of its data

  1. Address. The router’s choice can be read off for each token.
  2. Isolation. Coarse. An expert is a full feed-forward network that many tokens share.
  3. Provenance. Fails in Mixtral. FlexOlmo (Shi et al., 2025) trains one expert per closed dataset, so a data owner’s expert can be included or excluded at inference with no further training.

A memory layer can be read as a mixture of experts with one small expert per memory slot. That shrinks the address to a single slot.

Shi et al., 2025, abstract. Lin et al., 2025, §3, after He (2024).

20

A memory layer is a sparse feed-forward layer with a huge hidden layer

Read a feed-forward layer as a memory: hidden units are slots, the first matrix holds keys, the second holds values.

Now make the hidden layer enormous, hundreds of thousands to a million slots, and let each token read only the best.

That is a memory layer. Capacity grows with the number of slots. Computation grows with .

Feed-forward layerMemory layer x keysvaluesthousands of slots, all read x keysvaluesup to a million slots, top k read

Lample et al., NeurIPS 2019.

21

A memory layer reads like attention over fixed keys

Keys come fromNumber of keysRead
Feed-forward layerlearned parametershidden size, thousandsdense
Self-attentionthe current contextcontext lengthdense
Memory layerlearned parametersup to about a milliontop

A transformer computes its key-value reads densely. A memory layer keeps the same read and makes it sparse over a fixed, very large set of keys.

“the keys and values do not correspond to input tokens but are free embedding vectors, and the number of values (memory size) is very large”

Lample et al., NeurIPS 2019, §3.1 and Eqs. 1-3.

22

Product keys find the best of a million keys in a small corner

Split the query in two. Each half scores its own subkeys. A full key pairs one subkey from each half, and its score is the sum of the two.

1024 second-half subkeys, sorted by score 1024 first-half subkeys, sorted top k k × k corner: rank these sums other pairs: never summed

Sort each half by score. The top full keys always lie where the top rows meet the top columns, so ranking that corner is an exact search.

Lample et al., NeurIPS 2019, §3.2.

23
Lample et al. (2019), Fig. 4. Perplexity against inference speed with and without memory layers, labelled by layer count.
Lample et al. (2019), Fig. 4. Perplexity against inference speed with and without memory layers, labelled by layer count.

A memory layer beats a model twice as deep at almost twice the speed

On a 28-billion-word news corpus, a 12-layer model with one memory layer reaches perplexity 15.6. The 24-layer model without memory reaches 16.0.

The memory has k slots, read by 4 heads with each.

Lample et al., §4.3, Table 1 and Fig. 4.

24

Quick quiz: how much does the product-key search compute?

Each half of the query has 1024 subkeys, so there are , about a million, full keys.

How many query-subkey dot products does the search compute?

Sign in with your course account to choose. The lecturer will reveal responses and the answer.

25

A memory layer passes the address and isolation tests but not provenance

  1. Address. Computed by exact key search. You can list the slots a token reads.
  2. Isolation. A slot’s value can be updated alone. Sparse memory finetuning uses this.
  3. Provenance. Fails. The method records no link from a slot to the documents that trained it.
26
Berges et al. (2024), Fig. 1. Factual QA accuracy and NLL against memory size for a 1.3B base model. Dashed lines mark a 7B dense model trained with 10 times more FLOPs.
Berges et al. (2024), Fig. 1. Factual QA accuracy and NLL against memory size for a 1.3B base model. Dashed lines mark a 7B dense model trained with 10 times more FLOPs.

Scaled memory layers beat denser models on factual QA

Memory Layers at Scale (Berges et al., 2024) keeps the product-key lookup and scales it. It replaces the feed-forward layer in a few transformer layers, and all of them share one pool of keys and values.

They scale memory to 128B parameters, with base models up to 8B, trained on up to 1 trillion tokens.

Memory-augmented models beat dense models with more than twice the computation, and beat MoE matched for compute and parameters. Gains are largest on factual QA.

A 1.3B model with 64 million keys approaches Llama 2 7B on factual QA, which used 10 times more FLOPs.

Berges et al., arXiv 2024, abstract, §1, §3.1.1, §5.2, and Fig. 1.

27

Engram computes the address from the text itself

Why a lookup: a plain transformer spends several early layers of attention and feed-forward computation to combine the, Eiffel, and Tower into one representation of the name. Engram looks that representation up instead.

How it differs from product-key memory: there, a query from the hidden state is scored against keys. Engram scores nothing. A hash of the last tokens gives the row, in constant time however large the table. A gate from the hidden state turns down a row that does not fit the context, for example after a hash collision.

"the Eiffel Tower" hash table row gate α + hidden state

Cheng et al., arXiv 2026, abstract, §1, §2.2, and §2.3.

28
Cheng et al. (2026), Fig. 1. An Engram block sits before attention and MoE in some layers. Inside, 2-gram and 3-gram suffixes are hashed into embedding tables, concatenated, projected, gated against the hidden state, and passed through a convolution.
Cheng et al. (2026), Fig. 1. An Engram block sits before attention and MoE in some layers. Inside, 2-gram and 3-gram suffixes are hashed into embedding tables, concatenated, projected, gated against the hidden state, and passed through a convolution.

Inside an Engram block

  1. Hash the last 2 and the last 3 tokens, with several hash heads each.
  2. Read one row per head from the 2-gram and 3-gram tables, and concatenate the rows.
  3. Project them to a key and a value. The hidden state scores the key, and the score gates the value.
  4. A short causal convolution follows, and the result is added to the residual stream.

Only some layers carry an Engram block, before attention and MoE.

Cheng et al., §2.2-§2.4 and Fig. 1.

29
Cheng et al. (2026), Fig. 2b. At inference the Engram table sits in host (CPU) memory, and rows are sent to the blocks with Engram while the GPU computes.
Cheng et al. (2026), Fig. 2b. At inference the Engram table sits in host (CPU) memory, and rows are sent to the blocks with Engram while the GPU computes.

A text-based address lets the table sit in CPU memory

The address depends on the input tokens alone, so it is known before the forward pass.

The table can sit in the server’s CPU memory, which is far larger than GPU memory. The CPU sends the needed rows to the GPU ahead of time, while the GPU computes the earlier layers.

Offloading a 100B-parameter table costs under 3% overhead.

Cheng et al., §1, §2.5, and Fig. 2.

30
Cheng et al. (2026), Fig. 3 left. Validation loss against the share of the sparse budget given to MoE experts, at two compute budgets.
Cheng et al. (2026), Fig. 3 left. Validation loss against the share of the sparse budget given to MoE experts, at two compute budgets.

Engram complements experts under a fixed budget

Fix the total and active parameters. Let be the share of the sparse budget given to MoE experts; the rest goes to Engram.

Validation loss is U-shaped in . Pure MoE () is not optimal. The best loss comes from giving 20-25% of the sparse budget to Engram, stable across two compute budgets.

At 10B scale, loss falls from 1.7248 at to 1.7109 near .

Guided by this, the authors train Engram-27B. Against an MoE with the same parameters and FLOPs, it gains 3.4 points on MMLU and more on reasoning, 5.0 on BBH.

Cheng et al., abstract, §3.1, and Fig. 3.

31

Engram names its rows by text rather than by documents

  1. Address. A hash of the last few tokens, computed before the forward pass.
  2. Isolation. A row can be updated alone. Hash collisions can put several n-grams in one row; multi-head hashing reduces collisions.
  3. Provenance. A row stands for an n-gram, and an n-gram occurs in many documents. The method records no link from a row to documents.

Cheng et al., §2.2.

32

Updating and tracing memory

33

In this section

  • Sparse memory finetuning changes a new fact in its own slots, with less forgetting than full finetuning.
  • Hierarchical memories key memory blocks by clusters of training documents, so a block can be traced and blocked.
34

Finetuning on a new fact disturbs old ones

Full finetuning updates every parameter that a new fact’s gradient touches. Many of those parameters also serve other facts.

Learning a stream of new facts therefore erodes old knowledge. This is catastrophic forgetting.

A memory layer gives a finer handle: each token reads only about 32 slots per head, out of a million.

35
Lin et al. (2025), Figure 2. Memory accesses in a batch are counted, ranked against accesses on pretraining data, and only the top t slots are trained.
Lin et al. (2025), Figure 2. Memory accesses in a batch are counted, ranked against accesses on pretraining data, and only the top t slots are trained.

Update only the slots that are specific to the new fact

The problem: finetuning every slot the new fact reads still causes forgetting. Some of those slots serve many inputs, for example predicting syntax.

The fix borrows TF-IDF from document retrieval. Count how often each slot is read on the new batch, , and on 1000 batches of pretraining data. A slot read often here and rarely there is specific to the new fact:

𝟙

Only the top slots receive the gradient. All read slots still contribute to the output.

Lin et al., arXiv 2025, §4, §6, and Fig. 2.

36

Forgetting falls from 89% to 11%

Train on a stream of TriviaQA facts, then measure NaturalQuestions, which the model knew before.

Full finetuning LoRA Sparse memory finetuning 89%71%11% drop in NaturalQuestions F1 after learning TriviaQA facts

The three methods reach the same level of new-knowledge acquisition.

A finer address lets a correction target the parameters a fact uses.

Lin et al., arXiv 2025, abstract and §1.

37
Pouransari et al. (2026), Fig. 2. A retriever fetches memory blocks from a bank organised by hierarchical clusters of pretraining documents.
Pouransari et al. (2026), Fig. 2. A retriever fetches memory blocks from a bank organised by hierarchical clusters of pretraining documents.

Memory blocks keyed by document clusters can be traced

The problem: product-key memory and Engram link no slot to the documents that trained it, so both fail the provenance test. FlexOlmo makes the link only at the size of a whole expert.

Pouransari et al. build the link in. They cluster the pretraining documents and give each cluster a block of memory parameters. At inference, a small model fetches the blocks for its context.

Retrieval fetches weights, not tokens. Each block can be traced to a cluster of documents.

Blocking 1/16 of the memory bank cut accuracy on one class of facts from 70% to 20%. This is a preliminary test, not deletion.

Pouransari et al., ICLR 2026, Fig. 2.

38

Finer addresses pass more tests, and cost storage and sharing

DesignAddressIsolationProvenanceStorageOne entry serves
Dense FFNall slots readnonothousands of slots per layerevery token
MoElearned routercoarse, per expertnoall experts kept, 47B in Mixtralmany tokens
Memory layerexact key searchper slotnoup to millions of slotstokens that select it
Engramhash of the textper rowpartial: rows are n-grams100B-parameter table, in CPU memoryone n-gram
Hierarchical memoriesdocument clusterper blockyes, by clustermemory bank of billions of parametersone document cluster
kNN-LM datastorenearest neighboursper entryyesone entry per training tokenone training token

good partial poor

Towards the bottom, each entry serves fewer inputs. Addresses and deletion get easier, the store grows, and less is shared across inputs.

39

Next time

2 December discussion: should factual knowledge live in the weights or in an explicit store? Anchor: LMLM.

Next lecture: retrieval from the context, starting with attention as a lookup and the KV cache.