* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* \file ctrl_flow_cache_manager.cpp
* \brief Implementation of CtrlFlowCacheManager.
*/
#include "machine/runtime/launcher/ctrl_flow_cache_manager.h"
#include <sstream>
#include "tilefwk/pypto_fwk_log.h"
#include "tilefwk/error_code.h"
#include "interface/function/function.h"
#include "machine/runtime/launcher/device_launcher.h"
#include "machine/runtime/runner/kernel_binary.h"
#include "machine/runtime/runner/runtime_utils.h"
#include "machine/utils/dynamic/dev_encode_program.h"
#include "machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h"
namespace {
uint64_t ComputeValueDependHash(const std::vector<npu::tile_fwk::dynamic::DeviceTensorData>& tensors,
const std::vector<size_t>& indices)
{
uint64_t h = 14695981039346656037ull;
for (size_t idx : indices) {
if (idx >= tensors.size()) {
continue;
}
const auto& shape = tensors[idx].GetShape();
for (auto dim : shape) {
h ^= static_cast<uint64_t>(dim);
h *= 1099511628211ull;
}
auto addr = reinterpret_cast<const uint8_t*>(tensors[idx].GetAddr());
int64_t dataSize = tensors[idx].GetDataSize();
if (addr != nullptr && dataSize > 0) {
for (int64_t i = 0; i < dataSize; i++) {
h ^= addr[i];
h *= 1099511628211ull;
}
}
}
return h;
}
}
namespace npu::tile_fwk::dynamic {
CtrlFlowCacheManager& CtrlFlowCacheManager::Instance()
{
static CtrlFlowCacheManager instance;
return instance;
}
uint8_t* CtrlFlowCacheManager::FindOrBuildDevCache(KernelBinary* kernel, std::vector<DeviceTensorData>& tensors,
bool isCaptureMode)
{
if (kernel->DisableHostCtrlFlowCacheBuild()) {
COMPILER_LOGI("Skip host control flow cache build due to disableCtrlFlowCache.");
kernel->SetCtrlFlowCacheReplay(false);
return nullptr;
}
const auto& valueDependIndices = kernel->GetValueDependInputIndices();
if (!kernel->HasValueDepend()) {
auto devCache = kernel->FindCtrlFlowCache(tensors, true);
if (devCache == nullptr) {
AclModeGuard guard(AclMdlRICaptureMode::RELAXED);
devCache = kernel->BuildControlFlowCache(tensors, true);
}
kernel->SetCtrlFlowCacheReplay(devCache != nullptr);
COMPILER_LOGD("find ctrlflow cache: %p", devCache);
return devCache;
}
if (isCaptureMode) {
return nullptr;
}
uint64_t currentHash = ComputeValueDependHash(tensors, valueDependIndices);
uint64_t cachedHash = kernel->GetCachedCtrlFlowHash();
if (cachedHash == currentHash && kernel->GetValueDependDevCache() != nullptr) {
kernel->SetCtrlFlowCacheReplay(true);
kernel->BindValueDependActiveHost();
return kernel->GetValueDependDevCache();
}
kernel->FreeAndClearValueDependCache();
AclModeGuard guard(AclMdlRICaptureMode::RELAXED);
auto devCache = kernel->BuildControlFlowCache(tensors, true, false, currentHash);
kernel->SetCtrlFlowCacheReplay(devCache != nullptr);
return devCache;
}
DevControlFlowCache* CtrlFlowCacheManager::GetHostCtrlFlowCache(KernelBinary* kernel,
std::vector<DeviceTensorData>& tensors,
uint8_t* devCache, CtrlFlowCacheBlob& hostCache)
{
DevControlFlowCache* ctrlCache = kernel->FindHostCtrlFlowCache(tensors, hostCache);
if (ctrlCache == nullptr && devCache != nullptr) {
auto devProg = reinterpret_cast<DevAscendProgram*>(
kernel->GetFunction()->GetDyndevAttribute()->devProgBinary.data());
size_t ctrlCacheSize = devProg->ctrlFlowCacheSize;
CtrlFlowCacheBlob hostCacheVec;
hostCacheVec.resize(ctrlCacheSize);
MACHINE_LOGI("#ctrl.cache.align: site=GetHostCtrlFlowCache.build base=%p size=%zu align64=%lu %s",
static_cast<void*>(hostCacheVec.data()), ctrlCacheSize,
static_cast<unsigned long>(reinterpret_cast<uintptr_t>(hostCacheVec.data()) %
npu::tile_fwk::DUPPED_STITCH_NODE_ALIGN),
reinterpret_cast<uintptr_t>(hostCacheVec.data()) % npu::tile_fwk::DUPPED_STITCH_NODE_ALIGN == 0 ?
"OK" :
"MISALIGNED");
AclModeGuard guard(AclMdlRICaptureMode::RELAXED);
RuntimeMemcpy(hostCacheVec.data(), ctrlCacheSize, devCache, ctrlCacheSize, RtMemcpyKind::DEVICE_TO_HOST);
kernel->AttachHostCtrlFlowCache(tensors, std::move(hostCacheVec));
ctrlCache = kernel->FindHostCtrlFlowCache(tensors, hostCache);
}
if (ctrlCache != nullptr) {
MACHINE_LOGI("#ctrl.cache.align: site=GetHostCtrlFlowCache.copy base=%p size=%zu align64=%lu %s",
static_cast<void*>(hostCache.data()), hostCache.size(),
static_cast<unsigned long>(reinterpret_cast<uintptr_t>(hostCache.data()) %
npu::tile_fwk::DUPPED_STITCH_NODE_ALIGN),
reinterpret_cast<uintptr_t>(hostCache.data()) % npu::tile_fwk::DUPPED_STITCH_NODE_ALIGN == 0 ?
"OK" :
"MISALIGNED");
}
return ctrlCache;
}
}