* 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.
*/
* \file add_example_tiling.cpp
* \brief Add算子的tiling(分块)策略实现
*
* 本文件提供了tiling逻辑,将大型计算任务划分为较小的块,以便在AI Core上高效并行执行。
* Tiling对于最大化NPU利用率和性能至关重要。
*/
#include "log/log.h"
#include "util/math_util.h"
#include "util/platform_util.h"
#include "op_host/tiling_util.h"
#include "op_host/tiling_templates_registry.h"
#include "examples/add_example/op_kernel/add_example_tiling_data.h"
#include "examples/add_example/op_kernel/add_example_tiling_key.h"
namespace optiling {
constexpr uint32_t WS_SYS_SIZE = 0U;
constexpr int32_t DIMS_LIMIT = 4;
constexpr uint32_t INDEXZERO = 0;
constexpr uint32_t INDEXONE = 1;
constexpr uint32_t INDEXTWO = 2;
constexpr uint32_t INDEXTHREE = 3;
constexpr int64_t BUFFER_NUM = 6;
constexpr int64_t TYPE_SIZE = 4;
static const gert::Shape g_vec_1_shape = {1};
struct AddExampleCompileInfo {
};
* \brief 确保标量shape转换为{1}以保持一致性
*
* 对于标量输入,需要将其转换为1维张量以确保处理的一致性。
*
* \param in_shape 要检查的输入shape
* \return 如果输入是标量则返回{1},否则返回原始shape的引用
*/
inline const gert::Shape &EnsureNotScalar(const gert::Shape &in_shape) {
if (in_shape.IsScalar()) {
return g_vec_1_shape;
}
return in_shape;
}
* \brief 获取平台信息,包括UB(统一缓冲区)大小和AI Core数量
*
* 该函数查询平台以获取硬件资源信息,这对于确定最优tiling策略至关重要。
*
* \param context 指向tiling上下文的指针
* \param ubSize 输出参数:UB大小(字节)
* \param coreNum 输出参数:可用的AI Core数量
* \return 如果成功获取平台信息则返回ge::GRAPH_SUCCESS
*/
static ge::graphStatus GetPlatformInfo(gert::TilingContext* context, uint64_t& ubSize, int64_t& coreNum)
{
fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
coreNum = ascendcPlatform.GetCoreNumAiv();
OP_CHECK_IF(coreNum == 0, OP_LOGE(context, "coreNum is 0"), return ge::GRAPH_FAILED);
ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize);
OP_CHECK_IF(ubSize == 0, OP_LOGE(context, "ubSize is 0"), return ge::GRAPH_FAILED);
return ge::GRAPH_SUCCESS;
}
* \brief 从输入输出张量获取shape和属性信息
*
* 该函数检索并验证张量shape和数据类型。它确保:
* - 所有张量都有4维shape
* - 数据类型是支持的(FLOAT或INT32)
* - 计算总元素数量
*
* \param context 指向tiling上下文的指针
* \param totalIdx 输出参数:要处理的元素总数
* \param dataType 输出参数:输入张量的数据类型
* \return 如果成功检索和验证shape信息则返回ge::GRAPH_SUCCESS
*/
ge::graphStatus GetShapeAttrsInfo(gert::TilingContext* context, int64_t& totalIdx, ge::DataType& dataType)
{
auto inputX = context->GetInputShape(0);
OP_CHECK_NULL_WITH_CONTEXT(context, inputX);
auto inputShapeX = EnsureNotScalar(inputX->GetStorageShape());
auto inputY = context->GetInputShape(1);
OP_CHECK_NULL_WITH_CONTEXT(context, inputY);
auto inputShapeY = EnsureNotScalar(inputY->GetStorageShape());
auto outZ = context->GetOutputShape(0);
OP_CHECK_NULL_WITH_CONTEXT(context, outZ);
auto outShapeZ = EnsureNotScalar(outZ->GetStorageShape());
OP_CHECK_IF(
inputShapeX.GetDimNum() != DIMS_LIMIT || inputShapeY.GetDimNum() != DIMS_LIMIT ||
outShapeZ.GetDimNum() != DIMS_LIMIT,
OP_LOGE(
context, "AddExample: inputx,inputy,outputz shape dim = %zu, %zu, %zu, should be equal 4",
inputShapeX.GetDimNum(), inputShapeY.GetDimNum(), outShapeZ.GetDimNum()),
return ge::GRAPH_FAILED);
totalIdx = inputShapeX.GetShapeSize();
const std::set<ge::DataType> supportedDtype = {ge::DT_FLOAT, ge::DT_INT32};
auto inputDesc = context->GetInputDesc(0);
OP_CHECK_NULL_WITH_CONTEXT(context, inputDesc);
dataType = inputDesc->GetDataType();
if (supportedDtype.count(dataType) == 0) {
OP_LOGE(context, "invalid dtype");
return ge::GRAPH_FAILED;
}
return ge::GRAPH_SUCCESS;
}
* \brief 获取算子执行所需的工作空间大小
*
* 工作空间是计算期间使用的额外内存。对于Add算子,
* 不需要工作空间,因此大小设置为0。
*
* \param context 指向tiling上下文的指针
* \return
*/
ge::graphStatus GetWorkspaceSize(gert::TilingContext* context)
{
size_t* currentWorkspace = context->GetWorkspaceSizes(1);
OP_CHECK_NULL_WITH_CONTEXT(context, currentWorkspace);
currentWorkspace[0] = WS_SYS_SIZE;
return ge::GRAPH_SUCCESS;
}
* \brief Add算子的主tiling函数
*
* 该函数计算最优的tiling策略:
* 1. 检索平台信息(UB大小、AI Core数量)
* 2. 检索shape和属性信息
* 3. 计算Core tiling:将工作划分到多个AI Core以实现并行
* 4. 计算UB tiling:根据可用的UB内存在每个Core内划分工作
* 5. 根据数据类型设置tiling key(FLOAT和INT32有不同的tiling策略)
*
* Tiling数据存储在AddExampleTilingData结构中,将被传递给kernel。
*
* \param context 指向tiling上下文的指针
* \return 如果成功计算tiling策略则返回ge::GRAPH_SUCCESS
*/
static ge::graphStatus AddExampleTilingFunc(gert::TilingContext* context)
{
uint64_t ubSize;
int64_t coreNum;
OP_CHECK_IF(
GetPlatformInfo(context, ubSize, coreNum) != ge::GRAPH_SUCCESS,
OP_LOGE(context, "GetPlatformInfo error"),
return ge::GRAPH_FAILED);
int64_t totalIdx;
ge::DataType dataType;
OP_CHECK_IF(
GetShapeAttrsInfo(context, totalIdx, dataType) != ge::GRAPH_SUCCESS,
OP_LOGE(context, "GetShapeAttrsInfo error"),
return ge::GRAPH_FAILED);
OP_CHECK_IF(
GetWorkspaceSize(context) != ge::GRAPH_SUCCESS,
OP_LOGE(context, "GetWorkspaceSize error"),
return ge::GRAPH_FAILED);
AddExampleTilingData* tiling = context->GetTilingData<AddExampleTilingData>();
OP_CHECK_NULL_WITH_CONTEXT(context, tiling);
OP_CHECK_IF(
memset_s(tiling, sizeof(AddExampleTilingData), 0, sizeof(AddExampleTilingData)) != EOK,
OP_LOGE(context, "set tiling data error"),
return ge::GRAPH_FAILED);
tiling->totalNum = totalIdx;
tiling->blockFactor = Ops::Base::CeilDiv(totalIdx, coreNum);
int64_t usedCoreNum = Ops::Base::CeilDiv(totalIdx, tiling->blockFactor);
int64_t ubCanUse = static_cast<int64_t>(ubSize);
int64_t ubBlockSize = Ops::Base::GetUbBlockSize(context);
tiling->ubFactor = Ops::Base::FloorAlign(Ops::Base::FloorDiv((ubCanUse / TYPE_SIZE), BUFFER_NUM), ubBlockSize);
context->SetBlockDim(usedCoreNum);
uint64_t tilingKey = 0;
if (dataType == ge::DT_FLOAT) {
tilingKey = GET_TPL_TILING_KEY(ELEMENTWISE_TPL_SCH_MODE_0);
context->SetTilingKey(tilingKey);
} else if (dataType == ge::DT_INT32) {
tilingKey = GET_TPL_TILING_KEY(ELEMENTWISE_TPL_SCH_MODE_1);
context->SetTilingKey(tilingKey);
} else {
OP_LOGE(context, "get dtype error");
return ge::GRAPH_FAILED;
}
return ge::GRAPH_SUCCESS;
}
* \brief 解析Add算子的tiling信息
*
* 该函数在模型加载期间被调用,以解析任何静态tiling信息。
* 对于Add算子,不需要静态tiling,因此直接返回成功。
*
* \param context 指向tiling解析上下文的指针
* \return ge::GRAPH_SUCCESS
*/
static ge::graphStatus TilingParseForAddExample([[maybe_unused]] gert::TilingParseContext* context)
{
return ge::GRAPH_SUCCESS;
}
IMPL_OP_OPTILING(AddExample).Tiling(AddExampleTilingFunc).TilingParse<AddExampleCompileInfo>(TilingParseForAddExample);
}