Memory Augmentation › Memory Retrieval Mechanisms: From Soft to Efficient
Language AI HandbookAll chapters Memory Augmentation › Memory Retrieval Mechanisms: From Soft to EfficientOne section · Sections in this chapter4 / 12 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The basic memory read operation we just implemented has an elegant mathematical formulation, but it harbors a serious scalability problem. Let's understand this issue deeply before exploring solutions.
The Soft Attention Baseline
Our memory read computes attention over all memory slots:
This soft attention approach has an important advantage: it's fully differentiable. Gradients flow smoothly from the output through the softmax and into the query and key representations. The model can learn what to store and how to query through standard backpropagation.
But soft attention also inherits the same dilution problem we identified earlier for long-context transformers. When is large, each slot's weight becomes vanishingly small. The information we need gets drowned in a weighted average dominated by irrelevant entries.
Temperature: Controlling the Focus-Diffusion Tradeoff
One way to combat dilution is to sharpen the attention distribution. We can introduce a temperature parameter that scales the scores before softmax:
The temperature controls the "peakiness" of the distribution:
-
Low temperature (): Amplifies score differences. Small advantages become large, concentrating attention on the best-matching entries. In the limit , this approaches hard argmax selection.
-
High temperature (): Dampens score differences. The distribution becomes more uniform, spreading attention across many entries.
We can measure this effect through entropy, which quantifies how spread out a distribution is:
Low entropy means attention is concentrated on few slots; high entropy means it's spread across many.
def soft_attention_retrieval(
query, memory_keys, memory_values, temperature=1.0
):
"""
Retrieve from memory using soft attention with temperature scaling.
The temperature τ controls focus vs diffusion:
- τ < 1: More focused (approaches hard selection)
- τ = 1: Standard softmax
- τ > 1: More diffuse (approaches uniform)
Args:
query: (batch, dim)
memory_keys: (memory_size, dim)
memory_values: (memory_size, value_dim)
temperature: Scaling factor τ for attention sharpness
Returns:
retrieved: (batch, value_dim)
attention: (batch, memory_size)
"""
# Compute scaled similarities: s_i / (√d · τ)
scores = torch.matmul(query, memory_keys.T) # (batch, memory_size)
scores = scores / (np.sqrt(query.shape[-1]) * temperature)
# Soft attention weights: α_i = softmax(scores)_i
attention = F.softmax(scores, dim=-1)
# Weighted retrieval: r = Σ_i α_i · v_i
retrieved = torch.matmul(attention, memory_values)
return retrieved, attention
# Compare different temperatures
temperatures = [0.1, 0.5, 1.0, 2.0]
query = torch.randn(1, 128)
keys = torch.randn(100, 128)
values = torch.randn(100, 64)
temperature_results = []
for temp in temperatures:
_, attn = soft_attention_retrieval(query, keys, values, temperature=temp)
# Compute entropy: H(α) = -Σ α_i log(α_i)
entropy = -torch.sum(attn * torch.log(attn + 1e-10), dim=-1).item()
max_weight = attn.max().item()
temperature_results.append(
{"temperature": temp, "entropy": entropy, "max_weight": max_weight}
)Temperature Effect on Retrieval Sharpness:
Temperature Entropy Max Weight Interpretation
-----------------------------------------------------------------
0.1 0.515 0.877 Very focused (near hard)
0.5 3.313 0.179 Focused
1.0 4.210 0.061 Standard
2.0 4.500 0.027 Diffuse
The results demonstrate the entropy-focus tradeoff concretely. At temperature 0.1, the entropy is low and the maximum weight approaches 1.0, meaning the model concentrates almost all attention on a single memory slot. The softmax has become nearly a hard argmax. At temperature 2.0, entropy increases substantially as attention spreads more evenly across slots.
This tradeoff has practical implications:
- For fact retrieval (e.g., "What is the capital of France?"), use low temperatures. The answer exists in one specific memory location.
- For synthesis tasks (e.g., "Summarize the key themes"), use higher temperatures. Relevant information is distributed across many entries.
- For training, moderate temperatures (0.5-1.0) often work best. They allow gradient flow to multiple entries without completely diluting the signal.
However, temperature alone cannot solve the scalability problem. Even with sharp attention, we still compute similarities with all memory entries. When reaches millions, this becomes prohibitively expensive.
Top-k Retrieval: Trading Differentiability for Efficiency
A more radical solution abandons the soft attention over all entries. Instead, we first find the most relevant memory slots using a discrete selection step, then apply soft attention only over those entries:
The top- operation returns the indices of the highest-scoring entries. We then compute attention only over this subset:
The key insight is that is fixed regardless of memory size . Whether the memory contains 1,000 or 1,000,000 entries, we only attend over slots in the final step. This bounds the attention computation at regardless of .
The Differentiability Tradeoff
There's a catch: the top- selection involves an argmax-like operation (finding the highest scores), which is not differentiable. During backpropagation, we cannot compute gradients through the discrete selection step.
This means entries that weren't selected receive no gradient signal, even if they were "almost" selected. The model cannot learn to make a near-miss entry more relevant. In practice, this limitation is often acceptable because:
- The entries that were selected still receive full gradients
- The key representations are typically trained with other objectives (like the main language modeling loss)
- The selection step can be approximated during training using straight-through estimators or Gumbel-softmax tricks
For many applications, the efficiency gains vastly outweigh this differentiability cost.
def topk_retrieval(query, memory_keys, memory_values, k=10):
"""
Retrieve from memory using top-k selection followed by soft attention.
This is a two-stage process:
1. Find the k keys with highest similarity to the query (discrete)
2. Apply softmax attention over only those k entries (continuous)
Complexity: O(n) for scoring + O(k) for attention = O(n) total
but the attention step is bounded by k regardless of n
"""
# Stage 1: Compute all n similarity scores
scores = torch.matmul(query, memory_keys.T) # (batch, n)
scores = scores / np.sqrt(query.shape[-1])
# Stage 2: Select top-k (discrete, non-differentiable)
topk_scores, topk_indices = torch.topk(scores, k, dim=-1)
# Gather the k corresponding values
batch_size = query.shape[0]
topk_values = memory_values[topk_indices.view(-1)].view(batch_size, k, -1)
# Stage 3: Soft attention over just the k entries
topk_attention = F.softmax(topk_scores, dim=-1) # (batch, k)
# Weighted sum over k entries (not n!)
retrieved = torch.einsum("bk,bkd->bd", topk_attention, topk_values)
return retrieved, topk_indices, topk_attention
# Demonstrate top-k retrieval on a large memory
memory_size = 10000
query = torch.randn(4, 128)
large_keys = torch.randn(memory_size, 128)
large_values = torch.randn(memory_size, 64)
for k in [5, 20, 100]:
retrieved, indices, attn = topk_retrieval(
query, large_keys, large_values, k=k
)Top-k Retrieval from 10,000 Memory Slots:
k Compute Ratio Memory Slots Used
---------------------------------------------
5 0.05% 5
20 0.20% 20
100 1.00% 100The efficiency gains are dramatic. With , we use only 0.05% of the memory slots for the attention computation, a 2000x reduction compared to attending to all entries. Even with to retrieve more entries, we still use only 1% of the memory.
Complexity Analysis: Where the Time Goes
Let's trace through the computational costs:
- Scoring all keys: We still compute for all entries:
- Finding top-k: Selecting the largest from scores: (or for a heap-based algorithm)
- Attention over k: Softmax and weighted sum over entries:
The total is for exact top-k retrieval. We've bounded the attention step but not the scoring step.
For truly large memories (millions of entries), even linear scoring becomes prohibitive. This is where approximate nearest neighbor (ANN) algorithms enter. Methods like FAISS, ScaNN, or Annoy build index structures that support sublinear retrieval:
- Exact search: to find top-k
- Approximate search: or even depending on the algorithm and acceptable accuracy loss
With ANN indexing, a memory of 1 million entries can be queried nearly as fast as a memory of 1,000 entries, at the cost of occasionally missing the truly best match.

Hierarchical Memory
Top-k retrieval solves the attention efficiency problem but treats all memory entries uniformly. When memories span multiple time scales, a more sophisticated organization helps: hierarchical memory separates recent detailed information from older compressed summaries.
Short-term memory holds recent context at full resolution, while long-term memory stores condensed representations from earlier. The model queries both levels, using short-term memory for detailed recent information and long-term memory for general patterns.

The hierarchical approach mirrors how human memory works: detailed episodic memory for recent events, condensed semantic memory for older knowledge. For language models, this translates to storing recent token representations at full resolution while compressing older context into summary vectors.