{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"flops-of-a-matrix-multiply","title":"Counting the floating-point operations in a layer and a training run","url":"https://addaly.com/learn/maths-you-need/flops-of-a-matrix-multiply","position":59,"of":76,"minutes":9,"answer":"A matrix multiply costs 2mnk FLOPs, so a forward pass costs about two FLOPs per parameter per token and a training step about six.","keyIdea":"A matrix multiply costs 2mnk FLOPs, so a forward pass costs about two FLOPs per parameter per token and a training step about six, which lets you compute that seven billion parameters on a trillion tokens is 4.2 × 10²² FLOPs before anyone tells you the price."},"body":{"format":"markdown","text":"## One entry, one dot product\n\nModule 2 showed that every entry of a matrix product is a dot product between a row of the left matrix and a column of the right. A dot product of length `k` is `k` multiplications and `k` additions: `2k` floating-point operations, or FLOPs. Multiplying an `m × k` matrix by a `k × n` one produces `m × n` entries, so\n\n```\nFLOPs of (m × k) · (k × n) = 2 × m × n × k\n```\n\nThat one line is most of the cost accounting of deep learning.\n\nA linear layer taking a 4,096-dimensional vector to another 4,096-dimensional vector is a `4096 × 4096` matrix applied to one column:\n\n```\n2 × 4096 × 4096 × 1 = 33.5 million FLOPs\n```\n\nfor one token. The matrix has 16.8 million parameters, and each parameter was used exactly once, for one multiplication and one addition. That generalises.\n\n## The rule: two FLOPs per parameter per token\n\nIn a forward pass, every weight in every linear layer touches each token once, multiplying and adding. So\n\n```\nforward FLOPs per token ≈ 2 × (number of parameters)\n```\n\nA seven-billion-parameter model spends about 14 billion FLOPs to process, or generate, one token. Embedding lookups cost nothing, being a table read, and the attention scores are an extra term the next lesson counts separately; for ordinary context lengths the rule above is within twenty per cent.\n\n## Training is three times a forward pass\n\nBackpropagation, from module 3, computes two things per layer: the gradient with respect to the layer's input, to pass backward, and the gradient with respect to its weights. Each is a matrix multiply of the same size as the forward one. So the backward pass costs about twice the forward, and a full training step, forward plus backward, is about three times the forward:\n\n```\ntraining FLOPs ≈ 6 × parameters × tokens\n```\n\nSeven billion parameters on one trillion tokens:\n\n```\n6 × 7 × 10^9 × 10^12 = 4.2 × 10^22 FLOPs\n```\n\nThat number is the one to carry. Everything about the cost of a training run follows from it and from how fast the hardware actually runs.\n\n## From FLOPs to time\n\nA data-centre GPU of the A100 generation is rated at 312 trillion FLOPs per second in half precision. Nobody achieves that. Real training runs sustain around 40 per cent of the rating, for reasons the arithmetic-intensity lesson explains, so call it 125 trillion per second.\n\n```\n4.2 × 10^22 / 1.25 × 10^14 = 3.4 × 10^8 seconds ≈ 10.7 years on one GPU\n                                             ≈ 3.9 days on 1,000 GPUs\n```\n\nThat is roughly what a seven-billion-parameter model on a trillion tokens costs, and the arithmetic is two multiplications long. The course `how-llms-work` turns it into money; the point here is that you can do it yourself, from a parameter count and a token count, in under a minute.\n\n> Figure: From a parameter count to days on a cluster. Steps: 1. 7 billion parameters (the whole cost model starts from this one count); 2. 6 FLOPs per parameter per token (two for the forward pass, four for the backward); 3. 1 trillion training tokens (6 times 7 billion times 1 trillion is 4.2 times ten to the 22 FLOPs); 4. 125 trillion FLOPs a second (40 per cent of an A100's rating: 10.7 years on one card, 3.9 days on a thousand). Two multiplications and a division. Every published training run can be checked this way in under a minute, from numbers that are always in the announcement, and long before anyone quotes a price.\n\nOn a laptop CPU, NumPy manages on the order of `10^11` FLOPs per second on a well-shaped matrix multiply. A forward pass of the seven-billion model, at 14 billion FLOPs per token, would take 0.14 seconds per token if FLOPs were the only limit. They are not, and the third lesson from here says what is, but the FLOP count sets the floor.\n\n## Batches do not change the count\n\nProcessing a batch of 32 tokens through the same layer is `2 × 4096 × 4096 × 32` FLOPs: thirty-two times the work, for thirty-two times the tokens. The FLOPs per token are unchanged. What changes with batching is *efficiency*, how close the hardware gets to its rating, and that is a story about memory, not arithmetic.\n\n## Where the rule misleads\n\n- **Attention is extra, and grows with context.** The scores between every pair of tokens add `4 × L × d` FLOPs per token per layer for context length `L` and width `d`. At `L = 4096` and `d = 4096` that is one sixth of the linear layers' cost; at `L = 24,576` it equals them. The next lesson has the arithmetic.\n- **Mixture-of-experts models** have more parameters than they use per token. Count the parameters that a token actually passes through, not the total; a model with 400 billion parameters that routes each token through 40 billion costs like a 40-billion model per token.\n- **FLOPs are not time.** A layer that does `10^9` FLOPs on `10^9` bytes of weights is waiting for memory, and no FLOP count sees that.\n- **Rated FLOPs are for one shape.** The 312 trillion figure applies to large half-precision matrix multiplies. Elementwise operations, small matrices and single precision get a fraction of it.\n\n## Do it once\n\n```python\nimport numpy as np, time\nA = np.random.rand(4096, 4096).astype(np.float32)\nx = np.random.rand(4096, 32).astype(np.float32)\nt = time.perf_counter(); A @ x; dt = time.perf_counter() - t\nprint(2 * 4096 * 4096 * 32 / dt / 1e9, \"GFLOP/s\")\n```\n\nRun it on whatever you have. The number you get is the constant that turns every FLOP count in this module into seconds on your own machine."},"check":{"question":"A team plans to train a 3-billion-parameter model on 600 billion tokens. Using the standard rule, roughly how many FLOPs is that, and what is the rule made of?","options":[{"text":"About 1.8 × 10²¹, from two FLOPs per parameter per token for the forward pass alone.","correct":false,"tempting":"Counts only the forward pass; training also runs the backward pass, which costs about twice the forward, giving six per parameter per token."},{"text":"About 1.1 × 10²², from six FLOPs per parameter per token.","correct":true},{"text":"About 3.6 × 10²¹, from two FLOPs per parameter per token doubled for the backward pass.","correct":false,"tempting":"Underestimates the backward pass, which computes two gradients per layer and costs about twice the forward, not the same as it."},{"text":"It cannot be estimated without knowing the number of layers and the hidden width.","correct":false,"tempting":"The parameter count already summarises the architecture; the FLOP rule needs only parameters and tokens, with attention as a modest correction."}]},"prev":{"slug":"big-o-in-plain-terms","title":"Big-O, and the four growth rates you will meet","url":"https://addaly.com/learn/maths-you-need/big-o-in-plain-terms"},"next":{"slug":"quadratic-costs-and-pairwise-work","title":"Anything pairwise is quadratic: distances, attention and deduplication","url":"https://addaly.com/learn/maths-you-need/quadratic-costs-and-pairwise-work"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Counting the floating-point operations in a layer and a training run', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/flops-of-a-matrix-multiply (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Counting the floating-point operations in a layer and a training run","courseTitle":"The Maths You Actually Need","module":{"index":7,"slug":"cost-and-scale","title":"Counting the cost: complexity and the arithmetic of scale"},"answer":null,"keyIdea":"A matrix multiply costs 2mnk FLOPs, so a forward pass costs about two FLOPs per parameter per token and a training step about six, which lets you compute that seven billion parameters on a trillion tokens is 4.2 × 10²² FLOPs before anyone tells you the price.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"Two multiplications and a division. Every published training run can be checked this way in under a minute, from numbers that are always in the announcement, and long before anyone quotes a price.","steps":[{"label":"7 billion parameters","detail":"the whole cost model starts from this one count"},{"label":"6 FLOPs per parameter per token","detail":"two for the forward pass, four for the backward"},{"label":"1 trillion training tokens","detail":"6 times 7 billion times 1 trillion is 4.2 times ten to the 22 FLOPs"},{"label":"125 trillion FLOPs a second","detail":"40 per cent of an A100’s rating: 10.7 years on one card, 3.9 days on a thousand"}],"title":"From a parameter count to days on a cluster","type":"flow"},"alt":"Figure: From a parameter count to days on a cluster. Steps: 1. 7 billion parameters (the whole cost model starts from this one count); 2. 6 FLOPs per parameter per token (two for the forward pass, four for the backward); 3. 1 trillion training tokens (6 times 7 billion times 1 trillion is 4.2 times ten to the 22 FLOPs); 4. 125 trillion FLOPs a second (40 per cent of an A100’s rating: 10.7 years on one card, 3.9 days on a thousand). Two multiplications and a division. Every published training run can be checked this way in under a minute, from numbers that are always in the announcement, and long before anyone quotes a price."}},"blocks":[{"t":"h2","id":"one-entry-one-dot-product","runs":[{"text":"One entry, one dot product"}]},{"t":"p","runs":[{"text":"Module 2 showed that every entry of a matrix product is a dot product between a row of the left matrix and a column of the right. A dot product of length "},{"text":"k","code":true},{"text":" is "},{"text":"k","code":true},{"text":" multiplications and "},{"text":"k","code":true},{"text":" additions: "},{"text":"2k","code":true},{"text":" floating-point operations, or FLOPs. Multiplying an "},{"text":"m × k","code":true},{"text":" matrix by a "},{"text":"k × n","code":true},{"text":" one produces "},{"text":"m × n","code":true},{"text":" entries, so"}]},{"t":"code","lang":"","label":"Text","text":"FLOPs of (m × k) · (k × n) = 2 × m × n × k"},{"t":"p","runs":[{"text":"That one line is most of the cost accounting of deep learning."}]},{"t":"p","runs":[{"text":"A linear layer taking a 4,096-dimensional vector to another 4,096-dimensional vector is a "},{"text":"4096 × 4096","code":true},{"text":" matrix applied to one column:"}]},{"t":"code","lang":"","label":"Text","text":"2 × 4096 × 4096 × 1 = 33.5 million FLOPs"},{"t":"p","runs":[{"text":"for one token. The matrix has 16.8 million parameters, and each parameter was used exactly once, for one multiplication and one addition. That generalises."}]},{"t":"h2","id":"the-rule-two-flops-per-parameter-per-token","runs":[{"text":"The rule: two FLOPs per parameter per token"}]},{"t":"p","runs":[{"text":"In a forward pass, every weight in every linear layer touches each token once, multiplying and adding. So"}]},{"t":"code","lang":"","label":"Text","text":"forward FLOPs per token ≈ 2 × (number of parameters)"},{"t":"p","runs":[{"text":"A seven-billion-parameter model spends about 14 billion FLOPs to process, or generate, one token. Embedding lookups cost nothing, being a table read, and the attention scores are an extra term the next lesson counts separately; for ordinary context lengths the rule above is within twenty per cent."}]},{"t":"h2","id":"training-is-three-times-a-forward-pass","runs":[{"text":"Training is three times a forward pass"}]},{"t":"p","runs":[{"text":"Backpropagation, from module 3, computes two things per layer: the gradient with respect to the layer’s input, to pass backward, and the gradient with respect to its weights. Each is a matrix multiply of the same size as the forward one. So the backward pass costs about twice the forward, and a full training step, forward plus backward, is about three times the forward:"}]},{"t":"code","lang":"","label":"Text","text":"training FLOPs ≈ 6 × parameters × tokens"},{"t":"p","runs":[{"text":"Seven billion parameters on one trillion tokens:"}]},{"t":"code","lang":"","label":"Text","text":"6 × 7 × 10^9 × 10^12 = 4.2 × 10^22 FLOPs"},{"t":"p","runs":[{"text":"That number is the one to carry. Everything about the cost of a training run follows from it and from how fast the hardware actually runs."}]},{"t":"h2","id":"from-flops-to-time","runs":[{"text":"From FLOPs to time"}]},{"t":"p","runs":[{"text":"A data-centre GPU of the A100 generation is rated at 312 trillion FLOPs per second in half precision. Nobody achieves that. Real training runs sustain around 40 per cent of the rating, for reasons the arithmetic-intensity lesson explains, so call it 125 trillion per second."}]},{"t":"code","lang":"","label":"Text","text":"4.2 × 10^22 / 1.25 × 10^14 = 3.4 × 10^8 seconds ≈ 10.7 years on one GPU\n                                             ≈ 3.9 days on 1,000 GPUs"},{"t":"p","runs":[{"text":"That is roughly what a seven-billion-parameter model on a trillion tokens costs, and the arithmetic is two multiplications long. The course "},{"text":"how-llms-work","code":true},{"text":" turns it into money; the point here is that you can do it yourself, from a parameter count and a token count, in under a minute."}]},{"t":"p","runs":[{"text":"On a laptop CPU, NumPy manages on the order of "},{"text":"10^11","code":true},{"text":" FLOPs per second on a well-shaped matrix multiply. A forward pass of the seven-billion model, at 14 billion FLOPs per token, would take 0.14 seconds per token if FLOPs were the only limit. They are not, and the third lesson from here says what is, but the FLOP count sets the floor."}]},{"t":"h2","id":"batches-do-not-change-the-count","runs":[{"text":"Batches do not change the count"}]},{"t":"p","runs":[{"text":"Processing a batch of 32 tokens through the same layer is "},{"text":"2 × 4096 × 4096 × 32","code":true},{"text":" FLOPs: thirty-two times the work, for thirty-two times the tokens. The FLOPs per token are unchanged. What changes with batching is "},{"text":"efficiency","i":true},{"text":", how close the hardware gets to its rating, and that is a story about memory, not arithmetic."}]},{"t":"h2","id":"where-the-rule-misleads","runs":[{"text":"Where the rule misleads"}]},{"t":"ul","items":[[{"text":"Attention is extra, and grows with context.","b":true},{"text":" The scores between every pair of tokens add "},{"text":"4 × L × d","code":true},{"text":" FLOPs per token per layer for context length "},{"text":"L","code":true},{"text":" and width "},{"text":"d","code":true},{"text":". At "},{"text":"L = 4096","code":true},{"text":" and "},{"text":"d = 4096","code":true},{"text":" that is one sixth of the linear layers’ cost; at "},{"text":"L = 24,576","code":true},{"text":" it equals them. The next lesson has the arithmetic."}],[{"text":"Mixture-of-experts models","b":true},{"text":" have more parameters than they use per token. Count the parameters that a token actually passes through, not the total; a model with 400 billion parameters that routes each token through 40 billion costs like a 40-billion model per token."}],[{"text":"FLOPs are not time.","b":true},{"text":" A layer that does "},{"text":"10^9","code":true},{"text":" FLOPs on "},{"text":"10^9","code":true},{"text":" bytes of weights is waiting for memory, and no FLOP count sees that."}],[{"text":"Rated FLOPs are for one shape.","b":true},{"text":" The 312 trillion figure applies to large half-precision matrix multiplies. Elementwise operations, small matrices and single precision get a fraction of it."}]]},{"t":"h2","id":"do-it-once","runs":[{"text":"Do it once"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np, time\nA = np.random.rand(4096, 4096).astype(np.float32)\nx = np.random.rand(4096, 32).astype(np.float32)\nt = time.perf_counter(); A @ x; dt = time.perf_counter() - t\nprint(2 * 4096 * 4096 * 32 / dt / 1e9, \"GFLOP/s\")"},{"t":"p","runs":[{"text":"Run it on whatever you have. The number you get is the constant that turns every FLOP count in this module into seconds on your own machine."}]}],"check":{"question":[{"text":"A team plans to train a 3-billion-parameter model on 600 billion tokens. Using the standard rule, roughly how many FLOPs is that, and what is the rule made of?"}],"options":[{"text":[{"text":"About 1.8 × 10²¹, from two FLOPs per parameter per token for the forward pass alone."}],"correct":false,"tempting":[{"text":"Counts only the forward pass; training also runs the backward pass, which costs about twice the forward, giving six per parameter per token."}]},{"text":[{"text":"About 1.1 × 10²², from six FLOPs per parameter per token."}],"correct":true,"tempting":null},{"text":[{"text":"About 3.6 × 10²¹, from two FLOPs per parameter per token doubled for the backward pass."}],"correct":false,"tempting":[{"text":"Underestimates the backward pass, which computes two gradients per layer and costs about twice the forward, not the same as it."}]},{"text":[{"text":"It cannot be estimated without knowing the number of layers and the hidden width."}],"correct":false,"tempting":[{"text":"The parameter count already summarises the architecture; the FLOP rule needs only parameters and tokens, with attention as a modest correction."}]}],"order":[2,3,0,1],"why":null},"prev":{"slug":"big-o-in-plain-terms","title":"Big-O, and the four growth rates you will meet","minutes":8},"next":{"slug":"quadratic-costs-and-pairwise-work","title":"Anything pairwise is quadratic: distances, attention and deduplication","minutes":9}}}