已合并
optimize the performace of aicpu ClipByValueV2 operator #2460
ZhaiPeiChao创建于 4月25日
optimize the performace of aicpu ClipByValueV2 operator #2460
已合并
ZhaiPeiChao创建于 4月25日
ZhaiPeiChao
ZhaiPeiChao
4月25日

描述

本 PR 对 AICPU 的 ClipByValueV2 算子进行了全面的性能优化,通过重构核心计算逻辑、优化并行调度策略、分离标量/逐元素边界处理路径,显著提升了算子执行效率。

主要改动

  1. 核心计算函数重构: 将原有的函数指针调用模式重构为专用的内联函数(ClampScalar、ClampComplex),使用 if constexpr 和类型特征在编译期选择最优路径,消除了运行时开销。
  2. 并行调度优化: 将并行阈值从基于元素数量(256K/32K)改为基于字节数(200KB/40KB),新增统一的 DispatchParallel 调度函数,优化小数据量的串行回退策略。
  3. 分支路径分离: 区分标量边界和逐元素边界两种场景,为每种场景提供专用的 kernel 函数(KernelScalarBound、KernelElemBound 等),标量场景预加载边界值到寄存器。
  4. 编译器优化提示: 大量使用 attribute((always_inline))、attribute((hot))、restrict、__builtin_expect 等编译器提示,优化指令级并行和分支预测。
  5. 类型分发重构: 将 std::map 函数指针分发改为 switch-case 结构,利用编译器优化和更紧凑的代码布局。

关联的Issue

issue #1380

测试

  • 高频 fp32 scalar-bounds 中等规模(64K):T=4 7.5× / T=6 5.7×
  • 整型 elem-bounds(1M int32):T=6 1.37×
  • int8 scalar-bounds:T=1 1.29× / T=4 1.22×
  • 单线程普遍:1.1–1.4×(向量化 + 分支外提);
  • 大张量 DRAM 带宽饱和场景:0.93–0.97×(噪声范围)。

T=1(单线程,反映纯算法/访存/向量化收益)

case old(ms) new(ms) speedup old(GB/s) new(GB/s) equal
fp32 small 1K scalar-bounds 0.0130 0.0110 1.18× 0.60 0.71 Y
fp32 small 1K elem-bounds 0.0120 0.0130 0.92× 1.30 1.20 Y
fp32 64K scalar-bounds 0.4419 0.3173 1.39× 1.19 1.65 Y
fp32 256K elem-bounds 2.1340 2.2015 0.97× 1.96 1.91 Y
int32 256K scalar-bounds 0.9320 0.8534 1.09× 2.25 2.46 Y
int32 1M elem-bounds 8.7056 7.8415 1.11× 1.93 2.14 Y
int8 1M scalar-bounds 2.2670 1.7555 1.29× 0.92 1.19 Y
fp32 16M elem-bounds 162.6 175.2 0.93× 1.65 1.53 Y

T=4

case old(ms) new(ms) speedup new(GB/s) equal
fp32 64K scalar-bounds 0.4513 0.0600 7.52× 27.3 Y
fp32 256K elem-bounds 2.1500 1.6860 1.28× 2.49 Y
int32 1M elem-bounds 8.6700 8.4860 1.02× 1.98 Y
int8 1M scalar-bounds 2.2500 1.8450 1.22× 1.13 Y

T=6

case old(ms) new(ms) speedup new(GB/s) equal
fp32 64K scalar-bounds 0.4450 0.0776 5.73× 21.1 Y
int32 1M elem-bounds 8.6820 6.3410 1.37× 2.65 Y
int8 1M scalar-bounds 2.2700 1.9800 1.15× 1.06 Y

文档更新

不涉及

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 ZhaiPeiChao 的贡献)
ZhaiPeiChaoZhaiPeiChao
4月25日 创建了 pull request,commit 96c972cb
CANN-robot
CANN-robot成员
4月25日 评论:

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
conversion/clip_by_value_v2 唐燕峰, 张瑜翔 (2/2) 唐燕峰, 张瑜翔 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
4月25日 添加了label:cann-cla/yes
CANN-robotCANN-robot成员
4月25日 将FelixTang7,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,songkai111,zhangyuxiang0119,llimwang,gubaocheng设为评审人
CANN-robotCANN-robot成员
4月25日 将FelixTang7,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,songkai111,zhangyuxiang0119,llimwang,gubaocheng设为审查人
此处折叠了49条消息 查看更多
CANN-robot
CANN-robot成员
5月11日 评论:

The following users do not have permission to comment /lgtm or /approve on any module in this PR:
於欣洁

likedislike
CANN-robotCANN-robot成员
5月11日 添加了label:lgtm
CANN-robot
CANN-robot成员
5月11日 评论:

Review Guide

This pull-request passes review.
Committers who wrote a comment of /approve are: 唐燕峰, 张瑜翔.
Reviewers who wrote a comment of /lgtm are: 唐燕峰, 张瑜翔.

likedislike
CANN-robotCANN-robot成员
5月11日 关闭了关联的issue
CANN-robotCANN-robot成员
5月11日 合入了pull request