* 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_polar.h"
#include "aclnn_kernels/contiguous.h"
#include "polar.h"
#include "../../complex/op_host/op_api/complex.h"
#include "../../mul/op_api/mul.h"
#include "../../sin/op_api/sin.h"
#include "../../cos/op_api/cos.h"
#include "op_api/op_api_def.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/op_dfx.h"
#include "opdev/op_log.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "op_api/aclnn_check.h"
#include "opdev/platform.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT
};
static const std::initializer_list<op::DataType> OUTPUT_DTYPE_SUPPORT_LIST = {
op::DataType::DT_COMPLEX64
};
static bool CheckNotNull(const aclTensor* input, const aclTensor* angle, const aclTensor *out)
{
OP_CHECK_NULL(input, return false);
OP_CHECK_NULL(angle, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static bool CheckDtypeValid(const aclTensor *input, const aclTensor *angle, const aclTensor *out)
{
OP_CHECK_DTYPE_NOT_SUPPORT(input, DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, OUTPUT_DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(angle, DTYPE_SUPPORT_LIST, return false);
OP_CHECK_DTYPE_NOT_MATCH(input, angle->GetDataType(), return false);
return true;
}
constexpr size_t MAX_DIM_LEN = 8;
static bool CheckShape(const aclTensor *input, const aclTensor *angle, const aclTensor *out) {
OP_CHECK_MAX_DIM(input, MAX_DIM_LEN, return false);
OP_CHECK_MAX_DIM(angle, MAX_DIM_LEN, return false);
OP_CHECK_MAX_DIM(out, MAX_DIM_LEN, return false);
op::Shape broadcastShape;
OP_CHECK_BROADCAST_AND_INFER_SHAPE(input, angle, broadcastShape, 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* input, const aclTensor* angle, const aclTensor* out){
CHECK_RET(CheckNotNull(input, angle, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(input, angle, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(input, angle, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnPolarGetWorkspaceSize(const aclTensor* input, const aclTensor* angle, aclTensor* out,
uint64_t* workspaceSize, aclOpExecutor** executor) {
L2_DFX_PHASE_1(aclnnPolar, DFX_IN(input, angle), DFX_OUT(out));
OP_CHECK_COMM_INPUT(workspaceSize, executor);
auto ret = CheckParams(input, angle, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
if (input->IsEmpty() || angle->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
auto inputContiguous = l0op::Contiguous(input, uniqueExecutor.get());
CHECK_RET(inputContiguous != nullptr,ACLNN_ERR_INNER_NULLPTR);
auto angleContiguous = l0op::Contiguous(angle, uniqueExecutor.get());
CHECK_RET(angleContiguous != nullptr,ACLNN_ERR_INNER_NULLPTR);
const aclTensor* output = nullptr;
if (IsRegBase()) {
output = l0op::Polar(inputContiguous, angleContiguous, uniqueExecutor.get());
CHECK_RET(output != nullptr, ACLNN_ERR_INNER_NULLPTR);
} else {
auto angleSin = l0op::Sin(angleContiguous, uniqueExecutor.get());
CHECK_RET(angleSin != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto angleCos = l0op::Cos(angleContiguous, uniqueExecutor.get());
CHECK_RET(angleCos != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto absSin = l0op::Mul(angleSin, inputContiguous, uniqueExecutor.get());
auto absCos = l0op::Mul(angleCos, inputContiguous, uniqueExecutor.get());
CHECK_RET(absSin != nullptr, ACLNN_ERR_INNER_NULLPTR);
CHECK_RET(absCos != nullptr, ACLNN_ERR_INNER_NULLPTR);
output = l0op::Complex(absCos, absSin, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(output != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto copyResult = l0op::ViewCopy(output, out, uniqueExecutor.get());
CHECK_RET(copyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnPolar(void *workspace, uint64_t workspaceSize,aclOpExecutor *executor, aclrtStream stream){
L2_DFX_PHASE_2(aclnnPolar);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif