Merged
fix(operation): Add missing arch and format constraint checks for matmul/scaled_mm #5865
fix(operation): Add missing arch and format constraint checks for matmul/scaled_mm #5865
Merged
shi-dingkunAdd on Sep 2
shi-dingkun
shi-dingkunCollaborators
Sep 2

为 matmul/scaled_mm 补齐接口文档中缺失的业务约束检查以及文档针对多核切K功能的描述修正,并修正 FP4 UT。

约束检查

约束 检查位置
输入→输出 dtype 组合表(基础/量化/反量化场景) __validate_out_dtype
A2/A3 不支持 DT_HF8 输入 __validate_inputs
DT_HF8 不支持 NZ 格式 __validate_inputs
A2/A3 不支持 trans_mode __validate_trans_mode
scale_a/scale_b 仅支持 ND 格式 __validate_scaled_inputs
matmul 拒绝 FP4 输入(仅 scaled_mm 支持) __validate_matmul_fp4_input

FP4 UT 修正

test_fp4_packed_shape.py 重写为正规写法:独立 X/Y 矩阵,scaled_mm + scale tensors + DT_FP16 输出。

测试

test_matmul_op.py 20/20 通过,test_fp4_packed_shape.py 1/1 通过。

Related Issues: #3213

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for shi-dingkun's contribution.)
shi-dingkunshi-dingkunCollaborators
Sep 2 Created pull request, commit 50df1ee3
atomgit-bot
atomgit-bot
Sep 2 Comments:

变更摘要

该 PR 依据接口约束检查对比报告中完全缺失项 #2/#3/#4/#5,为 pypto.matmul / pypto.scaled_mm / pypto.set_cube_tile_shapes 补齐此前在 Python/C++ 校验链中均无拦截的 4 条文档约束,全部实现在 python/pypto/op/matmul.py 中:包括 DT_HF8 输入在 A2/A3 架构下的限制、DT_HF8 输入不支持 TILEOP_NZ 格式、extend_params 中 trans_mode 在 A2/A3 架构下的限制,以及 scaled_mm 的 scale_a/scale_b 仅支持 ND 格式。

主要改动

  • 新增架构判断辅助函数: 在 python/pypto/op/matmul.py 中新增 A2A3_ARCHS = ("DAV_1001", "DAV_2201") 常量与 __is_a2a3_arch() 辅助函数,通过 pypto_impl.GetNPUArch() 正向列举 A2/A3 架构,避免无 NPU 环境(UNKNOWN 架构)误报。
  • __validate_inputs 新增 DT_HF8 约束检查: 新增 DT_HF8 输入在 A2/A3 架构下的拦截(仅 Ascend 950 支持),以及 DT_HF8 输入使用 TILEOP_NZ 格式的拦截,报错均沿用 0xF00002 错误码风格。
  • __validate_scaled_inputs 新增 scale 格式检查: 新增 scale_a/scale_b 使用 TILEOP_NZ 格式时抛出 "Scale tensor must use ND format" 的拦截。
  • __validate_trans_mode 新增架构检查: 当 extend_params 中 trans_mode 非 CAST_NONE 且当前为 A2/A3 架构时抛出 ValueError 拦截。
  • 新增约束检查测试: 新增测试文件 python/tests/ut/operation/test_matmul_constraint_check.py,覆盖 HF8 输入在 DAV_1001/DAV_2201 下被拦截、HF8 + NZ 输入被拦截、trans_mode 在 A2/A3 下被拦截及在 DAV_3510 下正常放行、scaled_mm 的 NZ scale 被拦截等 7 个用例。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 2 Comments:

代码审查

关闭总结

审查结果

1. python/pypto/op/matmul.py(+36 行) — 审查结论:无问题

  • A2A3_ARCHS / __is_a2a3_arch()(309-313 行):与 pypto.platform.npuarch 的 GetNPUArch()/SetNPUArch() 机制一致,架构字符串(DAV_1001/DAV_2201)与 conv.py 先例及现有测试(test_npuarch.py、pypto_pro/runtime/platform.py)吻合;UNKNOWN 架构下返回 False,不会误报。
  • DT_HF8 A2/A3 架构检查(427-430 行):分组优先级正确,符合文档约束 #2。
  • DT_HF8 + NZ 检查(431-440 行):全局生效,符合文档约束 #3,写法与既有 FP32/FP8E5M2+NZ 检查一致。
  • scale_a/scale_b NZ 检查(488-494 行):符合文档约束 #5,且置于维度校验之前,错误信息与测试断言匹配。
  • trans_mode A2/A3 检查(649-655 行):在 extend_params is not None 作用域内,短路由保证常见调用不额外付出 GetNPUArch() 开销;950(DAV_3510)下不触发,不影响既有行为,符合文档约束 #4。
  • scaled_mm 经 __validate_inputs 复用这些检查,行为一致且与文档不冲突。

2. python/tests/ut/operation/test_matmul_constraint_check.py(新增 +82 行) — 审查结论:1 个 P1 问题

  • P1(41 行,同缺陷还出现在 61/74 行):@npuarch("DAV_3510") 把 @contextmanager 返回的 _GeneratorContextManager 实例当作函数装饰器调用,模块导入阶段即抛 TypeError,整个测试文件无法被 pytest 收集,7 个用例全部失效,PR 声称的校验覆盖(HF8+NZ、trans_mode 在 950 放行、scaled_mm NZ scale)实际从未执行。

统计与总体风险

  • 按优先级:P0 = 0,P1 = 1,P2 = 0,P3 = 0。
  • 总体判断:生产代码改动正确实现了文档约束,无逻辑、安全或回归问题;风险集中在新增测试文件——装饰器误用导致该文件无法导入,既破坏 CI 收集,也使本次新加的约束检查失去测试验证。建议修复测试文件的装饰器用法后合入。
类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 2 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 2 Comments:

CLA Signature Pass

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

likedislike
205 messages are collapsed here。 See more
shi-dingkun
shi-dingkunCollaborators
Sep 7 Comments:

/check-pr

likedislike
CANN-robot
CANN-robotCollaborators
Sep 7 Comments:

The following labels are not ready.

lgtm: Please wait for committers to review the code.

approved: Please wait for committers to review the code.

likedislike
xinweiliu
xinweiliuCollaborators
Sep 7 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 7 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Sep 7 Merged pull request