* Copyright (c) 2025 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.
*/
* \file aclnn_unique.cpp
* \brief
*/
#include "aclnn_unique.h"
#include "level0/unique_with_counts_and_sorting.h"
#include "index/unique_consecutive/op_host/op_api/unique_consecutive.h"
#include "level0/adjacent_difference.h"
#include "level0/cumsum.h"
#include "level0/sort.h"
#include "index/scatter_elements_v2/op_api/scatter_elements.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/reshape.h"
#include "aclnn/aclnn_base.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/shape_utils.h"
#include "opdev/format_utils.h"
#include "opdev/op_dfx.h"
#include "opdev/op_executor.h"
#include "opdev/op_log.h"
#include "opdev/tensor_view_utils.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "op_api/op_api_def.h"
#include "op_api/aclnn_util.h"
#include "op_api/level2_base.h"
#include "op_api/aclnn_util.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_BOOL, op::DataType::DT_UINT8, op::DataType::DT_INT8, op::DataType::DT_UINT16,
op::DataType::DT_INT16, op::DataType::DT_UINT32, op::DataType::DT_INT32, op::DataType::DT_UINT64,
op::DataType::DT_INT64, op::DataType::DT_DOUBLE, op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16};
static const std::initializer_list<op::DataType> ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_BOOL, op::DataType::DT_UINT8, op::DataType::DT_INT8, op::DataType::DT_UINT16,
op::DataType::DT_INT16, op::DataType::DT_UINT32, op::DataType::DT_INT32, op::DataType::DT_UINT64,
op::DataType::DT_INT64, op::DataType::DT_DOUBLE, op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16,
op::DataType::DT_BF16};
static int64_t GetTensorElementsNum(const aclTensor* tensor)
{
int64_t num = 1;
auto shape = tensor->GetViewShape();
for (size_t i = 0; i < shape.GetDimNum(); i++) {
num *= shape.GetDim(i);
}
return num;
}
static const aclIntArray* GetFlattenShape(const aclTensor* self, aclOpExecutor* executor)
{
int64_t valuePerm[1] = {GetTensorElementsNum(self)};
return executor->AllocIntArray(valuePerm, 1);
}
static bool CheckDtypeValid(const aclTensor* self, const aclTensor* inverseOut)
{
if (Ops::NN::AclnnUtil::IsRegbase()) {
OP_CHECK_DTYPE_NOT_SUPPORT(self, ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST, return false);
} else {
auto supportList = GetDtypeSupportListV1(ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST,
ASCEND910_DTYPE_DTYPE_SUPPORT_LIST);
OP_CHECK_DTYPE_NOT_SUPPORT(self, supportList, return false);
}
OP_CHECK_DTYPE_NOT_MATCH(inverseOut, op::DataType::DT_INT64, return false);
return true;
}
static bool CheckShapeValid(const aclTensor* self, bool returnInverse, const aclTensor* inverseOut)
{
OP_CHECK_MAX_DIM(self, MAX_SUPPORT_DIMS_NUMS, return false);
if (returnInverse) {
OP_CHECK_SHAPE_NOT_EQUAL(self, inverseOut, return false);
}
return true;
}
static aclnnStatus CheckParams(const aclTensor* self, bool returnInverse, aclTensor* valueOut, aclTensor* inverseOut)
{
CHECK_RET(CheckNotNull3Tensor(self, valueOut, inverseOut), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, inverseOut), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShapeValid(self, returnInverse, inverseOut), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static const std::initializer_list<op::DataType> XY_DTYPE_SUPPORT_LIST_ASCEND_REGBASE = {
op::DataType::DT_INT64, op::DataType::DT_INT32, op::DataType::DT_INT16, op::DataType::DT_INT8,
op::DataType::DT_UINT64, op::DataType::DT_UINT32, op::DataType::DT_UINT16, op::DataType::DT_UINT8,
op::DataType::DT_BF16, op::DataType::DT_FLOAT16, op::DataType::DT_FLOAT};
bool SupportAicore4Unique(const aclTensor* self)
{
OP_CHECK(Ops::NN::AclnnUtil::IsRegbase(), OP_LOGW("Aicore Unique only support arch 3510."), return false);
OP_CHECK(CheckType(self->GetDataType(), XY_DTYPE_SUPPORT_LIST_ASCEND_REGBASE),
OP_LOGW("Unsupport input dtype for aicore UniqueConsecutive."), return false);
return true;
}
aclnnStatus ComputeUniqueViaAicore(const aclTensor* selfContiguous, bool returnInverse, aclTensor* valueOut,
aclTensor* inverseOut, aclOpExecutor* executor)
{
constexpr int64_t NONE_N = 1000;
auto flattenShape = GetFlattenShape(selfContiguous, executor);
auto selfFlatten = l0op::Reshape(selfContiguous, flattenShape, executor);
OP_CHECK_NULL(selfFlatten, return ACLNN_ERR_INNER_NULLPTR);
auto indicesType = inverseOut->GetDataType();
auto sortRes = l0op::Sort(selfFlatten, 0, false, true, indicesType, executor);
auto sortedValues = std::get<0>(sortRes);
OP_CHECK_NULL(sortedValues, return ACLNN_ERR_INNER_NULLPTR);
auto sortedIndices = std::get<1>(sortRes);
OP_CHECK_NULL(sortedIndices, return ACLNN_ERR_INNER_NULLPTR);
aclTensor* dummyInverseOut = nullptr;
aclTensor* dummyCountsOut = nullptr;
if (Ops::NN::AclnnUtil::IsRegbase()) {
dummyInverseOut = executor->AllocTensor(inverseOut->GetStorageShape(), inverseOut->GetDataType(),
Format::FORMAT_ND);
dummyCountsOut = executor->AllocTensor(selfContiguous->GetStorageShape(), inverseOut->GetDataType(),
Format::FORMAT_ND);
} else {
dummyInverseOut = executor->AllocTensor(inverseOut->GetStorageShape(), DataType::DT_INT32, Format::FORMAT_ND);
dummyCountsOut = executor->AllocTensor(selfContiguous->GetStorageShape(), DataType::DT_INT32,
Format::FORMAT_ND);
}
auto uniqueConsRet = l0op::UniqueConsecutive(sortedValues, false, false, NONE_N, valueOut, dummyInverseOut,
dummyCountsOut, executor);
CHECK_RET(uniqueConsRet == ACLNN_SUCCESS, uniqueConsRet);
if (returnInverse) {
const aclTensor* dimTensor = nullptr;
int64_t firstDimOf1DTensor = 0;
dimTensor = executor->ConvertToTensor(&firstDimOf1DTensor, 1, DataType::DT_INT64);
auto adjDiff = l0op::AdjacentDifference(sortedValues, indicesType, executor);
auto sumIdx = l0op::Cumsum(adjDiff, dimTensor, executor);
auto newData = executor->AllocTensor(sumIdx->GetViewShape(), sumIdx->GetDataType(), sumIdx->GetViewFormat());
CHECK_RET(newData != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto inverseIdx = l0op::ScatterElements(newData, sortedIndices, sumIdx, 0, "none", executor);
auto inverseIdxReshape = l0op::Reshape(inverseIdx, selfContiguous->GetViewShape(), executor);
OP_CHECK_NULL(inverseIdxReshape, return ACLNN_ERR_INNER_NULLPTR);
const aclTensor* viewCopyInverseIdx = nullptr;
if (Ops::NN::AclnnUtil::IsRegbase()) {
viewCopyInverseIdx = l0op::ViewCopy(inverseIdxReshape, inverseOut, executor);
} else {
auto inverseIdxInt64 = l0op::Cast(inverseIdxReshape, DataType::DT_INT64, executor);
viewCopyInverseIdx = l0op::ViewCopy(inverseIdxInt64, inverseOut, executor);
}
CHECK_RET(viewCopyInverseIdx != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
return ACLNN_SUCCESS;
}
aclnnStatus aclnnUniqueGetWorkspaceSize(const aclTensor* self, bool sorted, bool returnInverse, aclTensor* valueOut,
aclTensor* inverseOut, uint64_t* workspaceSize, aclOpExecutor** executor)
{
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnUnique, DFX_IN(self, sorted, returnInverse), DFX_OUT(valueOut, inverseOut));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, returnInverse, valueOut, inverseOut);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (self->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (returnInverse) {
auto inverseViewShape = inverseOut->GetViewShape();
inverseOut->SetStorageShape(inverseViewShape);
inverseOut->SetOriginalShape(inverseViewShape);
}
if (SupportAicore4Unique(selfContiguous)) {
auto opRet = ComputeUniqueViaAicore(selfContiguous, returnInverse, valueOut, inverseOut, uniqueExecutor.get());
CHECK_RET(opRet == ACLNN_SUCCESS, ACLNN_ERR_INNER_NULLPTR);
} else {
auto opRet = l0op::UniqueWithCountsAndSorting(selfContiguous, sorted, returnInverse, valueOut, inverseOut,
uniqueExecutor.get());
CHECK_RET(opRet == ACLNN_SUCCESS, ACLNN_ERR_INNER_NULLPTR);
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnUnique(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnUnique);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif