* 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_equal.h"
#include "aclnn_kernels/cast.h"
#include "conversion/fill/op_api/fill.h"
#include "tensor_equal.h"
#include "aclnn_kernels/contiguous.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 "opdev/platform.h"
#include "aclnn_kernels/common/op_error_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
* self other
* | |
* \ /
* Contiguous(workspace_0) Contiguous(workspace_1)
* \ /
* TensorEqual(workspace_2)
* |
* ViewCopy
* |
* result
*/
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_910B_LIST = {
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_BOOL,
op::DataType::DT_DOUBLE, op::DataType::DT_UINT16, op::DataType::DT_UINT32, op::DataType::DT_UINT64,
op::DataType::DT_BF16};
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_910_LIST = {
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_BOOL,
op::DataType::DT_DOUBLE, op::DataType::DT_UINT16, op::DataType::DT_UINT32, op::DataType::DT_UINT64};
static const std::initializer_list<op::DataType> OUT_DTYPE_SUPPORT_LIST = {
op::DataType::DT_BOOL};
static const size_t DIM_SUPPORT_MAX = 8;
static bool CheckNotNull(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(other, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static inline bool CheckSocVersionGe910B(void) {
return GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E;
}
static bool CheckDtypeValid(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
bool is910BSocVersion = CheckSocVersionGe910B();
const std::initializer_list<DataType> DTYPE_SUPPORT_LIST =
is910BSocVersion ? DTYPE_SUPPORT_910B_LIST : DTYPE_SUPPORT_910_LIST;
op::DataType promoteType = op::PromoteType(self->GetDataType(), other->GetDataType());
if (promoteType == DataType::DT_UNDEFINED) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Self dtype %s and other dtype %s can not promote dtype.",
op::ToString(self->GetDataType()).GetString(), op::ToString(other->GetDataType()).GetString());
return false;
}
if (!CheckType(promoteType, DTYPE_SUPPORT_LIST)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Self dtype %s and other dtype %s get promoteType dtype %s should be in " \
"dtype support list [%s].", op::ToString(self->GetDataType()).GetString(),
op::ToString(other->GetDataType()).GetString(), op::ToString(promoteType).GetString(),
op::ToString(DTYPE_SUPPORT_LIST).GetString());
return false;
}
OP_CHECK_DTYPE_NOT_SUPPORT(out, OUT_DTYPE_SUPPORT_LIST, return false);
return true;
}
static bool CheckMaxShape(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
OP_CHECK_MAX_DIM(self, DIM_SUPPORT_MAX, return false);
OP_CHECK_MAX_DIM(other, DIM_SUPPORT_MAX, return false);
OP_CHECK_MAX_DIM(out, DIM_SUPPORT_MAX, return false);
return true;
}
static bool CheckOutShape(const aclTensor *out) {
op::Shape outShape;
outShape.SetDimNum(1);
outShape.SetDim(0, 1);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, outShape, return false);
return true;
}
static aclnnStatus CheckParams(const aclTensor *self, const aclTensor *other, const aclTensor *out) {
CHECK_RET(CheckNotNull(self, other, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckMaxShape(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckOutShape(out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnEqualGetWorkspaceSize(const aclTensor *self, const aclTensor *other, aclTensor *out,
uint64_t *workspaceSize, aclOpExecutor **executor) {
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnEqual, DFX_IN(self, other), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, other, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if ((self->GetViewShape() != other->GetViewShape()) || (self->IsEmpty() && other->IsEmpty())) {
int64_t dim = 1;
const aclTensor *dims = (uniqueExecutor.get())->ConvertToTensor(&dim, 1, op::DataType::DT_INT64);
aclIntArray *outShape = (uniqueExecutor.get())->AllocIntArray(&dim, 1);
CHECK_RET(dims != nullptr, ACLNN_ERR_INNER_NULLPTR);
CHECK_RET(outShape != nullptr, ACLNN_ERR_INNER_NULLPTR);
int64_t val = 0;
if ((self->IsEmpty() && other->IsEmpty()) && (self->GetViewShape() == other->GetViewShape())) {
val = 1;
}
const aclTensor *value = (uniqueExecutor.get())->ConvertToTensor(&val, 1, out->GetDataType());
CHECK_RET(value != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto equalOpOut = l0op::Fill(dims, value, outShape, uniqueExecutor.get());
CHECK_RET(equalOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(equalOpOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto promoteType = op::PromoteType(self->GetDataType(), other->GetDataType());
if (promoteType == op::DataType::DT_BF16) {
promoteType = op::DataType::DT_FLOAT;
}
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCasted = l0op::Cast(selfContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(selfCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherCasted = l0op::Cast(otherContiguous, promoteType, uniqueExecutor.get());
CHECK_RET(otherCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto equalOpOut = l0op::TensorEqual(selfCasted, otherCasted, uniqueExecutor.get());
CHECK_RET(equalOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(equalOpOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnEqual(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnEqual);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif