* 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_flatten.cpp
* \brief
*/
#include "aclnn_flatten.h"
#include "flatten.h"
#include "aclnn_kernels/reshape.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "op_api/op_api_def.h"
#include "opdev/format_utils.h"
#include "opdev/op_dfx.h"
#include "opdev/op_log.h"
#include "opdev/op_executor.h"
#include "opdev/tensor_view_utils.h"
#include "opdev/platform.h"
#include "op_api/aclnn_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static constexpr int64_t MAX_DIM_LEN = 8;
static const std::initializer_list<op::DataType> Ascend910_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_INT32, op::DataType::DT_INT8,
op::DataType::DT_UINT8, op::DataType::DT_INT64, op::DataType::DT_INT16, op::DataType::DT_UINT16,
op::DataType::DT_UINT32, op::DataType::DT_UINT64, op::DataType::DT_BOOL};
static const std::initializer_list<op::DataType> Ascend910B_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_INT32, op::DataType::DT_INT8,
op::DataType::DT_UINT8, op::DataType::DT_INT64, op::DataType::DT_INT16, op::DataType::DT_UINT16,
op::DataType::DT_UINT32, op::DataType::DT_UINT64, op::DataType::DT_BOOL, op::DataType::DT_BF16};
struct ShapeResult {
Shape outShape{};
bool isLegal{true};
};
static inline int64_t MakeWrapDim(int64_t dim, int64_t dimPostExpr) {
if (dimPostExpr <= 0) {
dimPostExpr = 1;
}
if (dim < 0) {
dim += dimPostExpr;
}
return dim;
}
static ShapeResult GetOutShape(const aclTensor *self, const int64_t axis) {
ShapeResult shapeResult;
int64_t dim0 = 1;
int64_t dim1 = 1;
size_t selfDim = self->GetViewShape().GetDimNum();
for (int64_t i = 0; i < axis; i++) {
int64_t dim = self->GetViewShape().GetDim(i);
if (dim > 0 && (dim0 > INT64_MAX / dim)) {
shapeResult.isLegal = false;
return shapeResult;
}
dim0 *= dim;
}
for (size_t i = axis; i < selfDim; i++) {
int64_t dim = self->GetViewShape().GetDim(i);
if (dim > 0 && (dim1 > INT64_MAX / dim)) {
shapeResult.isLegal = false;
return shapeResult;
}
dim1 *= dim;
}
shapeResult.outShape.AppendDim(dim0);
shapeResult.outShape.AppendDim(dim1);
return shapeResult;
}
static bool CheckNotNull(const aclTensor *self, const aclTensor *out) {
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static bool CheckDtypeValid(const aclTensor *self, const aclTensor *out) {
OP_CHECK_DTYPE_NOT_SAME(self, out, return false);
bool is910bSocVersion = (GetCurrentPlatformInfo().GetSocVersion() == SocVersion::ASCEND910B ||
GetCurrentPlatformInfo().GetSocVersion() == SocVersion::ASCEND910_93 ||
IsRegBase());
const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_CURRENT =
is910bSocVersion ? Ascend910B_DTYPE_SUPPORT_LIST : Ascend910_DTYPE_SUPPORT_LIST;
OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST_CURRENT, return false);
return true;
}
static bool CheckShape(const aclTensor *self, const int64_t axis, const aclTensor *out) {
OP_CHECK_MAX_DIM(self, MAX_SUPPORT_DIMS_NUMS, return false);
size_t selfDim = self->GetViewShape().GetDimNum();
int64_t axisWrap = MakeWrapDim(axis, static_cast<int64_t>(selfDim));
if (selfDim <= 1 && axisWrap > 0) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"Dimension out of range (expected to be in range of [-1, 0], but got %ld)",
axisWrap);
return false;
}
if ((axisWrap < 0) || (selfDim > 1 && static_cast<int64_t>(selfDim) <= axisWrap)) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"Dimension out of range (expected to be in range of [-%zu, %zu], but got %ld)",
selfDim, selfDim - 1, axis);
return false;
}
ShapeResult outShapeResult = GetOutShape(self, axisWrap);
if (!outShapeResult.isLegal) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"The output shape exceeds the int64_t data range");
return false;
}
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, outShapeResult.outShape, return false);
return true;
}
static void CheckFormat(const aclTensor* self)
{
if (self->GetStorageFormat() == Format::FORMAT_FRACTAL_NZ) {
OP_LOGW("Format of self gets [%s], this format may lead to precision failure.",
op::ToString(self->GetStorageFormat()).GetString());
}
}
static aclnnStatus CheckParams(const aclTensor *self, const int64_t axis, const aclTensor *out) {
CHECK_RET(CheckNotNull(self, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, axis, out), ACLNN_ERR_PARAM_INVALID);
CheckFormat(self);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnFlattenGetWorkspaceSize(const aclTensor *self, int64_t axis, aclTensor *out,
uint64_t *workspaceSize, aclOpExecutor **executor) {
L2_DFX_PHASE_1(aclnnFlatten, DFX_IN(self, axis), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, axis, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (self->IsEmpty() || out->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
int64_t axisWrap = MakeWrapDim(axis, static_cast<int64_t>(self->GetViewShape().GetDimNum()));
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (IsRegBase()) {
auto selfReshape = l0op::Reshape(selfContiguous, out->GetViewShape(), uniqueExecutor.get());
CHECK_RET(selfReshape != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(selfReshape, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
} else {
auto flattenResult = l0op::Flatten(selfContiguous, uniqueExecutor.get(), axisWrap);
CHECK_RET(flattenResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(flattenResult, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnFlatten(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, const aclrtStream stream) {
L2_DFX_PHASE_2(aclnnFlatten);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif