Merged
fix(moe): 修复MoeInitRoutingV3/V4动态量化及V3标杆比较 #15813
fix(moe): 修复MoeInitRoutingV3/V4动态量化及V3标杆比较 #15813
Merged
GuoqhAdd on 17 days ago
Guoqh
GuoqhCollaborators
17 days ago

描述

修复Ascend 950平台MoeInitRoutingV3/V4动态量化的图推导校验和精度问题,并修正V3标杆生成及E2E比较逻辑。

  1. V3/V4 INT4动态量化的未知rank校验:quant_mode=13时,GEIR动态图推导阶段的scale可能为未知rank {-2},原逻辑直接按二维shape检查并失败。增加未知rank判断,rank已知时继续使用原有校验。
  2. 大H动态量化的专家scale取址:quant_mode=1传入按专家配置的smooth scale时,H分块路径遗漏专家行偏移,导致不同专家均使用scale[0]。补上expertIdx * H,与不分块路径保持一致。V3/V4复用该kernel文件,均获得修复;INT4共享scale路径的expertIdx仍为0。
  3. V3标杆生成及E2E比较:增加H=0动态量化处理,避免空数组归约报错,并跳过kernel未写出的expandedScale比较;FP8非量化输出按输入类型恢复Golden,正确解释uint8字节载体,仅对4位类型执行4位打包,并使用有效路由前缀比较。精度阈值保持原值。

关联的Issue

关联Issue #7469:https://gitcode.com/cann/ops-transformer/issues/7469

测试

环境:Ascend950DT_9574,CANN 9.3.0,Python 3.10.21,torch 2.14.0+cpu、torch_npu 2.14.0,TTK 3.0.0。

2026-11-12基于提交b698ccaef重新执行库内回归:

场景 结果
V3 Kernel静态编译执行 62/62 PASS
V3同批输入适配E2E eager 62/62 PASS
V4 Kernel二进制执行 32/32 PASS
V4 ACLNN 43/43 PASS
合计 199/199 PASS
  • 四组执行均退出码0,V3/V4 Kernel内存越界检查全部通过。
  • V4 Kernel原CSV为33行,其中一条完全重复,TTK去重后执行32个独立配置。
  • V3 E2E由库内Kernel用例适配,仅将两条fp16别名等价规范为float16;输入shape、属性、数据范围和容差保持原值。原始库内CSV和Golden在本轮运行前后哈希一致。
  • 修复过程中的针对性验证:ACLNN原始0844、GEIR动态图原5条未知rank失败用例、E2E eager的0958及4条FP8比较失败用例均已复测通过;这些是修复时的验证记录,本轮库内回归未重跑GEIR和E2E图模式。

文档更新

无。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Guoqh's contribution.)
GuoqhGuoqhCollaborators
17 days ago Created pull request, commit 7ee32927
atomgit-bot
atomgit-bot
17 days ago Comments:

变更摘要

本 PR 针对 MoE 路由算子 MoeInitRoutingV3/V4 的推理与执行一致性问题进行修复:在 V3/V4 的 infershape 校验中放行 rank 未知的 scale 形状,修正 V3 arch35 动态量化 kernel 中平滑因子(smooth)按专家索引寻址的偏移计算,并更新 V3 的 Golden 标杆脚本以对齐零宽输入、FP8 原始类型及 4bit 打包等场景的比对逻辑。

主要改动

  • scale 未知维形状校验放行: moe_init_routing_v3_infershape.cpp 与 moe_init_routing_v4_infershape.cpp 的 CheckScaleShapeForInt4DynamicQuant 在 scaleShape 为单维且维度值为 ge::UNKNOWN_DIM_NUM(-2)时直接返回 ge::GRAPH_SUCCESS,将维度合法性校验推迟到 rank 确定之后,避免动态量化场景下未知形状被误判为非法。
  • 动态量化 smooth 偏移修正: moe_v3_gather_dynamic_quant_arch35.h 的 ComputeMax 在 IS_INPUT_SCALE 分支中,将 DataCopyPad 的源地址从 j * perLoopCols_ 改为 expertIdx * cols_ + j * perLoopCols_,使平滑因子按当前专家所在行正确读取。
  • 零宽输入 Golden 分支: golden.py 的 _moe_init_routing_v3_numpy 新增 quant_mode == 1 and h == 0 分支,按排序后的专家索引路由 expanded_x 并置 expanded_scale = None,与 kernel 在零宽输入下跳过量化、不写 expanded_scale 的行为保持一致。
  • FP8 类型比对对齐: E2eMoeInitRoutingV3Spec 在 quant_mode == -1 且比对输出索引为 0/3 时,依据输入 x/scale 的 e4m3/e5m2/e8m0 类型恢复 Golden dtype,并对 NPU 结果做字节视图或类型转换,消除 TTK 以 uint8 承载或提升 Golden 类型导致的误判。
  • 4bit 打包判定收紧: 在原始字节比对路径中新增 is_4bit 判断(匹配 float4/e2m1/e1m2/int4),仅在 4bit 场景才执行高低半字节合并,并改为对 npu_cmp/golden_cmp 而非原始数组取字节,避免非 4bit 数据被错误折叠。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
17 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
17 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.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
moe ✅ cpy_123456, 宋恺 (2/2) ✅ cpy_123456 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
17 days ago Set cpy_123456,cheng_zhiwei,juyangokok,monologue815,chen-linxin4,yu-xinjie62,jiang-lirui,Allan_Yu,songkai111,liudan12,Wei_NaChuan,cc-z,kdy18482276080,xuchengyang,mabing1118,Julius47 as approver
CANN-robotCANN-robotCollaborators
17 days ago Set cpy_123456,cheng_zhiwei,juyangokok,monologue815,chen-linxin4,yu-xinjie62,jiang-lirui,Allan_Yu,songkai111,liudan12,Wei_NaChuan,cc-z,kdy18482276080,xuchengyang,mabing1118,Julius47 as approver
Guoqh
GuoqhCollaborators
17 days ago Comments:

compile

likedislike
GuoqhGuoqhCollaborators
17 days ago Pre merge succeeded (commit_id: c484f4dda21cd1c0059209409227b83571cc54a8)
GuoqhGuoqhCollaborators
17 days ago Pre merge succeeded (commit_id: 64f28ede5f37449a209421bf006cf257c29a7563)
CANN-robot
CANN-robotCollaborators
17 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
17 days ago Added label: ci-pipeline-running
BartonFang
BartonFangCollaborators17 days agoStart code review1
moe/moe_init_routing_v3/tests/assets/golden.py
@@ -1278,3 +1317,2 @@
12781317 
1279- npu_raw = _to_raw_uint8(numpy.asarray(npu_arr))
1280- golden_raw = _to_raw_uint8(numpy.asarray(golden_arr))
1318+ npu_raw = _to_raw_uint8(numpy.asarray(npu_cmp))
BartonFang
BartonFang17 days agoComments:

【review】
问题分类:可测
位置:moe/moe_init_routing_v3/tests/assets/golden.py:1318,代码:npu_raw = _to_raw_uint8(numpy.asarray(npu_cmp))
问题:这里改为对 npu_cmp/golden_cmp 做 raw-byte 转换,但二者已经按转换前的 min_len 截断。当 INT4 的设备结果使用 packed uint8 载体、golden 为 ml_dtypes.int4 的逐元素数组时,一个设备字节对应两个 golden 元素,直接取两边元素数的最小值会先丢掉一半 golden,再打包和取 min_len_raw,最终只比较设备结果的前半段。CPU 复现:golden=[1,2,3,4,5,6,7,0],正确 packed 字节为 [0x21,0x43,0x65,0x07];将后两字节改为 [0xFF,0xEE] 后,当前 E2eMoeInitRoutingV3Spec.compare 仍返回 pass=true、precision=100,基线版本则返回失败。
可能导致的结果:INT4 输出后半部分的错误被新增截断隐藏,量化精度回归会被误报为通过。
建议的修改方法:先将两侧统一到相同的逻辑元素或 packed 字节表示,再按有效逻辑长度裁剪和比较;若需忽略输出填充,应显式换算每字节两个元素。增加只破坏后半段 packed 输出的负向自检,断言比较失败。

likedislike
BartonFangBartonFangCollaborators
17 days ago Resolved final issue
CANN-robotCANN-robotCollaborators
17 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
17 days ago Added label: ci-pipeline-passed
GuoqhGuoqhCollaborators
17 days ago Associated issue: [Bug-Report|缺陷反馈]: MoeInitRoutingV3/V4动态量化校验、scale取址及V3标杆比较异常
GuoqhGuoqhCollaborators
17 days ago Modify description of pull request
cpy_123456
cpy_123456Collaborators
17 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
17 days ago Added label: approved
songkai111
songkai111Collaborators
17 days ago Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
17 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
17 days ago Closed associated issue
CANN-robotCANN-robotCollaborators
17 days ago Merged pull request