* 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_aminmax_all.h"
#include "reduce_min.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/cast.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/op_log.h"
#include "opdev/shape_utils.h"
#include "opdev/tensor_view_utils.h"
#include "opdev/platform.h"
#include "math/reduce_max/op_api/reduce_max.h"
#include "op_api/aclnn_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const size_t MAX_DIM_LEN = 8;
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_INT32, op::DataType::DT_UINT8,
op::DataType::DT_BOOL,
op::DataType::DT_INT8, op::DataType::DT_INT16, op::DataType::DT_INT64, op::DataType::DT_DOUBLE};
static const std::initializer_list<op::DataType> ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_INT32, op::DataType::DT_UINT8,
op::DataType::DT_BOOL, op::DataType::DT_BF16,
op::DataType::DT_INT8, op::DataType::DT_INT16, op::DataType::DT_INT64, op::DataType::DT_DOUBLE};
static inline bool CheckNotNull(const aclTensor *self, const aclTensor *minOut, const aclTensor *maxOut) {
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(minOut, return false);
OP_CHECK_NULL(maxOut, return false);
return true;
}
static const std::initializer_list<DataType>& GetDtypeSupportList() {
if (GetCurrentPlatformInfo().GetSocVersion() == SocVersion::ASCEND910B ||
GetCurrentPlatformInfo().GetSocVersion() == SocVersion::ASCEND910_93 ||
IsRegBase()) {
return ASCEND910B_DTYPE_DTYPE_SUPPORT_LIST;
} else {
return ASCEND910_DTYPE_DTYPE_SUPPORT_LIST;
}
}
static inline bool CheckDtypeValid(const aclTensor *self, const aclTensor *minOut, const aclTensor *maxOut) {
const auto& supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(self, supportList, return false);
OP_CHECK_DTYPE_NOT_MATCH(minOut, self->GetDataType(), return false);
OP_CHECK_DTYPE_NOT_MATCH(maxOut, self->GetDataType(), return false);
return true;
}
static inline bool CheckShape(const aclTensor *self, const aclTensor *minOut, const aclTensor *maxOut) {
OP_CHECK_MAX_DIM(self, MAX_DIM_LEN, return false);
OP_CHECK_MAX_DIM(minOut, MAX_DIM_LEN, return false);
OP_CHECK_MAX_DIM(maxOut, MAX_DIM_LEN, return false);
return true;
}
static inline aclnnStatus CheckParams(const aclTensor *self, const aclTensor *minOut, const aclTensor *maxOut) {
CHECK_RET(CheckNotNull(self, minOut, maxOut), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, minOut, maxOut), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, minOut, maxOut), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnAminmaxAllGetWorkspaceSize(const aclTensor *self, aclTensor *minOut, aclTensor *maxOut,
uint64_t *workspaceSize, aclOpExecutor **executor) {
L2_DFX_PHASE_1(aclnnAminmaxAll, DFX_IN(self), DFX_OUT(minOut, maxOut));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, minOut, maxOut);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (self->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
if (self->GetViewShape().GetDimNum() == 0) {
auto viewCopyMinResult = l0op::ViewCopy(self, minOut, uniqueExecutor.get());
CHECK_RET(viewCopyMinResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyMaxResult = l0op::ViewCopy(self, maxOut, uniqueExecutor.get());
CHECK_RET(viewCopyMaxResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
size_t dimDum = self->GetViewShape().GetDimNum();
int64_t appendDim[dimDum];
for (size_t i = 0; i < dimDum; i++) {
appendDim[i] = i;
}
auto dim = uniqueExecutor.get()->AllocIntArray(appendDim, dimDum);
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCasted = selfContiguous->GetDataType() == op::DataType::DT_BOOL ?
l0op::Cast(selfContiguous, op::DataType::DT_UINT8, uniqueExecutor.get()) : selfContiguous;
CHECK_RET(selfCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor *min = l0op::ReduceMin(selfCasted, dim, false, uniqueExecutor.get());
CHECK_RET(min != nullptr, ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckShapeAndScalarSame(min, minOut), ACLNN_ERR_PARAM_INVALID);
const aclTensor *max = l0op::ReduceMax(selfCasted, dim, false, true, uniqueExecutor.get());
CHECK_RET(max != nullptr, ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckShapeAndScalarSame(max, maxOut), ACLNN_ERR_PARAM_INVALID);
auto castMinOut = l0op::Cast(min, minOut->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMinOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castMaxOut = l0op::Cast(max, maxOut->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMaxOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyMinResult = l0op::ViewCopy(castMinOut, minOut, uniqueExecutor.get());
CHECK_RET(viewCopyMinResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyMaxResult = l0op::ViewCopy(castMaxOut, maxOut, uniqueExecutor.get());
CHECK_RET(viewCopyMaxResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnAminmaxAll(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnAminmaxAll);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif