CANN/ops-sparse稀疏算子模板库

发布时间:2026/7/5 20:15:06
CANN/ops-sparse稀疏算子模板库 【免费下载链接】ops-sparse本项目是CANN提供的高性能稀疏矩阵计算的算子库专注于优化稀疏矩阵的计算效率。项目地址: https://gitcode.com/cann/ops-sparsename: sparse-op-templates description: ops-sparse 算子代码模板库提供不同编程模型下的标准化代码骨架。根据目标芯片架构和编程模型选择对应模板。Sparse 算子代码模板库使用方法根据目标芯片架构arch20/arch22/arch35和编程模型SIMD/RegBase/SIMT选择对应的模板文件templates/ ├── simd/ # 所有架构通用默认首选 │ ├── op_tiling_data.h # TilingData 结构体模板 │ ├── op_kernel.h # kernel.h 签名模板 │ ├── op_kernel.cpp # Kernel 实现模板 │ └── op_host.cpp # Host 侧实现模板 ├── regbase/ # 仅 arch35Ascend950可用 │ ├── op_tiling_data.h # 含 UB buffer 大小字段 │ ├── op_kernel.h # kernel.h 签名模板 │ ├── op_kernel.cpp # __VEC_SCOPE__ RegTensor 模板 │ └── op_host.cpp # 含 BufferLayout 预计算模板 └── simt/ # 仅 arch35Ascend950可用 ├── op_tiling_data.h # 含 nthreads 字段 ├── op_kernel.h # kernel.h 签名模板 ├── op_kernel.cpp # __simt_vf__ asc_vf_call 模板 └── op_host.cpp # 含 nthreads 计算模板{op_name}.h公共头文件的内容描述符转换函数见本文描述符转换函数章节直接内联在算子头文件中无需独立模板文件。RegBase 模板与 SIMD 模板的关键区别使用TBuf LocalTensor非TQue、__VEC_SCOPE__块内RegTensor寄存器级 API、host 侧预计算 UB buffer 大小。SIMT 模板与 SIMD/RegBase 模板的关键区别使用__simt_vf__装饰器、asc_vf_call调度、grid-stride loop线程级并行、无TPipe/TQue/DataCopyPad直接__gm__指针访问。编程模型与架构对应关系架构支持的编程模型说明arch22Ascend910B/910_93SIMD仅支持向量编程模型arch20Ascend310PSIMD仅支持向量编程模型arch35Ascend950SIMD / RegBase / SIMT三种模型均可由架构师在 1.3.A 决策架构无关性SIMD 编程模型在所有架构上可用是默认选择Kernel 侧代码的架构差异由 AscendC 编译器屏蔽RegBase 和 SIMT仅 Ascend950 (arch35) 可用有独立模板目录templates/regbase/、templates/simt/开发时直接参考对应模板扩展不需额外技能模板文件说明1. {op_name}.h - 公共头文件包含类型映射宏、描述符转换函数、公共常量。2. {op_name}_tiling_data.h - TilingData 结构体定义 Host/Kernel 共享的 Tiling 参数结构体。3. {op_name}_kernel.h - Kernel 头文件声明kernel_do函数签名和 Kernel 类声明。4. {op_name}_host.cpp - Host 侧实现包含参数校验 Kernel launch 的完整骨架。公共宏定义来自现有代码以下是仓内已有的公共宏新算子必须复用禁止重新定义#define GM_ADDR uint8_t * #define CHECK_RET(cond, return_expr) \ do { if (!(cond)) { return_expr; } } while (0)描述符转换函数来自 aclsparse_descr_internal.h新算子应#include aclsparse_descr_internal.h使用内部结构体并在本地头文件{op_name}.h中定义转换函数inline struct aclsparseContext *ToInternalHandle(aclsparseHandle_t handle) { return reinterpret_caststruct aclsparseContext *(handle); } inline struct aclsparseSpMatDescr *ToMatInner(aclsparseConstSpMatDescr_t desc) { return const_caststruct aclsparseSpMatDescr *( reinterpret_castconst struct aclsparseSpMatDescr *(desc)); } inline struct aclsparseDnVecDescr *ToVecInner(aclsparseConstDnVecDescr_t desc) { return const_caststruct aclsparseDnVecDescr *( reinterpret_castconst struct aclsparseDnVecDescr *(desc)); } inline struct aclsparseDnMatDescr *ToDnMatInner(aclsparseConstDnMatDescr_t desc) { return const_caststruct aclsparseDnMatDescr *( reinterpret_castconst struct aclsparseDnMatDescr *(desc)); }kernel_do 签名模式// {op_name}_kernel.h #ifndef {OP_NAME}_KERNEL_H_ #define {OP_NAME}_KERNEL_H_ #include {op_name}_tiling_data.h #define GM_ADDR uint8_t * // kernel_do 签名 // - 数据指针按算子需求填写GM_ADDR // - tiling 以 const 引用传入 // - numBlocks stream 为通用参数 void {op_name}_kernel_do( GM_ADDR dataPtr1, GM_ADDR dataPtr2, ..., uint32_t numBlocks, const {Op}TilingData tiling, void *stream); #endifGeneric API 算子若需要 alpha/beta/computeType 参数在 kernel_do 中按算子需求追加如const void *alpha, const void *beta并在 host 侧从描述符或接口参数中提取。Host 骨架模式// {op_name}_host.cpp // 禁止冗余 includeacl/acl.h、cann_ops_sparse_common.h、tiling/platform/platform_ascendc.h 均为冗余 #include cstdint #include new #include log/log.h #include cann_ops_sparse.h #include {op_name}.h #include {op_name}_kernel.h #include aclsparse_handle_internal.h #include aclsparse_descr_internal.h #include host_utils.h namespace { static aclsparseStatus_t Validate{Op}Params( aclsparseHandle_t handle, ...) { // 参数校验 - 每个参数 nullptr/value 检查 } static aclsparseStatus_t Launch{Op}Kernel( aclsparseHandle_t handle, ...) { // 描述符解析 - TilingData 计算 - kernel_do launch } } // namespace extern C { aclsparseStatus_t aclsparse{Op}( aclsparseHandle_t handle, ...) { aclsparseStatus_t status Validate{Op}Params(handle, ...); if (status ! ACL_SPARSE_STATUS_SUCCESS) return status; return Launch{Op}Kernel(handle, ...); } }【免费下载链接】ops-sparse本项目是CANN提供的高性能稀疏矩阵计算的算子库专注于优化稀疏矩阵的计算效率。项目地址: https://gitcode.com/cann/ops-sparse创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考