* 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 kernel_binary.h
* \brief KernelBinary class for managing compiled kernel binary and its runtime resources.
*/
#pragma once
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "interface/machine/device/tilefwk/aicpu_common.h"
#include "interface/function/function.h"
#include "machine/runtime/launcher/ctrl_flow_cache_manager.h"
#include "machine/runtime/launcher/device_launcher_types.h"
#include "machine/runtime/runner/load_aicpu_op.h"
#include "machine/utils/dynamic/dev_encode_tensor.h"
#include "machine/utils/machine_ws_intf.h"
#include "adapter/api/runtime_define.h"
#include "adapter/api/acl_define.h"
#include "machine/runtime/runner/host_prof.h"
#include "machine/runtime/runner/fed_prof_manager.h"
namespace npu::tile_fwk::dynamic {
namespace detail {
bool IsPyptoAicpuKernelConfigured();
}
class KernelBinary {
public:
KernelBinary(std::shared_ptr<Function> func, std::vector<std::shared_ptr<Function>> pinnedGraph = {});
~KernelBinary();
static void SetAiCpuSoOverride(std::vector<uint8_t> soBytes);
static void InitMetaData(DeviceArgs& devArgs);
static bool GetEnableDumpDevPref();
ToSubMachineConfig& GetMachineConfig();
uint8_t* FindCtrlFlowCache(std::vector<std::vector<int64_t>>& inputs, bool isOriginShape);
uint8_t* FindCtrlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape);
uint8_t* BuildControlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape, bool storeToCache = true,
uint64_t valueDependHash = 0);
int64_t GetWorkspaceSize(const std::vector<DeviceTensorData>& tensors);
std::pair<AiCpuArgs*, int64_t> BuildKernelArgs(const std::vector<DeviceTensorData>& tensors);
* Uses activeHostCtrlFlowCache_ bound by FindOrBuildDevCache (no second hash lookup). */
void MaterializeIoAddressArenaForAicore(AiCpuArgs* aicpuArgs);
bool CheckArgs(const std::vector<DeviceTensorData>& tensors) const;
void* GetKernelBin();
auto& GetArgTypes() { return argTypes; }
Function* GetFunction();
const std::string& GetKernelname() const;
bool DisableHostCtrlFlowCacheBuild() const;
bool HasValueDepend() const { return devProg != nullptr && devProg->hasValueDepend != 0; }
const std::vector<size_t>& GetValueDependInputIndices() const;
uint64_t GetCachedCtrlFlowHash() const { return valueDependCache_.hash; }
uint8_t* GetValueDependDevCache() const { return valueDependCache_.devCache; }
void FreeAndClearValueDependCache();
void BindValueDependActiveHost();
uint64_t GetMaxDynamicAssembleOutcastMem() const;
uint64_t GetMaxDynamicCellMatchTableMem() const;
uint64_t GetRuntimeDynamicCellMatchAddr() const;
uint64_t GetRuntimeDynamicCellMatchCapacity() const;
void SetCtrlFlowCacheReplay(bool replay)
{
ctrlFlowCacheReplay_ = replay;
if (!replay) {
activeHostCtrlFlowCache_ = nullptr;
}
}
bool IsCtrlFlowCacheReplay() const { return ctrlFlowCacheReplay_; }
DevControlFlowCache* GetActiveHostCtrlFlowCache() const { return activeHostCtrlFlowCache_; }
DevControlFlowCache* FindHostCtrlFlowCache(const std::vector<DeviceTensorData>& tensors,
CtrlFlowCacheBlob& hostCacheOut);
void AttachHostCtrlFlowCache(const std::vector<DeviceTensorData>& tensors, CtrlFlowCacheBlob&& hostCache);
bool BeginRingLaunch(uintptr_t epoch, bool isCapture, int64_t& outSequence);
AclRtEvent RingEvent(bool isCapture, int64_t idx) const
{
return isCapture ? graphEvents_[idx] : eagerEvents_[idx];
}
static constexpr int64_t kRingPingPongCount = 2;
void SetSyncMode(uint8_t syncModel);
uint8_t GetSyncMode();
void ResetRuntimeDynamicCellMatchPool(bool useHostMirror) const;
void PatchHostDynamicCellMatchAddr(DevAscendProgram* hostProg);
AicpuLaunchDesc& GetAicpuLaunchDesc() { return aicpuLaunchDesc_; }
RtArgsEx& GetRtAicoreArgs() { return rtAicoreArgs_; }
RtTaskCfgInfo& GetRtTaskCfg() { return rtTaskCfg_; }
private:
void InitCachedArgs();
void InitLaunchArgs();
void RefreshRuntimeDynamicCellMatchMeta(uint64_t needBytes);
void BeginRingEpoch(uintptr_t epoch, bool isCapture);
bool EnsureRingEventsCreated(bool isCapture);
static void InitDeviceArgs();
static void InitAicpuOp(int64_t* devArgsAddr);
static int InitDeviceArgs(DeviceArgs& args);
static int InitDeviceArgsCore(DeviceArgs& args);
static void GetAicoreRegs(const ArchInfo archInfo, std::vector<int64_t>& regs, std::vector<int64_t>& regsPmu);
static void InitAiCpuSoBin(DeviceArgs& devArgs);
std::shared_ptr<Function> dynFunc;
std::vector<std::shared_ptr<Function>> pinnedGraph_;
DyndevFunctionAttribute* dynAttr{nullptr};
DevAscendProgram* devProg{nullptr};
void* kernelBin{nullptr};
int64_t workspaceSize{0};
std::vector<ControlFlowCache> inferShapeCaches;
std::vector<ControlFlowCache> originShapeCaches;
struct ValueDependCtrlFlowCache {
uint64_t hash{0};
uint8_t* devCache{nullptr};
CtrlFlowCacheBlob hostCache;
};
DevControlFlowCache* activeHostCtrlFlowCache_{nullptr};
ValueDependCtrlFlowCache valueDependCache_;
bool ctrlFlowCacheReplay_{false};
int64_t ringSequence_{0};
uintptr_t ringEpoch_{0};
bool ringEpochInited_{false};
AclRtEvent eagerEvents_[kRingPingPongCount]{};
AclRtEvent graphEvents_[kRingPingPongCount]{};
bool eagerEventsCreated_{false};
bool graphEventsCreated_{false};
std::vector<int64_t> aicpuArgBuf;
uint64_t l2Offset{0};
std::vector<DeviceTensorData> argTypes;
std::vector<DevDynamicCellMatchStridePatch> dynamicCellMatchDescPatches_;
uint64_t lastPreparedDynamicCellMatchBytes_{0};
uint64_t runtimeDynamicCellMatchAddr_{0};
uint64_t runtimeDynamicCellMatchHostAddr_{0};
uint64_t runtimeDynamicCellMatchCapacity_{0};
bool runtimeDynamicCellMatchOwned_{false};
bool runtimeDynamicCellMatchHostOwned_{false};
std::string kernelName_;
ToSubMachineConfig toSubMachineConfig_;
uint8_t scheSyncModel_{0};
AicpuLaunchDesc aicpuLaunchDesc_;
RtArgsEx rtAicoreArgs_;
RtTaskCfgInfo rtTaskCfg_;
std::vector<uint8_t> aicoreArgBuf_;
RtHostInputInfo hostInfo_{};
RtHostInputInfo aicoreHostInfo_{};
static std::vector<size_t> emptyIndices_;
};
}