Context Length Challenges › Attention Memory Scaling
Language AI HandbookAll chapters Context Length Challenges › Attention Memory ScalingOne section · Sections in this chapter3 / 11 ↓
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 attention mechanism computes pairwise interactions between all tokens. For each layer and each attention head, we must store an matrix of attention weights, where every entry represents how much token attends to token . The key insight is that this pairwise structure is what makes attention so powerful: every token can directly influence every other token in a single forward pass. But that same all-pairs structure is precisely what makes scaling to long sequences so expensive. The total memory required to store these attention matrices is:
where:
- : total memory in bytes for all attention matrices
- : number of attention heads (each head computes an independent attention pattern)
- : number of transformer layers (each layer has its own set of attention matrices)
- : sequence length in tokens
- : the size of each attention matrix (one entry per query-key pair)
- : bytes per element (2 for fp16/bf16, 4 for fp32)
The critical insight is that appears squared: doubling the sequence length quadruples the memory for attention matrices. This is the source of the quadratic memory scaling that limits context length.
But this formula only accounts for the attention weight matrices themselves. A complete picture of memory consumption includes:
- Key-value cache (for inference):
- Activations (for training): Intermediate values needed for backpropagation
- Gradients: Same size as activations
def memory_breakdown_gb(n, d=4096, h=32, L=32, dtype_bytes=2, mode="training"):
"""
Break down memory requirements for a transformer.
Args:
n: Sequence length
d: Model dimension
h: Number of attention heads
L: Number of layers
dtype_bytes: Bytes per element
mode: "training" or "inference"
Returns:
Dictionary with memory breakdown in GB
"""
bytes_to_gb = 1 / (1024**3)
# Attention matrices: h * L * n^2 (but computed one layer at a time)
# For training, we need to store all for backprop
attn_matrices = h * L * n * n * dtype_bytes
# KV cache: 2 (K and V) * L * n * d
kv_cache = 2 * L * n * d * dtype_bytes
# Activations per layer (rough estimate):
# - Input embeddings: n * d
# - Attention output: n * d
# - FFN intermediate: n * 4d (assuming 4x expansion)
# - Total per layer: ~6 * n * d
activations_per_layer = 6 * n * d * dtype_bytes
if mode == "training":
# Store activations for all layers for backprop
activations = L * activations_per_layer
gradients = activations # Same size as activations
return {
"attention_matrices": attn_matrices * bytes_to_gb,
"activations": activations * bytes_to_gb,
"gradients": gradients * bytes_to_gb,
"total": (attn_matrices + activations + gradients) * bytes_to_gb,
}
else: # inference
return {
"attention_matrices": 0, # Computed on-the-fly with FlashAttention
"kv_cache": kv_cache * bytes_to_gb,
"total": kv_cache * bytes_to_gb,
}
# Analyze for LLaMA 7B style model
sequence_lengths = [2048, 8192, 32768, 131072]Memory Breakdown for LLaMA 7B Style Model (d=4096, h=32, L=32):
Training Mode:
Seq Len Attn Matrices Activations Gradients Total
-----------------------------------------------------------------
2K 8.0 GB 3.0 GB 3.0 GB 14.0 GB
8K 128.0 GB 12.0 GB 12.0 GB 152.0 GB
32K 2048.0 GB 48.0 GB 48.0 GB 2144.0 GB
131K 32768.0 GB 192.0 GB 192.0 GB 33152.0 GB
Inference Mode:
Seq Len KV Cache
------------------------------
2K 1.0 GB
8K 4.0 GB
32K 16.0 GB
131K 64.0 GBFor training, the numbers quickly become untenable. A 32K sequence requires over 500 GB for attention matrices alone, far exceeding any single GPU. Even with gradient checkpointing (recomputing activations during backprop instead of storing them), the attention matrices remain the bottleneck.
For inference, the KV cache dominates. At 128K tokens, storing keys and values for all layers consumes over 60 GB. This explains why long-context inference requires careful memory management and often multiple GPUs.


These visualizations reveal why long-context training and inference require fundamentally different approaches. Training hits the quadratic wall with attention matrices. Inference hits a linear wall with KV cache storage. Solutions like FlashAttention address the first problem by never materializing the full attention matrix. Solutions like KV cache compression and sliding window attention address the second.