In a Transformer architecture, which components are the primary contributors to computational cost, and why?
Answer
It depends on sequence length. For short sequences, the feed-forward network (FFN) usually dominates: its two wide GEMMs cost while attention’s quadratic term is still small. For long sequences, multi-head attention takes over: forming the
score matrix costs
and grows much faster than anything else in the block.
(1) Multi-Head Attention: Q/K/V projections cost , but the score matrix
and its product with V cost
, the quadratic term that explodes on long sequences.
(2) Feed-Forward Network: Two dense layers with expansion factor 4 cost , dominant when
is small but
is large.
(3) Crossover Point: Attention overtakes the FFN roughly when ; near
for the classic
design.

Figure 1: FLOP share vs sequence length (): FFN dominates below
; past the crossover, attention’s quadratic cost becomes the bottleneck.
Mathematical Formulation (per block):
Where:
is the sequence length and
; constants count multiply–adds per GEMM.
- The
term comes from multiplying
to form scores, and again to mix values.
- Softmax itself is cheap elementwise work but also scales with
entries.
| Sequence Length n | MHA Share (%) | FFN Share (%) | Dominant Component |
|---|---|---|---|
| 64 | 34.69 | 65.31 | FFN |
| 256 | 38.46 | 61.54 | FFN |
| 1024 | 50.00 | 50.00 | Tie |
| 4096 | 71.43 | 28.57 | MHA |
Table 1: FLOP breakdown at : the tie at n = 1024 marks where quadratic attention catches up with the linear-in-n FFN.

Figure 2: Where the FLOPs live inside one block: only the and
pair grows quadratically with sequence length.
Practical Caveat: The FFN’s dominance at short assumes the standard 4x expansion; shrink the expansion to 1x and the Q/K/V projections become the largest term, while efficient attention variants (sliding-window, linear attention) specifically attack the
term for long contexts.
Leave a Reply