Activation Memory Explosion from Quadratic Attention
Standard multi-head dot-product attention computes an attention matrix of size (batch_size, num_heads, seq_length, seq_length). The memory complexity is O(N^2) with respect to the sequence length. As sequence length increases, the intermediate activations saved for the backward pass become excessively large and exhaust VRAM.
Standard multi-head dot-product attention computes an attention matrix of size (batch_size, num_heads, seq_length, seq_length).
- Symptom
RuntimeError: CUDA out of memory. Tried to allocate (large amount) MiB- Root cause
- Standard multi-head dot-product attention computes an attention matrix of size (batch_size, num_heads, seq_length, seq_length). The memory complexity is O(N^2) with respect to the sequence length. As sequence length increases, the intermediate activations saved for the backward pass become excessively large and exhaust VRAM.
- Recommended fix
- Enable gradient checkpointing. model.gradient_checkpointing_enable() Trades compute for memory by discarding intermediate activations during the forward pass and recomputing them during the backward pass.
- How Denpex helps
- Denpex investigates Activation Memory Explosion from Quadratic Attention using the evidence you provide or your connected workload collects. Earlier rank, host or application evidence is needed to distinguish an initiating failure from a downstream report.
What this failure is
Activation Memory Explosion from Quadratic Attention is a Memory failure seen during ML training runs. Standard multi-head dot-product attention computes an attention matrix of size (batch_size, num_heads, seq_length, seq_length). The memory complexity is O(N^2) with respect to the sequence length. As sequence length increases, the intermediate activations saved for the backward pass become excessively large and exhaust VRAM. Common tags: Activation Memory.
Is this what broke your run? Paste your log.
You're reading about Activation Memory Explosion from Quadratic Attention. Paste your own traceback and relevant evidence for an investigation of your workload, with a next action or a specific missing fact. A reference entry does not establish your cause. No account or card for the free diagnosis. Review data handling before submitting sensitive logs.
Before uploading, review cloud data handling and local options.
Upload your log file. Large files are processed in your browser. Only selected evidence is submitted when you run diagnosis.
Why it happens (the mechanism)
Users assume memory usage scales linearly with sequence length. They don't anticipate the quadratic explosion caused by the attention matrix.
What you'll observe
- RuntimeError: CUDA out of memory. Tried to allocate (large amount) MiB
- OOM occurs during `model(inputs)` inside a SelfAttention layer.
Common symptoms and what they mean
| Symptom | Why it happens |
|---|---|
| Model trains fine on short texts, but crashes with CUDA OOM as soon as a batch contains long sequences. | Standard multi-head dot-product attention computes an attention matrix of size (batch_size, num_heads, seq_length, seq_length). The memory complexity is O(N^2) with respect to the sequence length. As sequence length increases, the intermediate activations saved for the backward pass become excessively large and exhaust VRAM. |
| Reducing the batch size to 1 still results in an OOM error for long documents. | Standard multi-head dot-product attention computes an attention matrix of size (batch_size, num_heads, seq_length, seq_length). The memory complexity is O(N^2) with respect to the sequence length. As sequence length increases, the intermediate activations saved for the backward pass become excessively large and exhaust VRAM. |
Which systems are affected
- PyTorch
- Transformers
How to confirm this is the problem
Use this checklist to test the hypothesis against a small reproduction. No single line proves the root cause, so preserve the preceding events and compare one variable at a time.
- ✓Trace the memory usage against sequence length.
- ✓Check the stack trace for operations like `torch.matmul` inside an `attention` module.
Root cause
- Standard multi-head dot-product attention computes an attention matrix of size (batch_size, num_heads, seq_length, seq_length). The memory complexity is O(N^2) with respect to the sequence length. As sequence length increases, the intermediate activations saved for the backward pass become excessively large and exhaust VRAM.
The fix and how to prevent it
Searchable error signature
RuntimeError: CUDA out of memory. Tried to allocate (large amount) MiBUse this text as a lookup key in logs and upstream issue trackers. It is not presented as a captured customer log. Confirm the cause from your own preceding events, versions, configuration and the cited references.
The fix and the prevention pattern
The root cause is on this page and stays free. A free account adds the exact remediation steps, saved history, and the fix on every entry in the encyclopedia.
Sign up free. Unlock the full analysisNo credit card. Daily allowance follows verified trust tier. Instant access.
Diagnose this failure in VS Code
Select the traceback or open the failed terminal, then run Denpex locally to see the initiating rank, collateral failures, exact fix, and verification command without uploading the log.
Install the free VS Code extensionDon't just read the fix, diagnose your run
The encyclopedia tells you what went wrong. Denpex tells you what went wrong in YOUR training run. With your logs, your config, and your stack.