All courses

Compute, memory and scale

Training and running LLMs is dominated by multiplying large matrices on GPUs. Work is measured in floating-point operations (FLOPs), and memory is measured in bytes: each weight is typically a 2-byte number. Big models are trained across thousands of GPUs that must constantly exchange results. Larger models trained on more data reach predictably lower loss, which is why scale matters. Serving users is a separate, ongoing cost, in which each generated token needs every weight read from memory.

▶ Watch this lesson Start the whole course

What the animation says

Counting the arithmetic

A language model is mostly a stack of big matrices full of learned numbers, called weights. To process one token, its vector gets multiplied by each matrix. Here's a tiny case: three by three weights times a vector of three. Nine multiplies, and nearly as many adds. Each multiply or add is one FLOP. In big matrices, every weight costs about two FLOPs per token. Training must also work out how to adjust each weight. Per weight, per token, roughly how many FLOPs then?

The training bill

Six. Working out the adjustments roughly triples the work, so training costs about six FLOPs per weight, per token. With N weights and D training tokens, the total is about six times N times D. Take a seven billion weight model and two trillion tokens. That's eight point four times ten to the twenty-two FLOPs. One GPU doing four hundred trillion FLOPs a second would need about fifty-eight thousand hours. Split across a thousand GPUs, that's about two and a half days, with GPUs exchanging results the whole time.

Holding the weights

Arithmetic is one bill. Memory is the other, and it's counted in bytes. Each weight is usually stored in two bytes. So N weights need about two N bytes. Our seven billion weight model needs fourteen gigabytes, just for the weights. When the weights don't fit on one GPU, they're split across several. And you can only use whole GPUs. Try it: twenty billion weights, GPUs with sixteen gigabytes each. Tap the last GPU you need.

One token, one user

Now serving. For one user, each new token needs every weight read from memory once. For our model, that's fourteen gigabytes read, and about fourteen billion FLOPs of arithmetic. This GPU reads about three point three trillion bytes a second, and computes four hundred trillion FLOPs a second. So which takes longer for each new token?

Memory sets the speed

Reading the weights. This line divides FLOPs by compute speed: about thirty-five thousandths of a millisecond. This line divides bytes by read speed: over four milliseconds. So the GPU mostly waits on memory, about a hundred and twenty times longer than it computes. That caps one user at about two hundred thirty-six tokens a second. And serving is paid for every token, for as long as people use the model.

Why bigger keeps winning

So why do labs keep building bigger models trained on more text? The loss measures how badly the model predicts the next token. Lower is better. As weights and training tokens grow, the loss falls along a smooth, predictable curve. So bigger models on more text buy lower loss, and you can predict how much. Remember: two N FLOPs per token, six N D to train, two N bytes to store the weights.

What do LLMs actually spend?

Training a frontier model takes thousands of GPUs for weeks. A chat reply streams out at a roughly steady pace, and every user's reply costs money. What is being consumed here: arithmetic, memory, or both? What limits the speed? And why do labs keep building bigger models trained on more text?

Big matrices, counted in FLOPs and bytes

An LLM is mostly a stack of large matrices of learned numbers, called weights. Processing one token multiplies its vector by each matrix. Work is counted in FLOPs (floating-point operations), where one multiply or one add is one FLOP. Every weight is used for about one multiply and one add, so a token costs about 2 FLOPs per weight. Training must also work out how to adjust every weight, which roughly triples the work. Each weight is usually stored in 2 bytes.

The rough rules

Multiplying $(m\times k)$ by $(k\times n)$ gives $mn$ outputs. Each output needs $k$ multiplies and about $k$ adds, so the product costs $2mkn$ FLOPs. With $N$ weights: - generate one token: $\approx 2N$ FLOPs - train on $D$ tokens: $C \approx 6ND$ FLOPs - store the weights: $\approx 2N$ bytes When the model or the work is too big for one GPU, many GPUs each do a part and constantly exchange results. The loss is a number measuring how badly the model predicts the next token, so lower is better. It falls along a smooth, predictable curve as $N$ and $D$ grow.

Training a 7B model

Take $N = 7\times10^9$ and $D = 2\times10^{12}$ tokens. - Weights: $2N = 14$ GB. - Compute: $C = 6\cdot 7\times10^9 \cdot 2\times10^{12} = 8.4\times10^{22}$ FLOPs. - Suppose one GPU sustains $4\times10^{14}$ FLOP/s. On one GPU, the time is $(8.4\times10^{22}) / (4\times10^{14}) = 2.1\times10^8$ s, about 58,000 GPU-hours. Split across 1,000 GPUs, that is about 58 hours, or 2.4 days, with the GPUs exchanging results the whole time.

Serving: reading weights sets the speed

For one user, each new token needs every weight read from memory once. For this 7B model on this GPU, the code prints math 0.035 ms and read 4.24 ms. Reading takes about 120× longer than the arithmetic, so the GPU mostly waits on memory, capping speed at about 236 tokens/s. Serving is an ongoing cost, paid for every token, for as long as people use the model.
N = 7e9                # weights
bytes_w = 2 * N        # 14e9 bytes of weights
flops_tok = 2 * N      # FLOPs per generated token
gpu_flops = 4e14       # sustained FLOP/s
gpu_read = 3.3e12      # memory read speed, bytes/s
t_math = flops_tok / gpu_flops   # arithmetic time
t_read = bytes_w / gpu_read      # time to read all weights
print(f"math {t_math*1e3:.3f} ms, read {t_read*1e3:.2f} ms")
print(f"max ~{1/t_read:.0f} tokens/s for one user")

Remember

Work is counted in FLOPs: about $2N$ per token, and $C\approx 6ND$ to train. Memory is counted in bytes: about $2N$ for the weights. Training is a huge one-time arithmetic bill spread over thousands of communicating GPUs. Serving is an ongoing bill, and its speed is set by reading every weight for each token. Larger $N$ and $D$ buy predictably lower loss.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.