Merged
refactor(machine): Remove DeviceRunner and redistribute init flow #5426
refactor(machine): Remove DeviceRunner and redistribute init flow #5426
Merged
wan_jianfenAdd on Aug 13
Total: 19 files changed+364-475
@@ -458,7 +458,6 @@ if (ENABLE_FEATURE_PYTHON_FRONT_END)
458 )458 )
459 install(FILES459 install(FILES
460 framework/src/machine/runtime/runner/device_perf.h460 framework/src/machine/runtime/runner/device_perf.h
461- framework/src/machine/runtime/runner/device_runner.h
462 framework/src/machine/runtime/runner/load_aicpu_op.h461 framework/src/machine/runtime/runner/load_aicpu_op.h
463 framework/src/machine/runtime/runner/host_prof.h462 framework/src/machine/runtime/runner/host_prof.h
464 framework/src/machine/runtime/runner/runtime_agent.h463 framework/src/machine/runtime/runner/runtime_agent.h
@@ -30,6 +30,7 @@
30#include "machine/runtime/context/stream_context.h"30#include "machine/runtime/context/stream_context.h"
31#include "machine/runtime/context/device_launcher_context.h"31#include "machine/runtime/context/device_launcher_context.h"
32#include "machine/runtime/runner/runtime_utils.h"32#include "machine/runtime/runner/runtime_utils.h"
33+#include "machine/runtime/runner/host_prof.h"
33#include "machine/runtime/launcher/device_launcher.h"34#include "machine/runtime/launcher/device_launcher.h"
34#include "machine/runtime/launcher/cell_match_dynamic.h"35#include "machine/runtime/launcher/cell_match_dynamic.h"
35 36 
@@ -41,7 +42,6 @@ int BundleLaunchAicpu(RtAicpuArgsEx& rtArgs, DevAscendProgram* devProg)
41{42{
42 auto ctrlStream = GetStreamContext().GetCtrlStream();43 auto ctrlStream = GetStreamContext().GetCtrlStream();
43 auto schedStream = GetStreamContext().GetScheStream();44 auto schedStream = GetStreamContext().GetScheStream();
44- auto& devRunner = DeviceRunner::Get();
45 int ret = 0;45 int ret = 0;
46 auto args = (AiCpuArgs*)rtArgs.args;46 auto args = (AiCpuArgs*)rtArgs.args;
47 const int nrAicpu = static_cast<int>(devProg->devArgs.nrAicpu);47 const int nrAicpu = static_cast<int>(devProg->devArgs.nrAicpu);
@@ -56,7 +56,7 @@ int BundleLaunchAicpu(RtAicpuArgsEx& rtArgs, DevAscendProgram* devProg)
56 ret = RuntimeAicpuKernelLaunchExWithArgs(static_cast<uint32_t>(npu::tile_fwk::RtKernelType::AICPU_KFC),56 ret = RuntimeAicpuKernelLaunchExWithArgs(static_cast<uint32_t>(npu::tile_fwk::RtKernelType::AICPU_KFC),
57 "AST_DYN_AICPU", 1, &rtArgs, nullptr, ctrlStream,57 "AST_DYN_AICPU", 1, &rtArgs, nullptr, ctrlStream,
58 RT_KERNEL_USE_SPECIAL_TIMEOUT);58 RT_KERNEL_USE_SPECIAL_TIMEOUT);
59- devRunner.ReportHostProfInfo(ctrlStream, startTime, 1, MSPF_GE_TASK_TYPE_AI_CPU, false);59+ HostProf::GetInstance().ReportHostProfInfo(ctrlStream, startTime, 1, MSPF_GE_TASK_TYPE_AI_CPU, false);
60 if (ret != RT_SUCCESS) {60 if (ret != RT_SUCCESS) {
61 return ret;61 return ret;
62 }62 }
@@ -66,7 +66,7 @@ int BundleLaunchAicpu(RtAicpuArgsEx& rtArgs, DevAscendProgram* devProg)
66 ret = RuntimeAicpuKernelLaunchExWithArgs(static_cast<uint32_t>(npu::tile_fwk::RtKernelType::AICPU_KFC),66 ret = RuntimeAicpuKernelLaunchExWithArgs(static_cast<uint32_t>(npu::tile_fwk::RtKernelType::AICPU_KFC),
67 "AST_DYN_AICPU", nrAicpu, &rtArgs, nullptr, schedStream,67 "AST_DYN_AICPU", nrAicpu, &rtArgs, nullptr, schedStream,
68 RT_KERNEL_USE_SPECIAL_TIMEOUT);68 RT_KERNEL_USE_SPECIAL_TIMEOUT);
69- devRunner.ReportHostProfInfo(schedStream, startTime, scheCpuNum, MSPF_GE_TASK_TYPE_AI_CPU, false);69+ HostProf::GetInstance().ReportHostProfInfo(schedStream, startTime, scheCpuNum, MSPF_GE_TASK_TYPE_AI_CPU, false);
70 return ret;70 return ret;
71}71}
72 72 
@@ -74,7 +74,6 @@ int BundleLaunchAicpu(RtAicpuArgsEx& rtArgs, DevAscendProgram* devProg)
74int BundleLaunchAicore(AclRtStream aicoreStream, void* kernel, RtArgsEx& rtArgs, RtTaskCfgInfo& rtTaskCfg,74int BundleLaunchAicore(AclRtStream aicoreStream, void* kernel, RtArgsEx& rtArgs, RtTaskCfgInfo& rtTaskCfg,
75 DevAscendProgram* devProg)75 DevAscendProgram* devProg)
76{76{
77- auto& devRunner = DeviceRunner::Get();
78 auto tilingKey = OpInfoManager::GetInstance().GetOpTilingKey();77 auto tilingKey = OpInfoManager::GetInstance().GetOpTilingKey();
79 int blockDim = static_cast<int>(devProg->ctrlBlockDim);78 int blockDim = static_cast<int>(devProg->ctrlBlockDim);
80 if (blockDim == 0) {79 if (blockDim == 0) {
@@ -82,7 +81,7 @@ int BundleLaunchAicore(AclRtStream aicoreStream, void* kernel, RtArgsEx& rtArgs,
82 }81 }
83 auto startTime = MspfSysCycleTime();82 auto startTime = MspfSysCycleTime();
84 auto ret = RuntimeKernelLaunchWithHandleV2(kernel, tilingKey, blockDim, &rtArgs, nullptr, aicoreStream, &rtTaskCfg);83 auto ret = RuntimeKernelLaunchWithHandleV2(kernel, tilingKey, blockDim, &rtArgs, nullptr, aicoreStream, &rtTaskCfg);
85- devRunner.ReportHostProfInfo(aicoreStream, startTime, blockDim, MSPF_GE_TASK_TYPE_MIX_AIC, true);84+ HostProf::GetInstance().ReportHostProfInfo(aicoreStream, startTime, blockDim, MSPF_GE_TASK_TYPE_MIX_AIC, true);
86 return ret;85 return ret;
87}86}
88} // namespace87} // namespace
@@ -216,7 +215,7 @@ int LaunchBundleKernelOnce(const std::vector<uint8_t>& devProgBinary, void* binH
216 }215 }
217 216 
218 if (streamSynchronize) {217 if (streamSynchronize) {
219- rc = DeviceRunner::Get().DynamicLaunchSynchronize(GetContextScheStream(), GetContextCtrlStream(), aicoreStream);218+ rc = DeviceLauncher::DynamicLaunchSynchronize(GetContextScheStream(), GetContextCtrlStream(), aicoreStream);
220 }219 }
221 MACHINE_LOGI("finish Kernel Launch (bundle).");220 MACHINE_LOGI("finish Kernel Launch (bundle).");
222 return rc;221 return rc;
@@ -11,7 +11,7 @@
11/*!11/*!
12 * \file kernel_bundle_registry.h12 * \file kernel_bundle_registry.h
13 * \brief Singleton that owns loaded bundles and their registered kernel handles.13 * \brief Singleton that owns loaded bundles and their registered kernel handles.
14- * The AICPU .so is copied to device by DeviceRunner::Init() (InitAiCpuSoBin), not here.14+ * The AICPU .so is copied to device by KernelBinary::InitDeviceArgs() (InitAiCpuSoBin), not here.
15 */15 */
16 16 
17#pragma once17#pragma once
@@ -23,7 +23,7 @@
23#include "machine/runtime/bundle/kernel_bundle_launch.h"23#include "machine/runtime/bundle/kernel_bundle_launch.h"
24#include "machine/runtime/launcher/device_launcher.h"24#include "machine/runtime/launcher/device_launcher.h"
25#include "machine/runtime/launcher/device_launcher_binding.h"25#include "machine/runtime/launcher/device_launcher_binding.h"
26-#include "machine/runtime/runner/device_runner.h"26+#include "machine/runtime/runner/kernel_binary.h"
27#include "tilefwk/data_type.h"27#include "tilefwk/data_type.h"
28#include "tilefwk/error_code.h"28#include "tilefwk/error_code.h"
29#include "tilefwk/platform.h"29#include "tilefwk/platform.h"
@@ -52,7 +52,7 @@ std::vector<DeviceTensorData> ToDeviceTensors(const PyptoTensorDesc* descs, uint
52}52}
53 53 
54// Post-load prep shared by the path and in-memory entries: seed the AiCpu backend .so from the bundle's AICPU_SO54// Post-load prep shared by the path and in-memory entries: seed the AiCpu backend .so from the bundle's AICPU_SO
55-// segment before any DeviceRunner::Init(), arch-check, and register the aicore .o. Stays pure host (no Get()).55+// segment before any KernelBinary::InitDeviceArgs(), arch-check, and register the aicore .o. Stays pure host.
56std::shared_ptr<LoadedBundle> PrepareBundle(std::shared_ptr<LoadedBundle> bundle)56std::shared_ptr<LoadedBundle> PrepareBundle(std::shared_ptr<LoadedBundle> bundle)
57{57{
58 if (bundle == nullptr) {58 if (bundle == nullptr) {
@@ -60,7 +60,7 @@ std::shared_ptr<LoadedBundle> PrepareBundle(std::shared_ptr<LoadedBundle> bundle
60 }60 }
61 if (!bundle->aicpuSo.empty()) {61 if (!bundle->aicpuSo.empty()) {
62 // Harmless to set repeatedly; only the first Get() (inside the first Launch) consumes it.62 // Harmless to set repeatedly; only the first Get() (inside the first Launch) consumes it.
63- DeviceRunner::SetAiCpuSoOverride(bundle->aicpuSo);63+ npu::tile_fwk::dynamic::KernelBinary::SetAiCpuSoOverride(bundle->aicpuSo);
64 } else {64 } else {
65 MACHINE_LOGW("[kernel-bundle] bundle has no AICPU_SO segment; will fall back to on-disk backend .so");65 MACHINE_LOGW("[kernel-bundle] bundle has no AICPU_SO segment; will fall back to on-disk backend .so");
66 }66 }
@@ -21,22 +21,28 @@
21#include "adapter/api/msprof_api.h"21#include "adapter/api/msprof_api.h"
22#include "adapter/api/acl_api.h"22#include "adapter/api/acl_api.h"
23#include "adapter/api/adump_api.h"23#include "adapter/api/adump_api.h"
24+#include "adapter/api/runtime_api.h"
24#include "interface/utils/op_info_manager.h"25#include "interface/utils/op_info_manager.h"
26+#include "interface/utils/common.h"
27+#include "interface/configs/config_manager.h"
25#include "machine/runtime/memory_utils/eslmodel_memory_utils.h"28#include "machine/runtime/memory_utils/eslmodel_memory_utils.h"
26#include "interface/configs/config_manager_ng.h"29#include "interface/configs/config_manager_ng.h"
27#include "machine/runtime/context/stream_context.h"30#include "machine/runtime/context/stream_context.h"
28#include "machine/runtime/context/device_launcher_context.h"31#include "machine/runtime/context/device_launcher_context.h"
29#include "machine/runtime/runner/runtime_utils.h"32#include "machine/runtime/runner/runtime_utils.h"
33+#include "machine/runtime/runner/device_dfx.h"
34+#include "machine/runtime/runner/kernel_binary.h"
30#include "machine/runtime/launcher/device_launcher_driver_gate.h"35#include "machine/runtime/launcher/device_launcher_driver_gate.h"
31#include "machine/runtime/launcher/emulation_launcher.h"36#include "machine/runtime/launcher/emulation_launcher.h"
32#include "machine/runtime/launcher/aicore_model_launcher.h"37#include "machine/runtime/launcher/aicore_model_launcher.h"
33#include "machine/runtime/launcher/ctrl_flow_cache_manager.h"38#include "machine/runtime/launcher/ctrl_flow_cache_manager.h"
34#include "machine/runtime/bundle/pack/kernel_bundle_pack.h"39#include "machine/runtime/bundle/pack/kernel_bundle_pack.h"
35-#include "machine/runtime/runner/kernel_binary.h"
36#include "machine/host/perf_analysis.h"40#include "machine/host/perf_analysis.h"
37#include "interface/program/program.h"41#include "interface/program/program.h"
38 42 
39namespace npu::tile_fwk::dynamic {43namespace npu::tile_fwk::dynamic {
44+ 
45+void DeviceLauncher::InitDevArgs(DeviceArgs& devArgs) { KernelBinary::InitMetaData(devArgs); }
atomgit-bot

🟠 High Priority

变更行:device_launcher.cpp:45 新增 void DeviceLauncher::InitDevArgs(DeviceArgs& devArgs) { KernelBinary::InitMetaData(devArgs); },同时 device_launcher.h:236 将原来的 DeviceRunner::Get().InitMetaData(devProg->devArgs) 改为 InitDevArgs(devProg->devArgs)。

受影响行为/契约:重构前 DeviceRunner::Get()(单例)在 InitMetaData 之前会触发设备初始化;重构后 KernelBinary::InitMetaData(kernel_binary.cpp:581-594)直接拷贝 g_deviceArgs 的字段,而 g_deviceArgs 只在 KernelBinary::InitDeviceArgs() 内的 std::call_once(g_initOnce, ...)(kernel_binary.cpp:559-579)中被填充,该 call_once 目前只在 KernelBinary 构造函数(kernel_binary.cpp:79)里被调用。

失败模式:bundle 离线启动路径 LaunchBundleKernelOnce(kernel_bundle_launch.cpp:129 调用 DeviceLauncher::FillDeviceKernelArgs → DeviceInitTilingData → PrepareDevProgArgs → InitDevArgs)使用的是 DeviceMemoryUtils(IsDevice()==true),但整条路径从不构造 KernelBinary,因此 InitMetaData 执行时 g_deviceArgs 仍是零初始化的静态变量。结果是 devProg->devArgs.nrAic/nrAiv 被覆盖为 0,sharedBuffer/coreRegAddr/corePmuAddr/corePmuRegAddr/taskWastTime/pmuEventAddr/aicpuPerfAddr/devDfxArgAddr/runtimeDataRingBufferAddr 全部被写成 0,得到零核数、空设备地址的设备程序,下发到设备端后会导致空指针/越界访问或静默不执行。同理 KernelBinary::GetEnableDumpDevPref()(被 SetDevPerfAddr 在 device_launcher.cpp:369 使用)在 bundle 路径返回 false,静默关闭 debug/perf 分支。这是删除 DeviceRunner 后未补上初始化守卫导致的回归。

likedislike
Incorrect?
40bool DeviceLauncher::inited_ = false;46bool DeviceLauncher::inited_ = false;
41std::vector<uint8_t> DeviceLauncher::tensorInfo_(kDefaultTensorinfoSize);47std::vector<uint8_t> DeviceLauncher::tensorInfo_(kDefaultTensorinfoSize);
42std::unordered_map<Function*, DeviceLauncher::DeviceRunCacheInfo> DeviceLauncher::cacheInfoDict_;48std::unordered_map<Function*, DeviceLauncher::DeviceRunCacheInfo> DeviceLauncher::cacheInfoDict_;
@@ -75,16 +81,36 @@ int DeviceLauncher::RunWithProfile(RtStream aicoreStream, RtStream aicpuStream,
75 "tilefwk_L1_prof_data may be empty.");81 "tilefwk_L1_prof_data may be empty.");
76 return 0;82 return 0;
77 }83 }
78- int rc = DeviceRunner::Get().DynamicLaunchSynchronize(aicpuStream, nullptr, aicoreStream);84+ int rc = DynamicLaunchSynchronize(aicpuStream, nullptr, aicoreStream);
79 if (rc < 0) {85 if (rc < 0) {
80 return rc;86 return rc;
81 }87 }
82- DeviceRunner::Get().SyncProfData(true);88+ DevicePerf::GetInstance().SyncProfData(true);
83- DeviceRunner::Get().ResetPerData();89+ DevicePerf::GetInstance().ResetPerData();
84 }90 }
85 return 0;91 return 0;
86}92}
87 93 
94+int DeviceLauncher::DynamicLaunchSynchronize(RtStream schedStream, RtStream ctrlStream, RtStream aicoreStream)
95+{
96+ int rcAicore = RuntimeStreamSynchronize(aicoreStream);
97+ int rcAicpu = RuntimeStreamSynchronize(schedStream);
98+ int rcCtrl = 0;
99+ if (ctrlStream != nullptr) {
100+ rcCtrl = RuntimeStreamSynchronize(ctrlStream);
101+ }
102+ if (IsPtoDataDumpEnabled()) {
103+ MACHINE_LOGD("DataDumpServerUnInit is called \n");
104+ (void)AdxDumpDataDumpServerUnInit();
105+ }
106+ int retAicorePrint = DeviceDfx::GetInstance().DumpAicoreLog();
107+ if (rcAicore != 0 || rcAicpu != 0 || rcCtrl != 0 || retAicorePrint != 0) {
108+ MACHINE_LOGW("sync stream failed aicpu:%d aicore:%d ctrl cpu:%d, aicorePrint: %d", rcAicpu, rcAicore, rcCtrl,
109+ retAicorePrint);
110+ }
111+ return rcAicore + rcAicpu + rcCtrl;
112+}
113+ 
88int DeviceLauncher::DeviceLaunchOnceWithDeviceTensorData(114int DeviceLauncher::DeviceLaunchOnceWithDeviceTensorData(
89 Function* function, const std::vector<DeviceTensorData>& inputList, const std::vector<DeviceTensorData>& outputList,115 Function* function, const std::vector<DeviceTensorData>& inputList, const std::vector<DeviceTensorData>& outputList,
90 RtStream aicoreStream, bool streamSynchronize, [[maybe_unused]] CachedOperator* cachedOperator,116 RtStream aicoreStream, bool streamSynchronize, [[maybe_unused]] CachedOperator* cachedOperator,
@@ -106,7 +132,7 @@ int DeviceLauncher::DeviceLaunchOnceWithDeviceTensorData(
106 }132 }
107 HOST_PERF_TRACE(TracePhase::RunDeviceSetCapture);133 HOST_PERF_TRACE(TracePhase::RunDeviceSetCapture);
108 134 
109- DeviceRunner::Get().SetHostProfFunction(function);135+ HostProf::GetInstance().SetProfFunction(function);
110 rc = AclInit(nullptr);136 rc = AclInit(nullptr);
111 if (rc != 0 && rc != ACLRT_ERROR_REPEAT_INITIALIZE) {137 if (rc != 0 && rc != ACLRT_ERROR_REPEAT_INITIALIZE) {
112 return rc;138 return rc;
@@ -149,7 +175,7 @@ int DeviceLauncher::DeviceLaunchOnceWithDeviceTensorData(
149 return rc;175 return rc;
150 }176 }
151 if (streamSynchronize) {177 if (streamSynchronize) {
152- rc = DeviceRunner::Get().DynamicLaunchSynchronize(launchInfo.schedStream, launchInfo.ctrlStream, aicoreStream);178+ rc = DynamicLaunchSynchronize(launchInfo.schedStream, launchInfo.ctrlStream, aicoreStream);
153 ASSERT(DevCommonErr::PARAM_CHECK_FAILED, DevMemoryPool::Instance().CheckAllSentinels());179 ASSERT(DevCommonErr::PARAM_CHECK_FAILED, DevMemoryPool::Instance().CheckAllSentinels());
154 }180 }
155 MACHINE_LOGI("finish Kernel Launch.");181 MACHINE_LOGI("finish Kernel Launch.");
@@ -161,7 +187,7 @@ int DeviceLauncher::DeviceLaunchOnceWithDeviceTensorData(
161 187 
162int DeviceLauncher::DeviceSynchronize(RtStream aicpuStream, RtStream aicoreStream)188int DeviceLauncher::DeviceSynchronize(RtStream aicpuStream, RtStream aicoreStream)
163{189{
164- int rc = DeviceRunner::Get().DynamicLaunchSynchronize(aicpuStream, nullptr, aicoreStream);190+ int rc = DynamicLaunchSynchronize(aicpuStream, nullptr, aicoreStream);
165 return rc;191 return rc;
166}192}
167 193 
@@ -337,12 +363,11 @@ bool DeviceLauncher::IsCaptureMode() { return DeviceLauncherContext::Get().IsCap
337 363 
338void DeviceLauncher::SetDevPerfAddr([[maybe_unused]] const bool debugEnable, [[maybe_unused]] const bool isCaptureMode)364void DeviceLauncher::SetDevPerfAddr([[maybe_unused]] const bool debugEnable, [[maybe_unused]] const bool isCaptureMode)
339{365{
340- auto& devRunner = DeviceRunner::Get();366+ if (debugEnable || KernelBinary::GetEnableDumpDevPref() || HostProf::GetInstance().GetHostProfType() == 1) {
341- if (debugEnable || devRunner.GetEnableDumpDevPref() || devRunner.GetHostProfType() == 1) {
342 if (isCaptureMode) {367 if (isCaptureMode) {
343 ExchangeCaptureModeRelax();368 ExchangeCaptureModeRelax();
344 }369 }
345- devRunner.SetDebugEnable();370+ DevicePerf::GetInstance().SetDebugEnable();
346 if (isCaptureMode) {371 if (isCaptureMode) {
347 ExchangeCaptureModeGlobal();372 ExchangeCaptureModeGlobal();
348 }373 }
@@ -394,8 +419,7 @@ int DeviceLauncher::LaunchAicpuKernel(AicpuLaunchDesc& launchDesc, [[maybe_unuse
394{419{
395 auto ctrlStream = GetStreamContext().GetCtrlStream();420 auto ctrlStream = GetStreamContext().GetCtrlStream();
396 auto schedStream = GetStreamContext().GetScheStream();421 auto schedStream = GetStreamContext().GetScheStream();
397- auto& devRunner = DeviceRunner::Get();422+ HostProf::GetInstance().SetProfFunction(function, tensors);
398- devRunner.SetHostProfFunction(function, tensors);
399 int ret = 0;423 int ret = 0;
400 auto args = static_cast<AiCpuArgs*>(launchDesc.args);424 auto args = static_cast<AiCpuArgs*>(launchDesc.args);
401 const int nrAicpu = static_cast<int>(DeviceLauncher::GetDevProg(function)->devArgs.nrAicpu);425 const int nrAicpu = static_cast<int>(DeviceLauncher::GetDevProg(function)->devArgs.nrAicpu);
@@ -411,7 +435,7 @@ int DeviceLauncher::LaunchAicpuKernel(AicpuLaunchDesc& launchDesc, [[maybe_unuse
411 launchDesc.stream = ctrlStream;435 launchDesc.stream = ctrlStream;
412 launchDesc.blockDim = 1U;436 launchDesc.blockDim = 1U;
413 ret = LoadAicpuOp::GetInstance().LaunchBuiltInOpWithHostArgs(launchDesc, "PyptoRun");437 ret = LoadAicpuOp::GetInstance().LaunchBuiltInOpWithHostArgs(launchDesc, "PyptoRun");
414- devRunner.ReportHostProfInfo(ctrlStream, startTime, 1, MSPF_GE_TASK_TYPE_AI_CPU, false);438+ HostProf::GetInstance().ReportHostProfInfo(ctrlStream, startTime, 1, MSPF_GE_TASK_TYPE_AI_CPU, false);
415 if (ret != RT_SUCCESS) {439 if (ret != RT_SUCCESS) {
416 return ret;440 return ret;
417 }441 }
@@ -421,14 +445,13 @@ int DeviceLauncher::LaunchAicpuKernel(AicpuLaunchDesc& launchDesc, [[maybe_unuse
421 launchDesc.stream = schedStream;445 launchDesc.stream = schedStream;
422 launchDesc.blockDim = static_cast<uint32_t>(nrAicpu);446 launchDesc.blockDim = static_cast<uint32_t>(nrAicpu);
423 ret = LoadAicpuOp::GetInstance().LaunchBuiltInOpWithHostArgs(launchDesc, "PyptoRun");447 ret = LoadAicpuOp::GetInstance().LaunchBuiltInOpWithHostArgs(launchDesc, "PyptoRun");
424- devRunner.ReportHostProfInfo(schedStream, startTime, scheCpuNum, MSPF_GE_TASK_TYPE_AI_CPU, false);448+ HostProf::GetInstance().ReportHostProfInfo(schedStream, startTime, scheCpuNum, MSPF_GE_TASK_TYPE_AI_CPU, false);
425 return ret;449 return ret;
426}450}
427 451 
428int DeviceLauncher::LaunchAicoreKernel(AclRtStream aicoreStream, void* kernel, RtArgsEx& rtArgs,452int DeviceLauncher::LaunchAicoreKernel(AclRtStream aicoreStream, void* kernel, RtArgsEx& rtArgs,
429 RtTaskCfgInfo& rtTaskCfg, bool debugEnable, [[maybe_unused]] Function* function)453 RtTaskCfgInfo& rtTaskCfg, bool debugEnable, [[maybe_unused]] Function* function)
430{454{
431- auto& devRunner = DeviceRunner::Get();
432 auto tilingKey = OpInfoManager::GetInstance().GetOpTilingKey();455 auto tilingKey = OpInfoManager::GetInstance().GetOpTilingKey();
433 int blockDim = static_cast<int>(DeviceLauncher::GetDevProg(function)->ctrlBlockDim);456 int blockDim = static_cast<int>(DeviceLauncher::GetDevProg(function)->ctrlBlockDim);
434 if (blockDim == 0) {457 if (blockDim == 0) {
@@ -436,7 +459,7 @@ int DeviceLauncher::LaunchAicoreKernel(AclRtStream aicoreStream, void* kernel, R
436 }459 }
437 auto startTime = MspfSysCycleTime();460 auto startTime = MspfSysCycleTime();
438 auto ret = RuntimeKernelLaunchWithHandleV2(kernel, tilingKey, blockDim, &rtArgs, nullptr, aicoreStream, &rtTaskCfg);461 auto ret = RuntimeKernelLaunchWithHandleV2(kernel, tilingKey, blockDim, &rtArgs, nullptr, aicoreStream, &rtTaskCfg);
439- devRunner.ReportHostProfInfo(aicoreStream, startTime, blockDim, MSPF_GE_TASK_TYPE_MIX_AIC, true);462+ HostProf::GetInstance().ReportHostProfInfo(aicoreStream, startTime, blockDim, MSPF_GE_TASK_TYPE_MIX_AIC, true);
440 if (debugEnable || !IsCaptureMode() || IsPtoDataDumpEnabled()) {463 if (debugEnable || !IsCaptureMode() || IsPtoDataDumpEnabled()) {
441 auto scheStream = GetStreamContext().GetScheStream();464 auto scheStream = GetStreamContext().GetScheStream();
442 int rc = DeviceSynchronize(scheStream, aicoreStream);465 int rc = DeviceSynchronize(scheStream, aicoreStream);
@@ -446,7 +469,7 @@ int DeviceLauncher::LaunchAicoreKernel(AclRtStream aicoreStream, void* kernel, R
446 }469 }
447 }470 }
448 if (debugEnable) {471 if (debugEnable) {
449- devRunner.SyncProfData(debugEnable);472+ DevicePerf::GetInstance().SyncProfData(debugEnable);
450 ASSERT(DevCommonErr::PARAM_CHECK_FAILED, DevMemoryPool::Instance().CheckAllSentinels());473 ASSERT(DevCommonErr::PARAM_CHECK_FAILED, DevMemoryPool::Instance().CheckAllSentinels());
451 }474 }
452 if (IsPtoDataDumpEnabled()) {475 if (IsPtoDataDumpEnabled()) {
@@ -33,7 +33,8 @@
33#include "machine/runtime/runner/runtime_agent.h"33#include "machine/runtime/runner/runtime_agent.h"
34#include "machine/runtime/memory_utils/device_memory_utils.h"34#include "machine/runtime/memory_utils/device_memory_utils.h"
35#include "machine/runtime/distributed/distributed_context.h"35#include "machine/runtime/distributed/distributed_context.h"
36-#include "machine/runtime/runner/device_runner.h"36+#include "machine/runtime/runner/host_prof.h"
37+#include "machine/runtime/runner/device_perf.h"
37#include "machine/runtime/runner/load_aicpu_op.h"38#include "machine/runtime/runner/load_aicpu_op.h"
38#include "machine/runtime/launcher/device_launcher_types.h"39#include "machine/runtime/launcher/device_launcher_types.h"
39#include "machine/runtime/launcher/launcher_router.h"40#include "machine/runtime/launcher/launcher_router.h"
@@ -83,6 +84,8 @@ public:
83 * format and below 25.5; ACL failure, empty string, or non-official format skips gate with MACHINE_LOGW. */84 * format and below 25.5; ACL failure, empty string, or non-official format skips gate with MACHINE_LOGW. */
84 static void CheckAscendDriverVersionOnboard();85 static void CheckAscendDriverVersionOnboard();
85 86 
87+ static void InitDevArgs(DeviceArgs& devArgs);
88+ 
86 static void DeviceLauncherConfigFillDeviceInfo(const DeviceLauncherConfig& config)89 static void DeviceLauncherConfigFillDeviceInfo(const DeviceLauncherConfig& config)
87 {90 {
88 DeviceLauncherConfig& devConfig = const_cast<DeviceLauncherConfig&>(config);91 DeviceLauncherConfig& devConfig = const_cast<DeviceLauncherConfig&>(config);
@@ -230,7 +233,7 @@ public:
230 }233 }
231 234 
232 if (isDevice) {235 if (isDevice) {
233- DeviceRunner::Get().InitMetaData(devProg->devArgs);236+ InitDevArgs(devProg->devArgs);
234 }237 }
235 238 
236 devProg->workspaceSize = devProg->memBudget.Total();239 devProg->workspaceSize = devProg->memBudget.Total();
@@ -468,6 +471,7 @@ public:
468 471 
469 static int SetCaptureStream(RtStream aicoreStream, RtStream aicpuStream, bool& isCapture);472 static int SetCaptureStream(RtStream aicoreStream, RtStream aicpuStream, bool& isCapture);
470 static int RunWithProfile(RtStream aicoreStream, RtStream aicpuStream, bool isCapture);473 static int RunWithProfile(RtStream aicoreStream, RtStream aicpuStream, bool isCapture);
474+ static int DynamicLaunchSynchronize(RtStream schedStream, RtStream ctrlStream, RtStream aicoreStream);
471 static int DeviceLaunchOnceWithDeviceTensorData(Function* function, const std::vector<DeviceTensorData>& inputList,475 static int DeviceLaunchOnceWithDeviceTensorData(Function* function, const std::vector<DeviceTensorData>& inputList,
472 const std::vector<DeviceTensorData>& outputList,476 const std::vector<DeviceTensorData>& outputList,
473 RtStream aicoreStream, bool streamSynchronize,477 RtStream aicoreStream, bool streamSynchronize,
@@ -17,9 +17,30 @@
17 17 
18#include <cstdint>18#include <cstdint>
19#include <numeric>19#include <numeric>
20+#include "adapter/api/runtime_define.h"
20#include "machine/utils/dynamic/dev_tensor_creator.h"21#include "machine/utils/dynamic/dev_tensor_creator.h"
22+#include "machine/utils/machine_ws_intf.h"
21 23 
22namespace npu::tile_fwk::dynamic {24namespace npu::tile_fwk::dynamic {
25+struct KernelLaunchInfo {
26+ RtStream schedStream;
27+ RtStream ctrlStream;
28+ RtStream aicoreStream;
29+ uint32_t blockDim;
30+ uint32_t aicpuNum;
31+ RtBinHandle binHandle;
32+ bool isCaptureActivate;
33+ KernelLaunchInfo(RtStream sStream, RtStream cStream, RtStream aStream, uint32_t blkDim, uint32_t cpuNum)
34+ : schedStream(sStream),
35+ ctrlStream(cStream),
36+ aicoreStream(aStream),
37+ blockDim(blkDim),
38+ aicpuNum(cpuNum),
39+ binHandle(nullptr),
40+ isCaptureActivate(false)
41+ {}
42+};
43+ 
23class DeviceTensorData {44class DeviceTensorData {
24public:45public:
25 DeviceTensorData() = default;46 DeviceTensorData() = default;
@@ -9,7 +9,7 @@
9 */9 */
10 10 
11/*!11/*!
12- * \file device_runner.cpp12+ * \file emulation_launcher.cpp
13 * \brief13 * \brief
14 */14 */
15 15 
@@ -26,6 +26,13 @@ namespace npu::tile_fwk {
26namespace {26namespace {
27constexpr uint32_t AICPU_NUM_OF_RUN_AICPU_TASKS = 1;27constexpr uint32_t AICPU_NUM_OF_RUN_AICPU_TASKS = 1;
28}28}
29+ 
30+DevicePerf& DevicePerf::GetInstance()
31+{
32+ static DevicePerf instance;
33+ return instance;
34+}
35+ 
29DevicePerf::DevicePerf() : args_() {}36DevicePerf::DevicePerf() : args_() {}
30DevicePerf::~DevicePerf()37DevicePerf::~DevicePerf()
31{38{
@@ -23,6 +23,7 @@
23namespace npu::tile_fwk {23namespace npu::tile_fwk {
24class DevicePerf {24class DevicePerf {
25public:25public:
26+ static DevicePerf& GetInstance();
26 DevicePerf();27 DevicePerf();
27 ~DevicePerf();28 ~DevicePerf();
28 void InitAndStartDumpThread(const DeviceArgs& args);29 void InitAndStartDumpThread(const DeviceArgs& args);
@@ -1,289 +0,0 @@
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 device_runner.cpp
13- * \brief
14- */
15- 
16-#include "machine/runtime/runner/device_runner.h"
17-#include <cstdlib>
18-#include <mutex>
19-#include "tilefwk/platform.h"
20-#include "tilefwk/pypto_fwk_log.h"
21-#include "tilefwk/error_code.h"
22-#include "adapter/api/msprof_api.h"
23-#include "adapter/api/acl_api.h"
24-#include "adapter/api/runtime_api.h"
25-#include "interface/utils/common.h"
26-#include "interface/utils/op_info_manager.h"
27-#include "interface/configs/config_manager.h"
28-#include "interface/machine/host/host_machine.h"
29-#include "utils/file_utils.h"
30-#include "machine/runtime/runner/runtime_agent.h"
31-#include "machine/runtime/runner/runtime_utils.h"
32-#include "machine/runtime/context/stream_context.h"
33-#include "machine/runtime/runner/pmu_common.h"
34-#include "machine/runtime/runner/load_aicpu_op.h"
35-#include "machine/runtime/runner/device_error_tracking.h"
36-#include "machine/runtime/runner/device_exception_dump.h"
37-#include "machine/runtime/runner/device_dfx.h"
38-#include "machine/runtime/memory_utils/memory_pool.h"
39-#include "machine/host/perf_analysis.h"
40-#include "machine/device/dynamic/device_common.h"
41-#include "machine/device/tilefwk/aicore_print_base.h"
42-#include "machine/utils/machine_ws_intf.h"
43-#include "securec.h"
44- 
45-namespace npu::tile_fwk {
46-namespace {
47-constexpr uint32_t MIX_BLOCK_DIM = 2;
48-constexpr uint32_t HIGHT_BIT = 16;
49-constexpr uint32_t SUB_CORE = 3;
50-constexpr uint32_t AIV_PER_AICORE = 2;
51-constexpr uint32_t AICPU_NUM_OF_RUN_AICPU_TASKS = 1;
52-// AiCpu backend .so bytes from a kernel bundle; when non-empty, InitAiCpuSoBin uses these instead of the
53-// on-disk copy. Set before Init() runs, so no locking is needed.
54-std::vector<uint8_t> g_aicpuSoOverride;
55-} // namespace
56- 
57-DeviceRunner::~DeviceRunner() {}
58- 
59-DeviceRunner& DeviceRunner::Get()
60-{
61- static DeviceRunner runner;
62- std::call_once(runner.once_, [&]() { ASSERT(DevCommonErr::INIT_FAILED, runner.Init() == 0); });
63- return runner;
64-}
65- 
66-void DeviceRunner::SetHostProfFunction(Function* function,
67- const std::vector<npu::tile_fwk::dynamic::DeviceTensorData>& tensors)
68-{
69- hostProf_.SetProfFunction(function, tensors);
70-}
71- 
72-uint32_t DeviceRunner::GetHostProfType() const { return (hostProf_.GetProfSwitch() & MSPF_TASK_TIME_L2_MASK) != 0; }
73- 
74-void DeviceRunner::ResetPerData() const { devicePerf_.ResetPerData(); }
75- 
76-void DeviceRunner::InitMetaData(DeviceArgs& devArgs) const
77-{
78- devArgs.runtimeDataRingBufferAddr = args_.runtimeDataRingBufferAddr;
79- devArgs.sharedBuffer = args_.sharedBuffer;
80- devArgs.coreRegAddr = args_.coreRegAddr;
81- devArgs.nrAic = args_.nrAic;
82- devArgs.nrAiv = args_.nrAiv;
83- devArgs.corePmuRegAddr = args_.corePmuRegAddr;
84- devArgs.corePmuAddr = args_.corePmuAddr;
85- devArgs.taskWastTime = args_.taskWastTime;
86- devArgs.pmuEventAddr = args_.pmuEventAddr;
87- devArgs.aicpuPerfAddr = args_.aicpuPerfAddr;
88- devArgs.devDfxArgAddr = args_.devDfxArgAddr;
89-}
90- 
91-int DeviceRunner::InitDeviceArgsCore(DeviceArgs& args)
92-{
93- std::vector<int64_t> regs;
94- std::vector<int64_t> regsPmu;
95- GetAicoreRegs(args.archInfo, regs, regsPmu);
96- 
97- args.nrAic = regs.size() / SUB_CORE;
98- args.nrAiv = args.nrAic * AIV_PER_AICORE;
99- uint64_t nrCore = regs.size() + AICPU_NUM_OF_RUN_AICPU_TASKS;
100- args.sharedBuffer = reinterpret_cast<uint64_t>(DevAlloc(nrCore * SHARED_BUFFER_SIZE));
101- args.corePmuAddr = reinterpret_cast<uint64_t>(DevAlloc(nrCore * PMU_BUFFER_SIZE));
102- if (args.sharedBuffer == 0 || args.corePmuAddr == 0) {
103- return -1;
104- }
105- 
106- // core reg
107- size_t regSize = regs.size() * sizeof(uint64_t);
108- args.coreRegAddr = reinterpret_cast<uint64_t>(CopyDataToDevice(regs.data(), regSize));
109- if (args.coreRegAddr == 0) {
110- MACHINE_LOGE(DevCommonErr::ALLOC_FAILED, "Fail to copy aicore reg data from host to device.");
111- return -1;
112- }
113- 
114- // core reg pmu
115- args.corePmuRegAddr = reinterpret_cast<uint64_t>(CopyDataToDevice(regsPmu.data(), regSize));
116- if (args.corePmuRegAddr == 0) {
117- MACHINE_LOGE(DevCommonErr::ALLOC_FAILED, "Fail to copy aicore pmu reg data from host to device.");
118- return -1;
119- }
120- MACHINE_LOGI("Dev args :aic %u aiv %u, sharedBuffer %lx coreRegAddr %lx corePmuRegAddr %lx", args.nrAic, args.nrAiv,
121- args.sharedBuffer, args.coreRegAddr, args.corePmuRegAddr);
122- 
123- args.taskWastTime = reinterpret_cast<uint64_t>(DevAlloc(sizeof(uint64_t)));
124- size_t shmSize = sizeof(dynamic::RuntimeDataRingBufferHead) + dynamic::DEVICE_SHM_SIZE +
125- dynamic::DEVICE_TASK_QUEUE_SIZE * args.nrAicpu;
126- args.runtimeDataRingBufferAddr = reinterpret_cast<uint64_t>(DevAlloc(shmSize));
127- 
128- // pmu evt info
129- std::vector<int64_t> pmuEvtType;
130- PmuCommon::InitPmuEventType(args.archInfo, pmuEvtType);
131- args.pmuEventAddr = reinterpret_cast<uint64_t>(
132- CopyDataToDevice(pmuEvtType.data(), pmuEvtType.size() * sizeof(int64_t)));
133- if (args.pmuEventAddr == 0) {
134- MACHINE_LOGE(DevCommonErr::ALLOC_FAILED, "Fail to copy pmu evt type from host to device.");
135- return -1;
136- }
137- 
138- if (!DeviceDfx::GetInstance().Init(args)) {
139- return -1;
140- }
141- return 0;
142-}
143- 
144-void DeviceRunner::GetAicoreRegs(const ArchInfo archInfo, std::vector<int64_t>& regs, std::vector<int64_t>& regsPmu)
145-{
146- if (archInfo == ArchInfo::DAV_3510) {
147- RuntimeAgent::GetAicoreRegInfoForDAV3510(regs, regsPmu);
148- return;
149- }
150- if (archInfo == ArchInfo::DAV_2201) {
151- std::vector<int64_t> aiv;
152- std::vector<int64_t> aic;
153- if (RuntimeAgent::GetAgent().GetAicoreRegInfo(aic, aiv, ADDR_MAP_TYPE_REG_AIC_CTRL) != 0) {
154- return;
155- }
156- regs.insert(regs.end(), aic.begin(), aic.end());
157- regs.insert(regs.end(), aiv.begin(), aiv.end());
158- 
159- std::vector<int64_t> aivPmu;
160- std::vector<int64_t> aicPmu;
161- if (RuntimeAgent::GetAgent().GetAicoreRegInfo(aicPmu, aivPmu, ADDR_MAP_TYPE_REG_AIC_PMU_CTRL) != 0) {
162- return;
163- }
164- regsPmu.insert(regsPmu.end(), aicPmu.begin(), aicPmu.end());
165- regsPmu.insert(regsPmu.end(), aivPmu.begin(), aivPmu.end());
166- }
167-}
168- 
169-int DeviceRunner::InitDeviceArgs(DeviceArgs& args)
170-{
171- memset_s(&args, sizeof(args), 0, sizeof(args));
172- args.deviceId = GetLogDeviceId();
173- args.archInfo = static_cast<ArchInfo>(Platform::Instance().GetSoc().GetNPUArch());
174- uint32_t aicpuNum = args.archInfo == ArchInfo::DAV_3510 ? dynamic::DEVICE_MAX_AICPU_NUM : 5;
175- uint32_t maxAicpuNum = static_cast<uint32_t>(Platform::Instance().GetSoc().GetAICPUNum());
176- args.nrValidAic = GetCfgBlockdim();
177- args.nrAicpu = std::min(aicpuNum, maxAicpuNum);
178- args.scheCpuNum = dynamic::CalcSchAicpuNumByBlockDim(args.nrValidAic, args.nrAicpu, args.archInfo);
179- MACHINE_LOGD("DevArgs: block dim[%u], aicpu num[%u], max aicpu num[%u], sche cpu num[%u].", args.nrValidAic,
180- args.nrAicpu, maxAicpuNum, args.scheCpuNum);
181- 
182- InitAiCpuSoBin(args);
183- 
184- return InitDeviceArgsCore(args);
185-}
186- 
187-void DeviceRunner::SyncProfData(bool debugEnable) { devicePerf_.SyncProfData(debugEnable); }
188- 
189-/**************************** DynamicFunction *****************************/
190-int DeviceRunner::DynamicLaunchSynchronize(RtStream schedStream, RtStream ctrlStream, RtStream aicoreStream)
191-{
192- int rcAicore = RuntimeStreamSynchronize(aicoreStream);
193- int rcAicpu = RuntimeStreamSynchronize(schedStream);
194- int rcCtrl = 0;
195- if (ctrlStream != nullptr) {
196- rcCtrl = RuntimeStreamSynchronize(ctrlStream);
197- }
198- if (IsPtoDataDumpEnabled()) {
199- MACHINE_LOGD("DataDumpServerUnInit is called \n");
200- (void)AdxDumpDataDumpServerUnInit();
201- }
202- int retAicorePrint = DeviceDfx::GetInstance().DumpAicoreLog();
203- if (rcAicore != 0 || rcAicpu != 0 || rcCtrl != 0 || retAicorePrint != 0) {
204- MACHINE_LOGW("sync stream failed aicpu:%d aicore:%d ctrl cpu:%d, aicorePrint: %d", rcAicpu, rcAicore, rcCtrl,
205- retAicorePrint);
206- }
207- return rcAicore + rcAicpu + rcCtrl;
208-}
209- 
210-void DeviceRunner::SetAiCpuSoOverride(std::vector<uint8_t> soBytes) { g_aicpuSoOverride = std::move(soBytes); }
211- 
212-void DeviceRunner::InitAiCpuSoBin(DeviceArgs& devArgs)
213-{
214- std::vector<uint8_t> buffer;
215- std::string source;
216- if (!g_aicpuSoOverride.empty()) {
217- buffer = g_aicpuSoOverride; // bundle-supplied .so takes precedence over the on-disk copy
218- source = "kernel-bundle AICPU_SO";
219- } else {
220- std::string fileName = GetPyptoLibPath() + "/libtilefwk_backend_server.so";
221- source = fileName;
222- buffer = ReadFile(fileName);
223- }
224- if (buffer.empty()) {
225- MACHINE_LOGE(DevCommonErr::FILE_ERROR,
226- "Read bin form tilefwk_backend_server.so failed, please check the so[%s]", source.c_str());
227- return;
228- }
229- void* devBufferPtr = CopyDataToDevice(buffer.data(), buffer.size());
230- if (devBufferPtr == nullptr) {
231- MACHINE_LOGE(DevCommonErr::MEMCPY_FAILED, "Failed to copy buffer of [%s] to device.", source.c_str());
232- return;
233- }
234- devArgs.aicpuSoBin = reinterpret_cast<uint64_t>(devBufferPtr);
235- devArgs.aicpuSoLen = buffer.size();
236- MACHINE_LOGI("[aicpu-so] init backend server .so from %s, len=%zu", source.c_str(), buffer.size());
237- HOST_PERF_TRACE(TracePhase::RunDevKernelInitAicpuSo);
238-}
239- 
240-bool DeviceRunner::GetEnableDumpDevPref() const { return args_.aicpuPerfAddr != 0; }
241- 
242-void DeviceRunner::SetDebugEnable() { devicePerf_.SetDebugEnable(); }
243- 
244-void DeviceRunner::ReportHostProfInfo(RtStream stream, uint64_t startTime, uint32_t blockDim, uint16_t taskType,
245- bool isCore) const
246-{
247- if (hostProf_.GetProfType() == MSPF_COMMANDHANDLE_TYPE_START) {
248- uint64_t endTime = MspfSysCycleTime();
249- if (isCore) {
250- uint32_t mixBlockDim = MIX_BLOCK_DIM;
251- blockDim = (mixBlockDim << HIGHT_BIT) | blockDim;
252- hostProf_.HostProfReportContextInfo(endTime);
253- }
254- if ((hostProf_.GetProfSwitch() & MSPF_TASK_TIME_L1_MASK) != 0) {
255- hostProf_.HostProfReportNodeInfo(endTime, blockDim, taskType);
256- }
257- endTime = MspfSysCycleTime();
258- (void)hostProf_.HostProfReportApi(startTime, endTime);
259- }
260- if (taskType == MSPF_GE_TASK_TYPE_MIX_AIC) {
261- hostProf_.HostProfReportCacheTaskInfo(stream, blockDim, taskType);
262- }
263-}
264- 
265-int DeviceRunner::Init()
266-{
267- hostProf_.RegHostProf();
268- InitializeErrorCallback();
269- 
270- if (InitDeviceArgs(args_) != 0) {
271- MACHINE_LOGE(HostLauncherErr::PREPARE_ARGS_FAILED, "prepareArgs failed\n");
272- return static_cast<int>(HostLauncherErr::PREPARE_ARGS_FAILED);
273- }
274- int64_t* devArgsAddr = static_cast<int64_t*>(CopyDataToDevice(&args_, sizeof(DeviceArgs)));
275- if (devArgsAddr == nullptr) {
276- MACHINE_LOGE(DevCommonErr::MEMCPY_FAILED, "Failed to copy args to device.");
277- return static_cast<int>(DevCommonErr::MEMCPY_FAILED);
278- }
279- if (config::GetRuntimeOption<int64_t>(CFG_RUN_MODE) != CFG_RUN_MODE_SIM) {
280- if (LoadAicpuOp::GetInstance().GetBuiltInOpBinHandle(devArgsAddr) != 0) {
281- MACHINE_LOGE(DevCommonErr::GET_HANDLE_FAILED, "Get builtInOp Funchandle failed\n");
282- return static_cast<int>(DevCommonErr::GET_HANDLE_FAILED);
283- }
284- devicePerf_.InitAndStartDumpThread(args_);
285- npu::tile_fwk::dynamic::AdumpRegExceptionDump();
286- }
287- return 0;
288-}
289-} // namespace npu::tile_fwk
@@ -1,80 +0,0 @@
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 device_runner.h
13- * \brief
14- */
15- 
16-#pragma once
17- 
18-#include <cstdint>
19-#include <vector>
20-#include <mutex>
21-#include "machine/runtime/runner/host_prof.h"
22-#include "machine/runtime/runner/device_perf.h"
23-#include "machine/utils/machine_ws_intf.h"
24- 
25-namespace npu::tile_fwk {
26-struct KernelLaunchInfo {
27- RtStream schedStream;
28- RtStream ctrlStream;
29- RtStream aicoreStream;
30- uint32_t blockDim;
31- uint32_t aicpuNum;
32- RtBinHandle binHandle;
33- bool isCaptureActivate;
34- KernelLaunchInfo(RtStream sStream, RtStream cStream, RtStream aStream, uint32_t blkDim, uint32_t cpuNum)
35- : schedStream(sStream),
36- ctrlStream(cStream),
37- aicoreStream(aStream),
38- blockDim(blkDim),
39- aicpuNum(cpuNum),
40- binHandle(nullptr),
41- isCaptureActivate(false)
42- {}
43-};
44-class DeviceRunner {
45-public:
46- static DeviceRunner& Get();
47- static int DynamicLaunchSynchronize(RtStream schedStream, RtStream ctrlStream, RtStream aicoreStream);
48- 
49- void SetHostProfFunction(Function* function,
50- const std::vector<npu::tile_fwk::dynamic::DeviceTensorData>& tensors = {});
51- uint32_t GetHostProfType() const;
52- void ReportHostProfInfo(RtStream stream, uint64_t startTime, uint32_t blockDim, uint16_t taskType,
53- bool isCore = false) const;
54- 
55- void InitMetaData(DeviceArgs& devArgs) const;
56- bool GetEnableDumpDevPref() const;
57- void ResetPerData() const;
58- void SyncProfData(bool debugEnable);
59- void SetDebugEnable();
60- 
61- // Use bundle-supplied AiCpu backend .so bytes for Init() instead of the on-disk copy.
62- // Must be called before Init(); a later call has no effect.
63- static void SetAiCpuSoOverride(std::vector<uint8_t> soBytes);
64- 
65-private:
66- DeviceRunner() = default;
67- ~DeviceRunner();
68- int Init();
69- int InitDeviceArgs(DeviceArgs& args);
70- static void InitAiCpuSoBin(DeviceArgs& devArgs);
71- static void GetAicoreRegs(const ArchInfo archInfo, std::vector<int64_t>& regs, std::vector<int64_t>& regsPmu);
72- static int InitDeviceArgsCore(DeviceArgs& args);
73- 
74-private:
75- std::once_flag once_;
76- DeviceArgs args_;
77- HostProf hostProf_;
78- DevicePerf devicePerf_;
79-};
80-} // namespace npu::tile_fwk
@@ -12,11 +12,13 @@
12 12 
13#include <sys/syscall.h>13#include <sys/syscall.h>
14#include "tilefwk/pypto_fwk_log.h"14#include "tilefwk/pypto_fwk_log.h"
15+#include "tilefwk/error_code.h"
15#include "machine/runtime/runner/runtime_utils.h"16#include "machine/runtime/runner/runtime_utils.h"
16#include "tilefwk/error.h"17#include "tilefwk/error.h"
17#include "adapter/api/msprof_api.h"18#include "adapter/api/msprof_api.h"
18#include "adapter/api/acl_api.h"19#include "adapter/api/acl_api.h"
19#include "interface/tensor/logical_tensor.h"20#include "interface/tensor/logical_tensor.h"
21+#include "interface/utils/common.h"
20#include "passes/pass_utils/pass_utils.h"22#include "passes/pass_utils/pass_utils.h"
21 23 
22#define CCECPU 2524#define CCECPU 25
@@ -26,10 +28,41 @@ namespace {
26const std::string kOpType = "PyPTO";28const std::string kOpType = "PyPTO";
27constexpr uint32_t kFormatNd = 2;29constexpr uint32_t kFormatNd = 2;
28constexpr uint32_t kFormatNz = 29;30constexpr uint32_t kFormatNz = 29;
31+constexpr uint32_t MIX_BLOCK_DIM = 2;
32+constexpr uint32_t HIGHT_BIT = 16;
29} // namespace33} // namespace
30 34 
35+HostProf& HostProf::GetInstance()
36+{
37+ thread_local HostProf instance;
38+ return instance;
39+}
40+ 
31HostProf::~HostProf() {}41HostProf::~HostProf() {}
32 42 
43+uint32_t HostProf::GetHostProfType() const { return (GetProfSwitch() & MSPF_TASK_TIME_L2_MASK) != 0; }
44+ 
45+void HostProf::ReportHostProfInfo(RtStream stream, uint64_t startTime, uint32_t blockDim, uint16_t taskType,
46+ bool isCore) const
47+{
48+ if (GetProfType() == MSPF_COMMANDHANDLE_TYPE_START) {
49+ uint64_t endTime = MspfSysCycleTime();
50+ if (isCore) {
51+ uint32_t mixBlockDim = MIX_BLOCK_DIM;
52+ blockDim = (mixBlockDim << HIGHT_BIT) | blockDim;
53+ HostProfReportContextInfo(endTime);
54+ }
55+ if ((GetProfSwitch() & MSPF_TASK_TIME_L1_MASK) != 0) {
56+ HostProfReportNodeInfo(endTime, blockDim, taskType);
57+ }
58+ endTime = MspfSysCycleTime();
59+ (void)HostProfReportApi(startTime, endTime);
60+ }
61+ if (taskType == MSPF_GE_TASK_TYPE_MIX_AIC) {
62+ HostProfReportCacheTaskInfo(stream, blockDim, taskType);
63+ }
64+}
65+ 
33uint64_t HostProf::GetProfSwitch() { return profSwitch_; }66uint64_t HostProf::GetProfSwitch() { return profSwitch_; }
34 67 
35uint32_t HostProf::GetProfType() { return profType_; }68uint32_t HostProf::GetProfType() { return profType_; }
@@ -15,6 +15,7 @@
15#include "interface/function/function.h"15#include "interface/function/function.h"
16#include "interface/interpreter/raw_tensor_data.h"16#include "interface/interpreter/raw_tensor_data.h"
17#include "machine/runtime/launcher/device_launcher_types.h"17#include "machine/runtime/launcher/device_launcher_types.h"
18+#include "machine/utils/machine_ws_intf.h"
18 19 
19namespace npu::tile_fwk {20namespace npu::tile_fwk {
20struct CacheTaskInfo {21struct CacheTaskInfo {
@@ -31,6 +32,7 @@ struct CacheTaskInfo {
31 32 
32class HostProf {33class HostProf {
33public:34public:
35+ static HostProf& GetInstance();
34 HostProf() = default;36 HostProf() = default;
35 ~HostProf();37 ~HostProf();
36 bool HostProfReportApi(const uint64_t& startTime, const uint64_t& endTime) const;38 bool HostProfReportApi(const uint64_t& startTime, const uint64_t& endTime) const;
@@ -38,6 +40,9 @@ public:
38 void HostProfReportContextInfo(const uint64_t& endTime) const;40 void HostProfReportContextInfo(const uint64_t& endTime) const;
39 void HostProfReportCacheTaskInfo(const AclRtStream stream, const uint32_t numBlocks, const uint32_t taskType) const;41 void HostProfReportCacheTaskInfo(const AclRtStream stream, const uint32_t numBlocks, const uint32_t taskType) const;
40 void SetProfFunction(Function* function, const std::vector<npu::tile_fwk::dynamic::DeviceTensorData>& tensors = {});42 void SetProfFunction(Function* function, const std::vector<npu::tile_fwk::dynamic::DeviceTensorData>& tensors = {});
43+ uint32_t GetHostProfType() const;
44+ void ReportHostProfInfo(RtStream stream, uint64_t startTime, uint32_t blockDim, uint16_t taskType,
45+ bool isCore = false) const;
41 static uint64_t GetProfSwitch();46 static uint64_t GetProfSwitch();
42 static uint32_t GetProfType();47 static uint32_t GetProfType();
43 static void RegHostProf();48 static void RegHostProf();
@@ -16,12 +16,29 @@
16#include "machine/runtime/runner/kernel_binary.h"16#include "machine/runtime/runner/kernel_binary.h"
17 17 
18#include <cstdlib>18#include <cstdlib>
19+#include <mutex>
19#include <sstream>20#include <sstream>
20 21 
22+#include "tilefwk/platform.h"
21#include "tilefwk/pypto_fwk_log.h"23#include "tilefwk/pypto_fwk_log.h"
22#include "tilefwk/error_code.h"24#include "tilefwk/error_code.h"
25+#include "adapter/api/msprof_api.h"
26+#include "adapter/api/acl_api.h"
27+#include "adapter/api/runtime_api.h"
23#include "interface/function/rebuildable_attribute.h"28#include "interface/function/rebuildable_attribute.h"
29+#include "interface/utils/common.h"
30+#include "interface/configs/config_manager.h"
31+#include "interface/machine/host/perf_analysis.h"
32+#include "machine/runtime/runner/host_prof.h"
33+#include "machine/runtime/runner/device_perf.h"
34+#include "machine/runtime/runner/runtime_agent.h"
35+#include "machine/runtime/runner/device_error_tracking.h"
36+#include "machine/runtime/runner/device_exception_dump.h"
37+#include "machine/runtime/runner/device_dfx.h"
38+#include "machine/runtime/runner/pmu_common.h"
24#include "machine/device/dump/dump_device_memory.h"39#include "machine/device/dump/dump_device_memory.h"
40+#include "machine/device/dynamic/device_common.h"
41+#include "machine/device/tilefwk/aicore_print_base.h"
25#include "machine/runtime/launcher/cell_match_dynamic.h"42#include "machine/runtime/launcher/cell_match_dynamic.h"
26#include "machine/runtime/launcher/device_launcher.h"43#include "machine/runtime/launcher/device_launcher.h"
27#include "machine/runtime/launcher/device_launcher_binding.h"44#include "machine/runtime/launcher/device_launcher_binding.h"
@@ -32,9 +49,23 @@
32#include "machine/runtime/runner/runtime_utils.h"49#include "machine/runtime/runner/runtime_utils.h"
33#include "machine/utils/dynamic/dev_encode_program.h"50#include "machine/utils/dynamic/dev_encode_program.h"
34#include "machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h"51#include "machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h"
52+#include "machine/utils/dynamic/dev_start_args.h"
53+#include "machine/utils/dynamic/device_task.h"
35#include "interface/configs/config_manager_ng.h"54#include "interface/configs/config_manager_ng.h"
55+#include "utils/file_utils.h"
56+#include "securec.h"
36 57 
37namespace npu::tile_fwk::dynamic {58namespace npu::tile_fwk::dynamic {
59+namespace {
60+constexpr uint32_t SUB_CORE = 3;
61+constexpr uint32_t AIV_PER_AICORE = 2;
62+constexpr uint32_t AICPU_NUM_OF_RUN_AICPU_TASKS = 1;
63+// AiCpu backend .so bytes from a kernel bundle; when non-empty, InitAiCpuSoBin uses these instead of the
64+// on-disk copy. Set before init runs, so no locking is needed.
65+std::vector<uint8_t> g_aicpuSoOverride;
66+DeviceArgs g_deviceArgs;
67+std::once_flag g_initOnce;
68+} // namespace
38 69 
39KernelBinary::KernelBinary(std::shared_ptr<Function> func, std::vector<std::shared_ptr<Function>> pinnedGraph)70KernelBinary::KernelBinary(std::shared_ptr<Function> func, std::vector<std::shared_ptr<Function>> pinnedGraph)
40 : dynFunc(std::move(func)), pinnedGraph_(std::move(pinnedGraph))71 : dynFunc(std::move(func)), pinnedGraph_(std::move(pinnedGraph))
@@ -45,6 +76,7 @@ KernelBinary::KernelBinary(std::shared_ptr<Function> func, std::vector<std::shar
45 workspaceSize = devProg->memBudget.Total();76 workspaceSize = devProg->memBudget.Total();
46 InitCachedArgs();77 InitCachedArgs();
47 InitLaunchArgs();78 InitLaunchArgs();
79+ InitDeviceArgs();
48 auto aicpuArgs = (AiCpuArgs*)aicpuArgBuf.data();80 auto aicpuArgs = (AiCpuArgs*)aicpuArgBuf.data();
49 DeviceLauncher::FillSwimLaneEnableInfo(toSubMachineConfig_);81 DeviceLauncher::FillSwimLaneEnableInfo(toSubMachineConfig_);
50 if (config::GetRuntimeOption<int64_t>(CFG_RUN_MODE) == CFG_RUN_MODE_SIM) {82 if (config::GetRuntimeOption<int64_t>(CFG_RUN_MODE) == CFG_RUN_MODE_SIM) {
@@ -395,4 +427,177 @@ void KernelBinary::RefreshRuntimeDynamicCellMatchMeta(uint64_t needBytes)
395 }427 }
396}428}
397 429 
430+void KernelBinary::SetAiCpuSoOverride(std::vector<uint8_t> soBytes) { g_aicpuSoOverride = std::move(soBytes); }
431+ 
432+void KernelBinary::InitAiCpuSoBin(DeviceArgs& devArgs)
433+{
434+ std::vector<uint8_t> buffer;
435+ std::string source;
436+ if (!g_aicpuSoOverride.empty()) {
437+ buffer = g_aicpuSoOverride; // bundle-supplied .so takes precedence over the on-disk copy
438+ source = "kernel-bundle AICPU_SO";
439+ } else {
440+ std::string fileName = GetPyptoLibPath() + "/libtilefwk_backend_server.so";
441+ source = fileName;
442+ buffer = ReadFile(fileName);
443+ }
444+ if (buffer.empty()) {
445+ MACHINE_LOGE(DevCommonErr::FILE_ERROR,
446+ "Read bin form tilefwk_backend_server.so failed, please check the so[%s]", source.c_str());
447+ return;
448+ }
449+ void* devBufferPtr = CopyDataToDevice(buffer.data(), buffer.size());
450+ if (devBufferPtr == nullptr) {
451+ MACHINE_LOGE(DevCommonErr::MEMCPY_FAILED, "Failed to copy buffer of [%s] to device.", source.c_str());
452+ return;
453+ }
454+ devArgs.aicpuSoBin = reinterpret_cast<uint64_t>(devBufferPtr);
455+ devArgs.aicpuSoLen = buffer.size();
456+ MACHINE_LOGI("[aicpu-so] init backend server .so from %s, len=%zu", source.c_str(), buffer.size());
457+ HOST_PERF_TRACE(TracePhase::RunDevKernelInitAicpuSo);
458+}
459+ 
460+void KernelBinary::GetAicoreRegs(const ArchInfo archInfo, std::vector<int64_t>& regs, std::vector<int64_t>& regsPmu)
461+{
462+ if (archInfo == ArchInfo::DAV_3510) {
463+ RuntimeAgent::GetAicoreRegInfoForDAV3510(regs, regsPmu);
464+ return;
465+ }
466+ if (archInfo == ArchInfo::DAV_2201) {
467+ std::vector<int64_t> aiv;
468+ std::vector<int64_t> aic;
469+ if (RuntimeAgent::GetAgent().GetAicoreRegInfo(aic, aiv, ADDR_MAP_TYPE_REG_AIC_CTRL) != 0) {
470+ return;
471+ }
472+ regs.insert(regs.end(), aic.begin(), aic.end());
473+ regs.insert(regs.end(), aiv.begin(), aiv.end());
474+ 
475+ std::vector<int64_t> aivPmu;
476+ std::vector<int64_t> aicPmu;
477+ if (RuntimeAgent::GetAgent().GetAicoreRegInfo(aicPmu, aivPmu, ADDR_MAP_TYPE_REG_AIC_PMU_CTRL) != 0) {
478+ return;
479+ }
480+ regsPmu.insert(regsPmu.end(), aicPmu.begin(), aicPmu.end());
481+ regsPmu.insert(regsPmu.end(), aivPmu.begin(), aivPmu.end());
482+ }
483+}
484+ 
485+int KernelBinary::InitDeviceArgsCore(DeviceArgs& args)
486+{
487+ std::vector<int64_t> regs;
488+ std::vector<int64_t> regsPmu;
489+ GetAicoreRegs(args.archInfo, regs, regsPmu);
490+ 
491+ args.nrAic = regs.size() / SUB_CORE;
492+ args.nrAiv = args.nrAic * AIV_PER_AICORE;
493+ uint64_t nrCore = regs.size() + AICPU_NUM_OF_RUN_AICPU_TASKS;
494+ args.sharedBuffer = reinterpret_cast<uint64_t>(DevAlloc(nrCore * SHARED_BUFFER_SIZE));
495+ args.corePmuAddr = reinterpret_cast<uint64_t>(DevAlloc(nrCore * PMU_BUFFER_SIZE));
496+ if (args.sharedBuffer == 0 || args.corePmuAddr == 0) {
497+ MACHINE_LOGE(DevCommonErr::ALLOC_FAILED, "Fail alloc sharedBuffer[%lu] or corePmuAddr[%lu].", args.sharedBuffer,
498+ args.corePmuAddr);
499+ return static_cast<int>(DevCommonErr::ALLOC_FAILED);
500+ }
501+ 
502+ // core reg
503+ size_t regSize = regs.size() * sizeof(uint64_t);
504+ args.coreRegAddr = reinterpret_cast<uint64_t>(CopyDataToDevice(regs.data(), regSize));
505+ if (args.coreRegAddr == 0) {
506+ MACHINE_LOGE(DevCommonErr::MEMCPY_FAILED, "Fail to copy aicore reg data from host to device.");
507+ return static_cast<int>(DevCommonErr::MEMCPY_FAILED);
508+ }
509+ 
510+ // core reg pmu
511+ args.corePmuRegAddr = reinterpret_cast<uint64_t>(CopyDataToDevice(regsPmu.data(), regSize));
512+ if (args.corePmuRegAddr == 0) {
513+ MACHINE_LOGE(DevCommonErr::MEMCPY_FAILED, "Fail to copy aicore pmu reg data from host to device.");
514+ return static_cast<int>(DevCommonErr::MEMCPY_FAILED);
515+ }
516+ MACHINE_LOGI("Dev args :aic %u aiv %u, sharedBuffer %lx coreRegAddr %lx corePmuRegAddr %lx", args.nrAic, args.nrAiv,
517+ args.sharedBuffer, args.coreRegAddr, args.corePmuRegAddr);
518+ 
519+ args.taskWastTime = reinterpret_cast<uint64_t>(DevAlloc(sizeof(uint64_t)));
520+ size_t shmSize = sizeof(dynamic::RuntimeDataRingBufferHead) + dynamic::DEVICE_SHM_SIZE +
521+ dynamic::DEVICE_TASK_QUEUE_SIZE * args.nrAicpu;
522+ args.runtimeDataRingBufferAddr = reinterpret_cast<uint64_t>(DevAlloc(shmSize));
523+ 
524+ // pmu evt info
525+ std::vector<int64_t> pmuEvtType;
526+ PmuCommon::InitPmuEventType(args.archInfo, pmuEvtType);
527+ args.pmuEventAddr = reinterpret_cast<uint64_t>(
528+ CopyDataToDevice(pmuEvtType.data(), pmuEvtType.size() * sizeof(int64_t)));
529+ if (args.pmuEventAddr == 0) {
530+ MACHINE_LOGE(DevCommonErr::MEMCPY_FAILED, "Fail to copy pmu evt type from host to device.");
531+ return static_cast<int>(DevCommonErr::MEMCPY_FAILED);
532+ }
533+ 
534+ if (!DeviceDfx::GetInstance().Init(args)) {
535+ MACHINE_LOGE(DevCommonErr::INIT_FAILED, "Device dfx info init not success.");
536+ return static_cast<int>(DevCommonErr::INIT_FAILED);
537+ }
538+ return 0;
539+}
540+ 
541+int KernelBinary::InitDeviceArgs(DeviceArgs& args)
542+{
543+ memset_s(&args, sizeof(args), 0, sizeof(args));
544+ args.deviceId = GetLogDeviceId();
545+ args.archInfo = static_cast<ArchInfo>(Platform::Instance().GetSoc().GetNPUArch());
546+ uint32_t aicpuNum = args.archInfo == ArchInfo::DAV_3510 ? dynamic::DEVICE_MAX_AICPU_NUM : 5;
547+ uint32_t maxAicpuNum = static_cast<uint32_t>(Platform::Instance().GetSoc().GetAICPUNum());
548+ args.nrValidAic = GetCfgBlockdim();
549+ args.nrAicpu = std::min(aicpuNum, maxAicpuNum);
550+ args.scheCpuNum = dynamic::CalcSchAicpuNumByBlockDim(args.nrValidAic, args.nrAicpu, args.archInfo);
551+ MACHINE_LOGD("DevArgs: block dim[%u], aicpu num[%u], max aicpu num[%u], sche cpu num[%u].", args.nrValidAic,
552+ args.nrAicpu, maxAicpuNum, args.scheCpuNum);
553+ 
554+ InitAiCpuSoBin(args);
555+ 
556+ return InitDeviceArgsCore(args);
557+}
558+ 
559+void KernelBinary::InitDeviceArgs()
560+{
561+ std::call_once(g_initOnce, []() {
562+ HostProf::GetInstance().RegHostProf();
563+ InitializeErrorCallback();
564+ ASSERT(DevCommonErr::INIT_FAILED, InitDeviceArgs(g_deviceArgs) == 0);
565+ int64_t* devArgsAddr = static_cast<int64_t*>(CopyDataToDevice(&g_deviceArgs, sizeof(DeviceArgs)));
566+ if (devArgsAddr == nullptr) {
567+ MACHINE_LOGE(DevCommonErr::MEMCPY_FAILED, "Failed to copy args to device.");
568+ ASSERT(false);
569+ }
570+ if (config::GetRuntimeOption<int64_t>(CFG_RUN_MODE) != CFG_RUN_MODE_SIM) {
571+ if (LoadAicpuOp::GetInstance().GetBuiltInOpBinHandle(devArgsAddr) != 0) {
572+ MACHINE_LOGE(DevCommonErr::GET_HANDLE_FAILED, "Get builtInOp Funchandle failed\n");
573+ ASSERT(false);
574+ }
575+ DevicePerf::GetInstance().InitAndStartDumpThread(g_deviceArgs);
576+ AdumpRegExceptionDump();
577+ }
578+ });
579+}
580+ 
581+void KernelBinary::InitMetaData(DeviceArgs& devArgs)
582+{
583+ InitDeviceArgs();
584+ devArgs.runtimeDataRingBufferAddr = g_deviceArgs.runtimeDataRingBufferAddr;
585+ devArgs.sharedBuffer = g_deviceArgs.sharedBuffer;
586+ devArgs.coreRegAddr = g_deviceArgs.coreRegAddr;
587+ devArgs.nrAic = g_deviceArgs.nrAic;
588+ devArgs.nrAiv = g_deviceArgs.nrAiv;
589+ devArgs.corePmuRegAddr = g_deviceArgs.corePmuRegAddr;
590+ devArgs.corePmuAddr = g_deviceArgs.corePmuAddr;
591+ devArgs.taskWastTime = g_deviceArgs.taskWastTime;
592+ devArgs.pmuEventAddr = g_deviceArgs.pmuEventAddr;
593+ devArgs.aicpuPerfAddr = g_deviceArgs.aicpuPerfAddr;
594+ devArgs.devDfxArgAddr = g_deviceArgs.devDfxArgAddr;
595+}
atomgit-bot

🟠 High Priority

变更行:KernelBinary::InitMetaData(kernel_binary.cpp:581-594)直接从未初始化的静态 g_deviceArgs 拷贝字段,且 GetEnableDumpDevPref(kernel_binary.cpp:596)同样只读 g_deviceArgs,两者都不再触发 InitDeviceArgs()(std::call_once)完成设备初始化。

受影响行为/契约:原实现 DeviceRunner::Get().InitMetaData(devProg->devArgs) 中,DeviceRunner::Get() 内部 std::call_once 会执行 Init() 完成 DevAlloc/CopyDataToDevice 等并填充 args_,因此即使不构造 KernelBinary 也能拿到真实设备参数。新实现中 g_deviceArgs 仅在 KernelBinary 构造函数(kernel_binary.cpp:79 调用 InitDeviceArgs())时才被填充。

失败模式:bundle 离线启动路径 LaunchBundleKernelOnce(kernel_bundle_launch.cpp:129 → FillDeviceKernelArgs → PrepareDevProgArgs → InitDevArgs → InitMetaData)从不构造 KernelBinary,因此 g_deviceArgs 保持静态零初始化。InitMetaData 会把 devProg->devArgs 的 nrAic/nrAiv/sharedBuffer/coreRegAddr/corePmuAddr/taskWastTime/pmuEventAddr/aicpuPerfAddr/devDfxArgAddr 全部覆盖为 0(PrepareDevProgArgs 之前赋的 nrAic=kDefaultAicNum、nrAiv=kDefaultAivNum 也被覆盖),导致设备侧内核以全零设备参数启动,bundle 启动失败或结果错误。同理 GetEnableDumpDevPref() 在 bundle 路径恒为 false,且 InitDeviceArgs() 里的 HostProf::GetInstance().RegHostProf()/DevicePerf::GetInstance().InitAndStartDumpThread 也永远不会执行,HostProf 回调与 device perf dump 在纯 bundle 场景下被静默禁用。

Suggestions
595
+ void KernelBinary::InitMetaData(DeviceArgs& devArgs)
596
+ {
597
+ InitDeviceArgs();
598
+ devArgs.runtimeDataRingBufferAddr = g_deviceArgs.runtimeDataRingBufferAddr;
599
+ devArgs.sharedBuffer = g_deviceArgs.sharedBuffer;
600
+ devArgs.coreRegAddr = g_deviceArgs.coreRegAddr;
601
+ devArgs.nrAic = g_deviceArgs.nrAic;
602
+ devArgs.nrAiv = g_deviceArgs.nrAiv;
603
+ devArgs.corePmuRegAddr = g_deviceArgs.corePmuRegAddr;
604
+ devArgs.corePmuAddr = g_deviceArgs.corePmuAddr;
605
+ devArgs.taskWastTime = g_deviceArgs.taskWastTime;
606
+ devArgs.pmuEventAddr = g_deviceArgs.pmuEventAddr;
607
+ devArgs.aicpuPerfAddr = g_deviceArgs.aicpuPerfAddr;
608
+ devArgs.devDfxArgAddr = g_deviceArgs.devDfxArgAddr;
595
609
  }
Apply suggestion
likedislike
Incorrect?
596+ 
597+bool KernelBinary::GetEnableDumpDevPref()
598+{
599+ InitDeviceArgs();
600+ return g_deviceArgs.aicpuPerfAddr != 0;
601+}
602+ 
398} // namespace npu::tile_fwk::dynamic603} // namespace npu::tile_fwk::dynamic
@@ -29,6 +29,8 @@
29#include "machine/utils/dynamic/dev_encode_tensor.h"29#include "machine/utils/dynamic/dev_encode_tensor.h"
30#include "machine/utils/machine_ws_intf.h"30#include "machine/utils/machine_ws_intf.h"
31#include "adapter/api/runtime_define.h"31#include "adapter/api/runtime_define.h"
32+#include "machine/runtime/runner/host_prof.h"
33+#include "machine/runtime/runner/device_perf.h"
32 34 
33namespace npu::tile_fwk::dynamic {35namespace npu::tile_fwk::dynamic {
34 36 
@@ -37,6 +39,10 @@ public:
37 KernelBinary(std::shared_ptr<Function> func, std::vector<std::shared_ptr<Function>> pinnedGraph = {});39 KernelBinary(std::shared_ptr<Function> func, std::vector<std::shared_ptr<Function>> pinnedGraph = {});
38 ~KernelBinary();40 ~KernelBinary();
39 41 
42+ static void SetAiCpuSoOverride(std::vector<uint8_t> soBytes);
43+ static void InitMetaData(DeviceArgs& devArgs);
44+ static bool GetEnableDumpDevPref();
45+ 
40 ToSubMachineConfig& GetMachineConfig();46 ToSubMachineConfig& GetMachineConfig();
41 47 
42 uint8_t* FindCtrlFlowCache(std::vector<std::vector<int64_t>>& inputs, bool isOriginShape);48 uint8_t* FindCtrlFlowCache(std::vector<std::vector<int64_t>>& inputs, bool isOriginShape);
@@ -77,6 +83,12 @@ private:
77 void InitLaunchArgs();83 void InitLaunchArgs();
78 void RefreshRuntimeDynamicCellMatchMeta(uint64_t needBytes);84 void RefreshRuntimeDynamicCellMatchMeta(uint64_t needBytes);
79 85 
86+ static void InitDeviceArgs();
87+ static int InitDeviceArgs(DeviceArgs& args);
88+ static int InitDeviceArgsCore(DeviceArgs& args);
89+ static void GetAicoreRegs(const ArchInfo archInfo, std::vector<int64_t>& regs, std::vector<int64_t>& regsPmu);
90+ static void InitAiCpuSoBin(DeviceArgs& devArgs);
91+ 
80 std::shared_ptr<Function> dynFunc;92 std::shared_ptr<Function> dynFunc;
81 std::vector<std::shared_ptr<Function>> pinnedGraph_;93 std::vector<std::shared_ptr<Function>> pinnedGraph_;
82 DyndevFunctionAttribute* dynAttr{nullptr};94 DyndevFunctionAttribute* dynAttr{nullptr};
@@ -310,7 +310,7 @@ private:
310 CheckDeviceId();310 CheckDeviceId();
311 311 
312 auto kernel = std::make_unique<KernelBinary>(Program::GetInstance().GetFunctionSharedPtr(function_));312 auto kernel = std::make_unique<KernelBinary>(Program::GetInstance().GetFunctionSharedPtr(function_));
313- DeviceRunner::Get().SetHostProfFunction(function_);313+ HostProf::GetInstance().SetProfFunction(function_);
314 314 
315 MemoryHelper memoryHelper(false);315 MemoryHelper memoryHelper(false);
316 std::vector<DeviceTensorData> inputList;316 std::vector<DeviceTensorData> inputList;
@@ -338,11 +338,10 @@ private:
338 rc = DeviceLauncher::LaunchKernel(aicoreStream, ctrlFlowCache, kernel.get(), wsAddr, tensors, false, 0);338 rc = DeviceLauncher::LaunchKernel(aicoreStream, ctrlFlowCache, kernel.get(), wsAddr, tensors, false, 0);
339 EXPECT_EQ(rc, 0);339 EXPECT_EQ(rc, 0);
340 340 
341- rc = DeviceRunner::Get().DynamicLaunchSynchronize(GetContextScheStream(), GetContextCtrlStream(),341+ rc = DeviceLauncher::DynamicLaunchSynchronize(GetContextScheStream(), GetContextCtrlStream(), aicoreStream);
342- aicoreStream);
343 EXPECT_EQ(rc, 0);342 EXPECT_EQ(rc, 0);
344 bool debugEnable = config::GetDebugOption<int64_t>(CFG_RUNTIME_DBEUG_MODE) == CFG_DEBUG_ALL;343 bool debugEnable = config::GetDebugOption<int64_t>(CFG_RUNTIME_DBEUG_MODE) == CFG_DEBUG_ALL;
345- DeviceRunner::Get().SyncProfData(debugEnable);344+ DevicePerf::GetInstance().SyncProfData(debugEnable);
346 }345 }
347 CopyBackInOut(memoryHelper, inputs, outputs);346 CopyBackInOut(memoryHelper, inputs, outputs);
348 if (IsDumpTensorEnable()) {347 if (IsDumpTensorEnable()) {
@@ -15,7 +15,7 @@
15 15 
16#include "gtest/gtest.h"16#include "gtest/gtest.h"
17#include <cstdlib>17#include <cstdlib>
18-#include "machine/runtime/runner/device_runner.h"18+#include "machine/runtime/runner/kernel_binary.h"
19#include <memory>19#include <memory>
20#include "machine/runtime/launcher/device_launcher.h"20#include "machine/runtime/launcher/device_launcher.h"
21#include "machine/runtime/runner/host_prof.h"21#include "machine/runtime/runner/host_prof.h"
@@ -71,12 +71,11 @@ TEST_F(TestDynamicDeviceRunner, DynPyptoKernelServerInit_ReturnsErrorWhenKernelN
71 71 
72TEST_F(TestDynamicDeviceRunner, TestInitArgs)72TEST_F(TestDynamicDeviceRunner, TestInitArgs)
73{73{
74- npu::tile_fwk::DeviceRunner runner;
75 [[maybe_unused]] DeviceArgs args;74 [[maybe_unused]] DeviceArgs args;
76 args.nrAic = 2;75 args.nrAic = 2;
77 args.nrAiv = 2;76 args.nrAiv = 2;
78 args.nrValidAic = args.nrAic;77 args.nrValidAic = args.nrAic;
79- runner.SyncProfData(false);78+ npu::tile_fwk::DevicePerf::GetInstance().SyncProfData(false);
80}79}
81 80 
82TEST_F(TestDynamicDeviceRunner, test_pypto_kernel_server_null)81TEST_F(TestDynamicDeviceRunner, test_pypto_kernel_server_null)
@@ -94,7 +93,7 @@ TEST_F(TestDynamicDeviceRunner, test_dump_device_perf)
94 setenv("DUMP_DEVICE_PERF", "true", 1);93 setenv("DUMP_DEVICE_PERF", "true", 1);
95 DeviceArgs devKernelArgs;94 DeviceArgs devKernelArgs;
96 config::SetOptionsNg<int64_t>("debug.runtime_debug_mode", 1);95 config::SetOptionsNg<int64_t>("debug.runtime_debug_mode", 1);
97- npu::tile_fwk::DeviceRunner::Get().InitMetaData(devKernelArgs);96+ npu::tile_fwk::dynamic::KernelBinary::InitMetaData(devKernelArgs);
98 devKernelArgs.nrAic = 1;97 devKernelArgs.nrAic = 1;
99 devKernelArgs.nrAiv = 2;98 devKernelArgs.nrAiv = 2;
100 devKernelArgs.nrValidAic = 1;99 devKernelArgs.nrValidAic = 1;
@@ -20,7 +20,8 @@
20#include "tilefwk/tilefwk.h"20#include "tilefwk/tilefwk.h"
21#include "tilefwk/platform.h"21#include "tilefwk/platform.h"
22#include "interface/inner/tilefwk.h"22#include "interface/inner/tilefwk.h"
23-#include "machine/runtime/runner/device_runner.h"23+#include "machine/runtime/runner/host_prof.h"
24+#include "machine/runtime/runner/device_perf.h"
24#include "machine/runtime/runner/pmu_common.h"25#include "machine/runtime/runner/pmu_common.h"
25#include "machine/utils/machine_ws_intf.h"26#include "machine/utils/machine_ws_intf.h"
26#define private public27#define private public
@@ -32,6 +33,7 @@
32#include "machine/device/dynamic/aicore_manager.h"33#include "machine/device/dynamic/aicore_manager.h"
33#include "machine/device/tilefwk/aicpu_common.h"34#include "machine/device/tilefwk/aicpu_common.h"
34#include "machine/device/dynamic/device_sche_context.h"35#include "machine/device/dynamic/device_sche_context.h"
36+#include "machine/runtime/launcher/device_launcher_types.h"
35 37 
36using namespace npu::tile_fwk;38using namespace npu::tile_fwk;
37class TestDeviceRunner : public testing::Test {39class TestDeviceRunner : public testing::Test {
@@ -47,7 +49,6 @@ public:
47 49 
48TEST_F(TestDeviceRunner, test_set_pmu_event)50TEST_F(TestDeviceRunner, test_set_pmu_event)
49{51{
50- // auto runner = npu::tile_fwk::DeviceRunner::Get();
51 std::vector<int64_t> pmuEvtType;52 std::vector<int64_t> pmuEvtType;
52 for (int i = 0; i < 9; i++) {53 for (int i = 0; i < 9; i++) {
53 setenv("PYPTO_PROF_PMU_EVENT_TYPE", std::to_string(i).c_str(), 1);54 setenv("PYPTO_PROF_PMU_EVENT_TYPE", std::to_string(i).c_str(), 1);
@@ -166,7 +167,7 @@ TEST_F(TestDeviceRunner, test_create_proflevel)
166 167 
167TEST_F(TestDeviceRunner, KernelLaunchInfo_Constructor)168TEST_F(TestDeviceRunner, KernelLaunchInfo_Constructor)
168{169{
169- KernelLaunchInfo info(nullptr, nullptr, nullptr, 24, 6);170+ npu::tile_fwk::dynamic::KernelLaunchInfo info(nullptr, nullptr, nullptr, 24, 6);
170 EXPECT_EQ(info.schedStream, nullptr);171 EXPECT_EQ(info.schedStream, nullptr);
171 EXPECT_EQ(info.ctrlStream, nullptr);172 EXPECT_EQ(info.ctrlStream, nullptr);
172 EXPECT_EQ(info.aicoreStream, nullptr);173 EXPECT_EQ(info.aicoreStream, nullptr);
@@ -176,90 +177,40 @@ TEST_F(TestDeviceRunner, KernelLaunchInfo_Constructor)
176 EXPECT_FALSE(info.isCaptureActivate);177 EXPECT_FALSE(info.isCaptureActivate);
177}178}
178 179 
179-TEST_F(TestDeviceRunner, DeviceRunner_InitMetaData)180+TEST_F(TestDeviceRunner, HostProf_GetHostProfType_Default) { EXPECT_EQ(HostProf::GetInstance().GetHostProfType(), 0u); }
181+ 
182+TEST_F(TestDeviceRunner, HostProf_SetProfFunction_Null) { HostProf::GetInstance().SetProfFunction(nullptr); }
183+ 
184+TEST_F(TestDeviceRunner, DevicePerf_SetDebugEnable) { DevicePerf::GetInstance().SetDebugEnable(); }
185+ 
186+TEST_F(TestDeviceRunner, HostProf_ReportHostProfInfo_NoProf)
180{187{
181- DeviceArgs srcArgs{};188+ HostProf::GetInstance().ReportHostProfInfo(nullptr, 100, 24, 1, false);
182- srcArgs.runtimeDataRingBufferAddr = 0x1000;
183- srcArgs.sharedBuffer = 0x2000;
184- srcArgs.coreRegAddr = 0x3000;
185- srcArgs.nrAic = 24;
186- srcArgs.nrAiv = 48;
187- srcArgs.corePmuRegAddr = 0x4000;
188- srcArgs.corePmuAddr = 0x5000;
189- srcArgs.taskWastTime = 0x6000;
190- srcArgs.pmuEventAddr = 0x7000;
191- srcArgs.aicpuPerfAddr = 0x8000;
192- srcArgs.devDfxArgAddr = 0x9000;
193- 
194- DeviceRunner runner;
195- runner.args_ = srcArgs;
196- 
197- DeviceArgs dstArgs{};
198- runner.InitMetaData(dstArgs);
199- 
200- EXPECT_EQ(dstArgs.runtimeDataRingBufferAddr, 0x1000u);
201- EXPECT_EQ(dstArgs.sharedBuffer, 0x2000u);
202- EXPECT_EQ(dstArgs.coreRegAddr, 0x3000u);
203- EXPECT_EQ(dstArgs.nrAic, 24u);
204- EXPECT_EQ(dstArgs.nrAiv, 48u);
205- EXPECT_EQ(dstArgs.corePmuRegAddr, 0x4000u);
206- EXPECT_EQ(dstArgs.corePmuAddr, 0x5000u);
207- EXPECT_EQ(dstArgs.taskWastTime, 0x6000u);
208- EXPECT_EQ(dstArgs.pmuEventAddr, 0x7000u);
209- EXPECT_EQ(dstArgs.aicpuPerfAddr, 0x8000u);
210- EXPECT_EQ(dstArgs.devDfxArgAddr, 0x9000u);
211}189}
212 190 
213-TEST_F(TestDeviceRunner, DeviceRunner_GetHostProfType_Default)191+TEST_F(TestDeviceRunner, HostProf_ReportHostProfInfo_WithProf)
214{192{
215- DeviceRunner runner;
216- EXPECT_EQ(runner.GetHostProfType(), 0u);
217-}
218- 
219-TEST_F(TestDeviceRunner, DeviceRunner_SetHostProfFunction_Null)
220-{
221- DeviceRunner runner;
222- runner.SetHostProfFunction(nullptr);
223-}
224- 
225-TEST_F(TestDeviceRunner, DeviceRunner_SetDebugEnable)
226-{
227- DeviceRunner runner;
228- runner.SetDebugEnable();
229-}
230- 
231-TEST_F(TestDeviceRunner, DeviceRunner_ReportHostProfInfo_NoProf)
232-{
233- DeviceRunner runner;
234- runner.ReportHostProfInfo(nullptr, 100, 24, 1, false);
235-}
236- 
237-TEST_F(TestDeviceRunner, DeviceRunner_ReportHostProfInfo_WithProf)
238-{
239- DeviceRunner runner;
240 HostProf::profSwitch_ = MSPF_TASK_TIME_L1_MASK | MSPF_TASK_TIME_L2_MASK;193 HostProf::profSwitch_ = MSPF_TASK_TIME_L1_MASK | MSPF_TASK_TIME_L2_MASK;
241 HostProf::profType_ = MSPF_COMMANDHANDLE_TYPE_START;194 HostProf::profType_ = MSPF_COMMANDHANDLE_TYPE_START;
242- runner.ReportHostProfInfo(nullptr, 100, 24, 1, false);195+ HostProf::GetInstance().ReportHostProfInfo(nullptr, 100, 24, 1, false);
243 HostProf::profSwitch_ = 0;196 HostProf::profSwitch_ = 0;
244 HostProf::profType_ = 0;197 HostProf::profType_ = 0;
245}198}
246 199 
247-TEST_F(TestDeviceRunner, DeviceRunner_ReportHostProfInfo_WithCore)200+TEST_F(TestDeviceRunner, HostProf_ReportHostProfInfo_WithCore)
248{201{
249- DeviceRunner runner;
250 HostProf::profSwitch_ = MSPF_TASK_TIME_L1_MASK | MSPF_TASK_TIME_L2_MASK;202 HostProf::profSwitch_ = MSPF_TASK_TIME_L1_MASK | MSPF_TASK_TIME_L2_MASK;
251 HostProf::profType_ = MSPF_COMMANDHANDLE_TYPE_START;203 HostProf::profType_ = MSPF_COMMANDHANDLE_TYPE_START;
252- runner.ReportHostProfInfo(nullptr, 100, 24, 1, true);204+ HostProf::GetInstance().ReportHostProfInfo(nullptr, 100, 24, 1, true);
253 HostProf::profSwitch_ = 0;205 HostProf::profSwitch_ = 0;
254 HostProf::profType_ = 0;206 HostProf::profType_ = 0;
255}207}
256 208 
257-TEST_F(TestDeviceRunner, DeviceRunner_ReportHostProfInfo_MixAic)209+TEST_F(TestDeviceRunner, HostProf_ReportHostProfInfo_MixAic)
258{210{
259- DeviceRunner runner;
260 HostProf::profSwitch_ = MSPF_TASK_TIME_L1_MASK | MSPF_TASK_TIME_L2_MASK;211 HostProf::profSwitch_ = MSPF_TASK_TIME_L1_MASK | MSPF_TASK_TIME_L2_MASK;
261 HostProf::profType_ = MSPF_COMMANDHANDLE_TYPE_START;212 HostProf::profType_ = MSPF_COMMANDHANDLE_TYPE_START;
262- runner.ReportHostProfInfo(nullptr, 100, 24, MSPF_GE_TASK_TYPE_MIX_AIC, false);213+ HostProf::GetInstance().ReportHostProfInfo(nullptr, 100, 24, MSPF_GE_TASK_TYPE_MIX_AIC, false);
263 HostProf::profSwitch_ = 0;214 HostProf::profSwitch_ = 0;
264 HostProf::profType_ = 0;215 HostProf::profType_ = 0;
265}216}