FilesLast commitLast update
README

Cube SpMM 1D

Operator Overview

cube_spmm_1d uses the Cube computing unit to implement multiplication of a sparse matrix and a dense matrix:

C = A × B
A: M × K, finite FP16
B: K × N, finite FP16
C: M × N, FP32

The native sparse format is vertical vector-CSR with d=16: each non-zero vector corresponds to 16 consecutive rows in the same column. Preparation APIs for standard host-side CSR arrays are also provided, and the converted data uses the same device computation and adaptive configuration rules. This operator coexists with spmm and cube_spmm and does not provide transposition, alpha/beta scaling, batching, or arbitrary leading dimensions.

Product support

Product Source Code Support
Atlas A2 products (Ascend 910B) Supported, arch22 / DAV-2201
Atlas A3 products (Ascend 910_93) Supported, arch22 / DAV-2201
Ascend 950PR&950DT products Not supported; no arch35 implementation
Ascend 310P Not supported; no arch20 implementation

The preceding support scope is described by the source code and SOC build selection. Currently, on-board verification covers only Ascend910B3, which does not mean that other models have completed board testing or have the same performance. Builds that do not select arch22 neither compile nor export this API family. On Ascend 950PR&950DT products, you cannot call these APIs based only on the declarations in the public header. For the matching requirements between the software environment and the source code version, refer to the environment preparation instructions in the repository README.

Interfaces and Invocation Sequence

The public declarations are located in include/cann_ops_sparse.h. Link libops_sparse.so and the CANN runtime library. Each API returns aclsparseStatus_t.

API Function
aclsparseCubeSpmm1dCreatePlan Creates and uploads immutable A from host-side vector-CSR
aclsparseCubeSpmm1dCreateCsrPlan Converts from standard host-side CSR and prepares the same plan
aclsparseCubeSpmm1dGetPlanInfo Queries logical/physical sizes, configuration, and memory requirements
aclsparseCubeSpmm1dExecute Pre-zeros C on the caller's stream and computes asynchronously
aclsparseCubeSpmm1dDestroyPlan Releases the plan and device A after all executions complete

The invocation process:

  1. Initialize ACL, set the device/context, create a stream and the public aclsparse handle, and bind the stream.
  2. Select a preparation API, and pass in the host arrays and a plan output variable initialized to empty.
  3. Call GetPlanInfo, and allocate B/C/workspace by the returned physical byte counts.
  4. Fill B with the paddedN row stride and pad trailing columns with zeros; call Execute.
  5. Queue subsequent operations on the same stream, or have the caller synchronize the stream and then read the logical result.
  6. After all related executions complete, destroy the plan in the creation context, and then clean up the remaining resources.

The preparation APIs copy the input, allocate and upload device A, and the host arrays can be released after the call returns. A is fixed during the plan lifecycle; modifying the original array does not update the plan, and updating A requires re-preparation. B can be changed per execution. The plan, B, C, and workspace must remain alive until the work on the related stream completes.

Plan Options and Query Fields

options == NULL uses structure-adaptive configuration. The meanings of the fields of explicit options are as follows:

aclsparseCubeSpmm1dOptions Field Requirement
structSize sizeof(aclsparseCubeSpmm1dOptions)
tilingKey -1 means adaptive; a non-negative value explicitly selects a compiled valid configuration
reserved Must be 0
workspaceLimitBytes 0 means no additional limit; a non-zero value is the upper limit in bytes for the device workspace

Adaptive selection determines the configuration and padding based on sizes, the sparse row-block structure, and the number of runtime cores. Specific fusion selections retain the device and core count conditions; the calibration rules of a specific model cannot be unconditionally applied to all platforms.

aclsparseCubeSpmm1dPlanInfo Field Meaning
m / n / k Original logical M/N/K
paddedM / paddedN M/N actually used by device storage
tilingKey / blockDim / deviceId Plan configuration, task block count, and the device ID at preparation
vectors Number P of non-zero vectors in the device vector-CSR
bBytes Device byte count of FP16 row-major [K,paddedN]
cBytes Device byte count of FP32 column-major [paddedM,paddedN]
workspaceBytes Device workspace byte count that the caller should provide
persistentABytes Total byte count of the device A array held by the plan, excluding B/C/workspace

All byte-count fields are in bytes. Always use the query results, and do not replace them with logical sizes or self-derived keys.

API Details

aclsparseCubeSpmm1dCreatePlan

aclsparseStatus_t aclsparseCubeSpmm1dCreatePlan(
    aclsparseHandle_t handle, int32_t m, int32_t n, int32_t k,
    const int32_t *hostOffsets, size_t offsetCount,
    const int32_t *hostColumns, const uint16_t *hostValues, size_t vectors,
    const aclsparseCubeSpmm1dOptions *options, aclsparseCubeSpmm1dPlan_t *plan);

Function: create a plan from host-native vector-CSR and upload A.

Parameter Direction/Location Description
handle IN, Host Valid public library handle with the current ACL device/context set
m / n / k IN, Host Logical sizes, all positive; M does not exceed INT32_MAX-15 and is a multiple of 16
hostOffsets IN, Host Non-empty int32_t[M/16+1] row-block pointers
offsetCount IN, Host Must be M/16+1
hostColumns IN, Host int32_t[P] column indexes; can be empty when P is 0
hostValues IN, Host uint16_t[16*P], storing finite IEEE binary16 bit patterns; can be empty when P is 0
vectors IN, Host P, must not exceed INT32_MAX/16
options IN, Host Nullable plan options, see the preceding table
plan OUT, Host Non-empty output address; *plan must be empty before the call; the caller destroys it after success

The first item of the row pointers is 0, monotonically non-decreasing, and the last item equals P. The column indexes in each row block strictly increase and are located in [0,K), and adjacent row pointers can be equal to represent empty row blocks. The 16 values of each vector are stored consecutively. The caller must guarantee the actual capacity of each array, and the lengths and endpoints must be consistent.

aclsparseCubeSpmm1dCreateCsrPlan

aclsparseStatus_t aclsparseCubeSpmm1dCreateCsrPlan(
    aclsparseHandle_t handle, int32_t m, int32_t n, int32_t k,
    const int64_t *hostOffsets, size_t offsetCount,
    const int64_t *hostColumns, const uint16_t *hostValues, size_t nnz,
    const aclsparseCubeSpmm1dOptions *options, aclsparseCubeSpmm1dPlan_t *plan);

Function: normalize and convert standard host-side CSR arrays to d=16 vector-CSR, and create the same device plan.

Parameter Direction/Location Description
handle IN, Host Same as the native preparation API
m / n / k IN, Host Positive logical sizes; M does not exceed INT32_MAX-15 and does not need to be a multiple of 16
hostOffsets IN, Host Non-empty int64_t[M+1] CSR row pointers
offsetCount IN, Host Must be M+1
hostColumns IN, Host int64_t[nnz] column indexes; can be empty when nnz is 0
hostValues IN, Host uint16_t[nnz], finite FP16 bit patterns; can be empty when nnz is 0
nnz IN, Host Number of CSR elements actually stored, including duplicates and explicit zeros
options IN, Host Same as the native preparation API
plan OUT, Host Non-empty output address; *plan must be empty before the call

All indexes start from 0. The first item of the row pointers is 0, monotonically non-decreasing, the last item is nnz, and the column indexes are located in [0,K). Unordered columns, duplicates, explicit zeros, and empty rows are allowed. The conversion steps are:

  1. Sort stably by column, and sum duplicates in the original input order using FP64.
  2. Requantize the result to FP16 with RNE, and remove exact zeros and quantized zeros; non-finite numbers or sums beyond the finite FP16 range are rejected.
  3. Take the union of columns for every 16 rows and pad zeros, and round M up to 16 to obtain the actual device vector-CSR.

Requantization after merging duplicates may produce different rounding results from per-item CSR multiply-add and is not lossless for all duplicates. The column union of a general CSR increases storage, and after conversion P likewise must not exceed INT32_MAX/16; preprocessing may fail due to insufficient range, memory, or workspace budget, and converting an ordinary sparse pattern does not guarantee performance gains.

aclsparseCubeSpmm1dGetPlanInfo

aclsparseStatus_t aclsparseCubeSpmm1dGetPlanInfo(
    aclsparseCubeSpmm1dPlan_t plan, aclsparseCubeSpmm1dPlanInfo *info);

Function: write the plan fields into the caller's host structure without starting device tasks.

Parameter Direction/Location Description
plan IN, Host Non-empty plan that is successfully prepared and not yet destroyed
info OUT, Host Non-empty output structure address; see "Plan Options and Query Fields" for the fields

On success, ACL_SPARSE_STATUS_SUCCESS is returned; if the plan or output parameter is empty, ACL_SPARSE_STATUS_INVALID_VALUE is returned.

aclsparseCubeSpmm1dExecute

aclsparseStatus_t aclsparseCubeSpmm1dExecute(
    aclsparseHandle_t handle, aclsparseCubeSpmm1dPlan_t plan,
    const void *b, size_t bBytes, void *c, size_t cBytes,
    void *workspace, size_t workspaceBytes);

Function: zero the complete physical C on the stream currently bound to the handle, and then asynchronously submit C=A×B.

Parameter Direction/Location Description
handle IN, Host Valid public handle bound to the stream that the caller wants to use
plan IN, Host Plan successfully prepared and still valid in the current device/context
b IN, Device Finite FP16, row-major [K,paddedN], 32-byte aligned, non-empty
bBytes IN, Host Actual available capacity of B, at least info.bBytes
c OUT, Device FP32, column-major [paddedM,paddedN], 32-byte aligned, non-empty
cBytes IN, Host Actual available capacity of C, at least info.cBytes
workspace IN/OUT, Device Non-empty workspace, 32-byte aligned
workspaceBytes IN, Host Actual available capacity of the workspace, at least info.workspaceBytes
  • The element position of B is B[i*paddedN+j], and the trailing columns with j >= N must be zero. B can be changed per execution, and the caller guarantees that all values are finite each time.
  • The element position of C is C[j*paddedM+i]. The logical result takes only the original M×N, empty rows and the padding area are zero, and the old values of C are not preserved.
  • B/C/workspace are located on the device/context at plan preparation, must not overlap each other, and must not overlap the A held by the plan.
  • Execute does not allocate device memory and does not perform tensor H2D/D2H. A successful return means that the task has been submitted, not that the device has completed it.
  • The caller synchronizes the stream to check asynchronous errors. Even if a subsequent submission fails, you must ensure that the queued work completes before releasing the related resources.
  • The immutable plan is reusable. Parallel calls need independent writable C/workspace, and you must ensure the stream dependency order by yourself.

aclsparseCubeSpmm1dDestroyPlan

aclsparseStatus_t aclsparseCubeSpmm1dDestroyPlan(aclsparseCubeSpmm1dPlan_t plan);

Function: release the plan and its device A. The caller's B/C/workspace is not released, and the stream is not implicitly waited on.

plan is a host input handle, and destroying an empty plan is a successful no-op. A non-empty plan must be destroyed in the creation context, and the caller must first complete all device work that uses it. After successful destruction, you must not query, execute, or destroy the same non-empty handle again. Errors such as context mismatch return ACL_SPARSE_STATUS_INVALID_VALUE. If the release of device A fails, ACL_SPARSE_STATUS_EXECUTION_FAILED is returned and the plan retains the unreleased buffer; the caller must retain the original handle and retry DestroyPlan in the creation context, and the released buffers are not released again. After destruction first starts, you must not query or execute the plan again; the handle becomes invalid only after destruction fully succeeds.

Returns and Caller Responsibilities

Status Code Typical Meaning
ACL_SPARSE_STATUS_SUCCESS Preparation/query/destroy succeeded, or the execution was successfully submitted
ACL_SPARSE_STATUS_HANDLE_IS_NULLPTR An API that requires a handle receives an empty handle
ACL_SPARSE_STATUS_INVALID_VALUE Invalid sizes, arrays/options, configuration key, alignment, alias, or context, and so on
ACL_SPARSE_STATUS_NOT_INITIALIZED No valid ACL context is obtained at preparation
ACL_SPARSE_STATUS_ALLOC_FAILED Host/device A allocation or upload fails
ACL_SPARSE_STATUS_INSUFFICIENT_RESOURCES Padding/size exceeds the limit, configuration or workspace budget is not satisfied, execution capacity is insufficient, and so on
ACL_SPARSE_STATUS_EXECUTION_FAILED Runtime operations such as device capability query, output zeroing, task submission, or device A release fail
ACL_SPARSE_STATUS_ARCH_MISMATCH The current SoC does not support this 1D Cube SpMM path
ACL_SPARSE_STATUS_INTERNAL_ERROR Unexpected host exception or internal state error

The preceding are common statuses, and the caller should check the return value of each API and ACL call. The actual address availability in parameters, array capacity, finiteness and padding of device B, and cross-stream lifecycle are still guaranteed by the caller and cannot be replaced by non-empty checks.

Build, Tests, and Examples

For environment configuration, building/installation, CSV/GTest tests, configuration verification, and timing boundaries, see Test and Run Instructions. For a standalone C example, see cube_spmm_1d_csr.c. At runtime, you must deploy libops_sparse.so together with its adjacent cube_spmm_1d_kernels/ device file directory.