Merged
inplace_add_layer_norm: 基于 AddLayerNorm 实现 Ascend 950 支持 #8736
inplace_add_layer_norm: 基于 AddLayerNorm 实现 Ascend 950 支持 #8736
Merged
rkAdd on Aug 15
rkCollaborators
Aug 15

描述

为 InplaceAddLayerNorm 算子新增 Ascend 950(ascend950 / mc62)支持,且 950 路径额外支持空进空出。

改动原因

  • InplaceAddLayerNorm 此前未注册 950 的 OpAICoreConfig,且 tiling 走 Tiling4AddLayerNorm(非 regbase 路径),不支持 950。

改动方法

1. 算子定义(def.cpp)

  • inplace_add_layer_norm_def.cpp:新增 OpAICoreConfig config_950,注册 ascend950,支持 fp16/bf16/fp32 多 dtype 组合,开启动态编译静态/动态 rank/动态 shape。
  • add_layer_norm_def.cpp:移除 950 的 ExtendCfgInfo("opFile.value", "add_layer_norm_apt"),统一走通用配置。

2. tiling

  • add_layer_norm_tiling.cpp:InplaceAddLayerNorm 的 tiling 函数从 Tiling4AddLayerNorm 改为 TilingAddLayerNorm(走 regbase 路径,支持 950)。
  • add_layer_norm_tiling.h:新增 AddLayerNorm_8010/8011/8012/8200 和 InplaceAddLayerNorm_8000~8200 的 tiling data class 注册(覆盖 950 子型号);新增成员 isReduceEmpty_、方法 CheckInplaceInputsDtype()、SetTilingData()。
  • add_layer_norm_tiling_arch35.cpp:新增空张量场景处理、inplace dtype 校验、tiling data 设置逻辑。

3. kernel

  • 新增 norm/add_layer_norm/op_kernel/CMakeLists.txt、norm/inplace_add_layer_norm/op_kernel/CMakeLists.txt:声明 ascend950 编译目标及选项 --cce-no-dcache-flush --cce-simd-vf-fusion=false。
  • 新增 arch35/add_layer_norm.cpp、arch35/inplace_add_layer_norm.cpp:kernel 入口。
  • 新增 arch35/add_layer_norm_regbase_reduce_empty.h:空张量兜底 kernel,合轴后 R==0 且 A>0 时 mean/rstd 填 NaN,y/x 为空。
  • 改 _regbase_common.h / _welford.h / _full_load.h:BUFFER_NUM 默认改为 DOUBLE_BUFFER_NUM=2(双 buffer),VFCalcY 增加 x 输出参数支持 inplace 写出 x,重构 VFCalcMeanVarFast 为 VFAddFrontend。

4. 配置/文档

  • 新增 inplace_add_layer_norm_binary.json(ascend950 binary 配置)。
  • ascendc_config.json 移除 AddLayerNorm、InplaceAddLayerNorm 旧全局条目。
  • README.md:InplaceAddLayerNorm 在 950 上 ×→√,并新增 950 支持空进空出说明。

关联的Issue

#https://gitcode.com/cann/ops-nn/issues/4858

测试

  • 泛化验证pass。

文档更新

  • norm/inplace_add_layer_norm/README.md:950 支持矩阵 ×→√,新增空进空出说明。

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for rk's contribution.)
RrkCollaborators
Aug 15 Created pull request, commit ab32d06d
atomgit-bot
atomgit-bot
Aug 15 Comments:

变更摘要

本 PR 主要使 InplaceAddLayerNorm 支持 Ascend 950,并复用 AddLayerNorm 的 tiling 与 kernel 实现:将 InplaceAddLayerNorm 的 tiling 入口统一到 TilingAddLayerNorm,通过模板化的 AddLayerNormImpl 供 inplace 与非 inplace 两个 kernel 入口复用;同时补充 Ascend 950 的算子定义和二进制配置,新增空张量(reduce empty)兜底路径、full-load 单/双缓冲切换,以及 welford 路径对 inplace 输出 x 的支持。

主要改动

  • InplaceAddLayerNorm 复用 AddLayerNorm 的 tiling 与 kernel:InplaceAddLayerNorm 的 tiling 由 Tiling4AddLayerNorm 改为 TilingAddLayerNorm,新增的 inplace_add_layer_norm kernel 入口通过 AddLayerNormImpl<DTYPE_X1> 复用 AddLayerNorm 实现,并在 AddLayerNormRegbaseTiling 中新增 CheckInplaceInputsDtype 校验 inplace 场景的 dtype 组合。
  • 新增 Ascend 950 算子配置与二进制配置:inplace_add_layer_norm_def.cpp 为 ascend950 配置了 x1/x2/gamma/beta/bias 输入与 x1/mean/rstd/x2 输出,支持 fp16/bf16/fp32 及 gamma/beta 为 fp32 的混合精度,并新增 inplace_add_layer_norm_binary.json 定义 5 种 dtype 组合的二进制编译配置。
  • 新增空张量(reduce empty)兜底路径:tiling 在 cols_ == 0 && rows_ > 0 时设置 isReduceEmpty_ 并使用独立 key TILING_REDUCE_EMPTY(8200),新增 RegbaseReduceEmpty kernel 将 mean/rstd 填充为 NaN,y/x 为空不写。
  • full-load 路径支持单/双缓冲动态切换:CalcUbBufferSize 重构为按 UB 空间在 DOUBLE_BUFFER_NUM 与 SINGLE_BUFFER_NUM 间选择,新增 TILING_FULL_LOAD_NO_DB 及 8010/8011/8012 分支,add_layer_norm.h 中同步增加 no-DB 分支并将 RegbaseWelford 默认 buffer 数改为双缓冲。
  • Welford kernel 支持 inplace 输出 x:RegbaseWelford::VFCalcY 增加对 xOutAddr 的写入并通过 StoreRegToOutput 输出 x,将原先的 copy-out-x 逻辑移入 y 计算循环,以支持 inplace 复用场景的输出。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 15 Comments:

代码审查

Closing Summary

已逐一审查全部 8 个变更文件:

  • norm/add_layer_norm/op_host/add_layer_norm_def.cpp — no issues(移除 .ExtendCfgInfo("opFile.value", "add_layer_norm_apt") 与 kernel 文件重命名为 add_layer_norm 一致,默认 opFile 与 op 名匹配)。
  • norm/add_layer_norm/op_host/add_layer_norm_tiling.cpp — no issues(InplaceAddLayerNorm 复用 TilingAddLayerNorm 分派,非 950 平台仍走旧 tiling,无回归)。
  • norm/add_layer_norm/op_host/add_layer_norm_tiling.h — no issues(新增 tilingKey 注册项与成员/方法声明,均被实现或使用)。
  • norm/add_layer_norm/op_host/add_layer_norm_tiling_arch35.cpp — 1 个 P3 发现(CalcUbBufferSize 的 rowConstUbSize 口径不一致)。
  • norm/add_layer_norm/op_kernel/CMakeLists.txt — no issues(add_kernel_sources 分块注册各计算单元,无法从静态确认其宏语义,未发现可证实问题)。
  • norm/add_layer_norm/op_kernel/arch35/add_layer_norm.cpp — no issues(新 kernel 入口直接转发到模板实现,签名与 tiling 一致)。
  • norm/add_layer_norm/op_kernel/arch35/add_layer_norm.h — no issues(新增 8010/8011/8012 与 8200 分发分支,与 tilingKey 生成逻辑及 bias 位一致)。
  • norm/add_layer_norm/op_kernel/arch35/add_layer_norm_regbase_common.h — no issues(CopyGammaAndBetaToUBCommon 增加 BUFFER_NUM 模板参数,与 full-load 单/双 buffer 实例化匹配)。

发现统计:P0 0 个,P1 0 个,P2 0 个,P3 1 个。

整体风险判断:变更整体结构清晰、前后契约一致(tilingKey 生成、注册、kernel 分发、CMake 内核重命名闭环正确),reduce-empty 兜底路径的核间切分与 kernel 偏移公式自洽。唯一报告项为 UB 估算口径的保守性/性能层面的轻微不一致,不影响功能正确性,风险较低。


我已逐一审查全部 8 个文件,最终确认一处需要修复的问题。

Closing Summary

审查结论(按文件):

  • norm/add_layer_norm/op_kernel/arch35/add_layer_norm_regbase_full_load.h — 该文件 diff 内容未展开(工具显示为空),已通读当前实现(VFAddFrontend 的 StoreRegToOutput 写 x、CalculateMeanVar* 不覆盖 x32 缓冲等),未发现可锚定到新增行的明确缺陷。
  • norm/add_layer_norm/op_kernel/arch35/add_layer_norm_regbase_reduce_empty.h — 已核查 tiling 侧 tailCoreStartIndex_ = blockNum - 1(单尾核模型),rowOffset_ = coreIdx * rowsPerCore 及 loopCount_/tailLen_ 边界均正确,无问题。
  • norm/add_layer_norm/op_kernel/arch35/add_layer_norm_regbase_welford.h — x 输出移动到第二循环重算(x1+x2+bias)逻辑正确;BUFFER_NUM 默认值 1→2 与 tiling 侧 DOUBLE_BUFFER_NUM 预算一致(实为修正),无问题。
  • norm/inplace_add_layer_norm/op_host/config/ascend950/inplace_add_layer_norm_binary.json — JSON 键名无拼写错误,5 个 config 结构完整,无问题。
  • norm/inplace_add_layer_norm/op_host/inplace_add_layer_norm_def.cpp — 发现 1 处问题(P1):ascend950 各端口 DataType 列表顺序与其二进制 JSON 的 op_list 顺序错位(详见上文)。
  • norm/inplace_add_layer_norm/op_kernel/CMakeLists.txt — ascend950 用 arch35/inplace_add_layer_norm.cpp + --cce-no-dcache-flush,与 add_layer_norm 的 CMakeLists 一致,无问题。
  • norm/inplace_add_layer_norm/op_kernel/arch35/inplace_add_layer_norm.cpp — 复用 AddLayerNormImpl<DTYPE_X1>,与 inplace 语义(输出 x1/x2 同输入 dtype)匹配,无问题。
  • scripts/kernel/binary_config/ascendc_config.json — 移除 AddLayerNorm/InplaceAddLayerNorm 两条目为向 CMakeLists 构建的干净迁移(add_layer_norm 的 CMakeLists 已覆盖 ascend950/mc62 及编译选项),无问题。

问题计数:

  • P0:0
  • P1:1
  • P2:0
  • P3:0

整体风险评估: 本次改动整体结构合理(复用 AddLayerNorm 实现、reduce-empty 兜底、x 输出逻辑重构),核心风险集中在 inplace_add_layer_norm_def.cpp 中 ascend950 配置的 DataType 列表顺序与其二进制 JSON 不一致,会直接影响 ascend950 上新算子的二进制选择/编译,建议合入前修正。

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

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 15 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 15 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.
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
*/*/README.md ✅ 蔡雯雯, 唐玮玮, 汤平川 (3/2) ✅ 蔡雯雯 (1/1)
*/*/op_host/*_def.cpp ✅ 汤平川, 汤磊, 唐玮玮 (3/2) ✅ 汤磊 (1/1)
norm ✅ 汤平川, 唐玮玮, 钱泽洪 (3/2) ✅ 汤平川, 钱泽洪 (2/1)
scripts ✅ 汤平川, 唐玮玮 (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

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

likedislike
55 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 20 Added label: approved
TangPC
TangPCCollaborators
Aug 20 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 20 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 20 Merged pull request
CANN-robot
CANN-robotCollaborators
Aug 20 Comments:

Pull Request 已合并或已关闭。

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

likedislike