* 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_amax.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/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;
constexpr size_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_UINT8, op::DataType::DT_INT32,
op::DataType::DT_INT64, op::DataType::DT_INT16, op::DataType::DT_INT8, 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_INT32,
op::DataType::DT_INT64, op::DataType::DT_INT16, op::DataType::DT_INT8, op::DataType::DT_DOUBLE,
op::DataType::DT_BOOL, op::DataType::DT_BF16};
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, aclOpExecutor* executor)
{
auto inputShape = self->GetViewShape();
size_t inputDimNum = inputShape.GetDimNum();
FVector<int64_t> dims;
for (size_t idx = 0; idx < inputDimNum; idx++) {
dims.emplace_back(idx);
}
return executor->AllocIntArray(dims.data(), dims.size());
}
static void AmaxInferShape(const op::Shape& selfShape, const aclIntArray* dim, bool keepDim, op::Shape& reduceShape)
{
bitset<MAX_MASK_LEN> dimMask = bitset<MAX_MASK_LEN>();
if (dim->Size() == 0) {
dimMask.flip();
}
for (size_t i = 0; i < dim->Size(); i++) {
int64_t index = GetPosDim(dim->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 (keepDim) {
reduceShape.AppendDim(1);
}
}
}
static inline bool CheckNotNull(const aclTensor* self, const aclIntArray* dim, const aclTensor* out)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(dim, 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 void CheckFormat(const aclTensor* self)
{
op::Format format = self->GetStorageFormat();
if (format == Format::FORMAT_FRACTAL_NZ){
OP_LOGW("Format of inputs gets [%s],this format mat lead to precision failure",op::ToString(format).GetString());
}
}
static bool CheckDimValid(const aclTensor* self, const aclIntArray* dim)
{
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 < dim->Size(); i++) {
if (dim->operator[](i) >= selfDimNum || dim->operator[](i) < (-selfDimNum)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "Provided dim %ld must be in the range of [%ld, %ld].", dim->operator[](i),
-selfDimNum, selfDimNum - 1);
return false;
}
uint64_t index = GetPosDim(dim->operator[](i), selfDimNum);
if (!isScalar && selfViewShape.GetDim(index) == 0) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Expected reducution dim %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* dim, const bool keepDim, const aclTensor* out)
{
OP_CHECK_MAX_DIM(self, MAX_DIM_LEN, return false);
OP_CHECK_MAX_DIM(out, MAX_DIM_LEN, return false);
op::Shape reduceShape;
AmaxInferShape(self->GetViewShape(), dim, keepDim, reduceShape);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(out, reduceShape, return false);
return true;
}
static aclnnStatus CheckParams(const aclTensor* self, const aclIntArray* dim, const bool keepDim, const aclTensor* out)
{
CHECK_RET(CheckNotNull(self, dim, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDimValid(self, dim), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, dim, keepDim, out), ACLNN_ERR_PARAM_INVALID);
CheckFormat(self);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnAmaxGetWorkspaceSize(
const aclTensor* self, const aclIntArray* dim, bool keepDim, aclTensor* out, uint64_t* workspaceSize,
aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnAmax, DFX_IN(self, dim, keepDim), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, dim, keepDim, 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 (dim->Size() == 0) {
dim = GetAllDims(self, uniqueExecutor.get());
CHECK_RET(dim != 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, dim, keepDim, true, 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 aclnnAmax(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnAmax);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif