* Copyright (c) 2025-2026 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 kernel_bundle_pack.cpp
* \brief Pack side of the kernel bundle: symbol serialization + the .pyptokb pack hook (see header).
*/
#include "machine/runtime/bundle/pack/kernel_bundle_pack.h"
#include <string>
#include <nlohmann/json.hpp>
#include "machine/runtime/bundle/kernel_bundle_format.h"
#include "machine/runtime/bundle/pack/kernel_bundle_packer.h"
#include "interface/function/function.h"
#include "interface/tensor/symbolic_scalar.h"
#include "utils/file_utils.h"
#include "tilefwk/pypto_fwk_log.h"
namespace npu::tile_fwk::bundle {
namespace {
constexpr int kSymbolMetaVersion = 1;
}
std::vector<uint8_t> SerializeWorkspaceSymbols(const DyndevFunctionAttribute& dynAttr)
{
const bool hasAssemble = dynAttr.maxDynamicAssembleOutcastMem.IsValid();
const bool hasCellMatch = dynAttr.maxDynamicCellMatchTableMem.IsValid();
if (!hasAssemble && !hasCellMatch) {
return {};
}
nlohmann::json root = nlohmann::json::object();
root["v"] = kSymbolMetaVersion;
if (hasAssemble) {
root["assembleMem"] = ToJson(dynAttr.maxDynamicAssembleOutcastMem);
}
if (hasCellMatch) {
root["cellMatchMem"] = ToJson(dynAttr.maxDynamicCellMatchTableMem);
}
nlohmann::json syms = nlohmann::json::object();
for (const auto& kv : dynAttr.inputSymbolDict) {
syms[kv.first] = static_cast<int64_t>(kv.second);
}
root["symbols"] = std::move(syms);
if (!dynAttr.dynamicCellMatchLaunchMetaList.empty()) {
nlohmann::json metas = nlohmann::json::array();
for (const auto& lm : dynAttr.dynamicCellMatchLaunchMetaList) {
nlohmann::json m = nlohmann::json::object();
m["slot"] = lm.slotIndex;
m["descOffset"] = static_cast<uint64_t>(lm.descOffset);
m["cellShape"] = lm.cellShape;
nlohmann::json cand = nlohmann::json::array();
for (const auto& dims : lm.candidateRawDims) {
nlohmann::json row = nlohmann::json::array();
for (const auto& expr : dims) {
row.push_back(ToJson(expr));
}
cand.push_back(std::move(row));
}
m["cand"] = std::move(cand);
metas.push_back(std::move(m));
}
root["cellMatchLaunch"] = std::move(metas);
}
const std::string dumped = root.dump();
MACHINE_LOGI("[kernel-bundle] serialized SymbolMeta: %zuB (assemble=%d cellMatch=%d symbols=%zu cmLaunch=%zu)",
dumped.size(), static_cast<int>(hasAssemble), static_cast<int>(hasCellMatch),
dynAttr.inputSymbolDict.size(), dynAttr.dynamicCellMatchLaunchMetaList.size());
return std::vector<uint8_t>(dumped.begin(), dumped.end());
}
void MaybePackKernelBundle(const DyndevFunctionAttribute& dynAttr, const std::vector<uint8_t>& ctrlFlowCache)
{
if (!IsKernelBundleEnabled()) {
return;
}
KernelBundlePacker packer;
packer.SetAicoreKernel(dynAttr.kernelBinary);
packer.SetAicpuSo(ReadFile(GetPyptoLibPath() + "/libtilefwk_backend_server.so"));
packer.SetDevProgram(dynAttr.devProgBinary);
if (!ctrlFlowCache.empty()) {
packer.SetCtrlFlowCache(ctrlFlowCache);
}
packer.SetSymbolMeta(SerializeWorkspaceSymbols(dynAttr));
packer.Pack(KernelBundleOutPath());
}
KernelBundlePackHook& KernelBundlePackHook::Instance()
{
static KernelBundlePackHook instance;
return instance;
}
void KernelBundlePackHook::StashCtrlFlowCache(const DyndevFunctionAttribute* dynAttr, const uint8_t* cache, size_t size)
{
if (!IsKernelBundleEnabled() || dynAttr == nullptr || cache == nullptr || size == 0) {
return;
}
std::lock_guard<std::mutex> lk(mutex_);
stashedFor_ = dynAttr;
stashedCache_.assign(cache, cache + size);
MACHINE_LOGI("[kernel-bundle] stashed ctrl-flow cache (%zuB) for pack at launch", size);
}
void KernelBundlePackHook::MaybePack(const DyndevFunctionAttribute* dynAttr)
{
if (!IsKernelBundleEnabled() || dynAttr == nullptr) {
return;
}
std::vector<uint8_t> cache;
{
std::lock_guard<std::mutex> lk(mutex_);
if (!packed_.insert(dynAttr).second) {
return;
}
if (stashedFor_ == dynAttr) {
cache = std::move(stashedCache_);
stashedCache_.clear();
stashedFor_ = nullptr;
}
}
MaybePackKernelBundle(*dynAttr, cache);
}
}