Part III / Inference Engineering
The GPU as a factory
A GPU has fast arithmetic machinery and a finite conveyor belt feeding it. Inference speed depends on which one your workload runs out of first.
Peak arithmetic operations per second.
Bytes delivered per second from each memory level.
Arithmetic intensity: useful operations per byte moved.
The kitchen and the pantry door
Imagine one hundred cooks behind a narrow pantry door. If every recipe needs a new ingredient for one chop, cooks wait for deliveries: the kitchen is bandwidth-bound. If one delivered ingredient supports hundreds of operations, the cooks stay busy: the kitchen is compute-bound. Buying more cooks does not help the first case; widening the pantry door or reusing ingredients does.
FLOPs describe arithmetic work. Bandwidth describes data movement. Arithmetic intensity is FLOPs per byte. The larger time floor - compute or movement - names the likely bottleneck.
Modern accelerators have a hierarchy: registers and shared memory near compute units, on-chip caches, high-bandwidth device memory, and interconnects to other devices or hosts. Peak marketing numbers describe different datatypes and assumptions. A kernel realizes only some fraction because shapes, dependencies, occupancy, synchronization, and memory access patterns matter.
Why one-token decode rereads a library
A simplified dense layer multiplies a hidden vector by a large weight matrix. For one sequence's decode token, each weight may participate in roughly one multiply-add. The operation reads many weight bytes for comparatively little arithmetic.
Batch 64 active sequences and the same weight tile can contribute to 64 token computations before it leaves fast memory. Arithmetic work grows faster than model-weight traffic, so arithmetic intensity rises. This is a central inference tension: larger batches improve hardware efficiency but may increase queueing and per-user latency.
The interactive uses a deliberately simple lower bound: compute_time = FLOPs / peak_FLOPs and memory_time = bytes / bandwidth. Real kernels overlap some work and encounter several memory levels, but they cannot beat both physical roofs. Roofline reasoning tells you which optimization family deserves attention before you rewrite code.
Arithmetic intensity creates a decision tree
If a kernel is memory-bound, reducing bytes or increasing reuse can help: lower-precision weights, kernel fusion that avoids intermediate writes, caching, batching, or an algorithm with less memory traffic. If it is compute-bound, faster tensor cores, lower-precision arithmetic, sparsity, fewer operations, or more efficient kernels matter. If communication dominates, parallel placement and interconnect become the target.
| Symptom | Likely bound | Promising direction | Trap |
|---|---|---|---|
| Compute utilization low, HBM bandwidth high | Memory | Quantize, batch, fuse, reuse | Adding theoretical FLOPs |
| Tensor cores saturated, bandwidth moderate | Compute | Cheaper math, better precision path | Optimizing cache that is not limiting |
| Devices idle during collectives | Communication | Placement, overlap, parallel strategy | Tuning one-device kernels only |
| GPU idle while queue is empty | Demand / scheduling | Autoscaling policy, consolidation | Calling it a kernel problem |
NVIDIA's performance guide uses the same basic distinction between math and memory limits and emphasizes that tensor-core utilization depends on dimensions, tiling, and data types.[source] Measure achieved bandwidth and achieved FLOPs with profilers rather than inferring solely from latency.
Attention moves different bytes
During prefill, self-attention forms interactions across prompt positions. A naive implementation materializes large score and probability matrices in device memory. FlashAttention reorganizes exact attention into tiles that stay in faster on-chip memory, reducing high-bandwidth-memory reads and writes.[source] The arithmetic definition is unchanged; the IO schedule changes.
During decode, attention reads the accumulated KV cache for active sequences. Longer context increases those reads. Grouped-query or multi-query attention reduces KV heads and therefore cache traffic.[source] Sliding-window or compressed attention changes which history is read. These architectural choices cross the boundary between model design and inference engineering.
Precision affects storage, bandwidth, and math
A 70-billion-parameter dense model stores 140 GB (about 130.4 GiB) of weights at 16 bits per parameter before runtime overhead. At 8 bits the nominal weight storage is 70 GB (about 65.2 GiB); at 4 bits, 35 GB (about 32.6 GiB). GB uses decimal powers of 1,000; the calculator reports binary GiB. Actual memory includes scales, metadata, embeddings, allocator fragmentation, KV cache, temporary activations, and runtime workspace. Hardware must also offer fast kernels for the chosen format; small storage alone does not guarantee speed.
Quantization can reduce the dominant weight bytes in decode and allow more replicas or larger batches. It may add dequantization work, change accuracy, or interact with speculative acceptance. Evaluate both model quality and end-to-end serving behavior on the target hardware.
Low compute utilization can mean memory saturation, communication waits, small shapes, synchronization, CPU starvation, or no admitted work. "GPU utilization" is not one diagnosis; inspect the corresponding bandwidth, kernels, queues, and traces.
Count useful work, not peak numbers
Peak FLOPs compare hardware capability under specified precision and shapes. Application goodput depends on model operations, memory, scheduler, context distribution, output length, reliability, and SLO. A hardware upgrade that doubles peak compute may barely move memory-bound low-batch decode. A smaller numeric format may double nominal throughput but fail quality gates.
Build a resource model before a benchmark: weight bytes per replica, KV bytes per active token, approximate prefill and decode operations, communication by parallel strategy, and expected arrival distribution. Use its first estimates to choose what to profile, then replace assumptions with measurements.
References
- NVIDIA's official GPU performance guide grounds the hardware terminology.
- FlashAttention is the primary source for IO-aware exact attention.
- Ainslie et al. is the primary source for grouped-query attention.
- Kiely's book supports treating hardware and software optimization as one inference discipline.