Skip to content

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.

Quick answer

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 matches Activation Memory Explosion from Quadratic Attention across every rank in a distributed run and reports which rank failed first, so you act on the initiating node instead of the loudest one.
Memory#Activation Memory

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.

Live diagnosis, no signup

Is this what broke your run? Paste your log.

You're reading about Activation Memory Explosion from Quadratic Attention. Paste your own crash log or traceback below and get the real root cause for YOUR run, not this generic entry. No account, no card. Logs are masked at ingress and never saved to account history.

training_logs.txt
No log to hand? Try one:

3 free diagnoses/day

Want 14 days on the Scale plan?

Request an evaluation code. A verified workplace organization activates up to 50 diagnoses a day, alerts, history, and follow-up questions. No credit card or automatic subscription.

Evaluate one incident

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

SymptomWhy 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.

Searchable error signature

search key
RuntimeError: CUDA out of memory. Tried to allocate (large amount) MiB

Use 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 analysis

No 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 extension

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

Evaluate Denpex on your own logs

Request a Scale evaluation code. A verified workplace organization activates 14 days with up to 50 diagnoses a day. Every account keeps its current diagnosis allowance and gets a verification path. No card or automatic subscription.

We send a single-use code tied to that address. Static provider and TLD rules do not reject valid addresses. Account trust determines the benefit after signup.

Don'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.