Memory Architecture

Engram

Conditional Memory via Scalable Lookup: a new sparsity axis for LLMs. Engram modernizes n-gram embeddings for O(1) retrieval, offloading static knowledge to memory while preserving compute for reasoning. A 27B model with Engram outperforms pure MoE baselines across all tasks.

DeepSeek AIJanuary 2025N-gram MemorySparsity Axis
O(1)
Lookup Time
5.7B
Memory Params
+5.0
BBH Improvement
2.8%
Offload Overhead

Deep Dive Contents

Modern LLMs are remarkably good at reasoning but surprisingly inefficient at remembering. Consider asking “What is the capital of France?”—the model doesn't retrieve this fact from memory. Instead, it reconstructs it through billions of matrix multiplications. This is like solving a math problem every time you want to recall your phone number.

Current LLM Approach

  • All knowledge stored in neural weights
  • Every fact requires full forward pass to recall
  • Static patterns waste compute on reconstruction
  • No distinction between “lookup” and “compute”

Engram Approach

  • Static patterns stored in embedding tables
  • O(1) retrieval via direct table lookup
  • Free up compute for actual reasoning
  • Explicit separation of memory and compute

The Core Insight

MoE (Mixture of Experts) introduced conditional computation—only activate relevant experts. Engram introduces conditional memory—only retrieve relevant patterns. Together, they form two orthogonal axes of sparsity that complement each other.

What Gets Stored in Engram?

Factual Patterns

“capital of France” → Paris associations

Linguistic N-grams

Common phrases, collocations, idioms

Code Patterns

“def __init__”, “import numpy”

Engram brings back a classic NLP technique: n-gram embeddings. Instead of storing individual token embeddings, we store embeddings for sequences of tokens. “the capital” gets its own embedding, distinct from “the” + “capital” individually.

Traditional Token Embeddings

“the” → E[4521] = [0.12, -0.34, ...]
“capital” → E[8923] = [0.56, 0.78, ...]
Context built up through attention layers

Engram N-gram Embeddings

“the capital” → E_2[hash] = [0.89, 0.23, ...]
“of France” → E_2[hash] = [0.45, -0.67, ...]
Context patterns directly stored

Tokenizer Compression

Before hashing, tokens undergo normalization to reduce vocabulary size:

NFKC Normalization

“ß” → “ss”, “Ⅰ” → “I”

Lowercasing

“The” ≡ “the” ≡ “THE”

23% Reduction

128K vocab → ~99K canonical IDs

N-gram Orders

Engram uses bigrams (n=2) and trigrams (n=3):

n=2 (Bigrams)

“the capital”, “capital of”, “of France”

n=3 (Trigrams)

“the capital of”, “capital of France”

4-grams were tested but suboptimal under fixed parameter budget.

The key innovation is O(1) constant-time retrieval. Unlike attention which requires O(n) operations to scan the context, Engram uses direct hash-based table lookup. The same n-gram always maps to the same embedding—no learned routing, no computation.

1
Input

Raw token sequence from model

["the", "capital"]
2
Normalize

NFKC normalization + lowercase

["the", "capital"]
3
Extract N-grams

Get 2-grams and 3-grams from context

2-gram: "the capital"
4
Multi-Head Hash

Hash each n-gram with K=8 different hash functions

h₁(the capital) = 42891, h₂(...) = 15723, ... h₈(...) = 98412
5
O(1) LookupConstant Time!

Direct index into embedding tables (prime-sized)

E[42891], E[15723], ..., E[98412] → 8 × 160d vectors
6
Concatenate

Merge all retrieved embeddings

concat([e₁, e₂, ..., e₈]) → 1280d embedding
7
Gate & Fuse

Context-aware gating controls injection strength

α = σ(q·k/√d) ≈ 0.73 → Y = α · V + conv(V)

O(1) Lookup

Direct table indexing. No attention computation, no iteration over sequence.

Deterministic

Same n-gram always retrieves same embedding. Enables prefetching and caching.

Offloadable

Tables can live in host memory. Only 2.8% overhead for 100B params!

Multi-Head Hashing

For each n-gram, K=8 different hash functions map to K different embedding tables:

z_{t,n,k} = φ_{n,k}(g_{t,n})
e_{t,n,k} = E_{n,k}[z_{t,n,k}]

Different hash functions reduce collision risk.

Prime-Sized Tables

Each embedding table has a prime number of slots M_{n,k} for better hash distribution:

M_{2,1} = 1000003 (prime)
M_{2,2} = 1000033 (prime)
...

Primes minimize clustering from hash collisions.

Why O(1) Matters

Prefetchable

Hash computed from tokens alone—start memory fetch early

Parallelizable

Each n-gram lookup is independent—full batch parallelism

Offloadable

Deterministic access patterns enable host memory storage

Raw n-gram embeddings are static—they don't know if they're relevant in context.Context-aware gating solves this by dynamically modulating the injection strength based on the current hidden state.

