* 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_complex.h"
#include "aclnn_kernels/cast.h"
#include "complex.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/common/op_error_check.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/platform.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static bool CheckNotNull(const aclTensor *real, const aclTensor *imag, const aclTensor *out) {
OP_CHECK_NULL(real, return false);
OP_CHECK_NULL(imag, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static const std::initializer_list<op::DataType> INPUT_DTYPE_SUPPORT_LIST = {DataType::DT_FLOAT16,
DataType::DT_FLOAT,
DataType::DT_DOUBLE};
static const std::initializer_list<op::DataType> OUTPUT_DTYPE_SUPPORT_LIST = {DataType::DT_COMPLEX32,
DataType::DT_COMPLEX64,
DataType::DT_COMPLEX128};
static const std::initializer_list<std::pair<op::DataType, op::DataType>> DTYPE_PAIR = {
{DataType::DT_FLOAT16, DataType::DT_COMPLEX32},
{DataType::DT_FLOAT, DataType::DT_COMPLEX64},
{DataType::DT_DOUBLE, DataType::DT_COMPLEX128}
};
static bool CheckDtypeValid(const aclTensor *real, const aclTensor *imag, const aclTensor *out) {
OP_CHECK_DTYPE_NOT_SUPPORT(real, INPUT_DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(imag, INPUT_DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, OUTPUT_DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SAME(real, imag, return false);
auto inOutPair = std::pair<op::DataType, op::DataType>(real->GetDataType(), out->GetDataType());
bool isSupport = std::find(DTYPE_PAIR.begin(), DTYPE_PAIR.end(), inOutPair) != DTYPE_PAIR.end();
if (!isSupport) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Input dtype and out dtype must be paired. Current dtype is %s and %s.",
op::ToString(real->GetDataType()).GetString(), op::ToString(out->GetDataType()).GetString());
}
return isSupport;
}
static bool CheckOutShape(const aclTensor *real, const aclTensor *imag, const aclTensor *out) {
const size_t MAX_DIM = 8;
OP_CHECK_MAX_DIM(real, MAX_DIM, return false);
OP_CHECK_MAX_DIM(imag, MAX_DIM, return false);
op::Shape outShape;
OP_CHECK_BROADCAST_AND_INFER_SHAPE(real, imag, outShape, return false);
if (outShape != out->GetViewShape()) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "BroadcastShape %s is not equal out's shape %s.",
op::ToString(outShape).GetString(), op::ToString(out->GetViewShape()).GetString());
return false;
}
return true;
}
static aclnnStatus CheckParams(const aclTensor *real, const aclTensor *imag, const aclTensor *out) {
CHECK_RET(CheckDtypeValid(real, imag, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckOutShape(real, imag, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnComplexGetWorkspaceSize(const aclTensor *real, const aclTensor *imag, aclTensor *out,
uint64_t *workspaceSize, aclOpExecutor **executor) {
L2_DFX_PHASE_1(aclnnComplex, DFX_IN(real, imag), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
CHECK_RET(CheckNotNull(real, imag, out), ACLNN_ERR_PARAM_NULLPTR);
if (real->IsEmpty() || imag->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto ret = CheckParams(real, imag, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
auto realContiguous = l0op::Contiguous(real, uniqueExecutor.get());
CHECK_RET(realContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto imagContiguous = l0op::Contiguous(imag, uniqueExecutor.get());
CHECK_RET(imagContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto complexOpOut = l0op::Complex(realContiguous, imagContiguous, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(complexOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(complexOpOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnComplex(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnComplex);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif