* 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_var_mean.h"
#include "math/reduce_std_v2_update/op_host/op_api/reduce_std_v2_update.h"
#include "math/reduce_mean/op_api/reduce_mean.h"
#include "reduce_var.h"
#include "aclnn_kernels/reshape.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "math/expand/op_api/expand.h"
#include "aclnn_kernels/transdata.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "op_api/op_api_def.h"
#include "aclnn/aclnn_base.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 "op_api/level2_base_caculation.h"
#include "op_api/aclnn_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16};
static const std::initializer_list<op::DataType> ARCH3510_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_BF16};
static const std::initializer_list<DataType>& GetDtypeSupportList() {
if (IsRegBase()) {
return ARCH3510_DTYPE_SUPPORT_LIST;
} else {
return DTYPE_SUPPORT_LIST;
}
}
static bool CheckDtypeValid(const aclTensor* self, aclTensor* meanOut, aclTensor* varOut) {
auto dtypeSupportList = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(self, dtypeSupportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(meanOut, dtypeSupportList, return false);
OP_CHECK_DTYPE_NOT_SUPPORT(varOut, dtypeSupportList, 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());
selfDimNum = selfDimNum <= 0 ? 1 : selfDimNum;
if (dim == nullptr) {
return true;
}
uint64_t dimMask[64] = {0};
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;
}
if (dim->operator[](i) < 0) {
if (dimMask[selfDimNum + dim->operator[](i)] == 1) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Dim %ld appears multiple times in the list of dims.",
selfDimNum + dim->operator[](i));
return false;
}
dimMask[selfDimNum + dim->operator[](i)] = 1;
continue;
}
if (dimMask[dim->operator[](i)] == 1) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Dim %ld appears multiple times in the list of dims.", dim->operator[](i));
return false;
}
dimMask[dim->operator[](i)] = 1;
}
return true;
}
static bool CheckShape(const aclTensor* self, const aclIntArray* dim, bool keepdim,
aclTensor* meanOut, aclTensor* varOut) {
OP_CHECK_MAX_DIM(self, MAX_SUPPORT_DIMS_NUMS, return false);
op::Shape reduceShape = ReduceShapeGetWithVar(self, dim, keepdim);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(meanOut, reduceShape, return false);
OP_CHECK_SHAPE_NOT_EQUAL_WITH_EXPECTED_SIZE(varOut, reduceShape, return false);
return true;
}
static aclnnStatus CheckParams(const aclTensor* self, const aclIntArray* dim, bool keepdim,
aclTensor* meanOut, aclTensor* varOut) {
CHECK_RET(CheckNotNull3Tensor(self, meanOut, varOut), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, meanOut, varOut), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDimValid(self, dim), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, dim, keepdim, meanOut, varOut), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static aclnnStatus aclnnVarMeanImplUnify(const aclTensor *self, const aclIntArray *dim, int64_t correction,
bool keepdim, aclTensor *varOut, aclTensor *meanOut, uint64_t* workspaceSize,
UniqueExecutor &uniqueExecutor, aclOpExecutor **executor)
{
bool isMeanOut = true;
auto reduceVarOut = l0op::ReduceVar(self, dim, correction, keepdim, isMeanOut, uniqueExecutor.get());
auto varOpOut = std::get<0>(reduceVarOut);
CHECK_RET(varOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut = l0op::Cast(varOpOut, varOut->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(castOut, varOut, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto meanOpOut = std::get<1>(reduceVarOut);
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castOut1 = l0op::Cast(meanOpOut, meanOut->GetDataType(), uniqueExecutor.get());
CHECK_RET(castOut1 != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult1 = l0op::ViewCopy(castOut1, meanOut, uniqueExecutor.get());
CHECK_RET(viewCopyResult1 != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnVarMeanGetWorkspaceSize(const aclTensor* self, const aclIntArray* dim,
int64_t correction, bool keepdim,
aclTensor* varOut, aclTensor* meanOut,
uint64_t* workspaceSize, aclOpExecutor** executor) {
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnVarMean, DFX_IN(self, dim, correction, keepdim), DFX_OUT(varOut, meanOut));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, dim, keepdim, meanOut, varOut);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (self->IsEmpty()) {
ret = CheckFillScalarShapeStdAndVar(varOut, NAN, uniqueExecutor.get());
ret = CheckFillScalarShapeStdAndVar(meanOut, NAN, uniqueExecutor.get());
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ret;
}
const aclIntArray* dimArray = dim;
if (dim == nullptr || dim->Size() == 0) {
dimArray = CalcDimWithVar(self, uniqueExecutor.get());
}
auto selfViewShape = self->GetViewShape();
size_t dimSize = dimArray->Size();
std::vector<int64_t> tmpDim(dimSize, 0);
for (size_t i = 0; i < dimSize; i++) {
auto dimValue = dimArray->operator[](i);
if (dimValue < 0) {
dimValue += selfViewShape.GetDimNum();
}
tmpDim[i] = dimValue;
}
dimArray = uniqueExecutor->AllocIntArray(tmpDim.data(), dimSize);
CHECK_RET(dimArray != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto selfReformat = l0op::ReFormat(selfContiguous, Format::FORMAT_ND, uniqueExecutor.get());
CHECK_RET(selfReformat != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (IsRegBase()) {
return aclnnVarMeanImplUnify(selfReformat, dimArray, correction, keepdim, varOut, meanOut,
workspaceSize, uniqueExecutor, executor);
}
auto meanOpOut = l0op::ReduceMean(selfReformat, dimArray, keepdim, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castMeanOut = l0op::Cast(meanOpOut, meanOut->GetDataType(), uniqueExecutor.get());
CHECK_RET(castMeanOut != nullptr, ACLNN_ERR_PARAM_NULLPTR);
auto viewCopyMeanResult = l0op::ViewCopy(castMeanOut, meanOut, uniqueExecutor.get());
CHECK_RET(viewCopyMeanResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
int64_t shapeProd = 1;
shapeProd = CalcShapeProdStdAndVarMean(self, dimArray);
if ((shapeProd == 1) && (shapeProd <= correction)) {
ret = CheckFillScalarShapeStdAndVar(varOut, NAN, uniqueExecutor.get());
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ret;
}
if ((correction > 1) && (shapeProd <= correction)) {
ret = CheckFillScalarShapeStdAndVar(varOut, INFINITY, uniqueExecutor.get());
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ret;
}
if (keepdim == false) {
meanOpOut = l0op::ReduceMean(selfReformat, dimArray, true, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
FVector<int64_t> shapeVector;
auto selfShape = self->GetViewShape();
size_t selfDimNum = selfShape.GetDimNum();
if (selfDimNum > 0) {
for (size_t i = 0; i < selfDimNum; i++) {
shapeVector.emplace_back(selfShape[i]);
}
auto shapeArray = uniqueExecutor.get()->AllocIntArray(shapeVector.data(), selfDimNum);
CHECK_RET(shapeArray != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (!keepdim) {
FVector<int64_t> reShapeVector(shapeVector);
for (size_t i = 0; i < dimArray->Size(); i++) {
reShapeVector[dimArray->operator[](i)] = 1;
}
auto reShapeArray = uniqueExecutor.get()->AllocIntArray(reShapeVector.data(), selfDimNum);
CHECK_RET(reShapeArray != nullptr, ACLNN_ERR_INNER_NULLPTR);
meanOpOut = l0op::Reshape(meanOpOut, reShapeArray, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
meanOpOut = l0op::Expand(meanOpOut, shapeArray, uniqueExecutor.get());
CHECK_RET(meanOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto varOpOut = l0op::ReduceStdV2UpdateCorrection(selfReformat, meanOpOut, dimArray,
correction, keepdim, uniqueExecutor.get());
CHECK_RET(varOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castVarOut = l0op::Cast(varOpOut, varOut->GetDataType(), uniqueExecutor.get());
CHECK_RET(castVarOut != nullptr, ACLNN_ERR_PARAM_NULLPTR);
auto viewCopyVarResult = l0op::ViewCopy(castVarOut, varOut, uniqueExecutor.get());
CHECK_RET(viewCopyVarResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnVarMean(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream) {
L2_DFX_PHASE_2(aclnnVarMean);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif