已合并
cleancode: norm 类算子代码去重 #7977
cleancode: norm 类算子代码去重 #7977
已合并
rk创建于 7月27日
rk成员
7月27日

描述

ops-nn/norm 系列 arch35 算子代码重复率治理(cleancode)。在严格保持实现逻辑、计算顺序、内存分配顺序完全一致前提下,通过抽取公共实现/分支尾上提/模板统一消除重复,覆盖 4 个算子:

  • batch_norm_v3
    • batch_norm_v3_regbase_common.hTwoRowAddPartialMeanWithTail 复用已有 TwoRowAddPartialMean(移动定义、去前向声明)
    • 三个 infer 变体(continuous_a/small_a/small_ab1)的 Compute 统一为模板 helper InferComputeImpl<Self, T>,经 self.VFNormalize(...) 静态派发
  • rms_norm_gradrms_norm_grad_regbase_dgamma.h 抽取 ComputePreDgamma(EnQue/DeQue/取址/VFCalcPreDgamma/Free),3 处复用
  • add_rms_normadd_rms_norm_regbase_common.hLoadForHandleRemainV1 三个 if-constexpr 分支公共尾上提
  • add_rms_norm_dynamic_quantadd_rms_norm_dynamic_quant_regbase_common.h 抽取 VF 单元 ComputeYAndAbsMaxVF(归一化输出 + running abs-max),两处复用

等价性说明

均为逐操作等价:函数体逐字节搬运,仅作用域/调用形式变化;if constexpr 下每个实例化指令流不变;寄存器声明位置变化不影响操作顺序与内存分配顺序。已用「去空白 md5 比对」确认无标识符级改动。

关联的Issue

测试

  • 逐操作等价校验:内联还原 / token 级去空白 md5 比对,确认无标识符改动
  • 950 上板编译 + 精度回归:pass

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 rk 的贡献)
Rrk成员
7月27日 创建了 pull request,commit d0587719
atomgit-bot
atomgit-bot
7月27日 评论:

变更摘要

本次 PR 是一次纯粹的代码清理(cleancode)提交,主要目标是在多个 Ascend 算子内核文件中消除重复代码。通过将 if constexpr 分支中的公共逻辑提取到分支外部、将重复的计算逻辑抽取为独立模板函数、以及将队列/缓冲区管理的通用模式封装为公共辅助函数,显著减少了代码冗余,提升了可维护性。

主要改动

  • add_rms_norm_regbase_common.h — 统一 LoadForHandleRemainV1 中三种数据类型的公共后处理逻辑:将 halfbfloat16_tfloat 三个分支中完全重复的 Add/DataCopy/Mul 操作提取到 if constexpr 块之后,同时把 RegTensor<float> mainA2, mainB2, tailA2, tailB2 声明提升到分支外部,消除了约 30 行重复代码。

  • add_rms_norm_dynamic_quant_regbase_common.h — 抽取 ComputeYAndAbsMaxVF 模板函数:将 ComputeYScaleComputeReduceMax 两处完全相同的计算序列(Mul→可选 Add(beta)→可选 Mul(smooth_scale)→DataCopyAbsMax)提取为新的 ComputeYAndAbsMaxVF<HAS_BETA, HAS_SMOOTH_SCALE> 模板函数,两处调用点统一使用该函数。

  • batch_norm_v3_regbase_common.h — 新增 InferComputeImpl 模板函数并重构 TwoRowAddPartialMeanWithTail:引入 InferComputeImpl<Self, T> 封装 batch_norm_v3 各变体中重复的队列出队/分配、缓冲区获取、物理地址提取和 VFNormalize 调用模式;同时 TwoRowAddPartialMeanWithTail 改为调用 TwoRowAddPartialMean 来消除其内部重复的均值计算逻辑。

  • 三个 batch_norm_v3 子类 — Compute 方法统一委托 InferComputeImplbatch_norm_v3_infer_last_channel_continuous_a.hbatch_norm_v3_infer_last_channel_small_a.hbatch_norm_v3_infer_small_ab1.h 中的 Compute 方法均由原先内联的队列/缓冲区管理代码替换为对 InferComputeImpl 的调用,同时将 VFNormalize 的访问权限从 private 改为 public 以支持模板函数调用。

  • rms_norm_grad_regbase_dgamma.h — 抽取 ComputePreDgamma 方法:将 CalcDgammaCalcDgammaAndReduceSumCalcDgammaAndReduceSumWithTail 三处方法中重复的队列操作(DeQueGetPhyAddrVFCalcPreDgammaFreeTensor)提取为 ComputePreDgamma,消除约 30 行重复代码。

likedislike
不准确?
atomgit-bot
atomgit-bot
7月27日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
7月27日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
7月27日 评论:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robot成员
7月27日 评论:

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

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

likedislike
CANN-robotCANN-robot成员
7月27日 将zengjuan,kevin_huang1234,crystalhu,yangyang016,fanqirui,renruhai,su-yueming,zhajianqing123,chenqi317,wang-xing001,liubo75,tangweiwei2,liujie12345678,Chen_HaoWen设为评审人
此处折叠了12条事件消息 查看更多
Rrk成员
7月29日 预合并成功(commit_id: 8681bd578ad231aa4ecb42a47c000bf43b480cd7)
rk成员
7月29日 评论:

compile

likedislike
Rrk成员
7月29日 预合并成功(commit_id: f9fd577c1b698a25623707a8eab1d684a2146c6b)
CANN-robotCANN-robot成员
7月29日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月29日 评论:

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

任务名称状态日志下载链接
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_Ascend_X86_mobile_station_9030_ubuntu24 ✅ 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_harmony-infer-chs-nn ✅ SUCCESS
UT_Test_harmony-infer-chs-nn-1 ✅ SUCCESS
UT_Test_report_lcov ✅ SUCCESS >>>>>

[2026-07-29 16:13:50]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
7月29日 评论:

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

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

[2026-07-29 15:59:25]    CI执行结束

likedislike
CANN-robotCANN-robot成员
7月29日 添加了label:api-check-pass
此处折叠了6条事件消息 查看更多
Rrk成员
7月29日 修改标题为 “cleancode: norm 类算子代码去重”,原标题为“norm: 抽取 arch35 norm 算子公共实现消除重复代码”
tangweiwei2成员
7月29日 评论:

/lgtm
/approve

likedislike
wang-xing001成员
7月30日 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
7月30日 添加了label:lgtmapproved
CANN-robotCANN-robot成员
7月30日 关闭了关联的issue
CANN-robotCANN-robot成员
7月30日 合入了pull request