* 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.
*/
#include "aclnn_isin.h"
#include "aclnn_kernels/contiguous.h"
#include "equal.h"
#include "math/reduce_any/op_api/reduce_any.h"
#include "math/logical_not/op_api/logical_not.h"
#include "aclnn_kernels/cast.h"
#include "conversion/fill/op_api/fill.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/format_utils.h"
#include "opdev/op_dfx.h"
#include "opdev/op_executor.h"
#include "opdev/op_log.h"
#include "opdev/shape_utils.h"
#include "opdev/tensor_view_utils.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const size_t MAX_DIM_LEN = 8;
static const int64_t ELEMENTS_THRESHOLD = 10;
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_INT8, op::DataType::DT_INT32,
op::DataType::DT_INT64, op::DataType::DT_UINT8,
op::DataType::DT_DOUBLE, op::DataType::DT_INT16};
static const std::initializer_list<op::DataType> ASCEND910B_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_INT8, op::DataType::DT_INT32,
op::DataType::DT_INT64, op::DataType::DT_UINT8, op::DataType::DT_BF16,
op::DataType::DT_DOUBLE, op::DataType::DT_INT16};
static const std::initializer_list<DataType>& GetDtypeSupportList()
{
if (GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E) {
return ASCEND910B_DTYPE_SUPPORT_LIST;
} else {
return ASCEND910_DTYPE_SUPPORT_LIST;
}
}
static inline bool CheckNotNull(const aclScalar* element, const aclTensor* testElements, const aclTensor* out)
{
OP_CHECK_NULL(element, return false);
OP_CHECK_NULL(testElements, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static inline bool CheckDtypeValid(const aclScalar* element, const aclTensor* testElements, const aclTensor* out)
{
const auto& supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(element, supportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(testElements, supportList, return false);
OP_CHECK_DTYPE_NOT_MATCH(out, op::DataType::DT_BOOL, return false);
return true;
}
static inline DataType PromoteTypeScalar(const aclScalar* element, const aclTensor* testElements)
{
if (testElements->Size() >= ELEMENTS_THRESHOLD) {
return op::PromoteType(testElements->GetDataType(), element->GetDataType());
}
if (IsFloatingType(testElements->GetDataType())) {
return testElements->GetDataType();
}
if (IsFloatingType(element->GetDataType())) {
return op::PromoteType(testElements->GetDataType(), element->GetDataType());
}
return testElements->GetDataType();
}
static inline bool CheckPromoteType(const aclScalar* element, const aclTensor* testElements)
{
auto promoteType = PromoteTypeScalar(element, testElements);
if (promoteType == DataType::DT_UNDEFINED) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "Expected element %s and testElements %s can promote dtype but check failed.",
op::ToString(element->GetDataType()).GetString(), op::ToString(testElements->GetDataType()).GetString());
return false;
}
OP_CHECK_RESULT_DTYPE_CAST_FAILED(element->GetDataType(), promoteType, return false);
OP_CHECK_RESULT_DTYPE_CAST_FAILED(testElements->GetDataType(), promoteType, return false);
return true;
}
static inline bool CheckShape(const aclTensor* testElements, const aclTensor* out)
{
OP_CHECK_MAX_DIM(testElements, MAX_DIM_LEN, return false);
OP_CHECK_WRONG_DIMENSION(out, 0, return false);
return true;
}
static inline aclnnStatus CheckParams(const aclScalar* element, const aclTensor* testElements, const aclTensor* out)
{
CHECK_RET(CheckNotNull(element, testElements, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(element, testElements, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckPromoteType(element, testElements), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(testElements, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static inline aclnnStatus FillScalar(aclTensor* out, bool val, aclOpExecutor* executor)
{
FVector<int64_t> shape;
shape.push_back(1);
auto dims = executor->ConvertToTensor(shape.data(), shape.size(), DataType::DT_INT64);
auto shapeArray = executor->AllocIntArray(shape.data(), shape.size());
FVector<bool> valVector = {val};
auto valTensor = executor->ConvertToTensor(valVector.data(), valVector.size(), out->GetDataType());
auto fillOut = l0op::Fill(dims, valTensor, shapeArray, executor);
CHECK_RET(fillOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(fillOut, out, executor);
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
return ACLNN_SUCCESS;
}
static const aclIntArray* GetAllDims(const aclTensor* tensor, aclOpExecutor* executor)
{
const size_t input_dim_num = tensor->GetViewShape().GetDimNum();
std::vector<int64_t> dims(input_dim_num);
for (size_t idx = 0; idx < input_dim_num; idx++) {
dims[idx] = idx;
}
return executor->AllocIntArray(dims.data(), input_dim_num);
}
aclnnStatus aclnnIsInScalarTensorGetWorkspaceSize(
const aclScalar* element, const aclTensor* testElements, bool assumeUnique, bool invert, aclTensor* out,
uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnIsInScalarTensor, DFX_IN(element, testElements, assumeUnique, invert), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(element, testElements, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (testElements->IsEmpty() ||
(IsFloatingType(element->GetDataType()) && element->ToFloat() != element->ToFloat())) {
ret = FillScalar(out, invert, uniqueExecutor.get());
if (ret == ACLNN_SUCCESS) {
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
}
return ret;
}
if (testElements->GetStorageFormat() != Format::FORMAT_ND) {
OP_LOGW("Format only support ND");
}
auto testElementsContiguous = l0op::Contiguous(testElements, uniqueExecutor.get());
CHECK_RET(testElementsContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto promoteType = PromoteTypeScalar(element, testElements);
auto testElementsCasted = l0op::Cast(testElementsContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(testElementsCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor* elementTensor = uniqueExecutor.get()->ConvertToTensor(element, promoteType);
CHECK_RET(elementTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto equalResult = l0op::Equal(testElementsCasted, elementTensor, uniqueExecutor.get());
CHECK_RET(equalResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto dim = GetAllDims(equalResult, uniqueExecutor.get());
CHECK_RET(dim != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto anyResult = l0op::ReduceAny(equalResult, dim, false, uniqueExecutor.get());
CHECK_RET(anyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto result = invert ? l0op::LogicalNot(anyResult, uniqueExecutor.get()) : anyResult;
CHECK_RET(result != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyMinResult = l0op::ViewCopy(result, out, uniqueExecutor.get());
CHECK_RET(viewCopyMinResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnIsInScalarTensor(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnIsInScalarTensor);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif