| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
perf: optimize BatchNorm/BatchNormV3 infer paths and guard aligned UB usage Co-authored-by: qianzehong<qianzehong@huawei.com> # message auto-generated for no-merge-commit merge: !4611 merge opt/bn-v3-small-a-infer into master perf: optimize BatchNorm/BatchNormV3 infer paths and guard aligned UB usage Created-by: qianzehong Commit-by: qianzehong Merged-by: cann-robot Description: ## 描述 本 PR 优化 BatchNormV3 infer 在 arch35/Ascend 950 上的小 A、连续 last-channel A 和 small-AB1 场景性能,并将同样的 902000/901000/911000 infer 优化模板同步扩展到 BatchNorm。当前实现复用 batch_norm_v3 / batch_norm 入口和已有 fatbin 体系,通过专用 tiling key 分发到优化模板,不新增独立算子入口。 同时合入 BatchNorm/BatchNormV3 infer 通用路径的 aligned UB 使用量保护:tiling 侧按 blockSize 对 x/y 和参数 buffer 做 AlignUp 后重新校验 UB 占用,避免未计入 32B 对齐开销时生成超过 UB 的 tile。 新增/调整的 infer 优化路径: - 902000:last-channel small-A infer 模板 BatchNormV3InferLastChannelSmallA。 - 901000:last-channel continuous-A infer 模板 BatchNormV3InferLastChannelContinuousA。 - 911000:small-AB1 infer 模板 BatchNormV3InferSmallAB1。 BatchNorm 同步新增/调整的 infer 优化路径: - 902000:last-channel small-A infer 模板 BatchNormInferLastChannelSmallA。 - 901000:last-channel continuous-A infer 模板 BatchNormInferLastChannelContinuousA。 - 911000:small-AB1 infer 模板 BatchNormInferSmallAB1。 ### last-channel small-A infer(tiling key 902000) 涉及文件: - norm/batch_norm_v3/op_host/batch_norm_v3_tiling_infer_last_channel_arch35.cpp - norm/batch_norm_v3/op_kernel/arch35/batch_norm_v3_infer_last_channel_small_a.h - norm/batch_norm_v3/op_kernel/batch_norm_v3_apt.cpp 当前触发条件: - 0 < fusedALen <= 32 - fusedBLen >= 65536 - 支持 NHWC/NDHWC last-channel 场景;NCHW/NCDHW 仅在空间轴全为 1 时映射到等价 last-channel 场景。 实现要点: - Host tiling 对 small-A 场景设置 aOuter=1、tileBlockALen=fusedALen,只沿 B 轴切分,返回 tiling key 902000。 - small-A kernel 使用 floor(VL_FP32 / A) * A 计算参数 cache 有效长度,缓存 gamma/beta/mean/rstd,避免参数按整个 B*A tile 展开。 - x/y 按连续 B*A 数据块搬运和计算,主循环使用 DataCopyUnAlign 处理非 32B 对齐的连续访问。 ### last-channel continuous-A infer(tiling key 901000) 涉及文件: - norm/batch_norm_v3/op_host/batch_norm_v3_tiling_infer_last_channel_arch35.cpp - norm/batch_norm_v3/op_kernel/arch35/batch_norm_v3_infer_last_channel_continuous_a.h - norm/batch_norm_v3/op_kernel/batch_norm_v3_apt.cpp 当前触发条件: - 32 < fusedALen <= 512 - fusedBLen >= 65536 - 支持 NHWC/NDHWC last-channel 场景;NCHW/NCDHW 仅在空间轴全为 1 时映射到等价 last-channel 场景。 实现要点: - Host tiling 对 continuous-A 场景设置 aOuter=1、tileBlockALen=fusedALen,只沿 B 轴切分,返回 tiling key 901000。 - Kernel 将 gamma/beta/mean/rstd 预处理为按 VL_FP32 对齐的 fp32 cache,主循环按 A 方向连续数据搬运和计算。 - 该路径用于 A > 32 的 last-channel 场景,解决普通 BatchNormV3InferLastChannel 搬运方式在大 B 连续 last-channel 场景下效率较低的问题。 ### BatchNorm 同步模板与 aligned UB 保护 涉及文件: - norm/batch_norm/op_host/arch35/batch_norm_tiling_infer_arch35.cpp - norm/batch_norm/op_host/arch35/batch_norm_tiling_infer_last_channel_arch35.cpp - norm/batch_norm/op_kernel/arch35/batch_norm_infer_last_channel_small_a.h - norm/batch_norm/op_kernel/arch35/batch_norm_infer_last_channel_continuous_a.h - norm/batch_norm/op_kernel/arch35/batch_norm_infer_small_ab1.h - norm/batch_norm/op_kernel/batch_norm_apt.cpp - norm/batch_norm_v3/op_host/batch_norm_v3_tiling_infer_arch35.cpp 实现要点: - BatchNorm 侧同步新增 902000/901000/911000 三个 infer 模板,触发条件与 BatchNormV3 对齐,分别覆盖 last-channel small-A、last-channel continuous-A 和 small-AB1 场景。 - BatchNorm 和 BatchNormV3 的通用 infer tiling 路径增加 aligned UB guard:按 blockSize_ 对 x/y buffer 和参数 buffer 做 AlignUp 后计算实际 UB 占用,若超过 aicoreParams_.ubSize 则递减 aInner,防止 vectorLength 较小或 UB 边界 case 生成超 UB tile。 - 新增 fp16 vectorLength=32 的 tiling UT,分别校验 BatchNorm 与 BatchNormV3 aligned UB 计算后仍不超过 compile info 中的 UB 大小。 ### small-AB1 infer(tiling key 911000) 涉及文件: - norm/batch_norm_v3/op_host/batch_norm_v3_tiling_infer_arch35.cpp - norm/batch_norm_v3/op_host/batch_norm_v3_tiling.h - norm/batch_norm_v3/op_kernel/arch35/batch_norm_v3_infer_small_ab1.h - norm/batch_norm_v3/op_kernel/batch_norm_v3_apt.cpp 当前触发条件: - fusedALen * fusedB1Len <= 32 - fusedB0Len >= 8 - 适用于 NCHW/NCDHW 的 [B0, A, ..., B1] 抽象 pattern,即 small-AB1 场景。 实现要点: - Host tiling 设置 smallAB1Mode=1,aOuter=1、b1Outer=1,只沿 B0 轴切分,返回 tiling key 911000。 - BatchNormV3InferTilingData 增加 smallAB1Mode 字段,并注册 BatchNormV3_911000 tiling data。 - small-AB1 kernel 将 [curB0, A, B1] 作为连续数据块搬入/搬出,减少原通用路径在 small A*B1 场景下的 stride 和多 tile 开销。 - 参数 cache 长度为 floor(VL_FP32 / (A * B1)) * (A * B1),通过 offset gather 生成 gamma/beta/mean/rstd fp32 cache,在主循环中复用。 ### 公共 helper 涉及文件: - norm/norm_common/op_kernel/reduce_common_regbase.h 新增可复用 RegBase helper: - NormCommon::LoadTensorUnAlignForDtypeT - NormCommon::StoreTensorUnAlignForDtypeT - NormCommon::NormalizeWithScaleBiasReg 用于提取 small-A、continuous-A、small-AB1 三个 infer 模板中的通用 unalign load/store 和 infer normalize 计算逻辑。 ## 关联的Issue 关联 Issue #2577 ## 测试 - 已执行 git diff --check,通过。 - 远程 Ascend 950 编译 BatchNorm 自定义 binary:bash build.sh --ops=batch_norm --soc=ascend950 --opkernel --noexec -j8,通过。 - 远程 Ascend 950 编译 BatchNormV3 自定义 binary:bash build.sh --ops=batch_norm_v3 --soc=ascend950 --opkernel --noexec -j8,通过。 - 远程 Ascend 950 编译 BatchNormV3 自定义 run 包并安装到 /home/qianzehong/workspace/ttk_custom_opp_bn,通过。 - 远程 Ascend 950 使用 TTK 验证 x=[256,42,42,168]、mean/gamma/beta/var=[168] 的 NHWC infer case,命中 tiling key 901000,y 输出精度 99.99962829253725%,PRECISION_STATUS: PASS,STATUS: PASS。 - 补充 op_host tiling UT,覆盖 last-channel small-A / continuous-A 和 small-AB1 的分发边界。 - PR 当前新增 BatchNorm/BatchNormV3 infer 模板优化,并合入 aligned UB guard;最新提交包含 fix: guard batch_norm infer aligned ub usage。 ### TTK 性能对比(902000/901000) 测试环境:远程 Ascend 950PR,TTK python3 -m ttk kernel -b=release --golden-mode Disable --warmup true;902000/901000 使用 --run 20,911000 dense case 使用 --run 30。PR 包路径 /home/qianzehong/Ascend/custom_ops/pr4611_bn_aouter_dtype_outer,master 包路径 /home/qianzehong/Ascend/custom_ops/master_bn。delta 为 PR 相对 master,负数表示 PR 更快。 #### 902000 small last-channel 模板 覆盖条件:0 < A <= 8 && B >= 65536。PR 全部命中 902000,master 对照为 900000,fp16/fp32 均未发现回退。 | dtype | A | B | master | PR | delta | |---|---:|---:|---:|---:|---:| | fp16 | 1 | 65536 | 6.517us | 5.699us | -12.6% | | fp16 | 2 | 65536 | 6.401us | 5.832us | -8.9% | | fp16 | 4 | 65536 | 6.469us | 5.907us | -8.7% | | fp16 | 8 | 65536 | 6.284us | 6.046us | -3.8% | | fp16 | 1 | 451584 | 21.201us | 6.085us | -71.3% | | fp16 | 8 | 451584 | 20.946us | 10.874us | -48.1% | | fp32 | 1 | 65536 | 5.853us | 4.665us | -20.3% | | fp32 | 2 | 65536 | 5.781us | 4.944us | -14.5% | | fp32 | 4 | 65536 | 5.844us | 5.027us | -14.0% | | fp32 | 8 | 65536 | 5.989us | 5.629us | -6.0% | | fp32 | 1 | 451584 | 17.683us | 5.664us | -68.0% | | fp32 | 8 | 451584 | 18.712us | 13.123us | -29.9% | 结论:902000 覆盖区间内 fp16/fp32 均无回退;最小收益点为 fp16 A=8,B=65536,约 3.8%。 #### 901000 continuous-A 模板 覆盖条件:A > 64 && B >= 65536 && 1 < baseAOuter <= dtypeLimit,其中 fp32 dtypeLimit=6,fp16 dtypeLimit=3。命中 901000 的 case 均未发现回退。 | dtype | A | B | master 900000 | PR 901000 | delta | |---|---:|---:|---:|---:|---:| | fp16 | 168 | 65536 | 41.987us | 20.884us | -50.3% | | fp16 | 168 | 262144 | 223.839us | 79.411us | -64.5% | | fp16 | 168 | 451584 | 426.368us | 162.006us | -62.0% | | fp16 | 336 | 65536 | 70.962us | 36.847us | -48.1% | | fp16 | 336 | 262144 | 376.066us | 198.286us | -47.3% | | fp16 | 336 | 451584 | 792.566us | 369.580us | -53.4% | | fp32 | 84 | 65536 | 41.496us | 18.499us | -55.4% | | fp32 | 84 | 262144 | 207.952us | 79.646us | -61.7% | | fp32 | 84 | 451584 | 434.988us | 162.476us | -62.6% | | fp32 | 168 | 65536 | 70.989us | 34.293us | -51.7% | | fp32 | 168 | 262144 | 459.882us | 194.651us | -57.7% | | fp32 | 168 | 451584 | 691.003us | 378.423us | -45.2% | | fp32 | 336 | 65536 | 150.684us | 75.992us | -49.6% | | fp32 | 336 | 262144 | 790.289us | 445.264us | -43.7% | | fp32 | 336 | 451584 | 1377.399us | 790.632us | -42.6% | 保护 case:fp16 A=84 未切 901000,仍走 900000;fp16/fp32 A=512 也仍走 900000。复测 fp16 A=84,B=262144,PR 36.475us、master 36.238us,差异约 0.7%,属于 900000 路径测量波动。 结论:901000 当前切换条件下,命中 case 无回退;fp16 A=84 和 A=512 风险点被条件挡回主线。 #### 911000 small-AB1 模板 覆盖条件:fusedALen_ > 0 && fusedB1Len_ > 0 && fusedALen_ * fusedB1Len_ <= 32 && fusedB0Len_ >= numBlocks * 2。本轮额外构造 96 个真实命中 911000 的 dense case,覆盖 fp16/fp32、NCHW/ND/NCDHW,B0/N 覆盖 128/256/512/1024,A/C 覆盖 2/4/8/16,B1 覆盖 2/4,A * B1 覆盖 4/8/16/32。PR 侧 96/96 全部命中 911000,master 对照全部为 910000,未发现回退。 | dtype | format | case数 | delta范围 | 平均delta | |---|---:|---:|---:|---:| | fp16 | NCHW | 28 | -75.7% ~ -24.0% | -58.7% | | fp16 | ND | 10 | -70.3% ~ -37.4% | -56.7% | | fp16 | NCDHW | 10 | -70.4% ~ -42.1% | -58.0% | | fp32 | NCHW | 28 | -64.7% ~ -13.5% | -41.8% | | fp32 | ND | 10 | -53.0% ~ -15.0% | -40.3% | | fp32 | NCDHW | 10 | -56.4% ~ -17.8% | -39.9% | 最小收益 case: | dtype | format | shape参数 | master 910000 | PR 911000 | delta | |---|---|---|---:|---:|---:| | fp32 | NCHW | N=1024,C=8,B1=4 | 5.609us | 4.851us | -13.5% | | fp32 | ND | N=512,C=8,B1=4 | 5.111us | 4.344us | -15.0% | | fp32 | NCHW | N=512,C=8,B1=4 | 5.137us | 4.273us | -16.8% | 结论:911000 在新增覆盖范围内没有性能回退;fp16 平均收益约 56.7%~58.7%,fp32 平均收益约 39.9%~41.8%。 ### BatchNorm(非 V3)950 TTK -b=release 性能复测 按 batch_norm(非 batch_norm_v3)补充在远程 Ascend 950 环境上的 TTK release binary 路径复测,主线性能直接使用系统 CANN 包,自定义性能使用本 PR 代码编出的 cann-ops-nn-custom_linux-x86_64.run 安装包。 测试命令关键参数: bash python3 -m ttk kernel \ -i tmp_cases/batch_norm_pr4611_perf_cases.csv \ -d=false -s=false -c=false -b=release \ --golden-mode Disable --warmup true --run 20 \ --device-whitelist=0 --pc=1 结果文件: - 主线 CANN 包:tmp_cases/bn_pr4611_mainline_bin_20260605_075523.csv - 自定义 run 包:tmp_cases/bn_pr4611_custom_bin_shim_20260605_082714.csv - 逐条对比:tmp_cases/bn_pr4611_perf_compare_20260605_082714.csv 状态:主线与自定义均为 127/127 PASS,precision_status 与 memory_oob_status 均为 127/127 PASS。 | 模板场景 | case数 | 主线 key | 自定义 key | delta min | delta avg | delta max | >3%回退 | |---|---:|---|---|---:|---:|---:|---:| | 902000_small_last_channel | 16 | 900000 | 902000 | -71.40% | -32.02% | +2.62% | 0 | | 901000_continuous_a | 15 | 900000 | 901000 | -64.27% | -53.20% | -42.56% | 0 | | 911000_small_ab1 | 96 | 910000 | 911000 | -75.57% | -54.19% | -13.68% | 0 | 唯一正向 delta 为 902000_small_last_channel 中的 `bn_lc_nchw_float32_a_ See merge request: cann/ops-nn!4611 | 1 个月前 | |
test: 为 activation 和 norm 算子补充ut测试 Co-authored-by: cai-chengchao<caichengchao1@h-partners.com> # message auto-generated for no-merge-commit merge: !4312 merge ut_0427 into master test: 为 activation 和 norm 算子补充ut测试 Created-by: cai-chengchao Commit-by: cai-chengchao Merged-by: cann-robot Description: ## 描述 为 activation 和 norm 目录下的算子补充ut测试,提升测试覆盖率。包括 op_kernel 和 op_host 两级测试,覆盖多种数据类型和不同维度组合。 ### 改动详情 **新增测试文件统计**: - op_kernel 级别 APT 测试:15 个算子,共 45 个文件(CMakeLists.txt、tiling_def.h、test_*.apt.cpp) - op_host 级别 infershape 测试:3 个算子,共 3 个文件 **涉及的算子**: activation 目录: - confusion_softmax_grad - log_softmax_grad - log_softmax_v2 - softmax_grad - softmax_v2 norm 目录: - add_rms_norm_dynamic_mx_quant - batch_norm - group_norm_v2 - layer_norm - layer_norm_v3 - lp_norm_v2 - rms_norm_dynamic_mx_quant - rms_norm_quant_v2 - sync_batch_norm_backward_elemt - sync_batch_norm_backward_reduce - sync_batch_norm_gather_stats **其他改动**: - 修复 infershape 文件缺失头文件:为 add_rms_norm_dynamic_mx_quant 和 rms_norm_quant_v2 的 infershape 文件添加 #include <algorithm> 头文件引用 **测试覆盖**: - op_kernel APT 测试:覆盖 float、half 等数据类型,不同 tiling key 场景(如 small_r、recompute、ara 等) - op_host infershape 测试:覆盖 DT_FLOAT16、DT_FLOAT、DT_BF16 数据类型,多种维度组合(1D-8D),包括动态 shape(-2) ## 关联的Issue 关联 Issue #2452 ## 测试 所有新增测试文件均可通过编译和执行: - CI Pipeline 已通过验证 - 测试用例覆盖多种数据类型和 shape 组合 ## 文档更新 无文档更新 ## 类型标签 - [ ] Bug修复 - [x] 新特性 - [ ] 性能优化 - [ ] 文档更新 - [ ] 其他,请描述: See merge request: cann/ops-nn!4312 | 3 个月前 | |
format cpp Co-authored-by: yang-di52<yangdi52@huawei.com> # message auto-generated for no-merge-commit merge: !6784 merge issue_fix into master format cpp Created-by: yang-di52 Commit-by: yang-di52 Merged-by: cann-robot Description: ## 描述 批量刷新cpp代码格式 ## 关联的Issue [#3791](https://gitcode.com/cann/ops-nn/issues/3791) ## 测试 <!--描述进行了哪些测试来验证你的改动。--> ## 文档更新 <!--如果这个PR包含文档的更新,请在这里指出。例如:更新了README.md文件。--> ## 类型标签 <!-- [x] 表示选中 --> - [ ] Bug修复 - [ ] 新特性 - [ ] 性能优化 - [ ] 文档更新 - [x] 其他,请描述:代码格式化 ## AI/Agent生成声明 <!-- [x] 表示选中 --> - [ ] AI辅助编写 See merge request: cann/ops-nn!6784 | 30 天前 |
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 个月前 | ||
| 3 个月前 | ||
| 30 天前 |