Count the matrix-multiply FLOPs of one transformer layer's forward pass for sequence length , model width and feed-forward width (one sequence, ignoring softmax, layer norm and biases). Which part dominates, and at what does attention's term equal the linear-in- part?
Sequence models and transformers, Transformers
Level L3 Intermediate. Length Long. Nature Mat Mathematical.
What's being asked
Whether you can count multiply–adds for each matrix product and compare the and parts.
Key idea
Multiplying an matrix by a matrix costs FLOPs. One layer costs FLOPs, so the part equals the rest at .
Steps
- Q, K, V and output projections are four products : .
- Scores over all heads together: costs . Applying weights to : costs another . Total .
- Feed-forward with : two products and cost .
- Total: .
- Numbers: and . The total is about FLOPs. The FFN alone () is and dominates; the four projections are and the attention scores and weighted sum are .
- Equality: . For shorter sequences the projections and FFN dominate; for longer ones the term does.
Common mistakes
- Counting a multiply–add as one FLOP; the usual convention is two.
- Forgetting that the FFN costs twice as much as the four attention projections combined ( against ).
- Writing for the attention score cost.
How long your answer should be
Half a page; 5 minutes.
Last checked on 4 December 2026.
Read this
Previous in Sequence models and transformers: For a GRU with , what is the direct term in , and why does a small let gradients survive many steps? If at every step, what does that direct path contribute over steps?
Next in Sequence models and transformers: Assume GPT-2 small has layers, , feed-forward width , vocabulary and context length with learned positional embeddings, and output weights tied to the token embedding, where each projection has a bias and each layer has two layer norms. How many parameters does it have?