已开启
Feat: 新增面向 arch22 的 aclblasSspr 接口 #333
Feat: 新增面向 arch22 的 aclblasSspr 接口 #333
已开启
guodong54_创建于 6 天前
guodong54_
guodong54_
6 天前

描述

新增面向 arch22 的 aclblasSspr 接口、Host 端参数校验与核函数分发,以及 Device Kernel 实现。

该接口实现单精度对称 packed rank-1 update:

AP ← AP + α × x × xᵀ

其中 AP 支持 upper 和 lower packed storage,输入向量支持连续、非单位步长及负步长。

实现中按 packed column 划分多核所有权:每个 Vector Core 独占一段 packed columns,因此不同 core 不会写入同一 AP 元素,无需 atomic 操作。

特殊设计

1. 分路径的 packed-column 工作量模型

packed matrix 的第 j 列长度随 j 改变,因此仅按 packed element 数量均分时,各 core 的实际执行时间并不均衡。

实际工作量还包含:

每列初始化和 x-column 读取开销
+ AP packed element 更新量
+ AP 分段搬运次数

因此实现使用如下加权模型划分 column 边界:

cost =
    column_count × column_setup_cost
  + packed_element_count
  + transfer_count × transfer_cost

实验中发现,不同数据路径的固定开销不同,统一的 partition 参数会造成明显的尾部负载不均衡。因此保留以下分路径配置:

upper triangular + contiguous vector:
    grain = 4096
    column cost = 2048
    transfer cost = 4096

non-unit or signed stride:
    grain = 2048
    column cost = 2048
    transfer cost = 4096

lower triangular + contiguous vector:
    保留默认参数

该设计不改变每个 core 独占 packed columns 的并发安全性,只调整各 core 的 column 边界。

在 Ascend 910B3 上,upper triangular contiguous path 的实测结果如下:

n Before After
4096 282.0 microseconds 83.2 microseconds
8192 465.2 microseconds 145.7 microseconds
16384 1680.3 microseconds 1104.9 microseconds

对于非单位和负步长输入,调整 partition grain 后也观察到改善:

incx Before After
2 574.6 microseconds 299.9 microseconds
3 597.2 microseconds 323.7 microseconds
-7 609.3 microseconds 335.9 microseconds

上述收益来自多核 packed-column ownership 的负载均衡修正,而不是单条 Vector 指令或流水线的加速。

2. 连续与 strided vector 的 canonical representation

对于连续输入,kernel 按需缓存当前 column 和 row tile 中的 x 数据。

对于非单位或负步长输入,Host 端先按逻辑向量语义归一化负步长基址;Device Kernel 仅搬运逻辑向量所覆盖的准确 physical span,再在 Unified Buffer 内完成 gather 和 compact,生成连续的 logical x representation。

该路径避免:

  • 将负步长直接解释为非法的反向 Global Memory 区间;
  • 对 caller-owned Global Memory 的 span 外读取;
  • 每次 rank-1 update 都通过 scalar Global Memory gather 访问 x。

3. AP 分段流水线实验结论

实现保留 AP input queue 和 AP output queue 的深度为 1,即每个 packed segment 按以下顺序完成:

read AP segment from Global Memory
→ compute AP update
→ write AP segment back to Global Memory
→ process next AP segment

曾实现并测试仅针对 packed matrix 的双槽 prime/pump/drain pipeline。该版本通过全部正确性测试,但由于 Unified Buffer 容量限制,AP segment 从 8192 floats 缩小为 4096 floats,导致搬运段数翻倍。

在 large lower triangular cases 中,pipeline version 反而变慢:

Case Depth-1 path AP pipeline
n = 16384 1127.1 microseconds 1273.8 microseconds
n = 16385 1135.0 microseconds 1277.2 microseconds
n = 24576 2441.7 microseconds 2769.2 microseconds

因此未保留该流水线。当前实现优先保持更大的 AP transaction grain,并通过 weighted partition 改善多核尾部负载。

关联的Issue

测试

在 Ascend 910B3、CANN 9.0 环境完成 arch22 SSPR 公共测试:

bash build.sh --ops=spr --run --device=<device>

最终结果:

53/53 tests passed
exit code 0

覆盖范围包括:

  • upper 和 lower packed storage;
  • 连续、非单位步长和负步长输入;
  • 非 2 次幂矩阵维度;
  • 小尺寸、单核和多核执行场景;
  • 参数校验和边界输入。

文档更新

类型标签

likedislike
合并受阻
guodong54_guodong54_
6 天前 创建了 pull request,commit f28a1e78
atomgit-bot
atomgit-bot
6 天前 评论:

变更摘要

本 PR 新增面向 arch22 的 aclblasSspr 接口,用于实现单精度对称 packed rank-1 update(AP ← AP + α × x × xᵀ),覆盖 upper/lower packed 存储以及连续、非单位步长、负步长输入向量。改动包含 Host 端参数校验与 tiling 计算、Device Kernel 分路径实现与多核加权 column 划分,以及 NPU 测试封装和 CSV 驱动的 gtest 校验。

主要改动

  • 新增 Host 端接口与参数校验: sspr_host.cpp 新增 aclblasSspr,完成 handle/n/uplo/incx/alpha/x/ap 校验,并对 n == 0alpha == 0 提前返回;根据 incxn 计算 useNumBlocks,填充 SsprTilingData 后调用 sspr_kernel_do 分发核函数。
  • Device Kernel 分路径实现: sspr_kernel.cpp 新增 sspr_kernel/sspr_kernel_do,对 incx == 1UnifiedContiguousColumns,对非单位或负步长走 CachedStridedColumns(由 CanonicalizeStridedRange 在 Unified Buffer 内 gather/compact 生成逻辑 x),超大 stride 或不可规范化时回退到 ScalarColumns
  • 多核加权 column 划分: 新增 ColumnWorkPrefix/PartitionTarget/WeightedColumnBoundary,以 kCostPerColumnkCostPerElementkCostPerTransfer 构成的成本模型划分各 AIV core 独占的 packed columns 区间,使各 core 不写同一 AP 元素从而无需 atomic。
  • 新增 tiling 结构与 UB 配置常量: sspr_tiling_data.h 定义 SsprTilingData(含 numThreads/columnsPerBlock/n/uplo/alpha/incx)以及 UB_X_FLOATSUB_THRESHOLD 常量。
  • 新增 NPU 测试与正确性校验: sspr_npu_wrapper.h 提供 aclblasSspr_npu,封装设备内存分配、H2D/D2H 与同步;sspr_test.cpp 通过 CSV 驱动 gtest 用例与 CPU golden 对比,并在 alpha == 0 时采用精确校验。
likedislike
atomgit-bot
atomgit-bot
6 天前 评论:

代码审查

我已完成对全部 6 个变更文件的逐一审查。

审查结论汇总

文件 审查结果
blas/spr/arch22/sspr_host.cpp 无独立问题(numThreads/columnsPerBlock 的计算冗余已并入 tiling_data 的 P3 清理项)
blas/spr/arch22/sspr_kernel.cpp 1 个 P2:CachedStridedColumns 列数超限静默返回导致整块漏算
blas/spr/arch22/sspr_tiling_data.h 1 个 P3:未使用/误导性的 tiling 字段与 UB 阈值常量
test/spr/arch22/sspr_npu_wrapper.h 1 个 P2:对 INT_MINstd::abs 触发未定义行为
test/spr/arch22/sspr_test.cpp 无问题(局部 xLen/absIncx 未使用属纯噪音,未上报)
test/spr/arch22/sspr_test.csv 无问题

按优先级统计:P2 × 2,P3 × 1,P0/P1 × 0。

整体风险判断:核函数的核心计算逻辑(packed 上/下三角、正/负/非单位步长、加权列划分、UB 流水线)经逐段核对基本正确,与 CPU golden(cblas_sspr 的 Fortran 负步长约定)及 makeBlasStrided 布局一致;53/53 测试通过也佐证了这一点。主要风险集中在两处静默/未定义行为:一是 stride 快路径在极端列划分(AIV 核数 ≤ 5 等场景)下会静默漏算整块数据(P2,真实环境大概率不触发,属潜在正确性隐患);二是测试 wrapper 对 INT_MIN 取绝对值触发 UB(P2,仅影响测试代码)。整体属于可合入但建议修复上述 P2 项的新功能代码。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
CANN-robotCANN-robot成员
6 天前 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
6 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
6 天前 评论:

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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/ops-blas ❌ (0/2)(You can also ask: 尹祺然, 张浩, 唐超力, zizhongchen, 宋恺) ❌ (0/1)(You can also ask: 宋恺, 尹祺然, 唐超力, zizhongchen, 张浩)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robot成员
6 天前 评论:

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
6 天前 将chenbinbin199309,luowen203_gg123,QK_25415,MaskHunter2008,songkai111,xutianze,wangzitao_leo,nino888,zhaoyingchao2,demoauguste,zizhongchen设为评审人
CANN-robotCANN-robot成员
6 天前 将chenbinbin199309,luowen203_gg123,QK_25415,MaskHunter2008,songkai111,xutianze,wangzitao_leo,nino888,zhaoyingchao2,demoauguste,zizhongchen设为审查人
guodong54_
guodong54_
6 天前 评论:

/check-cla

likedislike
CANN-robotCANN-robot成员
6 天前 删除了label:cann-cla/yes
CANN-robotCANN-robot成员
6 天前 添加了label:cann-cla/yes
guodong54_
guodong54_
6 天前 评论:

/compile

likedislike
CANN-robot
CANN-robot成员
6 天前 评论:

CLA检查已通过,详情可参考这里

likedislike
CANN-robotCANN-robot成员
6 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
6 天前 评论:

流水线任务触发成功
任务链接 [ddbb761cfe2a43cabea110e92a0472c9][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86_A2 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A2 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5 ✅ SUCCESS >>>>> >>>>>
codecheck ❌ FAILED >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_A2_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A2_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
codecheck_codestyle ⚠️ WARNING >>>>>
precommit ⚠️ WARNING >>>>> >>>>>
likedislike
CANN-robotCANN-robot成员
6 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
6 天前 添加了label:ci-pipeline-failed
atomgit-bot
atomgit-bot6 天前进行代码检视1
test/spr/arch22/sspr_test.cpp
@@ -0,0 +37,4 @@
37+ const auto& p = GetParam();
38+ 
39+ int absIncx = std::abs(p.incx);
40+ size_t xLen = (p.n > 0) ? static_cast<size_t>((p.n - 1) * absIncx + 1) : 0;
atomgit-bot
atomgit-bot6 天前评论:

🟡 Medium Priority

建议:删除未使用的 absIncx/xLen(第 39-40 行),或改为先判断 p.incx == INT_MIN 再计算,并将 (p.n - 1) * absIncx + 1 提升为 64 位运算避免有符号溢出。

likedislike
guodong54_
guodong54_
6 天前 评论:

/compile

likedislike
CANN-robotCANN-robot成员
6 天前 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
6 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
6 天前 评论:

流水线任务触发成功
任务链接 [579ee22dd82a41978e32cf30c775148a][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86_A2 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A2 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5 ✅ SUCCESS >>>>> >>>>>
codecheck ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_A2_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A2_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
codecheck_codestyle ⚠️ WARNING >>>>>
precommit ⚠️ WARNING >>>>> >>>>>
PreSmoke_A900_npupool ✅ SUCCESS >>>>>

[2026-08-14 16:47:47]    CI执行结束

likedislike
CANN-robotCANN-robot成员
5 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
5 天前 添加了label:ci-pipeline-passed