Merged
Dispatch_fullmeshv2算子支持按步进分核,打散热点专家,均衡Token发送负载 #9594
song-xinyi-001Add on Aug 4
Dispatch_fullmeshv2算子支持按步进分核,打散热点专家,均衡Token发送负载 #9594
Merged
song-xinyi-001Add on Aug 4
Merged fromDeleted :masterintocann/ops-transformermaster
song-xinyi-001
Aug 4

描述

背景:
full_mesh 的 A3(非 A5)分支当前采用 连续分核:core i 拥有专家区间 [i·sendNum_, (i+1)·sendNum_)(SplitExpertNumToCore L778-795:startId_ = sendNum_ * aivId_,dstExpertId = expertIndex + startId_)。
MoE 路由天然不均衡(热专家聚集),连续分核会让某核独揽多个热专家成为瓶颈。

体现在如下:
1、重复建表:余数专家 8 被 core0、core2 各建一次 bitmask(CalExpertSendNum 里各跑一次 CompareScalar 全扫 BS·K)。本例专家 8、9 各被建 2 次 → 12 次建表(vs 独占方案 10 次)。
2、重复遍历:余数专家的全部 token 被每个共享核都遍历一遍(ScalarGetSFFValue + 地址计算跑满),但每核只真发 1/组数。本例专家 8 的 token 被 core0、core2 各完整遍历一次,各只发一半 → 一半的遍历/地址计算是浪费。
3、连续归属:热专家聚集时负载不均,uint32_t expertIndex = (index + epRankId_ % sendNum_) % sendNum_;遍历专家时仅按epRankId_打散,同Rank的各核依然相同顺序访问目标 rank 窗口,会导致流量突发,瞬态Incast。

优化方案:
采用跳跃式步进分核,均衡打散每个AIV负责的专家,均衡地址计算和打散热点专家Token发送,优化算子时延;
A3 分支的分核方式从连续改为步进(交错):core i 拥有专家 {i, i+stride, i+2·stride, …}(stride = moeUsedAivNum_),把热专家打散到各核,均衡 CalExpertSendNum(建 bitmask)与发送的负载。
1、core i 拥有全局专家 {i, i+stride, i+2·stride, …},stride = moeUsedAivNum_,热专家被打散 → 各核更加均衡。
2、专家 e 的归属核 = e % stride;核内第 k 个专家全局 id = startId_ + k·stride。
3、余数前段兜底:sendNum_ = moeExpertNum_/stride,前 remainder 个核(aivId_ < remainder)各 +1。
4、专家遍历流程叠加 aivId_,同 rank 不同核进行错峰发送,uint32_t expertIndex = (index + epRankId_ + aivId_) % sendNum_; ,热专家被旋转错峰 → 降低瞬态incast,旋转后依然是每个核发送sendNum_的专家,每专家只被处理一次,不影响正确性。

关联的Issue

测试

测试A3单机和双机场景,重点测试大EP配置,譬如一卡8专家、一卡16专家,BS测试32/48/64/96等,实测单机与基线持平,双机部分场景有10~20%的收益。

文档更新

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for song-xinyi-001's contribution.)
song-xinyi-001song-xinyi-001
Aug 4 Created pull request, commit 5bd8d649
atomgit-bot
atomgit-bot
Aug 4 Comments:

变更摘要

本次 PR 对 MoeDistributeDispatchV2FullMesh 算子中的专家分核策略进行了重构:将原有的按专家号连续分核方式改为跳跃式步进分核,使每个 AIV 核负责的专家号在全局范围内均匀打散,从而均衡各核的地址计算开销,优化算子时延。核心改动集中在 SplitExpertNumToCore 函数及其调用方 CalExpertSendNum 和 SendToMoeExpert,同时移除了与此旧逻辑相关的成员变量和局部变量。

主要改动

  • SplitExpertNumToCore 改为步进分核逻辑:函数签名由 SplitExpertNumToCore(uint32_t &, uint32_t &) 简化为无参形式;实现上从按连续区间分配(core i 拥有 [i*sendNum_, (i+1)*sendNum_) 号专家)改为按步长 moeUsedAivNum_ 跳跃分配(core i 拥有 {i, i+stride, i+2*stride, ...} 号专家),startId_ 由 sendNum_ * aivId_ 变为直接取 aivId_,并增加了对 moeUsedAivNum_ == 0 的防御性除零检查。

  • 移除 delLastExpertId_ 成员变量及相关余数兜底逻辑:旧逻辑中 remainderExpertNum_ 不为零时需用 delLastExpertId_ 替换最后一个 dstExpertId,新步进方案通过 aivId_ < remainderExpertNum_ 时直接令 sendNum_ += 1 处理余数,不再需要该变量及对应的特殊分支。

  • CalExpertSendNum 中 dstExpertId 计算式更新:由 expertIndex + startId_ 改为 startId_ + expertIndex * moeUsedAivNum_,适配步进映射,同时移除对 remainderExpertNum_ 的尾专家特殊处理分支。

  • SendToMoeExpert 遍历与发送逻辑同步适配:expertIndex 的旋转偏移量由 epRankId_ % sendNum_ 改为 epRankId_ + aivId_,使不同源 rank 和同 rank 不同核均错峰遍历;dstExpertId 同步采用步进映射计算;移除原发送循环中基于 delCurExpertGroupNum 和 groupIdx 的条件跳过逻辑,简化了 TokenToExpertInQuant / TokenToExpert 的调用分支。

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 4 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 4 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
mc2 ✅ liuboxi, chenjunjian11 (2/2) ✅ liuboxi (1/1)

💡 Tip:

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

CLA Signature Pass

song-xinyi-001, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Aug 4 Set captainmiaow,monologue815,jiang-lirui,Allan_Yu,wang-minbo,tgwsakiko_,liudan12,chenjunjian11,yangzeheng,liuboxi,luobaiqing,libohao6,mabing1118,macech as approver
152 messages are collapsed here。 See more
song-xinyi-001song-xinyi-001
Aug 11 Change title to "Dispatch_fullmeshv2算子支持按步进分核,打散热点专家,均衡Token发送负载", originally titled "Dispatch_fullmeshv2算子支持按步进分核,优化地址计算开销,均衡Token发送负载".
liuboxi
liuboxiCollaborators
Aug 11 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 11 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Aug 11 Closed associated issue
CANN-robotCANN-robotCollaborators
Aug 11 Merged pull request