The Shape Blowup
Contrast linear token-embedding tensor growth against quadratic multi-head attention score growth as sequence length increases.
The problem
Dense layers on tabular data grow linearly with row count: doubling a batch doubles activation cells. Sequence models introduce a second axis, sequence length , and pair every token position with every other token position inside self-attention. That pairwise comparison changes tensor shape from three axes [B, T, d_model] to four axes [B, heads, T, T].
Suppose sequences, attention heads, and channels while sequence length steps through , , and across rows [[2, 4, 16, 64], [2, 4, 64, 64], [2, 4, 128, 64]]. How fast does the multi-head attention score tensor grow compared with the token representation tensor?
Work through the numbers
Evaluate both tensor sizes at each sequence length. At , the token tensor holds cells, and the 4-head attention score tensor holds cells, tying at [2048, 2048] because .
Now quadruple sequence length to . The token tensor quadruples linearly to cells. Meanwhile jumps 16-fold from to , pushing the attention score tensor to cells: [8192, 32768], already times larger than the token embeddings.
Double once more to ( times the starting length of ). The token tensor reaches cells (), whereas the attention score tensor hits cells (), giving [16384, 131072].
The live model
Blue bars plot token tensor cells and crimson bars plot attention score cells . At seq_scale , row 2 contrasts 16,384 token cells against 131,072 attention cells ([16384, 131072]). Dragging seq_scale to doubles sequence lengths to [32, 128, 256], doubling token cells to [4096, 16384, 32768] while quadrupling attention cells to [8192, 131072, 524288]. At seq_scale ( up to ), row 2 reaches 65,536 token cells versus 2,097,152 attention cells.
![Paired bars at T = 16, 64, 128 comparing linear token cells [2048, 8192, 16384] against quadratic attention cells [2048, 32768, 131072].](../assets/ml/the-shape-blowup--demo.png)
let seq_scale = scene.parameter("seq_scale", 1.0, 1.0, 4.0, 1.0);
let b = input.select(1, 0);
let h = input.select(1, 1);
let t = input.select(1, 2) * seq_scale;
let d = input.select(1, 3);
let tok = b * t * d;
let attn = b * h * t * t;
let cols = torch.list().add(tok.unsqueeze(1)).add(attn.unsqueeze(1));
let answer = torch.cat(cols, 1);
out.set("token_vs_attention_cells", answer);
{
"token_vs_attention_cells": [
[
2048,
2048
],
[
8192,
32768
],
[
16384,
131072
]
]
}Watch the parameter change
Your turn
In the starter program, the token-tensor column (tok) is already filled in while the attention-score column is a zero placeholder ([[2048, 0], [8192, 0], [16384, 0]]). Set answer so each [B, heads, T, d_model] row reports [token_cells, attention_score_cells]; Check evaluates both the visible three-row table ([[2048, 2048], [8192, 32768], [16384, 131072]]) and unseen batch, head, length, and channel specifications.
![Starter bars computing linear token cells [2048, 8192, 16384] with zero placeholders for the attention score column.](../assets/ml/the-shape-blowup--exercise.png)
let b = input.select(1, 0);
let h = input.select(1, 1);
let t = input.select(1, 2);
let d = input.select(1, 3);
let tok = b * t * d;
let attn = torch.zeros(input.size(0)).to(DType.F64);
let cols = torch.list().add(tok.unsqueeze(1)).add(attn.unsqueeze(1));
let answer = torch.cat(cols, 1);
out.set("token_vs_attention_cells", answer);
{
"token_vs_attention_cells": [
[
2048,
0
],
[
8192,
0
],
[
16384,
0
]
]
}Return [token_cells, attention_score_cells] for each [B, heads, T, d_model] row in input.
Read the result carefully
Solve for the exact crossover length where : canceling leaves , which equals head dimension . Whenever sequence length exceeds (here ), the attention score matrix holds more numbers than the token embedding tensor, by a factor of .
At long contexts such as with , that ratio reaches to per layer. No change in weight count caused that explosion: every attention head has zero extra weights when grows, yet its intermediate [B, heads, T, T] tensor dominates memory.
Recap
- Token representation tensors
[B, T, d_model]contain cells and scale linearly with sequence length . - Multi-head attention score tensors
[B, heads, T, T]contain cells and scale quadratically with . - Attention score cells surpass token embedding cells as soon as sequence length exceeds the per-head width .