已合并
fix: 修复 rmsnormquantv2/3 的 GM 内存越界问题 #7338
huanghaihong创建于 7月10日
fix: 修复 rmsnormquantv2/3 的 GM 内存越界问题 #7338
已合并
huanghaihong创建于 7月10日
huanghaihong
7月10日

描述

修复 rmsnormquantv2/3 中 GM 内存越界问题。

  • norm/rms_norm_quant_v2/op_kernel/arch35/rms_norm_quant_v2_regbase_full_load.h 中,CopyInOhters 函数通过 DataCopyPad 复制 gammascaleszeroPoints 时,原先使用了对齐后的长度(xGammaBetaAlignscalesAlignzeroPointsAlign)作为 blockLen。当实际数据元素数 numR 小于对齐长度时,会读取超出 GM 实际数据范围的内存,造成越界。本次改动将 gammablockLen 改为 numR * sizeof(T_X),并将 scaleszeroPoints 改为根据 numQ 分支:当 numQ == 1 时按单个元素复制,否则按 numR * sizeof(...) 复制,确保仅复制有效数据。
  • norm/rms_norm_quant_v3/op_api/aclnn_rms_norm_quant_v3.cpp 中移除未使用的头文件 op_api/op_api_def.h,避免冗余依赖。

关联的Issue

  • #4027

测试

  • 本地算子编译通过
  • 相关精度/功能测试验证通过

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 huanghaihong 的贡献)
Hhuanghaihong
7月10日 创建了 pull request,commit 4a2e60ca
atomgit-bot
atomgit-bot
7月10日 评论:

变更摘要

该 PR 主要修复 rms_norm_quant_v2 内核在 arch35 架构下因全局内存(GM)复制时使用对齐尺寸(xGammaBetaAlignscalesAlignzeroPointsAlign)而非实际元素数量计算 blockLen,导致 DataCopyPad 读取越界的问题。同时顺带清理了 rms_norm_quant_v3 中一个多余的 #include

主要改动

  • rms_norm_quant_v2_regbase_full_load.h 中 gamma 复制长度修正:将 copyInParamsGamma.blockLenxGammaBetaAlign * sizeof(T_X) 改为 numR * sizeof(T_X),避免因对齐值大于实际行数 numR 导致从 gamma 全局内存读取越界。
  • rms_norm_quant_v2_regbase_full_load.h 中 scales 复制长度修正:引入 numQ == 1 条件分支,当量化组数为 1 时 blockLen 设为 sizeof(T_SCALES)(仅一个元素),否则使用 numR * sizeof(T_SCALES),替代原先可能偏大的 scalesAlign * sizeof(T_SCALES)
  • rms_norm_quant_v2_regbase_full_load.h 中 zeroPoints 复制长度修正:同样引入 numQ == 1 条件分支逻辑,替换原先 zeroPointsAlign * sizeof(T_ZEROPOINTS) 的计算方式,防止 zeroPoints 全局内存越界读取。
  • aclnn_rms_norm_quant_v3.cpp 移除冗余头文件:删除了 #include "op_api/op_api_def.h",属于无关的代码清理。
likedislike
atomgit-bot
atomgit-bot
7月10日 评论:

代码审查

审查总结

审查结果

文件 结果
norm/rms_norm_quant_v2/op_kernel/arch35/rms_norm_quant_v2_regbase_full_load.h 2 个 P0 发现
norm/rms_norm_quant_v3/op_api/aclnn_rms_norm_quant_v3.cpp 无问题

发现统计

  • P0: 2 个
  • P1: 0 个
  • P2: 0 个
  • P3: 0 个

总体风险评估

高风险rms_norm_quant_v2_regbase_full_load.h 中第 387–391 行的复制粘贴错误是一个严重的正确性缺陷:zeroPoints 段的 blockLen 被错误地赋值到了 copyInParamsScales.blockLen(且使用了错误的类型 T_SCALES),导致:

  1. copyInParamszeroPoints.blockLen 保持未初始化状态,当 hasZeroPoints1hasZeroPoints2 为 true 时,DataCopyPad 使用栈上的脏数据作为拷贝长度,必然引发 GM 越界读写;
  2. 同时 copyInParamsScales.blockLen 被意外覆盖,如果 hasScales2 也为 true,scales2 的拷贝也会使用错误的数据量。

此缺陷与 PR 标题「fix rmsnormquantv2/3 gm memory out-of-bounds」的目标直接矛盾——它引入了一个新的、确定性的 GM 越界问题。建议在合入前必须修复。

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

⛔ 需要修改

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
norm 苏跃明, 任如海 (2/2) 苏跃明 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
7月10日 将kevin_huang1234,crystalhu,yangyang016,fanqirui,renruhai,zhajianqing123,chenqi317,fullt,wang-xing001,liubo75,tangweiwei2,liujie12345678,Chen_HaoWen,wangyongguang,zhang-wu设为评审人
CANN-robotCANN-robot成员
7月10日 将kevin_huang1234,crystalhu,renruhai,zhajianqing123,chenqi317,fullt,wang-xing001,liubo75,tangweiwei2,liujie12345678,Chen_HaoWen,wangyongguang,zhang-wu设为审查人
atomgit-bot
atomgit-bot7月10日进行代码检视1
norm/rms_norm_quant_v2/op_kernel/arch35/rms_norm_quant_v2_regbase_full_load.h
@@ -387,0 +388,4 @@
388+ copyInParamsScales.blockLen = sizeof(T_SCALES);
389+ } else {
390+ copyInParamsScales.blockLen = numR * sizeof(T_SCALES);
391+ }
atomgit-bot
atomgit-bot7月10日评论:

🔴 Critical

第 385 行声明 DataCopyExtParams copyInParamszeroPoints;,随后第 386 行仅设置了 blockCount = 1blockLen 字段从未被赋值。

由于第 387–391 行的复制粘贴错误将 blockLen 错误地写到了 copyInParamsScales.blockLen(参见上一条 P0 报告),copyInParamszeroPoints.blockLen 实际保持未初始化状态。

第 392–393 行设置了 srcStridedstStride,但 blockLen 仍然是栈上的脏数据。

随后在第 406 行(hasZeroPoints1 路径)和第 413 行(hasZeroPoints2 路径),DataCopyPad 使用这个未初始化的 copyInParamszeroPoints 进行 GM→UB 数据搬移,导致:

  • 拷贝数据量不可预测(栈上的随机值)
  • 可能从 GM 越界读取,触发内存访问错误
  • 可能向 UB 越界写入,破坏其他 LocalTensor 数据

触发条件hasZeroPoints1 == truehasZeroPoints2 == true 时必然触发。

建议:与上一条合并修复:将第 387–391 行的赋值目标从 copyInParamsScales 改为 copyInParamszeroPoints,类型从 T_SCALES 改为 T_ZEROPOINTS。修复后 copyInParamszeroPoints.blockLen 即被正确初始化。

改动建议
391
+ if (numQ == 1) {
392
+ copyInParamszeroPoints.blockLen = sizeof(T_ZEROPOINTS);
393
+ } else {
394
+ copyInParamszeroPoints.blockLen = numR * sizeof(T_ZEROPOINTS);
391
- }
395
+ }
应用建议
likedislike
atomgit-bot
atomgit-bot7月10日进行代码检视1
norm/rms_norm_quant_v2/op_kernel/arch35/rms_norm_quant_v2_regbase_full_load.h
@@ -387,0 +388,4 @@
388+ copyInParamsScales.blockLen = sizeof(T_SCALES);
389+ } else {
390+ copyInParamsScales.blockLen = numR * sizeof(T_SCALES);
391+ }
atomgit-bot
atomgit-bot7月10日评论:

🔴 Critical

CopyInOhters 函数中,第 387–391 行(zeroPoints 段)的 blockLen 赋值存在明显的复制粘贴错误:

这段代码是从第 370–374 行(scales 段)复制过来但未修改目标变量和类型。它导致了两个独立的缺陷:

  1. copyInParamszeroPoints.blockLen 未初始化:第 385 行声明 DataCopyExtParams copyInParamszeroPoints 后仅设置了 blockCount=1blockLen 从未被赋值,保持栈上的脏数据。第 406 行、413 行调用 DataCopyPad(... copyInParamszeroPoints ...) 时使用未初始化的 blockLen,导致未定义行为——可能拷贝错误数量的数据或越界。

  2. copyInParamsScales.blockLen 被意外覆盖:如果 hasScales2 为 true(第 395 行),第 399 行会复用 copyInParamsScales 做 scales2 拷贝,此时 blockLen 已被覆盖为 zeroPoints 段错误写入的值(sizeof(T_SCALES)numR * sizeof(T_SCALES),但语义上应是 zeroPoints 的 size),导致 scales2 拷贝数据量可能不正确。

触发条件:当 hasZeroPoints1hasZeroPoints2 为 true 时触发未初始化 blockLen 的问题;当 hasScales2 && (hasZeroPoints1 || hasZeroPoints2) 时还会触发 scales2 拷贝错误。

建议:将第 388 行的 copyInParamsScales 改为 copyInParamszeroPoints,将 T_SCALES 改为 T_ZEROPOINTS;第 390 行同理。修正后的代码如上。

改动建议
391
+ if (numQ == 1) {
392
+ copyInParamszeroPoints.blockLen = sizeof(T_ZEROPOINTS);
393
+ } else {
394
+ copyInParamszeroPoints.blockLen = numR * sizeof(T_ZEROPOINTS);
391
- }
395
+ }
应用建议
likedislike
Hhuanghaihong
7月10日 update merge request[project id: 7665709, iid: 7338, commit_id: 99f922385c83f43c343bf04ba990a6abbccf9f13] virtual merging success
此处折叠了6条事件消息 查看更多
Hhuanghaihong
7月10日 修改了pull request 的描述
huanghaihong
7月10日 评论:

compile

likedislike
Hhuanghaihong
7月10日 update merge request[project id: 7665709, iid: 7338, commit_id: 45b1d921552c3b0b469ff9427e6447a5ad62294b] virtual merging success
CANN-robotCANN-robot成员
7月10日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月10日 评论:

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

任务名称状态日志下载链接
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
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_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_classify ✅ 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 >>>>>
API_Check ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_report_lcov ✅ SUCCESS >>>>>

[2026-07-10 17:18:54]    CI执行结束

likedislike
刘琦刘琦成员
7月10日 解决了最后一个问题
CANN-robot
CANN-robot成员
7月10日 评论:

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

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>

[2026-07-10 17:02:58]    CI执行结束

likedislike
CANN-robotCANN-robot成员
7月10日 添加了label:api-check-pass
CANN-robotCANN-robot成员
7月10日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月10日 添加了label:ci-pipeline-passed
su-yueming成员
7月11日 评论:

/lgtm
/approve

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

/lgtm

likedislike
CANN-robotCANN-robot成员
7月11日 添加了label:lgtm
CANN-robotCANN-robot成员
7月11日 关闭了关联的issue
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
liulun0308成员
7月11日 评论:

/approve

likedislike