What the hardware actually does
A model is arithmetic plus memory traffic. Start at the chip and learn why decode is slow and prefill is fast.
By the end of this chapter
- Explain the roofline idea: compute-bound versus memory-bound
- See why matrix-vector (decode) and matrix-matrix (prefill) behave so differently
- Understand why the same input can give different outputs on the same machine
Two ways a chip can be slow
A language model, at the lowest level, is a long list of multiplications and additions on big grids of numbers. A GPU is a chip built to do very many of those at once. But the chip does not work on numbers where they are stored. Every number has to travel from memory to the arithmetic units first.
That gives two limits on how fast a piece of work can run. The compute limit is how many operations per second the chip can perform. The memory limit is how many bytes per second it can read. Which one you hit depends on how much arithmetic you do for each byte you fetch. A roofline chart draws both limits on one plot: a sloped line where memory is the bottleneck, then a flat ceiling where compute is. Work on the slope is memory-bound. Work on the ceiling is compute-bound.
Drag the slider below. It stands for how many tokens share each read of the model weights. With one token, every weight is read from memory and used once, so the dot sits low on the slope.
One more piece of vocabulary. On Apple Silicon the CPU and GPU share a single pool of unified memory, instead of the GPU having its own separate memory. The batch concurrency post points out that this avoids copying data between chips, but it also means the model, the cache and the operating system all compete for the same RAM. You will see why that matters in lesson 1.3.
Memory-bound. The chip waits for bytes. Doing more arithmetic per byte would make it faster.
Matrix-vector versus matrix-matrix
Generating text happens in two phases that we will meet properly in the next chapter. For now, all you need is the shape of the multiply. When a model produces one new token, each weight matrix is multiplied by a single vector. This is a GEMV (general matrix-vector multiply). When a model reads a whole prompt, or when several requests are combined, the same weight matrix is multiplied by a stack of vectors. This is a GEMM (general matrix-matrix multiply).
The arithmetic explains the difference. In the roofline post, Aditya notes that a GEMV on an N by N matrix does roughly 2N² operations while reading the whole weight matrix once, so its operations per byte are low and about the same at every size. A GEMM does roughly 2N³ operations over the same order of bytes, so its operations per byte grow with N.
He then measured it on an Apple M5 Pro with 52 GB of unified memory, using mlx 0.32.0, float16, medians of 9 repetitions. GEMM throughput stayed between 24 and 31 TFLOP/s across sizes from 1,024 to 12,288. GEMV stayed between 277 and 488 GFLOP/s. The ratio was roughly 80 to 85 times at every size tested. Choose a size to see the actual numbers.
At N = 1,024 the matrix-matrix multiply ran 79.0x faster than the matrix-vector one on the same chip.
The post reads the flat GEMM line as a sign that it is compute-bound from the smallest size tested, and the wandering GEMV numbers as a sign that it is memory-bound. It also lists what it did not establish: it never found the size where GEMM stops being compute-bound, and it did not compare against the chip's published peak. It is one chip and one framework.
Why batching helps, and where it stops
Since a GEMM is so much better than a GEMV, you can ask for GEMMs. Batching means serving several requests together so that their next tokens form one matrix instead of several separate vectors. The roofline post puts it this way: that is a change in the shape of the operation, not a scheduling trick.
But each request brings its own memory with it (the KV cache, covered in Chapter 2). In the batch concurrency post, Aditya ran mlx-community/Qwen3-8B-4bit on an M5 Pro with 48 GB, generating 64 tokens per request at 4,096 tokens of context. Total throughput rose from 50.2 tokens per second at batch 1 to a peak of 138.9 at batch 8. Then it fell: 97.7 at batch 16, and 40.0 at batch 32.
Batch 8: 138.9 tok/s total, peak memory 16 GB (31.1% of 48 GB), which is 17.36 tok/s per request (total divided by batch).
At batch 32, peak memory was 50.3 GB, 97.6% of the machine's 48 GB. The run did not stop with an error. It slowed until Aditya killed a later cell after more than 50 minutes, with 18.8 GB of swap in use. The post's reading is that macOS was paging memory to disk, so a memory problem turned into a latency problem.
The post also looked at other context lengths. At 512 tokens the sweep never collapsed and was still rising at batch 32, at 387.1 tok/s. At 16,384 tokens, batch 8 already used 30.5 GB (59.2% of RAM) and throughput had flattened against batch 4. A longer context means a bigger cache per request, so the wall arrives at a smaller batch. Its advice is to think in terms of the share of RAM that batch size times context length will use, not the batch size alone.
Why identical inputs can give different outputs
Computers store most numbers in floating point, which keeps only a fixed number of significant digits. After each addition the result is rounded. Rounding means that adding a list of numbers in a different order can give a slightly different total, even though the maths says the order should not matter.
Here is a real demonstration in 32-bit floats. The true sum of these six numbers is 4.
The true sum is 4. In 32-bit floating point the three orders give 0, 4, 3. Each is a correct run of the arithmetic; the rounding just happens in a different place.
This matters for GPUs because the best strategy for splitting up a multiply depends on the matrix shape. In the non-determinism post, Aditya describes Metal kernels that pick different tile layouts and summation orders for different sizes. So computing one row alone, and computing that row as part of a big batch, can sum in different orders. On an M2 MacBook Pro, that test gave a maximum difference of about 1449. Errors grew with matrix size, from about 145 at 512 by 512 to about 1771 at 4096 by 4096. NumPy on the CPU gave a difference of 0.00.
The post also tested data types. float32 gave consistent small errors, bfloat16 was inconsistent, and float16 was unstable with large values and often produced NaN. Chaining 100 matrix multiplications reached complete numerical breakdown (NaN) by operation 80. The post credits Thinking Machines research on batch invariance for the framing.
When the hardware says no
The same limits show up as error messages. The MLX errors reference separates two that look alike. [METAL] Command buffer execution failed: Insufficient Memory. is Metal's own text relayed by MLX. Aditya hit it when vllm-metal ran after SGLang had already reserved a large KV pool on the same 48 GB machine. The fix on unified memory is nearly always to stop competing with yourself, and run one inference runtime at a time.
[metal::malloc] Attempting to allocate N bytes which is greater than the maximum allowed buffer size is different. One single tensor is larger than Metal's per-buffer ceiling, and you can hit it with plenty of memory free. The post's advice is to reduce the batch or sequence length, or split the operation.
Its closing tip is a good habit: read the prefix. A [METAL] or [metal::...] message means the platform said no. A plain Python ValueError from mlx_lm means the library has not been taught your model yet.
Try it yourself
The batch-invariance test from the non-determinism post is short enough to run as is. It needs the mlx package on an Apple Silicon Mac.
import mlx.core as mx
# Test batch invariance
B, D = 2048, 4096
a = mx.linspace(-1000, 1000, B * D).reshape(B, D)
b = mx.linspace(-1000, 1000, D * D).reshape(D, D)
# Process one row
out1 = mx.matmul(a[:1], b)
# Process all rows, take first
out2 = mx.matmul(a, b)[:1]
print(f"Difference: {mx.abs(out1 - out2).max().item()}")Your number will differ from the 1449 reported on an M2, and that is expected: it depends on your chip and kernels. Try changing the matrix sizes and see whether the difference grows. If you have the benchmark repository from the roofline post, the GEMV and GEMM sweep is part of this script:
./scripts/benchmarks/exp2_bandwidth_roofline.py
No Mac handy? Use the roofline slider above and find, by eye, the number of tokens at which the dot stops moving.