* 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.
*/
* \file aclnn_affine_grid.cpp
* \brief
*/
#include "aclnn_affine_grid.h"
#include "aclnn_kernels/contiguous.h"
#include "affine_grid.h"
#include "aclnn_kernels/reshape.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/format_utils.h"
#include "opdev/op_dfx.h"
#include "opdev/op_executor.h"
#include "opdev/make_op_executor.h"
#include "opdev/op_log.h"
#include "opdev/shape_utils.h"
#include "opdev/tensor_view_utils.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const size_t DIM_LEN = 3;
static const int64_t DIM_N = 0;
static const int64_t DIM_C = 1;
static const int64_t DIM_D = 2;
static const int64_t DIM_H = 3;
static const int64_t DIM_W = 4;
static const int64_t DIM_H_2D = 2;
static const int64_t DIM_W_2D = 3;
static const int64_t AXIS_2D = 2;
static const int64_t AXIS = 3;
static const int64_t SECOND_DIM = 2;
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16};
static const std::initializer_list<op::DataType> ASCEND910B_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_BF16};
static inline bool CheckNotNull(const aclTensor *theta, const aclIntArray *size, const aclTensor *out) {
OP_CHECK_NULL(theta, return false);
OP_CHECK_NULL(size, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static inline bool CheckDtypeValid(const aclTensor *theta, const aclTensor *out) {
bool isAscend910BSocVersion = (GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E);
const std::initializer_list<op::DataType> dtypeSupportList =
isAscend910BSocVersion ? ASCEND910B_DTYPE_SUPPORT_LIST : ASCEND910_DTYPE_SUPPORT_LIST;
OP_CHECK_DTYPE_NOT_SUPPORT(theta, dtypeSupportList, return false);
OP_CHECK_DTYPE_NOT_MATCH(out, theta->GetDataType(), return false);
return true;
}
static bool CheckShape(const aclTensor *theta, const aclIntArray *size, const aclTensor *out) {
if (theta->IsEmpty()) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Expected theta to be not null tensor.");
return false;
}
if (size->Size() == 4) {
if (theta->GetViewShape().GetDimNum() != DIM_LEN || theta->GetViewShape().GetDim(1) != DIM_H_2D ||
theta->GetViewShape().GetDim(SECOND_DIM) != DIM_W_2D || theta->GetViewShape().GetDim(DIM_N) != (*size)[DIM_N]) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Expected a batch of 2D affine matrices of shape Nx2x3 for size "
"[%ld, %ld, %ld, %ld]. Got %s.", (*size)[DIM_N], (*size)[DIM_C],
(*size)[DIM_H_2D], (*size)[DIM_W_2D], op::ToString(theta->GetViewShape()).GetString());
return false;
}
op::Shape outShape;
outShape.AppendDim((*size)[DIM_N]);
outShape.AppendDim((*size)[DIM_H_2D]);
outShape.AppendDim((*size)[DIM_W_2D]);
outShape.AppendDim(AXIS_2D);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, outShape, return false);
} else if (size->Size() == 5) {
if (theta->GetViewShape().GetDimNum() != DIM_LEN || theta->GetViewShape().GetDim(1) != DIM_H ||
theta->GetViewShape().GetDim(SECOND_DIM) != DIM_W || theta->GetViewShape().GetDim(DIM_N) != (*size)[DIM_N]) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Expected a batch of 3D affine matrices of shape Nx3x4 for size "
"[%ld, %ld, %ld, %ld, %ld]. Got %s.", (*size)[DIM_N], (*size)[DIM_C],
(*size)[DIM_D], (*size)[DIM_H], (*size)[DIM_W], op::ToString(theta->GetViewShape()).GetString());
return false;
}
op::Shape outShape;
outShape.AppendDim((*size)[DIM_N]);
outShape.AppendDim((*size)[DIM_D]);
outShape.AppendDim((*size)[DIM_H]);
outShape.AppendDim((*size)[DIM_W]);
outShape.AppendDim(AXIS);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, outShape, return false);
} else {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "AffineGridGenerator needs 4d or 5d size(input).");
return false;
}
return true;
}
static void CheckFormat(const aclTensor* theta) {
ge::Format thetaStorageFormat = theta->GetStorageFormat();
if (thetaStorageFormat == ge::Format::FORMAT_FRACTAL_NZ) {
OP_LOGW("aclnnAffineGrid doesn't support format NZ.");
}
}
static inline aclnnStatus CheckParams(const aclTensor *theta, const aclIntArray *size, const aclTensor *out) {
CHECK_RET(CheckNotNull(theta, size, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(theta, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(theta, size, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnAffineGridGetWorkspaceSize(const aclTensor *theta, const aclIntArray *size, bool alignCorners,
aclTensor *out, uint64_t *workspaceSize, aclOpExecutor **executor) {
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnAffineGrid, DFX_IN(theta, size, alignCorners), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(theta, size, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
CheckFormat(theta);
auto thetaContiguous = l0op::Contiguous(theta, uniqueExecutor.get());
CHECK_RET(thetaContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor *affineGridOut = l0op::AffineGrid(thetaContiguous, size, alignCorners, uniqueExecutor.get());
CHECK_RET(affineGridOut != nullptr, ACLNN_ERR_PARAM_NULLPTR);
const int64_t dim5Shape[] = {(*size)[DIM_N], (*size)[DIM_D], (*size)[DIM_H], (*size)[DIM_W], AXIS};
const int64_t dim4Shape[] = {(*size)[DIM_N], (*size)[DIM_H_2D], (*size)[DIM_W_2D], AXIS_2D};
auto outReshape = l0op::Reshape(affineGridOut, uniqueExecutor.get()->AllocIntArray((size->Size() == 4) ? dim4Shape :
dim5Shape, size->Size()),
uniqueExecutor.get());
CHECK_RET(outReshape != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyMinResult = l0op::ViewCopy(outReshape, out, uniqueExecutor.get());
CHECK_RET(viewCopyMinResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnAffineGrid(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnAffineGrid);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif