合并受阻
变更摘要
本 PR 为 ops-sparse 的 SpMM 能力增加面向 ATK worker 进程的 ATen 适配层:新增 python_adapter 目录,将 torch.sparse.addmm 在 NPU(SparseCsrPrivateUse1)上的内核注册到 ops-sparse 实现,并通过 sitecustomize.py 实现按需自动加载;同时大幅扩展 arch22 SpMM 内核能力,支持 FP16/BF16/FP32/Complex64 多种数据类型、opA/opB 转置、COL-major 与带 padding 的 ld 布局、1-based 索引及复数 alpha/beta 标量,并在公开头文件新增 ACL_SPARSE_SPMM_CSR_ALG2/ACL_SPARSE_SPMM_CSR_ALG3 两种算法枚举,配套补充了覆盖数据类型、布局、算法和边界场景的测试。
主要改动
-
新增 ATen NPU 适配器:
python_adapter/sparse_addmm_npu.cpp通过TORCH_LIBRARY_IMPL(aten, SparseCsrPrivateUse1, m)注册aten::_sparse_addmm内核(SparseAddmmNpu),将稀疏 CSR 张量与稠密矩阵的乘法转发给 ops-sparse 的aclsparseSpMM*接口,支持 FP16/BF16/FP32/Complex64、复数标量及非连续输入,并在构造输出时始终 clone 以避免原地修改输入。 -
适配库自动构建与加载:新增
python_adapter/build_extension.py(通过torch.utils.cpp_extension.load编译sparse_addmm_npu.cpp并链接libops_sparse.so、libtorch_npu.so)与python_adapter/sitecustomize.py(设置OPS_SPARSE_TORCH_LIBRARY环境变量时在导入测试模块前自动加载 NPU 注册,并校验 dispatch 表确认注册生效)。 -
arch22 内核泛化与转置支持:
sparse/spmm/arch22/spmm_kernel.cpp将原 FP32 专用内核改造为模板化SpmmArch22Kernel<ValT>(FP32/FP16/BF16 实例化),新增SpmmArch22ComplexKernel处理复数计算(含共轭转置);同时支持opA/opB转置、COL-major 存储、带ld的 padded 布局、1-based 索引以及非 FP32 数据类型的 Cast 路径。 -
Host 端校验与 tiling 扩展:
sparse/spmm/arch22/spmm_host.cpp与spmm.h扩展SpmmArch22TilingData(新增dataType、orderB、orderC、opA、opB、indexBase、ldb、ldc、alphaImag、betaImag等字段),支持复数标量读取(ReadScalarComplex64)、更多操作/布局/算法组合校验,并补充 null handle/buffer 与零尺寸矩阵的边界处理。 -
公开接口与测试扩展:
include/cann_ops_sparse.h增加ACL_SPARSE_SPMM_CSR_ALG2(CSR 行定向确定性路径)与ACL_SPARSE_SPMM_CSR_ALG3(CSR 预处理路径,要求opA非转置且opB非共轭转置),并对 ACL 头文件引入ACLSPARSE_ACL_TYPES_PREINCLUDED防重入宏;test/spmm/arch22/spmm_test.cpp新增 FP16/BF16、布局/base/算法组合、Complex64 共轭转置、零nnz与空行等边界用例,并通过SPMM_CASE_INDEX、SPMM_DTYPE_SMOKE、SPMM_LAYOUT_SMOKE、SPMM_COMPLEX_SMOKE、SPMM_BOUNDARY_SMOKE环境变量控制用例选择。


Hi @nannan-2026, welcome to submitting your first PR to ops-sparse!
PR Merge Steps
1. CLA Signing
If the current PR label includes cann-cla/yes, it means you have signed the CLA and can proceed to the next step. If the label includes cann-cla/no, please sign the CLA first. If you have any questions, please refer to the FAQ.
2. CI Check
Please comment /compile to trigger the CI pipeline check. If the CI run is successful, the PR will be tagged with ci-pipeline-passed and you can proceed to the next step. If the CI run fails, the PR will be tagged with ci-pipeline-failed, please check the CI logs to fix the issues in the PR. If you have any questions, please refer to the FAQ.
3. Code Review
After CI passes, please refer to the PR Approval Progress and proactively @ the committers in the table to review the code. After approval, committers will comment /lgtm and /approve. Once the lgtm and approved labels are successfully added, the PR will be merged automatically.


流水线任务触发成功
任务链接 [9fecf3b01d334e8dbc536efa652a6d80][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| SCA | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| Check_Pr | ✅ SUCCESS | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_A2 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_A5 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_A2 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_A5 | ✅ 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 | ✅ SUCCESS | >>>>> | |
| codecheck_style | ✅ SUCCESS | >>>>> | |
| pre-commit | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> |
[2026-08-29 19:06:22] CI执行结束


@songkai111 @wangzitao_leo @MaskHunter2008 @chaotang233 @nino888
各位维护者好,麻烦协助审查 aclsparseSpMM A2/A3 社区任务代码 PR #128。
当前状态:
- CLA 已通过,标签为
cann-cla/yes - 最新 CI 已全部通过,标签为
ci-pipeline-passed - A2/A5、X86/ARM、Ubuntu 24 编译任务均通过
- CodeCheck、CodeCheck Style、SCA、AntiPoison、Pre-commit 均通过
- PreSmoke_A900 已通过
- A2/A3 功能及精度验证已完成
- A2/A3 ATK 精度用例:200/200 通过
- FP16、BF16、FP32、Complex64 已验证
- CSR base、矩阵布局、转置/共轭转置、稀疏边界及 workspace reuse 已覆盖
- 关联设计文档 PR #1113 已合入
代码 PR:
https://gitcode.com/cann/ops-sparse/pull/128
设计文档:
https://gitcode.com/cann/cann-ops-competitions/pull/1113


描述
本 PR 完成 CANN 社区任务“08-12-aclsparseSpMM 算子开发(A2/A3)”的算子功能扩展,完善 Ascend A2、Ascend A3 平台上的 aclsparseSpMM 能力,并增加 PyTorch/ATen NPU
torch.sparse.addmm适配。关联设计文档:
https://gitcode.com/cann/cann-ops-competitions/pull/1113
源代码信息:
feature/aclsparse-spmm-a2a3f385698d7c0b25890f7c3ef6acf24d5b3b933b86改动原因
现有 arch22 aclsparseSpMM 实现的数据类型、矩阵布局、转置方式、稀疏边界处理以及 PyTorch 调用链路尚不完整,无法完整覆盖任务书要求的 A2/A3 功能和测试场景。
本 PR 在现有
ops-sparse目录和接口基础上进行扩展,避免重复创建同名算子,主要解决以下问题:opA、opB转置及共轭转置场景不完整;nnz=0、空行等 CSR 边界场景缺少完整处理;torch.sparse.addmm到 aclsparseSpMM 的调用适配;主要改动
1. 扩展 aclsparseSpMM 接口能力
完善
include/cann_ops_sparse.h及 arch22 SpMM Host、Kernel 侧实现,支持:opA非转置、转置和共轭转置;opB非转置、转置和共轭转置;alpha、beta缩放;beta=0场景;input广播;nnz=0;2. 完善 Host 侧校验与 Tiling
Host 侧增加或完善以下逻辑:
3. 扩展 Ascend C Kernel
Kernel 侧增加或完善以下能力:
alpha * op(A) * op(B) + beta * C计算;beta=0时避免读取无效或非有限输入;nnz=0、空行等边界处理;4. 增加 PyTorch/ATen NPU Adapter
新增
python_adapter,通过 PyTorch PrivateUse1/SparseCsrPrivateUse1 调度机制,将:torch.sparse.addmm(input, mat1, mat2, beta, alpha)映射到:
aclsparseSpMMAdapter 主要包含:
同时增加:
sitecustomize.py自动加载机制;OPS_SPARSE_TORCH_LIBRARY环境变量支持;5. 完善 arch22 SpMM 测试
扩展
test/spmm/arch22/spmm_test.cpp,增加:SPMM_CASE_INDEX单用例选择;opA、opB转置及共轭转置测试;nnz=0边界测试;alpha、beta组合测试;测试程序支持按场景快速回归,避免每次调试都执行全部用例。
关联的Issue
无独立 Issue。
关联社区任务:
08-12-aclsparseSpMM 算子开发(A2/A3)关联设计文档 PR:
https://gitcode.com/cann/cann-ops-competitions/pull/1113
测试
本 PR 已在真实 Ascend A2、Ascend A3 环境中进行构建、功能、精度和调用链路验证。
A2 环境验证
feature/aclsparse-spmm-a2a3;f385698d7c0b25890f7c3ef6acf24d5b3b933b86;libops_sparse.so安装:通过;torch.sparse.addmm调用:通过。专项测试结果:
opA转置及共轭转置:通过;opB转置及共轭转置:通过;nnz=0:通过;A2 ATK 完整精度测试结果:
A2 精度通过率:
100%。A3 环境验证
feature/aclsparse-spmm-a2a3;f385698d7c0b25890f7c3ef6acf24d5b3b933b86;torch.sparse.addmm调用:通过。专项测试结果:
opA、opB转置及共轭转置:通过;nnz=0:通过;A3 ATK 完整精度测试结果:
A3 精度通过率:
100%。A3 性能采集链路预检结果:
Profiler 已成功生成:
op_statistic.csv;kernel_details.csv;上述结果用于确认
torch.sparse.addmm调用实际进入 NPU Kernel。当前 PR 不声明尚未形成正式完整报告的性能结论。构建与代码检查
git diff --check:通过;文档更新
本 PR 新增和更新了以下文档内容:
python_adapter/README.md;OPS_SPARSE_TORCH_LIBRARY环境变量;sitecustomize.py自动加载方法;torch.sparse.addmm的调用和验证方法;完整算子设计文档位于:
https://gitcode.com/cann/cann-ops-competitions/pull/1113
类型标签