| @@ -10,160 +10,179 @@ | |||||||||||||||
| 10 | 10 | ||||||||||||||
| 11 | /*! | 11 | /*! | ||||||||||||
| 12 | * \file foreach_log_tiling.cpp | 12 | * \file foreach_log_tiling.cpp | ||||||||||||
| 13 | - * \brief tiling implementation for foreach_log (arch35 / Ascend950) | 13 | + * \brief Operator-owned validation and dual-split tiling for ForeachLog | ||||||||||||
| 14 | + * (arch35 / Ascend950). Lists whose average tensor is small (avg <= | ||||||||||||||
| 15 | + * TENSOR_BALANCED_AVG) split cores by whole consecutive tensors so each | ||||||||||||||
| 16 | + * core drives at most a couple of per-tensor DMA pipelines — per-tensor | ||||||||||||||
| 17 | + * pipeline overhead dominates when a core's range covers many tiny | ||||||||||||||
| 18 | + * tensors; larger tensors keep the shared element-balanced split. | ||||||||||||||
| 14 | */ | 19 | */ | ||||||||||||
| 15 | 20 | ||||||||||||||
| 21 | +#include <algorithm> | ||||||||||||||
| 16 | #include <limits> | 22 | #include <limits> | ||||||||||||
| 17 | -#include "log/log.h" | 23 | +#include "register/op_impl_registry.h" | ||||||||||||
| 18 | -#include "util/math_util.h" | ||||||||||||||
| 19 | #include "util/platform_util.h" | 24 | #include "util/platform_util.h" | ||||||||||||
| 20 | -#include "op_host/tiling_util.h" | 25 | +#include "../../../foreach_utils/op_host/foreach_flat_regbase_validator.h" | ||||||||||||
| 21 | -#include "op_host/tiling_templates_registry.h" | 26 | +#include "../op_kernel/arch35/foreach_log_tiling_data.h" | ||||||||||||
| 22 | -#include "../../op_kernel/arch35/foreach_log_tiling_data.h" | ||||||||||||||
| 23 | -#include "../../op_kernel/arch35/foreach_log_tiling_key.h" | ||||||||||||||
| 24 | 27 | ||||||||||||||
| 25 | namespace optiling { | 28 | namespace optiling { | ||||||||||||
| 26 | 29 | ||||||||||||||
| 27 | -using namespace Ops::NN::OpTiling; | ||||||||||||||
| 28 | - | ||||||||||||||
| 29 | -constexpr uint32_t WS_SYS_SIZE = 0U; | ||||||||||||||
| 30 | -constexpr int32_t INPUT_IDX_0 = 0; | ||||||||||||||
| 31 | -constexpr uint32_t DCACHE_SIZE = 128 * 1024; | ||||||||||||||
| 32 | -constexpr int64_t MIN_PER_CORE = 1024; | ||||||||||||||
| 33 | -constexpr int32_t MAX_TENSOR_NUM = 256; | ||||||||||||||
| 34 | -constexpr size_t MAX_SUPPORT_DIMS_NUMS = 8; | ||||||||||||||
| 35 | - | ||||||||||||||
| 36 | struct ForeachLogCompileInfo {}; | 30 | struct ForeachLogCompileInfo {}; | ||||||||||||
| 37 | 31 | ||||||||||||||
| 38 | -static ge::graphStatus GetPlatformInfo(gert::TilingContext* context, uint64_t& ubSize, int64_t& coreNum) | 32 | +static bool IsForeachLogDtype(ge::DataType dtype) { return ForeachFlatRegbaseValidation::IsFloatFamily(dtype); } | ||||||||||||
| 33 | + | ||||||||||||||
| 34 | +static int64_t CeilDivInt64(int64_t value, int64_t divisor) | ||||||||||||||
| 39 | { | 35 | { | ||||||||||||
| 40 | - fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo(); | 36 | + return value / divisor + static_cast<int64_t>(value % divisor != 0); | ||||||||||||
| 41 | - OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr); | ||||||||||||||
| 42 | - auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr); | ||||||||||||||
| 43 | - coreNum = ascendcPlatform.GetCoreNumAiv(); | ||||||||||||||
| 44 | - OP_CHECK_IF(coreNum == 0, OP_LOGE(context, "coreNum is 0"), return ge::GRAPH_FAILED); | ||||||||||||||
| 45 | - ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); | ||||||||||||||
| 46 | - OP_CHECK_IF(ubSize == 0, OP_LOGE(context, "ubSize is 0"), return ge::GRAPH_FAILED); | ||||||||||||||
| 47 | - return ge::GRAPH_SUCCESS; | ||||||||||||||
| 48 | } | 37 | } | ||||||||||||
| 49 | 38 | ||||||||||||||
| 50 | -static ge::graphStatus FillTensorListInfo(gert::TilingContext* context, ForeachLogTilingData* tiling, | 39 | +static void LocateFlatOffset(int64_t flatOffset, int32_t tensorCount, const int64_t* tensorDataCountList, | ||||||||||||
| 51 | - uint64_t tensorNum, ge::DataType& dataType, int64_t& totalElements) | 40 | + uint16_t& tensorIndex, int64_t& tensorOffset) | ||||||||||||
| 52 | { | 41 | { | ||||||||||||
| 53 | - // An empty tensor list is legal (consistent with the aclnn entry): keep the default dtype | 42 | + for (int32_t index = 0; index < tensorCount; ++index) { | ||||||||||||
| 54 | - // for the tiling key and a zero element total. | 43 | + if (flatOffset < tensorDataCountList[index]) { | ||||||||||||
| 55 | - if (tensorNum == 0) { | 44 | + tensorIndex = static_cast<uint16_t>(index); | ||||||||||||
| 56 | - return ge::GRAPH_SUCCESS; | 45 | + tensorOffset = flatOffset; | ||||||||||||
| 57 | - } | 46 | + return; | ||||||||||||
| 58 | - for (uint64_t i = 0; i < tensorNum; i++) { | ||||||||||||||
| 59 | - auto memberDesc = context->GetDynamicInputDesc(INPUT_IDX_0, i); | ||||||||||||||
| 60 | - OP_CHECK_NULL_WITH_CONTEXT(context, memberDesc); | ||||||||||||||
| 61 | - // The tiling key is chosen from a single dtype, so every member must share it; a mixed | ||||||||||||||
| 62 | - // list would otherwise be computed with the first member's dtype. | ||||||||||||||
| 63 | - if (i == 0) { | ||||||||||||||
| 64 | - dataType = memberDesc->GetDataType(); | ||||||||||||||
| 65 | - } else { | ||||||||||||||
| 66 | - OP_CHECK_IF(memberDesc->GetDataType() != dataType, | ||||||||||||||
| 67 | - OP_LOGE(context, "the dtypes of all tensors in the tensor list must be the same"), | ||||||||||||||
| 68 | - return ge::GRAPH_FAILED); | ||||||||||||||
| 69 | } | 47 | } | ||||||||||||
| 70 | - auto shapePtr = context->GetDynamicInputShape(INPUT_IDX_0, i); | 48 | + flatOffset -= tensorDataCountList[index]; | ||||||||||||
| 71 | - OP_CHECK_NULL_WITH_CONTEXT(context, shapePtr); | ||||||||||||||
| 72 | - auto storageShape = shapePtr->GetStorageShape(); | ||||||||||||||
| 73 | - // The arch22 common tiling rejects tensors with more than 8 dims; keep arch35 | ||||||||||||||
| 74 | - // consistent so a rank that the aclnn entry rejects cannot pass in graph mode. | ||||||||||||||
| 75 | - OP_CHECK_IF(storageShape.GetDimNum() > MAX_SUPPORT_DIMS_NUMS, | ||||||||||||||
| 76 | - OP_LOGE(context, "tensor %lu has %zu dims, which exceeds the limit %zu", i, | ||||||||||||||
| 77 | - storageShape.GetDimNum(), MAX_SUPPORT_DIMS_NUMS), | ||||||||||||||
| 78 | - return ge::GRAPH_FAILED); | ||||||||||||||
| 79 | - int64_t numel = storageShape.GetShapeSize(); | ||||||||||||||
| 80 | - // A negative size means an unresolved/unknown dimension (e.g. -1/-2) and cannot be | ||||||||||||||
| 81 | - // converted into a concrete element count. | ||||||||||||||
| 82 | - OP_CHECK_IF(numel < 0, OP_LOGE(context, "tensor %lu has unknown or negative shape size", i), | ||||||||||||||
| 83 | - return ge::GRAPH_FAILED); | ||||||||||||||
| 84 | - // Checked accumulation: totalElements feeds CeilDiv/BlockDim below, so an int64 | ||||||||||||||
| 85 | - // overflow would silently corrupt the core split. | ||||||||||||||
| 86 | - OP_CHECK_IF(numel > std::numeric_limits<int64_t>::max() - totalElements, | ||||||||||||||
| 87 | - OP_LOGE(context, "total elements overflow at tensor %lu", i), return ge::GRAPH_FAILED); | ||||||||||||||
| 88 | - tiling->tensorElements[i] = numel; | ||||||||||||||
| 89 | - totalElements += numel; | ||||||||||||||
| 90 | } | 49 | } | ||||||||||||
| 91 | - return ge::GRAPH_SUCCESS; | 50 | + tensorIndex = static_cast<uint16_t>(tensorCount - 1); | ||||||||||||
| 51 | + tensorOffset = tensorDataCountList[tensorIndex] - 1; | ||||||||||||||
| 92 | } | 52 | } | ||||||||||||
| 93 | 53 | ||||||||||||||
| 54 | +// Lists whose average tensor is no larger than this many elements split cores | ||||||||||||||
| 55 | +// by whole tensors; per-core per-tensor pipeline overhead dominates otherwise. | ||||||||||||||
| 56 | +constexpr int64_t TENSOR_BALANCED_AVG = 32768; | ||||||||||||||
| 57 | +constexpr uint32_t BUFFER_DEPTH = 2U; | ||||||||||||||
| 58 | +constexpr uint32_t DMA_ALIGN_BYTES = 32U; | ||||||||||||||
| 59 | +constexpr int64_t MIN_CORE_WORK_BYTES = 4096; | ||||||||||||||
| 60 | +constexpr uint64_t WORKSPACE_BYTES = 32U; | ||||||||||||||
| 61 | + | ||||||||||||||
| 94 | static ge::graphStatus ForeachLogTilingFunc(gert::TilingContext* context) | 62 | static ge::graphStatus ForeachLogTilingFunc(gert::TilingContext* context) | ||||||||||||
| 95 | { | 63 | { | ||||||||||||
| 96 | - // 1. Get platform info | 64 | + if (ForeachFlatRegbaseValidation::ValidateHomogeneous<1>(context, IsForeachLogDtype) != ge::GRAPH_SUCCESS) { | ||||||||||||
| 97 | - uint64_t ubSize; | ||||||||||||||
| 98 | - int64_t coreNum; | ||||||||||||||
| 99 | - OP_CHECK_IF(GetPlatformInfo(context, ubSize, coreNum) != ge::GRAPH_SUCCESS, | ||||||||||||||
| 100 | - OP_LOGE(context, "GetPlatformInfo error"), return ge::GRAPH_FAILED); | ||||||||||||||
| 101 | - | ||||||||||||||
| 102 | - // 2. Get tensor list info | ||||||||||||||
| 103 | - auto computeNodeInfoPtr = context->GetComputeNodeInfo(); | ||||||||||||||
| 104 | - OP_CHECK_NULL_WITH_CONTEXT(context, computeNodeInfoPtr); | ||||||||||||||
| 105 | - auto idxInstanceInfoPtr = computeNodeInfoPtr->GetInputInstanceInfo(INPUT_IDX_0); | ||||||||||||||
| 106 | - OP_CHECK_NULL_WITH_CONTEXT(context, idxInstanceInfoPtr); | ||||||||||||||
| 107 | - uint64_t tensorNum = idxInstanceInfoPtr->GetInstanceNum(); | ||||||||||||||
| 108 | - // The tiling data arrays are sized to MAX_TENSOR_NUM; accepting more members would | ||||||||||||||
| 109 | - // silently drop the tail, so reject the oversized list instead. | ||||||||||||||
| 110 | - OP_CHECK_IF(tensorNum > MAX_TENSOR_NUM, | ||||||||||||||
| 111 | - OP_LOGE(context, "tensorNum[%lu] should be less than or equal to %u", tensorNum, MAX_TENSOR_NUM), | ||||||||||||||
| 112 | - return ge::GRAPH_FAILED); | ||||||||||||||
| 113 | - | ||||||||||||||
| 114 | - // 3. Fill tiling data | ||||||||||||||
| 115 | - ForeachLogTilingData* tiling = context->GetTilingData<ForeachLogTilingData>(); | ||||||||||||||
| 116 | - OP_CHECK_NULL_WITH_CONTEXT(context, tiling); | ||||||||||||||
| 117 | - OP_CHECK_IF(memset_s(tiling, sizeof(ForeachLogTilingData), 0, sizeof(ForeachLogTilingData)) != EOK, | ||||||||||||||
| 118 | - OP_LOGE(context, "set tiling data error"), return ge::GRAPH_FAILED); | ||||||||||||||
| 119 | - | ||||||||||||||
| 120 | - // 4. Collect per-member dtype and element counts. An empty tensor list is legal | ||||||||||||||
| 121 | - // (consistent with the aclnn entry), so the scan is skipped and the default dtype kept. | ||||||||||||||
| 122 | - ge::DataType dataType = ge::DT_FLOAT; | ||||||||||||||
| 123 | - tiling->tensorCount = static_cast<int32_t>(tensorNum); | ||||||||||||||
| 124 | - int64_t totalElements = 0; | ||||||||||||||
| 125 | - OP_CHECK_IF(FillTensorListInfo(context, tiling, tensorNum, dataType, totalElements) != ge::GRAPH_SUCCESS, | ||||||||||||||
| 126 | - OP_LOGE(context, "fill tensor list info failed"), return ge::GRAPH_FAILED); | ||||||||||||||
| 127 | - tiling->totalElements = totalElements; | ||||||||||||||
| 128 | - | ||||||||||||||
| 129 | - // 5. Calculate core split | ||||||||||||||
| 130 | - if (totalElements == 0) { | ||||||||||||||
| 131 | - tiling->needCoreNum = 1; | ||||||||||||||
| 132 | - context->SetBlockDim(1); | ||||||||||||||
| 133 | - } else { | ||||||||||||||
| 134 | - int64_t needCoreNum = std::min(coreNum, Ops::Base::CeilDiv(totalElements, MIN_PER_CORE)); | ||||||||||||||
| 135 | - // A non-positive core count would make SetBlockDim invalid. | ||||||||||||||
| 136 | - OP_CHECK_IF(needCoreNum <= 0, OP_LOGE(context, "needCoreNum is invalid"), return ge::GRAPH_FAILED); | ||||||||||||||
| 137 | - tiling->needCoreNum = static_cast<int32_t>(needCoreNum); | ||||||||||||||
| 138 | - context->SetBlockDim(static_cast<uint32_t>(needCoreNum)); | ||||||||||||||
| 139 | - } | ||||||||||||||
| 140 | - | ||||||||||||||
| 141 | - // 6. Set workspace | ||||||||||||||
| 142 | - size_t* currentWorkspace = context->GetWorkspaceSizes(1); | ||||||||||||||
| 143 | - OP_CHECK_NULL_WITH_CONTEXT(context, currentWorkspace); | ||||||||||||||
| 144 | - currentWorkspace[0] = WS_SYS_SIZE; | ||||||||||||||
| 145 | - | ||||||||||||||
| 146 | - // 7. Set local memory size. DCACHE_SIZE is reserved for the cache, so UB must be | ||||||||||||||
| 147 | - // strictly larger: at equality the result is 0, and below it the unsigned | ||||||||||||||
| 148 | - // subtraction wraps around before the uint32 narrowing. | ||||||||||||||
| 149 | - OP_CHECK_IF(ubSize <= DCACHE_SIZE, OP_LOGE(context, "ubSize[%lu] should be greater than %u", ubSize, DCACHE_SIZE), | ||||||||||||||
| 150 | - return ge::GRAPH_FAILED); | ||||||||||||||
| 151 | - context->SetLocalMemorySize(static_cast<uint32_t>(ubSize - DCACHE_SIZE)); | ||||||||||||||
| 152 | - | ||||||||||||||
| 153 | - // 8. Set tiling key by dtype | ||||||||||||||
| 154 | - uint64_t tilingKey = 0; | ||||||||||||||
| 155 | - if (dataType == ge::DT_FLOAT) { | ||||||||||||||
| 156 | - tilingKey = GET_TPL_TILING_KEY(FOREACH_LOG_TPL_SCH_MODE_FLOAT); | ||||||||||||||
| 157 | - } else if (dataType == ge::DT_FLOAT16) { | ||||||||||||||
| 158 | - tilingKey = GET_TPL_TILING_KEY(FOREACH_LOG_TPL_SCH_MODE_FLOAT16); | ||||||||||||||
| 159 | - } else if (dataType == ge::DT_BF16) { | ||||||||||||||
| 160 | - tilingKey = GET_TPL_TILING_KEY(FOREACH_LOG_TPL_SCH_MODE_BF16); | ||||||||||||||
| 161 | - } else { | ||||||||||||||
| 162 | - OP_LOGE(context, "unsupported dtype for foreach_log"); | ||||||||||||||
| 163 | return ge::GRAPH_FAILED; | 65 | return ge::GRAPH_FAILED; | ||||||||||||
| 164 | } | 66 | } | ||||||||||||
| 165 | - context->SetTilingKey(tilingKey); | ||||||||||||||
| 166 | 67 | ||||||||||||||
| 68 | + auto platformInfo = context->GetPlatformInfo(); | ||||||||||||||
| 69 | + auto computeNodeInfo = context->GetComputeNodeInfo(); | ||||||||||||||
| 70 | + if (platformInfo == nullptr || computeNodeInfo == nullptr) { | ||||||||||||||
| 71 | + OP_LOGE(context, "platform or compute-node metadata is null"); | ||||||||||||||
| 72 | + return ge::GRAPH_FAILED; | ||||||||||||||
| 73 | + } | ||||||||||||||
| 74 | + auto platform = platform_ascendc::PlatformAscendC(platformInfo); | ||||||||||||||
| 75 | + uint64_t platformCoreNum = platform.GetCoreNumAiv(); | ||||||||||||||
| 76 | + uint64_t ubSize = 0; | ||||||||||||||
| 77 | + platform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); | ||||||||||||||
| 78 | + if (platformCoreNum == 0U || ubSize == 0U) { | ||||||||||||||
| 79 | + OP_LOGE(context, "invalid platform resources: coreNum=%lu, ubSize=%lu", platformCoreNum, ubSize); | ||||||||||||||
| 80 | + return ge::GRAPH_FAILED; | ||||||||||||||
| 81 | + } | ||||||||||||||
| 82 | + | ||||||||||||||
| 83 | + const int32_t tensorCount = static_cast<int32_t>(computeNodeInfo->GetInputInstanceInfo(0)->GetInstanceNum()); | ||||||||||||||
🟠 High Priority 变更行: 被删除的旧实现明确做过这个校验: 失败模式: 修复方向:在取到 建议:在拿到 tensorCount 后立刻做上限校验:tensorCount 超过 FOREACH_LOG_MAX_TENSOR_COUNT(256) 时 OP_LOGE 并返回 GRAPH_FAILED,避免后续写 tensorDataCountList 越界。 Suggestions
![]() ![]() Incorrect? | |||||||||||||||
| 84 | + ForeachLogTilingData* tilingData = context->GetTilingData<ForeachLogTilingData>(); | ||||||||||||||
| 85 | + if (tilingData == nullptr) { | ||||||||||||||
| 86 | + OP_LOGE(context, "tiling data storage is null"); | ||||||||||||||
| 87 | + return ge::GRAPH_FAILED; | ||||||||||||||
| 88 | + } | ||||||||||||||
| 89 | + *tilingData = ForeachLogTilingData{}; | ||||||||||||||
| 90 | + | ||||||||||||||
| 91 | + int64_t totalDataCount = 0; | ||||||||||||||
| 92 | + for (int32_t tensorIndex = 0; tensorIndex < tensorCount; ++tensorIndex) { | ||||||||||||||
| 93 | + int64_t dataCount = context->GetDynamicInputShape(0, tensorIndex)->GetStorageShape().GetShapeSize(); | ||||||||||||||
| 94 | + // The in-tensor offsets and per-core range fields below are int32 to | ||||||||||||||
| 95 | + // keep the tiling struct under the ~4KB launch budget; a tensor whose | ||||||||||||||
| 96 | + // element count cannot be represented there would overflow the fields | ||||||||||||||
| 97 | + // the kernel consumes as GM cursors. | ||||||||||||||
| 98 | + OP_CHECK_IF(dataCount > std::numeric_limits<int32_t>::max(), | ||||||||||||||
| 99 | + OP_LOGE(context, "tensor %d has %lld elements, exceeding the int32 offset limit", tensorIndex, | ||||||||||||||
| 100 | + static_cast<long long>(dataCount)), | ||||||||||||||
| 101 | + return ge::GRAPH_FAILED); | ||||||||||||||
🟡 Medium Priority 变更行: 旧实现明确拒绝负值: 失败模式(触发条件:列表中某个成员张量的 storage shape 仍含未解析维, 修复方向:把上界校验扩展成 建议:把 98-101 行的校验改为双边:dataCount < 0(未解析/未知维)或 dataCount > INT32_MAX 都 OP_LOGE 并返回失败,恢复旧实现对负元素数的拒绝。 Suggestions
![]() ![]() Incorrect? | |||||||||||||||
| 102 | + tilingData->tensorDataCountList[tensorIndex] = dataCount; | ||||||||||||||
| 103 | + totalDataCount += dataCount; | ||||||||||||||
| 104 | + } | ||||||||||||||
| 105 | + tilingData->tensorCount = tensorCount; | ||||||||||||||
| 106 | + | ||||||||||||||
| 107 | + ge::DataType dataType = context->GetDynamicInputDesc(0, 0)->GetDataType(); | ||||||||||||||
🟡 Medium Priority 变更行: 旧实现对每一处都做了 失败模式:两种都不好——(a) 空 TensorList 输入时 tiling 空指针崩溃(tiling 进程 segfault,图编译失败);(b) 若共享校验器 修复方向:对 建议:为 GetInputInstanceInfo(0)、GetDynamicInputShape(0, i)、GetDynamicInputDesc(0, 0) 补空指针检查;空列表(tensorCount==0)按旧实现提前返回成功(BlockDim=1、设置 UB/workspace/tilingKey),保持与 aclnn 入口一致的合法性。 ![]() ![]() Incorrect? | |||||||||||||||
| 108 | + uint32_t storageBytes = (dataType == ge::DT_FLOAT) ? sizeof(float) : sizeof(uint16_t); | ||||||||||||||
| 109 | + // One double-buffered input queue plus one double-buffered output queue. | ||||||||||||||
| 110 | + uint64_t bytesPerElement = static_cast<uint64_t>(BUFFER_DEPTH) * static_cast<uint64_t>(storageBytes) * 2U; | ||||||||||||||
| 111 | + uint64_t rawTileElements = ubSize / bytesPerElement; | ||||||||||||||
| 112 | + uint64_t alignElements = DMA_ALIGN_BYTES / storageBytes; | ||||||||||||||
| 113 | + uint64_t tileElements = rawTileElements / alignElements * alignElements; | ||||||||||||||
| 114 | + if (tileElements == 0U || tileElements > 0xFFFFFFFFU) { | ||||||||||||||
| 115 | + OP_LOGE(context, "UB is insufficient for the hybrid transport tile"); | ||||||||||||||
| 116 | + return ge::GRAPH_FAILED; | ||||||||||||||
| 117 | + } | ||||||||||||||
| 118 | + tilingData->tileElems = static_cast<uint32_t>(tileElements); | ||||||||||||||
| 119 | + | ||||||||||||||
| 120 | + const int64_t limitedCoreNum = static_cast<int64_t>( | ||||||||||||||
| 121 | + std::min<uint64_t>(platformCoreNum, FOREACH_LOG_MAX_CORE_COUNT)); | ||||||||||||||
| 122 | + if (tensorCount > 0 && totalDataCount / tensorCount <= TENSOR_BALANCED_AVG) { | ||||||||||||||
| 123 | + // Tensor-balanced split: whole consecutive tensors per core. | ||||||||||||||
| 124 | + const int64_t coreCount = std::min<int64_t>(limitedCoreNum, tensorCount); | ||||||||||||||
| 125 | + const int64_t baseTensors = tensorCount / coreCount; | ||||||||||||||
| 126 | + const int64_t remainderTensors = tensorCount % coreCount; | ||||||||||||||
| 127 | + int32_t tensorCursor = 0; | ||||||||||||||
| 128 | + for (int64_t coreIndex = 0; coreIndex < coreCount; ++coreIndex) { | ||||||||||||||
| 129 | + const int64_t share = baseTensors + static_cast<int64_t>(coreIndex < remainderTensors); | ||||||||||||||
| 130 | + const int32_t startTensor = tensorCursor; | ||||||||||||||
| 131 | + const int32_t endTensor = tensorCursor + static_cast<int32_t>(share) - 1; | ||||||||||||||
| 132 | + tilingData->tensorStartList[coreIndex] = static_cast<uint16_t>(startTensor); | ||||||||||||||
| 133 | + tilingData->tensorEndList[coreIndex] = static_cast<uint16_t>(endTensor); | ||||||||||||||
| 134 | + tilingData->tensorStartOffsetList[coreIndex] = 0; | ||||||||||||||
| 135 | + tilingData->tensorEndOffsetList[coreIndex] = static_cast<int32_t>( | ||||||||||||||
| 136 | + tilingData->tensorDataCountList[endTensor] - 1); | ||||||||||||||
| 137 | + tensorCursor = endTensor + 1; | ||||||||||||||
| 138 | + } | ||||||||||||||
| 139 | + context->SetBlockDim(static_cast<uint32_t>(coreCount)); | ||||||||||||||
| 140 | + } else { | ||||||||||||||
| 141 | + // Element-balanced split, same as the shared flat tiler. | ||||||||||||||
| 142 | + const int64_t elementsPerBlock = MIN_CORE_WORK_BYTES / static_cast<int64_t>(storageBytes); | ||||||||||||||
| 143 | + const int64_t coreCount = std::max<int64_t>( | ||||||||||||||
| 144 | + 1, std::min<int64_t>(limitedCoreNum, CeilDivInt64(totalDataCount, elementsPerBlock))); | ||||||||||||||
| 145 | + const int64_t totalBlocks = CeilDivInt64(totalDataCount, elementsPerBlock); | ||||||||||||||
| 146 | + const int64_t baseBlocks = totalBlocks / coreCount; | ||||||||||||||
| 147 | + const int64_t remainderBlocks = totalBlocks % coreCount; | ||||||||||||||
| 148 | + int64_t flatCursor = 0; | ||||||||||||||
| 149 | + for (int64_t coreIndex = 0; coreIndex < coreCount; ++coreIndex) { | ||||||||||||||
| 150 | + const int64_t targetCount = (baseBlocks + static_cast<int64_t>(coreIndex < remainderBlocks)) * | ||||||||||||||
| 151 | + elementsPerBlock; | ||||||||||||||
| 152 | + const int64_t currentCount = std::min(targetCount, totalDataCount - flatCursor); | ||||||||||||||
| 153 | + uint16_t startTensor = 0; | ||||||||||||||
| 154 | + int64_t startOffset = 0; | ||||||||||||||
| 155 | + uint16_t endTensor = 0; | ||||||||||||||
| 156 | + int64_t endOffset = -1; | ||||||||||||||
| 157 | + LocateFlatOffset(flatCursor, tensorCount, tilingData->tensorDataCountList, startTensor, startOffset); | ||||||||||||||
| 158 | + LocateFlatOffset(flatCursor + currentCount - 1, tensorCount, tilingData->tensorDataCountList, endTensor, | ||||||||||||||
| 159 | + endOffset); | ||||||||||||||
| 160 | + OP_CHECK_IF(startOffset > std::numeric_limits<int32_t>::max() || | ||||||||||||||
| 161 | + endOffset > std::numeric_limits<int32_t>::max() || | ||||||||||||||
| 162 | + currentCount > std::numeric_limits<int32_t>::max(), | ||||||||||||||
| 163 | + OP_LOGE(context, "core %lld flat range exceeds the int32 offset limit", | ||||||||||||||
| 164 | + static_cast<long long>(coreIndex)), | ||||||||||||||
| 165 | + return ge::GRAPH_FAILED); | ||||||||||||||
| 166 | + tilingData->tensorStartList[coreIndex] = startTensor; | ||||||||||||||
| 167 | + tilingData->tensorEndList[coreIndex] = endTensor; | ||||||||||||||
| 168 | + tilingData->tensorStartOffsetList[coreIndex] = static_cast<int32_t>(startOffset); | ||||||||||||||
| 169 | + tilingData->tensorEndOffsetList[coreIndex] = static_cast<int32_t>(endOffset); | ||||||||||||||
| 170 | + flatCursor += currentCount; | ||||||||||||||
| 171 | + } | ||||||||||||||
| 172 | + context->SetBlockDim(static_cast<uint32_t>(coreCount)); | ||||||||||||||
| 173 | + } | ||||||||||||||
| 174 | + | ||||||||||||||
| 175 | + if (context->SetLocalMemorySize(static_cast<uint32_t>(ubSize)) != ge::GRAPH_SUCCESS) { | ||||||||||||||
| 176 | + OP_LOGE(context, "failed to set local memory size"); | ||||||||||||||
| 177 | + return ge::GRAPH_FAILED; | ||||||||||||||
| 178 | + } | ||||||||||||||
| 179 | + size_t* workspaceSizes = context->GetWorkspaceSizes(1); | ||||||||||||||
| 180 | + if (workspaceSizes == nullptr) { | ||||||||||||||
| 181 | + OP_LOGE(context, "workspace size storage is null"); | ||||||||||||||
| 182 | + return ge::GRAPH_FAILED; | ||||||||||||||
| 183 | + } | ||||||||||||||
| 184 | + workspaceSizes[0] = WORKSPACE_BYTES; | ||||||||||||||
| 185 | + context->SetTilingKey(0); | ||||||||||||||
| 167 | return ge::GRAPH_SUCCESS; | 186 | return ge::GRAPH_SUCCESS; | ||||||||||||
| 168 | } | 187 | } | ||||||||||||
| 169 | 188 | ||||||||||||||
| @@ -10,30 +10,22 @@ | |||
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file foreach_log.cpp | 12 | * \file foreach_log.cpp |
| 13 | - * \brief kernel entry for foreach_log (arch35 / Ascend950) | 13 | + * \brief Flat RegBase kernel entry for ForeachLog (arch35 / Ascend950). |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include "foreach_log_simt.h" | 16 | +#include "foreach_log_regbase.h" |
| 17 | 17 | ||
| 18 | -enum class ForeachLogTilingKey : uint32_t { | 18 | +using namespace ForeachLog; |
| 19 | - TILING_KEY_FLOAT = 0, | ||
| 20 | - TILING_KEY_FLOAT16 = 1, | ||
| 21 | - TILING_KEY_BF16 = 2, | ||
| 22 | -}; | ||
| 23 | 19 | ||
| 24 | -template <uint32_t schMode> | 20 | +extern "C" __global__ __aicore__ void foreach_log(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) |
| 25 | -__global__ __aicore__ void foreach_log(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) | ||
| 26 | { | 21 | { |
| 27 | REGISTER_TILING_DEFAULT(ForeachLogTilingData); | 22 | REGISTER_TILING_DEFAULT(ForeachLogTilingData); |
| 28 | - GET_TILING_DATA_WITH_STRUCT(ForeachLogTilingData, tilingData, tiling); | 23 | + GET_TILING_DATA_WITH_STRUCT(ForeachLogTilingData, tilingDataIn, tiling); |
| 24 | + (void)workspace; | ||
| 29 | 25 | ||
| 30 | - const ForeachLogTilingData* tilingGm = &tilingData; | 26 | + TPipe pipe; |
| 31 | - | 27 | + ForeachLogRegbase<DTYPE_X, ForeachLogTilingData> op; |
| 32 | - if constexpr (schMode == static_cast<uint32_t>(ForeachLogTilingKey::TILING_KEY_FLOAT)) { | 28 | + op.Init(&tilingDataIn, &pipe); |
| 33 | - NsForeachLog::Process<float>(x, y, tilingGm); | 29 | + op.Process(x, y); |
| 34 | - } else if constexpr (schMode == static_cast<uint32_t>(ForeachLogTilingKey::TILING_KEY_FLOAT16)) { | 30 | + pipe.Destroy(); |
| 35 | - NsForeachLog::Process<half>(x, y, tilingGm); | ||
| 36 | - } else if constexpr (schMode == static_cast<uint32_t>(ForeachLogTilingKey::TILING_KEY_BF16)) { | ||
| 37 | - NsForeachLog::Process<bfloat16_t>(x, y, tilingGm); | ||
| 38 | - } | ||
| 39 | } | 31 | } |
| @@ -9,25 +9,37 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | - * \file foreach_log_tiling_key.h | 12 | + * \file foreach_log_regbase.h |
| 13 | - * \brief foreach_log tiling key declare | 13 | + * \brief Register-only compute policy for ForeachLog on Ascend950. |
| 14 | + * Computes y = ln(x) for each tensor in the list. FP16 and FP32 use the | ||
| 15 | + * native dav_3510 Ln (mirrors the retired SIMT hlog/logf paths); BF16 is | ||
| 16 | + * promoted to FP32 and rounded back by the shared unary policy. | ||
| 14 | */ | 17 | */ |
| 15 | 18 | ||
| 16 | -#ifndef FOREACH_LOG_TILING_KEY_H | 19 | +#ifndef FOREACH_LOG_REGBASE_H |
| 17 | -#define FOREACH_LOG_TILING_KEY_H | 20 | +#define FOREACH_LOG_REGBASE_H |
| 18 | 21 | ||
| 19 | -#include "ascendc/host_api/tiling/template_argument.h" | 22 | +#include "kernel_operator.h" |
| 23 | +#include "kernel_tiling/kernel_tiling.h" | ||
| 24 | +#include "foreach_log_tiling_data.h" | ||
| 25 | +#include "../../foreach_utils/arch35/foreach_unary_regbase_common.h" | ||
| 20 | 26 | ||
| 21 | -#define FOREACH_LOG_TPL_SCH_MODE_FLOAT 0 | 27 | +namespace ForeachLog { |
| 22 | -#define FOREACH_LOG_TPL_SCH_MODE_FLOAT16 1 | 28 | +using namespace AscendC; |
| 23 | -#define FOREACH_LOG_TPL_SCH_MODE_BF16 2 | ||
| 24 | 29 | ||
| 25 | -ASCENDC_TPL_ARGS_DECL(ForeachLog, | 30 | +struct LogOperation { |
| 26 | - ASCENDC_TPL_UINT_DECL(schMode, 3, ASCENDC_TPL_UI_LIST, FOREACH_LOG_TPL_SCH_MODE_FLOAT, | 31 | + static constexpr bool kPromoteHalf = false; |
| 27 | - FOREACH_LOG_TPL_SCH_MODE_FLOAT16, FOREACH_LOG_TPL_SCH_MODE_BF16)); | ||
| 28 | 32 | ||
| 29 | -ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_UINT_SEL(schMode, ASCENDC_TPL_UI_LIST, FOREACH_LOG_TPL_SCH_MODE_FLOAT, | 33 | + template <typename T> |
| 30 | - FOREACH_LOG_TPL_SCH_MODE_FLOAT16, | 34 | + __simd_callee__ static inline void Apply(Reg::RegTensor<T, Reg::RegTraitNumOne>& dstReg, |
| 31 | - FOREACH_LOG_TPL_SCH_MODE_BF16))); | 35 | + Reg::RegTensor<T, Reg::RegTraitNumOne>& srcReg, Reg::MaskReg& mask) |
| 36 | + { | ||
| 37 | + Reg::Ln<T>(dstReg, srcReg, mask); | ||
| 38 | + } | ||
| 39 | +}; | ||
| 32 | 40 | ||
| 33 | -#endif // FOREACH_LOG_TILING_KEY_H | 41 | +template <typename T, typename Tiling> |
| 42 | +using ForeachLogRegbase = ForeachUnaryRegbase::UnaryApplyKernel<T, Tiling, LogOperation>; | ||
| 43 | +} // namespace ForeachLog | ||
| 44 | + | ||
| 45 | +#endif // FOREACH_LOG_REGBASE_H | ||
| @@ -1,94 +0,0 @@ | |||
| 1 | -/** | ||
| 2 | - * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | - */ | ||
| 10 | - | ||
| 11 | -/*! | ||
| 12 | - * \file foreach_log_simt.h | ||
| 13 | - * \brief SIMT kernel implementation for foreach_log | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#ifndef FOREACH_LOG_SIMT_H | ||
| 17 | -#define FOREACH_LOG_SIMT_H | ||
| 18 | - | ||
| 19 | -#include "kernel_operator.h" | ||
| 20 | -#include "kernel_tiling/kernel_tiling.h" | ||
| 21 | -#include "simt_api/asc_simt.h" | ||
| 22 | -#include "simt_api/math_functions.h" | ||
| 23 | -#include "simt_api/asc_fp16.h" | ||
| 24 | -#include "simt_api/asc_bf16.h" | ||
| 25 | -#include "foreach_log_tiling_data.h" | ||
| 26 | -#include "foreach_log_tiling_key.h" | ||
| 27 | - | ||
| 28 | -namespace NsForeachLog { | ||
| 29 | - | ||
| 30 | -constexpr int TENSOR_PTR_SHIFT = 3; | ||
| 31 | - | ||
| 32 | -using namespace AscendC; | ||
| 33 | - | ||
| 34 | -constexpr uint32_t THREAD_NUM = 1024; | ||
| 35 | - | ||
| 36 | -template <typename T> | ||
| 37 | -__simt_callee__ inline __gm__ T* SimtGetTensorAddr(GM_ADDR tensorListPtr, int64_t idx) | ||
| 38 | -{ | ||
| 39 | - __gm__ uint64_t* dataAddr = reinterpret_cast<__gm__ uint64_t*>(tensorListPtr); | ||
| 40 | - uint64_t tensorPtrOffset = *dataAddr; | ||
| 41 | - __gm__ uint64_t* tensorPtr = dataAddr + (tensorPtrOffset >> TENSOR_PTR_SHIFT); | ||
| 42 | - return reinterpret_cast<__gm__ T*>(*(tensorPtr + idx)); | ||
| 43 | -} | ||
| 44 | - | ||
| 45 | -template <typename T> | ||
| 46 | -__simt_callee__ inline T LogFunc(T x); | ||
| 47 | - | ||
| 48 | -template <> | ||
| 49 | -__simt_callee__ inline float LogFunc<float>(float x) | ||
| 50 | -{ | ||
| 51 | - return logf(x); | ||
| 52 | -} | ||
| 53 | - | ||
| 54 | -template <> | ||
| 55 | -__simt_callee__ inline half LogFunc<half>(half x) | ||
| 56 | -{ | ||
| 57 | - return hlog(x); | ||
| 58 | -} | ||
| 59 | - | ||
| 60 | -template <> | ||
| 61 | -__simt_callee__ inline bfloat16_t LogFunc<bfloat16_t>(bfloat16_t x) | ||
| 62 | -{ | ||
| 63 | - return hlog(x); | ||
| 64 | -} | ||
| 65 | - | ||
| 66 | -template <typename T> | ||
| 67 | -__simt_vf__ __aicore__ LAUNCH_BOUND(THREAD_NUM) inline void OpForeachLogSimt(int32_t tensorId, int64_t count, | ||
| 68 | - GM_ADDR xList, GM_ADDR yList) | ||
| 69 | -{ | ||
| 70 | - __gm__ T* xData = SimtGetTensorAddr<T>(xList, tensorId); | ||
| 71 | - __gm__ T* yData = SimtGetTensorAddr<T>(yList, tensorId); | ||
| 72 | - uint64_t tid = static_cast<uint64_t>(blockIdx.x * blockDim.x + threadIdx.x); | ||
| 73 | - uint64_t stride = static_cast<uint64_t>(blockDim.x * gridDim.x); | ||
| 74 | - for (uint64_t idx = tid; idx < static_cast<uint64_t>(count); idx += stride) { | ||
| 75 | - T val = xData[idx]; | ||
| 76 | - yData[idx] = LogFunc<T>(val); | ||
| 77 | - } | ||
| 78 | -} | ||
| 79 | - | ||
| 80 | -template <typename T> | ||
| 81 | -__aicore__ inline void Process(GM_ADDR x, GM_ADDR y, const ForeachLogTilingData* tilingGm) | ||
| 82 | -{ | ||
| 83 | - for (int32_t tensorId = 0; tensorId < tilingGm->tensorCount; tensorId++) { | ||
| 84 | - int64_t count = tilingGm->tensorElements[tensorId]; | ||
| 85 | - if (count <= 0) { | ||
| 86 | - continue; | ||
| 87 | - } | ||
| 88 | - asc_vf_call<OpForeachLogSimt<T>>(dim3(THREAD_NUM), tensorId, count, x, y); | ||
| 89 | - } | ||
| 90 | -} | ||
| 91 | - | ||
| 92 | -} // namespace NsForeachLog | ||
| 93 | - | ||
| 94 | -#endif // FOREACH_LOG_SIMT_H | ||
| @@ -10,17 +10,31 @@ | |||
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file foreach_log_tiling_data.h | 12 | * \file foreach_log_tiling_data.h |
| 13 | - * \brief tiling data struct for foreach_log | 13 | + * \brief Tiling schema for the ForeachLog arch35 dual-split transport. |
| 14 | + * Field names of the flat-range part match ForeachFlatTilingData so the | ||
| 15 | + * shared TensorListUnaryDispatcher/TypedUnaryMover templates keep working. | ||
| 16 | + * The whole struct must stay below the ~4KB kernel dynamic-shared-memory | ||
| 17 | + * launch limit (ForeachFlatTilingData at 3652B runs; 4.3KB fails to | ||
| 18 | + * launch), hence the 32-bit offsets — the tiling rejects tensors whose | ||
| 19 | + * element count or a core's flat range would not fit those fields. | ||
| 14 | */ | 20 | */ |
| 15 | 21 | ||
| 16 | #ifndef FOREACH_LOG_TILING_DATA_H | 22 | #ifndef FOREACH_LOG_TILING_DATA_H |
| 17 | #define FOREACH_LOG_TILING_DATA_H | 23 | #define FOREACH_LOG_TILING_DATA_H |
| 18 | 24 | ||
| 25 | +#include <cstdint> | ||
| 26 | + | ||
| 27 | +constexpr uint16_t FOREACH_LOG_MAX_TENSOR_COUNT = 256; | ||
| 28 | +constexpr uint16_t FOREACH_LOG_MAX_CORE_COUNT = 80; | ||
| 29 | + | ||
| 19 | struct ForeachLogTilingData { | 30 | struct ForeachLogTilingData { |
| 20 | - int32_t needCoreNum = 0; | ||
| 21 | int32_t tensorCount = 0; | 31 | int32_t tensorCount = 0; |
| 22 | - int64_t totalElements = 0; | 32 | + uint32_t tileElems = 0; |
| 23 | - int64_t tensorElements[256] = {0}; | 33 | + int64_t tensorDataCountList[FOREACH_LOG_MAX_TENSOR_COUNT] = {0}; |
| 34 | + uint16_t tensorStartList[FOREACH_LOG_MAX_CORE_COUNT] = {0}; | ||
| 35 | + uint16_t tensorEndList[FOREACH_LOG_MAX_CORE_COUNT] = {0}; | ||
| 36 | + int32_t tensorStartOffsetList[FOREACH_LOG_MAX_CORE_COUNT] = {0}; | ||
| 37 | + int32_t tensorEndOffsetList[FOREACH_LOG_MAX_CORE_COUNT] = {0}; | ||
| 24 | }; | 38 | }; |
| 25 | 39 | ||
| 26 | #endif // FOREACH_LOG_TILING_DATA_H | 40 | #endif // FOREACH_LOG_TILING_DATA_H |
| @@ -19,20 +19,21 @@ | |||
| 19 | #include "test_cube_util.h" | 19 | #include "test_cube_util.h" |
| 20 | #include "ut_op_util.h" | 20 | #include "ut_op_util.h" |
| 21 | #include "../../../../op_kernel/arch35/foreach_log_tiling_data.h" | 21 | #include "../../../../op_kernel/arch35/foreach_log_tiling_data.h" |
| 22 | -#include "../../../../op_kernel/arch35/foreach_log_tiling_key.h" | ||
| 23 | 22 | ||
| 24 | using namespace ut_util; | 23 | using namespace ut_util; |
| 25 | 24 | ||
| 26 | -class ForeachLogTilingTest : public testing::Test { | 25 | +class ForeachLogTilingArch35 : public testing::Test { |
| 27 | protected: | 26 | protected: |
| 28 | - static void SetUpTestCase() { std::cout << "ForeachLogTilingTest SetUp" << std::endl; } | 27 | + static void SetUpTestCase() { std::cout << "ForeachLogTilingArch35 SetUp" << std::endl; } |
| 29 | - static void TearDownTestCase() { std::cout << "ForeachLogTilingTest TearDown" << std::endl; } | 28 | + static void TearDownTestCase() { std::cout << "ForeachLogTilingArch35 TearDown" << std::endl; } |
| 30 | }; | 29 | }; |
| 31 | 30 | ||
| 32 | static void DoCase(std::initializer_list<int64_t> xShape, ge::DataType dt, uint64_t expectKey) | 31 | static void DoCase(std::initializer_list<int64_t> xShape, ge::DataType dt, uint64_t expectKey) |
| 33 | { | 32 | { |
| 34 | fe::PlatFormInfos pf; | 33 | fe::PlatFormInfos pf; |
| 35 | - std::map<std::string, std::string> soc, ai, intr; | 34 | + std::map<std::string, std::string> soc; |
| 35 | + std::map<std::string, std::string> ai; | ||
| 36 | + std::map<std::string, std::string> intr; | ||
| 36 | GetPlatFormInfos(R"({"hardware_info":{"UB_SIZE":253952,"CORE_NUM":64}})", soc, ai, intr); | 37 | GetPlatFormInfos(R"({"hardware_info":{"UB_SIZE":253952,"CORE_NUM":64}})", soc, ai, intr); |
| 37 | pf.Init(); | 38 | pf.Init(); |
| 38 | 39 | ||
| @@ -59,7 +60,7 @@ static void DoCase(std::initializer_list<int64_t> xShape, ge::DataType dt, uint6 | |||
| 59 | 60 | ||
| 60 | gert::StorageShape xS = {xShape, xShape}; | 61 | gert::StorageShape xS = {xShape, xShape}; |
| 61 | gert::StorageShape yS = {xShape, xShape}; | 62 | gert::StorageShape yS = {xShape, xShape}; |
| 62 | - auto param = gert::TilingData::CreateCap(4096); | 63 | + auto param = gert::TilingData::CreateCap(8192); |
| 63 | auto wsh = gert::ContinuousVector::Create<size_t>(4096); | 64 | auto wsh = gert::ContinuousVector::Create<size_t>(4096); |
| 64 | auto th = gert::TilingContextFaker() | 65 | auto th = gert::TilingContextFaker() |
| 65 | .NodeIoNum(1, 1) | 66 | .NodeIoNum(1, 1) |
| @@ -80,19 +81,70 @@ static void DoCase(std::initializer_list<int64_t> xShape, ge::DataType dt, uint6 | |||
| 80 | ctx->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", intr); | 81 | ctx->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", intr); |
| 81 | EXPECT_EQ(tilingFn(ctx), ge::GRAPH_SUCCESS); | 82 | EXPECT_EQ(tilingFn(ctx), ge::GRAPH_SUCCESS); |
| 82 | EXPECT_EQ(ctx->GetTilingKey(), expectKey); | 83 | EXPECT_EQ(ctx->GetTilingKey(), expectKey); |
| 84 | + auto* tilingData = ctx->GetTilingData<ForeachLogTilingData>(); | ||
| 85 | + EXPECT_GT(tilingData->tileElems, 0U); | ||
| 86 | + EXPECT_EQ(tilingData->tensorCount, 1); | ||
| 87 | + int64_t shapeSize = 1; | ||
| 88 | + for (const int64_t dim : xShape) { | ||
| 89 | + shapeSize *= dim; | ||
| 90 | + } | ||
| 91 | + EXPECT_EQ(tilingData->tensorDataCountList[0], shapeSize); | ||
| 92 | + EXPECT_EQ(tilingData->tensorStartList[0], 0); | ||
| 93 | + EXPECT_EQ(tilingData->tensorStartOffsetList[0], 0); | ||
| 94 | + EXPECT_EQ(tilingData->tensorEndList[0], 0); | ||
| 83 | } | 95 | } |
| 84 | 96 | ||
| 85 | -TEST_F(ForeachLogTilingTest, foreach_log_float32) | 97 | +// Small single tensor: average below TENSOR_BALANCED_AVG, tensor-balanced |
| 98 | +// split (a single core covers the whole list). | ||
| 99 | +TEST_F(ForeachLogTilingArch35, foreach_log_float32) { DoCase({4, 4}, ge::DT_FLOAT, 0); } | ||
| 100 | + | ||
| 101 | +TEST_F(ForeachLogTilingArch35, foreach_log_float16) { DoCase({4, 4}, ge::DT_FLOAT16, 0); } | ||
| 102 | + | ||
| 103 | +TEST_F(ForeachLogTilingArch35, foreach_log_bfloat16) { DoCase({4, 4}, ge::DT_BF16, 0); } | ||
| 104 | + | ||
| 105 | +// One tensor larger than the tensor-balanced average: element-balanced split; | ||
| 106 | +// core 0 covers only the first block range. | ||
| 107 | +TEST_F(ForeachLogTilingArch35, foreach_log_float32_large_flow) | ||
| 86 | { | 108 | { |
| 87 | - DoCase({4, 4}, ge::DT_FLOAT, GET_TPL_TILING_KEY(FOREACH_LOG_TPL_SCH_MODE_FLOAT)); | 109 | + DoCase({524288}, ge::DT_FLOAT, 0); |
| 110 | + // 64 cores, elementsPerBlock=1024: 512 blocks / 64 cores = 8 blocks/core. | ||
| 88 | } | 111 | } |
| 89 | 112 | ||
| 90 | -TEST_F(ForeachLogTilingTest, foreach_log_float16) | 113 | +// A single tensor whose element count exceeds the int32 offset limit must be |
| 114 | +// rejected before any narrowing write into the tiling fields. | ||
| 115 | +TEST_F(ForeachLogTilingArch35, foreach_log_float32_int32_overflow_rejected) | ||
| 91 | { | 116 | { |
| 92 | - DoCase({4, 4}, ge::DT_FLOAT16, GET_TPL_TILING_KEY(FOREACH_LOG_TPL_SCH_MODE_FLOAT16)); | 117 | + fe::PlatFormInfos pf; |
| 93 | -} | 118 | + std::map<std::string, std::string> soc; |
| 119 | + std::map<std::string, std::string> ai; | ||
| 120 | + std::map<std::string, std::string> intr; | ||
| 121 | + GetPlatFormInfos(R"({"hardware_info":{"UB_SIZE":253952,"CORE_NUM":64}})", soc, ai, intr); | ||
| 122 | + pf.Init(); | ||
| 94 | 123 | ||
| 95 | -TEST_F(ForeachLogTilingTest, foreach_log_bfloat16) | 124 | + struct ForeachLogCompileInfo { |
| 96 | -{ | 125 | + } ci; |
| 97 | - DoCase({4, 4}, ge::DT_BF16, GET_TPL_TILING_KEY(FOREACH_LOG_TPL_SCH_MODE_BF16)); | 126 | + auto tilingFn = gert::OpImplRegistry::GetInstance().GetOpImpl("ForeachLog")->tiling; |
| 127 | + | ||
| 128 | + gert::StorageShape xS = {{2147483648LL}, {2147483648LL}}; | ||
| 129 | + gert::StorageShape yS = {{2147483648LL}, {2147483648LL}}; | ||
| 130 | + auto param = gert::TilingData::CreateCap(8192); | ||
| 131 | + auto wsh = gert::ContinuousVector::Create<size_t>(4096); | ||
| 132 | + auto th = gert::TilingContextFaker() | ||
| 133 | + .NodeIoNum(1, 1) | ||
| 134 | + .IrInstanceNum({1}) | ||
| 135 | + .InputShapes({&xS}) | ||
| 136 | + .OutputShapes({&yS}) | ||
| 137 | + .CompileInfo(&ci) | ||
| 138 | + .PlatformInfo(reinterpret_cast<char*>(&pf)) | ||
| 139 | + .NodeInputTd(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND) | ||
| 140 | + .NodeOutputTd(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND) | ||
| 141 | + .TilingData(param.get()) | ||
| 142 | + .Workspace(reinterpret_cast<gert::ContinuousVector*>(wsh.get())) | ||
| 143 | + .Build(); | ||
| 144 | + auto* ctx = th.GetContext<gert::TilingContext>(); | ||
| 145 | + ctx->GetPlatformInfo()->SetPlatformRes("SoCInfo", soc); | ||
| 146 | + ctx->GetPlatformInfo()->SetPlatformRes("AICoreSpec", ai); | ||
| 147 | + ctx->GetPlatformInfo()->SetCoreNumByCoreType("AICore"); | ||
| 148 | + ctx->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", intr); | ||
| 149 | + EXPECT_NE(tilingFn(ctx), ge::GRAPH_SUCCESS); | ||
| 98 | } | 150 | } |


🟡 Medium Priority
变更行:
foreach_log_tiling.cpp:50-51(新增的LocateFlatOffset兜底分支)在循环一次都没命中时执行tensorIndex = static_cast<uint16_t>(tensorCount - 1); tensorOffset = tensorDataCountList[tensorIndex] - 1;当
tensorCount == 0时tensorCount - 1转成uint16_t后为 65535,tensorDataCountList[65535]对只有 256 个元素的数组(foreach_log_tiling_data.h:33)是一次约 522KB 的越界读。可达性由本文件自身给出:
122行的分支条件是if (tensorCount > 0 && ...),即作者显式允许tensorCount == 0落入else(元素均衡分支),于是157-159必然以tensorCount = 0调用LocateFlatOffset(flatCursor + currentCount - 1此时为 -1,同样进兜底分支)。旧实现把空列表视为合法(旧注释:“An empty tensor list is legal (consistent with the aclnn entry)”),本算子契约上存在空列表入口。失败模式:空张量列表做 tiling 时读越界内存,取到垃圾 offset 写入
tensorStartList/tensorEndList;轻则生成的核区间无意义(kernel 侧按该 index 取值会访问不存在的张量),重则越界地址未映射直接段错误,使 tiling 崩溃。修复方向:
LocateFlatOffset开头对tensorCount <= 0直接返回安全值(index 0 / offset -1),或在上游把空列表单独提前处理(不进入任何切分分支)。建议:在兜底分支前加空列表守卫:tensorCount <= 0 时直接置 tensorIndex=0、tensorOffset=-1 后返回,避免用 uint16_t(0-1)=65535 索引 256 元素的数组。