Gating Mechanism

α_t = σ(RMSNorm(h_t)² · RMSNorm(k_t) / √d)
h_t (Query)
Dynamic hidden state
k_t (Key)
Projected from retrieved embedding
α_t (Gate)
Scalar in [0, 1]

Non-linearity Enhancement

After gating, a depthwise causal convolution adds local context:

Y = SiLU(Conv1D(RMSNorm(Ṽ))) + Ṽ

Kernel size 4, dilation = max n-gram order (3).

Multi-Branch Fusion

For multi-branch architectures (attention + MoE), Engram shares values but has branch-specific gates:

Shared: W_V (one value projection)
Per-branch: W_K^{(m)} (M key projections)

When Does Gating Activate?

High α (relevant)

Context matches n-gram pattern—inject memory

Low α (irrelevant)

Context differs from pattern—suppress memory

A key contribution is the discovery of a U-shaped scaling law. Given a fixed parameter budget, how should you split between MoE experts (compute) and Engram tables (memory)? Pure MoE (ρ=100%) is not optimal—there's a sweet spot around ρ=75-80%.

Compute Budget:

ρ = 0%

Pure Engram. Too much static memory, not enough compute for reasoning.

ρ = 75-80%

Optimal allocation. Balanced compute and memory for best performance.

ρ = 100%

Pure MoE. Wastes compute reconstructing static patterns from weights.

The U-Shape Insight

Pure MoE (ρ=100%) is not optimal. At 6×10²&sup0; FLOPs, allocating 25% of inactive parameters to Engram memory improves validation loss by 0.028—equivalent to significant additional pretraining. The U-shape persists across compute budgets, suggesting a fundamental property of the compute-memory tradeoff.

Too Much Compute (ρ=100%)

Pure MoE wastes compute reconstructing static patterns. The network “memorizes” facts in weights instead of looking them up. Less capacity for actual reasoning.

Too Much Memory (ρ=0%)

Pure Engram can only retrieve patterns, not reason about them. Complex tasks requiring multi-step inference fail. Memory without compute is just a database.

Optimal Allocation: ρ ≈ 75-80%

At 6×10²&sup0; FLOPs, ρ=75% reduces validation loss by 0.028 compared to pure MoE. This U-shape persists across compute budgets, suggesting a fundamental property of the compute-memory tradeoff in language modeling.

Infinite Memory Regime

When scaling Engram slots from 2.58×10&sup5; to 1.0×10&sup7;:

Scaling behavior:Strict power-law (linear in log-space)

This means more memory always helps—no diminishing returns observed yet.

Engram-27B is the flagship model demonstrating the approach. It reallocates 5.7B parameters from MoE experts to Engram memory, keeping total parameters at 26.7B (iso-parameter with baseline).

Attention
MoE Experts
Engram Module
1
2
E
3
4
5
6
7
8
9
10
11
12
13
14
15
E
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Layer 2: Engram-Enhanced

H^1
Input
Attn
MoE
+
Engram
O(1) lookup
H^2
Output

Engram retrieves static n-gram patterns via O(1) lookup and adds them to the hidden state via residual connection: Hℓ ← Hℓ + Y

MoE Experts

55

Reduced from 72 to reallocate 5.7B params to Engram

Engram Memory

5.7B

Parameters in n-gram embedding tables

Total Params

26.7B

Iso-parameter with MoE-27B baseline

Model Configuration

Transformer layers30
Hidden dimension2560
Attention heads32 (MLA)
MoE expansion rate4
Engram layers[2, 15]
N-gram orders[2, 3]
Engram dimension1280
Hash heads8
55
MoE Experts
Reduced from 72
5.7B
Engram Params
~21% of total
26.7B
Total Params
Iso with MoE-27B

Engram modules are trained end-to-end with the backbone using different optimization strategies. The embedding tables require special handling due to their sparse access patterns.

Training Configuration

Training tokens262B
Batch size1280
Backbone optimizerMuon
Engram optimizerAdam
Engram LR scaling5× backbone
Engram weight decay0 (none)
Conv initZeros (identity)
Embedding initSmall random

Distributed Training

Embedding tables are sharded across GPUs:

  • Forward: All-to-All to gather active rows
  • Backward: All-to-All to dispatch gradients
  • Sparse updates only for accessed rows

Why Different Optimizers?

Muon uses Newton-Schulz orthogonalization which assumes dense gradients. Engram tables have extremely sparse gradients (only accessed n-grams update). Adam handles sparse gradients naturally through per-parameter momentum.

Identity Initialization

Convolution parameters are initialized to zero, making the conv layer an identity at the start. This ensures Engram doesn't disrupt the pretrained backbone early in training.

Engram-27B was evaluated against a strictly iso-parameter, iso-FLOPs MoE-27B baseline. The improvements are consistent across all domains—knowledge, reasoning, code, and math.

