Skip to content

perf(bridge): export MoE experts with one gather instead of per-shard broadcast - #271

Merged
hjh0119 merged 2 commits into
modelscope:mainfrom
hjh0119:export-perf
Nov 27, 2026
Merged

hjh0119 merged 2 commits into
modelscope:mainfrom
hjh0119:export-perf

Conversation

@hjh0119

@hjh0119 hjh0119 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Problem

_set_moe_state exported MoE experts by looping over EP ranks: one
all_reduce(src) + broadcast(meta) + broadcast(data) per rank, then a per-expert
clone() on the receiving side, plus concat(old, new).clone() accumulation for the
stacked HF layout. GRPO runs this path once per step, so both the collective count and
the copied bytes grow with EP (the concat path with EP²).

Change

  • _reduce_lora_flag: outside PEFT export every rank already answers False, so the
    is_lora vote is skipped instead of being run per shard.
  • _store_ep_shard: the grouped layout preallocates ep_size slots on first use and
    writes each shard into its own offset, replacing the accumulate-then-concat copy.
  • _expert_export_mode + _export_experts_ungrouped / _export_experts_grouped:
    one hop per expert tensor — local concat -> (etp>1) ETP all-gather -> convert only
    this rank's shard (transpose, gpt_oss gate/up interleave) -> one
    all_gather_into_tensor -> per-expert views (ungrouped) or the final tensor
    (grouped). No per-expert clone, no second full-size buffer.
  • Supports pp>1 (the owning stage gathers, then broadcasts the tensor to the other
    stages) and etp>1 (ETP gather before the EP gather).

Mode names mirror the existing classifier: _get_hf_experts_attr() returns
hf_grouped, so the fast paths are 'grouped' / 'ungrouped'.

Results

4x GPU, in-process A/B (the legacy path is forced by patching the gate, so both runs
use the same weights and topology), median of 3–7 reps.

Workload Before After
Qwen3.5-35B-A3B, EP=4 (64 GB experts) 1.425 s, 960 collectives, 62.4 GiB cloned 0.291 s, 80 collectives, 1.0 GiB cloned
gpt-oss-20b, EP=4 (38 GB experts) 0.456 s, 1152 collectives 0.127 s, 96 collectives
synthetic pp=2, ep=2 28.0 ms 18.4 ms
synthetic etp=2, ep=1 7.42 ms 6.75 ms

Peak memory does not grow: Qwen3.5 unchanged at 63.5 GiB, gpt-oss 38.4 -> 37.3 GiB.
With ep=1 (pure pp or pure etp) the gain is ~10% only — there is no cross-rank expert
traffic in that case; the value there is not falling back to the per-shard path.

Correctness

  • In-process A/B: the legacy path is re-run by forcing _expert_export_mode back to
    None; every key is compared with torch.equal (Qwen3.5 115/115, gpt-oss 18/18).
  • tests/test_moe_expert_export.py covers: slot write == torch.concat, ungrouped fast
    path == legacy, grouped fast path == legacy, CPU save_weights == export, packed
    formats (fp4) stay on the legacy path, and a topology matrix
    pp2 / ep2 / etp2 / pp2ep2 / etp2ep2. The matrix is env-driven because each topology
    needs its own world size; the exact commands are in the test module docstring.

Fallbacks (behaviour unchanged)

FP8 / NVFP4 (fp4_param) packed params, PEFT deltas, target_device='cpu' (including
offload_bridge=True), MTP layers, bias + etp>1 (Megatron rejects that combination at
config time) and any unrecognised expert layout keep the previous implementation.

@hjh0119
hjh0119 merged commit 215739e into modelscope:main Nov 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants