* 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_max_v2.h"
#include "reduce_max.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn/aclnn_base.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "op_api/op_api_def.h"
#include "op_api/aclnn_check.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/tensor_view_utils.h"
#include "opdev/op_errno.h"
#include <bitset>
using namespace op;
using std::bitset;
#ifdef __cplusplus
extern "C" {
#endif
constexpr size_t MAX_MASK_LEN = 64;
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_UINT8,
op::DataType::DT_INT8, op::DataType::DT_INT16, op::DataType::DT_INT32,
op::DataType::DT_INT64, op::DataType::DT_DOUBLE, 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_UINT8, op::DataType::DT_INT8,
op::DataType::DT_INT16, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_DOUBLE,
op::DataType::DT_BOOL, op::DataType::DT_BF16};
static inline bool CheckNotNull(const aclTensor* self, const aclIntArray* dims, const aclTensor* out)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(dims, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static inline const std::initializer_list<op::DataType>& GetDtypeSupportList()
{
if ((GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E) ||
IsRegBase()) {
return ASCEND910B_DTYPE_SUPPORT_LIST;
} else {
return ASCEND910_DTYPE_SUPPORT_LIST;
}
}
static bool CheckDtypeValid(const aclTensor* self, const aclTensor* out)
{
auto supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_MATCH(out, self->GetDataType(), return false);
OP_CHECK_DTYPE_NOT_SUPPORT(self, supportList, return false);
return true;
}
static inline uint64_t GetPosDim(int64_t dim, int64_t dimNum)
{
if (dimNum <= 0) {
dimNum = 1;
}
return dim >= 0 ? dim : dim + dimNum;
}
static inline const aclIntArray* GetAllDims(
const aclTensor* self, const bool noopWithEmptyDims, aclOpExecutor* executor)
{
auto inputShape = self->GetViewShape();
size_t inputDimNum = inputShape.GetDimNum();
FVector<int64_t> dims;
if (!noopWithEmptyDims) {
for (size_t idx = 0; idx < inputDimNum; idx++) {
dims.emplace_back(idx);
}
}
return executor->AllocIntArray(dims.data(), dims.size());
}
static void reduce_maxInferShape(
const op::Shape& selfShape, const aclIntArray* dims, bool keepDims, const bool noopWithEmptyDims,
op::Shape& reduceShape)
{
bitset<MAX_MASK_LEN> dimMask = bitset<MAX_MASK_LEN>();
if (dims->Size() == 0) {
if (noopWithEmptyDims) {
for (size_t i = 0; i < selfShape.GetDimNum(); ++i) {
reduceShape.AppendDim(selfShape.GetDim(i));
}
return;
} else {
if (keepDims) {
for (size_t i = 0; i < selfShape.GetDimNum(); i++) {
reduceShape.AppendDim(1);
}
}
return;
}
}
for (size_t i = 0; i < dims->Size(); i++) {
int64_t index = GetPosDim(dims->operator[](i), selfShape.GetDimNum());
dimMask.set(index);
}
for (size_t i = 0; i < selfShape.GetDimNum(); i++) {
if (!dimMask[i]) {
reduceShape.AppendDim(selfShape.GetDim(i));
} else if (keepDims) {
reduceShape.AppendDim(1);
}
}
}
static bool CheckDimValid(const aclTensor* self, const aclIntArray* dims)
{
auto selfViewShape = self->GetViewShape();
auto selfDimNum = static_cast<int64_t>(selfViewShape.GetDimNum());
bool isScalar = false;
if (selfDimNum <= 0) {
selfDimNum = 1;
isScalar = true;
}
bitset<MAX_MASK_LEN> dimMask = bitset<MAX_MASK_LEN>();
for (size_t i = 0; i < dims->Size(); i++) {
if (dims->operator[](i) >= selfDimNum || dims->operator[](i) < (-selfDimNum)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "Provided dims %ld must be in the range of [%ld, %ld].", dims->operator[](i),
-selfDimNum, selfDimNum - 1);
return false;
}
uint64_t index = GetPosDim(dims->operator[](i), selfDimNum);
if (!isScalar && selfViewShape.GetDim(index) == 0) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Expected reducution dims %lu to have non-zero size.", index);
return false;
}
if (dimMask[index]) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Dim %lu appears multiple times in the list of dims.", index);
return false;
}
dimMask.set(index);
}
return true;
}
static bool CheckShape(
const aclTensor* self, const aclIntArray* dims, const bool keepDims, const bool noopWithEmptyDims,
const aclTensor* out)
{
OP_CHECK_MAX_DIM(self, MAX_SUPPORT_DIMS_NUMS, return false);
OP_CHECK_MAX_DIM(out, MAX_SUPPORT_DIMS_NUMS, return false);
op::Shape reduceShape;
reduce_maxInferShape(self->GetViewShape(), dims, keepDims, noopWithEmptyDims, reduceShape);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, reduceShape, return false);
return true;
}
static aclnnStatus CheckParams(
const aclTensor* self, const aclIntArray* dims, const bool keepDims, const bool noopWithEmptyDims,
const aclTensor* out)
{
CHECK_RET(CheckNotNull(self, dims, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDimValid(self, dims), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, dims, keepDims, noopWithEmptyDims, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnMaxV2GetWorkspaceSize(
const aclTensor* self, const aclIntArray* dims, const bool keepDims, bool noopWithEmptyDims, aclTensor* out,
uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnMaxV2, DFX_IN(self, dims, keepDims, noopWithEmptyDims), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, dims, keepDims, noopWithEmptyDims, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (self->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
if (self->GetViewShape().GetDimNum() == 0) {
auto viewCopyResult = l0op::ViewCopy(self, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
if (dims->Size() == 0) {
dims = GetAllDims(self, noopWithEmptyDims, uniqueExecutor.get());
CHECK_RET(dims != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
op::DataType selfCastType =
(self->GetDataType() == op::DataType::DT_BOOL) ? op::DataType::DT_FLOAT : self->GetDataType();
auto selfCasted = l0op::Cast(selfContiguous, selfCastType, uniqueExecutor.get());
CHECK_RET(selfCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto maxResult = l0op::ReduceMax(selfCasted, dims, keepDims, noopWithEmptyDims, uniqueExecutor.get());
CHECK_RET(maxResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castMaxOut = l0op::Cast(maxResult, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMaxOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castMaxOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnMaxV2(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnMaxV2);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif