Pull Request已成功合入, 合并人@CANN-robot
(感谢 yulianjie 的贡献)变更摘要
此 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.cpp及bn_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路径以适配文件重组。


代码审查
审查总结
本次审查覆盖了 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 侧流水线同步(SyncAll、SetFlag/WaitFlag)和边界处理(ClearChunkExtensionVf、头尾块处理)均正确。
唯一的问题在 kernel 的 Group Tail-R 路径(tilingKey=5):Phase2 缺少对 sum 输出的 sumOutputScale_ 恢复,导致该路径的计算精度与其它全部路径不一致。建议按报告中给出的修复方案,在 Phase2Process 的 isTailR 分支内按 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 |
⛔ 需要修改


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
本 PR 为
BNTrainingReduce算子在 Ascend 950PR/Ascend 950DT(arch35)新增 AscendC 实现。算子对四维 NCHW 输入沿 N、H、W 轴归约,按通道输出元素和sum与平方和square_sum。主要改动:
[C]。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。aclnnBatchNormReduce产品支持说明和中英文算子清单。本次变更共涉及 27 个文件,新增 6373 行、删除 33 行。
关联的Issue
关联 Issue #4715:https://gitcode.com/cann/ops-nn/issues/4715
测试
已补充以下验证资产:
aclnnBatchNormReduceGetWorkspaceSize+aclnnBatchNormReduce两段式调用示例。当前 PR 描述未附可独立核对的全量执行报告;最终结果以仓库 CI、复测记录及未解决检视意见闭环为准。
文档更新
norm/bn_training_reduce/README.md,补充功能、公式、参数、约束和调用说明。norm/bn_training_reduce/docs/aclnnBatchNormReduce.md,声明 Ascend 950PR/Ascend 950DT 支持。docs/zh/op_api_list.md、docs/zh/op_list.md及对应英文算子清单。类型标签
AI/Agent生成声明