Merged
feat(mega_moe): support per-expert weight tensor lists #9493
feat(mega_moe): support per-expert weight tensor lists #9493
Merged
luobqAdd on Aug 3
luobq
luobqCollaborators
Aug 3

描述

背景

原来 MoE 权重以单元素 TensorList 承载三维/四维堆叠 tensor(weight 为 3D、scale 为 4D),不符合逐 expert TensorList 的常规表达方式。本 PR 在保持旧堆叠布局完全兼容的同时,新增逐 expert weight tensor 布局支持。

改动内容

1. 支持两种兼容布局

  • 旧布局(堆叠):TensorList 长度为 1,weight 为 3D,scale 为 4D;
  • 新布局(逐 expert):TensorList 长度为 MoE expert 数,weight 为 2D,scale 为 3D。

2. Tiling 改动(mega_moe_tiling.cpp / mega_moe_tiling.h)

  • 通过 weight 输入维数识别布局(weight 为 2D 判定为逐 expert 布局,3D 判定为堆叠布局);
  • 统一校验同一 TensorList 内各 tensor 属性、各组权重与 scale 的布局、数量以及矩阵维度;
  • 新增 isPerExpertWeightTensor tiling 字段,随 MegaMoeTilingData 下发到 Kernel。

3. Kernel 改动(mega_moe.h / mega_moe_wave.h)

  • MegaMoe 与 MegaMoeWave 保存原始 TensorList 地址(不再在 Init 阶段提前解析 TensorList[0]);
  • 旧布局:解析 TensorList[0] 后叠加累计元素 offset;
  • 新布局:直接解析 TensorList[expertIdx],不叠加专家间 offset。

4. 输入适配范围

  • MoE 与 shared expert 的四组输入 weight1 / weightScales1 / weight2 / weightScales2 均已适配;
  • shared expert 使用独立的本地 shared expert 下标,与 MoE expert 下标解耦。

5. 公共能力收敛(mega_moe_base.h)

  • ExpertWeightTensorListAddrs 结构体与模板函数 GetExpertWeightAddr<ElementType> 收敛到 mega_moe_base.h,避免普通路径与 Wave 路径重复实现。

6. 兼容性

  • 保留旧堆叠布局的累计 offset 行为,不影响存量调用;
  • 不修改 mega_moe_layered.h,layered 路径不在本 PR 支持范围。

7. 建议后续用例覆盖

  • 新旧两种布局;
  • 普通路径 / Wave 路径;
  • MoE / shared expert;
  • GMM1 / GMM2;
  • 零 token expert;
  • 各 TensorList 元素地址不连续的场景。

关联的Issue

无

测试

  • 完整 pre-commit、clang-format、OAT 与 git diff --check 均已通过;
  • 未执行完整 kernel 编译和功能用例,需在 CI 中补充编译与功能验证。

文档更新

无

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for luobq's contribution.)
luobqluobqCollaborators
Aug 3 Created pull request, commit f146e5b7
atomgit-bot
atomgit-bot
Aug 3 Comments:

变更摘要

本次 PR 为 MegaMoE 内核新增逐 expert 权重 TensorList 布局支持。原有实现将多个 expert 的权重堆叠在单个 3D/4D tensor 中(TensorList 长度为 1),现同时兼容每个 expert 独立一个 2D/3D tensor 的逐 expert 布局(TensorList 长度等于 expert 数)。tiling 阶段通过权重张量维数自动识别布局,kernel 阶段通过新增的 GetExpertWeightAddr 辅助函数统一解析地址,MoE 与 shared expert 的四组权重输入(weight1/weightScales1/weight2/weightScales2)均已适配。

主要改动

  • 新增 ExpertWeightTensorListAddrs 结构与 GetExpertWeightAddr 辅助函数:在 mega_moe_base.h 中定义结构体保存四组权重的 TensorList 入口地址,通过 GetExpertWeightAddr 根据 isPerExpertWeightTensor 标志决定是按 expert 下标直接取 tensor 地址(新布局),还是取 TensorList[0] 后叠加元素偏移量(旧布局)。

  • MegaMoeTilingData 新增 isPerExpertWeightTensor 字段:tiling 阶段根据 weight 张量维数(2D 为逐 expert,3D 为堆叠)判定布局,结果通过该字段下发到 kernel,MoE 与 shared expert 共用同一布局标志。

  • MegaMoe 与 MegaMoeWave 的 UpdateGlobalBuffer 和 UpdateSharedGlobalBuffer 签名变更:两个函数新增 expertIdx/sharedExpertIdx 参数,内部将对 params_.bGmAddr 等成员的直接偏移替换为调用 GetExpertWeightAddr,实现布局透明的权重地址解析。

  • Init 阶段改为保存原始 TensorList 地址:MegaMoe::Init 和 MegaMoeWave::Init 不再在初始化时提前解析 TensorList[0] 存入 params_,改为将四组权重 TensorList 地址存入 moeWeightTensorListAddrs_ 和 sharedWeightTensorListAddrs_ 成员变量。

  • 调用点适配:所有 UpdateGlobalBuffer 和 UpdateSharedGlobalBuffer 的调用点(如 Process、ProcessGmm1Wave、ProcessGmm2Wave、ProcessCombineExperts、ProcessSharedExpertGmm1、ProcessSharedExpertGmm2 等)均新增传递当前 expert 或 shared expert 下标。

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

💡 Tip:

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

CLA Signature Pass

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

likedislike
luobqluobqCollaborators
Aug 3 Pre merge succeeded (commit_id: 5537717280a9a2144849540c40d44de6173295bb)
luobqluobqCollaborators
Aug 3 Pushed 1 commits:01823d0f-fix(mega_moe): handle empty shared expert placeholders
luobqluobqCollaborators
Aug 3 Pre merge succeeded (commit_id: 76adde3647600fe498e0de8940ea9efa0dd6baf0)
CANN-robotCANN-robotCollaborators
Aug 3 Removed label: cann-cla/yes
CANN-robotCANN-robotCollaborators
Aug 3 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Aug 3 Added label: cann-cla/yes
luobq
luobqCollaborators
Aug 3 Comments:

/compile

likedislike
luobqluobqCollaborators
Aug 3 Pre merge succeeded (commit_id: 3a06219d68565ff04e7ff4bb13dca501640266f2)
CANN-robotCANN-robotCollaborators
Aug 3 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Aug 3 Comments:

流水线任务触发成功
任务链接 [40c87c65b63c4506aac51fb0ce100ca4][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Pre ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_classify ✅ SUCCESS >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_9030_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-transformer ✅ SUCCESS
UT_Test_harmony-infer-cpro-transformer ✅ SUCCESS
UT_Test_report_lcov ⚠️ WARNING >>>>>

[2026-08-03 21:12:55]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Aug 3 Comments:

流水线任务触发成功
任务链接 [aba3e2822dbb45fb9e738016eebd90af][流水线指导]

任务名称状态日志下载链接
codecheck ❌ FAILED >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_style ✅ SUCCESS >>>>>
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_checkpr ✅ SUCCESS
codecheck_precommit ✅ SUCCESS >>>>>
likedislike
CANN-robotCANN-robotCollaborators
Aug 3 Added label: api-check-pass
6 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 3 Added label: cann-cla/yes
zhuxueling
zhuxuelingCollaboratorsAug 3Start code review2
mc2/mega_moe/op_host/op_tiling/arch35/mega_moe_tiling.cpp
@@ -473,29 +522,30 @@
473522 }
474523 
475- auto sharedWeight1StorageShape = context->GetDynamicInputShape(config.sharedWeight1Index, 0);
476- int64_t sharedExpertNum = 0;
477- if (sharedWeight1StorageShape != nullptr) {
478- sharedExpertNum = sharedWeight1StorageShape->GetStorageShape().GetDim(0);
479- }
524+ int64_t sharedExpertNum = GetWeightExpertCount(context, config.sharedWeight1Index, isPerExpertWeightTensor);
480525 OP_TILING_CHECK(sharedExpertNum > NUM_FOUR,
481526 OP_LOGE_FOR_INVALID_VALUE(nodeName, "sharedExpertNum", std::to_string(sharedExpertNum).c_str(),
482527 "only support 0-4"),
@@ -517,12 +562,8 @@ static ge::graphStatus SetAttrParams(const gert::TilingContext *context, MegaMoe
517562 tilingData->activationAlpha = 1.0f;
518563 tilingData->activationBeta = 1.0f;
519564 
520- auto sharedWeight1StorageShape = context->GetDynamicInputShape(config.sharedWeight1Index, 0);
521- if (sharedWeight1StorageShape != nullptr) {
522- tilingData->sharedExpertNum = static_cast<uint32_t>(sharedWeight1StorageShape->GetStorageShape().GetDim(0));
523- } else {
524- tilingData->sharedExpertNum = 0;
525- }
565+ tilingData->sharedExpertNum = static_cast<uint32_t>(
566+ GetWeightExpertCount(context, config.sharedWeight1Index, tilingData->isPerExpertWeightTensor));
526567 
527568 auto weightOneDesc = context->GetDynamicInputDesc(config.weight1Index, 0);
528569 int64_t opQuantMode = GetOpQuantModeByAttrDispatchOutType(context, config);
@@ -977,182 +1018,313 @@ static ge::graphStatus CheckTensorPtrNullptr(const gert::TilingContext *context,
9771018 return ge::GRAPH_SUCCESS;
9781019}
9791020 
1021+struct NamedIndex {
zhuxueling
zhuxuelingAug 3Comments:

为什么要放到这个位置?

likedislike
luobq
luobqCollaborators
Aug 3 Comments:

已修改

likedislike
luobq
luobqCollaborators
Aug 4 Comments:

/compile

likedislike
luobqluobqCollaborators
Aug 4 Pre merge succeeded (commit_id: d4c0cda210681de53c92c39182bc9357cb85263d)
CANN-robotCANN-robotCollaborators
Aug 4 Removed label: ci-pipeline-failedapi-check-pass
CANN-robotCANN-robotCollaborators
Aug 4 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Aug 4 Comments:

流水线任务触发成功
任务链接 [b69c24d2174547438526eb3b28ff27c2][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Pre ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_classify ✅ SUCCESS >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_9030_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-transformer ✅ SUCCESS
UT_Test_harmony-infer-cpro-transformer ✅ SUCCESS
UT_Test_report_lcov ⚠️ WARNING >>>>>

[2026-08-04 09:30:41]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Aug 4 Comments:

流水线任务触发成功
任务链接 [953da6ed00074ce0885d28f82de6d92e][流水线指导]

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_style ✅ SUCCESS >>>>>
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_checkpr ✅ SUCCESS
codecheck_precommit ✅ SUCCESS >>>>>

[2026-08-04 09:16:28]    CI执行结束

likedislike
CANN-robotCANN-robotCollaborators
Aug 4 Added label: api-check-pass
CANN-robotCANN-robotCollaborators
Aug 4 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 4 Added label: ci-pipeline-passed
luobqluobqCollaborators
Aug 4 Resolved final issue
yangzeheng
yangzehengCollaborators
Aug 4 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 4 Added label: approved
macech
macechCollaborators
Aug 4 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Aug 4 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 4 Merged pull request