Training vs. Inference Challenges

By Ludovico Bessi, Senior ML Engineer at Google ยท Updated

The same model, two completely different workloads.

Same Math, Different Problem

Training and inference run the same forward pass, but almost everything else differs: what is bottlenecked, what is measured, and what "efficient" means. Many mistakes in LLM serving come from carrying training intuitions into inference.

Training Inference
Passes Forward + backward + optimizer step Forward only
Sequence handling All tokens in parallel (teacher forcing) Prefill parallel, decode one token at a time
Typical bottleneck Compute (FLOPs) Memory bandwidth (decode), compute (prefill)
Memory dominated by Optimizer states, gradients, activations Weights and KV cache
Batch Fixed, large, chosen by you Variable, arrives from users
Goal Maximize FLOPs utilization and time-to-train Meet latency targets at minimum cost per token
Lifetime Weeks, once Every request, indefinitely

Compute: Parallel vs. Sequential

During training, the model sees the whole target sequence at once. With teacher forcing, every position predicts its next token in the same forward pass, so a 4,096-token sequence is one big batch of matrix multiplications. GPUs love this; well-tuned training runs reach 40โ€“60% of peak FLOPs (model FLOPs utilization, or MFU).

Inference cannot do this for the output. Token t+1 depends on token t, so decoding is a loop of small forward passes. Each pass multiplies a handful of vectors (one per request in the batch) by every weight matrix. Matrix-vector products have low arithmetic intensity, so decode MFU is often in the single digits unless you batch aggressively.

Rule of thumb for compute: training costs about 6N FLOPs per token (2N forward, 4N backward), while inference costs about 2N FLOPs per generated token.

Memory: What Fills the GPU

Training memory

With mixed precision and the Adam optimizer, each parameter needs roughly 16 bytes before activations: 2 for BF16 weights, 2 for gradients, and 12 for the FP32 master copy and Adam's two moment estimates. A 7B model therefore needs about 112 GB of state โ€” more than one 80 GB GPU โ€” before storing a single activation. Activations come on top and scale with batch size and sequence length. This is why training needs memory-efficiency tricks and sharding across GPUs.

Inference memory

Inference needs only the weights โ€” 2 bytes per parameter in BF16, 1 in INT8, about 0.5 in INT4 โ€” plus the KV cache for every active request. There are no gradients and no optimizer state, and activations are small and short-lived.

But the KV cache is a new kind of pressure: it is state that lives for the whole request, grows with every token, and multiplies with concurrency. For Llama 2 7B (no GQA) it is about 0.5 MB per token in FP16, so a single 4,096-token request holds 2 GB. Serving 30 such requests at once needs 60 GB of KV cache โ€” more than four times the model's weights. The KV cache chapter works through these numbers.

Batching: Chosen vs. Imposed

In training, you pick a batch size, pad or pack sequences to the same length, and every step looks the same.

In inference, requests arrive at random times with different prompt lengths and generate different numbers of tokens. A batch is a moving target: requests join and leave constantly. Naive static batching wastes most of the GPU waiting for the longest request to finish. This is why serving systems use continuous batching and dynamic memory management like PagedAttention.

Latency: Irrelevant vs. Everything

A training step can take seconds; nobody waits on it. In inference a person or a downstream system is waiting, so you care about:

  • Time to first token (TTFT) โ€” queueing plus prefill.
  • Time per output token (TPOT) โ€” the speed of streaming.
  • Tail latency โ€” p95/p99, which batching and long prompts can blow up.

These targets constrain how big a batch can get, which in turn caps throughput. Training has no such tension.

Precision and Numerics

Training needs enough numerical range for gradients, which is why BF16 with FP32 master weights is standard and why FP16 needs loss scaling. Inference only needs the forward pass to stay accurate, so it tolerates much more aggressive compression: INT8 weights are nearly lossless, and 4-bit weight-only quantization usually costs only a small amount of quality. See Memory Optimizations and the deep dive on GPTQ vs AWQ vs GGUF.

Parallelism

Training spreads work across hundreds or thousands of GPUs to finish in weeks, using data, tensor, pipeline and sequence parallelism. Inference prefers the fewest GPUs that fit the model and its KV cache, because every extra GPU adds communication latency to each decode step. Tensor parallelism within one node is common for large models; pipeline parallelism is rarer because it adds latency per token. Many replicas, each as small as possible, is usually cheaper than one giant sharded deployment.

Where the Two Worlds Meet

Training decisions shape inference cost:

  • Architecture: GQA, MLA, sliding-window attention and MoE are chosen at training time and set the size of the KV cache and the FLOPs per token.
  • Context length: the length a model was trained on bounds what it can serve without extension tricks.
  • Model size vs. tokens: compute-optimal training (Chinchilla) suggests about 20 tokens per parameter, but when inference volume is large it is cheaper overall to train a smaller model on many more tokens, which is exactly what the Llama family does.

And some techniques travel the other way: FlashAttention was designed for training and is now standard in inference engines.

Key Takeaways

  1. Training is a parallel, compute-bound batch job; decoding is a sequential, memory-bound online service.
  2. Training memory is dominated by optimizer state and activations; inference memory by weights and the KV cache.
  3. Inference batches are imposed by traffic, which is why continuous batching and paged memory exist.
  4. Inference tolerates far more aggressive quantization than training.
  5. Inference wants the fewest GPUs that fit the model; extra parallelism costs latency.
  6. Training-time architecture choices (GQA, MoE, model size) largely decide serving cost.