* 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_masked_select.h"
#include "masked_select.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "aclnn/aclnn_base.h"
#include "op_api/aclnn_check.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 "conversion/broadcast_to/op_api/broadcast_to.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
* self mask
* | |
* \ /
* Contiguous(workspace_0) Contiguous(workspace_1)
* \ /
* \ Cast(workspace_2)
* \ /
* MaskedSelect(workspace_3)
* |
* Cast(workspace_4)
* |
* ViewCopy
* |
* result
*/
namespace ACLNN_MASKED_SELECT {
constexpr size_t MAX_DIM_LEN = 8;
static const std::initializer_list<op::DataType> SELF_DTYPE_SUPPORT_LIST_NOT_SUPPORT_BF16 = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_INT64,
op::DataType::DT_FLOAT16, op::DataType::DT_INT16, op::DataType::DT_INT8,
op::DataType::DT_UINT8, op::DataType::DT_DOUBLE, op::DataType::DT_BOOL};
static const std::initializer_list<op::DataType> SELF_DTYPE_SUPPORT_LIST_SUPPORT_BF16 = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_FLOAT16,
op::DataType::DT_INT16, op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_DOUBLE,
op::DataType::DT_BOOL, op::DataType::DT_BF16};
static const std::initializer_list<op::DataType> SELF_DTYPE_SUPPORT_LIST_SUPPORT_REGBASE = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_UINT32, op::DataType::DT_INT64,
op::DataType::DT_UINT64, op::DataType::DT_FLOAT16, op::DataType::DT_INT16, op::DataType::DT_UINT16,
op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_DOUBLE, op::DataType::DT_BOOL,
op::DataType::DT_BF16};
static const std::initializer_list<op::DataType> MASK_DTYPE_SUPPORT_LIST = {
op::DataType::DT_UINT8, op::DataType::DT_BOOL};
}
using namespace ACLNN_MASKED_SELECT;
inline static bool CheckNotNull(const aclTensor* self, const aclTensor* mask, const aclTensor* out)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(mask, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static const aclTensor* ResetFormatForRegBase(const aclTensor* x, const aclIntArray* shape)
{
if (!IsRegBase()) {
return x;
}
size_t tensorXSize = x->GetViewShape().GetDimNum();
if (tensorXSize == (*shape).Size()) {
return x;
}
auto constX = const_cast<aclTensor*>(x);
constX->SetViewFormat(op::Format::FORMAT_ND);
constX->SetStorageFormat(op::Format::FORMAT_ND);
constX->SetOriginalFormat(op::Format::FORMAT_ND);
return constX;
}
static const std::initializer_list<op::DataType> CheckSocVersionIsSupportBf16(void)
{
if (IsRegBase()) {
return SELF_DTYPE_SUPPORT_LIST_SUPPORT_REGBASE;
}
if (GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E) {
return SELF_DTYPE_SUPPORT_LIST_SUPPORT_BF16;
}
return SELF_DTYPE_SUPPORT_LIST_NOT_SUPPORT_BF16;
}
static bool CheckDtypeValid(const aclTensor* self, const aclTensor* mask, const aclTensor* out)
{
auto SELF_DTYPE_SUPPORT_LIST = CheckSocVersionIsSupportBf16();
OP_CHECK_DTYPE_NOT_SUPPORT(self, SELF_DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(mask, MASK_DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, SELF_DTYPE_SUPPORT_LIST, return false);
return true;
}
inline static bool isOutSizeSameWithBroadcastShapeSize(const aclTensor* y, op::Shape broadcastShape)
{
int64_t broadcastShapeSize = broadcastShape.GetShapeSize();
if (y->GetViewShape().GetShapeSize() == broadcastShapeSize) {
return true;
}
return false;
}
static bool CheckShape(const aclTensor* self, const aclTensor* mask, const aclTensor* y)
{
OP_CHECK_MAX_DIM(self, MAX_DIM_LEN, return false);
OP_CHECK_MAX_DIM(mask, MAX_DIM_LEN, return false);
Shape broadcastShape;
OP_CHECK_BROADCAST_AND_INFER_SHAPE(self, mask, broadcastShape, return false);
OP_CHECK_WRONG_DIMENSION(y, 1, return false);
if (!isOutSizeSameWithBroadcastShapeSize(y, broadcastShape)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "The out shape size is not same with broadcastShapeSize.");
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "y.shape: %ld, broadcastShape.shape: %ld.", y->GetViewShape().GetShapeSize(),
broadcastShape.GetShapeSize());
return false;
}
return true;
}
inline static aclnnStatus CheckParams(const aclTensor* self, const aclTensor* mask, const aclTensor* y)
{
CHECK_RET(CheckNotNull(self, mask, y), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, mask, y), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, mask, y), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static bool IsAiCoreSupport(const aclTensor* self)
{
if (IsRegBase()) {
return CheckType(self->GetDataType(), SELF_DTYPE_SUPPORT_LIST_SUPPORT_REGBASE);
} else if (
GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E) {
return CheckType(self->GetDataType(), SELF_DTYPE_SUPPORT_LIST_SUPPORT_BF16);
}
return false;
}
static void CheckFormat(const aclTensor* self, const aclTensor* target){
ge::Format selfStorageFormat = self->GetStorageFormat();
ge::Format targetStorageFormat = target->GetStorageFormat();
if (selfStorageFormat != ge::Format::FORMAT_ND || targetStorageFormat != ge::Format::FORMAT_ND){
OP_LOGW("aclnnMaskedSelect only support format ND.");
}
}
aclnnStatus aclnnMaskedSelectGetWorkspaceSize(
const aclTensor* self, const aclTensor* mask, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnMaskedSelect, DFX_IN(self, mask), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, mask, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
CheckFormat(self, mask);
if (self->IsEmpty() || mask->IsEmpty() || out->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 maskContiguous = l0op::Contiguous(mask, uniqueExecutor.get());
CHECK_RET(maskContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto maskCasted = l0op::Cast(maskContiguous, DataType::DT_BOOL, uniqueExecutor.get());
CHECK_RET(maskCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCasted = l0op::Cast(selfContiguous, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(selfCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor* selfBroadcast;
const aclTensor* maskBroadcast;
selfBroadcast = selfCasted;
maskBroadcast = maskCasted;
if (IsAiCoreSupport(self)) {
if (self->GetViewShape() != mask->GetViewShape()) {
op::Shape broadcastShape;
if (BroadcastInferShape(self->GetViewShape(), mask->GetViewShape(), broadcastShape)) {
op::FVector<int64_t, op::MAX_DIM_NUM> broadcastDims = op::ToShapeVector(broadcastShape);
auto broadcastShapeArray =
uniqueExecutor.get()->AllocIntArray(broadcastDims.data(), broadcastDims.size());
CHECK_RET(broadcastShapeArray != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCastedAfterFormat = ResetFormatForRegBase(selfCasted, broadcastShapeArray);
selfBroadcast = l0op::BroadcastTo(selfCastedAfterFormat, broadcastShapeArray, uniqueExecutor.get());
CHECK_RET(selfBroadcast != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto maskCastedAfterFormat = ResetFormatForRegBase(maskCasted, broadcastShapeArray);
maskBroadcast = l0op::BroadcastTo(maskCastedAfterFormat, broadcastShapeArray, uniqueExecutor.get());
CHECK_RET(maskBroadcast != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
}
l0op::MaskedSelectV3(selfBroadcast, maskBroadcast, out, uniqueExecutor.get());
} else {
l0op::MaskedSelect(selfBroadcast, maskBroadcast, out, uniqueExecutor.get());
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnMaskedSelect(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnMaskedSelect);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif