Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

[QUESTION] A few questions on expert data parallel

Open dhineshkumar-r opened this issue 4 months ago • 5 comments

Your question

Hey Folks, Please help me understand the observation. I'm fine-tuning(SFT) Qwen3.5-122B with 32GPUs(H200). As the length of longest sequence in training data is ~42K, I set expert-model-parallel-size=16(EMP) and CP=16. PP=1, TP=1 and DP=2. Since 16 GPUs form a DP group, I set EMP=16 as I was under the impression that it can't exceed DP ranks. However, the job runs without issues even if I set EMP=32.

  • How exactly training works when expert data parallel and data parallel are not equal, specifically data flow? For ex. DP=16 and EDP=1 in a 32 GPU setup with TP=2 and EMP=32; each DP group process a part of global batch size, how does experts process the data when EDP=1?

  • Although possible, is it recommended to have a state where EDP != DP?

  • While PP and TP operates within DP group, why EMP is allowed to exceed DP ranks?

  • When distributed optimizer flag is set, how would the optimizer state of experts distributed when they sharded across GPUs more than DP ranks?

Please let me know if I'm missing something or correct my understanding.

Tag @NVIDIA/mcore-oncall to get oncall's attention to this issue.

dhineshkumar-r avatar Jul 22 '26 06:07 dhineshkumar-r

@NVIDIA/mcore-oncall

dhineshkumar-r avatar Jul 22 '26 13:07 dhineshkumar-r

Thanks for the detailed questions. This behavior is expected. The key point is that expert parallelism is not nested inside the dense data-parallel group. MCore builds separate rank factorizations for dense and expert parameters.

Let:

  • (W) = world size
  • (TP), (PP), (CP) = dense tensor, pipeline, and context parallel sizes
  • (EP) = --expert-model-parallel-size
  • (ETP) = --expert-tensor-parallel-size

Then:

DP  = W / (TP  * PP * CP)
EDP = W / (ETP * PP * EP)

ETP defaults to TP when it is not specified. These are the two rank generators constructed in parallel_state.py.

For your original configuration, (W=32), (TP=1), (PP=1), and (CP=16):

DP = 32 / (1 * 1 * 16) = 2

So the group of 16 ranks is the CP group, not the DP group. Because CP partitions activations but replicates weights, dense gradient synchronization uses the combined (DP imes CP) domain.

For the expert layers:

  • (EP=16) gives (EDP=32/(1116)=2).
  • (EP=32) gives (EDP=32/(1132)=1).

The data/token flow is:

  1. The data loader partitions the batch across the two dense DP replicas. Within each replica, CP=16 partitions each sequence across 16 ranks.
  2. At an MoE layer, the router selects experts and the token dispatcher performs an all-to-all over the EP group; after local expert computation, the inverse all-to-all returns outputs to the originating ranks. The A2A dispatch/combine sequence is documented directly in token_dispatcher.py.
  3. With EP=16/EDP=2, there are two copies of each expert partition, and corresponding expert gradients are synchronized across the two EDP ranks.
  4. With EP=32/EDP=1, the expert set is distributed once across all 32 ranks. Tokens from both dense DP replicas are routed to the owning expert ranks and then returned. No expert-DP gradient collective is needed because there is only one replica of each expert partition.

In other words, EDP=1 does not mean one rank processes every expert. It means there is one replica of each EP/ETP expert partition.

For the other example, (W=32), (TP=2), (PP=1), (CP=1) gives dense (DP=16). EP=32/EDP=1 is possible only if (ETP=1), for example by explicitly setting --expert-tensor-parallel-size 1. If ETP is omitted, it defaults to TP=2, so (ETP imes EP imes PP = 64) and that configuration is invalid. With ETP=TP=2, EP=16 would give EDP=1. Also keep sequence parallelism enabled when combining TP and EP.

It is therefore valid, and often expected with CP or ETP differing from TP, for EDP != DP. It is not inherently better or worse:

  • Increasing EP reduces the number of local experts and expert-parameter memory per rank, but enlarges the EP all-to-all domain and can reduce communication/local-GEMM efficiency.
  • Increasing EDP replicates experts more times and permits optimizer-state sharding across more replicas, but uses a smaller EP communication domain.

The best choice depends on expert size/count, tokens per expert, and interconnect topology, so I would benchmark EP=16/EDP=2 against EP=32/EDP=1 for this setup.

With --use-distributed-optimizer, dense and expert parameters are handled in separate buffers. Dense parameters use the DP×CP group, while expert parameters use the EDP group; see distributed_data_parallel.py and the group selection at lines 206-216. Therefore:

  • EDP=2: each expert partition's optimizer state is sharded across its two replicas.
  • EDP=1: expert optimizer state is not further sharded; each rank keeps the state for its local experts (or local ETP shards).
  • EP ranks are not optimizer-shard peers, since they own different experts.

The hard constraints are that (W) must be divisible by (ETP imes EP imes PP), and the number of experts must be divisible by EP.

guihong-nv avatar Jul 22 '26 19:07 guihong-nv

Appreciate the detailed response @guihong-nv and pointing out the nuance between CP group and DP group for the case of CP=16 and DP=2 and EP=16 in 32 GPU setting.

A couple of follow up questions

  1. I notice documentations mention data-parallel ranks. What does that actually mean in this context? Isn't it same as CP ranks?
  2. Could you please share specific metrics I should be looking at if I decide to benchmark different settings? If you could share some reference PR, that'd be helpful as well.

dhineshkumar-r avatar Jul 22 '26 23:07 dhineshkumar-r

Great follow-up questions.

1. DP ranks and CP ranks are different coordinates

A global rank is a process/GPU. The same global rank simultaneously has a local rank in several process groups: TP, PP, CP, DP, EP, and so on. Therefore, “DP rank” and “CP rank” describe different coordinates of the same process; they are not interchangeable.

For the original case, assuming the default rank order, W=32, TP=1, PP=1, CP=16, DP=2:

CP groups:
  [0, 1, ..., 15]
  [16, 17, ..., 31]

DP groups:
  [0, 16], [1, 17], ..., [15, 31]

DP×CP group:
  [0, 1, ..., 31]

For example, global rank 17 is:

  • CP rank 1 in CP group [16, ..., 31]
  • DP rank 1 in DP group [1, 17]

The uncombined DP rank is the batch-replica coordinate. The data sampler uses get_data_parallel_rank() and get_data_parallel_world_size() without CP, as shown in data_samplers.py. Thus, all 16 CP ranks in the first CP group use DP rank 0 and collaborate on the same microbatch by processing different sequence partitions; the other 16 use DP rank 1 and process the other batch replica.

The reason the terminology can look ambiguous is that CP ranks hold replicated dense weights. For dense gradient synchronization and the distributed optimizer, MCore therefore also creates a combined DP×CP group. The plain DP, CP, and combined DP×CP groups are separately constructed in parallel_state.py and here. The API makes the distinction explicit through get_data_parallel_group(with_context_parallel=True/False) (source).

So when documentation says “data-parallel ranks,” check which operation it describes:

  • Data loading/global batch: the uncombined DP group, size 2 here.
  • Dense gradient/optimizer communication with CP: the DP×CP group, size 32 here.
  • Expert gradient/optimizer communication: the EDP group, size 2 for EP=16 or size 1 for EP=32.

2. Metrics for EP=16/EDP=2 versus EP=32/EDP=1

Run this as a controlled A/B test: keep the checkpoint, seed, exact sample/sequence order, global and micro batch sizes, sequence packing, recomputation, precision, optimizer settings, GPU placement, and software versions fixed. Change only EP/EDP. Exclude warm-up and measure at least 50–100 steady-state steps; if practical, repeat each run and compare medians.

I would track the following:

  1. Primary performance

    • Median and p95 iteration time.
    • Useful/non-padding tokens/s/GPU. This is especially important for variable-length 42K SFT data.
    • TFLOP/s/GPU from --log-throughput.
  2. Memory

    • Peak allocated, peak reserved, and total device memory after optimizer state initialization.
    • OOM headroom and fragmentation.
    • This captures the main EP=32/EDP=1 versus EP=16/EDP=2 memory trade-off.
  3. EP communication and compute

    • Dispatch and combine all-to-all duration and bytes.
    • More importantly, the exposed A2A time that is not hidden behind compute.
    • Grouped expert-GEMM duration/utilization.
    • Token counts per expert and per receiving EP rank: min/mean/max or p5/p50/p95. This reveals routing skew and whether GEMMs are too small.
    • EDP gradient reduce-scatter/all-reduce and parameter all-gather time. These should shrink or disappear when EDP becomes 1.
  4. Correctness/stability

    • Same loss trend, grad norm, skipped/NaN iterations, router auxiliary/z loss, and any dropped/overflowed tokens.
    • --moe-per-layer-logging is useful for the available per-layer router metrics.

A useful baseline logging setup is:

--log-interval 1
--log-throughput
--timing-log-level 1
--timing-log-option minmax
--log-memory-interval 10
--log-device-memory-used

Use the timing run diagnostically because higher timing levels can add overhead. For the A2A/GEMM breakdown, use Nsight Systems; the built-in iteration timers are too coarse. For example:

nsys profile -s none -t nvtx,cuda -o <output> --force-overwrite true \
  --capture-range=cudaProfilerApi --capture-range-end=stop \
  <launcher> ... \
  --profile --profile-step-start 20 --profile-step-end 25 \
  --profile-ranks <selected-global-ranks> --nvtx-ranges

Profile at least one rank per node, and add a suspected slow rank if token routing is imbalanced. In the timeline, compare NCCL A2A dispatch/combine, expert grouped GEMMs, idle gaps, and whether communication crosses the node boundary. For this topology, EP=32 may reduce expert replication and consolidate more tokens per expert copy, but it also expands the A2A domain; the profile will show which effect dominates.

The current MoE performance guide recommends the same memory-first, then throughput/profile-driven workflow.

For concrete references:

  • Merged PR #2280 adds --fake-process-group, memory snapshots, Nsight profiling, and includes a Qwen3 example with TP=2, EP=32, ETP=1. It is useful for checking whether a mapping fits and for local kernel/memory profiling. However, fake process groups skip NCCL, so use real 32-GPU runs to choose between EP=16 and EP=32.
  • Draft PR #2730 is a useful example of comparing MoE A2A overlap using Nsight timelines and end-to-end performance. I would treat it as a profiling reference, not as a recommendation to consume the unmerged implementation.

guihong-nv avatar Jul 23 '26 16:07 guihong-nv

Closing as this issue seems to be resolved. Please re-open if necessary.

Phlip79 avatar Nov 10 '26 14:11 Phlip79