已合并
增加aclnnBaddbmmToFmm拦截条件 #7390
wuyufei创建于 7月10日
增加aclnnBaddbmmToFmm拦截条件 #7390
已合并
wuyufei创建于 7月10日
wuyufei成员
7月10日

描述

关联的Issue

测试

文档更新

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:

AI/Agent生成声明

  • AI辅助编写
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 wuyufei 的贡献)
Wwuyufei成员
7月10日 创建了 pull request,commit 74ba6a98
atomgit-bot
atomgit-bot
7月10日 评论:

变更摘要

此变更为 checkFusedmm 函数新增了 shape 校验逻辑,用于在 aclnnBaddbmmToFmm 融合路径中增加拦截条件。通过比对 selfmat2bias 三个张量的维度形状,提前过滤掉不满足融合要求的输入,避免后续计算异常。

主要改动

  • 新增 bias 二维形状校验:当 biasShape 维度数为 2 时,校验 self 的倒数第二维(aM)和 mat2 的最后一维(bN)是否分别与 bias 的第 0 维和第 1 维匹配,不匹配则返回 false 拦截融合。
  • 新增 bias 非二维形状校验:当 biasShape 维度数不为 2 时,校验 bias 第 0 维是否为 1 或与 selfShape 第 0 维一致,同时校验 bias 第 1 维和第 2 维是否分别与 aMbN 匹配,不满足则返回 false 拦截融合。
likedislike
atomgit-bot
atomgit-bot
7月10日 评论:

代码审查

我已仔细审查了 matmul/common/op_host/op_api/batch_matmul_util.cpp 中新增的 shape 校验代码,以下是完整分析:

审查分析

逻辑分析

新增代码位于 checkFusedmm 函数(第 1318–1333 行),在 IsTransposeNonContiguous 检查通过后对 bias 的 shape 进行校验。

  1. aM 的计算(第 1322 行):selfShape.GetDim(selfShape.GetDimNum() - NUM_TWO)。由于 self 已被验证为 3 维(第 1298 行),GetDim(3-2) = GetDim(1) 即 M 维度。正确。

  2. bN 的计算(第 1323 行):mat2Shape.GetDim(mat2Shape.GetDimNum() - 1)mat2 为 3 维,且该文件第 489 行有注释明确说明「非连续场景 viewshape 一定是 bkn 格式」,因此 GetDim(2) 即为 N 维度。正确。

  3. 2D bias 校验(第 1324–1327 行):bias 形状为 [M, N],比对 aM == bias.GetDim(0)bN == bias.GetDim(1)。正确。

  4. 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

    逻辑正确,覆盖了广播和精确匹配两种场景。

与其他代码的一致性

  • 新增代码使用的维度索引约定(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

💬 仅评论

likedislike
CANN-robotCANN-robot成员
7月10日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
7月10日 评论:

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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
CANN-robotCANN-robot成员
7月10日 将crystalhu,yangyang016,fanqirui,chenqi317,liubo75,tangweiwei2,gitcode-chenjiao,gcw_kUomxQ2l,Andy-lb设为评审人
CANN-robotCANN-robot成员
7月10日 将gitcode-chenjiao,gcw_kUomxQ2l,Andy-lb设为审查人
atomgit-bot
atomgit-bot7月10日进行代码检视2
matmul/common/op_host/op_api/batch_matmul_util.cpp
@@ -1321,0 +1320,4 @@
1320+ const auto& mat2Shape = mat2->GetViewShape();
1321+ const auto& biasShape = bias->GetViewShape();
1322+ int64_t aM = selfShape.GetDim(selfShape.GetDimNum() - NUM_TWO);
1323+ int64_t bN = mat2Shape.GetDim(mat2Shape.GetDimNum() - 1);
atomgit-bot
atomgit-bot7月10日评论:

🟡 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)。

likedislike
wuyufei成员
7月11日 评论:
likedislike
Wwuyufei成员
7月11日 解决了最后一个问题
wuyufei成员
7月11日 评论:

compile

likedislike
Wwuyufei成员
7月11日 update merge request[project id: 7665709, iid: 7390, commit_id: 9c29c2d8bb756688ac8411b7a981141db52cc432] virtual merging success
CANN-robotCANN-robot成员
7月11日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月11日 评论:

流水线任务触发成功
任务链接 [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 >>>>>
likedislike
CANN-robot
CANN-robot成员
7月11日 评论:

流水线任务触发成功
任务链接 [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 >>>>>
likedislike
CANN-robotCANN-robot成员
7月11日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月11日 添加了label:ci-pipeline-failed
wuyufei成员
7月11日 评论:

compile

likedislike
CANN-robotCANN-robot成员
7月11日 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
7月11日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月11日 评论:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robot
CANN-robot成员
7月11日 评论:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robotCANN-robot成员
7月11日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月11日 添加了label:ci-pipeline-passed
liulun0308成员
7月11日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
7月11日 添加了label:approved
yangyang016成员
7月11日 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
7月11日 添加了label:lgtm
CANN-robotCANN-robot成员
7月11日 合入了pull request
CANN-robot
CANN-robot成员
7月11日 评论:

Pull Request 已合并或已关闭。

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

likedislike