Merged
feat(npu): add prepared L+B bindings and async communication APIs #20
feat(npu): add prepared L+B bindings and async communication APIs #20
Merged
&NewgateAdd on 23 days ago
&Newgate
&Newgate
23 days ago

PR功能描述 / 为什么需要这个合入

为 MoonEP 增加“本地 L 个专家 + B 个 SHMEM 对称通信槽”的存储布局,并扩展对应的专家通信、zero-copy 存储和同步接口。调用方可以保留本地权重与梯度地址,预先准备通信存储,再按数据生命周期安排计算通信交叠。

主要改动:

  • 新增 CommBuffers 和 ExpertPointerBinding,在 setup 阶段准备通信视图、专家地址表及必要 workspace;bank 数量、调度和复用顺序由调用方管理。
  • 扩展 Prefetch:新增普通权重指针源的 PUSH 接口,支持 MTE、URMA 后端,将本地 BF16 权重发送到目标 rank 的对称 B 槽,支持同步和异步提交,保留原 GET 接口。
  • 扩展 GradReduce:新增 GradReduceTargets,从对称梯度槽读取贡献,通过地址表直接累加到本地 FP32 梯度 Tensor,保留原 dense 接口及归约、核内同步和清槽语义。
  • 扩展 Dispatch、Combine 的显式通信 bank 选择,保留固定尺寸和原有 zero-copy 别名,通过现有通信流完成事件衔接消费者。
  • 打通 MTE Dispatch、Combine 的 num_sms 参数;UDMA 保持原有调度。
  • 新增统一的 barrier_mode / ASCEND_MOONEP_BARRIER_MODE,在公共流上屏障入口选择 mixed 或单 AIV 实现,独立于 payload 后端,保留完整 barrier 的 quiet 与 team sync 语义。
  • 补充相关测试,以及 Prefetch PUSH、GradReduceTargets benchmark。

A5验证结果

使用脚本与运行方式

算子 使用脚本 结果项
Dispatch 前向,含 Planning npu_patch/benchmarks/bench_comm.py dispatch_fwd
Combine 前向 npu_patch/benchmarks/bench_comm.py combine_fwd
Prefetch GET npu_patch/benchmarks/bench_prefetch.py GET
Prefetch PUSH npu_patch/benchmarks/bench_prefetch_push.py PUSH
dense GradReduce npu_patch/benchmarks/bench_grad_reduce.py dense
targets GradReduce npu_patch/benchmarks/bench_grad_reduce_targets.py targets

在 27.1 A5 上以 EP8 独立启动,MTE、URMA 分开,各组按 A1 → B1 → B2 → A2 执行,每次预热 10 轮、计时 50 轮,关闭 cudagraph。各臂取两次独立启动结果的几何均值,耗时变化为 (B/A−1)×100%。以下时延单位均为 ms。

Dispatch/Combine 历史实验

两项来自同一次 bench_comm.py 运行,指定以下参数:

--ep-size 8 --experts 48 --hidden 7168 --topk 8 --unbalance-ratio 1.0
--num-sms 32 --warmup 10 --iters 50 --no-cudagraph --no-allow-stub
--flows planning,dispatch_fwd,combine_fwd
--engine ubmem 或 urma

实际形状为 S=8192、H=7168、K=8、E=48。Dispatch 前向计时包含 Planning;Combine 为 hidden 前向。计时沿原脚本的迭代平均、跨 rank 均值口径。

Prefetch/GradReduce 最新实验

GET、PUSH、dense、targets 使用以下共同主配置:

--single --ep-size 8 --epn 6 --H 6144 --Hp 7168 --B 6 --nremote 6
--num-sms 32 --warmup 10 --iters 50 --no-cudagraph --no-allow-stub
--engine ubmem 或 urma

PUSH 另加 --barrier-mode mixed。实际外层通过 torch.distributed.run+runpy 执行仓内脚本,保留原计划、输入和计时函数。

四个投影 benchmark 使用原 _bench_common.time_kernel(..., agg="max"):各 rank 对连续 50 次调用整体计时后除以 50,再取 rank 最大值。

原接口 GET/GradReduce 回归:A 为目的分支 6fcf256,B 为 PR 0f5be48;GET/PUSH、dense/targets 对照均固定在 PR 0f5be48。各表使用各自完整 ABBA,不跨批拼接单臂。

原接口:PR 对比目标分支

算子 后端 目标分支 PR 耗时变化
Dispatch 前向,含 Planning† MTE 3.770000 3.772000 +0.053%
Combine 前向† MTE 3.813499 3.813500 +0.000%
Prefetch GET MTE 10.585 10.585 −0.005%
GradReduce(dense) MTE 28.271 28.304 +0.117%
Dispatch 前向,含 Planning† URMA 配置 6.778500 6.778000 −0.007%
Combine 前向† URMA 配置 3.782989 3.786998 +0.106%
Prefetch GET URMA 配置* 10.588 10.589 +0.005%
GradReduce(dense) URMA 34.594 34.573 −0.062%

† Dispatch/Combine 本轮未重跑,保留 2026-11-16 的 f407f0b 对 6fcf256 历史结果。其余四行使用 2026-11-19 完成的原接口 ABBA。

Prefetch:旧 GET 对比新 PUSH

形状 后端 GET PUSH 耗时变化 GET/PUSH
FC1:6144×7168 MTE 10.585 10.579 −0.057% 1.001×
FC1:6144×7168 URMA 配置* 10.588 9.980 −5.742% 1.061×

GradReduce:dense 对比 targets

形状 后端 dense targets 耗时变化
FC1:6144×7168 MTE 28.293 27.144 −4.059%
FC1:6144×7168 URMA(共卡探索) 34.592 34.621 +0.082%

* URMA 配置下 GET 实际仍启动 MTE kernel,PUSH 为 UDMA,因此该对照包含实际后端差异,不能将全部收益归因于 GET/PUSH 方向。

A3 验证结果

验证版本:pr20 / 1542f994。环境:Ascend910_93、CANN 9.2.0、Python 3.12.15、torch/torch_npu 2.10.0。本轮使用 EP2/EP8。

编译

npu_patch/scripts/build_a3.sh 编译成功(rc=0);import npu_patch 和 from npu_patch import _C 均通过。_C 导出 29 个符号,两项 SHMEM 描述符 ABI 大小均为 64 字节。

测试

既有套件 run_test.sh --soc_type Ascend910_93 共执行 17 条命令,结果为 16 PASS / 1 FAIL。

失败项为 test_buffer_grouped_e2e.py,报错为 rank does not match the active CANN-SHMEM PE,涉及测试工具对子通信组的处理。在目标分支dev_npu上以相同错误签名复现,涉及测试工具将子组覆盖为 WORLD,不是本 PR 引入的回归。两版本全部 17 条命令的退出码一致。

本 PR 新增、适用于 A3 的 4 个测试文件,在 EP2 和 EP8 下均通过:

测试文件 EP2 EP8
buffer_test/test_buffer_comm_buffers.py 1 passed 1 passed
buffer_test/test_buffer_grad_reduce_targets.py 14 passed 14 passed
buffer_test/test_buffer_prefetch_push.py 11 passed 11 passed
mte/test_grad_reduce_targets.py 17 passed 17 passed

Benchmark

使用 8 rank、engine=ubmem,9 个 benchmark 脚本均完成运行;新增两个脚本修复运行问题后,各完成 14/14 case。

Benchmark PR20 实测结果
bench_barrier 干净设备状态下两次运行:3.915 / 6.372 µs/barrier
bench_dispatch_maxvio 4 个配置:4556.3–4588.0 µs
bench_prefetch 14/14 case,46.2–2181 µs
bench_prefetch_push(新增) 14/14 case,59.0–1459 µs
bench_grad_reduce 14/14 case,129.7–6390 µs
bench_grad_reduce_targets(新增) 14/14 case,90.6–4649 µs
bench_grad_reduce_maxvio 4 个配置:3824.8–4300.0 µs
bench_vs 4 个 MaxVio 配置全部完成
bench_vs_all2all 4 个 target × 2 种实现全部完成

上述耗时对应本次运行配置。GradReduce dense/targets 存在工作量配置差异,不用于计算接口间加速比;Prefetch 和 barrier 的跨轮次波动较大。

希望检视人员了解:

  • 本 PR 包含 native 接口和 kernel 改动,部署时需重编匹配的 _C、rank/server kernel 库,不能混用旧构建产物。
  • 框架负责 Tensor、bank 和绑定的生命周期,以及生产、消费、复用之间的流依赖;绑定后的存储地址和布局需保持稳定。
  • MTE Dispatch、Combine 现在真正受 num_sms 控制。目标分支原来固定使用 32 个 AIV 核,复现原有核数时应显式设置 num_sms=32。
  • 原 Prefetch GET 路径保留。
  • 本轮证据限于上述单机配置、形状和路由,每个 ABBA 比较臂仅两次独立运行,不将微小差值视为稳定收益或严格等效性结论。
  • 参考文档见附件
likedislike
Pull Request has been successfully merged by ascend-robot.
(Thank you for &Newgate's contribution.)
ascend-robotascend-robotCollaborators
23 days ago Added label: ascend-cla/no
ascend-robotascend-robotCollaborators
23 days ago Added label: stat/needs-squash
ascend-robot
ascend-robotCollaborators
23 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-Ascend/MoonEP ✅ weixin_49096267, gcw_CXpJWhGA (2/2) ✅ weixin_49096267 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

zhangguobiao, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
&Newgate&Newgate
23 days ago Pushed 1 commits:f7ffb9ab-Unify expert pointer bindings and Prefetch launch routing
&Newgate&Newgate
23 days ago Pushed 1 commits:f5914caa-test(npu): consolidate DSV4 shapes into existing e2e tests
80 messages are collapsed here。 See more
ascend-robotascend-robotCollaborators
16 days ago Added label: approvedlgtm
ascend-robotascend-robotCollaborators
16 days ago Merged pull request
ascend-robot
ascend-robotCollaborators
16 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robotCollaborators
16 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robotCollaborators
16 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike