* 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_s_where.h"
#include "select.h"
#include "aclnn_kernels/cast.h"
#include "math/expand/op_api/expand.h"
#include "aclnn/aclnn_base.h"
#include "aclnn_kernels/contiguous.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"
#include "opdev/platform.h"
#include "op_api/level2_base.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const int64_t MAX_DIM = 8;
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_910_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_UINT64, op::DataType::DT_INT64,
op::DataType::DT_UINT32, op::DataType::DT_FLOAT16, op::DataType::DT_UINT16, op::DataType::DT_INT16,
op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_DOUBLE, op::DataType::DT_BOOL,
op::DataType::DT_COMPLEX64, op::DataType::DT_COMPLEX128};
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_910B_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_UINT64, op::DataType::DT_INT64,
op::DataType::DT_UINT32, op::DataType::DT_FLOAT16, op::DataType::DT_UINT16, op::DataType::DT_INT16,
op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_DOUBLE, op::DataType::DT_BOOL,
op::DataType::DT_COMPLEX64, op::DataType::DT_COMPLEX128, op::DataType::DT_BF16};
static const std::initializer_list<op::DataType> CONDITION_DTYPE_SUPPORT = {
op::DataType::DT_BOOL, op::DataType::DT_UINT8};
static bool CheckDtypeValid(
const aclTensor* self, const aclTensor* other, const aclTensor* condition, const aclTensor* out)
{
const std::initializer_list<op::DataType> dtypeSupportList =
GetDtypeSupportListV3(DTYPE_SUPPORT_910B_LIST, DTYPE_SUPPORT_910_LIST);
OP_CHECK_DTYPE_NOT_SUPPORT(self, dtypeSupportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(other, dtypeSupportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, dtypeSupportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(condition, CONDITION_DTYPE_SUPPORT, return false);
return true;
}
static bool CheckShape(const aclTensor* self, const aclTensor* other, const aclTensor* condition, const aclTensor* out)
{
OP_CHECK_MAX_DIM(self, MAX_DIM, return false);
OP_CHECK_MAX_DIM(other, MAX_DIM, return false);
OP_CHECK_MAX_DIM(condition, MAX_DIM, return false);
op::Shape broadcastShape;
OP_CHECK_BROADCAST_AND_INFER_SHAPE(self, other, broadcastShape, return false);
if (!BroadcastInferShape(condition->GetViewShape(), broadcastShape, broadcastShape)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Shape of self other and condition can't broadcast.");
return false;
}
if (broadcastShape != out->GetViewShape()) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "Shape of out should be %s, but current is %s.",
op::ToString(broadcastShape).GetString(), op::ToString(out->GetViewShape()).GetString());
return false;
}
return true;
}
static aclnnStatus CheckParams(
const aclTensor* self, const aclTensor* condition, const aclTensor* other, const aclTensor* out)
{
CHECK_RET(CheckNotNull4Tensor(self, other, condition, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, other, condition, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, other, condition, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnSWhereGetWorkspaceSize(
const aclTensor* condition, const aclTensor* self, const aclTensor* other, aclTensor* out, uint64_t* workspaceSize,
aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnSWhere, DFX_IN(condition, self, other), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, condition, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (self->IsEmpty() || condition->IsEmpty() || other->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto conditionContiguous = l0op::Contiguous(condition, uniqueExecutor.get());
CHECK_RET(conditionContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto promoteType = op::PromoteType(self->GetDataType(), other->GetDataType());
auto conditionCast = l0op::Cast(conditionContiguous, op::DataType::DT_BOOL, uniqueExecutor.get());
CHECK_RET(conditionCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor* selRes = nullptr;
if (selfContiguous->GetDataType() == DataType::DT_BOOL && otherContiguous->GetDataType() == DataType::DT_BOOL) {
auto selfView = uniqueExecutor.get()->CreateView(
selfContiguous, selfContiguous->GetViewShape(), selfContiguous->GetViewOffset());
CHECK_RET(selfView != nullptr, ACLNN_ERR_INNER_NULLPTR);
selfView->SetDataType(DataType::DT_INT8);
auto otherView = uniqueExecutor.get()->CreateView(
otherContiguous, otherContiguous->GetViewShape(), otherContiguous->GetViewOffset());
CHECK_RET(otherView != nullptr, ACLNN_ERR_INNER_NULLPTR);
otherView->SetDataType(DataType::DT_INT8);
auto opRes = l0op::SelectV2(conditionCast, selfView, otherView, uniqueExecutor.get());
CHECK_RET(opRes != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto resView = uniqueExecutor.get()->CreateView(opRes, opRes->GetViewShape(), opRes->GetViewOffset());
CHECK_RET(resView != nullptr, ACLNN_ERR_INNER_NULLPTR);
resView->SetDataType(DataType::DT_BOOL);
selRes = l0op::ViewCopy(resView, out, uniqueExecutor.get());
} else {
auto selfCast = l0op::Cast(selfContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(selfCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherCast = l0op::Cast(otherContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(otherCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto opOut = l0op::SelectV2(conditionCast, selfCast, otherCast, uniqueExecutor.get());
CHECK_RET(opOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selCast = l0op::Cast(opOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(selCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
selRes = l0op::ViewCopy(selCast, out, uniqueExecutor.get());
}
CHECK_RET(selRes != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnSWhere(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnSWhere);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif