Merged
feat(machine): Cache AOT control-flow binaries in multi-slot LRU pool #5007
LordOfMysteriesAdd on Jul 22
feat(machine): Cache AOT control-flow binaries in multi-slot LRU pool #5007
Merged
Merged fromLordOfMysteries/pyptoperf/aot-cf-code-poolintocann/pyptomaster
Total: 13 files changed+475-160
| @@ -51,6 +51,7 @@ target_compile_options(tile_fwk_server | |||
| 51 | -std=gnu++17 | 51 | -std=gnu++17 |
| 52 | -rdynamic | 52 | -rdynamic |
| 53 | -O3 | 53 | -O3 |
| 54 | + -march=armv8-a | ||
| 54 | -shared | 55 | -shared |
| 55 | -fPIC | 56 | -fPIC |
| 56 | -D__DEVICE__ | 57 | -D__DEVICE__ |
| @@ -0,0 +1,81 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 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 aot_binary.cpp | ||
| 13 | + * \brief AOT control-flow code pool manager (flat array + LRU clock). | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "machine/device/dynamic/aot_binary.h" | ||
| 17 | + | ||
| 18 | +namespace npu::tile_fwk::dynamic { | ||
| 19 | + | ||
| 20 | +AOTCodePoolManager& AOTCodePoolManager::Instance() | ||
| 21 | +{ | ||
| 22 | + static AOTCodePoolManager mgr; | ||
| 23 | + return mgr; | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +int AOTCodePoolManager::FindEntry(uint64_t hashKey, uint8_t lastId) const | ||
| 27 | +{ | ||
| 28 | + if (lastId < AOT_CODE_POOL_NUM && ownerHashKey_[lastId] == hashKey) { | ||
| 29 | + return static_cast<int>(lastId); | ||
| 30 | + } | ||
| 31 | + for (int i = 0; i < AOT_CODE_POOL_NUM; ++i) { | ||
| 32 | + if (ownerHashKey_[i] == hashKey) { | ||
| 33 | + return i; | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + return -1; | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +int AOTCodePoolManager::SelectVictimEntry() const | ||
| 40 | +{ | ||
| 41 | + for (int i = 0; i < AOT_CODE_POOL_NUM; ++i) { | ||
| 42 | + if (ownerHashKey_[i] == 0) { | ||
| 43 | + return i; | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + uint64_t minLru = UINT64_MAX; | ||
| 47 | + int victim = 0; | ||
| 48 | + for (int i = 0; i < AOT_CODE_POOL_NUM; ++i) { | ||
| 49 | + if (lruSeq_[i] < minLru) { | ||
| 50 | + minLru = lruSeq_[i]; | ||
| 51 | + victim = i; | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + return victim; | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +void AOTCodePoolManager::LoadEntry(int entryId, uint64_t hashKey, const void* data, uint64_t size) | ||
| 58 | +{ | ||
| 59 | + const uintptr_t base = EntryCodeBase(entryId); | ||
| 60 | + if (size > 0) { | ||
| 61 | + PerfBegin(PERF_EVT_CONTROL_FLOW_MAPEXE_MEMCPY); | ||
| 62 | + DevMemcpyS(reinterpret_cast<void*>(base), size, data, size); | ||
| 63 | + __builtin___clear_cache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base) + size); | ||
| 64 | + PerfEnd(PERF_EVT_CONTROL_FLOW_MAPEXE_MEMCPY); | ||
| 65 | + } | ||
| 66 | + ownerHashKey_[entryId] = hashKey; | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +int AOTCodePoolManager::EnsureCached(uint64_t hashKey, uint8_t& lastId, const void* data, uint64_t size) | ||
| 70 | +{ | ||
| 71 | + int entryId = FindEntry(hashKey, lastId); | ||
计算hashkey 能保证唯一性吗,加个size 校验 更健壮? ![]() ![]() | |||
| 72 | + if (entryId < 0) { | ||
| 73 | + entryId = SelectVictimEntry(); | ||
| 74 | + LoadEntry(entryId, hashKey, data, size); | ||
| 75 | + } | ||
| 76 | + lruSeq_[entryId] = ++lruClock_; | ||
| 77 | + lastId = static_cast<uint8_t>(entryId); | ||
| 78 | + return entryId; | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +} // namespace npu::tile_fwk::dynamic | ||
| @@ -10,16 +10,27 @@ | |||
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file aot_binary.h | 12 | * \file aot_binary.h |
| 13 | - * \brief | 13 | + * \brief Process-global AOT control-flow code pool. |
| 14 | + * | ||
| 15 | + * Policy: each pool entry records the DevProg hash that currently owns it. | ||
| 16 | + * EnsureCached(hash) — if that hash is still in the pool, skip memcpy; otherwise | ||
| 17 | + * overwrite a free / LRU entry. No refCount / pin / Detach. | ||
| 18 | + * | ||
| 19 | + * Pool size is small (AOT_CODE_POOL_NUM=16), so metadata uses a flat array + | ||
| 20 | + * monotonic LRU clock rather than open-hash / linked lists. | ||
| 14 | */ | 21 | */ |
| 15 | 22 | ||
| 16 | #pragma once | 23 | #pragma once |
| 17 | 24 | ||
| 18 | #include <cstdint> | 25 | #include <cstdint> |
| 19 | #include <cstddef> | 26 | #include <cstddef> |
| 27 | +#include <climits> | ||
| 28 | +#include <vector> | ||
| 29 | +#include <tuple> | ||
| 20 | #include "securec.h" | 30 | #include "securec.h" |
| 21 | #include "machine/device/dynamic/device_utils.h" | 31 | #include "machine/device/dynamic/device_utils.h" |
| 22 | #include "machine/device/dynamic/device_perf.h" | 32 | #include "machine/device/dynamic/device_perf.h" |
| 33 | +#include "machine/utils/dynamic/dev_encode_program.h" | ||
| 23 | #include "tilefwk/aicpu_runtime.h" | 34 | #include "tilefwk/aicpu_runtime.h" |
| 24 | #include "tilefwk/aikernel_data.h" | 35 | #include "tilefwk/aikernel_data.h" |
| 25 | 36 | ||
| @@ -28,61 +39,77 @@ | |||
| 28 | #define STR(n) STR_(n) | 39 | #define STR(n) STR_(n) |
| 29 | #endif | 40 | #endif |
| 30 | 41 | ||
| 42 | +#define AOT_CODE_POOL_NUM 16 | ||
| 31 | #define AOT_CODE_POOL_CODE_SIZE (4096 * 0x1000) | 43 | #define AOT_CODE_POOL_CODE_SIZE (4096 * 0x1000) |
| 32 | -extern uint8_t aotCodePoolCode[]; | 44 | +#define AOT_CODE_POOL_TOTAL_SIZE (AOT_CODE_POOL_NUM * AOT_CODE_POOL_CODE_SIZE) |
| 45 | +#define AOT_POOL_ENTRY_INVALID 0xFFU | ||
| 46 | + | ||
| 47 | +// Weak comdat BSS (shared across TUs). Symbol must be at global scope so the | ||
| 48 | +// C++ reference name matches the unmangled asm label (same pattern as the | ||
| 49 | +// single-slot aotCodePoolCode pool). Do not put this extern inside a namespace. | ||
| 50 | +#define AOT_DEFINE_BSS_POOL(name, totalSize) \ | ||
| 51 | + extern uint8_t name[totalSize]; \ | ||
| 52 | + asm("\n\t.pushsection .bss." STR(name) ",\"axwG\",@nobits," STR( \ | ||
| 53 | + name) ",comdat" \ | ||
| 54 | + "\n\t.p2align 12" \ | ||
| 55 | + "\n\t.weak " STR(name) "\n\t.type " STR(name) ", @gnu_unique_object" \ | ||
| 56 | + "\n\t.size " STR(name) ", " STR(totalSize) "\n" STR( \ | ||
| 57 | + name) ":" \ | ||
| 58 | + "\n\t.zero " STR(totalSize) "\n\t.popsection") | ||
| 59 | + | ||
| 60 | +AOT_DEFINE_BSS_POOL(aotCodePoolCodes, AOT_CODE_POOL_TOTAL_SIZE); | ||
| 33 | 61 | ||
| 34 | namespace npu::tile_fwk::dynamic { | 62 | namespace npu::tile_fwk::dynamic { |
| 35 | -asm("\n\t.pushsection .bss." STR(aotCodePoolCode) ",\"axwG\",@nobits," STR( | ||
| 36 | - aotCodePoolCode) ",comdat" | ||
| 37 | - "\n\t.p2align 12" | ||
| 38 | - "\n\t.weak " STR(aotCodePoolCode) "\n\t.type " STR( | ||
| 39 | - aotCodePoolCode) ", @gnu_unique_object" | ||
| 40 | - "\n\t.size " STR(aotCodePoolCode) ", " STR(AOT_CODE_POOL_CODE_SIZE) "\n" STR( | ||
| 41 | - aotCodePoolCode) ":" | ||
| 42 | - "\n\t.zero " STR( | ||
| 43 | - AOT_CODE_POOL_CODE_SIZE) "\n\t.popsection"); | ||
| 44 | 63 | ||
| 45 | -const size_t TUBLE_INDEX_2 = 2; | 64 | +// hashKey is written at encode time in EncodeDevAscendProgram::Init (dev_encode.cpp). |
| 46 | -const size_t TUBLE_INDEX_3 = 3; | 65 | +// 0 is reserved as the empty-entry sentinel for the AOT code pool. |
| 66 | +inline uint64_t GetAOTCacheKey(const DevAscendProgram* prog) { return prog->hashKey; } | ||
| 47 | 67 | ||
| 48 | -struct AOTCodePool { | 68 | +// Shared host/device pool (16 entries x 16MiB BSS). |
| 49 | - uintptr_t base{0}; | 69 | +struct AOTCodePoolManager { |
| 50 | - uintptr_t offset{0}; | 70 | + uint64_t ownerHashKey_[AOT_CODE_POOL_NUM]{}; |
| 71 | + uint64_t lruSeq_[AOT_CODE_POOL_NUM]{}; | ||
| 72 | + uint64_t lruClock_{0}; | ||
| 51 | 73 | ||
| 52 | - void MapExec() {} | 74 | + static AOTCodePoolManager& Instance(); |
| 53 | 75 | ||
| 54 | - static AOTCodePool& GetCodePool() | 76 | + static uintptr_t EntryCodeBase(int entryId) |
| 55 | { | 77 | { |
| 56 | - static AOTCodePool pool = {0}; | 78 | + return reinterpret_cast<uintptr_t>(aotCodePoolCodes) + |
| 57 | - if (pool.base == 0) { | 79 | + static_cast<uintptr_t>(entryId) * static_cast<uintptr_t>(AOT_CODE_POOL_CODE_SIZE); |
| 58 | - pool.base = (uintptr_t)aotCodePoolCode; | 80 | + } |
| 59 | - } | 81 | + |
| 60 | - return pool; | 82 | + // lastId: DevProg hint of the previous pool entry that held this hash. |
| 61 | - }; | 83 | + int EnsureCached(uint64_t hashKey, uint8_t& lastId, const void* data, uint64_t size); |
| 84 | + | ||
| 85 | +private: | ||
| 86 | + int FindEntry(uint64_t hashKey, uint8_t lastId) const; | ||
| 87 | + int SelectVictimEntry() const; | ||
| 88 | + void LoadEntry(int entryId, uint64_t hashKey, const void* data, uint64_t size); | ||
| 62 | }; | 89 | }; |
| 63 | 90 | ||
| 64 | struct AOTBinary { | 91 | struct AOTBinary { |
| 65 | AOTBinary() {} | 92 | AOTBinary() {} |
| 66 | 93 | ||
| 67 | - void InitCodeSize(const void* data, uint64_t size) | 94 | + void InitCodeSizeCached(uint64_t hashKey, uint8_t& lastId, const void* data, uint64_t size) |
| 68 | { | 95 | { |
| 96 | + // Empty CF binary must not occupy / evict a pool entry (LoadEntry still writes ownerHashKey_). | ||
| 97 | + if (size == 0) { | ||
| 98 | + code_ = nullptr; | ||
| 99 | + size_ = 0; | ||
| 100 | + return; | ||
| 101 | + } | ||
| 69 | if (size > AOT_CODE_POOL_CODE_SIZE) { | 102 | if (size > AOT_CODE_POOL_CODE_SIZE) { |
| 70 | DEV_ERROR(DevCommonErr::MEMCPY_FAILED, "AOTBinary code size %zu is too large, max %d", size, | 103 | DEV_ERROR(DevCommonErr::MEMCPY_FAILED, "AOTBinary code size %zu is too large, max %d", size, |
| 71 | AOT_CODE_POOL_CODE_SIZE); | 104 | AOT_CODE_POOL_CODE_SIZE); |
| 72 | DEV_ASSERT(DevCommonErr::MEMCPY_FAILED, false); | 105 | DEV_ASSERT(DevCommonErr::MEMCPY_FAILED, false); |
| 73 | return; | 106 | return; |
| 74 | } | 107 | } |
| 75 | - auto& pool = AOTCodePool::GetCodePool(); | 108 | + const int entryId = AOTCodePoolManager::Instance().EnsureCached(hashKey, lastId, data, size); |
| 76 | - code_ = reinterpret_cast<unsigned char*>(pool.base); | 109 | + code_ = reinterpret_cast<unsigned char*>(AOTCodePoolManager::EntryCodeBase(entryId)); |
| 77 | size_ = size; | 110 | size_ = size; |
| 78 | - if (size == 0) { | ||
| 79 | - return; | ||
| 80 | - } | ||
| 81 | - PerfBegin(PERF_EVT_CONTROL_FLOW_MAPEXE_MEMCPY); | ||
| 82 | - DevMemcpyS(reinterpret_cast<void*>(pool.base), size, data, size); | ||
| 83 | - __builtin___clear_cache(reinterpret_cast<char*>(pool.base), reinterpret_cast<char*>(pool.base) + size); | ||
| 84 | - PerfEnd(PERF_EVT_CONTROL_FLOW_MAPEXE_MEMCPY); | ||
| 85 | } | 111 | } |
| 112 | + | ||
| 86 | void InitCode(const void* data) { code_ = reinterpret_cast<const unsigned char*>(data); } | 113 | void InitCode(const void* data) { code_ = reinterpret_cast<const unsigned char*>(data); } |
| 87 | 114 | ||
| 88 | const unsigned char* code_{nullptr}; | 115 | const unsigned char* code_{nullptr}; |
| @@ -98,20 +125,45 @@ struct AOTBinaryControlFlow : AOTBinary { | |||
| 98 | 125 | ||
| 99 | AOTBinaryControlFlow() = default; | 126 | AOTBinaryControlFlow() = default; |
| 100 | 127 | ||
| 101 | - AOTBinaryControlFlow(const std::tuple<const void*, uint64_t>& code, controlFlowEntry entry = nullptr) | 128 | + AOTBinaryControlFlow(const AOTBinaryControlFlow&) = delete; |
| 102 | - : AOTBinaryControlFlow(std::get<0>(code), std::get<1>(code), entry) | 129 | + AOTBinaryControlFlow& operator=(const AOTBinaryControlFlow&) = delete; |
| 130 | + | ||
| 131 | + AOTBinaryControlFlow(AOTBinaryControlFlow&& other) noexcept | ||
| 132 | + { | ||
| 133 | + code_ = other.code_; | ||
| 134 | + size_ = other.size_; | ||
| 135 | + other.code_ = nullptr; | ||
| 136 | + other.size_ = 0; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + AOTBinaryControlFlow& operator=(AOTBinaryControlFlow&& other) noexcept | ||
| 140 | + { | ||
| 141 | + if (this != &other) { | ||
| 142 | + code_ = other.code_; | ||
| 143 | + size_ = other.size_; | ||
| 144 | + other.code_ = nullptr; | ||
| 145 | + other.size_ = 0; | ||
| 146 | + } | ||
| 147 | + return *this; | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + AOTBinaryControlFlow(const std::tuple<const void*, uint64_t>& code, DevAscendProgram* prog, | ||
| 151 | + AOTBinaryControlFlow::controlFlowEntry entry = nullptr) | ||
| 152 | + : AOTBinaryControlFlow(std::get<0>(code), std::get<1>(code), prog, entry) | ||
| 103 | {} | 153 | {} |
| 104 | 154 | ||
| 105 | - AOTBinaryControlFlow(const std::vector<uint8_t>& code, controlFlowEntry entry = nullptr) | 155 | + AOTBinaryControlFlow(const std::vector<uint8_t>& code, DevAscendProgram* prog, |
| 106 | - : AOTBinaryControlFlow(code.data(), code.size(), entry) | 156 | + AOTBinaryControlFlow::controlFlowEntry entry = nullptr) |
| 157 | + : AOTBinaryControlFlow(code.data(), code.size(), prog, entry) | ||
| 107 | {} | 158 | {} |
| 108 | 159 | ||
| 109 | - AOTBinaryControlFlow(const void* code, uint64_t codeSize, controlFlowEntry entry = nullptr) | 160 | + AOTBinaryControlFlow(const void* code, uint64_t codeSize, DevAscendProgram* prog, |
| 161 | + AOTBinaryControlFlow::controlFlowEntry entry = nullptr) | ||
| 110 | { | 162 | { |
| 111 | if (entry != nullptr) { | 163 | if (entry != nullptr) { |
| 112 | InitCode(reinterpret_cast<void*>(entry)); | 164 | InitCode(reinterpret_cast<void*>(entry)); |
| 113 | } else { | 165 | } else { |
| 114 | - InitCodeSize(code, codeSize); | 166 | + InitCodeSizeCached(GetAOTCacheKey(prog), prog->aotPoolLastId, code, codeSize); |
| 115 | } | 167 | } |
| 116 | } | 168 | } |
| 117 | 169 | ||
| @@ -123,38 +175,37 @@ struct AOTBinaryControlFlow : AOTBinary { | |||
| 123 | } | 175 | } |
| 124 | }; | 176 | }; |
| 125 | 177 | ||
| 126 | -struct AOTBinaryExpressionTable : AOTBinary { | ||
| 127 | - using exprEntry = uint64_t (*)(struct DeviceExecuteContext* ctx, int64_t* symbolTable); | ||
| 128 | - AOTBinaryExpressionTable() {} | ||
| 129 | - AOTBinaryExpressionTable(const std::tuple<const void*, uint64_t, const uint64_t*, uint64_t>& table) | ||
| 130 | - : offsetList(std::get<TUBLE_INDEX_2>(table)), offsetSize(std::get<TUBLE_INDEX_3>(table)) | ||
| 131 | - { | ||
| 132 | - InitCodeSize(std::get<0>(table), std::get<1>(table)); | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - uint64_t CallExpr(struct DeviceExecuteContext* ctx, int64_t* symbolTable, uint64_t index) | ||
| 136 | - { | ||
| 137 | - return (reinterpret_cast<exprEntry>(const_cast<unsigned char*>(code_ + offsetList[index])))(ctx, symbolTable); | ||
| 138 | - } | ||
| 139 | - | ||
| 140 | - const uint64_t* offsetList{nullptr}; | ||
| 141 | - uint64_t offsetSize{0}; | ||
| 142 | -}; | ||
| 143 | - | ||
| 144 | struct DeviceExecuteProgram { | 178 | struct DeviceExecuteProgram { |
| 145 | DevAscendProgram* prog{nullptr}; | 179 | DevAscendProgram* prog{nullptr}; |
| 146 | 180 | ||
| 147 | AOTBinaryControlFlow controlFlowBinary; | 181 | AOTBinaryControlFlow controlFlowBinary; |
| 148 | - AOTBinaryExpressionTable exprBinary; | ||
| 149 | 182 | ||
| 150 | DeviceExecuteProgram() {} | 183 | DeviceExecuteProgram() {} |
| 151 | DeviceExecuteProgram(DevAscendProgram* prog_, AOTBinaryControlFlow::controlFlowEntry entry = nullptr) | 184 | DeviceExecuteProgram(DevAscendProgram* prog_, AOTBinaryControlFlow::controlFlowEntry entry = nullptr) |
| 152 | : prog(prog_), | 185 | : prog(prog_), |
| 153 | controlFlowBinary(IsDeviceMode() ? prog_->GetDevControlFlowBinary() : prog_->GetHostControlFlowBinary(), | 186 | controlFlowBinary(IsDeviceMode() ? prog_->GetDevControlFlowBinary() : prog_->GetHostControlFlowBinary(), |
| 154 | - entry), | 187 | + prog_, entry) |
| 155 | - exprBinary(prog_->GetExpressionTableBinary()) | ||
| 156 | {} | 188 | {} |
| 157 | 189 | ||
| 190 | + DeviceExecuteProgram(const DeviceExecuteProgram&) = delete; | ||
| 191 | + DeviceExecuteProgram& operator=(const DeviceExecuteProgram&) = delete; | ||
| 192 | + | ||
| 193 | + DeviceExecuteProgram(DeviceExecuteProgram&& other) noexcept | ||
| 194 | + : prog(other.prog), controlFlowBinary(std::move(other.controlFlowBinary)) | ||
| 195 | + { | ||
| 196 | + other.prog = nullptr; | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + DeviceExecuteProgram& operator=(DeviceExecuteProgram&& other) noexcept | ||
| 200 | + { | ||
| 201 | + if (this != &other) { | ||
| 202 | + prog = other.prog; | ||
| 203 | + controlFlowBinary = std::move(other.controlFlowBinary); | ||
| 204 | + other.prog = nullptr; | ||
| 205 | + } | ||
| 206 | + return *this; | ||
| 207 | + } | ||
| 208 | + | ||
| 158 | const void* GetControlFlowEntry() { return controlFlowBinary.code_; } | 209 | const void* GetControlFlowEntry() { return controlFlowBinary.code_; } |
| 159 | }; | 210 | }; |
| 160 | } // namespace npu::tile_fwk::dynamic | 211 | } // namespace npu::tile_fwk::dynamic |
| @@ -86,10 +86,12 @@ DeviceExecuteContext::DeviceExecuteContext(DevStartArgs* startArgs) | |||
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | PerfBegin(PERF_EVT_CONTROL_FLOW_MAPEXE); | 88 | PerfBegin(PERF_EVT_CONTROL_FLOW_MAPEXE); |
| 89 | - if (startArgs->controlFlowEntry) { | 89 | + // Prefer injected entry (device custom ctrl / host UT stubs). |
| 90 | + // Production already resolves AOT CF in InitDevProgram and sets controlFlowEntry before constructing. | ||
| 91 | + // Do not fall back to AOT here when entry is unset: DevStartArgs.devProg may be null/uninit in host UT. | ||
| 92 | + if (startArgs->controlFlowEntry != nullptr && devProg != nullptr) { | ||
| 90 | execProg = DeviceExecuteProgram(devProg, reinterpret_cast<AOTBinaryControlFlow::controlFlowEntry>( | 93 | execProg = DeviceExecuteProgram(devProg, reinterpret_cast<AOTBinaryControlFlow::controlFlowEntry>( |
| 91 | const_cast<void*>(startArgs->controlFlowEntry))); | 94 | const_cast<void*>(startArgs->controlFlowEntry))); |
| 92 | - AOTCodePool::GetCodePool().MapExec(); | ||
| 93 | } | 95 | } |
| 94 | PerfEnd(PERF_EVT_CONTROL_FLOW_MAPEXE); | 96 | PerfEnd(PERF_EVT_CONTROL_FLOW_MAPEXE); |
| 95 | PerfEnd(PERF_EVT_INIT); | 97 | PerfEnd(PERF_EVT_INIT); |
| @@ -114,7 +116,6 @@ void DeviceExecuteContext::GELaunchRunCached(DevStartArgs* startArgs, PushTaskEn | |||
| 114 | this->args = startArgs; | 116 | this->args = startArgs; |
| 115 | this->devProg = startArgs->devProg; | 117 | this->devProg = startArgs->devProg; |
| 116 | PerfEnd(PERF_EVT_CONTROL_FLOW_INIT); | 118 | PerfEnd(PERF_EVT_CONTROL_FLOW_INIT); |
| 117 | - PerfMtTrace(PERF_TRACE_INIT, CTRL_CPU_THREAD_IDX); | ||
| 118 | PerfBegin(PERF_EVT_CONTROL_FLOW); | 119 | PerfBegin(PERF_EVT_CONTROL_FLOW); |
| 119 | for (size_t index = 0; index < devProg->ctrlFlowCacheAnchor->deviceTaskCount; index++) { | 120 | for (size_t index = 0; index < devProg->ctrlFlowCacheAnchor->deviceTaskCount; index++) { |
| 120 | DynDeviceTask* dynTask = reinterpret_cast<DynDeviceTask*>( | 121 | DynDeviceTask* dynTask = reinterpret_cast<DynDeviceTask*>( |
| @@ -232,11 +232,16 @@ public: | |||
| 232 | memBarrier(); | 232 | memBarrier(); |
| 233 | 233 | ||
| 234 | #ifdef __USE_CUSTOM_CTRLFLOW__ | 234 | #ifdef __USE_CUSTOM_CTRLFLOW__ |
| 235 | - DEV_INFO("Use built in ctrl flow func."); | 235 | + if (devProg->controlFlowBinaryAddr == nullptr) { |
| 236 | - devProg->controlFlowBinaryAddr = GetCtrlFlowFunc(); | 236 | + DEV_INFO("Use built in ctrl flow func."); |
| 237 | + devProg->controlFlowBinaryAddr = GetCtrlFlowFunc(); | ||
| 238 | + } | ||
| 237 | #else | 239 | #else |
| 238 | - auto execProg = DeviceExecuteProgram(devProg, nullptr); | 240 | + // Resolve CF by DevProg hash: already in pool and not overwritten -> skip memcpy. |
| 239 | - devProg->controlFlowBinaryAddr = execProg.GetControlFlowEntry(); | 241 | + { |
| 242 | + auto execProg = DeviceExecuteProgram(devProg, nullptr); | ||
| 243 | + devProg->controlFlowBinaryAddr = execProg.GetControlFlowEntry(); | ||
| 244 | + } | ||
| 240 | #endif | 245 | #endif |
| 241 | return firstInit; | 246 | return firstInit; |
| 242 | } | 247 | } |
| @@ -55,7 +55,7 @@ constexpr bool SEND_TASK_IMMEDIATELY_SWITCH = true; | |||
| 55 | #define DEBUG_MEM_DUMP_LIGHT 1 | 55 | #define DEBUG_MEM_DUMP_LIGHT 1 |
| 56 | #define DEBUG_MEM_DUMP_FULL 2 | 56 | #define DEBUG_MEM_DUMP_FULL 2 |
| 57 | 57 | ||
| 58 | -#define DEBUG_MEM_DUMP_LEVEL DEBUG_MEM_DUMP_LIGHT | 58 | +#define DEBUG_MEM_DUMP_LEVEL DEBUG_MEM_DUMP_DISABLE |
| 59 | 59 | ||
| 60 | #define SLAB_OBJ_MAGIC_VERIFY 0 | 60 | #define SLAB_OBJ_MAGIC_VERIFY 0 |
| 61 | 61 | ||
| @@ -2904,6 +2904,9 @@ struct EncodeDevAscendProgramInfo { | |||
| 2904 | devProg->devArgs.enableVFFusion = GetEnableVFFusion(); | 2904 | devProg->devArgs.enableVFFusion = GetEnableVFFusion(); |
| 2905 | devProg->devArgs.hasAicpuTask = HasAicpuTask(); | 2905 | devProg->devArgs.hasAicpuTask = HasAicpuTask(); |
| 2906 | devProg->devArgs.all1c2vMixTask = CheckAll1c2vMixTask(dyndevAttr->cceCodeInfo); | 2906 | devProg->devArgs.all1c2vMixTask = CheckAll1c2vMixTask(dyndevAttr->cceCodeInfo); |
| 2907 | + devProg->hashKey = func->ComputeHash().GetHash(); | ||
| 2908 | + ASSERT(DevCommonErr::PARAM_CHECK_FAILED, devProg->hashKey != 0) | ||
| 2909 | + << "Function hash must be non-zero: AOT code pool uses 0 as empty-entry sentinel"; | ||
| 2907 | devProg->slotSize = dyndevAttr->inoutLink.totalSlot; | 2910 | devProg->slotSize = dyndevAttr->inoutLink.totalSlot; |
| 2908 | devProg->assembleSlotSize = dyndevAttr->inoutLink.assembleSlotIndexList.size(); | 2911 | devProg->assembleSlotSize = dyndevAttr->inoutLink.assembleSlotIndexList.size(); |
| 2909 | devProg->InitSymbolTable(initOffset, &dyndevAttr->symbolTable, fillContent); | 2912 | devProg->InitSymbolTable(initOffset, &dyndevAttr->symbolTable, fillContent); |
| @@ -14,12 +14,28 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "machine/utils/dynamic/dev_encode_program.h" | 16 | #include "machine/utils/dynamic/dev_encode_program.h" |
| 17 | +#include "machine/device/dynamic/aot_binary.h" | ||
| 17 | 18 | ||
| 18 | namespace npu::tile_fwk::dynamic { | 19 | namespace npu::tile_fwk::dynamic { |
| 19 | namespace { | 20 | namespace { |
| 20 | const size_t WIDTH = 16; | 21 | const size_t WIDTH = 16; |
| 21 | const int ADDRESS_MIN_WIDTH = 6; | 22 | const int ADDRESS_MIN_WIDTH = 6; |
| 22 | } // namespace | 23 | } // namespace |
| 24 | + | ||
| 25 | +void DevAscendProgram::ResetFromLaunch() | ||
| 26 | +{ | ||
| 27 | + DevArgsPreservedParams preservedParams = BackupDevArgsParams(devArgs); | ||
| 28 | + memset_s(&devArgs, sizeof(devArgs), 0, sizeof(devArgs)); | ||
| 29 | + RestoreDevArgsParams(devArgs, preservedParams); | ||
| 30 | + | ||
| 31 | + controlFlowBinaryAddr = nullptr; | ||
| 32 | + aotPoolLastId = AOT_POOL_ENTRY_INVALID; | ||
| 33 | + runtimeDataRingBufferInited = false; | ||
| 34 | + workspaceSize = 0; | ||
| 35 | + ctrlFlowCacheAnchor = nullptr; | ||
| 36 | + RelocProgram(reinterpret_cast<int64_t>(this), 0); | ||
| 37 | +} | ||
| 38 | + | ||
| 23 | void DevAscendProgram::DumpCce(std::ostringstream& oss, int indent) const | 39 | void DevAscendProgram::DumpCce(std::ostringstream& oss, int indent) const |
| 24 | { | 40 | { |
| 25 | std::string INDENTINNER(indent + IDENT_SIZE, ' '); | 41 | std::string INDENTINNER(indent + IDENT_SIZE, ' '); |
| @@ -205,4 +221,97 @@ void DevAscendProgram::DumpFile(const std::string& filePath) const | |||
| 205 | ofs << Dump(); | 221 | ofs << Dump(); |
| 206 | ofs.close(); | 222 | ofs.close(); |
| 207 | } | 223 | } |
| 224 | + | ||
| 225 | +#if ENABLE_COMPILE_VERBOSE_LOG | ||
| 226 | +namespace { | ||
| 227 | +using RelocRange = DevAscendProgram::DevRelocRange; | ||
| 228 | + | ||
| 229 | +std::vector<RelocRange> CollectProgramRelocRanges(const DevAscendProgram& prog) | ||
| 230 | +{ | ||
| 231 | + return { | ||
| 232 | + prog.symbolTable, // 0 | ||
| 233 | + prog.symbolTableNameList, | ||
| 234 | + prog.expressionTableOffsetList, | ||
| 235 | + prog.hostControlFlowBinary, | ||
| 236 | + prog.devControlFlowBinary, | ||
| 237 | + prog.devEncodeList, // 5 | ||
| 238 | + prog.devEncodeDataList, | ||
| 239 | + prog.cceCodeList, | ||
| 240 | + prog.aicpuLeafCodeList, | ||
| 241 | + prog.aicpuLeafCodeDataList, | ||
| 242 | + prog.startArgsInputTensorSlotIndexList, // 10 | ||
| 243 | + prog.startArgsOutputTensorSlotIndexList, | ||
| 244 | + prog.assembleSlotIndexList, | ||
| 245 | + prog.outputInplaceSlotList, | ||
| 246 | + prog.partialUpdateList, | ||
| 247 | + prog.cellMatchRuntimePartialUpdateTableList, // 15 | ||
| 248 | + prog.prefetchInfoList, | ||
| 249 | + prog.disableL2List, | ||
| 250 | + prog.controlFlowCache.inputTensorDataList, | ||
| 251 | + prog.controlFlowCache.outputTensorDataList, | ||
| 252 | + prog.controlFlowCache.runtimeBackup.workspace.tensorAllocators[0].slottedOutcastsBlockList, // 20 | ||
| 253 | + prog.controlFlowCache.runtimeBackup.slotContext.slotList, | ||
| 254 | + prog.controlFlowCache.runtimeBackup.workspace.runtimeOutcastTensorPool, | ||
| 255 | + prog.controlFlowCache.deviceTaskCacheList, | ||
| 256 | + prog.controlFlowCache.cacheData, | ||
| 257 | + }; | ||
| 258 | +} | ||
| 259 | + | ||
| 260 | +void VerifySingleRangeValid(size_t idx, const RelocRange& range) | ||
| 261 | +{ | ||
| 262 | + if (range.begin > range.end) { | ||
| 263 | + DEV_ERROR(ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 264 | + "#ctrl.program.verify: Invalid range: range[%d].begin (0x%p) > range[%d].end (0x%p)", (int)idx, | ||
| 265 | + (void*)range.begin, (int)idx, (void*)range.end); | ||
| 266 | + } | ||
| 267 | + DEV_ASSERT_MSG(ProgEncodeErr::RANGE_VERIFY_FAILED, range.begin <= range.end, "range:%d", (int)idx); | ||
| 268 | +} | ||
| 269 | + | ||
| 270 | +void VerifyAdjacentRanges(size_t prevIdx, const RelocRange& prev, const RelocRange& curr) | ||
| 271 | +{ | ||
| 272 | + if (prev.end > curr.begin) { | ||
| 273 | + DEV_ERROR(ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 274 | + "#ctrl.program.verify: Ranges overlap: range[%d].end (0x%p) > range[%d].begin (0x%p)", (int)prevIdx, | ||
| 275 | + (void*)prev.end, (int)(prevIdx + 1), (void*)curr.begin); | ||
| 276 | + } | ||
| 277 | + DEV_ASSERT_MSG(ProgEncodeErr::RANGE_VERIFY_FAILED, prev.end <= curr.begin, "range:%d->%d", (int)prevIdx, | ||
| 278 | + (int)(prevIdx + 1)); | ||
| 279 | + VerifySingleRangeValid(prevIdx + 1, curr); | ||
| 280 | +} | ||
| 281 | + | ||
| 282 | +void VerifyProgramDataLayout(const std::vector<RelocRange>& rangeList, uint8_t* data, uint64_t dataSize) | ||
| 283 | +{ | ||
| 284 | + if ((uintptr_t)data != rangeList[0].begin) { | ||
| 285 | + DEV_ERROR(ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 286 | + "#ctrl.program.verify: Assertion failed: data (0x%p) != rangeList[0].begin (0x%p)", data, | ||
| 287 | + (void*)rangeList[0].begin); | ||
| 288 | + } | ||
| 289 | + DEV_ASSERT(ProgEncodeErr::RANGE_VERIFY_FAILED, (uintptr_t)data == rangeList[0].begin); | ||
| 290 | + VerifySingleRangeValid(0, rangeList[0]); | ||
| 291 | + for (size_t k = 1; k < rangeList.size(); k++) { | ||
| 292 | + VerifyAdjacentRanges(k - 1, rangeList[k - 1], rangeList[k]); | ||
| 293 | + } | ||
| 294 | + uintptr_t lastEnd = rangeList.back().end; | ||
| 295 | + uintptr_t dataEnd = (uintptr_t)(&data[dataSize]); | ||
| 296 | + if (lastEnd != dataEnd) { | ||
| 297 | + DEV_ERROR( | ||
| 298 | + ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 299 | + "#ctrl.program.verify: Last range end does not match data end: rangeList.back().end (0x%p) != dataEnd " | ||
| 300 | + "(0x%p)", | ||
| 301 | + (void*)lastEnd, (void*)dataEnd); | ||
| 302 | + } | ||
| 303 | + DEV_ASSERT(ProgEncodeErr::RANGE_VERIFY_FAILED, lastEnd == dataEnd); | ||
| 304 | +} | ||
| 305 | +} // namespace | ||
| 306 | +#endif | ||
| 307 | + | ||
| 308 | +void DevAscendProgram::RuntimeVerify(uintptr_t workspaceBegin, uintptr_t workspaceEnd) const | ||
| 309 | +{ | ||
| 310 | + (void)workspaceBegin; | ||
| 311 | + (void)workspaceEnd; | ||
| 312 | + // Verbose-only: verify encoded DevRelocVector ranges are contiguous and non-overlapping. | ||
| 313 | +#if ENABLE_COMPILE_VERBOSE_LOG | ||
| 314 | + VerifyProgramDataLayout(CollectProgramRelocRanges(*this), data, dataSize); | ||
| 315 | +#endif | ||
| 316 | +} | ||
| 208 | } // namespace npu::tile_fwk::dynamic | 317 | } // namespace npu::tile_fwk::dynamic |
| @@ -41,6 +41,7 @@ struct DevAscendProgram { | |||
| 41 | uint64_t workspaceSize; | 41 | uint64_t workspaceSize; |
| 42 | uint64_t l2CacheOffset; | 42 | uint64_t l2CacheOffset; |
| 43 | uint64_t configKey; | 43 | uint64_t configKey; |
| 44 | + // Set at encode (EncodeDevAscendProgram::Init). Must be non-zero: AOT pool uses 0 as empty-entry sentinel. | ||
| 44 | uint64_t hashKey; | 45 | uint64_t hashKey; |
| 45 | uint32_t slotSize; | 46 | uint32_t slotSize; |
| 46 | uint32_t assembleSlotSize; | 47 | uint32_t assembleSlotSize; |
| @@ -107,6 +108,8 @@ struct DevAscendProgram { | |||
| 107 | } memBudget; | 108 | } memBudget; |
| 108 | DeviceRuntimeOffset deviceRuntimeOffset; | 109 | DeviceRuntimeOffset deviceRuntimeOffset; |
| 109 | const void* controlFlowBinaryAddr{nullptr}; | 110 | const void* controlFlowBinaryAddr{nullptr}; |
| 111 | + // Last AOT pool entry id that held this program's CF binary (hint for EnsureCached). | ||
| 112 | + uint8_t aotPoolLastId{0xFF}; | ||
| 110 | std::atomic<bool> runtimeDataRingBufferInited{false}; | 113 | std::atomic<bool> runtimeDataRingBufferInited{false}; |
| 111 | uint32_t stitchFunctionsize{0}; | 114 | uint32_t stitchFunctionsize{0}; |
| 112 | uint32_t stitchMaxFunctionNum{0}; | 115 | uint32_t stitchMaxFunctionNum{0}; |
| @@ -407,18 +410,7 @@ struct DevAscendProgram { | |||
| 407 | dst.launchSchedSameCluster = params.launchSchedSameCluster; | 410 | dst.launchSchedSameCluster = params.launchSchedSameCluster; |
| 408 | } | 411 | } |
| 409 | 412 | ||
| 410 | - void ResetFromLaunch() | 413 | + void ResetFromLaunch(); |
| 411 | - { | ||
| 412 | - DevArgsPreservedParams preservedParams = BackupDevArgsParams(devArgs); | ||
| 413 | - memset_s(&devArgs, sizeof(devArgs), 0, sizeof(devArgs)); | ||
| 414 | - RestoreDevArgsParams(devArgs, preservedParams); | ||
| 415 | - | ||
| 416 | - controlFlowBinaryAddr = nullptr; | ||
| 417 | - runtimeDataRingBufferInited = false; | ||
| 418 | - workspaceSize = 0; | ||
| 419 | - ctrlFlowCacheAnchor = nullptr; | ||
| 420 | - RelocProgram(reinterpret_cast<int64_t>(this), 0); | ||
| 421 | - } | ||
| 422 | 414 | ||
| 423 | void ResetRerun() | 415 | void ResetRerun() |
| 424 | { | 416 | { |
| @@ -438,80 +430,7 @@ struct DevAscendProgram { | |||
| 438 | uintptr_t end; | 430 | uintptr_t end; |
| 439 | }; | 431 | }; |
| 440 | 432 | ||
| 441 | - void RuntimeVerify(uintptr_t workspaceBegin, uintptr_t workspaceEnd) const | 433 | + void RuntimeVerify(uintptr_t workspaceBegin, uintptr_t workspaceEnd) const; |
| 442 | - { | ||
| 443 | - (void)workspaceBegin, (void)workspaceEnd; | ||
| 444 | - DEV_IF_VERBOSE_DEBUG {} | ||
| 445 | - else | ||
| 446 | - { | ||
| 447 | - return; | ||
| 448 | - } | ||
| 449 | - std::vector<DevRelocRange> rangeList = { | ||
| 450 | - symbolTable, // 0 | ||
| 451 | - symbolTableNameList, | ||
| 452 | - expressionTableOffsetList, | ||
| 453 | - hostControlFlowBinary, | ||
| 454 | - devControlFlowBinary, | ||
| 455 | - devEncodeList, // 5 | ||
| 456 | - devEncodeDataList, | ||
| 457 | - cceCodeList, | ||
| 458 | - aicpuLeafCodeList, | ||
| 459 | - aicpuLeafCodeDataList, | ||
| 460 | - startArgsInputTensorSlotIndexList, // 10 | ||
| 461 | - startArgsOutputTensorSlotIndexList, | ||
| 462 | - assembleSlotIndexList, | ||
| 463 | - outputInplaceSlotList, | ||
| 464 | - partialUpdateList, | ||
| 465 | - cellMatchRuntimePartialUpdateTableList, // 15 | ||
| 466 | - prefetchInfoList, | ||
| 467 | - disableL2List, | ||
| 468 | - controlFlowCache.inputTensorDataList, | ||
| 469 | - controlFlowCache.outputTensorDataList, | ||
| 470 | - controlFlowCache.runtimeBackup.workspace.tensorAllocators[0].slottedOutcastsBlockList, // 20 | ||
| 471 | - controlFlowCache.runtimeBackup.slotContext.slotList, | ||
| 472 | - controlFlowCache.runtimeBackup.workspace.runtimeOutcastTensorPool, | ||
| 473 | - controlFlowCache.deviceTaskCacheList, | ||
| 474 | - controlFlowCache.cacheData, | ||
| 475 | - }; | ||
| 476 | - if ((uintptr_t)data != rangeList[0].begin) { | ||
| 477 | - DEV_ERROR(ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 478 | - "#ctrl.program.verify: Assertion failed: data (0x%p) != rangeList[0].begin (0x%p)", data, | ||
| 479 | - (void*)rangeList[0].begin); | ||
| 480 | - } | ||
| 481 | - DEV_ASSERT(ProgEncodeErr::RANGE_VERIFY_FAILED, (uintptr_t)data == rangeList[0].begin); | ||
| 482 | - if (rangeList[0].begin > rangeList[0].end) { | ||
| 483 | - DEV_ERROR(ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 484 | - "#ctrl.program.verify: Assertion failed: rangeList[0].begin (0x%p) > rangeList[0].end (0x%p)", | ||
| 485 | - (void*)rangeList[0].begin, (void*)rangeList[0].end); | ||
| 486 | - } | ||
| 487 | - DEV_ASSERT(ProgEncodeErr::RANGE_VERIFY_FAILED, rangeList[0].begin <= rangeList[0].end); | ||
| 488 | - for (size_t k = 1; k < rangeList.size(); k++) { | ||
| 489 | - if (rangeList[k - 1].end > rangeList[k].begin) { | ||
| 490 | - DEV_ERROR(ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 491 | - "#ctrl.program.verify: Ranges overlap: range[%d].end (0x%p) > range[%d].begin (0x%p)", | ||
| 492 | - (int)(k - 1), (void*)rangeList[k - 1].end, (int)k, (void*)rangeList[k].begin); | ||
| 493 | - } | ||
| 494 | - if (rangeList[k].begin > rangeList[k].end) { | ||
| 495 | - DEV_ERROR(ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 496 | - "#ctrl.program.verify: Invalid range: range[%d].begin (0x%p) > range[%d].end (0x%p)", (int)k, | ||
| 497 | - (void*)rangeList[k].begin, (int)k, (void*)rangeList[k].end); | ||
| 498 | - } | ||
| 499 | - DEV_ASSERT_MSG(ProgEncodeErr::RANGE_VERIFY_FAILED, rangeList[k - 1].end <= rangeList[k].begin, | ||
| 500 | - "range:%d->%d", (int)(k - 1), (int)(k)); | ||
| 501 | - DEV_ASSERT_MSG(ProgEncodeErr::RANGE_VERIFY_FAILED, rangeList[k].begin <= rangeList[k].end, "range:%d", | ||
| 502 | - (int)k); | ||
| 503 | - } | ||
| 504 | - uintptr_t lastEnd = rangeList.back().end; | ||
| 505 | - uintptr_t dataEnd = (uintptr_t)(&data[dataSize]); | ||
| 506 | - if (lastEnd != dataEnd) { | ||
| 507 | - DEV_ERROR( | ||
| 508 | - ProgEncodeErr::RANGE_VERIFY_FAILED, | ||
| 509 | - "#ctrl.program.verify: Last range end does not match data end: rangeList.back().end (0x%p) != dataEnd " | ||
| 510 | - "(0x%p)", | ||
| 511 | - (void*)lastEnd, (void*)dataEnd); | ||
| 512 | - } | ||
| 513 | - DEV_ASSERT(ProgEncodeErr::RANGE_VERIFY_FAILED, lastEnd == dataEnd); | ||
| 514 | - } | ||
| 515 | 434 | ||
| 516 | uint64_t GetSize() const | 435 | uint64_t GetSize() const |
| 517 | { | 436 | { |
| @@ -311,8 +311,11 @@ void LogWorkspaceEncodeSummary(int kMin, uint32_t stitchNumMax, const DevAscendP | |||
| 311 | devProg.memBudget.metadata.Total(), WorkspaceBytesToKbCeil(maxWorkspaceBytes), | 311 | devProg.memBudget.metadata.Total(), WorkspaceBytesToKbCeil(maxWorkspaceBytes), |
| 312 | static_cast<int>(depthConfig.memoryDrivenWorkspace), devProg.memBudget.tensor.runtimeOutcastPoolSize); | 312 | static_cast<int>(depthConfig.memoryDrivenWorkspace), devProg.memBudget.tensor.runtimeOutcastPoolSize); |
| 313 | if (depthConfig.memoryDrivenWorkspace == 0) { | 313 | if (depthConfig.memoryDrivenWorkspace == 0) { |
| 314 | - EmitWorkspaceUserMessage(BuildNonMemoryDrivenWorkspaceUserMessage(WorkspaceBytesToKbCeil(devProg.workspaceSize), | 314 | + // No meaningful workspace budget to tune; skip the recommendation tip. |
| 315 | - WorkspaceBytesToKbCeil(workspaceStitchMin))); | 315 | + if (devProg.workspaceSize > 0) { |
| 316 | + EmitWorkspaceUserMessage(BuildNonMemoryDrivenWorkspaceUserMessage( | ||
| 317 | + WorkspaceBytesToKbCeil(devProg.workspaceSize), WorkspaceBytesToKbCeil(workspaceStitchMin))); | ||
| 318 | + } | ||
| 316 | return; | 319 | return; |
| 317 | } | 320 | } |
| 318 | EmitWorkspaceUserMessage( | 321 | EmitWorkspaceUserMessage( |
| @@ -52,9 +52,9 @@ struct DevScheState { | |||
| 52 | }; | 52 | }; |
| 53 | 53 | ||
| 54 | struct DevStartArgs : DevStartArgsBase { | 54 | struct DevStartArgs : DevStartArgsBase { |
| 55 | - uint64_t contextWorkspaceAddr; | 55 | + uint64_t contextWorkspaceAddr{0}; |
| 56 | - uint64_t contextWorkspaceSize; | 56 | + uint64_t contextWorkspaceSize{0}; |
| 57 | - DevAscendProgram* devProg; | 57 | + DevAscendProgram* devProg{nullptr}; |
| 58 | 58 | ||
| 59 | DevInputSymbol* inputSymbolList; | 59 | DevInputSymbol* inputSymbolList; |
| 60 | uint64_t inputSymbolSize; | 60 | uint64_t inputSymbolSize; |
| @@ -411,6 +411,7 @@ void DeviceWorkspaceAllocator::MarkAsNewStitchWindow() | |||
| 411 | 411 | ||
| 412 | void DeviceWorkspaceAllocator::DumpMemoryUsage(const char* hint) const | 412 | void DeviceWorkspaceAllocator::DumpMemoryUsage(const char* hint) const |
| 413 | { | 413 | { |
| 414 | + (void)hint; | ||
| 414 | #if DEBUG_MEM_DUMP_LEVEL >= DEBUG_MEM_DUMP_FULL | 415 | #if DEBUG_MEM_DUMP_LEVEL >= DEBUG_MEM_DUMP_FULL |
| 415 | wsMemDelayedDumper_.DumpStitchWindowMemoryUsage(); | 416 | wsMemDelayedDumper_.DumpStitchWindowMemoryUsage(); |
| 416 | #endif // DEBUG_MEM_DUMP_LEVEL >= DEBUG_MEM_DUMP_FULL | 417 | #endif // DEBUG_MEM_DUMP_LEVEL >= DEBUG_MEM_DUMP_FULL |
| @@ -0,0 +1,141 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 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 test_aot_code_pool.cpp | ||
| 13 | + * \brief Cover AOT CF pool when distinct kernels exceed pool entry capacity. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <gtest/gtest.h> | ||
| 17 | +#include <string> | ||
| 18 | +#include <vector> | ||
| 19 | + | ||
| 20 | +#include "test_suite_stest_ops.h" | ||
| 21 | +#include "interface/interpreter/raw_tensor_data.h" | ||
| 22 | +#include "interface/configs/config_manager.h" | ||
| 23 | +#include "machine/device/dynamic/aot_binary.h" | ||
| 24 | +#include "machine/runtime/launcher/device_launcher.h" | ||
| 25 | +#include "machine/runtime/launcher/emulation_launcher.h" | ||
| 26 | +#include "machine/runtime/runner/runtime_utils.h" | ||
| 27 | +#include "machine/runtime/context/device_launcher_context.h" | ||
| 28 | + | ||
| 29 | +using namespace npu::tile_fwk; | ||
| 30 | +using namespace npu::tile_fwk::dynamic; | ||
| 31 | + | ||
| 32 | +namespace { | ||
| 33 | +// Device/host AOT pool has AOT_CODE_POOL_NUM entries. Run beyond capacity to exercise LRU eviction. | ||
| 34 | +constexpr int kAotPoolEntries = AOT_CODE_POOL_NUM; | ||
| 35 | +constexpr int kKernelCountBeyondPool = kAotPoolEntries + 4; | ||
| 36 | +constexpr int kTile = 32; | ||
| 37 | +} // namespace | ||
| 38 | + | ||
| 39 | +class AotCodePoolTest : public testing::Test { | ||
| 40 | +public: | ||
| 41 | + void SetUp() override | ||
| 42 | + { | ||
| 43 | + DeviceLauncherContext::Get().Initialize(); | ||
| 44 | + RuntimeSetDevice(GetDeviceIdByEnvVar()); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + void TearDown() override { DeviceLauncherContext::Get().Finalize(); } | ||
| 48 | +}; | ||
| 49 | + | ||
| 50 | +namespace { | ||
| 51 | + | ||
| 52 | +void ResetProgramState() | ||
| 53 | +{ | ||
| 54 | + ProgramData::GetInstance().Reset(); | ||
| 55 | + Program::GetInstance().Reset(); | ||
| 56 | + config::Reset(); | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +int ExpectedResult(int kernelIndex) | ||
| 60 | +{ | ||
| 61 | + const int fillA = 1; | ||
| 62 | + const int fillB = 2 + kernelIndex; | ||
| 63 | + int value = fillA + fillB; | ||
| 64 | + for (int step = 0; step < (kernelIndex % 3); ++step) { | ||
| 65 | + (void)step; | ||
| 66 | + value += fillB; | ||
| 67 | + } | ||
| 68 | + return value; | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +// Build a unique CF binary per kernelIndex so each launch maps to a distinct AOT pool key. | ||
| 72 | +void BuildUniqueAddKernel(int kernelIndex, int n) | ||
| 73 | +{ | ||
| 74 | + Tensor inputA(DT_INT32, {n, n}, "A"); | ||
| 75 | + Tensor inputB(DT_INT32, {n, n}, "B"); | ||
| 76 | + Tensor output(DT_INT32, {n, n}, "O"); | ||
| 77 | + | ||
| 78 | + const int fillA = 1; | ||
| 79 | + const int fillB = 2 + kernelIndex; | ||
| 80 | + ProgramData::GetInstance().AppendInputs({ | ||
| 81 | + RawTensorData::CreateConstantTensor<int32_t>(inputA, fillA), | ||
| 82 | + RawTensorData::CreateConstantTensor<int32_t>(inputB, fillB), | ||
| 83 | + }); | ||
| 84 | + ProgramData::GetInstance().AppendOutputs({ | ||
| 85 | + RawTensorData::CreateConstantTensor<int32_t>(output, 0), | ||
| 86 | + }); | ||
| 87 | + | ||
| 88 | + const std::string funcName = "aot_pool_main_" + std::to_string(kernelIndex); | ||
| 89 | + const std::string loopName = "aot_pool_loop_" + std::to_string(kernelIndex); | ||
| 90 | + FUNCTION(funcName, {inputA, inputB}, {output}) | ||
| 91 | + { | ||
| 92 | + LOOP(loopName, FunctionType::DYNAMIC_LOOP, _, LoopRange(1)) | ||
| 93 | + { | ||
| 94 | + (void)_; | ||
| 95 | + // Vary op chain length with kernelIndex so CF hash differs even if names collide. | ||
| 96 | + Tensor acc = Add(inputA, inputB); | ||
| 97 | + for (int step = 0; step < (kernelIndex % 3); ++step) { | ||
| 98 | + (void)step; | ||
| 99 | + acc = Add(acc, inputB); | ||
| 100 | + } | ||
| 101 | + output = acc; | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +void ExpectOutputEquals(int kernelIndex, int n, const char* tag) | ||
| 107 | +{ | ||
| 108 | + auto outputResult = ProgramData::GetInstance().GetOutputData(0); | ||
| 109 | + ASSERT_NE(outputResult, nullptr) << tag << " null output at kernelIndex=" << kernelIndex; | ||
| 110 | + std::vector<int32_t> golden(static_cast<size_t>(n) * static_cast<size_t>(n), ExpectedResult(kernelIndex)); | ||
| 111 | + EXPECT_TRUE(resultCmp(golden, reinterpret_cast<int32_t*>(outputResult->data()), 0.001f)) | ||
| 112 | + << tag << " precision mismatch at kernelIndex=" << kernelIndex; | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +} // namespace | ||
| 116 | + | ||
| 117 | +TEST_F(AotCodePoolTest, ExceedPoolCapacitySequentialKernels) | ||
| 118 | +{ | ||
| 119 | + ASSERT_GT(kKernelCountBeyondPool, kAotPoolEntries); | ||
| 120 | + ASSERT_GT(kKernelCountBeyondPool, static_cast<int>(AOT_CODE_POOL_NUM)); | ||
| 121 | + | ||
| 122 | + const int n = kTile * 2; | ||
| 123 | + DeviceLauncherConfig config; | ||
| 124 | + config.blockdim = 24; | ||
| 125 | + | ||
| 126 | + for (int kernelIndex = 0; kernelIndex < kKernelCountBeyondPool; ++kernelIndex) { | ||
| 127 | + ResetProgramState(); | ||
| 128 | + TileShape::Current().SetVecTile(kTile, kTile); | ||
| 129 | + BuildUniqueAddKernel(kernelIndex, n); | ||
| 130 | + | ||
| 131 | + // Emulation does not execute aicore leaves (CostModel); only check rc for host AOT pool. | ||
| 132 | + EXPECT_EQ(0, EmulationLauncher::EmulationRunOnce(Program::GetInstance().GetLastFunction(), nullptr, config)) | ||
| 133 | + << "Emulation failed at kernelIndex=" << kernelIndex; | ||
| 134 | + | ||
| 135 | +#ifdef BUILD_WITH_CANN | ||
| 136 | + EXPECT_EQ(0, DeviceLauncher::DeviceRunOnce(Program::GetInstance().GetLastFunction(), nullptr, config)) | ||
| 137 | + << "Device failed at kernelIndex=" << kernelIndex; | ||
| 138 | + ExpectOutputEquals(kernelIndex, n, "Device"); | ||
| 139 | +#endif | ||
| 140 | + } | ||
| 141 | +} | ||


三次循环对头开销的影响有多大?