With Megatron-Core 0.19, how would you set TP, PP, CP and DP for a 70B dense decoder on 32 nodes of eight 80 GB GPUs?
answer
- what is left becomes data parallel
- the chattiest group stays in the node
- world size divisible by TP x PP x CP
- virtual stages per pipeline rank
- --use-distributed-optimizer
basics
~20 sTensor parallel 8 with sequence parallelism inside each node, pipeline parallel 4 across nodes with virtual stages, context parallel 1 at 8K tokens, leaving data parallel 256 / (8 x 4 x 1) = 8, plus the distributed optimizer.
solid answer
~40 sMegatron-Core 0.19 builds a dense grid of tensor, pipeline and context groups and makes data parallel whatever is left: `world_size // (TP × PP × CP)`, with a `RuntimeError` when that does not divide. TP talks inside every layer, so `--tensor-model-parallel-size 8` with `--sequence-parallel` fills one NVLink node; the default rank order puts TP innermost, so those eight ranks are one node. For an 80-layer model, `--pipeline-model-parallel-size 4` gives 20 layers per stage across nodes, and `--num-layers-per-virtual-pipeline-stage 5` interleaves four chunks per rank to shrink the bubble. At 8K tokens context parallel stays 1, so DP = 256 / 32 = 8. `--use-distributed-optimizer` shards the fp32 state over those 8 ranks: about 7.5 bytes per parameter, roughly 16 GB of model state per GPU, leaving room for activations.
code
bash · 18 lines# 32 nodes x 8 GPUs = 256 ranks; TP 8 x PP 4 x CP 1 = 32, so DP = 8
MEGATRON_LAYOUT=(
--tensor-model-parallel-size 8
--sequence-parallel
--pipeline-model-parallel-size 4
--num-layers-per-virtual-pipeline-stage 5
--context-parallel-size 1
--use-distributed-optimizer
--overlap-grad-reduce
--overlap-param-gather
--num-layers 80
--hidden-size 8192
--num-attention-heads 64
--seq-length 8192
--micro-batch-size 1
--global-batch-size 1024
--bf16
)go deeper
Recall that Megatron-Core multiplies TP, PP and CP and gives whatever GPUs remain to data parallelism, and that TP belongs inside one node.
Explain each flag's effect: tensor parallel with sequence parallel, pipeline stages and virtual chunks, context parallel, and how the distributed optimizer divides fp32 state by the data-parallel size.
Lay out a stated model on a stated cluster with the arithmetic shown — group product, data-parallel size, bytes per GPU, micro-batches per pipeline — and defend which groups cross nodes.
Decide how much model parallelism to buy back as data parallelism, setting a trial run of the smaller layout against the safety margin of the deeper one before committing a long pretraining run.
## The rule Megatron-Core applies In Megatron-Core 0.19, `initialize_model_parallel` builds a **dense grid** of tensor-parallel (TP), pipeline-parallel (PP) and context-parallel (CP) groups and makes **data parallel** (DP) whatever is left: `data_parallel_size = world_size // (TP × PP × CP)`. When the world size is not divisible it raises `RuntimeError`. The command-line flags are generated from config dataclass fields, so `tensor_model_parallel_size` is `--tensor-model-parallel-size`, `context_parallel_size` is `--context-parallel-size`, and so on. The default rank order, `tp-cp-ep-dp-pp`, makes TP the fastest-varying dimension: with eight ranks per node, a TP group of 8 is exactly one node, and pipeline stages are the slowest-varying, spread across nodes. ## A layout for 70B on 32 × 8 GPUs Assume 80 layers, hidden size 8192, 64 attention heads and 8,192-token sequences. 1. **TP = 8 with `--sequence-parallel`.** Tensor parallelism communicates inside every layer, forward and backward, so it belongs on NVLink — one node. Sequence parallelism splits LayerNorm and dropout activations over the same group; Megatron switches it off when TP is 1. 2. **PP = 4 with virtual stages.** 80 / 4 = 20 layers per stage. `--num-layers-per-virtual-pipeline-stage 5` gives each pipeline rank 20 / 5 = 4 interleaved chunks, which shrinks the pipeline bubble at the price of more point-to-point sends. Stage-to-stage traffic is boundary activations only, so it tolerates the inter-node network. 3. **CP = 1** at 8K tokens; TP with sequence parallelism already divides activations by 8. CP becomes the lever if a long-context phase raises the sequence length. 4. **DP = 256 / (8 × 4 × 1) = 8.** 5. **`--use-distributed-optimizer`.** For bf16 parameters with fp32 gradients, Megatron's distributed-optimizer documentation gives 6 + 12/d bytes per parameter (d = data-parallel size) against 18 without it. Each GPU holds 70B / (8 × 4) ≈ 2.19B parameters, so model state is about 2.19B × 7.5 ≈ **16.4 GB** instead of about 39.4 GB. ## Checking the alternatives | Layout | TP × PP × CP | DP | Model state per GPU | Verdict | |---|---|---|---|---| | TP 8, PP 4, CP 1 | 32 | 8 | ≈ 16.4 GB | fits with wide activation headroom | | TP 8, PP 2, CP 1 | 16 | 16 | ≈ 4.375B × 6.75 ≈ 29.5 GB | fewer stages, more DP; viable if activations fit | | TP 16, PP 2, CP 1 | 32 | 8 | ≈ 16.4 GB | TP crosses the node boundary — avoid | | TP 8, PP 3, CP 1 | 24 | — | — | 256 is not divisible by 24: `RuntimeError` | Megatron's own guidance is to keep model parallelism as small as fits and give the rest to data parallelism, so the PP 2 row is worth a short trial run: if its activations fit beside 29.5 GB, it halves the pipeline depth and doubles DP. The TP 16 row has the same arithmetic as the first but puts per-layer TP traffic on the inter-node network. ## Micro-batches and the global batch `--global-batch-size` ÷ (`--micro-batch-size` × DP) gives the micro-batches each pipeline runs per step: 1024 / (1 × 8) = 128, far more than 4 stages. If `--global-batch-size` is omitted, Megatron sets it to micro-batch × DP — 8 here — which leaves a 4-stage pipeline almost empty. Why micro-batches hide the bubble is the pipeline foundation's subject; the engine-level check is that the flags give enough of them. ## Overlap flags worth adding - `--overlap-grad-reduce` overlaps the data-parallel gradient reduction with backward. - `--overlap-param-gather` overlaps the distributed optimizer's parameter all-gather with forward; Megatron accepts it only with the distributed optimizer or one of its other sharded modes, and only together with `--overlap-grad-reduce`. ## Mistakes interviewers listen for - Stretching TP across nodes to avoid pipeline parallelism. - Group sizes whose product does not divide the world size. - Counting expert parallelism in the dense product: Megatron's parallelism guide writes the total as TP × PP × CP × EP × DP, but `parallel_state.py` builds experts on a separate grid, and dense DP is the world size over TP × PP × CP. - Leaving out the distributed optimizer and paying 18 bytes per parameter on every rank. - Omitting `--global-batch-size` with PP > 1.
- The same 70B run moves to a 32K-token phase. How would the Megatron-Core layout change?Add context parallelism: `--context-parallel-size 4` keeps TP 8 and PP 4, so DP becomes 256 / (8 × 4 × 4) = 2, and each CP rank holds 8K of the 32K tokens. The sequence length must be a multiple of 2 × CP, and 32,768 is. With fewer DP replicas the global batch needs more micro-batches per pipeline, or a smaller global batch.
- Why does a TP group of 8 land inside one node without any placement flag?Megatron-Core's default order `tp-cp-ep-dp-pp` makes TP the fastest-varying rank dimension, so ranks 0-7 form the first TP group, 8-15 the next, and so on. When the launcher puts eight consecutive ranks on each node, every TP group is one node, data-parallel peers sit eight ranks apart and pipeline stages span nodes.
- Data parallel size is a flag you set alongside TP, PP and CP
- TP 16 across two nodes is as good as TP 8 with PP 2
- Megatron rounds the data-parallel size down when the world size does not divide
- Sequence parallelism is a separate group that also multiplies the world size
- The distributed optimizer only matters for checkpoint speed, not memory
Assumes Megatron-Core 0.19.2 (core_v0.19.2): the DP-is-what-is-left rule, the tp-cp-ep-dp-pp default order and the generated flag names are checked against that release.