Merged
add hybrid template for dispatch training #9388
zhong-zixinAdd on Jul 31
add hybrid template for dispatch training #9388
Merged
Pull Request has been successfully merged by CANN-robot.
(Thank you for zhong-zixin's contribution.)Jul 31 Created pull request, commit 9b8c3350
Jul 31 Added label: stat/needs-squash
Jul 31 Added label: cann-cla/yes
CANN-robot
Jul 31 Comments:
Jul 31 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
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| */*/op_host/*_def.cpp | ✅ 王永光, 俞郑中, 蒋礼锐 (3/2) | ✅ 王永光 (1/1) |
| mc2 | ✅ 俞郑中, 蒋礼锐 (2/2) | ✅ 俞郑中 (1/1) |
| sig-ops-transformer | ✅ 俞郑中, 蒋礼锐 (2/2) | ✅ 俞郑中 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
zhong-zixin, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Jul 31 Set captainmiaow,monologue815,jiang-lirui,Allan_Yu,wang-minbo,tgwsakiko_,tang-lei01,liudan12,chenjunjian11,yangzeheng,liuboxi,luobaiqing,libohao6,mabing1118,wangyongguang,macech as approver
Jul 31 Set captainmiaow,monologue815,jiang-lirui,Allan_Yu,wang-minbo,tgwsakiko_,tang-lei01,liudan12,chenjunjian11,yangzeheng,liuboxi,luobaiqing,libohao6,mabing1118,wangyongguang,macech as approver
163 messages are collapsed here。 See more
Aug 3 Added label: approved
jiang-lirui
Aug 3 Comments:
Aug 3 Comments:
/lgtm


Aug 3 Added label: lgtm
Aug 3 Closed associated issue
Aug 3 Merged pull request
描述
为dispatch训练增加跨超模板,引入二级去重,降低跨超通信量。
关联的Issue
关联Issue #4089
测试
本地验证
文档更新
不涉及
类型标签
PR #9388 代码审查报告
hybrid-exp->masterb8c3ca70cd8db202f2de225cf8f3a0e4c186076a)ci-pipeline-failed/api-check-failed/stat/needs-squash一、PR 概要
本 PR 为 MoE EP(Expert Parallelism)分发/合并通信链路引入 hybrid 网络拓扑支持,在原有 DIRECT(全互联)模式基础上新增 HYBRID 模式——节点内(scaleup)通过 HCCLS window 直传,跨节点(scaleout)通过 proxy rank 中转。变更贯穿 ACLNN API -> Host Tiling -> Kernel -> Torch Extension 全链路。
Commit 列表
变更文件清单(19 个文件)
mc2/moe_ep_dispatch/op_kernel/moe_ep_dispatch_hybrid.hmc2/moe_ep_dispatch/op_kernel/moe_ep_dispatch_base.hmc2/moe_ep_dispatch/op_kernel/moe_ep_dispatch.cppmc2/moe_ep_dispatch/op_kernel/moe_ep_dispatch_tiling.hmc2/moe_ep_dispatch/op_host/op_tiling/moe_ep_dispatch_tiling.cppmc2/moe_ep_dispatch/op_host/moe_ep_dispatch_def.cppmc2/moe_ep_dispatch/op_api/aclnn_moe_ep_dispatch.cppmc2/moe_ep_dispatch_epilogue/op_api/aclnn_moe_ep_dispatch_epilogue.cppmc2/moe_ep_dispatch_epilogue/op_host/moe_ep_dispatch_epilogue_def.cppmc2/moe_ep_dispatch_epilogue/op_host/op_tiling/moe_ep_dispatch_epilogue_tiling.cppmc2/moe_ep_dispatch_epilogue/op_kernel/moe_ep_dispatch_epilogue_tiling.hmc2/moe_ep_combine/op_api/aclnn_moe_ep_combine.cppmc2/moe_ep_combine/op_host/moe_ep_combine_def.cppmc2/moe_ep_combine/op_host/op_tiling/moe_ep_combine_tiling.cppmc2/moe_ep_combine/op_kernel/moe_ep_combine.cppmc2/moe_ep_combine/op_kernel/moe_ep_combine.hmc2/moe_ep_combine/op_kernel/moe_ep_combine_tiling.htorch_extension/cann_ops_transformer/ops/csrc/elastic_buffer.cpptorch_extension/cann_ops_transformer/ops/elastic_buffer.py二、审查结论
合入建议:可合入,建议处理以下问题
无阻塞级问题。存在 1 个性能取舍项、2 个待改进项、6 个后续优化建议。
问题统计
三、性能取舍
1. [moe_ep_dispatch_hybrid.h:1547]
SendEntries远程路径每条 entry 调用DrainSendEntries中每次WriteWithNotifyNbi后立即hcomm_.Drain(commHandle),序列化所有 HCCS 写,无法利用 channel pipeline。但这是规避 URMA CQ 超时和偶现卡死的稳定性措施。在底层免 Drain 问题解决前,不能简单改为循环末尾统一 Drain。
四、待改进项
1. [moe_ep_dispatch_hybrid.h:1986]
TransferDispatchPayloads中SyncAll阻断 scaleout→scaleup 流水TransferDispatchPayloads的执行流程:第一个
SyncAll要求所有 AIV 完成 scaleout 发送和 fanout 记录构建后,节点内 payload 发送才能开始。这意味着 scaleout 通信和 scaleup 通信无法重叠,对于跨节点延迟较高的场景,节点内 AIV 会空闲等待。2. [三个 op definition]
rank_num_per_server默认值仍为1,与 ElasticBuffer 内部默认值2不一致moe_ep_dispatch_def.cpp:104Int(1)moe_ep_dispatch_epilogue_def.cpp:63Int(1)moe_ep_combine_def.cpp:65Int(1)elastic_buffer.cpp:748rankNumPerServer_2elastic_buffer.cpp:838moeRankSizePerServer_2ElasticBuffer 的
EnsureMoeContext在Build后会用rankNumPerUbDomain_覆盖rankNumPerServer_,实际运行时不会使用默认值,因此不影响功能正确性。但 op definition 侧的 attr 默认值1与 ElasticBuffer 侧默认值2不一致,容易误导排查。五、后续建议项
moe_ep_dispatch_hybrid.h:1691-1710WaitScaleoutSlotCount/WaitScaleoutSlotReady使用 100 cycle 退避 busy-wait 轮询 GM,可研究动态调整退避周期。moe_ep_dispatch_hybrid.h:1290-1306SendScaleoutPayloadShard逐 entry SyncFuncmoe_ep_dispatch_hybrid.h:1926PipeBarrier<PIPE_ALL>()可研究缩小SyncAll和后续TPipe::Reset,不能把所有位置统一换成两个SyncFunc。非正确性问题。moe_ep_combine_tiling.h:40scaleoutSlotAlignedBytes被 tiling 计算但 kernel 未使用moe_ep_combine_tiling.cpp:43MAX_EP_WORLD_SIZE从 128 提升到 1024sendRankSlotTensor_(按epWorldSize_分配)需 4KB UB,需确认大规模部署时 UB 容量充足。elastic_buffer.cpp:1232routeDstScaleout和routeScaleoutSlot各numTokens * topK * sizeof(int32_t)字节在 DIRECT 模式下全为 -1 且不被消费。不影响正确性,大 token 场景下浪费显存。可考虑在 DIRECT 模式下分配{1, 1}占位 tensor。六、已确认无问题的项(初版误报,已排除)
以下项目在初版报告中列为问题,经核对不构成功能或正确性问题:
hidden * 2(max_out_dtype_size = 2)预留 dispatch slot。FP8 实际为hidden * 1 + scales,合法 scales 规格下不超过这份 2 字节预留。全部支持范围hidden=1..8192、topk=1..32、float32/MX scales 均无溢出。max_out_dtype_size = 2保守覆盖,无需新增 dtype 参数。DataCopy按 32B datablock 搬运。HostPinnedCounter分配 4 个int64_t(32B),只有第一个元素被轮询,尾部不参与功能。DIRECT 原模板也采用相同写法。route_*输出elastic_buffer.py调用,ACLNN 接口不对外开放,Python 中 DIRECT 模式不使用这些参数。不构成实际问题。_MoeEpWindowLayout、_get_moe_ep_window_layout、_get_moe_ep_direct_window_bytes。已修复。MOE_EP_DEBUG_FORCE_NETWORK_MODEMOE_EP_DEBUG_*、getenv、PRINTF等调试代码。已移除。rank_num_per_serverworld_size <= 1024,计算开销很小,主要代价是可能多分配 window 内存。已知设计取舍。publishedStatus - 1Uuint32 下溢ClearReceivedScaleoutStatuses前已通过WaitScaleoutSlotCount等到状态非零;零 payload 也会发布1。远端异常时会先卡在等待阶段,不会执行到减一。如需防御应超时报错或断言,而非continue(会掩盖协议错误)。不构成功能问题。networkMode已有OP_LOGD输出。可观测性建议,非功能问题。moeRankSizePerServer_默认值不一致Build后用rankNumPerUbDomain_覆盖。不影响功能(但 op definition 侧 attr 默认值仍建议统一,见待改进 #2)。七、模块详细审查
模块 A: moe_ep_dispatch hybrid
变更规模: 新增
moe_ep_dispatch_hybrid.h(2002 行模板头文件),moe_ep_dispatch_tiling.cpp+327 行,moe_ep_dispatch_base.h+81 行,aclnn_moe_ep_dispatch.cpp+57 行,其余文件少量适配。总计 +2510/-56 行。核心逻辑: Hybrid 模式实现两级通信拓扑——节点内(scaleup)通过 HCCLS window 直传,跨节点(scaleout)通过 proxy rank 中转。
流程:
CalSendCnt(多核并行统计 per-rank/per-expert/per-scaleout token count)->ExchangeCount(HCCLS 通知远端 rank)->GetRecvCount(AIV0 轮询接收)->PreparePayloads(构建 payload stash + send entry + route table)->TransferDispatchPayloads(scaleout 发送到 proxy + fanout entry 构建 + scaleup 发送 + status 清理)架构亮点:
sendRouteIndexByScaleoutTensor_直接索引 scaleout->route entry 映射InitRouteStateTensors将numRecvPerRankBuf_精确切片为 slot 前缀、rank 去重、route 状态等多个生命周期不重叠的 tensorAtomicAdd+PipeBarrier<PIPE_MTE3>写入,ReadGmByPassDCache读取,配合SyncAll<true>确保多核数据一致SCALEOUT_SLOT_READY,producer-consumer 模式严谨cached_route_*输入和route_*输出的 shape/dtype/format 校验完整,topo_type和rank_num_per_server属性校验(整除关系、正数)到位if constexpr (NetworkMode == TILINGKEY_HYBRID)分支编译期隔离整体评价: 架构设计成熟,流水同步(MTE2/V/S/MTE3 间的 SyncFunc 配对)在 2000 行代码中基本无遗漏,UB buffer 复用生命周期管理清晰。
SendEntriesper-entry Drain 是已知稳定性取舍。TransferDispatchPayloads中 scaleout→scaleup 的SyncAll是可后续优化的流水瓶颈。MOE_EP_DEBUG_FORCE_NETWORK_MODE已移除。模块 B: moe_ep_dispatch_epilogue
变更规模: +156/- 行
核心逻辑: epilogue tiling 新增 HYBRID 模式窗口布局分支,窗口分为 state -> scaleout recv data -> scaleup final recv data -> scaleout recv status -> combine data -> payload stash 六段。DIRECT 模式保持原有四段布局。
整体评价: 窗口 offset 链式推导可追溯,HYBRID/DIRECT 路径分离良好。perSlotBytes 计算含 scales,FP8 场景覆盖正确。
模块 C: moe_ep_combine
变更规模: +196/- 行
核心逻辑: combine 新增
topoType+rankNumPerServer参数支持 hybrid 网络模式,窗口布局按 DIRECT/HYBRID 分路计算。移除从未使用的bias0/bias1参数和硬编码为 1 的expertAlignment。亮点:
src_token_idx和src_topK_idx边界检查(moe_ep_combine.h:437-439),防止越界访问topkWeightsGm_和xGm_,是重要安全改进topo_type=0(DIRECT)、rank_num_per_server=1、has_topk_weights=true,确保旧调用方不受影响整体评价: 功能逻辑正确,hybrid 窗口布局设计合理。perSlotBytes 使用
hidden * 2保守覆盖 FP8 scales,无 sizing 遗漏。模块 D: elastic_buffer
变更规模: +295/- 行
核心逻辑: Python/C++ 接口同步新增 route count/dst_scaleout/scaleout_slot 三组 tensor,贯穿 Python->C++->ACLNN 调用链。C++ extension 对 optional output 创建 dummy tensor(
at::empty({1}, ...))而非传空,与 REQUIRED 输出变更一致。cachedRouteCount/DstScaleout/ScaleoutSlot的 all-or-none 校验和 hybrid+cached 交叉校验逻辑严谨。Python sizing 已提取
_MoeEpWindowLayout、_get_moe_ep_window_layout、_get_moe_ep_direct_window_bytes共享 helper,消除了计算逻辑重复。max_out_dtype_size = 2保守覆盖 FP8 scales。整体评价: Python/C++ buffer 公式与 C++ tiling 的 HYBRID 窗口布局结构一致。静态 sizing 遍历所有合法
rank_num_per_server取 max 是已知设计取舍(sizing 时 context 尚未创建)。八、PR 元信息问题
ci-pipeline-failed+api-check-failedstat/needs-squash,5 个 commit 需 squash九、审查方法说明
初版修正记录
upstream/master(错误)ea55db8bed(正确)ea55db8bed(正确)本版核对变更
hidden*2覆盖)、Host counter 32B(AscendC datablock 对齐)、DIRECT route_* 输出(ACLNN 不对外开放)、uint32 下溢(协议保证)、单 server 降级日志(非功能)、默认值不一致(不参与计算)、单 server 降级日志MOE_EP_DEBUG_FORCE_NETWORK_MODE(已移除)rank_num_per_serverSendEntriesper-entry Drain(稳定性措施)TransferDispatchPayloadsSyncAll 阻断 scaleout→scaleup 流水rank_num_per_server默认值仍为 107cbbc6af->16a8cc93f审查方法
由子 agent 分模块深度审查后汇总,核对基于
git diff ea55db8bed..16a8cc93f逐文件分析。审查重点:内存安全、同步正确性、数据竞争、逻辑正确性、Tiling 一致性、API 参数校验、Python/C++ 接口一致性、性能、代码风格。