A Full-Stack Guide to LLM Engineering: Training, Inference, and Capacity Planning
Competition in generative AI has shifted from model algorithms to engineering capability. This guide walks through four stages of the LLM engineering lifecycle: distributed training architecture, inference and KV Cache estimation, production metrics and stress testing, and the data feedback loop with capacity planning. Each section provides selection tables, formulas, or checklists that can be applied directly to your own projects.
Competition in generative AI has shifted from model algorithms to engineering capability. As models grow larger, training and inference place ever higher demands on compute, GPU memory, networking, and operations. This article covers four stages: distributed architecture for training, inference and KV Cache estimation, production metrics and stress testing, and the data feedback loop with capacity planning. Each section provides selection tables, formulas, or checklists that readers can apply directly to their own projects. The numbers shown are estimation examples; actual values depend on the hardware and framework versions you use.
1. Distributed Architecture for LLM Training
A single accelerator card has limited memory. Once model parameters grow from tens of billions to hundreds of billions, the weights alone can exceed the capacity of a single card. Add optimizer states and gradients, and memory requirements multiply. Large-scale training therefore depends on distributed parallelism.
1.1 Choosing a Parallelism Strategy
There are three common types of parallelism:
- Data Parallelism (DP): Each card holds a complete copy of the model and processes a different batch of data; gradients are synchronized after backpropagation. It is simple to implement, but every card stores the full model, which creates heavy memory pressure. Combined with ZeRO or FSDP, parameters, gradients, and optimizer states can be sharded across cards, significantly reducing memory pressure at the cost of additional parameter-gathering communication.
- Tensor Parallelism (TP): Matrix multiplications within a layer are split across multiple cards. It reduces per-card memory usage, but every layer requires communication, so it is typically used only among cards on the same machine connected via high-speed interconnects such as NVLink.
- Pipeline Parallelism (PP): Different layers of the model are assigned to different devices, with micro-batches keeping each stage busy concurrently. Cross-node communication is relatively low, but it introduces pipeline bubbles, increasing device idle time.
In practice, these are combined. Tensor parallelism is used within a machine to confine the most communication-intensive operations to high-speed interconnects; pipeline parallelism or data parallelism is used across machines to reduce cross-node traffic. When both model size and cluster size are large, a common approach is to combine TP, PP, and DP simultaneously—known as 3D parallelism.
Comparison table:
| Approach | Suitable Scale | Memory Benefit | Communication Overhead | Typical Combination |
|---|---|---|---|---|
| DP | Model fits on a single card; large data volume | None | Gradient sync every step | Single-node multi-GPU training of small/mid models |
| DP + ZeRO/FSDP | Model parameters slightly exceed a single card | Medium to high, depending on sharding level | Additional parameter-gathering communication | Default starting point for mid-size models |
| TP | Single-layer matrices too large; multi-GPU within one node | Medium, shards weights and activations | Communication every layer; bandwidth-sensitive | Multi-GPU within a node over high-speed interconnect |
| PP | Many layers; requires cross-node training | Medium, each stage stores only some layers | Low point-to-point traffic, but pipeline bubbles | Multi-node training with enough micro-batches |
| TP + PP + DP | Hundreds of billions of parameters and above; large clusters | High | Requires comprehensive tuning | TP intra-node, PP inter-node, DP outermost |
When DP is combined with ZeRO-3, parameters must be gathered during both the forward and backward passes, significantly increasing communication volume. If network bandwidth is insufficient, training speed suffers. In that case, prioritize increasing the share of TP or reducing the scale of DP.
1.2 Estimating Training Memory
With mixed-precision training using the Adam optimizer, each parameter occupies roughly 16 bytes: 2 bytes for the FP16 weight, 2 bytes for the FP16 gradient, 4 bytes for the FP32 master weight, 4 bytes for the first moment, and 4 bytes for the second moment. This figure excludes activations. For a 70B-parameter model:
70 × 10^9 × 16 bytes ≈ 1.12 TB
With ZeRO-3 sharding across 64 cards, model states take about 17.5 GB per card, and you still need headroom for activations and communication buffers. This number is only a starting point; actual usage should be measured with a short trial run.
Pre-training checklist:
- Calculate the total footprint of model states, activations, and communication buffers, and reserve a 10%–20% margin.
- Record peak memory with a short trial run instead of relying solely on theoretical values.
- For long-sequence training, enable gradient checkpointing (activation checkpointing) to trade extra computation for lower activation memory.
2. Inference and KV Cache Memory Estimation
During inference, model weights are a fixed cost; the component most likely to overflow memory is the KV Cache. Every time the model generates a new token, it must read the Keys and Values of all previous tokens. To avoid recomputation, the framework caches them in GPU memory. As a result, the longer the context and the higher the concurrency, the larger the KV Cache footprint.
The KV Cache size per token can be estimated with the following formula:
Bytes per token = 2 × number of layers × number of KV heads × head dimension × bytes per element
The factor of 2 in the formula accounts for the two caches, Key and Value.
Example: a 70B-class model with 80 layers, grouped-query attention (GQA, 8 KV heads), a head dimension of 128, and FP16 storage