Knowledge & Reasoning

MMLU
57.4 → 60.4
CMMLU
57.9 → 61.9
BBH
50.9 → 55.9
ARC-C
70.1 → 73.8

Code & Math

HumanEval
37.8 → 40.8
MATH
28.3 → 30.7
GSM8K
58.4 → 60.6
+5.0
BBH
Biggest gain in reasoning
+4.0
CMMLU
Chinese knowledge
+3.7
ARC-C
Science reasoning

Long Context (RULER)

Under iso-loss comparison, Engram dramatically improves long-context retrieval:

Multi-Query NIAH84.2 → 97.0
Variable Tracking77.0 → 89.0

A practical benefit: Engram tables can be offloaded to host memory. Unlike neural network weights that need GPU compute, embedding tables just need random access. This enables massive memory capacity without consuming precious HBM.

Why Offloading Works

  • Deterministic access: Hash computed from tokens alone
  • Prefetchable: Start memory load during prior layer
  • No GPU compute: Just memory read + simple addition

Caching Strategy

N-gram access follows Zipfian distribution—few patterns are very common:

  • L1: GPU registers for active batch
  • L2: GPU memory for hot n-grams
  • L3: Host DRAM for full table

Offloading Performance

100B
Params offloaded
2.8%
Throughput penalty
PCIe
Bandwidth sufficient

Implications for Scaling

This changes how we think about model capacity. With offloading, a server with 8×H100 (640GB HBM) can effectively have terabytes of model capacity by using host RAM for Engram tables. Memory becomes cheap again.

Why does Engram help reasoning tasks even though it's designed for memory? Mechanistic analysis reveals that Engram effectively deepens the network by offloading pattern matching from early layers.

LogitLens Analysis

Tracking when hidden states converge to final predictions:

MoE-27B convergesLayer ~20
Engram-27B convergesLayer ~15

Earlier convergence = more layers free for complex reasoning.

CKA Similarity Analysis

Comparing representation similarity across layers:

Key Finding

Engram Layer 5 ≈ MoE Layer 12 representations

Engram compresses 7 layers of pattern matching into memory lookup.

The Depth Hypothesis

Transformers use early layers for “shallow” tasks like pattern matching and later layers for “deep” tasks like reasoning. By handling patterns via lookup, Engram frees early layers to start reasoning sooner. The network becomes effectively deeper for complex tasks without adding parameters.

Attention Capacity

By delegating local dependencies (n-grams) to memory lookup, attention heads can focus on global context:

Without Engram

Attention wastes heads on “the capital of” patterns

With Engram

All heads available for long-range dependencies

Extensive ablations reveal which components matter most and how to optimally configure Engram.

Component Importance

Validation loss regression from reference (1.768):

Remove multi-branch fusion+0.015significant
Remove context-aware gating+0.012substantial
Remove tokenizer compression+0.008notable
Remove depthwise convolution+0.003marginal
Add 4-grams+0.005suboptimal

Layer Placement

Layer 2 only1.770
Layer 6 only1.773
Layers 2 + 61.768 (best)

Early layers benefit most; splitting memory helps.

Sensitivity Analysis

Performance retention without Engram:

Factual knowledge29-44%
Reading comprehension81-93%

Confirms Engram stores facts, not reasoning.

Why Not 4-grams?

Under fixed parameter budget, 4-grams don't help. They capture rarer patterns but require more table slots, reducing capacity for common patterns. The 2+3 gram combination is the sweet spot for efficiency.

Engram opens up a new research direction: explicit memory in transformers. Several exciting extensions are possible.

Near-Term Extensions

  • Larger tables: 100B+ parameter memory
  • Dynamic insertion: Learn when to use Engram
  • Cross-lingual: Shared memory across languages
  • Domain adaptation: Swap memory for different tasks

Research Questions

  • ?Can Engram store semantic patterns, not just n-grams?
  • ?How does the U-shape change at trillion-parameter scale?
  • ?Can we update memory at inference time (RAG-style)?
  • ?Does Engram help with multimodal models?

The Bigger Picture

Engram challenges the assumption that “all knowledge must be in neural weights.” By separating static patterns (memory) from dynamic computation (reasoning), we may be able to build models that are both more capable and more efficient. The brain doesn't reconstruct every memory from scratch—neither should LLMs.

Industry Implications

Cost Reduction

Less GPU compute for factual recall = cheaper inference

Hardware Design

Memory bandwidth matters more; HBM less critical

Specialization

Swap memory tables for domain-specific models

Engram: The Complete Picture

Engram introduces conditional memory as a new sparsity axis for LLMs. By modernizing n-gram embeddings for O(1) lookup, it separates static pattern storage from dynamic computation. The U-shaped scaling law guides optimal allocation, and memory offloading enables massive capacity at minimal inference cost.

O(1)
Lookup Time
75-80%
Optimal ρ
+5.0
BBH Gain
2.8%
Offload Overhead