已开启
add ut,examples && fix kernel bug #223
zhuoyunhang创建于 6 天前
add ut,examples && fix kernel bug #223
已开启
zhuoyunhang创建于 6 天前
zhuoyunhang成员
6 天前

描述

关联的Issue

测试

文档更新

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:
likedislike
合并受阻
Zzhuoyunhang成员
6 天前 创建了 pull request,commit eb67dcbd
Zzhuoyunhang成员
6 天前 关联了issue:[Bug] kernel_qbmm_mx_activation_quant.h: CV 融合 Kernel 异常路径死锁 + 硬编码 + 嵌套过深
atomgit-bot
atomgit-bot
6 天前 评论:

变更摘要

本 PR 主要新增了 quant_matmul_activation_quant 的示例代码和单元测试,同时对内核中的 Epilogue、Scheduler 及 Kernel 模块进行了多处 Bug 修复。新增示例演示了 MX 量化矩阵乘法 → Gelu 激活 → 动态 MX 量化的完整融合流水线(AIC 做 MX GEMM,AIV 做 Gelu + 量化),修复涉及命名空间显式化、API 调用更新(LoadAlign/StoreUnAlignPostDataCopy/DataCopyUnAlignPost)、Erf 函数签名适配、空 Block 时的同步通知以及 scaleN 计算公式修正等。

主要改动

  • 新增 quant_matmul_activation_quant 示例:添加了完整的 C++ 示例(quant_matmul_activation_quant.cpp)、运行脚本(run.sh)、CSV 批量执行器(parse_csv.py)、数据生成(gen_data.py)和结果验证(verify_result.py)脚本,展示 MX 量化 matmul + Gelu + 动态 MX 量化的端到端流程。

  • 新增 quant_matmul_activation_quant 单元测试:在 tests/ut/op_kernel/quant_matmul_activation_quant/ 下添加了 UT 包装头文件和测试文件(test_quant_matmul_activation_quant.cpp),覆盖 DoubleBuffer、AFullLoad、TripleBuffer、QuadBuffer 四种配置,验证内核执行的正确性。

  • 修复 BlockEpilogueGeluMxQuant 中命名空间与 API:移除 using namespace Gemm; 并显式添加 Gemm:: 前缀到 Get/CeilDiv/CeilAlign/Align32 等调用;将 LoadAlign 替换为 DataCopyStoreUnAlignPost 替换为 DataCopyUnAlignPost,适配 AscendC API 变更。

  • 修复 Erf 调用签名与 Gelu 计算逻辑:在 GeluTanh 方法中新增 AscendC::ErfConfig 常量并传入 Erf 调用,指定 SUBSECTION_POLYNOMIAL_APPROXIMATION 算法;将 Duplicate 清零操作调整到 nAligned 计算之后并使用 mSize * nAligned 而非固定常量。

  • 修复 BlockScheduler 与 Kernel 中的 NSplit 对齐和空 Block 处理:在 BlockSchedulerQuantBatchMatmulV3::GetBlockShape 中新增 EnableNSplitAlign32_ 模板参数支持 32 字节对齐;在 ProcessSingleBatch 中启用该对齐并在 baseM/baseN 为 0 时分别向 AIC/AIV 发送 NotifyVector/NotifyCube 通知,同时修正 AddBatchOffset 中 scaleN 的计算公式。

likedislike
atomgit-bot
atomgit-bot
6 天前 评论:

代码审查

审查结束——关闭总结

审查文件清单(15/15)

文件 结论
examples/CMakeLists.txt 无问题(仅新增一行 add_subdirectory
examples/quant_matmul_activation_quant/CMakeLists.txt 无问题(新增子目录 CMake)
examples/quant_matmul_activation_quant/quant_matmul_activation_quant/CMakeLists.txt 无问题(新增可执行目标)
examples/quant_matmul_activation_quant/quant_matmul_activation_quant/parse_csv.py 无问题
examples/quant_matmul_activation_quant/quant_matmul_activation_quant/quant_matmul_activation_quant.cpp P2:yScale 缓冲区大小计算不一致;P3:未使用常量
examples/quant_matmul_activation_quant/quant_matmul_activation_quant/quant_matmul_activation_quant.csv 无问题(数据文件)
examples/quant_matmul_activation_quant/quant_matmul_activation_quant/run.sh 无问题
examples/quant_matmul_activation_quant/scripts/gen_data.py 无问题
examples/quant_matmul_activation_quant/scripts/verify_result.py 无问题
include/blaze/epilogue/block/block_epilogue_gelu_mx_quant.h 无问题(API 重命名/命名空间清理/精度改进,均为合理变更)
include/blaze/gemm/block/block_scheduler_qbmm.h 无问题(新增模板参数兼容旧调用)
include/blaze/gemm/kernel/kernel_qbmm_mx_activation_quant.h 无问题(死锁修复 + scaleN 公式修正,均为正确修复)
tests/ut/op_kernel/quant_matmul_activation_quant/CMakeLists.txt 无问题
tests/ut/op_kernel/quant_matmul_activation_quant/quant_matmul_activation_quant.h 无问题
tests/ut/op_kernel/quant_matmul_activation_quant/test_quant_matmul_activation_quant.cpp P2:yScale 缓冲区大小计算不一致

统计

  • P0:0
  • P1:0
  • P2:2(yScale 缓冲区大小计算与 kernel 内部 layout 不一致,分别在示例代码和测试代码中)
  • P3:1(未使用的常量 MX_IDENTITY_SCALE

整体风险评估

中低风险。核心 kernel 修复(死锁修复、scaleN 公式修正、命名空间清理)均是正确且必要的变更。唯一的实际问题是 yScale 输出缓冲区大小在示例和测试代码中少算了 CeilAlign 的对齐量——当前测试配置不会触发,但新增特定 N 值的用例时会越界写 GM。建议在上线前修复该缓冲区大小计算。

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

💬 仅评论

likedislike
CANN-robotCANN-robot成员
6 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
6 天前 评论:

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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/ops-tensor ❌ (0/2)(You can also ask: 宋恺, 陈琦, 刘波, 陈康, 胡碧霞) ❌ (0/1)(You can also ask: 林鹏翔, 唐玮玮, 陈康, 胡碧霞, 张磊)

💡 Tip:

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

CLA Signature Pass

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

likedislike
zhuoyunhang成员
6 天前 评论:

/compile

likedislike
CANN-robotCANN-robot成员
6 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
6 天前 评论:

流水线任务触发成功
任务链接 [f939e6611d864faab95f1ed5e60e96a5][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
Check_Pr ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
codecheck_codestyle ⚠️ WARNING >>>>>
precommit ❌ FAILED >>>>> >>>>>
UT_Test ❌ FAILED >>>>>
likedislike
CANN-robotCANN-robot成员
6 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
6 天前 添加了label:ci-pipeline-failed
atomgit-bot
atomgit-bot6 天前进行代码检视1
examples/quant_matmul_activation_quant/quant_matmul_activation_quant/quant_matmul_activation_quant.cpp
@@ -0,0 +250,4 @@
250+ const size_t x2ScaleSize = static_cast<size_t>(config.n) * scaleK;
251+ const size_t ySize = static_cast<size_t>(config.m * config.n);
252+ const size_t yScaleSize = static_cast<size_t>(config.m) *
253+ AlignUp(static_cast<uint64_t>(config.n), 32U) / 32U;
atomgit-bot
atomgit-bot6 天前评论:

🟡 Medium Priority

变更行:第 252-253 行使用 AlignUp(config.n, 32U) / 32U 计算 yScaleSize,等价于 ceil(n/32)

但 epilogue 内部 scaleNAlign_ = CeilAlign(CeilDiv(N, 32), 2),当 ceil(N/32) 为奇数时多出一个对齐元素。当前 CSV 用例 N=128(ceilDiv=4,偶数)不会触发,但如果 CSV 中增加 N=32 等用例,DeviceBuffer yScale 分配的空间将不足以容纳 kernel 写入的 scale 数据,导致越界写入相邻 GM 内存。

修复方向:将 yScaleSize 改为 AlignUp(AlignUp(n, 32U) / 32U, 2U)CeilDiv(n, 64U) * 2U

建议:将 yScaleSize 计算改为 AlignUp(AlignUp(n, 32U) / 32U, 2U)CeilDiv(n, 64U) * 2U,与 kernel 内部的 scaleNAlign_ 保持一致。

likedislike
atomgit-bot
atomgit-bot6 天前进行代码检视1
examples/quant_matmul_activation_quant/quant_matmul_activation_quant/run.sh
@@ -0,0 +441,4 @@
441+ EXAMPLES="$TARGET"
442+ MODE="single"
443+ else
444+ EXAMPLES="$(discover_examples)"
atomgit-bot
atomgit-bot6 天前评论:

🟡 Medium Priority

变更行 444:EXAMPLES="$(discover_examples)" 通过命令替换调用 discover_examples。当 discover_examples 内部因未注册任何示例而执行 exit 1(第 219 行)时,该 exit 仅终止子 shell,命令替换的退出码不会被 set -e 捕获——因为赋值语句本身的退出码为 0。结果是 EXAMPLES 为空字符串,脚本打印 "Mode: multi" 后 for example in $EXAMPLES 零次迭代,all_passed 保持 true,最终以退出码 0 成功退出,完全掩盖了错误。触发条件:未指定 --target 且未指定 --case,且 CMakeLists.txt 中无注册示例。

建议:在命令替换之后检查 EXAMPLES 是否为空,为空时打印错误并退出;或者将 discover_examples 的 exit 改为 return 1,由调用方检查函数返回值。

likedislike
atomgit-bot
atomgit-bot6 天前进行代码检视2
tests/ut/op_kernel/quant_matmul_activation_quant/test_quant_matmul_activation_quant.cpp
@@ -0,0 +163,4 @@
163+ const size_t biasSize = static_cast<size_t>(cfg.N) * sizeof(BiasType);
164+ const size_t ySize = GetMxInputSize<OutType>(cfg.M * cfg.N);
165+ const size_t yScaleSize = static_cast<size_t>(cfg.M) * Blaze::Gemm::CeilDiv(static_cast<int64_t>(cfg.N),
166+ static_cast<int64_t>(32)) * sizeof(AscendC::fp8_e8m0_t);
atomgit-bot
atomgit-bot6 天前评论:

🟡 Medium Priority

变更行:test_quant_matmul_activation_quant.cpp 第 165-166 行使用 CeilDiv(cfg.N, 32) 计算 yScaleSizequant_matmul_activation_quant.cpp 第 252-253 行使用 AlignUp(n, 32) / 32

但 epilogue (block_epilogue_gelu_mx_quant.h) 在 UpdateNextProblem 中计算的内部步长为 scaleNAlign_ = CeilAlign(CeilDiv(N, 32), 2),即当 ceil(N/32) 为奇数时向上取整到偶数。CopyScaleFromUb2Gm 以此为跨行 stride 写入 GM。

触发条件:当 N 使得 ceil(N/32) 为奇数时(如 N=32→1→2, N=96→3→4),epilogue 每行实际写入 scaleNAlign_ 个元素,但分配缓冲区大小仅为 M * CeilDiv(N, 32),少分配了 M * (scaleNAlign_ - CeilDiv(N, 32)) 个元素 → 最后一行写入时越界,破坏相邻 GM 内存。

当前测试用例全部使用 N=128(ceilDiv=4,偶数)不会触发,但新增 N=32/96/160 等用例时会触发。

修复方向:将 yScaleSize 的计算改为 M * CeilAlign(CeilDiv(N, 32), 2) 或等价的 M * CeilDiv(N, 64) * 2,与 kernel 内部 scaleNAlign_ 一致。

建议:将 yScaleSize 计算改为 M * CeilAlign(CeilDiv(N, 32), 2)。同时在 quant_matmul_activation_quant.cpp 第 252-253 行做同样的修复。

likedislike
zhuoyunhang成员
3 天前 评论:
likedislike
CANN-robotCANN-robot成员
3 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
3 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
3 天前 评论:

流水线任务触发成功
任务链接 [b75d7e0b6ede45dc9edc8fac3cbcb9be][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
Check_Pr ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
codecheck_codestyle ⚠️ WARNING >>>>>
precommit ❌ FAILED >>>>> >>>>>
UT_Test ❌ FAILED >>>>>
likedislike
CANN-robotCANN-robot成员
3 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
3 天前 添加了label:ci-pipeline-failed