已合并
BatchNormReduce算子在ascend 950 NPU上支持ascendc实现 #8543
yulianjie创建于 27 天前
BatchNormReduce算子在ascend 950 NPU上支持ascendc实现 #8543
已合并
yulianjie创建于 27 天前
yulianjie成员
27 天前

描述

本 PR 为 BNTrainingReduce 算子在 Ascend 950PR/Ascend 950DT(arch35)新增 AscendC 实现。算子对四维 NCHW 输入沿 N、H、W 轴归约,按通道输出元素和 sum 与平方和 square_sum

主要改动:

  • 新增 BNTrainingReduce 算子原型、OpDef 注册及图级 InferShape/InferDataType;输入支持 FLOAT16、BFLOAT16、FLOAT + NCHW,两个输出固定为 FLOAT + ND,Shape 为 [C]
  • 新增 arch35 Host Tiling,实现基于 Shape、DType、UB 容量和核数的多核切分,覆盖 normal、group、tail 和 empty 等执行分支。
  • 新增 AscendC Kernel,实现 sum_c = Σ(n,h,w) x[n,c,h,w]square_sum_c = Σ(n,h,w) x[n,c,h,w]^2;FLOAT16/BFLOAT16 输入按 FLOAT 精度执行平方与累加,并处理空 Tensor。
  • 调整 ACLNN 文件目录与构建配置,新增 Ascend 950 ACLNN 两段式调用示例。
  • 新增 GEIR 静态/动态 Shape、未知 Rank、数据类型/格式/非法输入及特殊值场景的验证代码。
  • 更新算子 README、aclnnBatchNormReduce 产品支持说明和中英文算子清单。

本次变更共涉及 27 个文件,新增 6373 行、删除 33 行。

关联的Issue

关联 Issue #4715:https://gitcode.com/cann/ops-nn/issues/4715

测试

已补充以下验证资产:

  • ACLNN:新增 aclnnBatchNormReduceGetWorkspaceSize + aclnnBatchNormReduce 两段式调用示例。
  • GEIR:新增静态/动态 Shape 与未知 Rank 执行框架,覆盖 FLOAT16、BFLOAT16、FLOAT、NCHW 输入、FLOAT/ND 输出、空 Tensor、NaN/Inf 及非法 Rank/Format/DType 等场景。
  • Host/API UT:同步修正 ACLNN UT 的 include 路径以适配目录调整。

当前 PR 描述未附可独立核对的全量执行报告;最终结果以仓库 CI、复测记录及未解决检视意见闭环为准。

文档更新

  • 更新 norm/bn_training_reduce/README.md,补充功能、公式、参数、约束和调用说明。
  • 更新 norm/bn_training_reduce/docs/aclnnBatchNormReduce.md,声明 Ascend 950PR/Ascend 950DT 支持。
  • 更新 docs/zh/op_api_list.mddocs/zh/op_list.md 及对应英文算子清单。

类型标签

AI/Agent生成声明

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 yulianjie 的贡献)
Yyulianjie成员
27 天前 创建了 pull request,commit 71c017ff
atomgit-bot
atomgit-bot
27 天前 评论:

变更摘要

此 PR 为 BNTrainingReduce 算子在 Ascend 950 (arch35) NPU 上新增了完整的 AscendC 实现,包括算子注册、图级形状/数据类型推导、Host 端 Tiling 计算、Device 端 Kernel 执行以及配套的单元测试与示例文件。核心功能是将输入的 4 维 NCHW 张量沿 C 轴进行规约,分别输出 sum 和 square_sum 两个一维 float32 结果,支持 FLOAT16 / BFLOAT16 / FLOAT32 三种输入数据类型。

主要改动

  • 算子协议与定义注册:新增 bn_training_reduce_proto.h 定义 BNTrainingReduce 的输入输出结构(x→sum, square_sum),bn_training_reduce_def.cpp 注册算子并配置 ascend950 平台支持动态形状、动态 Rank 等特性。

  • AscendC Kernel 实现:新增 bn_training_reduce.h,核心类 BNTrainingReduceKernel<DType, isTailR> 实现了双缓冲流水线、二分缓存树规约、Tail-A/Tail-R 两种布局模式的完整 Device 端计算逻辑;bn_training_reduce_empty.h 处理空张量场景;Kernel 入口 bn_training_reduce.cpp 根据模板参数分派到 Normal、Group 或 Empty 分支。

  • Host 端 Tiling 系统:新增 bn_training_reduce_tiling_arch35.cppbn_training_reduce_tiling_public.cpp,实现了从算子接口输入(shape、dtype、format)和平台信息(UB 大小、核数等)计算出多核分配方案、UB 缓冲区大小、workspace 空间和 tiling key 的完整 Tiling 逻辑,支持 5 个分支(Normal Tail-A/R、Group Tail-A/R、Empty)。

  • 图级推断与数据结构bn_training_reduce_graph_infer.cpp 实现了 InferShape(校验 4 维 NCHW 输入并推导 1 维输出 shape)和 InferDataType(限定输入类型为 F16/BF16/F32,输出固定为 F32);新增 bn_training_reduce_tiling_data.h 定义了 Kernel 与 Tiling 之间传递的 BNTrainingReduceTilingData 结构体。

  • 测试与示例:新增 test_aclnn_batch_norm_reduce.cpp(ACLNN 接口示例)和两份 GEIR 测试框架文件(test_geir_bn_training_reduce.cpp 及 arch35 版本),覆盖正例验证、拒绝用例、运行时输入契约检查、分区可加性不变性等多种测试场景;同时修正了已有单测中的 #include 路径以适配文件重组。

likedislike
不准确?
atomgit-bot
atomgit-bot
27 天前 评论:

代码审查

审查总结

本次审查覆盖了 8 个变更文件,共发现 6 个问题(2 个 P2,4 个 P3):

优先级 数量 说明
P2 2 示例代码中的魔术数字和未检查返回值,可能导致静默错误
P3 4 禁用代码块、实现定义行为依赖、缺少 license 头部、类型不一致

各文件审查结论

文件 结论
norm/bn_training_reduce/CMakeLists.txt 无问题 — add_modules_sources 使用符合 CANN 规范
norm/bn_training_reduce/examples/arch35/test_aclnn_batch_norm_reduce.cpp 3 个问题(2×P2 + 1×P3):硬编码 sizeof、未检查 aclCreateTensor、无符号回绕
norm/bn_training_reduce/examples/arch35/test_geir_bn_training_reduce.cpp 1 个问题(P3):缺少版权头部
norm/bn_training_reduce/examples/test_geir_bn_training_reduce.cpp 2 个问题(P3):缺少版权头部、FP16 类型选择不一致
norm/bn_training_reduce/op_api/aclnn_batch_norm_reduce.cpp 无问题 — 纯重命名,无内容变更
norm/bn_training_reduce/op_api/aclnn_batch_norm_reduce.h 无问题 — 纯重命名,无内容变更
norm/bn_training_reduce/op_graph/bn_training_reduce_graph_infer.cpp 1 个问题(P3):大型 #if 0 禁用代码块含过时实现
norm/bn_training_reduce/op_graph/bn_training_reduce_proto.h 无问题 — 算子注册正确,类型定义一致

整体风险评估

该 PR 的核心算子逻辑(graph infer shape/dtype、op proto 注册、op_api 实现)均正确无误。所有发现的问题均位于示例/测试代码或可维护性层面,不涉及运行时正确性、安全或数据一致性风险。整体风险为 ,可以合入。


Now I have all the information needed. Let me compile the final summary.


审查总结

我逐一审查了本次 diff 涉及的 7 个变更文件(加上 1 个删除文件),各文件审查结论如下:

文件 审查结论
norm/bn_training_reduce/op_host/CMakeLists.txt(删除) 无问题
norm/bn_training_reduce/op_host/arch35/bn_training_reduce_tiling_arch35.cpp 无问题
norm/bn_training_reduce/op_host/arch35/bn_training_reduce_tiling_arch35.h 无问题
norm/bn_training_reduce/op_host/arch35/bn_training_reduce_tiling_public.cpp 无问题
norm/bn_training_reduce/op_host/arch35/bn_training_reduce_tiling_public.h 无问题
norm/bn_training_reduce/op_host/bn_training_reduce_def.cpp 无问题
norm/bn_training_reduce/op_host/bn_training_reduce_infershape.cpp 无问题(#if 0 整文件禁用,标记为 [REF_SAMPLE],故为有意行为)
norm/bn_training_reduce/op_kernel/arch35/bn_training_reduce.h 1 个 P1 发现

按优先级统计:

  • P0: 0
  • P1: 1(Tail-R group 路径 Phase2 缺失 sum 输出的 scale 恢复)
  • P2: 0
  • P3: 0

整体风险评估:

该变更引入了 BNTrainingReduce 算子在 ascend 950 NPU 上的 AscendC 实现,代码整体质量较高——tiling 侧有完善的溢出检查(TryMulNonNegative / TryAddNonNegative)、输入校验(ValidateInputs)、tiling 结果验证(各 HasValid* 函数),kernel 侧流水线同步(SyncAllSetFlag/WaitFlag)和边界处理(ClearChunkExtensionVf、头尾块处理)均正确。

唯一的问题在 kernel 的 Group Tail-R 路径(tilingKey=5):Phase2 缺少对 sum 输出的 sumOutputScale_ 恢复,导致该路径的计算精度与其它全部路径不一致。建议按报告中给出的修复方案,在 Phase2ProcessisTailR 分支内按 outputIdx 分流处理。


All seven files reviewed. Let me now produce the closing summary.


审查总结

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

文件 结果
norm/bn_training_reduce/op_kernel/arch35/bn_training_reduce_empty.h 发现 1 个 P0 问题DuplicateEmptyROutputVfImpl 循环步长与写入量不匹配
norm/bn_training_reduce/op_kernel/arch35/bn_training_reduce_kernel.h 无问题(全部代码位于 #if 0 禁用块内,仅为参考样例)
norm/bn_training_reduce/op_kernel/arch35/bn_training_reduce_struct.h 无问题(模板参数声明正确,覆盖了三个必要分支)
norm/bn_training_reduce/op_kernel/arch35/bn_training_reduce_tiling_data.h 无问题(扁平 tiling 数据结构,与 kernel 入口匹配)
norm/bn_training_reduce/op_kernel/arch35/bn_training_reduce_tiling_struct.h 无问题(仅为兼容性重导出的 include)
norm/bn_training_reduce/op_kernel/bn_training_reduce.cpp 无问题(active 代码的模板分发逻辑正确,与 tiling struct 和 kernel class 契约一致)
norm/bn_training_reduce/tests/ut/op_host/op_api/test_aclnn_batch_norm_reduce.cpp 无问题(include 路径修正,从 op_host/op_api/ 改为 op_api/,与实际文件位置一致)

各优先级统计:

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

整体风险评估:高风险。 P0 问题会导致 isEmptyTensor 路径(输入 reduce 轴为空时)输出 sum/squareSum 包含未初始化的 UB 脏数据,且存在末尾越界写风险,必须在合入前修复。

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

⛔ 需要修改

likedislike
不准确?
CANN-robotCANN-robot成员
27 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
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.
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 陈琦, 陈娇 (2/2) 陈娇 (1/1)
*/*/docs/acl*.md 陈娇, 陈琦 (2/2) 陈娇 (1/1)
*/*/op_api/*.h 王永光, 陈琦 (2/2) 王永光 (1/1)
*/*/op_graph/*_proto.h 王永光, 陈琦 (2/2) 王永光 (1/1)
*/*/op_graph/*_proto_extend.h 王永光, 陈琦 (2/2) 王永光 (1/1)
*/*/op_host/*_def.cpp 王永光, 陈琦 (2/2) 王永光 (1/1)
docs 陈娇, 陈琦 (2/2) 陈娇 (1/1)
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

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

likedislike
此处折叠了170条消息 查看更多
chenqi317成员
20 天前 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
20 天前 添加了label:lgtm
CANN-robotCANN-robot成员
20 天前 关闭了关联的issue
CANN-robotCANN-robot成员
20 天前 合入了pull request
CANN-robot
CANN-robot成员
20 天前 评论:

Pull Request 已合并或已关闭。

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

likedislike