Merged
event-leak-9.2.0 #7854
LordOfMysteriesAdd on 2 days ago
event-leak-9.2.0 #7854
Merged
LordOfMysteriesAdd on 2 days ago
Total: 13 files changed+135-10
@@ -148,6 +148,18 @@ AclError AclRtCreateEventExWithFlag(AclRtEvent* event, uint32_t flag)
148 return StubRtCreateEventExWithFlag(event, flag);148 return StubRtCreateEventExWithFlag(event, flag);
149}149}
150 150 
151+AclError AclRtDestroyEvent(AclRtEvent event)
152+{
153+#ifdef BUILD_WITH_CANN
154+ void* func = AdapterManager::Instance().GetAclAdapter().GetFunction(AclFunc::RtDestroyEvent);
155+ if (func != nullptr) {
156+ aclError (*aclFunc)(aclrtEvent) = reinterpret_cast<aclError (*)(aclrtEvent)>(func);
157+ return aclFunc(event);
158+ }
159+#endif
160+ return StubRtDestroyEvent(event);
161+}
162+ 
151AclError AclRtStreamWaitEvent(AclRtStream stream, AclRtEvent event)163AclError AclRtStreamWaitEvent(AclRtStream stream, AclRtEvent event)
152{164{
153#ifdef BUILD_WITH_CANN165#ifdef BUILD_WITH_CANN
@@ -26,6 +26,7 @@ AclError AclRtResetDevice(int32_t deviceId);
26AclError AclRtCreateEvent(AclRtEvent* event);26AclError AclRtCreateEvent(AclRtEvent* event);
27AclError AclRtRecordEvent(AclRtEvent event, AclRtStream stream);27AclError AclRtRecordEvent(AclRtEvent event, AclRtStream stream);
28AclError AclRtCreateEventExWithFlag(AclRtEvent* event, uint32_t flag);28AclError AclRtCreateEventExWithFlag(AclRtEvent* event, uint32_t flag);
29+AclError AclRtDestroyEvent(AclRtEvent event);
29AclError AclRtStreamWaitEvent(AclRtStream stream, AclRtEvent event);30AclError AclRtStreamWaitEvent(AclRtStream stream, AclRtEvent event);
30AclError AclRtGetStreamResLimit(AclRtStream stream, AclRtDevResLimitType type, uint32_t* value);31AclError AclRtGetStreamResLimit(AclRtStream stream, AclRtDevResLimitType type, uint32_t* value);
31AclError AclRtGetStreamAttribute(AclRtStream stream, AclRtStreamAttr stmAttrType, AclRtStreamAttrValue* value);32AclError AclRtGetStreamAttribute(AclRtStream stream, AclRtStreamAttr stmAttrType, AclRtStreamAttrValue* value);
@@ -28,6 +28,7 @@ enum class AclFunc {
28 RtCreateEvent,28 RtCreateEvent,
29 RtRecordEvent,29 RtRecordEvent,
30 RtCreateEventExWithFlag,30 RtCreateEventExWithFlag,
31+ RtDestroyEvent,
31 RtStreamWaitEvent,32 RtStreamWaitEvent,
32 RtGetStreamResLimit,33 RtGetStreamResLimit,
33 RtGetStreamAttribute,34 RtGetStreamAttribute,
@@ -57,6 +58,7 @@ const std::map<AclFunc, std::string> kAclFuncStrMap{
57 {AclFunc::RtCreateEvent, "aclrtCreateEvent"},58 {AclFunc::RtCreateEvent, "aclrtCreateEvent"},
58 {AclFunc::RtRecordEvent, "aclrtRecordEvent"},59 {AclFunc::RtRecordEvent, "aclrtRecordEvent"},
59 {AclFunc::RtCreateEventExWithFlag, "aclrtCreateEventExWithFlag"},60 {AclFunc::RtCreateEventExWithFlag, "aclrtCreateEventExWithFlag"},
61+ {AclFunc::RtDestroyEvent, "aclrtDestroyEvent"},
60 {AclFunc::RtStreamWaitEvent, "aclrtStreamWaitEvent"},62 {AclFunc::RtStreamWaitEvent, "aclrtStreamWaitEvent"},
61 {AclFunc::RtGetStreamResLimit, "aclrtGetStreamResLimit"},63 {AclFunc::RtGetStreamResLimit, "aclrtGetStreamResLimit"},
62 {AclFunc::RtGetStreamAttribute, "aclrtGetStreamAttribute"},64 {AclFunc::RtGetStreamAttribute, "aclrtGetStreamAttribute"},
@@ -94,6 +94,13 @@ AclError StubRtCreateEventExWithFlag(AclRtEvent* event, uint32_t flag)
94 return ACLRT_SUCCESS;94 return ACLRT_SUCCESS;
95}95}
96 96 
97+AclError StubRtDestroyEvent(AclRtEvent event)
98+{
99+ ADAPTER_LOGD("Enter stub function of AclRtDestroyEvent.");
100+ (void)event;
101+ return ACLRT_SUCCESS;
102+}
103+ 
97AclError StubRtStreamWaitEvent(AclRtStream stream, AclRtEvent event)104AclError StubRtStreamWaitEvent(AclRtStream stream, AclRtEvent event)
98{105{
99 ADAPTER_LOGD("Enter stub function of AclRtStreamWaitEvent.");106 ADAPTER_LOGD("Enter stub function of AclRtStreamWaitEvent.");
@@ -28,6 +28,7 @@ AclError StubRtResetDevice(int32_t deviceId);
28AclError StubRtCreateEvent(AclRtEvent* event);28AclError StubRtCreateEvent(AclRtEvent* event);
29AclError StubRtRecordEvent(AclRtEvent event, AclRtStream stream);29AclError StubRtRecordEvent(AclRtEvent event, AclRtStream stream);
30AclError StubRtCreateEventExWithFlag(AclRtEvent* event, uint32_t flag);30AclError StubRtCreateEventExWithFlag(AclRtEvent* event, uint32_t flag);
31+AclError StubRtDestroyEvent(AclRtEvent event);
31AclError StubRtStreamWaitEvent(AclRtStream stream, AclRtEvent event);32AclError StubRtStreamWaitEvent(AclRtStream stream, AclRtEvent event);
32AclError StubRtGetStreamResLimit(AclRtStream stream, AclRtDevResLimitType type, uint32_t* value);33AclError StubRtGetStreamResLimit(AclRtStream stream, AclRtDevResLimitType type, uint32_t* value);
33AclError StubRtGetStreamAttribute(AclRtStream stream, AclRtStreamAttr stmAttrType, AclRtStreamAttrValue* value);34AclError StubRtGetStreamAttribute(AclRtStream stream, AclRtStreamAttr stmAttrType, AclRtStreamAttrValue* value);
@@ -16,9 +16,10 @@
16#include "machine/runtime/context/stream_context.h"16#include "machine/runtime/context/stream_context.h"
17 17 
18namespace npu::tile_fwk {18namespace npu::tile_fwk {
19+// AICPU does not support concurrent multi-stream execution.
19StreamContext& GetStreamContext()20StreamContext& GetStreamContext()
20{21{
21- static thread_local StreamContext context;22+ static StreamContext context;
22 return context;23 return context;
23}24}
24 25 
@@ -134,6 +134,8 @@ bool DeviceLauncher::inited_ = false;
134std::vector<uint8_t> DeviceLauncher::tensorInfo_(kDefaultTensorinfoSize);134std::vector<uint8_t> DeviceLauncher::tensorInfo_(kDefaultTensorinfoSize);
135std::unordered_map<Function*, DeviceLauncher::DeviceRunCacheInfo> DeviceLauncher::cacheInfoDict_;135std::unordered_map<Function*, DeviceLauncher::DeviceRunCacheInfo> DeviceLauncher::cacheInfoDict_;
136std::atomic<int64_t> DeviceLauncher::sequence_(0);136std::atomic<int64_t> DeviceLauncher::sequence_(0);
137+AclRtEvent DeviceLauncher::preSyncEvent_ = nullptr;
138+std::mutex DeviceLauncher::preSyncEventMutex_;
137 139 
138void DeviceLauncher::CheckAscendDriverVersionOnboard() { AscendDriverVersionGate::EnsureDriverVersionForOnboardOnce(); }140void DeviceLauncher::CheckAscendDriverVersionOnboard() { AscendDriverVersionGate::EnsureDriverVersionForOnboardOnce(); }
139 141 
@@ -476,29 +478,60 @@ int DeviceLauncher::LaunchSyncTask(AclRtStream aicoreStream, bool isCaptureMode,
476 return RunPreSync(IsAicoreResolveEnabled() ? nullptr : schedStream, ctrlStream, aicoreStream);478 return RunPreSync(IsAicoreResolveEnabled() ? nullptr : schedStream, ctrlStream, aicoreStream);
477}479}
478 480 
481+int DeviceLauncher::GetOrCreatePreSyncEvent(AclRtEvent& event)
482+{
483+ if (preSyncEvent_ == nullptr) {
484+ AclRtEvent created = nullptr;
485+ AclError ret = AclRtCreateEventExWithFlag(&created, ACL_EVENT_SYNC);
486+ if (ret != ACLRT_SUCCESS) {
487+ MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtCreateEvent failed %d\n", ret);
488+ return -1;
489+ }
490+ preSyncEvent_ = created;
491+ }
492+ event = preSyncEvent_;
493+ return 0;
494+}
495+ 
496+void DeviceLauncher::Finalize()
497+{
498+ std::lock_guard<std::mutex> lock(preSyncEventMutex_);
499+ if (preSyncEvent_ != nullptr) {
500+ AclError ret = AclRtDestroyEvent(preSyncEvent_);
501+ if (ret != ACLRT_SUCCESS) {
502+ MACHINE_LOGW("AclRtDestroyEvent failed %d\n", ret);
503+ }
504+ preSyncEvent_ = nullptr;
505+ }
506+}
507+ 
479int DeviceLauncher::RunPreSync(RtStream scheStream, RtStream ctrlStream, RtStream aicoreStream)508int DeviceLauncher::RunPreSync(RtStream scheStream, RtStream ctrlStream, RtStream aicoreStream)
480{509{
481- AclRtEvent event;510+ // The pre-sync event is created once per process and reused by every launch, so it must be
482- if (AclRtCreateEventExWithFlag(&event, ACL_EVENT_SYNC) < 0) {511+ // serialized: otherwise a later launch could record the shared event before the previous
483- MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtCreateEvent failed.");512+ // launch has enqueued its waits.
484- return -1;513+ std::lock_guard<std::mutex> lock(preSyncEventMutex_);
485- }514+ AclRtEvent event = nullptr;
486- int rc = AclRtRecordEvent(event, aicoreStream);515+ int rc = GetOrCreatePreSyncEvent(event);
487 if (rc < 0) {516 if (rc < 0) {
517+ return rc;
518+ }
519+ rc = AclRtRecordEvent(event, aicoreStream);
520+ if (rc != ACLRT_SUCCESS) {
488 MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtRecordEvent failed %d\n", rc);521 MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtRecordEvent failed %d\n", rc);
489 return rc;522 return rc;
490 }523 }
491 524 
492 if (scheStream) {525 if (scheStream) {
493 rc = AclRtStreamWaitEvent(scheStream, event);526 rc = AclRtStreamWaitEvent(scheStream, event);
494- if (rc < 0) {527+ if (rc != ACLRT_SUCCESS) {
495 MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtStreamWaitEvent failed %d\n", rc);528 MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtStreamWaitEvent failed %d\n", rc);
496 return rc;529 return rc;
497 }530 }
498 }531 }
499 532 
500 rc = AclRtStreamWaitEvent(ctrlStream, event);533 rc = AclRtStreamWaitEvent(ctrlStream, event);
501- if (rc < 0) {534+ if (rc != ACLRT_SUCCESS) {
502 MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtStreamWaitEvent failed %d\n", rc);535 MACHINE_LOGE(RtErr::RT_EVENT_FAILED, "AclRtStreamWaitEvent failed %d\n", rc);
503 return rc;536 return rc;
504 }537 }
@@ -18,6 +18,7 @@
18 18 
19#include <cinttypes>19#include <cinttypes>
20#include <atomic>20#include <atomic>
21+#include <mutex>
21#include <unordered_map>22#include <unordered_map>
22#include "tilefwk/data_type.h"23#include "tilefwk/data_type.h"
23#include "tilefwk/tilefwk.h"24#include "tilefwk/tilefwk.h"
@@ -513,10 +514,13 @@ public:
513 static void DumpIOTensorsWithCann(AclRtStream stream, std::vector<DeviceTensorData>& tensors,514 static void DumpIOTensorsWithCann(AclRtStream stream, std::vector<DeviceTensorData>& tensors,
514 const std::string& funcName);515 const std::string& funcName);
515 static int RunPreSync(RtStream scheStream, RtStream ctrlStream, RtStream aicoreStream);516 static int RunPreSync(RtStream scheStream, RtStream ctrlStream, RtStream aicoreStream);
517+ static void Finalize();
516 518 
517private:519private:
518 static void DataDumpInit();520 static void DataDumpInit();
519 static void DataDumpUnInit();521 static void DataDumpUnInit();
522+ // Caller must hold preSyncEventMutex_.
523+ static int GetOrCreatePreSyncEvent(AclRtEvent& event);
520 struct DeviceRunCacheInfo {524 struct DeviceRunCacheInfo {
521 /* By default: devProg cache is enabled */525 /* By default: devProg cache is enabled */
522 bool devProgEnabled{true};526 bool devProgEnabled{true};
@@ -527,6 +531,9 @@ private:
527 static std::vector<uint8_t> tensorInfo_;531 static std::vector<uint8_t> tensorInfo_;
528 static std::unordered_map<Function*, DeviceRunCacheInfo> cacheInfoDict_;532 static std::unordered_map<Function*, DeviceRunCacheInfo> cacheInfoDict_;
529 static std::atomic<int64_t> sequence_;533 static std::atomic<int64_t> sequence_;
534+ // Process-level pre-sync event, created once and reused by every launch.
535+ static AclRtEvent preSyncEvent_;
536+ static std::mutex preSyncEventMutex_;
530};537};
531} // namespace npu::tile_fwk::dynamic538} // namespace npu::tile_fwk::dynamic
532#endif // SRC_MACHINE_DEVICE_LAUNCHER_H539#endif // SRC_MACHINE_DEVICE_LAUNCHER_H
@@ -46,7 +46,11 @@ int HasInplaceArgs(Function* function) { return DeviceLauncher::HasInplaceArgs(f
46 46 
47void DeviceLauncherInit() { DeviceLauncherContext::Get().Initialize(); }47void DeviceLauncherInit() { DeviceLauncherContext::Get().Initialize(); }
48 48 
49-void DeviceLauncherFini() { DeviceLauncherContext::Get().Finalize(); }49+void DeviceLauncherFini()
50+{
51+ DeviceLauncher::Finalize();
52+ DeviceLauncherContext::Get().Finalize();
53+}
50 54 
51void CopyDevToHost(const DeviceTensorData& devTensor, DeviceTensorData& hostTensor)55void CopyDevToHost(const DeviceTensorData& devTensor, DeviceTensorData& hostTensor)
52{56{
@@ -95,6 +95,9 @@ KernelBinary::KernelBinary(std::shared_ptr<Function> func, std::vector<std::shar
95 95 
96KernelBinary::~KernelBinary()96KernelBinary::~KernelBinary()
97{97{
98+ // graphEvents_ are referenced by the captured model and may still be replayed after this
99+ // per-launch KernelBinary is destroyed, so they are released together with the model.
100+ DestroyRingEvents(false);
98 if (runtimeDynamicCellMatchOwned_ && runtimeDynamicCellMatchAddr_ != 0) {101 if (runtimeDynamicCellMatchOwned_ && runtimeDynamicCellMatchAddr_ != 0) {
99 DevMemoryPool::Instance().FreeDevAddr(reinterpret_cast<uint8_t*>(runtimeDynamicCellMatchAddr_));102 DevMemoryPool::Instance().FreeDevAddr(reinterpret_cast<uint8_t*>(runtimeDynamicCellMatchAddr_));
100 }103 }
@@ -154,6 +157,22 @@ bool KernelBinary::EnsureRingEventsCreated(bool isCapture)
154 return true;157 return true;
155}158}
156 159 
160+void KernelBinary::DestroyRingEvents(bool isCapture)
161+{
162+ AclRtEvent* events = isCapture ? graphEvents_ : eagerEvents_;
163+ bool& created = isCapture ? graphEventsCreated_ : eagerEventsCreated_;
164+ for (int64_t i = 0; i < kRingPingPongCount; ++i) {
165+ if (events[i] != nullptr) {
166+ AclError ret = AclRtDestroyEvent(events[i]);
167+ if (ret != ACLRT_SUCCESS) {
168+ MACHINE_LOGW("AclRtDestroyEvent failed for ring event %ld, ret %d.\n", i, ret);
169+ }
170+ events[i] = nullptr;
171+ }
172+ }
173+ created = false;
174+}
175+ 
157uint8_t* KernelBinary::FindCtrlFlowCache(std::vector<std::vector<int64_t>>& inputs, bool isOriginShape)176uint8_t* KernelBinary::FindCtrlFlowCache(std::vector<std::vector<int64_t>>& inputs, bool isOriginShape)
158{177{
159 int64_t inHash = ControlFlowCache::Hash(inputs);178 int64_t inHash = ControlFlowCache::Hash(inputs);
@@ -108,6 +108,7 @@ private:
108 108 
109 void BeginRingEpoch(uintptr_t epoch, bool isCapture);109 void BeginRingEpoch(uintptr_t epoch, bool isCapture);
110 bool EnsureRingEventsCreated(bool isCapture);110 bool EnsureRingEventsCreated(bool isCapture);
111+ void DestroyRingEvents(bool isCapture);
111 112 
112 static void InitDeviceArgs();113 static void InitDeviceArgs();
113 static int InitDeviceArgs(DeviceArgs& args);114 static int InitDeviceArgs(DeviceArgs& args);
@@ -0,0 +1,36 @@
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+/*!
12+ * \file test_stream_context.cpp
13+ * \brief UT for machine/runtime/context/stream_context: the instance must stay process-wide.
14+ */
15+ 
16+#include <gtest/gtest.h>
17+ 
18+#include <thread>
19+ 
20+#include "machine/runtime/context/stream_context.h"
21+ 
22+using namespace npu::tile_fwk;
23+ 
24+TEST(StreamContextTest, InstanceIsProcessWide)
25+{
26+ StreamContext* fromMain = &GetStreamContext();
27+ StreamContext* fromWorker = nullptr;
28+ 
29+ std::thread worker([&fromWorker]() { fromWorker = &GetStreamContext(); });
30+ worker.join();
31+ 
32+ ASSERT_NE(fromWorker, nullptr);
33+ EXPECT_EQ(fromMain, fromWorker);
34+ EXPECT_EQ(fromMain->GetScheStream(), fromWorker->GetScheStream());
35+ EXPECT_EQ(fromMain->GetCtrlStream(), fromWorker->GetCtrlStream());
36+}
@@ -59,6 +59,7 @@ TEST_F(TestAdapterApi, test_acl_api)
59 CHECK_API_RET(AclRtCreateEvent(nullptr), ACLRT_SUCCESS);59 CHECK_API_RET(AclRtCreateEvent(nullptr), ACLRT_SUCCESS);
60 CHECK_API_RET(AclRtRecordEvent(nullptr, nullptr), ACLRT_SUCCESS);60 CHECK_API_RET(AclRtRecordEvent(nullptr, nullptr), ACLRT_SUCCESS);
61 CHECK_API_RET(AclRtCreateEventExWithFlag(nullptr, 0), ACLRT_SUCCESS);61 CHECK_API_RET(AclRtCreateEventExWithFlag(nullptr, 0), ACLRT_SUCCESS);
62+ CHECK_API_RET(AclRtDestroyEvent(nullptr), ACLRT_SUCCESS);
62 CHECK_API_RET(AclRtStreamWaitEvent(nullptr, nullptr), ACLRT_SUCCESS);63 CHECK_API_RET(AclRtStreamWaitEvent(nullptr, nullptr), ACLRT_SUCCESS);
63 CHECK_API_RET(AclRtGetStreamResLimit(nullptr, AclRtDevResLimitType::CUBE_CORE, nullptr), ACLRT_SUCCESS);64 CHECK_API_RET(AclRtGetStreamResLimit(nullptr, AclRtDevResLimitType::CUBE_CORE, nullptr), ACLRT_SUCCESS);
64 CHECK_API_RET(AclRtGetStreamAttribute(nullptr, AclRtStreamAttr::FAILURE_MODE, nullptr), ACLRT_SUCCESS);65 CHECK_API_RET(AclRtGetStreamAttribute(nullptr, AclRtStreamAttr::FAILURE_MODE, nullptr), ACLRT_SUCCESS);