KnowML

Efficient AI & Systems

Follow one chat request on a single GPU to see why generating text waits on memory instead of math, and how paging, batching, quantization and splitting a model across GPUs each respond to that.

⏱ 33 min readAssumes: attention & KV cache (08), LLM training basics (10)
Start reading

Suppose you've put Llama-3-8B, a language model with about 8 billion parameters, on a single H100 GPU with 80 GB of memory, and a user types a question into your chat product. An H100 is among the most powerful chips you can buy. Yet for the few seconds it spends writing the answer, almost all of its arithmetic units will be sitting idle, waiting for something. This chapter is about what they're waiting for, and about the handful of ideas that keep a GPU like this busy when it's serving real traffic.

A GPU can be slow in two different ways. It can run out of arithmetic, meaning it can't multiply and add fast enough. Or it can run out of data, meaning the numbers it needs haven't arrived from memory yet. The two problems look the same from outside, since in both cases the answer comes back late, but they need opposite fixes, and a fix aimed at the wrong one does nothing. Training a model is mostly the first kind. Generating text for one user is mostly the second, and we'll see exactly how lopsided it is with numbers from our 8B model.

We'll use that one request as our running example. First we'll put numbers on the two limits (sections 1 to 3). Then we'll follow the request through the serving stack: the cache that grows with every token, and the four techniques built to manage it: paged memory, continuous batching, FlashAttention and speculative decoding (sections 4 and 5). After that we'll make the model itself cheaper to move, by storing it in fewer bits (sections 6 and 7). Finally, for the case where the model is too big for one GPU to train, we'll split the work across many (sections 8 and 9), and then look at what goes wrong and how to find out which limit you're up against (sections 10 to 12).

This chapter assumes the attention mechanism and KV cache from chapter 08 and the training basics from chapter 10. New terms get defined where they first appear. If one idea has to stick, make it this one: LLM inference is usually limited by memory bandwidth and rarely by arithmetic, so almost every serving optimization is a way to move fewer bytes for each useful token generated.

01Two ways a GPU can be slow

Two limits, measured in different units

A GPU has two separate hard limits, and they're measured in different units. First there's compute throughput: how many floating-point operations, or FLOPs, it can execute per second. One FLOP is a single addition or multiplication on decimal numbers. Second comes memory bandwidth: how many bytes per second can move between HBM, the GPU's main memory (the 80 GB on our card), and the chip's arithmetic units. Arithmetic units can only work on numbers that have already arrived, so both limits apply to everything you run, and whichever one you hit first sets the speed.

Let's see what that means for one step of our request. To produce a single new token, the model multiplies that token's vector by every weight matrix in the network. Each weight takes part in one multiplication and one addition, so that's 2 FLOPs per parameter. For 8.03 billion parameters, that comes to roughly 16 billion FLOPs. But the weights are stored in 16-bit (fp16) form, 2 bytes each, so the same 8.03 billion parameters occupy 16.06 GB, and every one of those bytes has to travel from HBM to the arithmetic units before it can be used.

Now compare the two costs, using the H100 figures we'll use throughout the chapter: 989 trillion FLOP/s of peak compute and 3.35 trillion bytes/s of peak memory bandwidth. The 16 billion FLOPs take about 0.016 milliseconds. Moving 16.06 GB takes about 4.8 milliseconds. Data takes roughly 295 times longer to arrive than the math takes to finish, so the arithmetic units spend nearly the whole step waiting. (If the data took 4.8 ms and nothing else got in the way, the card could produce at most around 208 tokens a second for one user, however many FLOPs it had.)

Compute-bound and memory-bound

This gives us two regimes, and it helps to have names for them. A workload is compute-bound when its data arrives long before the arithmetic on it finishes. More bandwidth wouldn't help it, and what it needs is fewer FLOPs or faster ones. A workload is memory-bound when the arithmetic finishes almost instantly and the chip then sits idle for the next chunk of data. More compute units wouldn't help it at all, and what it needs is to move less data, or to move it faster.

One number tells you which regime you're in: the arithmetic intensity. It counts how many FLOPs you get for each byte moved. Lots of math per byte fetched means compute-bound, and a little math per byte means memory-bound. Our token step does about one FLOP per byte (we'll compute it exactly in section 3), which is very little.

Why training looks different, and what batching does

Training a large model sits on the other side. It runs big batched matrix multiplications, so each weight you load gets reused across every example in the batch, and each byte fetched does a lot of work. Generating one token for one user is the opposite extreme. Every step drags the entire set of weights, plus the growing KV cache, through memory, to compute output for often a single new token per sequence. That's huge byte traffic for a comparatively tiny amount of arithmetic, and it's the most important systems fact for anyone building or interviewing on LLM serving.

It also tells us what to try first. Suppose the GPU served 32 users' requests in the same step. It would load the weights once, paying the same memory cost as for one user, and get 32 times the useful compute out of them. That moves the work back toward the compute-bound side, where the GPU is earning its keep. So the question for the rest of this part is how far batching can take us, and why decoding is so much worse than processing the prompt in the first place.

02Why generating text waits on memory

To see why decoding is the bad case, we first need to know what the GPU is good at. Modern GPUs have tensor cores, hardware units built for exactly one operation: a multiply-accumulate on small tiles of a matrix, done extremely fast and in parallel, in a single instruction. Most deep learning compute is matrix multiplication. Every linear layer, every attention projection and every feed-forward block reduces to it, so dedicated silicon for that one operation pays off a great deal. But a tensor core can only run as fast as data reaches it, and that's the constraint that bites during generation.

Our request has two phases. Say the user's prompt is 2048 tokens long. Before the model writes anything, it processes the whole prompt in one go, a step called prefill. All 2048 tokens go through the model together, so a large batch of tokens gets multiplied against the same loaded weights, and each weight fetched is reused across thousands of tokens' worth of arithmetic. That's high arithmetic intensity, and prefill is comfortably compute-bound. Training has the same shape, which is why both run well.

Then the model starts writing the answer, and the picture inverts. In autoregressive decoding the model generates one token at a time, one per sequence per step, because each new token depends on the one before it. Every forward pass pulls the whole set of weights through memory (tens or hundreds of gigabytes for bigger models), and the growing KV cache comes along too. All of that is moved to compute output for typically one new token per sequence. You pay the full cost of moving the weights and get only a sliver of arithmetic per byte, so single-request decoding is memory-bandwidth-bound. Tensor cores finish their multiply-accumulate work almost instantly and then wait for the next chunk of weights to arrive from HBM.

Why not buy a GPU with more FLOPs?

Because decoding isn't waiting on FLOPs. If the arithmetic units are already idle most of the time, adding more of them leaves the wait exactly where it was, and the wait is most likely data arriving at 3.35 trillion bytes a second. What helps is, roughly, anything that reduces the bytes moved per token or raises the useful work done per byte, and batching is the first example. When several requests are batched together, one load of the weights is reused across all of their tokens in a single pass. That raises the useful compute per byte moved and pushes decoding back toward the compute-bound side of the ridge point, where the expensive tensor cores stop idling.

That word, the ridge point, is where the line between the two regimes sits, and so far we've only been waving at it. In the next section we write it down as a formula and then counts the bytes and FLOPs for our model.

03The one number that decides everything

Everything so far has been about one ratio, so let's write it as an equation.

  • FLOPs total floating-point operations the computation performs. The numerator: how much math you're asking the chip to do.
  • Bytes moved total bytes read from and written to memory to perform that computation. The denominator: how much data has to physically travel.
  • Threshold every GPU has a ridge point, its peak compute throughput divided by its peak memory bandwidth, in FLOPs/byte. Above that ridge point you're compute-bound, because the chip's math units are the limit. Below it you're memory-bound, because the chip finishes the math and then waits on data.
  • Practical read operations that reuse each loaded byte many times, like the big batched matrix multiplications in most of training, sit above the ridge point. Operations that load a lot of data to do comparatively little math with it, like a single-token decoding step, sit well below it. That's the formal version of "LLM inference is memory-bound."

A picture makes the ridge point easier to hold on to. The chart below is called a roofline. Both axes are logarithmic. Arithmetic intensity runs along the horizontal axis, and the vertical axis shows how many FLOP/s an operation achieves. Two lines come straight off the GPU's datasheet and form a roof over everything: a diagonal for peak memory bandwidth, because at low intensity the data supply caps your speed, and a flat ceiling for peak compute. They meet at the ridge point. Anything to the left of it is memory-bound, and anything to the right is compute-bound.

The roofline — one chart that tells you which resource you are wasting
arithmetic intensity — FLOPs per byte moved (log) achieved FLOP/s (log) slope = peak memory bandwidth peak compute — tensor cores saturated nothing can run above this line decode, batch 1 one token per weight load decode, batch 32 same weights, 32× the useful work prefill / training thousands of tokens per weight load batching MEMORY-BOUND math units idle, waiting on data COMPUTE-BOUND bandwidth to spare, math is the limit ▲ ridge
Both axes are logarithmic, and the two straight lines come off a datasheet: the diagonal is peak memory bandwidth, the flat ceiling is peak compute. Where they meet is the ridge point, and which side of it an operation falls on is what "memory-bound" means. Batching doesn't lift decode off the roof. It slides decode along the roof toward the ridge, so the win is real but bounded: once you reach the ridge, more batching buys nothing. Prefill sits over on the right because it has high intensity inherently, and nobody had to batch it harder.

Now let's count, for our 8B model. Here's code that computes the bytes and FLOPs of one decode step on an H100, at batch sizes 1, 8, 32 and 256. Each step reads every weight (w_bytes) plus the KV cache of every sequence in the batch, each holding 4096 tokens (kv_tok * CTX * B), and does 2 FLOPs per parameter per token. Ridge is the H100's peak FLOP/s divided by its peak bytes/s. A final line computes the 2048-token prefill from section 2 for comparison. The per-token KV figure comes from a formula we'll derive in section 4, so take it as given for now.

Try it Count the bytes and the FLOPs in one decode step, at batch 1 and batch 256
# Llama-3-8B shapes, fp16 weights, decoding on one H100.
P = 8.03e9                      # parameters
L, KVH, HD = 32, 8, 128         # layers, KV heads (GQA), head dim
CTX = 4096                      # tokens already sitting in the cache

w_bytes = P * 2                 # every weight is read on every step
kv_tok = L * KVH * HD * 2 * 2   # K and V, fp16, per token, per seq
ridge = 989e12 / 3.35e12        # peak FLOP/s divided by peak bytes/s

print("weights", w_bytes / 1e9, "GB   KV per token", kv_tok / 1024, "KB")
print("ridge point", round(ridge, 1), "FLOPs/byte\n")

for B in (1, 8, 32, 256):
    moved = w_bytes + kv_tok * CTX * B   # weights plus the whole cache
    flops = 2 * P * B                    # 2 FLOPs per param per token
    ai = flops / moved
    side = "compute" if ai > ridge else "MEMORY"
    print(f"batch {B:4d}  {moved/1e9:6.1f} GB moved  AI {ai:6.2f}"
          f"  {side}-bound")

pre = 2 * P * 2048 / (w_bytes + kv_tok * 2048)   # a 2048-token prefill
print(f"\nprefill 2048   AI {pre:6.2f}  compute-bound")
At batch 1 the intensity is 0.97 FLOPs per byte, close to our estimate of about one. Batch 32 gets 15.46, and batch 256 gets 26.78. Eight times the batch from 32 to 256 bought only 1.7x the intensity, and the middle column explains why. The weights are a fixed 16.06 GB, but the KV cache costs 128.0 KB per token per sequence, so its bytes grow with the batch exactly as the useful work does. Around batch 30 the cache bytes overtake the weight bytes, and past that you're mostly paying to re-read cache. Every decode row is still memory-bound against an H100 ridge of 295.2. Last, one pass over 2048 prompt tokens gets 2014.33, the only number here that clears the ridge.

Read the output as a story about batching. Going from batch 1 to batch 32 does move the point up the roof, from 0.97 to 15.46. But the roof is steep to climb and batching alone never gets us near 295, because every extra sequence brings its own cache that has to be read as well. Prefill gets there for free, since its thousands of tokens share a single read of the weights.

If batching stops helping, what does?

Anything that cuts the bytes. Shrinking the weights shrinks the largest term in the denominator, and that's the idea behind quantization in section 6. Managing the KV cache well lets us fit more sequences into the batch without wasting memory, and that's the subject of the next section. You don't need to memorize ridge-point numbers for specific chips. What matters is that this ratio, and not raw FLOPs or raw bandwidth taken alone, decides where your bottleneck is.

04Serving many users: the KV cache and what's built around it

Training happens once, and inference happens continuously, at whatever your product's traffic is, for the whole life of the model in production. That's why the serving stack gets so much systems attention. Its pieces all trace back to one object, the KV cache, so we start there.

The KV cache grows with every token

Chapter 08 covered the mechanism. During generation, every new token's attention needs the key and value vectors of all the tokens before it, so instead of recomputing them at each step the model stores them. That store is the KV cache. What matters for systems is its size. For each request it grows linearly with the number of tokens, the number of layers, the number of heads and the head dimension, and the stored numbers come in two kinds (keys and values) at a given precision in bytes. For long conversations or long contexts it routinely becomes the biggest consumer of GPU memory during serving, often larger than the model's own weights.

Here's code that applies that formula to our model. It has 32 layers, 8 KV heads and a head dimension of 128. The 8 comes from grouped-query attention (GQA), a design in which several query heads share one key/value head, so far fewer keys and values need storing. kv_tok is the cache per token, with the factor of 2 for K and V. A loop then asks, for four context lengths, how big the cache is for one sequence and for 32 at once, and flags the point where it overtakes the 16.1 GB of weights or overflows the 80 GB card. The final lines redo one case as if the model had 32 KV heads, which would be the plain multi-head attention (MHA).

Try it Size the KV cache from the formula and find where it overtakes the weights
# Llama-3-8B: 32 layers, 8 KV heads after GQA, head dim 128, fp16.
L, KVH, HD, BY = 32, 8, 128, 2
W_GB = 8.03e9 * BY / 1e9              # fp16 weights: 16.1 GB, and fixed

kv_tok = L * KVH * HD * 2 * BY        # x2 because K and V are both cached
print("KV per token per sequence:", kv_tok / 1024, "KB")
print("weights", round(W_GB, 1), "GB on an 80 GB card\n")

print(f"{'ctx':>7} {'batch 1':>9} {'batch 32':>10}")
for ctx in (2048, 8192, 32768, 131072):
    one = kv_tok * ctx / 1e9
    b32 = one * 32
    tag = ""
    if b32 > W_GB:
        tag = "1 seq > weights" if one > W_GB else "b32 > weights"
    if b32 > 80 - W_GB:
        tag += "  OOM"
    print(f"{ctx:>7} {one:8.1f}G {b32:9.1f}G  {tag}")

mha = L * 32 * HD * 2 * BY * 32768 / 1e9   # the same model without GQA
print(f"\n32k ctx, 1 seq:  GQA {kv_tok*32768/1e9:.1f} GB"
      f"   MHA {mha:.1f} GB")
The crossover arrives earlier than it feels like it should. At 128.0 KB per token per sequence, 32 concurrent requests at an 8k context need 34.4 GB of cache, more than double the 16.1 GB of weights that all 32 share. At 32k it's 137.4 GB and the card is long gone. A single 131,072-token sequence carries 17.2 GB by itself, more than the whole model. That last line shows what GQA buys: the same 32k sequence costs 17.2 GB under plain multi-head attention and 4.3 GB with GQA's 8 KV heads. That's a 4x cut to the thing that decides how many users fit on the box, and it's more than a modelling nicety.

So the cache is large, it grows unpredictably (nobody knows in advance how long an answer will be), and it has to live in the same 80 GB as the weights. Four techniques respond to a different part of that problem.

PagedAttention: no memory reserved in advance

The plain way to store a request's cache is one contiguous block of memory, sized for the longest sequence it might reach. That wastes a lot. Most requests probably never get that long, so most of the reserved memory goes unused, and because the block is contiguous, nobody else can use the unused part in the meantime.

Operating systems ran into this decades ago and solved it with virtual memory paging. PagedAttention, introduced with the vLLM serving system, applies the same idea to KV caches. Memory is divided into fixed-size blocks, or pages. A request's cache lives across blocks that needn't be next to each other, and a block table records which physical blocks hold which positions of each sequence. Fragmentation goes away, and memory is handed out exactly as needed instead of reserved upfront. As a bonus, sharing blocks between requests becomes cheap. That helps when many requests begin with the same system prompt.

Continuous batching: no waiting for stragglers

Batching gets a bit harder in practice because requests finish at very different lengths: a one-sentence answer ends long before a detailed explanation. With static batching, the server waits for a whole fixed-size batch to finish before starting the next. By then most of the batch is finished requests waiting on one straggler, and the capacity they freed is wasted.

Continuous batching, also called in-flight batching, removes that lockstep. At every generation step, finished requests are evicted and new ones from the queue slot in immediately, so the batch's membership changes all the time. The GPU stays close to fully used instead of swinging between busy and mostly idle. It seems to be one of the largest practical throughput wins in serving, and it works for the reason from section 1: more real work per byte of weights moved is what pushes you toward compute-bound.

FlashAttention: no wasted traffic inside attention

Those first two techniques decide where cache bytes live and who shares the batch. This one removes waste inside attention itself. FlashAttention computes exactly the same attention output as the standard formula. It changes nothing about the math. It changes the memory-access pattern.

A standard implementation computes the full matrix of attention scores and writes it out to HBM, the GPU's large but relatively slow main memory, before applying the softmax and multiplying by V. FlashAttention instead works through attention in small tiles that fit in SRAM, the GPU's much faster but much smaller on-chip memory, and the full score matrix never gets written out. During the backward pass it recomputes what it needs on the fly and doesn't store that intermediate matrix at all. This is called IO-aware exact attention: the same result with far less data shuffled between slow and fast memory. Since memory bandwidth is usually the real bottleneck, that turns into wall-clock speedups with no approximation.

Speculative decoding: fewer sequential steps

One limit survives all of this. Decoding is sequential: token can't exist until token does, so you can't spread the steps of a single sequence across the GPU the way you can spread a batch of requests. Speculative decoding works around that indirectly. A small, cheap draft model proposes several tokens in a row. Then the large target model checks all of them in one parallel forward pass, because verifying a guess can be done in parallel even though generating one can't. It accepts the proposed tokens up to the first one that differs from what it would have produced anyway, then rejects and corrects from there.

Is speculative decoding an approximation?

No. Its output distribution is provably identical to running the large model alone, so this is an exact sampling procedure and no trade of quality for speed. It's faster whenever the draft model's guesses are often right, because you get several tokens of large-model-verified output for close to the cost of one large-model forward pass. We'll see in section 10 what happens when the guesses are wrong.

05Putting the serving stack together

None of these pieces is exotic by itself, and the interesting part is how they stack. Follow our request through. It's folded into a running batch immediately instead of waiting in a queue. Its KV cache lives in non-contiguous pages instead of one reserved block. Its attention computation never writes the full score matrix to slow memory. And optionally, a small model proposes tokens that the big model verifies in bulk instead of generating them one at a time.

LLM serving pipeline — a request's path through a modern stack
Incoming requests Continuous batching scheduler Paged KV cache block table FlashAttention kernel SRAM-tiled → Speculative decode draft → verify (optional) requests join/leave the batch every step, not in lockstep fixed-size blocks, no contiguous reservation recompute, don't store — stay in fast on-chip memory
Each stage attacks a different bottleneck. Scheduling attacks GPU idle time, the paged KV cache attacks memory fragmentation, FlashAttention attacks wasted memory traffic inside attention, and speculative decoding (dashed, optional) attacks the number of sequential steps itself. They compose, and production stacks run all four together. A kernel, in the labels, is a function that runs on the GPU.

It took a few years to get here. Early transformer serving ran full-precision forward passes, materialized the entire attention matrix in memory and processed one fixed batch at a time. That became wasteful and then impossible as context lengths and models grew. FlashAttention (2022) computed the same attention without writing the huge intermediate matrix to slow memory, PagedAttention (vLLM, 2023) managed the KV cache with OS-style virtual paging, and continuous batching slotted requests in and out at every step. Modern stacks add quantization, speculative decoding and MoE-aware routing on top of that. These days the field increasingly optimizes cost per token, because a model that is 2x faster but not cheaper per useful token doesn't change the economics.

Every technique so far left the model alone and arranged how it runs. But the biggest term in our byte count was the weights themselves, 16.06 GB moved on every step, and we can attack those directly by storing them in fewer bits.

06Moving fewer bytes: quantization, distillation, pruning

Quantization reduces the numeric precision used to store and compute with weights, and sometimes activations (the intermediate values the layers pass along). Instead of FP16, you represent each weight as an int8 or int4, an integer of 8 or 4 bits. Here's the idea in its simplest form. The code below takes a million random weights and squeezes them from 32 bits to 8. It picks a scale so that the largest weight maps to 127, divides every weight by the scale and rounds to the nearest integer (q, one byte each), then multiplies back by the scale (back) to see what the model would effectively use. The last two lines measure the damage.

Try it Store a million weights in 8 bits instead of 32
import numpy as np

rng = np.random.default_rng(0)
w = rng.normal(0, 0.02, 1_000_000).astype(np.float32)   # fake weights

scale = np.abs(w).max() / 127                 # largest weight maps to 127
q = np.round(w / scale).astype(np.int8)       # 1 byte per weight
back = q.astype(np.float32) * scale

print(f"float32: {w.nbytes / 1e6:.1f} MB   int8: {q.nbytes / 1e6:.1f} MB")
print(f"mean absolute error per weight: {np.abs(w - back).mean():.6f}")
print(f"typical weight size:            {np.abs(w).mean():.6f}")
Memory drops from 4.0 MB to 1.0 MB, and the average weight changes by 0.000186. The typical weight has a size of about 0.016, so the rounding error is around 1% of it. These are neat random numbers, and real trained weights are probably less well-behaved, so treat this as a best case.

A quarter of the memory for about 1% error looks like a pretty good bargain. In practice there are two ways to quantize a model. Post-training quantization (PTQ) takes an already-trained model and quantizes it afterward. A calibration step runs a small representative dataset through the model to pick good scaling factors for mapping continuous weight values into the reduced integer range. Quantization-aware training (QAT) instead simulates quantization during training itself, so the weights adapt to tolerate the reduced precision. It's generally more accurate at very low bit-widths, at the cost of retraining or fine-tuning where PTQ just post-processes a finished checkpoint.

Why do int4 weights barely hurt an LLM?

Weight-only int8 or int4 quantization works surprisingly well for LLM inference specifically, and the explanation follows from everything above. Inference is memory-bandwidth-bound, so shrinking the weights shrinks exactly the thing you're bottlenecked on: bytes moved through memory. Keeping the activations at higher precision keeps the actual arithmetic reasonably accurate. You get most of the speed and memory benefit without touching the more precision-sensitive parts of the computation.

The one place the bargain breaks is the scale. In the first experiment a single scale covered every weight. Real LLMs often have a few outlier channels with much larger weights, and an outlier sets the scale for everyone else. The next experiment builds a 512x512 weight matrix, makes row 7 forty times larger than the rest, and quantizes it two ways. per_channel=False uses one scale for the whole tensor, and per_channel=True gives each row its own. For each, it prints the largest and mean error, and how many of the 255 available int8 codes an ordinary row (row 0) ends up using.

Try it Quantize a weight matrix to int8 and watch one outlier row wreck the rest
import torch
torch.manual_seed(0)

W = torch.randn(512, 512) * 0.02  # a plausible weight matrix
W[7] *= 40                        # one outlier channel -- real LLMs
                                  # have them, and they set the scale

def fake_quant(w, per_channel):
    if per_channel:
        amax = w.abs().amax(dim=1, keepdim=True)  # one scale per row
    else:
        amax = w.abs().max()                      # one scale for all
    s = amax / 127                                # symmetric int8
    q = torch.clamp((w / s).round(), -127, 127).to(torch.int8)
    return q, q.float() * s                       # dequantised back

for name, pc in (("per-tensor", False), ("per-channel", True)):
    q, deq = fake_quant(W, pc)
    err = (W - deq).abs()
    codes = q[0].unique().numel()   # distinct levels a normal row uses
    print(f"{name:12s} q {tuple(q.shape)} {q.dtype}")
    print(f"  max err {err.max():.5f}  mean err {err.mean():.6f}"
          f"  row 0 uses {codes:3d}/255 codes")

print("fp16", W.numel() * 2 / 1e6, "MB  ->  int8", W.numel() / 1e6, "MB")
One row ruins it for everyone. Row 7 is 40x larger than the rest, and under a single per-tensor scale it alone sets the step size, so row 0, a completely ordinary row, lands on only 8 of the 255 available int8 codes. Give every row its own scale and row 0 uses 132, and mean absolute error drops from 0.005321 to 0.000139, 38x better for the cost of one extra float per row. Max error barely moves (0.01066 to 0.01065) because it belongs to the outlier row itself, and that row is poorly served either way, so the mean is the number to look at here. Both versions cost the same 0.262144 MB, half of fp16.

Quantization changes how many bits each weight has. Two other techniques change which weights exist at all. Distillation trains a smaller "student" model to match the behavior of a larger "teacher", typically on the teacher's output distribution and not only on hard labels. That gives a smaller, cheaper model, and no longer a compressed version of the same one. Pruning and sparsity remove weights, or whole structural components like attention heads and neurons, that contribute little to the output. Unstructured sparsity removes individual scattered weights. That's hard to accelerate on standard hardware without specialized kernels. Structured sparsity removes aligned blocks, which is easier to get speedups from.

These three techniques complement each other. A real deployment might well run a distilled model that's also quantized and has some structural sparsity applied, stacking savings from three directions at once. Quantization has a cousin on the training side, where the question is how few bits you can use while the model is still learning, and that's next.

07Mixed precision: doing arithmetic in fewer bits

Numbers in deep learning don't need to be stored or computed at the same precision everywhere. Fewer bits is a direct, compounding win. There's less memory to store and move, and that matters a great deal given the bandwidth story above. Arithmetic is faster too, since modern GPUs execute lower-precision matrix multiplications at higher throughput than full FP32. To compare the formats we need one fact about floating-point numbers. Each one is stored as an exponent, which sets how large or small the number can be (its range), plus a mantissa, which holds the significant digits (its precision). The formats differ in how many of their bits go to each.

  • FP32 (32-bit float) is the traditional default: high precision and high range, but expensive in both memory and compute.
  • FP16 (16-bit float) halves both. But its exponent range is narrow, so very large or very small values (common in gradients) can overflow or underflow to zero during training. That historically required loss scaling: multiply the loss by a constant before backprop, then divide it back out so that small gradient values stay representable.
  • BF16 (bfloat16) also uses 16 bits but divides them differently. It keeps FP32's wide exponent range and gives up mantissa precision instead. That wider range is why BF16 tends to be numerically stable for training without loss scaling at all. It represents the same enormous dynamic range as FP32, just less precisely, and range turns out to matter far more than precision for training stability.
  • FP8 pushes further. It's mostly used for inference, and increasingly for training the least precision-sensitive parts of the computation, trading more precision for more speed and memory savings.

In practice the precision is mixed and not uniform. A master copy of the weights stays in FP32, or at least higher precision, for the optimizer's accumulation step, where small updates need to register. Forward and backward passes themselves run in FP16 or BF16. That gives you the speed and memory benefit of low precision where it barely costs anything, in the bulk of the matrix multiplications, and it protects the one part of training that is sensitive to precision loss: the accumulation of many small weight updates over time.

All of this assumed the model still fits on one GPU. For a frontier model it doesn't, and the rest of the chapter is about what to do then.

08Training: splitting the model across devices

A frontier model's parameters, gradients and optimizer state don't fit on one GPU. Even if they did, training on a single GPU would take years. So the work has to be divided, and there are four common ways to divide it. They answer different questions about what to split, and real training runs use several at once because each one alone hits a wall.

Data parallelism: split the batch

Data parallelism is the simplest. You replicate the entire model on every GPU, split the training batch across them, run forward and backward passes independently, then average the gradients before the optimizer step. It scales well, perhaps better than any other strategy here, as long as the model fits in one GPU's memory, and the communication cost is one gradient sync per step. It does nothing when the model is too big for a single device, and that's the situation for every modern frontier LLM.

Tensor parallelism: split each matrix

So the next idea is to split the model itself. Tensor parallelism splits individual matrix multiplications across devices. If you split a weight matrix by columns, each GPU computes part of the output, and the partial results get exchanged mid-layer. You need this when a single layer's weights are too large for one GPU, and data parallelism offers no way around that. The price is that GPUs now communicate within every single layer, so it demands very fast interconnects, and devices doing tensor parallelism are usually on the same physical node.

Pipeline parallelism: split the layers

Pipeline parallelism splits the layers across devices instead. GPU 1 holds layers 1 to 8, GPU 2 holds layers 9 to 16, and activations flow through like an assembly line. It needs far less bandwidth than tensor parallelism, since only activations cross stage boundaries, so it's a bit more workable over the slower links between nodes. The catch is the pipeline bubble: early stages sit idle waiting for the first microbatch (a small slice of the batch) to reach later stages, and later stages sit idle waiting for the first to start. You need many microbatches in flight to keep the idle fraction small.

Expert parallelism: split the experts

Expert parallelism is specific to mixture-of-experts (MoE) models, where a layer's feed-forward part is divided into many separate sub-networks called experts. Different experts live on different devices, and a router sends each token over the network, mid-forward-pass, to only the handful of experts it needs. This scales total parameter count far beyond what any single accelerator could hold, while compute per token stays roughly fixed. The extra capacity costs communication and leaves compute cost alone.

Why combine them?

Each strategy solves a different constraint, and none is sufficient alone at real scale. A frontier training run typically uses tensor parallelism within a node, where links are fastest, pipeline parallelism across nodes, and data parallelism across many copies of that whole pipeline. Expert parallelism sometimes layers on top for MoE architectures. Stacking them is how a few thousand GPUs become one coherent training job. Even then, data parallelism's copies carry a waste of their own, and that's the next section.

09ZeRO and FSDP: sharding what data parallelism duplicates

Plain data parallelism has a wasteful property. Every replica holds a full copy of three things: the model's parameters, its gradients and its optimizer state. For Adam, that optimizer state is itself roughly twice the size of the parameters, because it stores momentum and variance terms for every weight. For a large model, that redundancy alone can make data parallelism impossible before compute even comes into it.

ZeRO (Zero Redundancy Optimizer), and its widely used implementation in PyTorch's FSDP (Fully Sharded Data Parallel), attacks exactly that redundancy. Instead of every device holding a full copy, each of the three things is sharded, meaning split into pieces across the data-parallel devices. Each device permanently holds only its own shard. When a layer's full parameters are needed for a forward or backward computation, the devices briefly gather the missing pieces from each other, use them, and discard them again instead of keeping a persistent full copy.

What this buys you is significant. You can train models far larger than any single GPU's memory could hold, using ordinary data-parallel-style training, without hand-designing a tensor-parallel or pipeline-parallel split of the architecture. It doesn't replace tensor or pipeline parallelism at the largest scales, and you still combine them, but it removes a lot of unnecessary memory duplication that would otherwise push you into more complex parallelism sooner than needed. It's usually the first lever pulled before reaching for more invasive splitting strategies.

We now have a toolbox for both training and serving, and each tool has a price. The next section lines up the parallelism strategies side by side and then looks at three ways these optimizations fail in practice.

10Tradeoffs, and what breaks

The table compares the four parallelism strategies from section 8 along the same four questions: what each one splits, how its devices communicate, when you need it and what it costs.

Data parallelTensor parallelPipeline parallelExpert parallel
What's splitThe batch, across full model replicasIndividual weight matrices, within a layerLayers, across devicesExperts (MoE sub-networks), across devices
Communication patternOne gradient all-reduce per stepFrequent, mid-layer, needs fast interconnectActivations at stage boundaries onlyToken routing to remote experts, mid-forward
When you need itModel fits on one GPU, you want more throughputA single layer's weights don't fit on one GPUMany layers, moderate cross-node bandwidthMoE architecture, want capacity without proportional compute
Main costFull parameter/optimizer redundancy per replica (unless sharded via ZeRO/FSDP)Needs very fast, usually intra-node, linksPipeline bubble — idle stages without enough microbatches in flightRouting imbalance can stall some experts while others queue up

Three failure modes show up in real deployments and in interview follow-ups. The first comes from stacking the compression techniques of section 6. Drop weight precision, then also use speculative decoding, then also run a distilled model: each step is individually reasonable, but together they can add up to a noticeably degraded model if nobody tracks the combined effect as well as each optimization in isolation. The second is that speculative decoding can backfire. If the draft model's proposals are often wrong, you pay for its forward passes and get little acceptance benefit back, so it helps exactly when draft and target agree often and can slow things down when they don't.

The third is MoE routing imbalance, a hard serving problem of its own. If the router sends a disproportionate share of tokens to a handful of popular experts, those devices become bottlenecks while others sit underused, and the expert-parallel overhead of shipping tokens to remote experts can dominate when routing isn't reasonably balanced. That one deserves a closer look, along with two other deployment settings.

11MoE serving, edge inference, and deployment tooling

Serving a mixture-of-experts model adds two problems that dense models don't have. The first is routing imbalance. Some experts get far more tokens than others, because the router's choices are learned and not guaranteed to be even. That makes it a pure load-balancing problem, and an overloaded expert becomes a serving-time bottleneck no matter how fast the underlying hardware is. The second is expert-parallel communication overhead. When experts live on different devices, routed tokens physically travel over the network to reach their assigned expert, and results travel back. A dense model, with everything colocated, doesn't pay that cost. Both are why MoE serving infrastructure is meaningfully more complex than dense-model serving, even though MoE's whole appeal is cheaper compute per token at a given parameter count.

Edge and mobile inference operates under a different constraint entirely. The question isn't how to maximize throughput across a data center's worth of GPUs, but how to fit and run anything useful inside a phone's tight memory, power and thermal budget. That usually means small models to begin with, aggressive quantization (often int4 or lower), and hardware-specific compiled kernels instead of a general-purpose serving framework. The optimization target shifts from throughput at scale to fitting inside a hard, fixed resource ceiling.

ONNX and TensorRT-style deployment tools sit at yet another layer: graph compilation and kernel fusion. A trained model is a graph of many small operations: a matrix multiply, then an activation function, then a normalization, and so on. Run naively, each one is a separate kernel launch, with its own overhead and its own round trip to memory for the intermediate results. Compiling the graph lets the tool fuse adjacent operations into single kernels, skipping the memory round trip between them, and pick optimized kernel implementations for the specific target hardware. It's similar in spirit to what FlashAttention does for attention, applied across an entire model's computation graph.

With so many possible fixes, the remaining question is which one to reach for first.

12Profiling: find the bottleneck before you fix it

The first diagnostic step, before applying any optimization from this chapter, is to find out whether the workload is compute-bound or memory-bound. Profiling tools report GPU utilization, memory bandwidth utilization and time spent in each kernel, and you can read the result two ways.

If compute utilization is high and bandwidth is low, you're compute-bound. Look at reducing FLOPs, with a smaller model, fewer layers or algorithmic changes, or at faster numeric formats. If bandwidth is near its ceiling and the compute units are idle, you're memory-bound. Look at reducing bytes moved through quantization, better batching and KV cache management, and leave alone the arithmetic that was never the bottleneck. Applying a compute-side fix to a memory-bound problem, or the reverse, is a common and completely avoidable mistake, so profile first and optimize second. That's the whole chapter in one habit, and the Summary below collects the pieces.

13Summary

  1. A GPU has two limits, compute and memory bandwidth, and whichever you hit first sets the speed. The fix for one does nothing for the other.
  2. Arithmetic intensity, FLOPs per byte moved, tells you which limit you're at. Compared with the GPU's ridge point (peak FLOP/s divided by peak bytes/s), it says whether you're compute-bound or memory-bound.
  3. Decoding is memory-bound. Each step reads every weight and the whole KV cache to produce one token per sequence, and for our 8B model that's about 1 FLOP per byte against an H100 ridge of 295.
  4. Prefill and training are compute-bound because thousands of tokens share a single read of the weights.
  5. Batching slides decode along the roof, then stops helping, because every extra sequence brings its own KV cache to read.
  6. The KV cache can outgrow the weights. GQA cuts it 4x for this model, PagedAttention stops it wasting reserved memory, and continuous batching keeps the batch full as requests come and go.
  7. FlashAttention and speculative decoding are exact. FlashAttention keeps the score matrix in fast on-chip memory, and speculative decoding verifies a draft model's tokens in one pass, with output identical to the large model alone.
  8. Quantization shrinks exactly what decoding is bottlenecked on. Weight-only int8 or int4 cuts bytes moved, but outlier channels need per-channel scales to avoid wrecking the other rows.
  9. Mixed precision uses fewer bits where it's safe. BF16 keeps FP32's range for stability, and a higher-precision master copy protects the optimizer's small updates.
  10. Large training runs combine data, tensor, pipeline and expert parallelism, and ZeRO/FSDP removes the replicas' redundant copies of parameters, gradients and optimizer state.
  11. Optimizations stack, and so do their costs. Quantization, distillation and speculative decoding together can degrade quality, MoE routing can be imbalanced, and a bad draft model slows decoding. Profile first, optimize second.

14Build this

The roofline turns "memory-bound" from a phrase you repeat into a place on a graph. Your GPU has one, and you can measure it yourself in an afternoon.

Project Measure your GPU's roofline with one matmul ~3 hours · PyTorch + one NVIDIA GPU

Benchmark a single operation, , across a sweep of batch sizes. That is the whole experiment: one frozen weight matrix, one growing batch, and the arithmetic intensity from section 03 worked out by hand from the shapes.

As the batch grows, the same loaded weights do more useful work per byte fetched. The operation walks out of one regime and into the other, and you get to watch it. This one needs a GPU. A free Colab T4 is enough.

  1. Fix at something like 4096×4096 in FP16. Sweep the batch size over powers of two, from 1 up to whatever still fits in memory.
  2. Time each size with torch.cuda.Event. Warm up first, average over many repeats, and call torch.cuda.synchronize() before you trust any timing.
  3. Compute both quantities by hand from the shapes and skip the profiler. FLOPs is . Bytes is the weight matrix plus the input and output activations, 2 bytes apiece. Divide one by the other.
  4. Plot achieved FLOP/s against arithmetic intensity, both axes logarithmic. Then draw two straight lines from your GPU's datasheet: a diagonal at its peak memory bandwidth, and a flat ceiling at its peak FP16 throughput.
  5. Now change one thing. Store in int8, rerun the identical sweep, recompute intensity at 1 byte per weight, and plot the new points on the same axes.
You'll know it worked when your measured points trace both roofs. At small they climb the bandwidth diagonal. At large they flatten out underneath the compute ceiling. The bend between the two is your GPU's ridge point, sitting where your own two datasheet lines cross. Throughput keeps rising with batch size until batching stops helping. That is the knee, and you found it by measurement rather than by argument.
What the change teaches. Halving the bytes per weight does not change the FLOP count at all. It slides every point to the right along the intensity axis. Watch which of them move. The small-batch points are pinned to the diagonal, so sliding right walks them up the bandwidth roof. The large-batch points were already pressed against the compute ceiling, so they have nowhere to go. That is the "why do int4 weights barely hurt an LLM" question from section 06, drawn rather than argued. It is also section 12's advice in one picture: which fix helps depends entirely on which side of the knee you started.

Where this runs in production

Say you're standing up serving infrastructure for a chat product. Three decisions, in order:

  • The KV cache budget per GPU sets your concurrency ceiling. Work out your model's per-token cache size: layers × heads × head_dim × 2 for K and V × precision in bytes.

    Multiply by your target max context length and expected concurrent conversations. That's how much GPU memory is spoken for by cache alone, before you've served a single request beyond what fits.

  • Continuous batching determines how efficiently you use that concurrency. With static batching you leave a large fraction of GPU time on the table waiting for stragglers. With continuous batching you keep the batch close to full almost all the time.
  • PagedAttention lets you hit the budget. No memory wasted on over-reserved, under-used contiguous blocks, and cheap cache sharing for a common system prompt across many concurrent users.

Together these determine realized throughput: tokens served per second per GPU. Combine that with your GPU's hourly cost and you get cost-per-token.

That's the number that decides whether the product is economically viable at the traffic you expect. Not whether any single request feels fast in isolation.

15Interview questions

BeginnerIs LLM inference compute-bound or memory-bound, and why does that matter?

Autoregressive decoding is usually memory-bandwidth-bound, not compute-bound. Each generation step moves the entire set of model weights (and the growing KV cache) through memory to compute output for typically just one new token per sequence — a lot of bytes moved for comparatively little arithmetic, i.e. low arithmetic intensity, which lands you below the GPU's compute/bandwidth ridge point. It matters because it tells you which class of optimization actually helps: reducing bytes moved (quantization, better cache management, batching) helps a memory-bound workload; reducing FLOPs alone does not, since the tensor cores were already sitting idle waiting on data, not on math.

BeginnerWhat is arithmetic intensity, in plain terms?

FLOPs of computation divided by bytes of data moved to support that computation. High arithmetic intensity means you're doing a lot of math per byte fetched, which tends to be compute-bound. Low arithmetic intensity means you're fetching a lot of data to do comparatively little math with it, which tends to be memory-bound. It's the single number that tells you which of the GPU's two hard limits — compute throughput or memory bandwidth — you're actually up against for a given operation.

IntermediateExplain PagedAttention and why it matters for serving.

Naive KV cache allocation reserves one contiguous block per request sized for the worst-case sequence length, which wastes memory (most requests don't reach that length) and fragments memory (that reserved space can't be reused for other requests in the meantime). PagedAttention, from the vLLM paper, borrows the OS virtual-memory idea of paging: the cache is divided into fixed-size blocks, a request's cache can live across non-contiguous blocks, and a block table tracks the mapping. This eliminates fragmentation, lets memory be allocated on demand rather than reserved upfront, and enables efficient sharing of cache blocks across requests — e.g. a shared system prompt — which directly increases how many concurrent requests you can serve out of a fixed pool of GPU memory.

IntermediateWhy does continuous batching improve GPU utilization over static batching?

Static batching waits for every request in a fixed batch to finish generating before starting the next batch, but requests finish at very different lengths — so for much of the batch's lifetime, the GPU is doing useful work for only the few requests still generating while capacity freed up by finished requests sits unused. Continuous batching evicts finished requests and admits new ones from the queue at every generation step instead of at fixed batch boundaries, keeping the batch close to full essentially continuously. Since memory-bound decode steps benefit hugely from batching (more useful compute extracted per byte of weights moved), keeping the batch full translates directly into much higher realized throughput.

IntermediateWhy does speculative decoding not change the output distribution, even though it's using a different, smaller model to help?

The draft model only proposes candidate tokens; it never gets to unilaterally decide what's actually output. The large target model verifies every proposed token in one parallel forward pass and only accepts a proposed token if it matches (via an exact, correct acceptance/rejection procedure) what the target model itself would have sampled. Anything the target model wouldn't have produced gets rejected and regenerated from the target model directly. The math guarantees the final sampled sequence has exactly the same distribution as running the target model alone — the draft model only affects how many large-model forward passes you need, not what gets generated.

DeepDesign a cost-efficient serving system for a high-traffic chat product. What do you actually decide, in order?

Start by profiling to confirm you're memory-bound at your expected sequence lengths and batch sizes — that dictates the rest of the priorities. Pick a quantization level (often int8 weights, sometimes int4) that shrinks memory traffic without unacceptable quality loss, calibrated on representative data. Use PagedAttention-style KV cache management sized against your target max context and expected concurrency, so memory isn't wasted on over-reserved blocks. Run continuous batching so the GPU stays close to fully utilized instead of oscillating with request completions. Use FlashAttention-class kernels so attention itself isn't burning extra memory bandwidth beyond what's structurally necessary. Consider speculative decoding if you have a cheap, reasonably accurate draft model available and users are latency-sensitive. Then measure realized tokens/second/GPU under realistic traffic, multiply by GPU hourly cost, and that gives cost-per-token — the number to actually optimize against, since it captures throughput, latency, and hardware cost together rather than any one of those in isolation.

DeepYou quantize a model, add speculative decoding, and serve a distilled version of it — and quality drops more than any single change would predict. What's going on, and how do you debug it?

Each optimization was validated in isolation, but their errors can compound rather than simply add: a distilled model already approximates the teacher's behavior, quantizing it further compounds that approximation, and if the draft model used for speculative decoding was itself calibrated against the original full-precision model, its acceptance behavior may now be miscalibrated against the new quantized-distilled target, changing acceptance rates and effectively changing what gets generated in edge cases. Debug by isolating variables — evaluate the distilled model alone, then distilled+quantized, then the full stack with speculative decoding, at each step measuring quality on the same eval set, to find where the compounding actually happens rather than assuming it's evenly distributed across all three changes.

16Go deeper

●Now write it yourself

Reading the derivation and being able to produce it are different skills. These are Deep-ML problems that exercise what this page covers — each one is checked against real test cases, not multiple choice.

Matched to this page from Deep-ML's catalogue of 1,380 problems. More at deep-ml.com, and Where to practise covers the other platforms and what each one trains.