Skip to content

feat(optimizer): overlap Muon shard communication and compute - #1689

Open
townwish4git wants to merge 1 commit into
mindspore-ai:masterfrom
townwish4git:feat/muon-shard-comm-overlap
Open

townwish4git wants to merge 1 commit into
mindspore-ai:masterfrom
townwish4git:feat/muon-shard-comm-overlap

Conversation

@townwish4git

@townwish4git townwish4git commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Paired: GitHub #1689 ↔ GitCode !2054

What type of PR is this?

/kind feature

What does this PR do / why do we need it

用户接口

新增 Muon 级别的布尔开关 overlap_shard_comm,默认 False。直接构造 Muon 或从混合优化器工厂都可以启用。所有 rank 需设置相同值。

optimizer = Muon(params, overlap_shard_comm=True)

optimizer = get_hyper_optimizer(
    model=model,
    muon_params=muon_groups,
    adamw_params=adamw_groups,
    muon_kwargs={"overlap_shard_comm": True},
)

该开关适用于 CUDA 或 Ascend NPU 上大于一个 rank 的单分片轴,也支持符合条件的 HSDP 分片组。配置了自定义动量或更新后回调时使用串行路径。矩阵 reshape 和 NS 输入变换可用于重叠路径,跨参数的回调调用顺序可能变化。

开启后的调度

两批分片工作的串行与重叠调度。NS A 与 Gather B 重叠,NS B 与 Broadcast A 重叠,本地 NS 填补首轮收集和广播窗口。

  • 相邻分片批次:在计算流执行 A 的 Newton–Schulz 时,通信流可准备 B 的 AllGather;B 就绪后,B 的计算可与 A 的广播重叠。写回流等待广播完成,再更新本卡分片。HSDP 副本同步等待对应写回完成。
  • 本地批次:无需分片通信或副本同步的工作按参数组和设备排队,均匀分配到首轮 AllGather 和广播窗口。没有本地批次时,相邻分片批次仍可重叠。只有一个分片批次时,本地计算可通过异步通信回调插入。
  • 临时缓冲区:流水线复用 AllGather、计算归属 rank 和广播的缓冲区;打包使用批量复制并只清零填充部分。

Which issue(s) this PR fixes

Fixes #1688

Test Plan and Test result

A2 使用 Ascend 910B3 和 torch/torch_npu 2.9.0,A5 使用 Ascend 950DT 和 torch/torch_npu 2.14.0。基准采用官方 Qwen3.5 模型结构、默认 Muon 参数分组与固定非零 BF16 梯度,测量完整 Muon.step()。每组预热 2 次、测量 7 次。计时从 Host 调用到设备同步完成,每步取最慢 rank,再取中位数。

以下各表均按 关 → 开 展示。峰值显存取各 rank 中最大的 torch.npu.max_memory_allocated(),统计窗口覆盖优化器构造、预热和计时,包含参数、梯度、动量和临时张量。CANN profiler 来自单独采集的一步,各列取 rank 中位数。

FSDP 完整 Muon step

使用八卡单轴 Shard(0)。Qwen3.5 2B–35B-A3B 的同步 Muon step 耗时在 A2 下降 9.0%–36.0%。 0.8B–35B-A3B 的单卡峰值显存最多增加 0.77 GiB。

机器与布局 Qwen3.5 规格 同步 Muon step(ms) 耗时变化 峰值显存(GiB/卡)
A2 8 卡 0.8B 73.5 → 78.0 +6.0% 0.830 → 0.865
A2 8 卡 2B 160.8 → 112.7 −29.9% 1.821 → 1.825
A2 8 卡 4B 356.5 → 228.1 −36.0% 3.712 → 3.954
A2 8 卡 9B 889.1 → 595.5 −33.0% 7.051 → 7.379
A2 8 卡 27B 3250.4 → 2254.2 −30.6% 20.484 → 21.249
A2 8 卡 35B-A3B 905.6 → 824.1 −9.0% 24.569 → 25.071
A5 8 卡¹ 122B-A10B 2553.7 → 2418.8 −5.3% 86.556 → 88.337
FSDP Host 调用与 Device event(关 → 开,ms)
机器与布局 Qwen3.5 规格 Host 调用 Device event
A2 8 卡 0.8B 73.2 → 77.8 73.3 → 77.9
A2 8 卡 2B 102.7 → 112.4 160.7 → 112.5
A2 8 卡 4B 135.6 → 163.0 356.4 → 228.0
A2 8 卡 9B 139.6 → 163.0 889.0 → 595.4
A2 8 卡 27B 457.1 → 289.2 3250.1 → 2253.9
A2 8 卡 35B-A3B 236.2 → 245.9 905.4 → 824.0
A5 8 卡¹ 122B-A10B 2049.5 → 1509.9 2553.4 → 2418.6
FSDP CANN profiler(关 → 开,ms)
机器与布局 Qwen3.5 规格 计算 总通信 未掩盖通信
A2 8 卡 0.8B 18.4 → 18.5 71.7 → 151.0 71.7 → 132.8
A2 8 卡 2B 59.2 → 60.6 130.4 → 172.5 130.4 → 114.1
A2 8 卡 4B 147.4 → 155.9 243.5 → 243.8 243.5 → 101.3
A2 8 卡 9B 349.2 → 361.0 548.9 → 468.8 548.9 → 254.1
A2 8 卡 27B 1824.8 → 1944.0 1440.1 → 1870.1 1440.1 → 336.3
A2 8 卡 35B-A3B 791.0 → 810.5 124.7 → 629.1 124.7 → 8.8
A5 8 卡¹ 122B-A10B 2416.3 → 2398.1 163.0 → 2098.9 163.0 → 56.0
  • 122B-A10B。 A5 八卡默认分配器关闭 overlap 时,端到端耗时为 2555.1 ms,峰值显存为 86.602 GiB/卡。开启后在本地 Newton–Schulz 临时缓冲区分配处 OOM。¹ 表中的两组均使用 PYTORCH_NPU_ALLOC_CONF=expandable_segments:True,峰值显存增加 1.781 GiB/卡。
  • 容量限制。 A2 的 122B-A10B 和两种机器上的 397B-A17B 均超过八卡布局下 BF16 权重、梯度和动量的最低显存需求。

HSDP 完整 Muon step

使用单机八卡,网格维度为 replicate × shard,DTensor placement 为 (Replicate(), Shard(0)),hsdp_replica_count 等于副本维度大小。2×4 下,2B–27B 的同步 Muon step 耗时在 A2 下降 23.1%–29.6%,在 A5 下降 24.9%–38.3%。 2×4 的单卡峰值显存最多增加 0.44 GiB,A2 4×2 最多增加 1.28 GiB。

机器与布局 Qwen3.5 规格 同步 Muon step(ms) 耗时变化 峰值显存(GiB/卡)
A2 8 卡 2×4 0.8B 65.8 → 62.5 −4.9% 1.484 → 1.679
A2 8 卡 2×4 2B 184.6 → 141.9 −23.1% 3.067 → 3.231
A2 8 卡 2×4 4B 460.4 → 324.1 −29.6% 5.968 → 6.219
A2 8 卡 2×4 9B 935.8 → 707.3 −24.4% 10.123 → 10.391
A2 8 卡 2×4 27B 3801.4 → 2868.9 −24.5% 31.863 → 32.305
A2 8 卡 2×4 35B-A3B 1624.8 → 1579.7 −2.8% 41.187 → 41.185
A5 8 卡 2×4 0.8B 32.3 → 28.8 −10.9% 1.473 → 1.667
A5 8 卡 2×4 2B 83.8 → 59.2 −29.4% 3.069 → 3.223
A5 8 卡 2×4 4B 224.1 → 138.2 −38.3% 5.963 → 6.197
A5 8 卡 2×4 9B 452.7 → 325.9 −28.0% 10.117 → 10.389
A5 8 卡 2×4 27B 1871.8 → 1406.3 −24.9% 31.871 → 32.300
A5 8 卡 2×4 35B-A3B 661.8 → 633.2 −4.3% 41.202 → 41.201
A2 8 卡 4×2 0.8B 62.2 → 62.9 +1.1% 2.252 → 2.252
A2 8 卡 4×2 2B 186.4 → 169.8 −8.9% 5.810 → 6.858
A2 8 卡 4×2 4B 441.9 → 388.4 −12.1% 10.747 → 11.963
A2 8 卡 4×2 9B 1039.4 → 868.4 −16.4% 18.488 → 19.769

A2 2×4 的 CANN profiler 中,27B 的未掩盖通信从 1772.9 ms 降至 567.1 ms,4B 从 315.2 ms 降至 148.8 ms。

HSDP Host 调用与 Device event(关 → 开,ms)
机器与布局 Qwen3.5 规格 Host 调用 Device event
A2 8 卡 2×4 0.8B 55.5 → 58.2 65.6 → 62.4
A2 8 卡 2×4 2B 75.0 → 74.8 184.4 → 141.7
A2 8 卡 2×4 4B 102.5 → 126.4 460.3 → 324.0
A2 8 卡 2×4 9B 108.6 → 132.7 935.6 → 707.2
A2 8 卡 2×4 27B 210.0 → 263.8 3801.2 → 2868.6
A2 8 卡 2×4 35B-A3B 151.0 → 157.4 1624.7 → 1579.5
A5 8 卡 2×4 0.8B 26.8 → 27.9 32.2 → 28.7
A5 8 卡 2×4 2B 34.4 → 37.7 83.7 → 59.1
A5 8 卡 2×4 4B 48.8 → 56.5 223.9 → 138.1
A5 8 卡 2×4 9B 56.6 → 66.2 452.6 → 325.7
A5 8 卡 2×4 27B 107.5 → 131.2 1871.7 → 1406.1
A5 8 卡 2×4 35B-A3B 76.0 → 79.1 661.6 → 633.1
A2 8 卡 4×2 0.8B 39.3 → 42.9 62.0 → 62.8
A2 8 卡 4×2 2B 45.3 → 52.0 186.2 → 169.6
A2 8 卡 4×2 4B 61.3 → 66.5 441.6 → 388.2
A2 8 卡 4×2 9B 73.3 → 82.0 1039.2 → 868.3
HSDP CANN profiler(关 → 开,ms)
机器与布局 Qwen3.5 规格 计算 总通信 未掩盖通信
A2 8 卡 2×4 0.8B 19.3 → 23.0 55.0 → 78.4 53.4 → 58.5
A2 8 卡 2×4 2B 66.1 → 82.5 173.1 → 264.5 160.5 → 195.5
A2 8 卡 2×4 4B 162.5 → 194.0 351.2 → 323.9 315.2 → 148.8
A2 8 卡 2×4 9B 463.0 → 554.7 567.1 → 596.8 500.2 → 178.2
A2 8 卡 2×4 27B 2058.3 → 2353.4 2015.7 → 2241.1 1772.9 → 567.1
A2 8 卡 2×4 35B-A3B 1065.4 → 1080.9 1070.2 → 1121.7 576.6 → 569.4
  • 容量限制。 2×4 下,122B-A10B 和 397B-A17B 的 BF16 权重及梯度分别需要约 112.7 和 367.7 GiB/卡,超过两种机器的单卡容量。
  • 回归覆盖。 本地 50 个 Muon 与 NS backend 单测及 14 个子测试通过,7 个多卡用例收集成功。A2 实机通过 test_muon_e2e_hsdp_overlap_4p 和 test_muon_e2e_hybrid_shard_replicate_8p。

Self-checklist

  • 设计已通过 Maintainer 评审
  • 随 PR 提供开关行为及数值对照的测试覆盖
  • 已记录性能目标、实机结果和容量限制
  • 对外接口已完成接口评审
  • 官网文档同步

@townwish4git

Copy link
Copy Markdown
Contributor Author

当前提交 85b0ca6b 尚无 Jenkins 流水线结果,申请运行本次 Muon 通算重叠改动的 CI,以取得当前提交的门禁结果。

@townwish4git

Copy link
Copy Markdown
Contributor Author

/retest

@mindspore-ai-bot

Copy link
Copy Markdown

Gate trigger failed: please fix the /check-pr findings and obtain the pr-check-pass label before retrying.

@townwish4git

Copy link
Copy Markdown
Contributor Author

/check-pr

@townwish4git

Copy link
Copy Markdown
Contributor Author

/retest

@mindspore-ai-bot

Copy link
Copy Markdown

🔵 The pipeline #2234 is running. Please wait a moment... (Link)

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details
Hyper-parallel_Github_Gate - ❌ FAILURE 2234
Check_Pylint FAILURE 2234

@mindspore-ai-bot

Copy link
Copy Markdown

@townwish4git, your gate pipeline failed at the code check stage. You can configure some code check tools locally following the (pre-push quick guide), to enable automatic checking when you push your code every time. Tools included are: Check_CMakelint, Check_Codespell, Check_Cpplint, Check_Lizard, Check_Pylint, Check_ClangFormat, Check_ShellCheck.

@townwish4git
townwish4git force-pushed the feat/muon-shard-comm-overlap branch from 85b0ca6 to 678f287 Compare November 25, 2026 09:40
@townwish4git

Copy link
Copy Markdown
Contributor Author

已在 678f2875 处理 Jenkins #2234 报告的三处 pylint 问题。批量复制沿用 FSDP 同类调用的行级 protected-access 标注,两个测试回调使用 unused_name。本地以 Jenkins 对应规则复核通过,50 个相关单测及 14 个子测试均通过,申请对新提交重新运行 CI。

@townwish4git

Copy link
Copy Markdown
Contributor Author

/check-pr

@townwish4git

Copy link
Copy Markdown
Contributor Author

/retest

@mindspore-ai-bot

Copy link
Copy Markdown

🔵 The pipeline #2236 is running. Please wait a moment... (Link)

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details(Full/Incremental) Detail
Coverage Line Coverage ✅ SUCCESS 80%/59% Link

@mindspore-ai-bot

Copy link
Copy Markdown
Platform Type Module Excution Time Time Limit
- distributed_data 372.745s 450.0s
- activation_memory 369.544s 375.0s
- auto_models 105.653s 150.0s
- shard 149.893s 300.0s
- swap_optimizer 318.248s 300.0s
- optimizer 323.129s 300.0s
- pipeline_parallel 591.004s 675.0s
- fully_shard 337.098s 600.0s
- accuracy 90.466s 120.0s
- clip_grad 95.176s 120.0s
- process_group 79.538s 90.0s
- distributed_checkpoint 79.304s 120.0s
- context_parallel 92.711s 240.0s
- llama3_examples 63.929s 120.0s
- expert_parallel 237.5725s 200.0s
- tensor_parallel 214.018s 300.0s
- dtensor 73.703s 120.0s
- init_weights 80.443s 118.75s
- ops 2.57125s 150.0s

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details
Hyper-parallel_Github_Gate - ✅ SUCCESS 2236

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: 支持 Muon 分片通信与计算重叠

2 participants