Merged
fix: 修复 QuantMatmulActivationQuant golden #13974
panzhijaAdd on 12 days ago
fix: 修复 QuantMatmulActivationQuant golden #13974
Merged
panzhijaAdd on 12 days ago
panzhija
panzhijaCollaborators
12 days ago

描述

修复 QuantMatmulActivationQuant 的 E2E NPU golden 在权重声明为 FRACTAL_NZ 时保留 ND 转置视图的问题。golden 按用例格式转换权重,使转置权重和连续 scale 的组合能够执行独立算子拼接。

统一 Kernel、ACLNN、E2E golden 的输入逻辑类型解析,校验输出类型与 x1 一致以及 FP4 输入配对,兼容 TTK 将输入及类型元数据提升为 FP32 的场景。补齐 E2E 的 MX 参数校验,支持 group_sizes 的空列表默认值和 scale_alg=None。

新增 3 条 E2E CSV 回归用例,覆盖 NZ 转置视图、可选属性默认值和 FP4 NZ。清理 Kernel ST 表中 9 条内联大量固定输入数组的用例。

关联的Issue

#8431

https://gitcode.com/cann/ops-nn/issues/8431

测试

  • 在 Ascend950DT、CANN 9.3.0 weekly 20261104、torch 2.10.0 和 torch_npu 2.10.0.post7 环境完成回归,E2E 53/53、ACLNN ND 55/55、ACLNN NZ 114/114 通过。
  • 原始 SiLU NZ 网络用例 M=6877、K=6144、N=16384 通过,两个输出均为 100%,golden_source=npu。
  • 额外 8 条 NPU 模式检查中,6 条由 NPU golden 通过,2 条 E5M2 x1 搭配 NZ 权重的用例因独立 QuantMatmulWeightNz 的类型限制回退 CPU 后通过。
  • ACLNN 回归使用 --warmup=false,当前环境的 TTK 通用预热辅助 kernel 编译失败。
  • 预提交检查通过,格式调整前后 Python AST 一致。

文档更新

测试结果和独立 NPU 拼接链的验证边界记录在本 PR 中。

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for panzhija's contribution.)
panzhijapanzhijaCollaborators
12 days ago Created pull request, commit dc29e4dd
atomgit-bot
atomgit-bot
12 days ago Comments:

变更摘要

本 PR 修复 QuantMatmulActivationQuant 的 golden 计算逻辑:在权重声明为 FRACTAL_NZ 时,正确处理 ND 转置视图与连续 scale 的组合,使 golden 与独立算子拼接链保持一致。核心做法是在 matmul/quant_matmul_activation_quant/tests/assets/spec.py 中新增统一的 MX 输入类型解析函数 _mx_operand_dtypes,供 Kernel、ACLNN、E2E 三套 TestSpec 共用,同时补齐 E2E 的 MX 参数校验和 NZ 格式绑定。此外按 PR 描述新增 3 条 E2E CSV 用例并清理 Kernel ST 表中 9 条内联固定输入数组的用例。

主要改动

  • 新增统一的 MX 逻辑类型解析:_mx_operand_dtypes 依据声明类型、input_dtypes/tensor_dtypes 元数据和实际张量 dtype 逐级回退,兼容 TTK/ACLNN 将输入及类型元数据提升为 FP32/FP64 的场景,并集中校验「输出类型必须与 x1 一致」和「MXFP4 要求两侧输入同为 FP4」。
  • _npu_activation_golden 复用新解析逻辑:原内联的 mx_dtype 转换与两条合法性检查被替换为一次 _mx_operand_dtypes 调用,a_dtype/b_dtype 直接用于 _to_npu_tensor。
  • Kernel 与 ACLNN TestSpec 提前解析类型并修正 NZ 解码:在进入 NPU 分支前先得到 a_dtype/b_dtype,并用 b_dtype 作为 weight_nz_to_nd 的 dtype 参数,避免权重按输出 dtype 而非权重自身逻辑类型解码;NPU 分支改为传入已解析的类型。
  • E2E TestSpec 补齐参数校验与 NZ 绑定:dtype 解析链改为 output_dtype → x1_dtype → tensor_dtypes[0] → x1.dtype;新增 quant_mode 必须为 mx 的校验,group_sizes 允许空元组默认值,scale_alg=None 归一化为 0,并在 FRACTAL_NZ 场景下通过 torch_npu.npu_format_cast(..., 29) 先将 x2 转成 NZ 以匹配实际算子调用前的绑定;CPU 路径改为按 a_dtype/b_dtype 生成 MX 输入。

说明:本次仅提供 spec.py 的补丁,CSV 用例新增与 Kernel ST 用例清理的内容依据 PR 描述记录,未包含在可见补丁中。


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

likedislike
Incorrect?
atomgit-bot
atomgit-bot
12 days ago Comments:

代码审查

审查结论

发现的问题

优先级 文件 行 概要
P2 matmul/quant_matmul_activation_quant/tests/assets/spec.py 63-69 _mx_operand_dtypes 的 FP32 提升兜底对 x2 也回退成 output_dtype,混合 dtype(e5m2 x1 + e4m3 x2)用例在元数据被提升时静默按错误类型算 golden;FP4 场景下 CPU 分支还会把 FP32 值当 packed nibble 解包
P3 matmul/quant_matmul_activation_quant/tests/assets/spec.py 1370-1376 E2E NZ 分支把可能非连续的转置视图直接送入 npu_format_cast,未像 _npu_nz_weight 那样先物化连续逻辑数据、也未校验 cast 结果格式

各文件审查确认

  • matmul/quant_matmul_activation_quant/tests/assets/spec.py — 已审查(_mx_operand_dtypes 新增、Kernel/ACLNN/E2E 三个 TestSpec 的类型解析改造、E2E 的 group_sizes/scale_alg/NZ 处理)。共 2 处问题,见上表。其余改动(Kernel/ACLNN 路径把 weight_nz_to_nd/_npu_activation_golden 的 dtype 从输出类型改为各自操作数类型、FP4 配对校验合并、E2E 输出类型从 tensor_dtypes[0] 兜底)经与调用链、_to_npu_tensor/_is_fractal_nz/_unpack_fp4/_raw_outputs 交叉核对未发现新的确定性缺陷。
  • matmul/quant_matmul_activation_quant/tests/st/arch35/ttk_e2e_quant_matmul_activation_quant_st.csv — 已审查,无问题。新增 3 条用例的列数与表头一致;52/53/54 行的 shape/stride/scale 形状与 K、N、packed FP4 语义自洽(52: K=128/N=256;53: K=64/N=128;54: uint8 打包 K=64/N=128),属性字段(output_dtype=None、group_sizes=[]、scale_alg=None)与 spec 新的默认值处理匹配。
  • matmul/quant_matmul_activation_quant/tests/st/arch35/ttk_kernel_quant_matmul_activation_quant_st.csv — 已审查,无问题(受限:diff 中该文件 hunk 未渲染任何删除内容,仅显示空内容,故只能核对工作区保留的 121 行结构/列数一致、fp4 用例以 float4_e2m1 类型名声明可被新 _mx_operand_dtypes 正确解析;被删除的 9 条用例内容无法核对,无法判断是否移除了唯一覆盖,建议提交者自查)。

总体风险判断

变更集中在测试 golden 侧,不影响算子生产行为,但直接决定 E2E/ACLNN/Kernel 用例的验收结论,风险等级为中等:主路径(ND、NZ 单类型 dtype)逻辑改造是净收益且未发现确定性回归;主要风险在"TTK 类型提升 + 混合 x1/x2 dtype"组合会静默产出错误 golden(假通过/假失败),以及 E2E NZ 转置视图 scene 的格式转换缺少连续性规范化。建议在合入前补一条"提升为 FP32 元数据 + e5m2 x1 / e4m3 x2"的回归用例用于锁定该分支行为。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论


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

likedislike
Incorrect?
panzhijapanzhijaCollaborators
12 days ago Modify description of pull request
CANN-robotCANN-robotCollaborators
12 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
12 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
matmul ✅ 赵颖超, 王子韬 (2/2) ✅ 赵颖超, 王子韬 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
12 days ago Set zengjuan,oscillated,sxb154714,chaotang233,crystalhu,yangyang016,fanqirui,chenqi317,LinPX,liubo75,tangweiwei2,wangzitao_leo,void_ptr,xubinglin,zhaoyingchao2,rueenov11,pingchuantang,liuyang_806 as approver
CANN-robotCANN-robotCollaborators
12 days ago Set oscillated,sxb154714,chaotang233,yangyang016,fanqirui,chenqi317,LinPX,tangweiwei2,wangzitao_leo,void_ptr,xubinglin,zhaoyingchao2,rueenov11,liuyang_806 as approver
panzhija
panzhijaCollaborators
12 days ago Comments:

compile

likedislike
panzhijapanzhijaCollaborators
12 days ago Pre merge succeeded (commit_id: 9618852989b6c6da92b8d6c88c2fd63b3deb4e05)
CANN-robot
CANN-robotCollaborators
12 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
12 days ago Added label: ci-pipeline-running
atomgit-bot
atomgit-bot12 days agoStart code review1
matmul/quant_matmul_activation_quant/tests/assets/spec.py
@@ -55,0 +66,4 @@
66+ dtype = str(value.dtype).removeprefix("torch.")
67+ # ACLNN may promote both the values and their dtype metadata.
68+ if dtype in ("float32", "float64"):
69+ dtype = output_dtype
atomgit-bot
atomgit-bot12 days agoComments:

🟡 Medium Priority

新增的 _mx_operand_dtypes 在检测到 TTK 把“输入值 + dtype 元数据”同时提升为 FP32 时(第 65-69 行),对 x1 和 x2 都 回退到 output_dtype。

证据链:

  1. 循环体 for index, value in enumerate((x1, x2)) 共用同一段兜底逻辑,dtype = output_dtype 与 index 无关;
  2. output_dtype 只代表输出(= x1)的逻辑类型,代码后面第 71-72 行也明说 “MX output dtype must match x1”。因此对 index=1 用 output_dtype 是一个未经声明的猜测;
  3. 新 CSV 与既有 CSV 里存在 x1/x2 逻辑类型不同的合法用例,例如 e2e 表 53 行 silu_nz_optional_defaults_e5m2_e4m3(x1=e5m2、x2=e4m3)、18/19/21/35/44/51 行等。一旦这些用例在 TTK 提升元数据的路径上运行,b_dtype 会被判成 e5m2 而不是 e4m3。

失败模式:

触发条件:TTK 同时提升值与 dtype 元数据到 FP32,且该用例 x1 与 x2 逻辑类型不同(CSV 中已有多条此类用例)。

建议:只在 index==0(x1,与输出同类型)时回退到 output_dtype;对 index==1 仅在输出为 FP4(此时两个输入必须同为 FP4)时才允许回退,其余情况抛出明确错误(如 "x2 logical dtype is ambiguous after FP32 promotion, please declare x2_dtype"),避免静默按错误类型量化。同时建议在 FP4 CPU 分支保持"输入必须是 packed uint8 载体"的断言,防止 FP32 值被当成 nibble 解包。

likedislike
Incorrect?
panzhijapanzhijaCollaborators
12 days ago Resolved final issue
CANN-robotCANN-robotCollaborators
12 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
12 days ago Added label: ci-pipeline-passed
zhaoyingchao2
zhaoyingchao2Collaborators
12 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
12 days ago Added label: approved
yuning_chen
yuning_chenCollaborators
12 days ago Comments:

您好,pr合入已准备就绪,请尽快联系committer进行检视,谢谢!
@zhaoyingchao2 @wangzitao_leo @chaotang233 @liuyang_806 @fanqirui

likedislike
wangzitao
wangzitaoCollaborators
12 days ago Comments:

/lgtm
/approve

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

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike