* 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 "defalut_reg_func.h"
namespace af {
namespace ascir {
std::vector<std::unique_ptr<TmpBufDesc>> CalcWelfordUpdateTmpSize(const AscNode &node) {
AscNodeInputs node_inputs = node.inputs;
uint32_t input_id = GetNonScalarAxisId(node_inputs);
const auto& attr = node_inputs[input_id].attr;
constexpr uint32_t WEL_UP_REP_SIZE = 256;
constexpr uint32_t WEL_UP_FLOAT_SIZE = WEL_UP_REP_SIZE / sizeof(float);
constexpr uint32_t WEL_UP_MIN_VALUE = 2 * WEL_UP_REP_SIZE;
const Expression rn_len = attr.repeats[0];
const Expression ab_len = attr.repeats.size() >= 2 ? attr.repeats[1] : Symbol(1);
const Expression total_elements = sym::Mul(rn_len, ab_len);
const Expression blocks = sym::Div(sym::Add(total_elements, Symbol(WEL_UP_FLOAT_SIZE - 1)),
Symbol(WEL_UP_FLOAT_SIZE));
const Expression total_size = sym::Mul(blocks, Symbol(WEL_UP_MIN_VALUE));
GELOGD("Node %s[%s] WelfordUpdate tmp buffer size: %s (rn_len: %s, ab_len: %s)",
node.GetTypePtr(), node.GetNamePtr(), total_size.Str().get(),
rn_len.Str().get(), ab_len.Str().get());
return GetTmpBuffer(total_size);
}
std::vector<std::unique_ptr<TmpBufDesc>> CalcWelfordFinalizeTmpSize(const AscNode &node) {
AscNodeInputs node_inputs = node.inputs;
uint32_t input_id = GetNonScalarAxisId(node_inputs);
if (input_id == UINT32_MAX) {
input_id = 0;
}
const auto& attr = node_inputs[input_id].attr;
constexpr uint32_t BASICBLOCK_UNIT = 64;
constexpr uint32_t MIN_VALUE = BASICBLOCK_UNIT * 4 * sizeof(float);
const Expression ab_len = attr.repeats[0];
const Expression var_size = sym::Add(Symbol(BASICBLOCK_UNIT * 2 * sizeof(float)),
sym::Mul(ab_len, Symbol(2 * sizeof(float))));
const Expression total_size = sym::Max(Symbol(MIN_VALUE), var_size);
GELOGD("Node %s[%s] WelfordFinalize tmp buffer size: %s (ab_len: %s)",
node.GetTypePtr(), node.GetNamePtr(), total_size.Str().get(),
ab_len.Str().get());
return GetTmpBuffer(total_size);
}
}
}