Speed

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 TT, 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 B=2B = 2 sequences, h=4h = 4 attention heads, and dmodel=64d_{\mathrm{model}} = 64 channels while sequence length TT steps through 1616, 6464, and 128128 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 [B⋅T⋅dmodel, B⋅h⋅T2][B \cdot T \cdot d_{\mathrm{model}},\, B \cdot h \cdot T^2] at each sequence length. At T=16T = 16, the token tensor holds 2⋅16⋅64=2,0482 \cdot 16 \cdot 64 = 2{,}048 cells, and the 4-head attention score tensor holds 2⋅4⋅162=2,0482 \cdot 4 \cdot 16^2 = 2{,}048 cells, tying at [2048, 2048] because T=dmodel/h=64/4=16T = d_{\mathrm{model}} / h = 64 / 4 = 16.

Now quadruple sequence length to T=64T = 64. The token tensor quadruples linearly to 2⋅64⋅64=8,1922 \cdot 64 \cdot 64 = 8{,}192 cells. Meanwhile T2T^2 jumps 16-fold from 256256 to 4,0964{,}096, pushing the attention score tensor to 2⋅4⋅4,096=32,7682 \cdot 4 \cdot 4{,}096 = 32{,}768 cells: [8192, 32768], already 44 times larger than the token embeddings.

Double TT once more to 128128 (88 times the starting length of 1616). The token tensor reaches 2⋅128⋅64=16,3842 \cdot 128 \cdot 64 = 16{,}384 cells (8⋅2,0488 \cdot 2{,}048), whereas the attention score tensor hits 2⋅4⋅1282=131,0722 \cdot 4 \cdot 128^2 = 131{,}072 cells (64⋅2,04864 \cdot 2{,}048), giving [16384, 131072].

The live model

Blue bars plot token tensor cells B⋅T⋅dmodelB \cdot T \cdot d_{\mathrm{model}} and crimson bars plot attention score cells B⋅h⋅T2B \cdot h \cdot T^2. At seq_scale =1= 1, row 2 contrasts 16,384 token cells against 131,072 attention cells ([16384, 131072]). Dragging seq_scale to 22 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 =4= 4 (TT up to 512512), 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].
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.
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 B⋅h⋅T2=B⋅T⋅dmodelB \cdot h \cdot T^2 = B \cdot T \cdot d_{\mathrm{model}}: canceling B⋅TB \cdot T leaves T=dmodel/hT = d_{\mathrm{model}} / h, which equals head dimension dheadd_{\mathrm{head}}. Whenever sequence length TT exceeds dheadd_{\mathrm{head}} (here 64/4=1664 / 4 = 16), the attention score matrix holds more numbers than the token embedding tensor, by a factor of T/dheadT / d_{\mathrm{head}}.

At long contexts such as T=8,192T = 8{,}192 with dhead=64d_{\mathrm{head}} = 64, that ratio reaches 128128 to 11 per layer. No change in weight count caused that explosion: every attention head has zero extra weights when TT grows, yet its intermediate [B, heads, T, T] tensor dominates memory.

Recap

  • Token representation tensors [B, T, d_model] contain B⋅T⋅dmodelB \cdot T \cdot d_{\mathrm{model}} cells and scale linearly with sequence length TT.
  • Multi-head attention score tensors [B, heads, T, T] contain B⋅h⋅T2B \cdot h \cdot T^2 cells and scale quadratically with TT.
  • Attention score cells surpass token embedding cells as soon as sequence length TT exceeds the per-head width dmodel/hd_{\mathrm{model}} / h.