Part III / Inference Engineering
Make every pass cheaper
Inference optimization is a stack. Algorithms decide required work; kernels decide how it moves through one device; quantization changes representation; parallelism decides where weights, tokens, and communication live.
Fusion, tiling, compilation, and numerical kernels.
Quantized weights, activations, and KV cache.
Replicate or shard work across devices and nodes.
Optimize the route, cargo, and convoy
To move freight faster, you can shorten the route, pack cargo more densely, load trucks more efficiently, or use several trucks. These interventions are not interchangeable. A fused kernel shortens trips to memory. Quantization packs values into fewer bits. Compilation specializes the route for known shapes. Parallelism divides cargo and introduces coordination.
Every optimization changes one or more of four budgets: operations, bytes, synchronization, and quality. Ask which budget is currently limiting.
The highest-level win is avoiding work: reuse a prefix, shorten a prompt, stop generation correctly, route easy work to a smaller model, or use a more efficient architecture. Runtime work begins after the required computation is clear.
A long-context reasoning fleet
The incident-reasoning engine serves a 70B model across four accelerators. Production has low-latency interactive turns; RL generates high-concurrency long-context rollouts. The team must choose precision, kernels, and parallel layout for both workload classes.
At low decode batch, weight bandwidth is expensive. Weight-only quantization can reduce bytes and make the model fit with fewer devices. At long prefill, attention IO and compute become important; FlashAttention-style kernels and chunking matter. At high rollout concurrency, tensor cores may saturate and communication between model shards can become visible. One "optimized configuration" is unlikely to dominate every point.
The memory calculator supplies a first feasibility check. It does not decide performance. A 4-bit checkpoint that fits on one device may use a dequantization path slower than a well-optimized 8-bit kernel on the target accelerator. Benchmark the exact format, group size, calibration, kernel, and quality gates.
Kernels: move less, launch less, specialize more
A framework graph can materialize intermediates and launch many small GPU kernels. Kernel fusion combines operations so intermediate values remain in registers or on-chip memory and launch overhead falls. Common fusions include normalization plus projections, activation plus multiplication, and sampling operations. Fusion can reduce modularity and make unusual shapes harder to support.
FlashAttention is a canonical IO-aware algorithm: it computes exact attention in tiles and avoids writing the full attention matrix to high-bandwidth memory.[source] It is not "approximate attention" and does not remove quadratic mathematical interactions; it changes the memory schedule.
Compilers and graph capture can specialize shapes, fuse operations, reduce CPU launch overhead, and replay execution efficiently. Dynamic sequence lengths, adapters, structured generation, and many model architectures make specialization difficult. Production runtimes maintain sets of kernels and fallback paths; the slow fallback may dominate if benchmarks cover only the preferred shape.
Quantization is a three-part contract
First, choose what is quantized: weights only, weights and activations, or KV cache. Second, choose representation: integer, floating-point, scale granularity, group size, and outlier handling. Third, choose a hardware kernel that realizes the theoretical benefit.
SmoothQuant moves some activation-scale difficulty into weights through a mathematically equivalent transformation, enabling W8A8 post-training quantization in its studied models.[source] AWQ uses activation statistics to identify and protect salient weight channels for low-bit weight-only quantization.[source] These methods illustrate different answers to "where is quantization error hardest?"
Evaluate perplexity or generic benchmarks only as a screen. The product gate includes tool syntax, structured output, calibration, rare-domain knowledge, long-context behavior, safety, and speculative acceptance. Quantization can affect small logits enough to change sampled trajectories even when aggregate accuracy looks stable.
| Format target | Primary benefit | Characteristic risk |
|---|---|---|
| Weight-only | Reduce model storage and decode bandwidth | Dequant overhead or unsupported fast path |
| Weights + activations | Use lower-precision matrix engines end to end | Activation outliers and calibration sensitivity |
| KV cache | Increase active-token capacity and reduce attention traffic | Long-context quality and kernel complexity |
Parallelism decides what communicates
Data parallelism replicates the whole model and routes different requests to replicas. It scales throughput cleanly when the model fits. Tensor parallelism shards large matrix operations across devices and uses collectives each layer. It lowers per-device weight memory and can reduce per-request latency, but interconnect is on the token path.
Pipeline parallelism places different layer ranges on stages. It can fit large models and process microbatches, but introduces pipeline bubbles and awkward autoregressive scheduling. Expert parallelism distributes mixture-of-experts weights and routes tokens to selected experts, creating all-to-all communication and load-balancing problems. Megatron-LM describes combining tensor, pipeline, and data parallelism at scale.[source]
Sequence parallelism works with tensor parallelism by partitioning selected activations, such as layer-normalization and dropout activations, along the sequence dimension. Context parallelism partitions the full input sequence and its activations across devices, including the attention computation, to make long contexts fit. They share a token-dimension intuition but impose different communication patterns.[source] Prefill/decode disaggregation partitions by phase rather than model tensor. These methods can combine, which is why placement diagrams and byte accounting matter.
Fewer bits reduce nominal storage. Speed depends on whether memory was limiting, whether hardware supports the format, dequantization cost, kernel quality, batch shape, communication, and whether quality remains acceptable.
Optimize in an evidence order
- Define the application workload and quality/SLO contract.
- Profile queue, CPU, kernels, bandwidth, compute, memory, and communication.
- Model the limiting resource and choose an intervention that attacks it.
- Verify numerical and task quality.
- Sweep concurrency and lengths rather than one point.
- Measure operational effects: startup, memory fragmentation, failures, and autoscaling.
A skilled inference engineer can locate the bottleneck in a layered system and choose an intervention that moves it without violating the model or product contract.
References
- FlashAttention, SmoothQuant, and AWQ are primary sources for the named techniques.
- Megatron-LM supports the parallelism discussion; Megatron Core's context-parallelism guide distinguishes sequence and context parallelism; TensorRT-LLM documentation is an official implementation reference.