Closed
[WIP] feat(npu): add prepared L+B bindings and async communication APIs #19
&NewgateAdd on 23 days agoClosed at 23 days ago
[WIP] feat(npu): add prepared L+B bindings and async communication APIs #19
Closed
&NewgateAdd on 23 days agoClosed at 23 days ago
&Newgate
&Newgate
23 days ago

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

为 MindSpeed 的计算通信重叠接入提供原生 Buffer 接口,支持框架预分配通信 bank,以及“本地 L 份权重/主梯度 + 对称 B 槽”的存储布局。

主要改动:

  • 新增 CommBuffers 和 LPlusBBinding,在初始化阶段完成存储绑定、地址表及 workspace 准备,供后续通信复用;bank 数量、调度和复用顺序由框架管理。
  • 新增预绑定 Prefetch PUSH,支持 MTE、URMA 后端,将普通本地权重 Tensor 发送到对称 B 槽,支持同步和异步提交。
  • 新增 GradReduceTargets,通过地址表直接归约到本地 FP32 主梯度 Tensor,无需将这些目标 Tensor 重排为连续的全局专家存储。
  • Dispatch、Combine 支持显式通信 bank;异步接口通过通信流完成事件与上层衔接。
  • 打通 MTE Dispatch、Combine 的 num_sms 参数,支持独立 planning_num_sms;UDMA 保持原有调度。
  • 修复共享 Combine workspace 在不同布局之间切换时的重新准备逻辑,补充能力声明、加载库身份审计、相关测试及 Prefetch PUSH、GradReduceTargets benchmark。

实测证据

验证环境为 Ascend 950DT、CANN 9.2.0、单机 EP2/EP8。使用冻结源码及配套重编 native 库,并核验实际加载路径、SHA 和 ABI。

  • 本地 Host 回归 425 项及 18 个子测试通过,其中包含新增 benchmark 的 26 项 CPU 回归。
  • 24.1 完成 112/112 次基准运行:EP2 准入 18 次、EP8 主基准 18 次、ABBA 32 次、扩展扫描及回归 44 次。
  • 其中 94 次新增路径运行通过非零 payload 校验;18 次旧 benchmark 为运行/计时回归,不作为旧接口数值正确性证据。

当前 PR 与目标分支的同核数对照

目标分支为 d8b9db5,PR 实测版本为 3c9853f。当前源版本 72b8e69 相比实测版本仅移除了已迁往 docs 的说明文件,算子及 benchmark 代码一致。

两版 Planning、Dispatch、Combine、GradReduce 均设为 32 核。通信链及两种 dense GradReduce 形状分别按“目标分支 → PR → PR → 目标分支”执行 ABBA,共 12/12 次运行成功。

固定配置:

  • 通信链:EP8,S=8192、H=7168、K=8、E=48,rank 去重。
  • dense GradReduce:FC1 为 6144×7168,FC2 为 7168×3072,epn=B=6、nremote=2。
  • 每次预热 10 次、计时 50 次,关闭 graph capture。

单位为 µs,取每版两次独立运行耗时的几何均值;变化率为 (PR/目标分支−1)×100%,正数表示变慢。

benchmark 项目 目标分支,32 核 PR,32 核 耗时变化
Planning 90.545 90.140 −0.447%
Dispatch 前向,含 Planning 3,766.500 3,766.500 0.000%
Dispatch 反向 3,551.500 3,552.500 +0.028%
Combine 前向 3,808.999 3,807.000 −0.052%
Combine 反向 3,893.000 3,892.000 −0.026%
dense GradReduce FC1 22,294.500 22,290.500 −0.018%
dense GradReduce FC2 11,203.500 11,201.500 −0.018%

本轮同核数下未观察到明显性能退化。 Dispatch、Combine 和 dense GradReduce 的变化均在 ±0.06% 内。

旧 benchmark 的统计口径为迭代平均耗时:通信链取跨 rank 均值,dense GradReduce 取最慢 rank,不是新增 benchmark 的 median/P95。

新增路径的 MTE/URMA 后端对照

以下为 PR 内 balanced 负载的 ABBA 对照,不是相对目标分支的加速比。EP8,L=B=6;Prefetch 两臂均使用 mixed barrier、沿各自原生调度,GradReduceTargets 两臂均使用 AIV barrier、8 核。

算子 形状 MTE 延迟/ms URMA 延迟/ms URMA 相对 MTE 加速比
Prefetch PUSH FC1:6144×7168 10.4093 1.8654 5.58×
Prefetch PUSH FC2:7168×3072 5.1171 0.9868 5.19×
GradReduceTargets FC1:6144×7168 24.1337 11.1031 2.17×
GradReduceTargets FC2:7168×3072 12.1286 5.6823 2.13×

表中为各臂两次运行 median 的几何均值。计时包含公开接口提交、native barrier 和异步完成等待,排除 setup、填数、外部 HCCL barrier、校验及收尾。

旧 dense 与 targets 的存储布局、路由和计时边界不同,不直接比较二者的绝对耗时。

希望检视人员了解:

  • 本 PR 包含 native 接口和 kernel 改动,部署时需重编匹配的 _C、rank/server kernel 库,不能混用旧构建产物。
  • 框架负责 Tensor、bank 和绑定的生命周期,以及生产、消费、复用之间的流依赖;绑定后的存储地址和布局需保持稳定。
  • MTE Dispatch、Combine 现在真正受 num_sms 控制。目标分支原来固定使用 32 个 AIV 核,复现原有核数时应显式设置 num_sms=32。
  • 旧 dense GradReduce 保留原核内 barrier 和清槽逻辑,新增 targets 路径使用外置 barrier;原 Prefetch GET 路径保留。
  • 本轮证据限于上述单机配置、形状和路由,每个 ABBA 比较臂仅两次独立运行,不将微小差值视为稳定收益或严格等效性结论。
likedislike
The current Pull Request has been closed by &Newgate.
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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-Ascend/MoonEP ❌ (0/2)(You can also ask: Jia_Austin, 孙银磊, u012145166111, wangjiangben, nino888) ❌ (0/1)(You can also ask: 郑加利, lishaoxun, wangjiangben, 孙银磊, weixin_49096267)

💡 Tip:

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

CLA Signature Guide

@zhangguobiao , thanks for your pull request.

The following commit(s) have not signed Contributor License Agreement (CLA).

Commit Reason
72b8e69a docs: move L+B benchmark usage g... the email is not signed for CLA! please check if it is correct and the same as your signed email.

If you need to sign CLA, you can click here to sign the CLA.

If you need to check if the email is set up correctly, you can click here to do it in the FAQs.

After signing the CLA or updating the email, you must comment /check-cla to check the CLA status again.

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
npu_patch/api.py
@@ -587,0 +695,4 @@
695+ grad_reduce_num_sms = kwargs.pop('grad_reduce_num_sms', None)
696+ planning_num_sms = kwargs.pop('planning_num_sms', None)
697+ prefetch_barrier_mode = kwargs.pop('prefetch_barrier_mode', 'mixed')
698+ async_stream_barrier_mode = kwargs.pop('async_stream_barrier_mode', 'mixed')
Jia_Austin
Jia_Austin23 days agoComments:

这个公共接口和开源保持一致

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
npu_patch/api.py
@@ -813,0 +1056,4 @@
1056+ self._validate_lplusb_plan(plan, binding.slot, 'native L+B', allow_distinct_slots=True)
1057+ return ctx
1058+ 
1059+ def prefetch_lplusb(self, plan, binding, async_finish: bool = False):
Jia_Austin
Jia_Austin23 days agoComments:

名称改下 容易 理解 , 一眼能知道功能

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
npu_patch/api.py
@@ -813,0 +1088,4 @@
1088+ self.mark_failed(error=exc, resources=(ctx, plan, binding))
1089+ raise
1090+ 
1091+ def _reduce_lplusb(self, plan, binding, *, async_finish):
Jia_Austin
Jia_Austin23 days agoComments:

改下名称 易理解

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
npu_patch/api.py
@@ -897,0 +1242,4 @@
1242+ 
1243+ 
1244+ @staticmethod
1245+ def _lplusb_pointer_table(refs, slot_view, *, dtype, operation, expected_refs=None):
Jia_Austin
Jia_Austin23 days agoComments:

把“lplusb”都改下

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
npu_patch/api.py
@@ -894,0 +1311,4 @@
1311+ return experts_to_copy
1312+ 
1313+ 
1314+ def _reduce_expert_grads(self, plan, canonical_targets, grad_slot_view) -> None:
Jia_Austin
Jia_Austin23 days agoComments:

两个离散的 留一个

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
npu_patch/api.py
@@ -1359,2 +1858,4 @@
13591858 up_reduce_buffer: torch.Tensor | None = None,
13601859 down_reduce_buffer: torch.Tensor | None = None,
1860+ *,
1861+ lplusb: LPlusBBinding | None = None,
Jia_Austin
Jia_Austin23 days agoComments:

保持和开源API一致

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
npu_patch/api.py
@@ -894,29 +1347,30 @@
1347+ unchanged. No new symmetric allocation or expert-row staging is made.
1348+ """
1349+ ctx = self._require_ctx()
1350+ self._ensure_shmem_views()
1351+ if route_weights_nvs.dtype != torch.float32:
1352+ raise TypeError("route weights for combine must be FP32")
1353+ if not route_weights_nvs.is_contiguous():
1354+ raise ValueError("route weights for combine must be contiguous")
1355+ logical_nvs = int(route_weights_nvs.numel())
1356+ if route_weights_nvs.ndim != 1 or logical_nvs > int(ctx['NvS']):
1357+ raise ValueError("route weights for combine must be a one-dimensional capacity prefix")
1358+ carrier = ctx['weights_buf_local'].view(torch.float32)[:logical_nvs]
1359+ carrier.copy_(route_weights_nvs)
1360+ return carrier
8941361 
8951362 def _run_prefetch_weight_on_current_stream(
8961363 self,
@@ -987,7 +1454,7 @@ class Buffer:
9871454 f'invalid expert layout for grad reduce: E={E}, R={R}')
9881455 epn = E // R
9891456 B = int(ctx['B'])
990- num_sms = int(ctx['num_sms'])
1457+ num_sms = int(ctx.get('grad_reduce_num_sms', ctx['num_sms']))
9911458 for full_grad, reduce_buffer in zip(projection_grads, reduce_buffers):
9921459 # Buffer-backed only: full_grad / reduce_buffer -> local shmem
9931460 # tensors. The framework attaches this rank's local physical
@@ -1045,6 +1512,7 @@ class Buffer:
10451512 inter_rank_sync: bool = True,
10461513 zero_copy: bool = False,
10471514 router_weights_zero_copy: bool = False,
1515+ comm_buffers: CommBuffers | None = None,
Jia_Austin
Jia_Austin23 days agoComments:

保持和 开源API一致

likedislike
Jia_Austin
Jia_Austin23 days agoStart code review1
test_npu/mte/test_e2e_dsv4.py
@@ -0,0 +1,0 @@
1+# SPDX-License-Identifier: Apache-2.0
Jia_Austin
Jia_Austin23 days agoComments:

可以合入原来ut

likedislike
&Newgate&Newgate
23 days ago Close pull request