Pull Request已成功合入, 合并人@CANN-robot
(感谢 wuyufei 的贡献)变更摘要
此变更为 checkFusedmm 函数新增了 shape 校验逻辑,用于在 aclnnBaddbmmToFmm 融合路径中增加拦截条件。通过比对 self、mat2 和 bias 三个张量的维度形状,提前过滤掉不满足融合要求的输入,避免后续计算异常。
主要改动
- 新增
bias二维形状校验:当biasShape维度数为 2 时,校验self的倒数第二维(aM)和mat2的最后一维(bN)是否分别与bias的第 0 维和第 1 维匹配,不匹配则返回false拦截融合。 - 新增
bias非二维形状校验:当biasShape维度数不为 2 时,校验bias第 0 维是否为 1 或与selfShape第 0 维一致,同时校验bias第 1 维和第 2 维是否分别与aM和bN匹配,不满足则返回false拦截融合。


代码审查
我已仔细审查了 matmul/common/op_host/op_api/batch_matmul_util.cpp 中新增的 shape 校验代码,以下是完整分析:
审查分析
逻辑分析
新增代码位于 checkFusedmm 函数(第 1318–1333 行),在 IsTransposeNonContiguous 检查通过后对 bias 的 shape 进行校验。
-
aM的计算(第 1322 行):selfShape.GetDim(selfShape.GetDimNum() - NUM_TWO)。由于self已被验证为 3 维(第 1298 行),GetDim(3-2) = GetDim(1)即 M 维度。正确。 -
bN的计算(第 1323 行):mat2Shape.GetDim(mat2Shape.GetDimNum() - 1)。mat2为 3 维,且该文件第 489 行有注释明确说明「非连续场景 viewshape 一定是 bkn 格式」,因此GetDim(2)即为 N 维度。正确。 -
2D bias 校验(第 1324–1327 行):bias 形状为
[M, N],比对aM == bias.GetDim(0)和bN == bias.GetDim(1)。正确。 -
3D bias 校验(第 1328–1333 行):bias 形状为
[batch_b, M, N]:- batch 维度必须为 1(可广播)或与
self的 batch 维度一致 - M 维度比对
aM == bias.GetDim(1) - N 维度比对
bN == bias.GetDim(2)(NUM_TWO=2)
逻辑正确,覆盖了广播和精确匹配两种场景。
- batch 维度必须为 1(可广播)或与
与其他代码的一致性
- 新增代码使用的维度索引约定(M 在
dimNum-2,N 在dimNum-1)与同文件中CheckTransNonContiguousShapeSupport(第 487–489 行)等多处完全一致。 const auto&引用临时对象在 C++ 中有生命周期延长保证,安全无 dangling 风险。- 未引入新的内存分配、资源管理或线程安全问题。
边界情况
- 空 tensor 已在第 1284 行被拦截(
IsEmpty),不会到达此处。 - 维度为 0 时 shape 比较自然失败(
aM != 0),行为正确。 - 没有引入整数溢出风险(仅作比较运算)。
审查结论
该 diff 未引入任何正确性、安全性、可靠性或性能问题。 新增的 shape 校验逻辑正确、与现有代码约定一致,边界情况处理得当。
| 文件 | 审查结果 |
|---|---|
matmul/common/op_host/op_api/batch_matmul_util.cpp |
无问题 |
- P0: 0
- P1: 0
- P2: 0
- P3: 0
整体风险评估:低风险,变更安全。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


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 |
|---|---|---|
| ** | ✅ 杨阳, gcw_kUomxQ2l (2/2) | ✅ gcw_kUomxQ2l (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
wuyufei, thanks for your pull request. All authors of the commits have signed the CLA. 👍


🟡 Medium Priority
第 1323 行,bN 被定义为 mat2Shape.GetDim(mat2Shape.GetDimNum() - 1),即始终取 mat2 的最后一维。但根据第 1314-1316 行的注释和逻辑,mat2 支持两种形式:(n,b,k) 和 (k,b,n)。当 isNeedSwapInnerTwoDim = true 时,mat2 的原始 shape 为 [B, N, K](最后两维需要交换),此时语义上的 N 维度位于倒数第二维(GetDimNum() - NUM_TWO),而非最后一维(GetDimNum() - 1)。
ExecFusedmmOp 函数(第 1346-1348 行)正是通过 SwapLastTwoDimValue 将最后两维交换来得到正确的 [B, K, N] 形式。但 checkFusedmm 中的 shape 校验未考虑这一点,导致当 isNeedSwapInnerTwoDim = true 时,bN 实际取到的是 K 维度,与 bias 的 N 维度比较会失败,从而错误地拒绝合法的配置(false negative)。
对于 isNeedSwapInnerTwoDim = false 的情况(mat2 为 [B, K, N]),最后一维恰好是 N,此时 bN 取值正确。
建议:bN 应根据 isNeedSwapInnerTwoDim 决定从哪个维度读取:当需要交换内部两维时,N 在倒数第二维(GetDimNum() - NUM_TWO);否则 N 在最后一维(GetDimNum() - 1)。


流水线任务触发成功
任务链接 [58a9a7265ee5427591cdb686cd4ee856][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86_mobile_station | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Pre | ❌ FAILED | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_harmony-infer-chs-nn | ✅ SUCCESS | >>>>> |


流水线任务触发成功
任务链接 [628538da5c0d4bc4bfa100c531ddb32e][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ❌ FAILED | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_checkpr | ✅ SUCCESS | ||
| StaticCheck_codespell_check | ✅ SUCCESS | ||
| StaticCheck_link_validity_check | ✅ SUCCESS | ||
| StaticCheck_resource_existence_check | ✅ SUCCESS | ||
| StaticCheck_tag_closed_check | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_style | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ⚠️ WARNING | >>>>> | >>>>> |
| SCA | ✅ SUCCESS | >>>>> |


流水线任务触发成功
任务链接 [ea6f2ea600954faa86e5e8f956abde7f][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86_mobile_station | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Pre | ✅ SUCCESS | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_harmony-infer-chs-nn | ✅ SUCCESS | >>>>> | |
| UT_Test_ophost | ✅ SUCCESS | ||
| UT_Test_opapi | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| UT_Test_opgraph | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> |
[2026-07-11 11:18:26] CI执行结束


流水线任务触发成功
任务链接 [086048b32b164496ad8d5b768feba16e][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_checkpr | ✅ SUCCESS | ||
| StaticCheck_codespell_check | ✅ SUCCESS | ||
| StaticCheck_link_validity_check | ✅ SUCCESS | ||
| StaticCheck_resource_existence_check | ✅ SUCCESS | ||
| StaticCheck_tag_closed_check | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_style | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ⚠️ WARNING | >>>>> | >>>>> |
| SCA | ✅ SUCCESS | >>>>> |
[2026-07-11 11:09:48] CI执行结束


/approve


/lgtm


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
关联的Issue
测试
文档更新
类型标签
AI/Agent生成声明