← The rest of the block: RMSNorm and SwiGLU⌂ A Language Model, From ScratchQuiz: The Maths II: Inside the Transformer →
Part 4 · The Maths II: Inside the Transformer
Counting what it costs
Per layer, with hidden size d and feed-forward size dff, the forward pass costs (in FLOPs per token, counting a multiply-add as 2):
6d² (QKV) + 2d²
(output) + 4Ld (scores and
values) + 2·nmat·d·dff
(FFN)
d the model width · dff the feed-forward hidden width · L the context length · nmat the number of FFN matrices (2 classic, 3 for SwiGLU)
Set dff = 4d with two matrices and the quadratic term overtakes everything else only when 4Ld > 24d², that is L > 6d.