已合并
optimize the performance of aicpu add operator #2462
ZhaiPeiChao创建于 4月25日
optimize the performance of aicpu add operator #2462
已合并
ZhaiPeiChao创建于 4月25日
ZhaiPeiChao
ZhaiPeiChao
4月25日

描述

AICPU add算子性能优化

fast path 负责 ≥99% 真实工作负载(深度学习里 Add 几乎都是 same-shape 或 scalar-bcast),通用 Eigen 路径只兜底多维非平凡 broadcast。

Compute
 ├─ NormalMathCheck + dtype permission check
 ├─ 显式 raw-rank ≤ 8 校验  (保持与原实现等价的失败语义)
 ├─ Bcast::GenerateBcastInfo
 ├─ 显式 output-shape 一致性校验
 ├─ [fast] 同形 shape  → AddSameShape
 ├─ [fast] scalar bcast → AddScalarBcast
 └─ [fallback] AddGenericBcast → 原 Eigen TensorMap broadcast 路径

Fast path 关键点:

  • 裸指针 + __restrict__ + 定长循环 → 编译器自动 NEON/AVX 向量化 (对应 V / E)。
  • output bytes 阈值动态选择串行 / ParallelFor (对应 T / O10):
    • kParallelBytesThresh = 192 KiB:基于 10 μs ParallelFor 固定调度开销 + ~8 GB/s 单核 add 带宽反推 ~160 KiB,取 20% 余量。
    • kBytesPerShard = 256 KiB:初版用 32 KiB 在 16 MiB 负载上产生 0.67× 回退(shard 数过多导致线程间抢带宽),调到 256 KiB 后恢复至 1.02×。
  • __builtin_expect(rc != OK, 0) 标注冷分支,保持 C++17 合规(不使用 C++20 [[likely]])。
  • 全部 reinterpret_castPtrToPtr<void,T>

关联的Issue

issue #1522

测试

基于4线程进行测试:

形状 dtype case old (μs) new (μs) speedup
SameShape fp32 4 KiB 0.41 0.50 0.82×
SameShape fp32 96 KiB 2.48 2.52 0.99×
SameShape fp32 1 MiB 48.99 18.42 2.66×
SameShape fp32 16 MiB 766 752 1.02×
ScalarBcast fp32 4 KiB 0.43 0.39 1.10×
ScalarBcast fp32 96 KiB 3.02 1.87 1.62×
ScalarBcast fp32 1 MiB 31.7 8.07 3.93×
ScalarBcast fp32 16 MiB 557 520 1.07×
SameShape i64 2 MiB 80 37 2.16×
ScalarBcast i64 8 KiB 0.38 0.14 2.77×
ScalarBcast i64 192 KiB 5.99 2.04 2.94×
ScalarBcast i64 2 MiB 509 187 2.73×

文档更新

不涉及

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 ZhaiPeiChao 的贡献)
ZhaiPeiChaoZhaiPeiChao
4月25日 创建了 pull request,commit ed862471
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
math/add 张瑜翔, 宋凯 (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设为审查人
此处折叠了131条消息 查看更多
songkai111成员
5月25日 评论:

/lgtm
/approve

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

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月25日 关闭了关联的issue
CANN-robotCANN-robot成员
5月25日 合入了pull request