Merged
feat(layer_norm): reuse LayerNormV3 two-pass perf template #13207
rkAdd on 22 days ago
feat(layer_norm): reuse LayerNormV3 two-pass perf template #13207
Merged
rkAdd on 22 days ago
rk
rkCollaborators
22 days ago

描述

为 LayerNorm(V1)接入 LayerNormV3 已有的二趟高性能(two-pass perf)tiling/kernel 模板,使 V1 在满足条件的 shape 上复用同一套寄存器级实现;并通过模板参数区分 V1/V3 第三个输出的语义(V1 输出 variance,V3 输出 rstd),保证共用 kernel 时结果正确。

改动原因

  • LayerNormV3 已实现 two-pass perf 模板(tiling key 500/510/511/520/522),但此前只注册给 LayerNormV3(REGISTER_OPS_TILING_TEMPLATE(LayerNormV3, ..., 150))。LayerNorm(V1)在同 shape 下只能命中 two-pass 模板(priority 200)等路径,未复用该高性能实现。
  • V1 的第三个输出是 variance,V3 是 rstd。原 perf kernel 在输出缓冲区上原地把 variance 转成 rstd,直接复用于 V1 会导致第三输出错误,因此需要按算子区分输出语义。

改动方法

  1. kernel 输出语义参数化:LayerNormV3RegbaseTwoPassPerf 与 RegbaseTwoPassPerfImpl 新增 bool IsOutRstd 模板参数;layer_norm_impl 将该参数透传到五个 two-pass perf tiling key 分支(V3 传 true、V1 传 false)。
  2. V1(IsOutRstd=false)路径:
    • 输出缓冲区保留原始 variance 作为 lastout,新增 rstdBuf_ 存放 rstd;
    • CalculateRstdVF 增加 variance 入参,改为 ComputeRstdNewtonRaphson(variance, rstd) 非原地计算;y 归一化使用 rstd,第三输出写 variance;
    • rstdGm_/rstdQueue_/rstdOutUb_ 更名为 lastout*,CopyOutMeanRstd/CastMeanRstd 更名为 CopyOutMeanLastout/CastMeanLastout。
  3. tiling 注册与 UB 预算:
    • 新增 REGISTER_OPS_TILING_TEMPLATE(LayerNorm, LayerNormV3RegBaseTwoPassPerfTiling, 150);
    • CanFitInBuffer 在 commonParams.isV1 时额外扣除 curAAlign * sizeof(float) 的 rstd 缓冲区,避免 UB 超限。
  4. UT:LayerNorm tiling UT 重命名为 layer_norm_tiling_regbase_two_pass_perf,并启用 tiling key 断言((64,4096) FP32 → key 500)。

涉及文件(4 个,+53 -40):

  • norm/layer_norm/tests/ut/op_host/arch35/test_layer_norm_tiling.cpp(+2 -2)
  • norm/layer_norm_v3/op_host/arch35/layer_norm_v3_regbase_two_pass_perf_tiling.cpp(+6 -2)
  • norm/layer_norm_v3/op_kernel/arch35/layer_norm_v3_two_pass_perf.h(+36 -27)
  • norm/layer_norm_v3/op_kernel/layer_norm_v3_apt.h(+9 -9)

关联的Issue

测试

  • UT:layer_norm_tiling_regbase_two_pass_perf 覆盖 (64,4096) FP32,校验命中 two-pass perf 模板(tiling key=500)。
  • 回归关注点:V1 第三输出 variance、V3 第三输出 rstd 以及 y 的数值一致性,由现有 LayerNorm/LayerNormV3 ST 用例覆盖。

文档更新

不涉及

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for rk's contribution.)
rkrkCollaborators
22 days ago Created pull request, commit 8e3ee785
atomgit-bot
atomgit-bot
22 days ago Comments:

变更摘要

本 PR 将 LayerNormV3 的 two-pass 性能模板复用给 LayerNorm 算子,并在该模板中引入 IsOutRstd 编译期开关,使同一套内核既能输出 rstd,也能输出方差(variance)。配套调整了 UB 空间占用估算和算子注册,并补充了对应的 host 侧 tiling 单测与 kernel 侧精度单测。

主要改动

  • 模板增加 IsOutRstd 参数: LayerNormV3RegbaseTwoPassPerf 与 RegbaseTwoPassPerfImpl 新增 bool IsOutRstd 模板参数,layer_norm_v3_apt.h 中 FLOAT/FLOAT、HALF/FLOAT、HALF/HALF、BF16/FLOAT、BF16/BF16 五个 tiling key 分支均透传该参数。
  • 输出语义由 rstd 泛化为 lastout: 内核内 rstdGm_、rstdQueue_、rstdOutUb_、CopyOutMeanRstd、CastMeanRstd 等统一改名为 lastoutGm_、lastoutQueue_、lastoutOutUb_、CopyOutMeanLastout、CastMeanLastout,最后一个输出根据 IsOutRstd 承载 rstd 或方差。
  • 新增独立 rstd 缓冲: 当 IsOutRstd == false 时额外 InitBuffer 出 rstdBuf_,CalculateRstdVF 改为接收 varianceUb 输入与 rstdOutUb 输出两个地址,先用方差缓冲区计算 rstd 再写入独立缓冲,从而把原始方差保留在 lastout 输出中。
  • tiling 容量判断与算子注册调整: CanFitInBuffer 将输出缓冲按字节计算(sizeof(float)),并在 commonParams.isV1 时额外计入一份 curAAlign * sizeof(float);同时新增 REGISTER_OPS_TILING_TEMPLATE(LayerNorm, LayerNormV3RegBaseTwoPassPerfTiling, 150),使 LayerNorm 复用该 tiling 模板。
  • 测试覆盖更新: host 侧 UT 用例更名为 layer_norm_tiling_regbase_two_pass_perf 并校验 tiling key 为 500;kernel 侧 UT 新增 RunTwoPassPerf 内核入口与 CheckPerfOutputs 校验函数,针对多组 columns 分别验证方差模式与 rstd 模式下 mean、lastout 和 y 的计算结果。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
22 days ago Comments:

代码审查

检视结论

按文件的检视结果

文件 结论
norm/layer_norm/tests/ut/op_host/arch35/test_layer_norm_tiling.cpp 无问题。用例重命名与 ASSERT_EQ(tiling_key, 500) 已核对:LayerNormV3TilingKey::LAYER_NORM_REGBASE_TWO_PASS_PERF_FLOAT32_FLOAT32 = 500,且 perf 模板优先级 150 高于 two_pass 200、IsCapable() 对该 [64,4096]/float 形状成立,断言值正确。
norm/layer_norm/tests/ut/op_kernel/test_layer_norm_apt.cpp 2 个问题(见下),均为测试侧。
norm/layer_norm_v3/op_host/arch35/layer_norm_v3_regbase_two_pass_perf_tiling.cpp 无问题。outputBufferSize 用字节数与 ubCanUseSize 单位一致,且与 kernel 侧 meanQueue_/lastoutQueue_(2×DOUBLE_BUFFER)及 V1 额外 rstdBuf_ 一一对应(isV1 = (opType == "LayerNorm") 与 kernel IsOutRstd=false 语义一致);REGISTER_OPS_TILING_TEMPLATE(LayerNorm, ...) 与 welford/two_pass/no_reduce 等文件的双注册惯例一致。
norm/layer_norm_v3/op_kernel/arch35/layer_norm_v3_two_pass_perf.h 无问题。!IsOutRstd 时方差保留在 lastoutOutUb_、rstd 改写入 rstdBuf_,CalculateNormalizeVF 使用 rstd,CalculateRstdVF(variance, rstd) 实参顺序正确;rstdGm_/rstdQueue_/rstdOutUb_/CastMeanRstd/CopyOutMeanRstd 的重命名在文件内无残留旧名。
norm/layer_norm_v3/op_kernel/layer_norm_v3_apt.h 无问题。RegbaseTwoPassPerfImpl 新增 IsOutRstd 模板参数后,500/510/511/520/522 五个调用点全部同步更新,无遗漏调用。

问题计数

  • P0:0
  • P1:0
  • P2:1(新增 UT 的 block 数与同文件既有 (blockNum+1)/2 换算约定不一致,可能导致 tailA 为负、越界 DataCopy 或行未处理而断言失败)
  • P3:1(新增 UT 未覆盖 !IsOutRstd 与 M != float、nullptrGamma/nullptrBeta 的组合)

总体风险判断

生产代码(host tiling + device kernel)的改动在语义与 UB 资源核算上是自洽且正确的:IsOutRstd 参数化把 LayerNormV1「第三输出为方差」的语义补齐,tiling 侧同步为 V1 额外申请了 rstd 临时 buffer,注册项与仓库既有双注册惯例一致。整体风险较低,剩余风险集中在新增 kernel UT 的启动 block 数假设与覆盖度上,建议在合并前确认 simulator 下 GetBlockNum() 与 ICPU_RUN_KF blockDim 的换算关系。

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

💬 仅评论


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
22 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
22 days ago Comments:

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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
norm ✅ 陈展熹, 钱泽洪 (2/2) ✅ 陈展熹, 钱泽洪 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
22 days ago Set zengjuan,u010470851,chaotang233,kevin_huang1234,crystalhu,yangyang016,fanqirui,wangrui_,renruhai,su-yueming,zhajianqing123,yourealize,chenqi317,wang-xing001,liubo75,tangweiwei2,chenxingyu18,liujie12345678,east_yang,pingchuantang,Chen_HaoWen,qianzehong as approver
CANN-robotCANN-robotCollaborators
22 days ago Set u010470851,kevin_huang1234,wangrui_,renruhai,su-yueming,zhajianqing123,yourealize,wang-xing001,chenxingyu18,liujie12345678,east_yang,pingchuantang,Chen_HaoWen,qianzehong as approver
atomgit-bot
atomgit-bot22 days agoStart code review2
norm/layer_norm/tests/ut/op_kernel/test_layer_norm_apt.cpp
Outdated
@@ -241,0 +304,4 @@
304+ td->nullptrGamma = !hasGamma;
305+ td->nullptrBeta = !hasBeta;
306+ AscendC::SetKernelMode(KernelMode::AIV_MODE);
307+ ICPU_RUN_KF(RunTwoPassPerf<IsOutRstd>, 2, x, gamma, beta, y, mean, lastout, tiling);
atomgit-bot
atomgit-bot22 days agoComments:

🟡 Medium Priority

变更行(新增 CheckPerfOutputs,第 306-307 行):AscendC::SetKernelMode(KernelMode::AIV_MODE); ICPU_RUN_KF(RunTwoPassPerf<IsOutRstd>, 2, ...),即按“blockDim=2 就是 2 个核”的假设启动。

受影响行为/契约:同一个文件里既有的 ExcuteTestCase 在完全相同的 AIV_MODE 下调用 ICPU_RUN_KF 时,并不直接使用 tiling 里的 blockNum,而是先做 uint32_t realBlockNum = (blockNum + 1) / 2;(第 79、94-95 行)再传。新增代码没有沿用这一换算。与此同时,新增代码上方构造的 tiling 是按“共 2 个 block”算出来的:a = ROWS = 5、aBlockFactor = 3、formerBlockUbLoops = ceil(3/ubRows)、tailBlockUbLoops = ceil(2/ubRows),且 aUbFactor 只有 1~2 行。

失败模式(取决于 sim 中 GetBlockNum() 与 ICPU_RUN_KF blockDim 的换算关系,同文件既有代码暗示二者不等):

  1. 若 GetBlockNum() > 2(例如按 2×blockDim 解释得到 4),kernel 的最后一块执行 tailA = tl_->a - aBlockFactor*(blockNum-1) - (tailBlockUbLoops-1)*aUbFactor = 5 - 3*3 = -4,currentA 变为 -4,CopyInX 中 copyInParams.blockCount = currentANum(负值转 uint32 后约 0xFFFFFFFC)→ DataCopyPad 越界读,同时 3、4 两行永远不会被写,EXPECT_NEAR(outputMean[3..4], ...) 直接失败。
  2. 若 GetBlockNum() == 1,block0 走 tail 分支得到 currentA = tailA = 5,而 elemNum_ = aUbFactor * rAlign(最多 2 行)以及 mean/lastout 队列只有 aUbFactorAlignB32(8 个 float)容量 → 越界读写 UB,断言失败。

建议:统一 block 数换算:参考同文件 ExcuteTestCase 的 realBlockNum 计算方式传入,或在启动前用 ASSERT 校验 GetBlockNum() 与构造 tiling 时假设的 block 数(本处为 2)一致;同时把该常量(2)与 ROWS_PER_CORE 等一起提为具名常量,避免 tiling 假设与启动核数脱节。

likedislike
Incorrect?
System
System MessageSystem
22 days ago Comments:

changed this line on 9f837f38 view diff detail

likedislike
rkrkCollaborators
22 days ago Pre merge succeeded (commit_id: 80f93fdd599c76af24a5206e1ac0e253aaa60d6b)
19 event messages are collapsed here。 See more
rkrkCollaborators
16 days ago Resolved final issue
rk
rkCollaborators
16 days ago Comments:

compile

likedislike
rkrkCollaborators
16 days ago Pre merge succeeded (commit_id: 9ef9318100082d695539f0d5f7950b6a9957135a)
CANN-robot
CANN-robotCollaborators
16 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
16 days ago Added label: ci-pipeline-running
rkrkCollaborators
16 days ago Modify description of pull request
CANN-robotCANN-robotCollaborators
16 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
16 days ago Added label: ci-pipeline-passed
yuning_chen
yuning_chenCollaborators
16 days ago Comments:

您好,pr合入已准备就绪,请尽快联系committer进行检视,谢谢!
@liujie12345678 @fanqirui @wangrui_ @wang-xing001 @Chen_HaoWen @yourealize @zhajianqing123 @su-yueming @u010470851 @pingchuantang

likedislike
陈展熹
陈展熹Collaborators
16 days ago Comments:

/lgtm
/approve

likedislike
qianzehong
qianzehongCollaborators
16 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
16 days ago Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
16 days ago Closed associated issue
CANN-robotCANN-robotCollaborators
16 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
16 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike
fanzhaoyun001
fanzhaoyun00116 days agoStart code review1
norm/layer_norm_v3/op_host/arch35/layer_norm_v3_regbase_two_pass_perf_tiling.cpp
@@ -53,3 +53,2 @@
5353 
54- return curA * static_cast<int64_t>(commonParams.rowAlign) * rowWeight <=
55- ubCanUseSize - LNV3_NUM_TWO * LNV3_DOUBLE_BUFFER * curAAlign;
54+ int64_t outputBufferSize = LNV3_NUM_TWO * LNV3_DOUBLE_BUFFER * curAAlign * sizeof(float);
fanzhaoyun001
fanzhaoyun00116 days agoComments:

[Major] 这个 UB 预算修正同时改了 LayerNormV3 的 aUbFactor,建议补 V3 的回归数据:kernel 里 meanQueue_/lastoutQueue_ 都是 DOUBLE_BUFFER * aUbFactorAlignB32 * sizeof(float),实际 16B/元素,而原式 LNV3_NUM_TWO * LNV3_DOUBLE_BUFFER * curAAlign 只扣了 4B/元素(少算 12B)。乘上 sizeof(float) 后是对的,但 V3 走的是同一份 CanFitInBuffer,它的 aUbFactor 也会跟着变小、UB loop 变多。PR 描述写"未提供性能数据",建议补 V3(大 A / 大 rowAlign 场景)的精度与性能对比,确认这次修正没有把 V3 拖慢。

likedislike