| Files | Last commit | Last update |
|---|---|---|
SpMM Operator
Operator Overview
The SpMM (Sparse Matrix - Dense Matrix Multiplication) operator implements the multiplication of a sparse matrix and a dense matrix. The core operation is C = alpha * op(A) * op(B) + beta * C, where A is a sparse matrix in CSR format, and B and C are dense matrices.
Mathematical expression:
C = alpha * op(A) * op(B) + beta * C
Here op(A) is the operation of sparse matrix A (transpose/non-transpose), and op(B) is the operation of dense matrix B.
The invocation flow consists of three steps:
- GetBufferSize: queries the required workspace size
- Preprocess (optional): preprocesses the sparse matrix (row reordering + binning) to accelerate subsequent computation; when omitted, the first SpMM builds it automatically
- SpMM: executes C = alpha * op(A) * op(B) + beta * C
It includes the following interfaces:
| Interface | Brief Function |
|---|---|
| aclsparseSpMMGetBufferSize | Queries the workspace size (in bytes) required by SpMM |
| aclsparseSpMMPreprocess | Preprocesses the sparse matrix (row reordering + binning) |
| aclsparseSpMM | Executes the sparse matrix-dense matrix multiplication C = alpha * op(A) * op(B) + beta * C |
Operator Execution Interface
aclsparseSpMMGetBufferSize
Product support
- Ascend 950PR&950DT products: Supported
- Atlas A3 products: Supported
- Atlas A2 products: Supported
Function prototype
aclsparseStatus_t aclsparseSpMMGetBufferSize(aclsparseHandle_t handle, aclsparseOperation_t opA, aclsparseOperation_t opB, const void *alpha, aclsparseConstSpMatDescr_t matA, aclsparseConstDnMatDescr_t matB, const void *beta, aclsparseDnMatDescr_t matC, aclDataType computeType, aclsparseSpMMAlg_t alg, size_t *size)
Parameter description
| Parameter | Input/Output | Parameter Type | Description |
|---|---|---|---|
| handle | Input | aclsparseHandle_t | ops-sparse library context handle, carrying a stream, Host memory |
| opA | Input | aclsparseOperation_t | Operation type of sparse matrix A; supports non-transpose, transpose, and conjugate transpose; CSR_ALG3 supports only non-transpose; Host memory |
| opB | Input | aclsparseOperation_t | Operation type of dense matrix B; supports non-transpose, transpose, and conjugate transpose under valid algorithm combinations; Host memory |
| alpha | Input | const void* | Scalar alpha pointer; the type must match computeType. The memory location is controlled by aclsparseSetPointerMode; Host/Device memory |
| matA | Input | aclsparseConstSpMatDescr_t | Descriptor of sparse matrix A; only CSR format is supported; Host memory |
| matB | Input | aclsparseConstDnMatDescr_t | Descriptor of dense matrix B; Host memory |
| beta | Input | const void* | Scalar beta pointer; the type must match computeType. The memory location is controlled by aclsparseSetPointerMode; Host/Device memory |
| matC | Input/Output | aclsparseDnMatDescr_t | Descriptor of dense matrix C; Host memory |
| computeType | Input | aclDataType | Computation precision type; use ACL_FLOAT for real numbers and ACL_COMPLEX64 for complex numbers; the INT8 combination of Ascend 950PR&950DT products uses ACL_INT32; Host memory |
| alg | Input | aclsparseSpMMAlg_t | Algorithm type; Atlas A2 products and Atlas A3 products support DEFAULT, CSR_ALG1, CSR_ALG2, CSR_ALG3, and FP32_HIGH_PRECISION; Host memory; different algorithms select the corresponding layout/deterministic paths |
| size | Output | size_t* | Outputs the required workspace size (in bytes); Host memory |
Constraints
- handle cannot be nullptr
- matA, matB, and matC cannot be nullptr
- matA supports only CSR format (
ACL_SPARSE_FORMAT_CSR) - The row offset and column index types of matA must both be
ACL_SPARSE_INDEX_32I, and the two types must be identical
Ascend 950PR&950DT products
- The index base of matA supports
ACL_SPARSE_INDEX_BASE_ZEROandACL_SPARSE_INDEX_BASE_ONE - opA supports non-transpose, transpose, and conjugate transpose; CSR_ALG3 supports only non-transpose
- opB supports
ACL_SPARSE_OP_NON_TRANSPOSE,ACL_SPARSE_OP_TRANSPOSE, andACL_SPARSE_OP_CONJUGATE_TRANSPOSE; CSR_ALG3 does not support conjugate transpose - The following data type combinations are supported:
- matA=ACL_FLOAT, matB=ACL_FLOAT, matC=ACL_FLOAT, computeType=ACL_FLOAT
- matA=ACL_FLOAT16, matB=ACL_FLOAT16, matC=ACL_FLOAT16, computeType=ACL_FLOAT
- matA=ACL_FLOAT16, matB=ACL_FLOAT16, matC=ACL_FLOAT, computeType=ACL_FLOAT
- matA=ACL_BF16, matB=ACL_BF16, matC=ACL_BF16, computeType=ACL_FLOAT
- matA=ACL_BF16, matB=ACL_BF16, matC=ACL_FLOAT, computeType=ACL_FLOAT
- matA=ACL_COMPLEX64, matB=ACL_COMPLEX64, matC=ACL_COMPLEX64, computeType=ACL_COMPLEX64
- matA=ACL_INT8, matB=ACL_INT8, matC=ACL_INT32, computeType=ACL_INT32
- Assume that the logical shape of op(A) is
m x kand the shape of C ism x n:- When
opB=ACL_SPARSE_OP_NON_TRANSPOSE, the shape of the B descriptor isk x n; - When
opB=ACL_SPARSE_OP_TRANSPOSE, the shape of the B descriptor isn x k; - For a row-major matrix,
ldis no smaller than the number of physical columns; for a column-major matrix,ldis no smaller than the number of physical rows.
- When
- Zero-dimension input: when any of m / k / n (matA.rows / matA.cols / matC.cols) is 0,
ACL_SPARSE_STATUS_INVALID_VALUEis returned - When opA is transpose or conjugate transpose, the logical shape is the number of columns of the original A multiplied by the number of rows; dimension validation and workspace size are both computed according to the logical shape.
- Transpose preprocessing saves the permutation relationship between the CSR structure and the original values, and reads the current values during execution; the Complex64 conjugate transpose also takes the complex conjugate.
- Non-zero-dimension matrices allow
nnz=0and empty rows.
Atlas A2 products and Atlas A3 products
- The matA index base of Atlas A2 products and Atlas A3 products supports
ACL_SPARSE_INDEX_BASE_ZEROandACL_SPARSE_INDEX_BASE_ONE - The opA/opB of Atlas A2 products and Atlas A3 products support
ACL_SPARSE_OP_NON_TRANSPOSE,ACL_SPARSE_OP_TRANSPOSE, andACL_SPARSE_OP_CONJUGATE_TRANSPOSE - The B/C of Atlas A2 products and Atlas A3 products support row-major and column-major orders; for row-major,
ld >= max(1, cols), and for column-major,ld >= max(1, rows) - Atlas A2 products and Atlas A3 products support the following four data type combinations:
- matA=ACL_FLOAT, matB=ACL_FLOAT, matC=ACL_FLOAT, computeType=ACL_FLOAT
- matA=ACL_FLOAT16, matB=ACL_FLOAT16, matC=ACL_FLOAT16, computeType=ACL_FLOAT
- matA=ACL_BF16, matB=ACL_BF16, matC=ACL_BF16, computeType=ACL_FLOAT
- matA=ACL_COMPLEX64, matB=ACL_COMPLEX64, matC=ACL_COMPLEX64, computeType=ACL_COMPLEX64
- Dimension matching is validated according to the logical shape after opA/opB
- All of M/K/N are allowed to be 0; zero-length output returns success directly, and when K=0 the result follows
C = beta * C - The
CSR_ALG1of Atlas A2 products and Atlas A3 products uses the layout-aware general path and is preferred for column-major B/C;CSR_ALG2uses the row-major fast path for compact row-major N/N combinations and falls back to the general path for other valid layouts;CSR_ALG3uses the fixed original row order, requires opA to be non-transpose, and requires opB not to be conjugate transpose - The
FP32_HIGH_PRECISIONof Atlas A2 products and Atlas A3 products supports only fp32 and uses Kahan compensated summation - size cannot be nullptr
Supported sparse formats
| Format | Supported | Description |
|---|---|---|
| CSR | ✅ | Sparse matrix A supports the CSR format |
| COO | ❌ | Not supported |
| CSC | ❌ | Not supported |
aclsparseSpMMPreprocess
Product support
- Ascend 950PR&950DT products: Supported
- Atlas A3 products: Supported
- Atlas A2 products: Supported
Function prototype
aclsparseStatus_t aclsparseSpMMPreprocess(aclsparseHandle_t handle, aclsparseOperation_t opA, aclsparseOperation_t opB, const void *alpha, aclsparseConstSpMatDescr_t matA, aclsparseConstDnMatDescr_t matB, const void *beta, aclsparseDnMatDescr_t matC, aclDataType computeType, aclsparseSpMMAlg_t alg, void *buffer)
Parameter description
| Parameter | Input/Output | Parameter Type | Description |
|---|---|---|---|
| handle | Input | aclsparseHandle_t | ops-sparse library context handle, carrying a stream, Host memory |
| opA | Input | aclsparseOperation_t | Operation type of sparse matrix A; supports non-transpose, transpose, and conjugate transpose; CSR_ALG3 supports only non-transpose; Host memory |
| opB | Input | aclsparseOperation_t | Operation type of dense matrix B; supports non-transpose, transpose, and conjugate transpose under valid algorithm combinations; Host memory |
| alpha | Input | const void* | Scalar alpha pointer. The memory location is controlled by aclsparseSetPointerMode; Host/Device memory |
| matA | Input | aclsparseConstSpMatDescr_t | Descriptor of sparse matrix A; only CSR format is supported; Host memory |
| matB | Input | aclsparseConstDnMatDescr_t | Descriptor of dense matrix B; Host memory |
| beta | Input | const void* | Scalar beta pointer. The memory location is controlled by aclsparseSetPointerMode; Host/Device memory |
| matC | Input/Output | aclsparseDnMatDescr_t | Descriptor of dense matrix C; Host memory |
| computeType | Input | aclDataType | Computation precision type; use ACL_FLOAT for real numbers and ACL_COMPLEX64 for complex numbers; the INT8 combination of Ascend 950PR&950DT products uses ACL_INT32; Host memory |
| alg | Input | aclsparseSpMMAlg_t | Algorithm type; Host memory |
| buffer | Input | void* | workspace buffer (allocated with the size returned by GetBufferSize), Device memory |
Constraints
- Same constraints as aclsparseSpMMGetBufferSize
- buffer cannot be nullptr and must be allocated with the size returned by GetBufferSize
Supported sparse formats
| Format | Supported | Description |
|---|---|---|
| CSR | ✅ | Sparse matrix A supports the CSR format |
| COO | ❌ | Not supported |
| CSC | ❌ | Not supported |
aclsparseSpMM
Product support
- Ascend 950PR&950DT products: Supported
- Atlas A3 products: Supported
- Atlas A2 products: Supported
Function prototype
aclsparseStatus_t aclsparseSpMM(aclsparseHandle_t handle, aclsparseOperation_t opA, aclsparseOperation_t opB, const void *alpha, aclsparseConstSpMatDescr_t matA, aclsparseConstDnMatDescr_t matB, const void *beta, aclsparseDnMatDescr_t matC, aclDataType computeType, aclsparseSpMMAlg_t alg, void *buffer)
Parameter description
| Parameter | Input/Output | Parameter Type | Description |
|---|---|---|---|
| handle | Input | aclsparseHandle_t | ops-sparse library context handle, carrying a stream, Host memory |
| opA | Input | aclsparseOperation_t | Operation type of sparse matrix A; supports non-transpose, transpose, and conjugate transpose; CSR_ALG3 supports only non-transpose; Host memory |
| opB | Input | aclsparseOperation_t | Operation type of dense matrix B; supports non-transpose, transpose, and conjugate transpose under valid algorithm combinations; Host memory |
| alpha | Input | const void* | Scalar alpha pointer. The memory location is controlled by aclsparseSetPointerMode; Host/Device memory |
| matA | Input | aclsparseConstSpMatDescr_t | Descriptor of sparse matrix A; only CSR format is supported; Host memory |
| matB | Input | aclsparseConstDnMatDescr_t | Descriptor of dense matrix B; Host memory |
| beta | Input | const void* | Scalar beta pointer. The memory location is controlled by aclsparseSetPointerMode; Host/Device memory |
| matC | Input/Output | aclsparseDnMatDescr_t | Descriptor of dense matrix C; Host memory |
| computeType | Input | aclDataType | Computation precision type; use ACL_FLOAT for real numbers and ACL_COMPLEX64 for complex numbers; the INT8 combination of Ascend 950PR&950DT products uses ACL_INT32; Host memory |
| alg | Input | aclsparseSpMMAlg_t | Algorithm type; Host memory |
| buffer | Input | void* | workspace buffer (allocated with the size returned by GetBufferSize), Device memory |
Constraints
- Same constraints as aclsparseSpMMGetBufferSize
- buffer cannot be nullptr and must be allocated with the size returned by GetBufferSize
- Atlas A2 products and Atlas A3 products must call
aclsparseSpMMPreprocessfor preprocessing before the call - For Ascend 950PR&950DT products,
aclsparseSpMMPreprocessis an optional step; direct execution for the first time builds the workspace - Atlas A2 products and Atlas A3 products support DEFAULT, CSR_ALG1, CSR_ALG2, CSR_ALG3, and FP32_HIGH_PRECISION; the algorithm participates in the host-side path selection, and the operation restrictions of CSR_ALG3 are described in the GetBufferSize constraints
- The
ACL_SPARSE_SPMM_CSR_FP32_HIGH_PRECISION_ALGof Atlas A2 products and Atlas A3 products supports only fp32 and uses Kahan compensated summation
Supported sparse formats
| Format | Supported | Description |
|---|---|---|
| CSR | ✅ | Sparse matrix A supports the CSR format |
| COO | ❌ | Not supported |
| CSC | ❌ | Not supported |
Invocation example
The sample code is as follows and is for reference only. For the specific compilation and execution process, refer to Compilation and Running Sample.
#include <cstdio>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "cann_ops_sparse.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
class AclContext {
public:
explicit AclContext(int32_t deviceId) : deviceId_(deviceId) {}
~AclContext()
{
if (stream_ != nullptr) {
aclrtDestroyStream(stream_);
stream_ = nullptr;
}
if (deviceSet_) {
aclrtResetDevice(deviceId_);
deviceSet_ = false;
}
if (aclInited_) {
aclFinalize();
aclInited_ = false;
}
}
int Init()
{
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
aclInited_ = true;
ret = aclrtSetDevice(deviceId_);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
deviceSet_ = true;
ret = aclrtCreateStream(&stream_);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return ACL_SUCCESS;
}
aclrtStream Stream() const { return stream_; }
private:
int32_t deviceId_;
aclrtStream stream_ = nullptr;
bool aclInited_ = false;
bool deviceSet_ = false;
};
// Helper: allocates Device memory and copies Host data
static void* AllocAndCopyDevice(const void *hostPtr, size_t sizeBytes)
{
void *dPtr = nullptr;
aclrtMalloc(&dPtr, sizeBytes, ACL_MEM_MALLOC_HUGE_FIRST);
if (hostPtr != nullptr && sizeBytes > 0) {
aclrtMemcpy(dPtr, sizeBytes, hostPtr, sizeBytes, ACL_MEMCPY_HOST_TO_DEVICE);
}
return dPtr;
}
int aclsparseSpMMTest(AclContext& ctx)
{
aclrtStream stream = ctx.Stream();
// 1. Create the ops-sparse handle
aclsparseHandle_t rawHandle = nullptr;
auto sparseRet = aclsparseCreate(&rawHandle);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreate failed. ERROR: %d\n", sparseRet);
return sparseRet);
std::unique_ptr<aclsparseContext, aclsparseStatus_t (*)(aclsparseHandle_t)> handlePtr(rawHandle, aclsparseDestroy);
sparseRet = aclsparseSetStream(static_cast<aclsparseHandle_t>(handlePtr.get()), stream);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseSetStream failed. ERROR: %d\n", sparseRet);
return sparseRet);
// 2. Set PointerMode
sparseRet = aclsparseSetPointerMode(static_cast<aclsparseHandle_t>(handlePtr.get()), ACL_SPARSE_POINTER_MODE_HOST);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseSetPointerMode failed. ERROR: %d\n", sparseRet);
return sparseRet);
// 3. Prepare the Host-side CSR data
// A (4x3, nnz=5): B (3x2):
// [1.0 0.0 2.0] [1.0 0.0]
// [0.0 3.0 0.0] [0.0 1.0]
// [4.0 0.0 0.0] [1.0 1.0]
// [0.0 0.0 5.0]
//
// C = 1.0 * A * B (4x2):
// C[0] = [3.0, 2.0]
// C[1] = [0.0, 3.0]
// C[2] = [4.0, 0.0]
// C[3] = [5.0, 5.0]
int64_t m = 4, k = 3, n = 2;
int64_t nnzA = 5;
float hAlpha = 1.0f;
float hBeta = 0.0f;
std::vector<int> hRowPtrA = {0, 2, 3, 4, 5};
std::vector<int> hColIndA = {0, 2, 1, 0, 2};
std::vector<float> hValA = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
// B: row-major 3x2
int64_t ldb = n, ldc = n;
aclsparseOrder_t orderB = ACL_SPARSE_ORDER_ROW;
aclsparseOrder_t orderC = ACL_SPARSE_ORDER_ROW;
std::vector<float> hB(static_cast<size_t>(k) * n, 0.0f);
hB[0 * n + 0] = 1.0f; hB[0 * n + 1] = 0.0f;
hB[1 * n + 0] = 0.0f; hB[1 * n + 1] = 1.0f;
hB[2 * n + 0] = 1.0f; hB[2 * n + 1] = 1.0f;
std::vector<float> hC(static_cast<size_t>(m) * n, 0.0f);
// 4. Copy the data to the Device
void *dRowPtrA = AllocAndCopyDevice(hRowPtrA.data(), (m + 1) * sizeof(int));
void *dColIndA = AllocAndCopyDevice(hColIndA.data(), nnzA * sizeof(int));
void *dValA = AllocAndCopyDevice(hValA.data(), nnzA * sizeof(float));
void *dB = AllocAndCopyDevice(hB.data(), static_cast<size_t>(k) * n * sizeof(float));
void *dC = AllocAndCopyDevice(hC.data(), static_cast<size_t>(m) * n * sizeof(float));
// 5. Create the descriptors
aclsparseSpMatDescr_t matA = nullptr;
sparseRet = aclsparseCreateCsr(&matA, m, k, nnzA, dRowPtrA, dColIndA, dValA,
ACL_SPARSE_INDEX_32I, ACL_SPARSE_INDEX_32I,
ACL_SPARSE_INDEX_BASE_ZERO, ACL_FLOAT);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreateCsr failed. ERROR: %d\n", sparseRet);
return sparseRet);
aclsparseDnMatDescr_t matB = nullptr, matC = nullptr;
sparseRet = aclsparseCreateDnMat(&matB, k, n, ldb, dB, ACL_FLOAT, orderB);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreateDnMat B failed. ERROR: %d\n", sparseRet);
return sparseRet);
sparseRet = aclsparseCreateDnMat(&matC, m, n, ldc, dC, ACL_FLOAT, orderC);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreateDnMat C failed. ERROR: %d\n", sparseRet);
return sparseRet);
// 6. Step 1 — GetBufferSize
size_t bufferSize = 0;
sparseRet = aclsparseSpMMGetBufferSize(
static_cast<aclsparseHandle_t>(handlePtr.get()),
ACL_SPARSE_OP_NON_TRANSPOSE, ACL_SPARSE_OP_NON_TRANSPOSE,
&hAlpha, matA, matB, &hBeta, matC, ACL_FLOAT,
ACL_SPARSE_SPMM_CSR_ALG1, &bufferSize);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("SpMMGetBufferSize failed. ERROR: %d\n", sparseRet);
return sparseRet);
LOG_PRINT("bufferSize = %zu bytes\n", bufferSize);
void *dBuffer = nullptr;
auto aclRet = aclrtMalloc(&dBuffer, bufferSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(aclRet == ACL_SUCCESS, LOG_PRINT("aclrtMalloc for buffer failed. ERROR: %d\n", aclRet); return aclRet);
// 7. Step 2 — Preprocess
sparseRet = aclsparseSpMMPreprocess(
static_cast<aclsparseHandle_t>(handlePtr.get()),
ACL_SPARSE_OP_NON_TRANSPOSE, ACL_SPARSE_OP_NON_TRANSPOSE,
&hAlpha, matA, matB, &hBeta, matC, ACL_FLOAT,
ACL_SPARSE_SPMM_CSR_ALG1, dBuffer);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("SpMMPreprocess failed. ERROR: %d\n", sparseRet);
return sparseRet);
// 8. Step 3 — SpMM
sparseRet = aclsparseSpMM(
static_cast<aclsparseHandle_t>(handlePtr.get()),
ACL_SPARSE_OP_NON_TRANSPOSE, ACL_SPARSE_OP_NON_TRANSPOSE,
&hAlpha, matA, matB, &hBeta, matC, ACL_FLOAT,
ACL_SPARSE_SPMM_CSR_ALG1, dBuffer);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("SpMM failed. ERROR: %d\n", sparseRet);
return sparseRet);
// 9. Synchronize and wait for the computation to complete
aclRet = aclrtSynchronizeStream(stream);
CHECK_RET(aclRet == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", aclRet); return aclRet);
// 10. Copy the result back to the Host and print it
aclRet = aclrtMemcpy(hC.data(), static_cast<size_t>(m) * n * sizeof(float),
dC, static_cast<size_t>(m) * n * sizeof(float),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(aclRet == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", aclRet);
return aclRet);
for (int64_t i = 0; i < m; i++) {
LOG_PRINT("C[%lld] = %.1f, %.1f\n", static_cast<long long>(i), hC[i * n + 0], hC[i * n + 1]);
}
// 11. Clean up resources
aclsparseDestroySpMat(matA);
aclsparseDestroyDnMat(matB);
aclsparseDestroyDnMat(matC);
if (dRowPtrA) aclrtFree(dRowPtrA);
if (dColIndA) aclrtFree(dColIndA);
if (dValA) aclrtFree(dValA);
if (dB) aclrtFree(dB);
if (dC) aclrtFree(dC);
if (dBuffer) aclrtFree(dBuffer);
return ACL_SPARSE_STATUS_SUCCESS;
}
int main()
{
AclContext ctx(0);
auto ret = ctx.Init();
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclsparseSpMMTest(ctx);
CHECK_RET(ret == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseSpMMTest failed. ERROR: %d\n", ret); return ret);
return 0;
}
The expected output is as follows:
bufferSize = 512 bytes
C[0] = 3.0, 2.0
C[1] = 0.0, 3.0
C[2] = 4.0, 0.0
C[3] = 5.0, 5.0
PyTorch/ATen Entry
The torch_extension adaptation is located in sparse/spmm/torch_extension/; it registers the
SparseCsrPrivateUse1 implementation of aten::addmm.out, so that the standard public entry goes through the Dispatcher directly to aclsparseSpMM*,
without CPU backfill:
import torch
import torch_npu
import cann_ops_sparse # Importing completes the registration
out = torch.addmm(bias, csr_mat1, dense_mat2, beta=-0.5, alpha=1.5)
The call chain is torch.addmm → aten::addmm.out → SparseCsrPrivateUse1 implementation →
aclsparseSpMMGetBufferSize/Preprocess/aclsparseSpMM → Ascend C Kernel.
This public entry fixes opA/opB to non-transpose and the index base to 0 (neither PyTorch's sparse_csr_tensor nor
torch.addmm exposes these two controls), supports the four dtypes fp16/bf16/fp32/complex64,
a broadcastable self, and zero-dimension M/K/N. Transpose and 1-based indexing are still available through the raw C++ interfaces above.
For installation, parameters, and constraints, see torch_extension/cann_ops_sparse/docs/zh/spmm.md,
which also describes the known issue that the current torch.sparse.addmm sparse×dense call is shadowed by the SpGEMM registration and requires using
torch.addmm instead.