Optimizing Transformer Inference & Training
Techniques for making transformers faster and more memory-efficient: operator fusion, mixed precision, gradient checkpointing, tensor parallelism, sequence parallelism, speculative decoding, and continuous batching.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Inference Bottleneck: Memory-Bound vs Compute-Bound
- Mixed Precision Training (FP16/BF16)
- Operator Fusion and FlashAttention
- Gradient Checkpointing
- Tensor Parallelism
- Speculative Decoding
- Continuous Batching
- Quantization for Inference
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Classify transformer operations as memory-bound or compute-bound.
- Derive the arithmetic intensity of attention and FFN layers.
- Explain how mixed precision maintains accuracy with loss scaling.
- Derive the memory savings from gradient checkpointing.
- Explain speculative decoding and derive its expected speedup.
Notation
- — batch size
- — sequence length
- — model dimension
- — arithmetic intensity (FLOPs/byte)
- — memory bandwidth (bytes/second)
- — compute throughput (operations/second)
Core Intuition
Transformers are bottlenecked by different resources at different stages: training is typically compute-bound (massive matrix multiplications), while inference (especially autoregressive generation) is often memory-bandwidth-bound (loading model weights for each token). Optimization strategies must target the actual bottleneck.
FlashAttention Tiling
The Inference Bottleneck: Memory-Bound vs Compute-Bound
Arithmetic intensity (AI): ratio of FLOPs to bytes transferred.
A kernel is:
- Compute-bound if (GPU compute is the bottleneck).
- Memory-bound if (memory bandwidth is the bottleneck).
For autoregressive generation (batch size 1, generating one token at a time):
- Each FFN layer loads parameters (bytes in FP16) but performs FLOPs on a single vector.
- FLOPs/byte.
- GPU A100: FLOPS/BW . Since : severely memory-bound.
Implication: For inference, reducing memory footprint (quantization, pruning) helps more than adding compute.
Mixed Precision Training (FP16/BF16)
Strategy: Keep a master copy of weights in FP32; perform forward/backward passes in FP16/BF16.
Loss scaling: FP16 has limited dynamic range ( to ). Small gradients underflow to zero. Solution: multiply loss by a scale factor ; divide gradients by before weight update.
BF16 advantage: Same dynamic range as FP32 (8 exponent bits) but reduced precision (7 mantissa bits vs 23). No loss scaling needed. Standard for LLM training.
Memory savings: Model weights: 2x reduction. Activations: 2x reduction. Optimizer states remain FP32.
Operator Fusion and FlashAttention
Problem: Each elementwise operation (add, multiply, softmax) requires a separate GPU kernel launch and HBM round-trip.
Solution: Fuse multiple operations into a single kernel:
- Fuse bias + activation: one kernel instead of two.
- Fuse LayerNorm into attention output.
- FlashAttention: Fuses Q×K, scaling, masking, softmax, and ×V into a single tiled kernel.
FlashAttention memory: Standard attention stores full matrix. FlashAttention never materializes it — only stores intermediate values (row-wise max and sum for online softmax).
Speedup: 2–4x for training, enables 4–16x longer contexts.
Gradient Checkpointing
Problem: Storing all activations for backprop requires memory.
Solution: Only store activations at checkpointed layers; recompute others during backward pass.
Memory: With checkpoints every layers: memory reduces from to .
Cost: ~33% additional compute (recomputing one forward pass segment).
Tensor Parallelism
Split large matrix multiplications across GPUs.
For FFN layer :
- Split column-wise across GPUs: each GPU computes .
- Split row-wise: each GPU computes partial output.
- All-reduce to combine results.
For attention: Split heads across GPUs (each GPU handles heads).
Communication: 2 all-reduce operations per layer (one for attention, one for FFN). Each all-reduce transfers bytes.
Speculative Decoding
Idea: Use a small "draft" model to generate candidate tokens quickly, then verify all in parallel with the large model.
Algorithm:
- Draft model generates tokens: .
- Large model scores all tokens in one forward pass (parallel, not sequential).
- Accept tokens that match the large model's distribution; reject from the first mismatch.
Expected speedup: If draft model matches large model with probability per token:
where is the per-token cost. Typical speedup: 2–3x for well-matched draft models.
Continuous Batching
Problem: Static batching wastes compute — shorter sequences finish early but GPUs wait for the longest sequence.
Solution: Insert new requests as soon as others complete.
Iteration-level scheduling: At each decoding step, decide independently for each sequence whether to continue or swap in a new request. Maximizes GPU utilization.
PagedAttention (vLLM): Store KV-cache in non-contiguous memory pages, like virtual memory. Avoids memory fragmentation; enables sharing cache between requests with common prefixes.
Quantization for Inference
Reduce weight precision from FP16 to INT8/INT4:
where (scale) and (zero-point) are calibration parameters.
INT8 (W8A8): 2x memory reduction, ~1% accuracy loss. Works for most models.
INT4 (W4A16): 4x memory reduction; weights in INT4, activations in FP16. GPTQ, AWQ achieve near-lossless quality.
Impact on inference: For memory-bound generation, 4x weight compression → ~4x speedup (limited by bandwidth).
Common Pitfalls
Pitfall 1. Optimizing compute when memory-bound. For single-request inference, reducing FLOPs (e.g., pruning 20% of parameters) barely helps if you're still loading the same weights.
Pitfall 2. Applying FP16 without loss scaling. Small gradients underflow silently, causing divergence after thousands of steps.
Pitfall 3. Using static batching for serving. Wastes 30–50% of GPU compute compared to continuous batching.
Summary
- Autoregressive inference is memory-bandwidth-bound; training is compute-bound.
- Mixed precision halves memory; BF16 avoids loss scaling complexity.
- FlashAttention fuses attention into a single IO-efficient kernel.
- Gradient checkpointing trades memory for 33% more compute.
- Speculative decoding achieves 2–3x inference speedup with a draft model.
- Quantization (INT4/INT8) provides near-linear speedup for memory-bound workloads.
Exercises
Exercise 1. Compute the arithmetic intensity of a matrix multiply by in FP16.
Exercise 2. For a model with layers and activation memory per layer, compute the memory with gradient checkpointing every 4 layers.
Exercise 3. Derive the expected number of accepted tokens in speculative decoding with acceptance rate and draft length .
Exercise 4. For an A100 GPU (312 TFLOPS FP16, 2 TB/s bandwidth), determine whether a batch-1 FFN layer with is compute-bound or memory-bound.
Exercise 5. Compute the memory savings from INT4 quantization for a 70B parameter model (original in FP16).