已合并
LayerNorm类算子VL/UB block去硬编码 #9042
rk创建于 15 天前
LayerNorm类算子VL/UB block去硬编码 #9042
已合并
rk创建于 15 天前
rk成员
15 天前

描述

本 PR 对 norm 系列算子 arch35 路径下的 VL/UB block size 处理进行代码清理,去除散落在各算子中的硬编码常量 32,统一改用平台 API 在运行时获取,消除魔法数字,提升代码可维护性与跨硬件适配能力。

改动原因

arch35 路径下 add_layer_norm_quant、layer_norm_grad/grad_v3、layer_norm_v3、layer_norm_v4 等算子的 host tiling 与 kernel 侧多处使用 constexpr ... = 32 表示 UB block 字节数(部分派生出 B32_ALIGN_NUM = 32/sizeof(float) 等常量)。这些魔法数字:

  • 与具体硬件强绑定,未来硬件 block size 变化时需要逐文件修改,维护成本高;
  • 同一语义在 host 侧(BLOCK_SIZE/BLOCK_BYTES/B32_ALIGN_NUM)与 kernel 侧(自定义 GetUbBlockSize()BLOCK_SIZE)重复定义,缺乏单一数据源。

改动方法

统一替换为平台 API:

  • Host tiling 侧:删除本地 BLOCK_SIZE/BLOCK_BYTES/B32_ALIGN_NUM 等常量,改用 Ops::Base::GetUbBlockSize(context_)(新增 #include "op_common/op_host/util/platform_util.h"),或复用类成员 this->blockSize_
  • Kernel 侧(layer_norm_v3/v4 common/regbase/welford):constexpr static int64_t BLOCK_SIZE = 32platform::GetUbBlockSize()(新增 #include "../../inc/platform.h");
  • Kernel 侧(layer_norm_grad_base.h):删除自定义 GetUbBlockSize() 内联函数,统一使用 AscendC 框架的 BLOCK_SIZE

涉及算子:add_layer_norm_quant、layer_norm_grad、layer_norm_grad_v3、layer_norm_v3、layer_norm_v4。

关联的Issue

测试

  • pass

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 rk 的贡献)
Rrk成员
15 天前 创建了 pull request,commit 11f6d518
atomgit-bot
atomgit-bot
15 天前 评论:

变更摘要

本 PR 对 norm 系列算子的 arch35 路径做去硬编码清理:将代码中写死的 32 字节 UB block(及 256 字节向量寄存器)常量,统一替换为平台查询接口(host 侧 Ops::Base::GetUbBlockSize(context)/commonParams.blockSize,kernel 侧 platform::GetUbBlockSize()/Ops::Base::GetVRegSize()),涉及 add_layer_norm_quantlayer_norm_gradlayer_norm_grad_v3layer_norm_v3layer_norm_v4 等算子的 tiling 与 kernel 代码,使 UB/VL block 大小随平台自适应。

主要改动

  • kernel 侧 block 常量改为平台接口:在 layer_norm_v3_common.hlayer_norm_v4_regbase_common.hlayer_norm_v4_welford.h 中将 constexpr static int64_t BLOCK_SIZE = 32 改为 platform::GetUbBlockSize()layer_norm_grad_api.hBLOCK_SIZE 改为 Ops::Base::GetUbBlockSize()VREG_SIZE = 256 改为 VECTOR_LENGTH = Ops::Base::GetVRegSize()
  • host 侧 tiling 使用 commonParams.blockSizelayer_norm_v3_regbase_no_reduce_tiling.cpplayer_norm_v3_regbase_norm_not_equal_params_tiling.cpplayer_norm_v3_regbase_two_pass_perf_tiling.cpplayer_norm_v3_welford_multi_reduce_tiling.cpplayer_norm_v4_regbase_two_pass_perf_tiling.cpp 删除 *_BLOCK_SIZE = 32/*_B32_ALIGN_NUM 等常量,改用 commonParams.blockSize / sizeof(float) 计算 B32 对齐数。
  • add_layer_norm_quant 去硬编码:host 侧 add_layer_norm_quant_tiling_arch35.cpp 删除 BLOCK_SIZEcolsAligned_blkFp32NumsconstTmpBufSize 改用 this->blockSize_;kernel 侧 add_layer_norm_quant_regbase_helper.hFLOAT_BLOCK_ELEM = 8 改为 blockSize_ / sizeof(float)
  • layer_norm_grad/layer_norm_grad_v3 统一 block 获取方式layer_norm_grad_base.h 删除自定义返回 32U 的 GetUbBlockSize(),相关调用改用 API 中定义的 BLOCK_SIZElayer_norm_grad_v3_base.hGetUbBlockSize() 改为返回 Ops::Base::GetUbBlockSize()
  • 对齐函数参数化layer_norm_v3_regbase_norm_not_equal_params_tiling.cppAlignB32(val) 改为 AlignB32(val, alignNum),由调用方基于 commonParams.blockSize 传入对齐数,去除对固定 32B 对齐的假设。
likedislike
atomgit-bot
atomgit-bot
15 天前 评论:

代码审查

✅ 未发现问题

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

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) 钱泽洪 (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
此处折叠了61条消息 查看更多
CANN-robotCANN-robot成员
5 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
5 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
5 天前 添加了label:ci-pipeline-passed
CANN-robotCANN-robot成员
5 天前 关闭了关联的issue
CANN-robotCANN-robot成员
5 天前 合入了pull request