Build a Data Parallel vs Model Parallel Training Pipeline
distributed-systems performance cloud-infrastructure
AI System Design Deep Dive
Data vs Model Parallel Pipeline
Memory says cut the model. The wire says do not. Pick the cut that wastes the fewest seconds.
Picture a car factory with 512 workers. You can build 16 identical factories, each assembling different cars and comparing tool calibrations at the end of every shift. That is data parallelism. You can cut each factory into an assembly line of 4 stations, so no station has to stock every tool. That is pipeline parallelism. Or you can put 8 workers at the same workbench, handing parts across it every few seconds. That is tensor parallelism, and it only works if the 8 workers stand within arm’s reach. Every training layout is a mix of those three, and the mix decides whether 512 H100 GPUs spend their time multiplying matrices or waiting for each other.
Now the numbers. We train a 70B dense model (80 layers, hidden size 8192, 4,096-token sequences) with a global batch of 4,194,304 tokens, which is 1,024 sequences per optimizer step. Mixed precision AdamW keeps 16 bytes per parameter (2 for bf16 weights, 2 for bf16 gradients, 4 for the fp32 master copy, 8 for the two Adam moments), so the model state is 1,120 GB while one H100 holds 80 GB. One step costs 6 * 70e9 * 4,194,304 = 1.76e18 FLOPs. At 989 TFLOPS peak bf16 per GPU and 40 percent MFU across 512 GPUs, that is 8.7 seconds per step and 36 days for 1.5 trillion tokens.
The naive answers each fail in a different place. Sharding everything with FSDP across all 512 GPUs fits in memory (2.19 GB of state per GPU) but moves 419 GB per GPU per step, which is 8.4 seconds of pure network time at a full 50 GB/s EFA share and 16.8 seconds when the spine is shared. A 16-way tensor parallel group that spills across two nodes pushes 1,288 GB per step over EFA instead of 601 GB over NVLink and lands at 17 percent MFU. A deep pipeline with no tensor parallelism has a 43.8 percent bubble because 16 microbatches cannot fill 8 stages. The right layout puts each kind of traffic on the fastest wire that can carry it, and that is a planning problem before it is a coding problem.
We need to solve for memory fit per GPU, bubble and exposed communication, and placement of each group on the right wire simultaneously, then freeze the answer into an immutable plan the rest of the run can trust. The layout itself was an input to the previous post’s orchestrator. This post derives it.
Requirements and Constraints
Functional requirements
- Take a model spec, a GPU count and a global batch, enumerate every legal
tp x pp x dpfactorization, and rank them with a calibrated cost model. - Build four families of process groups (tensor, pipeline, data, and one model-parallel group per replica) with a rank map that keeps tensor groups inside a node and pipeline groups inside a rack.
- Run a 1F1B pipeline schedule with gradient accumulation across 64 microbatches per replica.
- Synchronize gradients across the 16 data parallel replicas with bucketed all-reduce that overlaps backward compute.
- Agree on a global “skip this step” decision when any rank sees a non-finite gradient.
- Emit per-step timers so the plan can be checked against reality.
Non-functional requirements
- Throughput: 8.7 s per step, 482,255 tokens/s, at least 40 percent MFU across the run.
- Bubble and comm: pipeline bubble at most 5 percent of compute time, exposed gradient all-reduce under 0.1 s per step.
- Capacity: at most 72 GB of the 80 GB HBM (90 percent) in use, counting model state, activations and a 10 GB workspace for NCCL buffers and fragmentation.
- Quality: gradients from the distributed layout match a single-device reference on a 1B proxy model with cosine similarity of at least 0.999 and relative error under 5 percent.
- Cost: under $2.10 of GPU spend per million training tokens at on-demand prices.
- Stability: every rank agrees on every skip-step decision, with zero divergence between replicas over 1,000 steps.
Constraints
- Hardware: 64
p5.48xlargenodes, 8 H100 80GB each, NVLink inside the node (about 360 GB/s usable per GPU, our assumption), 3,200 Gbps EFA per node which is 50 GB/s per GPU, four leaf racks of 16 nodes and a 2:1 oversubscribed spine (25 GB/s per GPU across racks). Price $55.04 per node-hour in us-east-1. - Software: PyTorch with NCCL,
bf16autocast, FlashAttention so attention activations are linear in sequence length, AdamW. - Out of scope: context parallelism, expert parallelism,
fp8, and resharding a checkpoint when the layout changes (the next two posts).
A layout is not a model property, it is a property of the model plus the cluster's wiring. The same 70B model needs a different cut on a cluster with 900 GB/s of NVLink than on one with PCIe.
High-Level Architecture
The system has six components. Four run before the first step, two run inside every step.
- Layout Planner enumerates factorizations of 512 GPUs, rejects the ones that do not fit in HBM or do not fit the topology, and ranks the rest by modeled step time.
- Plan Store holds the chosen plan as an immutable Postgres row plus a copy in etcd that every rank reads at startup.
- Group Builder turns the plan into a rank map and four families of
torch.distributedprocess groups. - Pipeline Scheduler decides, for each stage, the exact order of forward and backward microbatch operations (1F1B).
- Gradient Synchronizer accumulates over microbatches, runs the bucketed data parallel all-reduce, and makes the global skip decision.
- Profiler records measured step time, bubble time and communication time, and compares them with what the planner predicted.
A step flows like this. The data loader hands each of the 16 replicas 64 sequences. Replica d splits them into 64 microbatches of one sequence each. Stage 0 starts forward on microbatch 0 and ships an 8.4 MB activation shard to stage 1, and while stage 1 works, stage 0 starts microbatch 1. After a warmup of 3 microbatches the pipeline settles into one forward and one backward per stage. The last backward finishes on stage 0, the data parallel all-reduce flushes its final bucket, every rank checks that the global gradient norm is finite, and the optimizer updates the fp32 master weights.
The plan is immutable for a run. Changing tensor, pipeline or data parallel degree changes the shape of every checkpoint shard, so the planner runs once before launch and the rest of the system treats its output as a constant.
The Layout Planner
The planner’s job is to turn “512 GPUs, 70B parameters, 1,024 sequences per step” into one tp x pp x dp triple, and to refuse any triple the hardware cannot run.
Think of a travel agent pricing a trip: the cheapest ticket is useless if the connection is 40 minutes and the airports are 90 minutes apart. The planner does the same for parallelism. A layout with a beautiful FLOP count is useless if its tensor group straddles two nodes, and a layout that fits in memory is useless if its bubble eats 40 percent of the step.
The non-obvious part is that most of the search space dies on two cheap rules. Tensor parallel degree above 8 leaves the NVLink domain, so it is priced at EFA bandwidth and loses. Pipeline degree must divide 80 layers and leave enough microbatches to fill the pipe, so tp 1 pp 8 dp 64 has only 16 microbatches per replica and a 43.8 percent bubble. After pruning for memory and topology, 13 of the 25 factorizations remain, and the cost model ranks those.
# Layout planner: enumerate TP x PP x DP, reject infeasible, rank by modeled step time
from dataclasses import dataclass
P=70e9; L=80; H=8192; S=4096; GBT=4_194_304; N=512; GPUS_PER_NODE=8; RACK_GPUS=128
PEAK=989e12; NVLINK=360e9; EFA=50e9; SPINE=25e9
HBM=80e9; WORKSPACE=10e9
SEQS=GBT//S
FLOPS=6*P*GBT
TP_EXPOSED=0.5; DP_FLOOR=0.1; BACKWARD_SHARE=2/3
@dataclass
class Plan:
tp:int; pp:int; dp:int; zero:int; states:float; act:float; bubble:float
tp_s:float; dp_s:float; step_s:float; fits:bool
def evaluate(tp, pp, dp, t_compute):
pg = P/(tp*pp)
m = SEQS//dp
zero = 0
states = 16*pg
if states + WORKSPACE > HBM*0.9: # plain DP states do not fit: shard optimizer (ZeRO-1)
zero = 1; states = (4+12/dp)*pg
inflight = min(pp, m)
layers = L/pp
act = 34*S*H/tp*layers*inflight if tp > 1 else 2*S*H*layers*inflight + 34*S*H
fits = states + act + WORKSPACE <= HBM*0.9
bubble = (pp-1)/m
tp_b = 0 if tp == 1 else 8*(tp-1)/tp*S*H*2*layers*m
tp_s = tp_b/(NVLINK if tp <= GPUS_PER_NODE else EFA)
dp_b = 2*(dp-1)/dp*2*pg
dp_s = dp_b/SPINE
dp_exp = max(DP_FLOOR*dp_s, dp_s - BACKWARD_SHARE*t_compute)
step = t_compute*(1+bubble) + TP_EXPOSED*tp_s + dp_exp
return Plan(tp,pp,dp,zero,states,act,bubble,tp_s,dp_s,step,fits)
def enumerate_plans(t_compute):
out=[]
for tp in (1,2,4,8,16):
for pp in (1,2,4,5,8,10,16):
if L % pp: continue
if N % (tp*pp): continue
dp = N//(tp*pp)
if dp > SEQS or SEQS % dp: continue
if tp*pp > RACK_GPUS: continue
p=evaluate(tp,pp,dp,t_compute)
if p.fits: out.append(p)
return out
def calibrate(tp=8,pp=4,dp=16,target=8.697):
lo,hi=1.0,20.0
for _ in range(60):
mid=(lo+hi)/2
if evaluate(tp,pp,dp,mid).step_s < target: lo=mid
else: hi=mid
return (lo+hi)/2
The cost model has four terms: compute time, the pipeline bubble (p - 1) / m of that compute time, tensor parallel traffic on NVLink (half of it assumed exposed), and the data parallel all-reduce on the spine (exposed only where it exceeds the backward window it overlaps). calibrate solves for the one unknown, the compute time per step, so that the layout used in the previous post reproduces its 8.7 s step. That puts kernel efficiency at 46.5 percent before communication, which is plausible for bf16 GEMMs on H100. Every other layout is then priced with the same constants, so ratios between layouts are more trustworthy than absolute seconds.
Running it on this cluster ranks 13 feasible plans. These are the rows that matter for the story, sorted by step time:
tp 4 pp 2 dp 64 z1 fits=True states= 36.6 act= 22.8 bub= 6.2% step= 8.44 mfu=41.2% days= 34.9 cost=$2.95M
tp 8 pp 1 dp 64 z1 fits=True states= 36.6 act= 11.4 bub= 0.0% step= 8.45 mfu=41.2% days= 35.0 cost=$2.96M
tp 4 pp 4 dp 32 z1 fits=True states= 19.1 act= 22.8 bub= 9.4% step= 8.61 mfu=40.4% days= 35.6 cost=$3.01M
tp 8 pp 2 dp 32 z1 fits=True states= 19.1 act= 11.4 bub= 3.1% step= 8.62 mfu=40.4% days= 35.7 cost=$3.01M
tp 4 pp 8 dp 16 z0 fits=True states= 35.0 act= 22.8 bub=10.9% step= 8.69 mfu=40.0% days= 36.0 cost=$3.04M
tp 8 pp 4 dp 16 z0 fits=True states= 35.0 act= 11.4 bub= 4.7% step= 8.70 mfu=40.0% days= 36.0 cost=$3.04M
tp 8 pp 8 dp 8 z0 fits=True states= 17.5 act= 11.4 bub= 5.5% step= 8.74 mfu=39.8% days= 36.2 cost=$3.06M
tp 1 pp 8 dp 64 z1 fits=True states= 36.6 act= 6.5 bub=43.8% step= 10.89 mfu=32.0% days= 45.1 cost=$3.81M
tp16 pp 1 dp 32 z1 fits=True states= 19.1 act= 5.7 bub= 0.0% step= 20.43 mfu=17.0% days= 84.6 cost=$7.15MPremium Content
Unlock the full article along with everything else in the archive — all in one place.