* 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_mean.h"
#include "reduce_mean.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/common/op_error_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/platform.h"
#include "opdev/tensor_view_utils.h"
#include "opdev/op_errno.h"
#include "conversion/fill/op_api/fill.h"
#include "op_api/aclnn_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
*
* self ——> Contiguous(workspace_0) ——> Cast(workspace_1) ——>
* Mean(workspace2) ——> Cast(workspace_3) ——> ViewCopy ——> Out
*
*/
static bool CheckNotNull(const aclTensor* self, const aclIntArray* dim, aclTensor* out)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(dim, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_FLOAT16,
op::DataType::DT_INT16, op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_DOUBLE,
op::DataType::DT_COMPLEX64, op::DataType::DT_COMPLEX128};
static const std::initializer_list<op::DataType> ASCEND910B_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_FLOAT16,
op::DataType::DT_INT16, op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_DOUBLE,
op::DataType::DT_BF16, op::DataType::DT_COMPLEX64, op::DataType::DT_COMPLEX128};
static const std::initializer_list<op::DataType> EMPTY_INPUT_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_FLOAT16,
op::DataType::DT_INT16, op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_DOUBLE,
op::DataType::DT_BF16, op::DataType::DT_BOOL};
static const std::initializer_list<op::DataType> NON_CONTIOUS_SUPPORT_DTYPE_SUPPORT_LIST = {op::DataType::DT_FLOAT};
static inline const std::initializer_list<op::DataType>& GetDtypeSupportList()
{
if (GetCurrentPlatformInfo().GetSocVersion() >= SocVersion::ASCEND910B &&
GetCurrentPlatformInfo().GetSocVersion() <= SocVersion::ASCEND910E) {
return ASCEND910B_DTYPE_SUPPORT_LIST;
} else {
return ASCEND910_DTYPE_SUPPORT_LIST;
}
}
static bool CheckDtypeValid(const aclTensor* self, aclDataType dtype, aclTensor* out)
{
auto supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(self, supportList, return false);
if (!CheckType(op::ToOpDataType(dtype), supportList)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "type %s should be in dtype support list [%s].",
op::ToString(op::ToOpDataType(dtype)).GetString(), op::ToString(supportList).GetString());
return false;
}
OP_CHECK_DTYPE_NOT_SUPPORT(out, supportList, return false);
return true;
}
static bool CheckDtypeValidONNX(const aclTensor* self, aclTensor* out)
{
auto supportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(self, supportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(out, supportList, return false);
OP_CHECK_DTYPE_NOT_SAME(self, out, return false);
return true;
}
static bool CheckPromoteType(const aclTensor* self, aclDataType dtype, aclTensor* out)
{
OP_CHECK_RESULT_DTYPE_CAST_FAILED(self->GetDataType(), op::ToOpDataType(dtype), return false);
OP_CHECK_RESULT_DTYPE_CAST_FAILED(op::ToOpDataType(dtype), out->GetDataType(), return false);
return true;
}
static bool CheckDimValid(const aclTensor* self, const aclIntArray* dim)
{
auto selfViewShape = self->GetViewShape();
auto selfDimNum = static_cast<int64_t>(selfViewShape.GetDimNum());
if (selfDimNum <= 0) {
selfDimNum = 1;
}
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 dimMask[64] = {0};
for (size_t i = 0; i < dim->Size(); i++) {
int64_t realDim = dim->operator[](i);
if (realDim < 0) {
realDim = selfDimNum + realDim;
}
if (dimMask[realDim] == 1) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "provided dim %ld is repeated.", dim->operator[](i));
return false;
} else {
dimMask[realDim] = 1;
}
}
return true;
}
constexpr size_t MAX_DIM_LEN = 8;
static bool CheckShape(const aclTensor* self, aclTensor* out)
{
OP_CHECK_MAX_DIM(self, MAX_DIM_LEN, return false);
OP_CHECK_MAX_DIM(out, MAX_DIM_LEN, return false);
return true;
}
static aclnnStatus CheckParams(const aclTensor* self, const aclIntArray* dim, aclDataType dtype, aclTensor* out)
{
CHECK_RET(CheckNotNull(self, dim, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, dtype, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckPromoteType(self, dtype, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDimValid(self, dim), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static aclnnStatus CheckParamsONNX(const aclTensor* self, const aclIntArray* dim, aclTensor* out)
{
CHECK_RET(CheckNotNull(self, dim, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValidONNX(self, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDimValid(self, dim), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static aclnnStatus FillScalar(aclTensor* out, float val, aclOpExecutor* executor)
{
OP_CHECK_DTYPE_NOT_SUPPORT(out, EMPTY_INPUT_DTYPE_SUPPORT_LIST, return ACLNN_ERR_PARAM_INVALID);
FVector<int64_t> shape;
size_t dimNum = out->GetViewShape().GetDimNum();
for (size_t idx = 0; idx < dimNum; idx++) {
int64_t tmpVal = out->GetViewShape().GetDim(idx);
shape.push_back(tmpVal);
}
auto dims = executor->ConvertToTensor(shape.data(), shape.size(), DataType::DT_INT64);
CHECK_RET(dims != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto shapeArray = executor->AllocIntArray(shape.data(), shape.size());
CHECK_RET(shapeArray != nullptr, ACLNN_ERR_INNER_NULLPTR);
FVector<float> valVector = {val};
auto valTensor = executor->ConvertToTensor(valVector.data(), valVector.size(), out->GetDataType());
CHECK_RET(valTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto fillOut = l0op::Fill(dims, valTensor, shapeArray, executor);
CHECK_RET(fillOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(fillOut, out, executor);
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
return ACLNN_SUCCESS;
}
static bool IsNonContiguousSupport(const aclTensor* self, DataType dtype, const aclIntArray* dim)
{
if (!IsRegBase()) {
return false;
}
if (!CheckType(self->GetDataType(), NON_CONTIOUS_SUPPORT_DTYPE_SUPPORT_LIST) || self->GetDataType() != dtype) {
return false;
}
if (!op::IsReduceNonContiguousSupport(self, dim)) {
return false;
}
return true;
}
static void CheckFormat(const aclTensor* self) {
ge::Format selfStorageFormat = self->GetStorageFormat();
if (selfStorageFormat == ge::Format::FORMAT_FRACTAL_NZ) {
OP_LOGW("aclnnMean doesn't support format NZ.");
}
}
aclnnStatus aclnnMeanGetWorkspaceSize(
const aclTensor* self, const aclIntArray* dim, bool keepDim, aclDataType dtype, aclTensor* out,
uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnMean, DFX_IN(self, dim, keepDim, dtype), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, dim, dtype, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
CheckFormat(self);
if (self->IsEmpty()) {
ret = FillScalar(out, NAN, uniqueExecutor.get());
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ret;
}
if (IsNonContiguousSupport(self, op::ToOpDataType(dtype), dim)) {
OP_LOGD("Enter NonContigous");
auto selfContiguous = uniqueExecutor.get()->CreateView(
self, self->GetViewShape(), self->GetStorageShape(), self->GetViewStrides(), self->GetViewOffset());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto meanOpOut = l0op::ReduceMean(selfContiguous, dim, keepDim, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
CHECK_RET(CheckShapeAndScalarSame(meanOpOut, out), ACLNN_ERR_PARAM_INVALID);
auto castMeanOut = l0op::Cast(meanOpOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMeanOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castMeanOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
} else {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfCasted = l0op::Cast(selfContiguous, op::ToOpDataType(dtype), uniqueExecutor.get());
CHECK_RET(selfCasted != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto meanOpOut = l0op::ReduceMean(selfCasted, dim, keepDim, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
CHECK_RET(CheckShapeAndScalarSame(meanOpOut, out), ACLNN_ERR_PARAM_INVALID);
auto castMeanOut = l0op::Cast(meanOpOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMeanOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castMeanOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnMean(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnMean);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
aclnnStatus aclnnMeanV2GetWorkspaceSize(
const aclTensor* self, const aclIntArray* dim, bool keepDim, bool noopWithEmptyAxes, aclTensor* out,
uint64_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnMeanV2, DFX_IN(self, dim, keepDim, noopWithEmptyAxes), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParamsONNX(self, dim, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
CheckFormat(self);
if (self->IsEmpty()) {
ret = FillScalar(out, NAN, uniqueExecutor.get());
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ret;
}
uint64_t dimSize = dim->Size();
if (dimSize == 0 && noopWithEmptyAxes == true) {
CHECK_RET(CheckShapeAndScalarSame(self, out), ACLNN_ERR_PARAM_INVALID);
auto result = l0op::ViewCopy(self, out, uniqueExecutor.get());
CHECK_RET(result != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
if (IsNonContiguousSupport(self, out->GetDataType(), dim)) {
OP_LOGD("Enter NonContigous");
auto selfContiguous = uniqueExecutor.get()->CreateView(
self, self->GetViewShape(), self->GetStorageShape(), self->GetViewStrides(), self->GetViewOffset());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto meanOpOut = l0op::ReduceMean(selfContiguous, dim, keepDim, noopWithEmptyAxes, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
CHECK_RET(CheckShapeAndScalarSame(meanOpOut, out), ACLNN_ERR_PARAM_INVALID);
auto castMeanOut = l0op::Cast(meanOpOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMeanOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castMeanOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
} else {
OP_LOGD("Enter Contigous");
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto meanOpOut = l0op::ReduceMean(selfContiguous, dim, keepDim, noopWithEmptyAxes, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
CHECK_RET(CheckShapeAndScalarSame(meanOpOut, out), ACLNN_ERR_PARAM_INVALID);
auto castMeanOut = l0op::Cast(meanOpOut, out->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMeanOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castMeanOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnMeanV2(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnMeanV2);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif