已合并
[sync] PR-40312: Update moe token unpermute DTensor support #42530
[sync] PR-40312: Update moe token unpermute DTensor support #42530
已合并
ascend-robot创建于 26 天前
ascend-robot
ascend-robot成员
26 天前

1. Origin pull request:

https://gitcode.com/Ascend/pytorch/merge_requests/40312

https://gitcode.com/Ascend/pytorch/issues/3108

Sha Datetime Message
fc5ecd2f 2026-07-16 09:43:21 +0800 CST Update moe token unpermute DTensor support
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)
ascend-robotascend-robot成员
26 天前 创建了 pull request,commit 69c61834
ascend-robotascend-robot成员
26 天前 关联了issue:Update moe token unpermute DTensor support
atomgit-bot
atomgit-bot
26 天前 评论:

变更摘要

此 PR 为 MoE(Mixture of Experts)token 反置换操作新增了 DTensor 分布式分片策略支持,主要针对 _npu_moe_token_unpermutenpu_moe_token_unpermute_grad_v2 两个算子注册了分片策略函数。

主要改动

  • 新增 _npu_moe_token_unpermute_strategy 策略函数:为 npu._npu_moe_token_unpermute.default 算子注册 DTensor 分片策略,提供全复制(Replicate)和隐藏维度分片(Shard(1))两种策略,其中隐藏维度分片策略对输出张量和输入 permuted_tokens 沿第 1 维进行分片。
  • 新增 npu_moe_token_unpermute_grad_v2_strategy 策略函数:为 npu.npu_moe_token_unpermute_grad_v2.default 算子注册 DTensor 分片策略,同样支持全复制和隐藏维度分片两种策略,隐藏维度分片策略中梯度输出 permuted_tokens 沿第 1 维分片,probs 梯度使用 Partial() 归约。
  • 可选张量参数处理:两个新增策略函数均对可选的 probspermuted_tokens 参数进行了判空处理,当参数为 None 时对应策略位置填 None
likedislike
atomgit-bot
atomgit-bot
26 天前 评论:

代码审查

经过完整的审查,我对变更文件做了以下检查:

  1. _npu_moe_token_unpermute_strategy(行 101-123):与已有的 npu_moe_token_unpermute_strategy(行 77-98)对比,新增函数多了一个输出(permuted_tokens_for_backward),两个输出的 sharding 均与输入的 permuted_tokens 保持一致,参数数量与函数注释完全匹配。

  2. npu_moe_token_unpermute_grad_v2_strategy(行 151-177):与已有的 npu_moe_token_unpermute_grad_strategy(行 126-148)对比,v2 版本将必选的 permuted_tokens 张量替换为 permuted_tokens_size_0(int)和 permuted_tokens_dtype(ScalarType),并在末尾增加了可选的 permuted_tokens。输出 sharding 语义与 v1 完全一致,参数数量(8 个)与函数注释匹配。该模式亦与文件中已有的 npu_moe_token_permute_grad_v2_strategy(行 54-74)一致。

  3. 未发现逻辑错误、安全漏洞、可靠性问题或破坏性变更。


总结

优先级 数量
P0 0
P1 0
P2 0
P3 1
  • torch_npu/distributed/tensor/_moe_ops.py:已审查,发现 1 个 P3 级别问题(缺少对新增策略的测试覆盖),无其他问题。

整体风险判断:低。两个新增的分片策略函数严格遵循文件中已有的模式,分片语义正确、参数数量匹配。唯一的缺口是缺少对应的自动化测试,建议后续补充。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
ascend-robotascend-robot成员
26 天前 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
26 天前 评论:

CLA Signature Pass

ascend-ds-bot, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robot
ascend-robot成员
26 天前 评论:

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
torch_npu/distributed liangsongwei, 楚浩田 (2/2) liangsongwei, 楚浩田 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
ascend-robotascend-robot成员
26 天前 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
26 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
26 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
26 天前 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
26 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
26 天前 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
26 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
26 天前 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
26 天前 评论:
流水线 PR-pipeline_pytorch#48803 [ commitID:fb135cd1 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 >>>
UT_DIST_ARM_Part_02 >>>
UT_DIST_ARM_Part_03 >>>
UT_DIST_ARM_Part_04 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
htchu成员
26 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
26 天前 添加了label:approved
梁松伟
梁松伟成员
26 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
26 天前 添加了label:lgtm
ascend-robotascend-robot成员
26 天前 合入了pull request
ascend-robot
ascend-robot成员
26 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13054 [ commitID:fb135cd1 ] 已完成
likedislike