| @@ -5,6 +5,7 @@ cmake-build-debugindocker/ | |||
| 5 | .DS_Store | 5 | .DS_Store |
| 6 | # distribution and packaging | 6 | # distribution and packaging |
| 7 | build/ | 7 | build/ |
| 8 | +build_ut/ | ||
| 8 | build_out/ | 9 | build_out/ |
| 9 | output/ | 10 | output/ |
| 10 | dist/ | 11 | dist/ |
| @@ -0,0 +1,116 @@ | |||
| 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 | +#ifndef AICPU_LRU_CACHE_H | ||
| 12 | +#define AICPU_LRU_CACHE_H | ||
| 13 | + | ||
| 14 | +#include <list> | ||
| 15 | +#include <memory> | ||
| 16 | +#include <mutex> | ||
| 17 | +#include <string> | ||
| 18 | +#include <unordered_map> | ||
| 19 | +#include <vector> | ||
| 20 | + | ||
| 21 | +#include "opdev/aicpu/aicpu_task.h" | ||
| 22 | + | ||
| 23 | +namespace op { | ||
| 24 | +namespace internal { | ||
| 25 | + | ||
| 26 | +struct IdleLruInfo { | ||
| 27 | + std::list<AicpuTask*>::iterator idleIt; | ||
| 28 | + size_t seed = 0U; | ||
| 29 | +}; | ||
| 30 | +struct SpaceCacheData { | ||
| 31 | + size_t cachedTaskNum = 0U; | ||
| 32 | + std::list<AicpuTask*> idleLru; | ||
| 33 | + std::unordered_map<AicpuTask*, IdleLruInfo> taskInfo; | ||
| 34 | +}; | ||
| 35 | +inline std::mutex gSpaceCacheMutex; | ||
| 36 | +inline std::unordered_map<void*, SpaceCacheData>& SpaceCacheDataMap() | ||
| 37 | +{ | ||
| 38 | + static std::unordered_map<void*, SpaceCacheData> instance; | ||
| 39 | + return instance; | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +inline size_t GetCachedTaskNum(const void* space) | ||
| 43 | +{ | ||
| 44 | + const std::lock_guard<std::mutex> lk(gSpaceCacheMutex); | ||
| 45 | + const auto& map = SpaceCacheDataMap(); | ||
| 46 | + const auto it = map.find(const_cast<void*>(space)); | ||
| 47 | + return (it == map.end()) ? 0U : it->second.cachedTaskNum; | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +inline void IncCachedTaskNum(const void* space) | ||
| 51 | +{ | ||
| 52 | + const std::lock_guard<std::mutex> lk(gSpaceCacheMutex); | ||
| 53 | + ++SpaceCacheDataMap()[const_cast<void*>(space)].cachedTaskNum; | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +inline void RegisterCachedTask(void* space, AicpuTask* task, const size_t seed) | ||
| 57 | +{ | ||
| 58 | + const std::lock_guard<std::mutex> lk(gSpaceCacheMutex); | ||
| 59 | + auto& data = SpaceCacheDataMap()[space]; | ||
| 60 | + const auto [it, inserted] = data.taskInfo.emplace(task, IdleLruInfo{}); | ||
| 61 | + if (!inserted) { | ||
| 62 | + return; | ||
| 63 | + } | ||
| 64 | + data.idleLru.push_front(task); | ||
| 65 | + it->second.idleIt = data.idleLru.begin(); | ||
| 66 | + it->second.seed = seed; | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +inline void ActivateCachedTask(AicpuTask* task, void* space) | ||
| 70 | +{ | ||
| 71 | + const std::lock_guard<std::mutex> lk(gSpaceCacheMutex); | ||
| 72 | + auto& map = SpaceCacheDataMap(); | ||
| 73 | + const auto spaceIt = map.find(space); | ||
| 74 | + if (spaceIt == map.end()) { | ||
| 75 | + return; | ||
| 76 | + } | ||
| 77 | + auto& data = spaceIt->second; | ||
| 78 | + const auto it = data.taskInfo.find(task); | ||
| 79 | + if (it == data.taskInfo.end()) { | ||
| 80 | + return; | ||
| 81 | + } | ||
| 82 | + data.idleLru.splice(data.idleLru.begin(), data.idleLru, it->second.idleIt); | ||
| 83 | + it->second.idleIt = data.idleLru.begin(); | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +inline void ClearSpaceCacheData(void* space) | ||
| 87 | +{ | ||
| 88 | + const std::lock_guard<std::mutex> lk(gSpaceCacheMutex); | ||
| 89 | + (void)SpaceCacheDataMap().erase(space); | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +struct TaskSpaceCtx { | ||
| 93 | + AicpuTaskSpace* space; | ||
| 94 | + std::unordered_map<size_t, std::vector<std::unique_ptr<AicpuTask>>>& hashMap; | ||
| 95 | + std::mutex& mutex; | ||
| 96 | + bool& hasInit; | ||
| 97 | + const bool isTf; | ||
| 98 | + const ge::UnknowShapeOpType unknownType; | ||
| 99 | + const std::string& opType; | ||
| 100 | + bool (*isIdle)(const AicpuTask*); | ||
| 101 | +}; | ||
| 102 | + | ||
| 103 | +bool AicpuCreateTaskUnlocked(const TaskSpaceCtx& taskCache, aclOpExecutor* executor, | ||
| 104 | + const FVector<std::string>& attrNames, op::OpArgContext* args, | ||
| 105 | + const FVector<const aclTensor*>& inputs, std::unique_ptr<AicpuTask>& task); | ||
| 106 | + | ||
| 107 | +std::unique_ptr<AicpuTask> AicpuEvictOneTask(const TaskSpaceCtx& taskCache); | ||
| 108 | + | ||
| 109 | +AicpuTask* AicpuCommitTask(const TaskSpaceCtx& taskCache, std::unique_ptr<AicpuTask>& task, const size_t seed, | ||
| 110 | + aclOpExecutor* executor, const FVector<const aclTensor*>& inputs, bool& needRegister, | ||
| 111 | + std::unique_ptr<AicpuTask>& evictedOwner); | ||
| 112 | + | ||
| 113 | +} // namespace internal | ||
| 114 | +} // namespace op | ||
| 115 | + | ||
| 116 | +#endif // AICPU_LRU_CACHE_H | ||
| @@ -9,12 +9,16 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | #include <algorithm> | 11 | #include <algorithm> |
| 12 | +#include <atomic> | ||
| 12 | #include <array> | 13 | #include <array> |
| 13 | #include <exception> | 14 | #include <exception> |
| 15 | +#include <list> | ||
| 14 | #include <memory> | 16 | #include <memory> |
| 17 | +#include <unordered_map> | ||
| 15 | 18 | ||
| 16 | #include "opdev/aicpu/aicpu_task.h" | 19 | #include "opdev/aicpu/aicpu_task.h" |
| 17 | #include "aicpu_engine_struct.h" | 20 | #include "aicpu_engine_struct.h" |
| 21 | +#include "aicpu_lru_cache.h" | ||
| 18 | #include "aclnn/aclnn_base.h" | 22 | #include "aclnn/aclnn_base.h" |
| 19 | #include "opdev/fast_vector.h" | 23 | #include "opdev/fast_vector.h" |
| 20 | #include "mmpa/mmpa_api.h" | 24 | #include "mmpa/mmpa_api.h" |
| @@ -56,6 +60,7 @@ AicpuTask* SavePendingOverflowTask(std::unique_ptr<AicpuTask> task) | |||
| 56 | gPendingOverflowTask = std::move(task); | 60 | gPendingOverflowTask = std::move(task); |
| 57 | return gPendingOverflowTask.get(); | 61 | return gPendingOverflowTask.get(); |
| 58 | } | 62 | } |
| 63 | + | ||
| 59 | } // namespace | 64 | } // namespace |
| 60 | AicpuTimeStamp gAicpuTimeStamp = {}; | 65 | AicpuTimeStamp gAicpuTimeStamp = {}; |
| 61 | std::set<AicpuTaskSpace*> gAicpuTaskSpaceSet; | 66 | std::set<AicpuTaskSpace*> gAicpuTaskSpaceSet; |
| @@ -87,6 +92,7 @@ void aclnnAicpuFinalize() | |||
| 87 | for (auto space : gAicpuTaskSpaceSet) { | 92 | for (auto space : gAicpuTaskSpaceSet) { |
| 88 | if (space != nullptr) { | 93 | if (space != nullptr) { |
| 89 | space->Clear(); | 94 | space->Clear(); |
| 95 | + ClearSpaceCacheData(space); | ||
| 90 | } | 96 | } |
| 91 | } | 97 | } |
| 92 | gAicpuTaskSpaceSet.clear(); | 98 | gAicpuTaskSpaceSet.clear(); |
| @@ -287,7 +293,20 @@ bool AicpuTaskSpace::IsRef(const size_t index, const bool isInput) const | |||
| 287 | } | 293 | } |
| 288 | } | 294 | } |
| 289 | 295 | ||
| 290 | -void AicpuTask::SetVisit(bool visit) { isVisit_ = visit; } | 296 | +void AicpuTask::SetVisit(bool visit) |
| 297 | +{ | ||
| 298 | + if (visit) { | ||
| 299 | + isVisit_ = visit; | ||
| 300 | + return; | ||
| 301 | + } | ||
| 302 | + auto* space = static_cast<AicpuTaskSpace*>(space_); | ||
| 303 | + if (space == nullptr) { | ||
| 304 | + isVisit_ = false; | ||
| 305 | + return; | ||
| 306 | + } | ||
| 307 | + const std::lock_guard<std::mutex> lk(space->mutex_); | ||
| 308 | + isVisit_ = false; | ||
| 309 | +} | ||
🟡 Medium Priority 改动点: 受影响契约: 失败模式:task 的释放/析构路径一旦调用 ![]() ![]() Incorrect? | |||
| 291 | 310 | ||
| 292 | uint64_t RoundUp(const uint64_t originValue, const uint64_t multipleOf) | 311 | uint64_t RoundUp(const uint64_t originValue, const uint64_t multipleOf) |
| 293 | { | 312 | { |
| @@ -357,109 +376,190 @@ AicpuTask* AicpuTaskSpace::FindTask(aclOpExecutor* executor, op::OpArgContext* a | |||
| 357 | size_t keyLen = 0; | 376 | size_t keyLen = 0; |
| 358 | auto seed = GenTaskKey(inputKey, keyLen, args, inputs); | 377 | auto seed = GenTaskKey(inputKey, keyLen, args, inputs); |
| 359 | const auto& iter = hashMap_.find(seed); | 378 | const auto& iter = hashMap_.find(seed); |
| 379 | + AicpuTask* task = nullptr; | ||
| 360 | if (iter != hashMap_.end()) { | 380 | if (iter != hashMap_.end()) { |
| 361 | - for (auto& task : iter->second) { | 381 | + for (auto& cached : iter->second) { |
| 362 | - if (task->isVisit_) { | 382 | + if (cached->isVisit_ || cached->keyLen_ != keyLen) { |
| 363 | - continue; | ||
| 364 | - } | ||
| 365 | - if (task->keyLen_ != keyLen) { | ||
| 366 | continue; | 383 | continue; |
| 367 | } | 384 | } |
| 368 | size_t i = 0U; | 385 | size_t i = 0U; |
| 369 | for (; i < keyLen; ++i) { | 386 | for (; i < keyLen; ++i) { |
| 370 | - if (task->inputKey_[i] != inputKey[i]) { | 387 | + if (cached->inputKey_[i] != inputKey[i]) { |
| 371 | break; | 388 | break; |
| 372 | } | 389 | } |
| 373 | } | 390 | } |
| 374 | if (i == keyLen) { | 391 | if (i == keyLen) { |
| 375 | - uint64_t deviceCacheSize = CalcDeviceCacheSize(inputs, task); | 392 | + const uint64_t deviceCacheSize = CalcDeviceCacheSize(inputs, cached); |
| 376 | if (deviceCacheSize > executor->workspaceDeviceAicpuMem_) { | 393 | if (deviceCacheSize > executor->workspaceDeviceAicpuMem_) { |
| 377 | executor->workspaceDeviceAicpuMem_ = deviceCacheSize; | 394 | executor->workspaceDeviceAicpuMem_ = deviceCacheSize; |
| 378 | } | 395 | } |
| 379 | - RecordAicpuTime(kFindTaskEnd); | 396 | + cached->isVisit_ = true; |
| 380 | - task->isVisit_ = true; | 397 | + ActivateCachedTask(cached.get(), this); |
| 381 | - OP_LOGI("Find %s task success, no need create, cache_size=%lu.", opType_.c_str(), | 398 | + task = cached.get(); |
| 382 | - executor->workspaceDeviceAicpuMem_); | 399 | + break; |
| 383 | - return task.get(); | ||
| 384 | } | 400 | } |
| 385 | } | 401 | } |
| 386 | } | 402 | } |
| 387 | - OP_LOGI("Do not find %s task, need to create task.", opType_.c_str()); | ||
| 388 | RecordAicpuTime(kFindTaskEnd); | 403 | RecordAicpuTime(kFindTaskEnd); |
| 404 | + if (task == nullptr) { | ||
| 405 | + OP_LOGI("Do not find %s task, need to create task.", opType_.c_str()); | ||
| 406 | + return nullptr; | ||
| 407 | + } | ||
| 408 | + OP_LOGI("Find %s task success, no need create, cache_size=%lu.", opType_.c_str(), | ||
| 409 | + executor->workspaceDeviceAicpuMem_); | ||
| 410 | + return task; | ||
| 411 | +} | ||
| 412 | + | ||
| 413 | +bool AicpuCreateTaskUnlocked(const TaskSpaceCtx& taskCache, aclOpExecutor* executor, | ||
| 414 | + const FVector<std::string>& attrNames, op::OpArgContext* args, | ||
| 415 | + const FVector<const aclTensor*>& inputs, std::unique_ptr<AicpuTask>& task) | ||
| 416 | +{ | ||
| 417 | + if (taskCache.isTf) { | ||
| 418 | + task = std::make_unique<AicpuTfTask>(taskCache.opType, taskCache.unknownType); | ||
| 419 | + } else { | ||
| 420 | + task = std::make_unique<AicpuCCTask>(taskCache.opType, taskCache.unknownType); | ||
| 421 | + } | ||
| 422 | + if (task == nullptr) { | ||
| 423 | + OP_LOGE(ACLNN_ERR_INNER, "Make aicpu task failed!."); | ||
| 424 | + return false; | ||
| 425 | + } | ||
| 426 | + task->SetSpace(taskCache.space); | ||
| 427 | + AicpuAttrs attrs; | ||
| 428 | + CreateAicpuAttrs(*args->GetOpArg(op::OP_ATTR_ARG), attrNames, attrs); | ||
| 429 | + FVector<aclTensor*> outputs; | ||
| 430 | + CreateTensorList(executor, *args->GetOpArg(op::OP_OUTPUT_ARG), outputs); | ||
| 431 | + if (task->Init(inputs, outputs, attrs) != OK) { | ||
| 432 | + OP_LOGE(ACLNN_ERR_INNER, "Aicpu task init failed!."); | ||
| 433 | + return false; | ||
| 434 | + } | ||
| 435 | + task->SetVisit(true); | ||
| 436 | + const uint64_t deviceCacheSize = taskCache.space->CalcDeviceCacheSize(inputs, task); | ||
| 437 | + if (deviceCacheSize > executor->workspaceDeviceAicpuMem_) { | ||
| 438 | + executor->workspaceDeviceAicpuMem_ = deviceCacheSize; | ||
| 439 | + } | ||
| 440 | + return true; | ||
| 441 | +} | ||
| 442 | + | ||
| 443 | +std::unique_ptr<AicpuTask> AicpuEvictOneTask(const TaskSpaceCtx& taskCache) | ||
| 444 | +{ | ||
| 445 | + AicpuTask* evicted = nullptr; | ||
| 446 | + size_t evictedSeed = 0; | ||
| 447 | + { | ||
| 448 | + const std::lock_guard<std::mutex> lk(gSpaceCacheMutex); | ||
| 449 | + auto& map = SpaceCacheDataMap(); | ||
| 450 | + const auto spaceIt = map.find(taskCache.space); | ||
| 451 | + if (spaceIt != map.end()) { | ||
| 452 | + auto& data = spaceIt->second; | ||
| 453 | + for (auto rit = data.idleLru.rbegin(); rit != data.idleLru.rend(); ++rit) { | ||
| 454 | + const auto it = data.taskInfo.find(*rit); | ||
| 455 | + if (it == data.taskInfo.end()) { | ||
| 456 | + continue; | ||
| 457 | + } | ||
| 458 | + if (!taskCache.isIdle(*rit)) { | ||
| 459 | + continue; | ||
| 460 | + } | ||
| 461 | + evicted = *rit; | ||
| 462 | + evictedSeed = it->second.seed; | ||
| 463 | + data.idleLru.erase(it->second.idleIt); | ||
| 464 | + (void)data.taskInfo.erase(it); | ||
| 465 | + --data.cachedTaskNum; | ||
| 466 | + break; | ||
| 467 | + } | ||
| 468 | + } | ||
| 469 | + } | ||
| 470 | + if (evicted == nullptr) { | ||
| 471 | + return nullptr; | ||
| 472 | + } | ||
| 473 | + const auto bucketIt = taskCache.hashMap.find(evictedSeed); | ||
| 474 | + if (bucketIt == taskCache.hashMap.end()) { | ||
| 475 | + OP_LOGE(ACLNN_ERR_INNER, "Evicted aicpu task not found in hash bucket, skip insert."); | ||
| 476 | + return nullptr; | ||
| 477 | + } | ||
🟡 Medium Priority 改动点: 受影响契约:三个状态必须保持一致—— 失败模式:一旦走到失败分支,被淘汰者仍留在 建议:调整顺序:先在 ![]() ![]() Incorrect? | |||
| 478 | + auto& bucket = bucketIt->second; | ||
| 479 | + for (auto bIt = bucket.begin(); bIt != bucket.end(); ++bIt) { | ||
| 480 | + if (bIt->get() == evicted) { | ||
| 481 | + std::unique_ptr<AicpuTask> owner = std::move(*bIt); | ||
| 482 | + bucket.erase(bIt); | ||
| 483 | + if (bucket.empty()) { | ||
| 484 | + taskCache.hashMap.erase(bucketIt); | ||
| 485 | + } | ||
| 486 | + OP_LOGI("Evict %s idle task (LRU tail) for new key, cache=%zu.", taskCache.opType.c_str(), | ||
| 487 | + kAicpuCacheLimit); | ||
| 488 | + return owner; | ||
| 489 | + } | ||
| 490 | + } | ||
| 491 | + OP_LOGE(ACLNN_ERR_INNER, "Evicted aicpu task not found in hash bucket, skip insert."); | ||
| 389 | return nullptr; | 492 | return nullptr; |
| 390 | } | 493 | } |
| 391 | 494 | ||
| 495 | +AicpuTask* AicpuCommitTask(const TaskSpaceCtx& taskCache, std::unique_ptr<AicpuTask>& task, const size_t seed, | ||
| 496 | + aclOpExecutor* executor, const FVector<const aclTensor*>& inputs, bool& needRegister, | ||
| 497 | + std::unique_ptr<AicpuTask>& evictedOwner) | ||
| 498 | +{ | ||
| 499 | + const std::lock_guard<std::mutex> lk(taskCache.mutex); | ||
| 500 | + if (!taskCache.hasInit) { | ||
| 501 | + taskCache.hasInit = true; | ||
| 502 | + needRegister = true; | ||
| 503 | + } | ||
| 504 | + size_t cachedTaskNum = GetCachedTaskNum(taskCache.space); | ||
| 505 | + bool slotAvailable = (cachedTaskNum < kAicpuCacheLimit); | ||
| 506 | + if (!slotAvailable) { | ||
| 507 | + evictedOwner = AicpuEvictOneTask(taskCache); | ||
| 508 | + slotAvailable = (evictedOwner != nullptr); | ||
| 509 | + if (slotAvailable) { | ||
| 510 | + cachedTaskNum = kAicpuCacheLimit - 1U; | ||
| 511 | + } | ||
| 512 | + } | ||
| 513 | + if (slotAvailable) { | ||
| 514 | + taskCache.hashMap[seed].emplace_back(std::move(task)); | ||
| 515 | + IncCachedTaskNum(taskCache.space); | ||
| 516 | + RegisterCachedTask(taskCache.space, taskCache.hashMap[seed].back().get(), seed); | ||
| 517 | + OP_LOGI("Create %s task success, total=%zu, cache_size=%lu.", taskCache.opType.c_str(), cachedTaskNum + 1U, | ||
| 518 | + executor->workspaceDeviceAicpuMem_); | ||
| 519 | + return taskCache.hashMap[seed].back().get(); | ||
| 520 | + } | ||
| 521 | + AicpuTask* const overflow = SavePendingOverflowTask(std::move(task)); | ||
| 522 | + if (overflow == nullptr) { | ||
| 523 | + OP_LOGE(ACLNN_ERR_INNER, "Create %s overflow task failed.", taskCache.opType.c_str()); | ||
| 524 | + return nullptr; | ||
| 525 | + } | ||
| 526 | + OP_LOGI("Create %s task success without cache, cache full: total=%zu, max=%zu, cache_size=%lu.", | ||
| 527 | + taskCache.opType.c_str(), cachedTaskNum, kAicpuCacheLimit, executor->workspaceDeviceAicpuMem_); | ||
| 528 | + return overflow; | ||
| 529 | +} | ||
| 530 | + | ||
| 392 | AicpuTask* AicpuTaskSpace::GetOrCreateTask(aclOpExecutor* executor, const FVector<std::string>& attrNames, | 531 | AicpuTask* AicpuTaskSpace::GetOrCreateTask(aclOpExecutor* executor, const FVector<std::string>& attrNames, |
| 393 | op::OpArgContext* args) | 532 | op::OpArgContext* args) |
| 394 | { | 533 | { |
| 395 | gAicpuTimeStamp.isEnable = EnableAicpuTimeStamp(); | 534 | gAicpuTimeStamp.isEnable = EnableAicpuTimeStamp(); |
| 396 | FVector<const aclTensor*> inputs; | 535 | FVector<const aclTensor*> inputs; |
| 397 | - FVector<aclTensor*> outputs; | ||
| 398 | CreateTensorList(executor, *args->GetOpArg(op::OP_INPUT_ARG), inputs); | 536 | CreateTensorList(executor, *args->GetOpArg(op::OP_INPUT_ARG), inputs); |
| 399 | - auto cacheTaskNum = [this]() { | ||
| 400 | - size_t taskNum = 0U; | ||
| 401 | - for (const auto& item : hashMap_) { | ||
| 402 | - taskNum += item.second.size(); | ||
| 403 | - } | ||
| 404 | - return taskNum; | ||
| 405 | - }; | ||
| 406 | auto task = FindTask(executor, args, inputs); | 537 | auto task = FindTask(executor, args, inputs); |
| 407 | if (task != nullptr) { | 538 | if (task != nullptr) { |
| 408 | return task; | 539 | return task; |
| 409 | } | 540 | } |
| 410 | - // 将task设置到全局的space中,需要加锁 | ||
| 411 | - const std::lock_guard<std::mutex> lk(mutex_); | ||
| 412 | OP_LOGI("Start to create %s task!", opType_.c_str()); | 541 | OP_LOGI("Start to create %s task!", opType_.c_str()); |
| 413 | - std::unique_ptr<AicpuTask> uniqueTask; | ||
| 414 | - if (isTf_) { | ||
| 415 | - // todo ge::MakeUnique | ||
| 416 | - uniqueTask = std::make_unique<AicpuTfTask>(opType_, unknownType_); | ||
| 417 | - } else { | ||
| 418 | - uniqueTask = std::make_unique<AicpuCCTask>(opType_, unknownType_); | ||
| 419 | - } | ||
| 420 | - if (uniqueTask == nullptr) { | ||
| 421 | - OP_LOGE(ACLNN_ERR_INNER, "Make aicpu task failed!."); | ||
| 422 | - return nullptr; | ||
| 423 | - } | ||
| 424 | - uniqueTask->SetSpace(this); | ||
| 425 | - AicpuAttrs attrs; | ||
| 426 | - CreateAicpuAttrs(*args->GetOpArg(op::OP_ATTR_ARG), attrNames, attrs); | ||
| 427 | - CreateTensorList(executor, *args->GetOpArg(op::OP_OUTPUT_ARG), outputs); | ||
| 428 | - if (uniqueTask->Init(inputs, outputs, attrs) != OK) { | ||
| 429 | - OP_LOGE(ACLNN_ERR_INNER, "Aicpu task init failed!."); | ||
| 430 | - return nullptr; | ||
| 431 | - } | ||
| 432 | - SaveAicpuTaskSpace(uniqueTask->space_); | ||
| 433 | uint8_t inputKey[kAicpuKeyBufLen]; | 542 | uint8_t inputKey[kAicpuKeyBufLen]; |
| 434 | size_t keyLen = 0; | 543 | size_t keyLen = 0; |
| 435 | - auto seed = GenTaskKey(inputKey, keyLen, args, inputs); | 544 | + size_t seed = 0; |
| 436 | - uniqueTask->keyLen_ = keyLen; | 545 | + std::unique_ptr<AicpuTask> newTask; |
| 437 | - for (size_t i = 0U; i < keyLen; ++i) { | 546 | + TaskSpaceCtx taskCache{this, hashMap_, mutex_, hasInit_, |
| 438 | - uniqueTask->inputKey_[i] = inputKey[i]; | 547 | + isTf_, unknownType_, opType_, [](const AicpuTask* t) { return !t->isVisit_; }}; |
| 439 | - } | 548 | + seed = GenTaskKey(inputKey, keyLen, args, inputs); |
| 440 | - uniqueTask->isVisit_ = true; | 549 | + if (!AicpuCreateTaskUnlocked(taskCache, executor, attrNames, args, inputs, newTask)) { |
🟡 Medium Priority 改动点: 受影响契约: 失败模式:同一个 space 上,线程 A 正在 ![]() ![]() Incorrect? | |||
| 441 | - uint64_t deviceCacheSize = CalcDeviceCacheSize(inputs, uniqueTask); | ||
| 442 | - if (deviceCacheSize > executor->workspaceDeviceAicpuMem_) { | ||
| 443 | - executor->workspaceDeviceAicpuMem_ = deviceCacheSize; | ||
| 444 | - } | ||
| 445 | - hasInit_ = true; | ||
| 446 | - const size_t cachedTaskNum = cacheTaskNum(); | ||
| 447 | - if (cachedTaskNum < kAicpuCacheLimit) { | ||
| 448 | - hashMap_[seed].emplace_back(std::move(uniqueTask)); | ||
| 449 | - task = hashMap_[seed].back().get(); | ||
| 450 | - OP_LOGI("Create %s task success, total=%zu, cache_size=%lu.", opType_.c_str(), cachedTaskNum + 1U, | ||
| 451 | - executor->workspaceDeviceAicpuMem_); | ||
| 452 | - return task; | ||
| 453 | - } | ||
| 454 | - | ||
| 455 | - task = SavePendingOverflowTask(std::move(uniqueTask)); | ||
| 456 | - if (task == nullptr) { | ||
| 457 | - OP_LOGE(ACLNN_ERR_INNER, "Create %s overflow task failed.", opType_.c_str()); | ||
| 458 | return nullptr; | 550 | return nullptr; |
| 459 | } | 551 | } |
| 460 | - OP_LOGI("Create %s task success without cache, cache full: total=%zu, max=%zu, cache_size=%lu.", opType_.c_str(), | 552 | + newTask->keyLen_ = keyLen; |
| 461 | - cachedTaskNum, kAicpuCacheLimit, executor->workspaceDeviceAicpuMem_); | 553 | + for (size_t i = 0U; i < keyLen; ++i) { |
| 462 | - return task; | 554 | + newTask->inputKey_[i] = inputKey[i]; |
| 555 | + } | ||
| 556 | + bool needRegister = false; | ||
| 557 | + std::unique_ptr<AicpuTask> evictedOwner; | ||
| 558 | + AicpuTask* committedTask = AicpuCommitTask(taskCache, newTask, seed, executor, inputs, needRegister, evictedOwner); | ||
| 559 | + if (needRegister) { | ||
| 560 | + SaveAicpuTaskSpace(this); | ||
| 561 | + } | ||
| 562 | + return committedTask; | ||
| 463 | } | 563 | } |
| 464 | 564 | ||
| 465 | aclnnStatus AicpuTask::SetIoTensors(aclOpExecutor* executor, op::OpArgContext* args) | 565 | aclnnStatus AicpuTask::SetIoTensors(aclOpExecutor* executor, op::OpArgContext* args) |
| @@ -0,0 +1,254 @@ | |||
| 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 | +#include <gtest/gtest.h> | ||
| 11 | +#include <atomic> | ||
| 12 | +#include <chrono> | ||
| 13 | +#include <memory> | ||
| 14 | +#include <thread> | ||
| 15 | +#include <vector> | ||
| 16 | + | ||
| 17 | +#define private public | ||
| 18 | +#define protected public | ||
| 19 | +#include "opdev/aicpu/aicpu_task.h" | ||
| 20 | +#undef private | ||
| 21 | +#undef protected | ||
| 22 | + | ||
| 23 | +#include "aicpu_lru_cache.h" | ||
| 24 | + | ||
| 25 | +using namespace op::internal; | ||
| 26 | + | ||
| 27 | +namespace { | ||
| 28 | +class AicpuLruCacheUt : public testing::Test { | ||
| 29 | +protected: | ||
| 30 | + void SetUp() override { space = std::make_unique<AicpuTaskSpace>("UtLruOp", ge::DEPEND_IN_SHAPE, false); } | ||
| 31 | + | ||
| 32 | + void TearDown() override { ClearSpaceCacheData(space.get()); } | ||
| 33 | + | ||
| 34 | + std::unique_ptr<AicpuCCTask> MakeRegisteredTask() | ||
| 35 | + { | ||
| 36 | + auto task = std::make_unique<AicpuCCTask>("UtLruOp", ge::DEPEND_IN_SHAPE); | ||
| 37 | + task->SetSpace(space.get()); | ||
| 38 | + RegisterCachedTask(space.get(), task.get(), 0U); | ||
| 39 | + IncCachedTaskNum(space.get()); | ||
| 40 | + return task; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + TaskSpaceCtx MakeCtx(AicpuTaskSpace& target) | ||
| 44 | + { | ||
| 45 | + return TaskSpaceCtx{ | ||
| 46 | + &target, target.hashMap_, target.mutex_, target.hasInit_, | ||
| 47 | + target.isTf_, target.unknownType_, target.opType_, [](const AicpuTask* t) { return !t->isVisit_; }}; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + void EvictViaSpace(AicpuTaskSpace& target) | ||
| 51 | + { | ||
| 52 | + const std::lock_guard<std::mutex> lk(target.mutex_); | ||
| 53 | + (void)AicpuEvictOneTask(MakeCtx(target)); | ||
| 54 | + } | ||
🟡 Medium Priority 问题:新增 UT 只覆盖了淘汰索引的“手工注册”状态,完全没有覆盖本次改动的核心路径,且把一条错误分支的行为固化成了期望值。 证据链:
具体会漏掉的回归: 建议:新增通过 AicpuCommitTask 真实入缓存的用例(task 同时存在于 hashMap_ 与 LRU),覆盖淘汰成功分支与“全部 task 使用中→淘汰失败→overflow”分支,并断言 cachedTaskNum / idleLru / taskInfo / hashMap 四者一致;避免让用例依赖“hashMap 无对应桶”的错误分支来断言计数变化。 ![]() ![]() Incorrect? | |||
| 55 | + | ||
| 56 | + SpaceCacheData& Data() | ||
| 57 | + { | ||
| 58 | + // UT内独占访问(单线程用例),直接加锁读取快照亦可;此处持锁返回引用仅侢单线程断言 | ||
| 59 | + return SpaceCacheDataMap()[space.get()]; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + std::unique_ptr<AicpuTaskSpace> space; | ||
| 63 | +}; | ||
| 64 | + | ||
| 65 | +// 空间不存在时旁路操作安全;无space的task释放仅清标记 | ||
| 66 | +TEST_F(AicpuLruCacheUt, UnknownSpaceSafety) | ||
| 67 | +{ | ||
| 68 | + int fakeSpace = 0; | ||
| 69 | + EXPECT_EQ(GetCachedTaskNum(&fakeSpace), 0U); | ||
| 70 | + AicpuCCTask task("UtLruOp", ge::DEPEND_IN_SHAPE); | ||
| 71 | + task.SetVisit(false); | ||
| 72 | + ActivateCachedTask(&task, &fakeSpace); | ||
| 73 | + ClearSpaceCacheData(&fakeSpace); | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +// 计数器增减与淘汰联动;占用中的task不可淘汰 | ||
| 77 | +TEST_F(AicpuLruCacheUt, CounterWithEvictSkipInUse) | ||
| 78 | +{ | ||
| 79 | + EXPECT_EQ(GetCachedTaskNum(space.get()), 0U); | ||
| 80 | + auto taskA = MakeRegisteredTask(); | ||
| 81 | + auto taskB = MakeRegisteredTask(); | ||
| 82 | + EXPECT_EQ(GetCachedTaskNum(space.get()), 2U); | ||
| 83 | + taskA->SetVisit(true); | ||
| 84 | + EvictViaSpace(*space); | ||
| 85 | + EXPECT_EQ(GetCachedTaskNum(space.get()), 1U); | ||
| 86 | + EXPECT_EQ(Data().taskInfo.count(taskA.get()), 1U); | ||
| 87 | + EXPECT_EQ(Data().taskInfo.count(taskB.get()), 0U); | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +// 注册即入链;释放只清标记不动链;激活刷新到链头 | ||
| 91 | +TEST_F(AicpuLruCacheUt, RegisterInChainReleaseKeepsPosition) | ||
| 92 | +{ | ||
| 93 | + auto task = MakeRegisteredTask(); | ||
| 94 | + EXPECT_EQ(Data().idleLru.size(), 1U); | ||
| 95 | + EXPECT_EQ(Data().idleLru.front(), task.get()); | ||
| 96 | + | ||
| 97 | + task->SetVisit(false); | ||
| 98 | + EXPECT_EQ(task->isVisit_, false); | ||
| 99 | + EXPECT_EQ(Data().idleLru.size(), 1U); | ||
| 100 | + | ||
| 101 | + auto taskB = MakeRegisteredTask(); | ||
| 102 | + EXPECT_EQ(Data().idleLru.front(), taskB.get()); | ||
| 103 | + ActivateCachedTask(task.get(), space.get()); | ||
| 104 | + EXPECT_EQ(Data().idleLru.front(), task.get()); | ||
| 105 | + EXPECT_EQ(Data().idleLru.size(), 2U); | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +// 重复释放幂等 | ||
| 109 | +TEST_F(AicpuLruCacheUt, DoubleReleaseGuard) | ||
| 110 | +{ | ||
| 111 | + auto task = MakeRegisteredTask(); | ||
| 112 | + task->SetVisit(false); | ||
| 113 | + task->SetVisit(false); | ||
| 114 | + EXPECT_EQ(Data().idleLru.size(), 1U); | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +// 激活刷新位置到链头;淘汰从链尾扫,跳过占用 | ||
| 118 | +TEST_F(AicpuLruCacheUt, ActivateMovesToFrontAndEvictTail) | ||
| 119 | +{ | ||
| 120 | + auto taskA = MakeRegisteredTask(); | ||
| 121 | + auto taskB = MakeRegisteredTask(); | ||
| 122 | + EXPECT_EQ(Data().idleLru.front(), taskB.get()); | ||
| 123 | + ActivateCachedTask(taskA.get(), space.get()); | ||
| 124 | + EXPECT_EQ(Data().idleLru.front(), taskA.get()); | ||
| 125 | + taskA->SetVisit(true); | ||
| 126 | + EvictViaSpace(*space); | ||
| 127 | + EXPECT_EQ(Data().taskInfo.count(taskB.get()), 0U); | ||
| 128 | + EXPECT_EQ(Data().taskInfo.count(taskA.get()), 1U); | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +// 未注册的task释放为no-op | ||
| 132 | +TEST_F(AicpuLruCacheUt, ReleaseUnregisteredTaskNoop) | ||
| 133 | +{ | ||
| 134 | + auto task = std::make_unique<AicpuCCTask>("UtLruOp", ge::DEPEND_IN_SHAPE); | ||
| 135 | + task->SetSpace(space.get()); | ||
| 136 | + task->SetVisit(false); | ||
| 137 | + EXPECT_EQ(Data().idleLru.size(), 0U); | ||
| 138 | + EXPECT_EQ(GetCachedTaskNum(space.get()), 0U); | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +// SetVisit(true)不触碰LRU | ||
| 142 | +TEST_F(AicpuLruCacheUt, SetVisitTrueNoLruInteraction) | ||
| 143 | +{ | ||
| 144 | + auto task = MakeRegisteredTask(); | ||
| 145 | + EXPECT_EQ(Data().idleLru.size(), 1U); | ||
| 146 | + task->SetVisit(true); | ||
| 147 | + EXPECT_EQ(task->isVisit_, true); | ||
| 148 | + EXPECT_EQ(Data().idleLru.size(), 1U); | ||
| 149 | + EXPECT_EQ(Data().idleLru.front(), task.get()); | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +// space_为空时SetVisit(false)仅清标记 | ||
| 153 | +TEST_F(AicpuLruCacheUt, SetVisitNullSpaceSafe) | ||
| 154 | +{ | ||
| 155 | + auto task = std::make_unique<AicpuCCTask>("UtLruOp", ge::DEPEND_IN_SHAPE); | ||
| 156 | + task->SetVisit(true); | ||
| 157 | + task->SetVisit(false); | ||
| 158 | + EXPECT_EQ(task->isVisit_, false); | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +// 多space隔离 | ||
| 162 | +TEST_F(AicpuLruCacheUt, MultiSpaceIsolation) | ||
| 163 | +{ | ||
| 164 | + AicpuTaskSpace otherSpace("UtLruOtherOp", ge::DEPEND_IN_SHAPE, false); | ||
| 165 | + auto taskA = MakeRegisteredTask(); | ||
| 166 | + auto taskB = std::make_unique<AicpuCCTask>("UtLruOtherOp", ge::DEPEND_IN_SHAPE); | ||
| 167 | + taskB->SetSpace(&otherSpace); | ||
| 168 | + RegisterCachedTask(&otherSpace, taskB.get(), 0U); | ||
| 169 | + IncCachedTaskNum(&otherSpace); | ||
| 170 | + | ||
| 171 | + taskA->SetVisit(true); | ||
| 172 | + taskB->SetVisit(false); | ||
| 173 | + EXPECT_EQ(GetCachedTaskNum(space.get()), 1U); | ||
| 174 | + EXPECT_EQ(GetCachedTaskNum(&otherSpace), 1U); | ||
| 175 | + | ||
| 176 | + EvictViaSpace(otherSpace); | ||
| 177 | + EXPECT_EQ(GetCachedTaskNum(&otherSpace), 0U); | ||
| 178 | + EXPECT_EQ(GetCachedTaskNum(space.get()), 1U); | ||
| 179 | + EXPECT_EQ(Data().taskInfo.count(taskA.get()), 1U); | ||
| 180 | + ClearSpaceCacheData(&otherSpace); | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | +// Clear清除全部旁路状态;空间清除后SetVisit仅清标记,安全no-op | ||
| 184 | +TEST_F(AicpuLruCacheUt, ClearRemovesAll) | ||
| 185 | +{ | ||
| 186 | + auto task = MakeRegisteredTask(); | ||
| 187 | + task->SetVisit(false); | ||
| 188 | + ClearSpaceCacheData(space.get()); | ||
| 189 | + EXPECT_EQ(GetCachedTaskNum(space.get()), 0U); | ||
| 190 | + task->SetVisit(false); | ||
| 191 | + EXPECT_EQ(task->isVisit_, false); | ||
| 192 | + const std::lock_guard<std::mutex> lk(space->mutex_); | ||
| 193 | + EXPECT_EQ(AicpuEvictOneTask(MakeCtx(*space)), nullptr); | ||
| 194 | +} | ||
| 195 | + | ||
| 196 | +// 重复注册幂等 | ||
| 197 | +TEST_F(AicpuLruCacheUt, RegisterIdempotent) | ||
| 198 | +{ | ||
| 199 | + auto task = std::make_unique<AicpuCCTask>("UtLruOp", ge::DEPEND_IN_SHAPE); | ||
| 200 | + task->SetSpace(space.get()); | ||
| 201 | + RegisterCachedTask(space.get(), task.get(), 0U); | ||
| 202 | + RegisterCachedTask(space.get(), task.get(), 0U); | ||
| 203 | + EXPECT_EQ(Data().taskInfo.size(), 1U); | ||
| 204 | + EXPECT_EQ(Data().idleLru.size(), 1U); | ||
| 205 | +} | ||
| 206 | + | ||
| 207 | +// 并发压力:占用声明与释放(space锁内) vs 淘汰(space锁内扫描),验证锁序与状态一致 | ||
| 208 | +TEST_F(AicpuLruCacheUt, ConcurrentReleaseEvictStress) | ||
| 209 | +{ | ||
| 210 | + constexpr size_t kTaskNum = 32U; | ||
| 211 | + constexpr int kWorkerPairs = 4; | ||
| 212 | + std::vector<std::unique_ptr<AicpuCCTask>> tasks; | ||
| 213 | + for (size_t i = 0U; i < kTaskNum; ++i) { | ||
| 214 | + tasks.push_back(MakeRegisteredTask()); | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + std::atomic<bool> stop{false}; | ||
| 218 | + std::vector<std::thread> pool; | ||
| 219 | + for (int w = 0; w < kWorkerPairs; ++w) { | ||
| 220 | + pool.emplace_back([&stop, &tasks, this]() { | ||
| 221 | + for (size_t i = 0U; !stop.load(std::memory_order_relaxed); ++i) { | ||
| 222 | + AicpuTask* const task = tasks[i % tasks.size()].get(); | ||
| 223 | + { | ||
| 224 | + const std::lock_guard<std::mutex> lk(space->mutex_); | ||
| 225 | + task->SetVisit(true); | ||
| 226 | + ActivateCachedTask(task, space.get()); | ||
| 227 | + } | ||
| 228 | + task->SetVisit(false); | ||
| 229 | + } | ||
| 230 | + }); | ||
| 231 | + pool.emplace_back([&stop, this]() { | ||
| 232 | + while (!stop.load(std::memory_order_relaxed)) { | ||
| 233 | + const std::lock_guard<std::mutex> lk(space->mutex_); // 对齐生产淘汰路径的持锁方式 | ||
| 234 | + (void)AicpuEvictOneTask(MakeCtx(*space)); | ||
| 235 | + } | ||
| 236 | + }); | ||
| 237 | + } | ||
| 238 | + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); | ||
| 239 | + stop.store(true); | ||
| 240 | + for (auto& th : pool) { | ||
| 241 | + th.join(); | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + // 收敛后校验不变式:链长==索引数、计数一致 | ||
| 245 | + const std::lock_guard<std::mutex> lk(space->mutex_); | ||
| 246 | + const std::lock_guard<std::mutex> lk2(gSpaceCacheMutex); | ||
| 247 | + const SpaceCacheData& data = Data(); | ||
| 248 | + EXPECT_EQ(data.idleLru.size(), data.taskInfo.size()); | ||
| 249 | + EXPECT_EQ(data.cachedTaskNum, data.taskInfo.size()); | ||
| 250 | + for (AicpuTask* const t : data.idleLru) { | ||
| 251 | + EXPECT_NE(data.taskInfo.find(t), data.taskInfo.end()); | ||
| 252 | + } | ||
| 253 | +} | ||
| 254 | +} // namespace | ||


🟡 Medium Priority
改动点:
aclnnAicpuFinalize中新增了ClearSpaceCacheData(space)(95 行),与原有的space->Clear()(94 行)构成两步清理。受影响契约:新增的全局淘汰索引
SpaceCacheDataMap()[space](aicpu_lru_cache.h:36-40)里存放的是AicpuTask*裸指针,这些对象的唯一持有者是space->hashMap_;而AicpuTaskSpace::Clear()会清空hashMap_(进而析构 task),这一步按 88-91 行注释是故意不加 space 锁的。失败模式:
Clear()析构的AicpuTask*。此窗口内若有线程在同一 space 上走GetOrCreateTask→AicpuCommitTask(自动加锁后已满)→AicpuEvictOneTask,会在 458 行用taskCache.isIdle(*rit)解引用已释放对象(use-after-free)。AicpuCommitTask里完成hashMap_插入 +IncCachedTaskNum+RegisterCachedTask,随后 95 行把整个SpaceCacheData条目 erase 掉,这些仍活在hashMap_里的 task 就彻底失去淘汰索引、计数被清零,导致cachedTaskNum < kAicpuCacheLimit永远成立,缓存不再受限。 88-91 行注释明确写了 “finalize 及 task init 接口同时调用” 是需要处理的场景,因此该窗口不是纯理论。