Repository navigation
perf(bridge): export MoE experts with one gather instead of per-shard broadcast - #271
Merged
Merged
Conversation
tastelikefeet
approved these changes
Nov 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
_set_moe_stateexported MoE experts by looping over EP ranks: oneall_reduce(src) + broadcast(meta) + broadcast(data)per rank, then a per-expertclone()on the receiving side, plusconcat(old, new).clone()accumulation for thestacked 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 answersFalse, so theis_loravote is skipped instead of being run per shard._store_ep_shard: the grouped layout preallocatesep_sizeslots on first use andwrites 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.
pp>1(the owning stage gathers, then broadcasts the tensor to the otherstages) and
etp>1(ETP gather before the EP gather).Mode names mirror the existing classifier:
_get_hf_experts_attr()returnshf_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.
pp=2, ep=2etp=2, ep=1Peak 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 experttraffic in that case; the value there is not falling back to the per-shard path.
Correctness
_expert_export_modeback toNone; every key is compared withtorch.equal(Qwen3.5 115/115, gpt-oss 18/18).tests/test_moe_expert_export.pycovers: slot write ==torch.concat, ungrouped fastpath == legacy, grouped fast path == legacy, CPU
save_weights== export, packedformats (fp4) stay on the legacy path, and a topology matrix
pp2 / ep2 / etp2 / pp2ep2 / etp2ep2. The matrix is env-driven because each topologyneeds 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'(includingoffload_bridge=True), MTP layers,bias + etp>1(Megatron rejects that combination atconfig time) and any unrecognised expert layout keep the previous implementation.