* Copyright (c) 2026 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_eq_scalar.h"
#include "equal.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/cast.h"
#include "opdev/op_log.h"
#include "opdev/op_dfx.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/format_utils.h"
#include "opdev/make_op_executor.h"
#include "opdev/platform.h"
#include "aclnn_kernels/common/op_error_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const int64_t EQ_MAX_DIM_NUM = 8;
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_COMPLEX64, op::DataType::DT_COMPLEX128, op::DataType::DT_BF16};
static inline double GetCastedDouble(const aclTensor* self, const aclScalar* other)
{
double res = 0;
switch (self->GetDataType()) {
case DataType::DT_FLOAT:
res = static_cast<double>(other->ToFloat());
break;
case DataType::DT_FLOAT16:
res = static_cast<double>(other->ToFp16());
break;
default:
res = other->ToDouble();
break;
}
return res;
}
static inline bool IsDoubleEqual(double a, double b)
{
if (std::abs(a - b) <= std::numeric_limits<float>::epsilon()) {
return true;
}
return false;
}
static op::DataType PromoteTypeScalar(op::DataType selfDtype, op::DataType otherDtype)
{
if (IsComplexType(selfDtype)) {
return selfDtype;
}
if (!IsComplexType(otherDtype) && IsFloatingType(selfDtype)) {
return selfDtype;
}
if (selfDtype == op::DataType::DT_BOOL || IsFloatingType(otherDtype) || IsComplexType(otherDtype)) {
return op::PromoteType(selfDtype, otherDtype);
}
return selfDtype;
}
static bool CheckNotNull(const aclTensor* self, const aclScalar* 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 bool HasEmptyTensor(const aclTensor* self)
{
if (self->IsEmpty()) {
return true;
}
return false;
}
static bool CheckDtypeValid(const aclTensor* self, const aclScalar* other, const aclTensor* out)
{
const std::initializer_list<op::DataType> dtypeSupportList = DTYPE_SUPPORT_910B_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);
return true;
}
static bool CheckPromoteType(const aclTensor* self, const aclScalar* other, const aclTensor* out)
{
op::DataType promoteType;
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;
}
OP_CHECK_RESULT_DTYPE_CAST_FAILED(self->GetDataType(), promoteType, return false);
OP_CHECK_RESULT_DTYPE_CAST_FAILED(other->GetDataType(), promoteType, return false);
OP_CHECK_RESULT_DTYPE_CAST_FAILED(DataType::DT_BOOL, out->GetDataType(), return false);
return true;
}
static bool CheckShape(const aclTensor* self, const aclTensor* out)
{
OP_CHECK_MAX_DIM(self, EQ_MAX_DIM_NUM, return false);
OP_CHECK_MAX_DIM(out, EQ_MAX_DIM_NUM, return false);
OP_CHECK_SHAPE_NOT_EQUAL(out, self, return false);
return true;
}
static aclnnStatus CheckParams(const aclTensor* self, const aclScalar* 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(CheckPromoteType(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnEqScalarGetWorkspaceSize(
const aclTensor* self, const aclScalar* other, aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnEqScalar, 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 (HasEmptyTensor(self)) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto promoteType = PromoteTypeScalar(self->GetDataType(), other->GetDataType());
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);
const aclTensor* otherTensor = uniqueExecutor.get()->ConvertToTensor(other, promoteType);
CHECK_RET(otherTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto equalResult = l0op::Equal(selfCasted, otherTensor, uniqueExecutor.get());
CHECK_RET(equalResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(equalResult, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnInplaceEqScalarGetWorkspaceSize(
const aclTensor* selfRef, const aclScalar* other, uint64_t* workspaceSize, aclOpExecutor** executor)
{
auto out = const_cast<aclTensor*>(selfRef);
return aclnnEqScalarGetWorkspaceSize(selfRef, other, out, workspaceSize, executor);
}
aclnnStatus aclnnEqScalar(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, const aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnEqScalar);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnInplaceEqScalar(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnInplaceEqScalar);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif