* ----------------------------------------------------------------------------------------------------------
* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
* ----------------------------------------------------------------------------------------------------------
*/
#include "cann_ops_sparse.h"
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <new>
#include <string.h>
#include <vector>
#include "unistd.h"
#include "spvv_tiling_data.h"
#include "spvv_kernel.h"
#include "aclsparse_host_utils.h"
#include "aclsparse_descr_internal.h"
#include "tiling/platform/platform_ascendc.h"
static aclsparseStatus_t ValidateSpvvParams(aclsparseHandle_t handle, aclsparseOperation_t op,
aclsparseSpVecDescr *xInner, aclsparseDnVecDescr *yInner, void *result,
aclDataType computeType)
{
if (handle == nullptr) {
fprintf(stderr, "aclsparseSpvv: handle is nullptr\n");
return ACL_SPARSE_STATUS_HANDLE_IS_NULLPTR;
}
if (xInner == nullptr || yInner == nullptr || result == nullptr) {
fprintf(stderr, "aclsparseSpvv: x/y/result must be non-null\n");
return ACL_SPARSE_STATUS_INVALID_VALUE;
}
if ((yInner->nums > 0 && yInner->values == nullptr) ||
(xInner->nnz > 0 && (xInner->indices == nullptr || xInner->values == nullptr))) {
fprintf(stderr, "aclsparseSpvv: nonempty vectors require bound values and sparse indices\n");
return ACL_SPARSE_STATUS_INVALID_VALUE;
}
if (op != ACL_SPARSE_OP_NON_TRANSPOSE) {
fprintf(stderr, "aclsparseSpvv: op %d not supported (only NON_TRANSPOSE)\n",
static_cast<int>(op));
return ACL_SPARSE_STATUS_NOT_SUPPORTED;
}
if (computeType != ACL_FLOAT) {
fprintf(stderr, "aclsparseSpvv: computeType %d not supported (only FP32)\n",
static_cast<int>(computeType));
return ACL_SPARSE_STATUS_NOT_SUPPORTED;
}
if (xInner->idxType != ACL_SPARSE_INDEX_32I) {
fprintf(stderr, "aclsparseSpvv: idxType not supported (only INDEX_32I)\n");
return ACL_SPARSE_STATUS_NOT_SUPPORTED;
}
if (xInner->idxBase != ACL_SPARSE_INDEX_BASE_ZERO) {
fprintf(stderr, "aclsparseSpvv: idxBase not supported (only BASE_ZERO)\n");
return ACL_SPARSE_STATUS_NOT_SUPPORTED;
}
if (xInner->valueType != ACL_FLOAT && xInner->valueType != ACL_FLOAT16) {
fprintf(stderr, "aclsparseSpvv: x valueType %d not supported (FP32/FP16)\n",
static_cast<int>(xInner->valueType));
return ACL_SPARSE_STATUS_NOT_SUPPORTED;
}
if (yInner->valueType != xInner->valueType) {
fprintf(stderr, "aclsparseSpvv: y valueType %d != x valueType %d\n",
static_cast<int>(yInner->valueType), static_cast<int>(xInner->valueType));
return ACL_SPARSE_STATUS_INVALID_VALUE;
}
return ACL_SPARSE_STATUS_SUCCESS;
}
aclsparseStatus_t aclsparseSpvv(aclsparseHandle_t handle, aclsparseOperation_t op,
aclsparseConstSpVecDescr_t x, aclsparseConstDnVecDescr_t y,
void *result, aclDataType computeType)
{
aclsparseSpVecDescr *xInner = const_cast<aclsparseSpVecDescr *>(x);
aclsparseDnVecDescr *yInner = const_cast<aclsparseDnVecDescr *>(y);
aclsparseStatus_t st = ValidateSpvvParams(handle, op, xInner, yInner, result, computeType);
if (st != ACL_SPARSE_STATUS_SUCCESS) {
return st;
}
uint32_t nnz = static_cast<uint32_t>(xInner->nnz);
uint64_t yLen = yInner->nums;
aclrtStream stream = nullptr;
st = aclsparseGetStream(handle, &stream);
if (st != ACL_SPARSE_STATUS_SUCCESS) {
fprintf(stderr, "aclsparseSpvv: aclsparseGetStream failed, st=%d\n",
static_cast<int>(st));
return st;
}
if (nnz == 0) {
CHECK_ACL(aclrtMemsetAsync(result, sizeof(float), 0, sizeof(float), stream));
return ACL_SPARSE_STATUS_SUCCESS;
}
auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance();
if (ascendcPlatform == nullptr) {
fprintf(stderr, "aclsparseSpvv: platform instance is nullptr\n");
return ACL_SPARSE_STATUS_INTERNAL_ERROR;
}
uint32_t coreNum = ascendcPlatform->GetCoreNumAiv();
if (coreNum == 0) {
fprintf(stderr, "aclsparseSpvv: GetCoreNumAiv returned 0\n");
return ACL_SPARSE_STATUS_INTERNAL_ERROR;
}
uint32_t nnzPerCore = (nnz + coreNum - 1) / coreNum;
if (nnzPerCore < SPVV_MIN_NNZ_PER_CORE) {
nnzPerCore = SPVV_MIN_NNZ_PER_CORE;
}
uint32_t blockNum = (nnz + nnzPerCore - 1) / nnzPerCore;
SpvvTilingData tiling{nnz, yLen, nnzPerCore};
CHECK_ACL(aclrtMemsetAsync(result, sizeof(float), 0, sizeof(float), stream));
spvv_kernel_do(reinterpret_cast<GM_ADDR>(xInner->indices),
reinterpret_cast<GM_ADDR>(xInner->values),
reinterpret_cast<GM_ADDR>(yInner->values),
reinterpret_cast<GM_ADDR>(result),
tiling, blockNum, xInner->valueType, stream);
return ACL_SPARSE_STATUS_SUCCESS;
}