From 0ba73da4e50d2b743fa91fbc2b1cf22cc60384a4 Mon Sep 17 00:00:00 2001 From: "Wang, Yang" Date: Mon, 11 Nov 2024 15:54:46 +0800 Subject: [PATCH 1/6] Using shared_ptr to resolve segmentation faults caused by the release order of static variables. --- src/plugins/auto/src/auto_schedule.hpp | 7 +++ src/plugins/auto/src/plugin.cpp | 70 +++++++++++++++----------- src/plugins/auto/src/plugin.hpp | 4 +- 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/plugins/auto/src/auto_schedule.hpp b/src/plugins/auto/src/auto_schedule.hpp index 5590c2085906e7..02e2a5e95841e0 100644 --- a/src/plugins/auto/src/auto_schedule.hpp +++ b/src/plugins/auto/src/auto_schedule.hpp @@ -14,6 +14,11 @@ namespace auto_plugin { class AutoSchedule : public Schedule { public: using Ptr = std::shared_ptr; + AutoSchedule(std::shared_ptr& mtx, + std::shared_ptr>>& priority_map) + : Schedule(), + m_mtx(mtx), + m_priority_map(priority_map) {} virtual ~AutoSchedule(); AutoCompileContext m_compile_context[CONTEXTNUM]; @@ -38,6 +43,8 @@ class AutoSchedule : public Schedule { std::future m_firstload_future; std::promise m_firstload_promise; bool m_exitflag = {false}; + std::shared_ptr m_mtx; + std::shared_ptr>> m_priority_map; }; } // namespace auto_plugin } // namespace ov diff --git a/src/plugins/auto/src/plugin.cpp b/src/plugins/auto/src/plugin.cpp index 06b3b7dbc947e4..5c72025029e02c 100644 --- a/src/plugins/auto/src/plugin.cpp +++ b/src/plugins/auto/src/plugin.cpp @@ -75,8 +75,9 @@ namespace { namespace ov { namespace auto_plugin { -std::mutex Plugin::m_mtx; -std::map> Plugin::m_priority_map; +std::shared_ptr Plugin::m_mtx = std::make_shared(); +std::shared_ptr>> Plugin::m_priority_map = + std::make_shared>>(); ov::SoPtr Plugin::create_context(const ov::AnyMap& remote_properties) const { OPENVINO_NOT_IMPLEMENTED; @@ -441,8 +442,9 @@ std::shared_ptr Plugin::compile_model_impl(const std::string auto_s_context->m_bind_buffer = load_config.get_property(ov::intel_auto::device_bind_buffer); auto_s_context->m_schedule_policy = load_config.get_property(ov::intel_auto::schedule_policy); std::shared_ptr impl; - std::shared_ptr scheduler = is_cumulative ? std::static_pointer_cast(std::make_shared()) : - std::static_pointer_cast(std::make_shared()); + std::shared_ptr scheduler = + is_cumulative ? std::static_pointer_cast(std::make_shared()) + : std::static_pointer_cast(std::make_shared(m_mtx, m_priority_map)); scheduler->launch(auto_s_context); ov::SoPtr device_context; try { @@ -591,19 +593,25 @@ DeviceInformation Plugin::select_device(const std::vector& me DeviceInformation last_device = valid_devices.back(); { // begin to filter devices - std::lock_guard lck(m_mtx); - for (auto && kvp : m_priority_map) { - if (kvp.first >= priority) { - continue; + if (m_mtx && m_priority_map) { + std::lock_guard lck(*m_mtx); + for (auto&& kvp : *m_priority_map) { + if (kvp.first >= priority) { + continue; + } + auto& filter_devices = kvp.second; + auto sd = std::remove_if(valid_devices.begin(), + valid_devices.end(), + [&filter_devices](const DeviceInformation& device) { + auto iter = std::find_if(filter_devices.begin(), + filter_devices.end(), + [&device](std::string uniqueName) { + return (uniqueName == device.unique_name); + }); + return iter != filter_devices.end() ? true : false; + }); + valid_devices.erase(sd, valid_devices.end()); } - auto& filter_devices = kvp.second; - auto sd = std::remove_if(valid_devices.begin(), valid_devices.end(), [&filter_devices](const DeviceInformation& device) { - auto iter = std::find_if(filter_devices.begin(), filter_devices.end(), [&device](std::string uniqueName) { - return (uniqueName == device.unique_name); - }); - return iter != filter_devices.end() ? true : false; - }); - valid_devices.erase(sd, valid_devices.end()); } } @@ -621,24 +629,26 @@ DeviceInformation Plugin::select_device(const std::vector& me return *ptr_select_device; } -void Plugin::unregister_priority(const unsigned int& priority, - const std::string& device_name) { - std::lock_guard lck(m_mtx); - auto& priority_devices = m_priority_map[priority]; - for (auto iter = priority_devices.begin(); iter != priority_devices.end();) { - if (*iter == device_name) { - priority_devices.erase(iter); - break; +void Plugin::unregister_priority(const unsigned int& priority, const std::string& device_name) { + if (m_mtx.get() && m_priority_map) { + std::lock_guard lck(*m_mtx); + auto& priority_devices = (*m_priority_map)[priority]; + for (auto iter = priority_devices.begin(); iter != priority_devices.end();) { + if (*iter == device_name) { + priority_devices.erase(iter); + break; + } + iter++; } - iter++; } } -void Plugin::register_priority(const unsigned int& priority, - const std::string& device_name) { - std::lock_guard lck(m_mtx); - auto& priority_devices = m_priority_map[priority]; - priority_devices.push_back(device_name); +void Plugin::register_priority(const unsigned int& priority, const std::string& device_name) { + if (m_mtx && m_priority_map) { + std::lock_guard lck(*m_mtx); + auto& priority_devices = (*m_priority_map)[priority]; + priority_devices.push_back(device_name); + } } std::string Plugin::get_device_list(const ov::AnyMap& properties) const { diff --git a/src/plugins/auto/src/plugin.hpp b/src/plugins/auto/src/plugin.hpp index 75ad7ce5919afb..521a87f08bea46 100644 --- a/src/plugins/auto/src/plugin.hpp +++ b/src/plugins/auto/src/plugin.hpp @@ -78,8 +78,8 @@ class Plugin : public ov::IPlugin { const std::shared_ptr& model, PluginConfig& load_config) const; std::string get_log_tag() const noexcept; - static std::mutex m_mtx; - static std::map> m_priority_map; + static std::shared_ptr m_mtx; + static std::shared_ptr>> m_priority_map; PluginConfig m_plugin_config; }; From 0bda3356a9cbf5cc5315476605f86d9e79d5ced4 Mon Sep 17 00:00:00 2001 From: "Wang, Yang" Date: Mon, 11 Nov 2024 16:11:37 +0800 Subject: [PATCH 2/6] Update. --- src/plugins/auto/src/auto_schedule.hpp | 1 + src/plugins/auto/src/schedule.cpp | 2 ++ src/plugins/auto/src/schedule.hpp | 1 + 3 files changed, 4 insertions(+) diff --git a/src/plugins/auto/src/auto_schedule.hpp b/src/plugins/auto/src/auto_schedule.hpp index 02e2a5e95841e0..4c2bf9b7756b95 100644 --- a/src/plugins/auto/src/auto_schedule.hpp +++ b/src/plugins/auto/src/auto_schedule.hpp @@ -43,6 +43,7 @@ class AutoSchedule : public Schedule { std::future m_firstload_future; std::promise m_firstload_promise; bool m_exitflag = {false}; + // hold the resource of static variable to avoid the destruction of the static variable. std::shared_ptr m_mtx; std::shared_ptr>> m_priority_map; }; diff --git a/src/plugins/auto/src/schedule.cpp b/src/plugins/auto/src/schedule.cpp index 96a815cc21c8c6..56799a481bf395 100644 --- a/src/plugins/auto/src/schedule.cpp +++ b/src/plugins/auto/src/schedule.cpp @@ -14,6 +14,8 @@ void Schedule::launch(const ScheduleContext::Ptr& context) { m_context = context; m_log_tag = context->m_log_tag; m_plugin = std::const_pointer_cast(std::dynamic_pointer_cast(context->m_plugin)); + // hold the resource of static variable to avoid the destruction of the static variable. + m_logger = Log::instance(); LOG_INFO_TAG("scheduler starting"); init(); } diff --git a/src/plugins/auto/src/schedule.hpp b/src/plugins/auto/src/schedule.hpp index 99efa3138cef00..16539dd9dfce57 100644 --- a/src/plugins/auto/src/schedule.hpp +++ b/src/plugins/auto/src/schedule.hpp @@ -50,6 +50,7 @@ class Schedule : public std::enable_shared_from_this, public ov::threa mutable std::atomic m_request_id = {0}; std::mutex m_dev_infer_mutex; std::unordered_map m_dev_infer; + std::shared_ptr m_logger; }; } // namespace auto_plugin From be9adfbe0d736a420d6121e4eb2e0a9715fee94b Mon Sep 17 00:00:00 2001 From: "Wang, Yang" Date: Mon, 11 Nov 2024 16:28:47 +0800 Subject: [PATCH 3/6] update. --- src/plugins/auto/src/auto_schedule.hpp | 8 -------- src/plugins/auto/src/common.hpp | 3 +++ src/plugins/auto/src/plugin.cpp | 4 +++- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/plugins/auto/src/auto_schedule.hpp b/src/plugins/auto/src/auto_schedule.hpp index 4c2bf9b7756b95..5590c2085906e7 100644 --- a/src/plugins/auto/src/auto_schedule.hpp +++ b/src/plugins/auto/src/auto_schedule.hpp @@ -14,11 +14,6 @@ namespace auto_plugin { class AutoSchedule : public Schedule { public: using Ptr = std::shared_ptr; - AutoSchedule(std::shared_ptr& mtx, - std::shared_ptr>>& priority_map) - : Schedule(), - m_mtx(mtx), - m_priority_map(priority_map) {} virtual ~AutoSchedule(); AutoCompileContext m_compile_context[CONTEXTNUM]; @@ -43,9 +38,6 @@ class AutoSchedule : public Schedule { std::future m_firstload_future; std::promise m_firstload_promise; bool m_exitflag = {false}; - // hold the resource of static variable to avoid the destruction of the static variable. - std::shared_ptr m_mtx; - std::shared_ptr>> m_priority_map; }; } // namespace auto_plugin } // namespace ov diff --git a/src/plugins/auto/src/common.hpp b/src/plugins/auto/src/common.hpp index 28567eb23392c4..7b9e9bb17ce16e 100644 --- a/src/plugins/auto/src/common.hpp +++ b/src/plugins/auto/src/common.hpp @@ -220,6 +220,9 @@ class ScheduleContext : public std::enable_shared_from_this { std::mutex m_fallback_mutex; SoCompiledModel m_hw_compiled_model; std::string m_model_precision; + // hold the resource of static variable to avoid the destruction of the static variable. + std::shared_ptr m_mtx; + std::shared_ptr>> m_priority_map; virtual ~ScheduleContext() = default; }; diff --git a/src/plugins/auto/src/plugin.cpp b/src/plugins/auto/src/plugin.cpp index 5c72025029e02c..29b50e6074a8d7 100644 --- a/src/plugins/auto/src/plugin.cpp +++ b/src/plugins/auto/src/plugin.cpp @@ -441,10 +441,12 @@ std::shared_ptr Plugin::compile_model_impl(const std::string auto_s_context->m_runtime_fallback = load_config.get_property(ov::intel_auto::enable_runtime_fallback); auto_s_context->m_bind_buffer = load_config.get_property(ov::intel_auto::device_bind_buffer); auto_s_context->m_schedule_policy = load_config.get_property(ov::intel_auto::schedule_policy); + auto_s_context->m_mtx = m_mtx; + auto_s_context->m_priority_map = m_priority_map; std::shared_ptr impl; std::shared_ptr scheduler = is_cumulative ? std::static_pointer_cast(std::make_shared()) - : std::static_pointer_cast(std::make_shared(m_mtx, m_priority_map)); + : std::static_pointer_cast(std::make_shared()); scheduler->launch(auto_s_context); ov::SoPtr device_context; try { From 4c2fd13cba8bb1356c67c54918670bb8d9d78049 Mon Sep 17 00:00:00 2001 From: "Wang, Yang" Date: Mon, 11 Nov 2024 16:33:26 +0800 Subject: [PATCH 4/6] update. --- src/plugins/auto/src/common.hpp | 1 + src/plugins/auto/src/schedule.cpp | 2 -- src/plugins/auto/src/schedule.hpp | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/plugins/auto/src/common.hpp b/src/plugins/auto/src/common.hpp index 7b9e9bb17ce16e..16e5933e934255 100644 --- a/src/plugins/auto/src/common.hpp +++ b/src/plugins/auto/src/common.hpp @@ -223,6 +223,7 @@ class ScheduleContext : public std::enable_shared_from_this { // hold the resource of static variable to avoid the destruction of the static variable. std::shared_ptr m_mtx; std::shared_ptr>> m_priority_map; + std::shared_ptr m_logger = Log::instance(); virtual ~ScheduleContext() = default; }; diff --git a/src/plugins/auto/src/schedule.cpp b/src/plugins/auto/src/schedule.cpp index 56799a481bf395..96a815cc21c8c6 100644 --- a/src/plugins/auto/src/schedule.cpp +++ b/src/plugins/auto/src/schedule.cpp @@ -14,8 +14,6 @@ void Schedule::launch(const ScheduleContext::Ptr& context) { m_context = context; m_log_tag = context->m_log_tag; m_plugin = std::const_pointer_cast(std::dynamic_pointer_cast(context->m_plugin)); - // hold the resource of static variable to avoid the destruction of the static variable. - m_logger = Log::instance(); LOG_INFO_TAG("scheduler starting"); init(); } diff --git a/src/plugins/auto/src/schedule.hpp b/src/plugins/auto/src/schedule.hpp index 16539dd9dfce57..99efa3138cef00 100644 --- a/src/plugins/auto/src/schedule.hpp +++ b/src/plugins/auto/src/schedule.hpp @@ -50,7 +50,6 @@ class Schedule : public std::enable_shared_from_this, public ov::threa mutable std::atomic m_request_id = {0}; std::mutex m_dev_infer_mutex; std::unordered_map m_dev_infer; - std::shared_ptr m_logger; }; } // namespace auto_plugin From 783d2d93ccaceb64a383b92661c4676dc070f83b Mon Sep 17 00:00:00 2001 From: "Wang, Yang" Date: Thu, 14 Nov 2024 09:30:29 +0800 Subject: [PATCH 5/6] Update src/plugins/auto/src/common.hpp Co-authored-by: yanlan song --- src/plugins/auto/src/common.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/auto/src/common.hpp b/src/plugins/auto/src/common.hpp index 16e5933e934255..bfcb30441f71f2 100644 --- a/src/plugins/auto/src/common.hpp +++ b/src/plugins/auto/src/common.hpp @@ -220,7 +220,7 @@ class ScheduleContext : public std::enable_shared_from_this { std::mutex m_fallback_mutex; SoCompiledModel m_hw_compiled_model; std::string m_model_precision; - // hold the resource of static variable to avoid the destruction of the static variable. + // hold the resource of static variable to avoid the unexpected destruction. std::shared_ptr m_mtx; std::shared_ptr>> m_priority_map; std::shared_ptr m_logger = Log::instance(); From 72f93cba55139fb1555a792d6130d03c6ac3f714 Mon Sep 17 00:00:00 2001 From: "Wang, Yang" Date: Thu, 14 Nov 2024 09:30:35 +0800 Subject: [PATCH 6/6] Update src/plugins/auto/src/plugin.cpp Co-authored-by: yanlan song --- src/plugins/auto/src/plugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/auto/src/plugin.cpp b/src/plugins/auto/src/plugin.cpp index 29b50e6074a8d7..2371107281d630 100644 --- a/src/plugins/auto/src/plugin.cpp +++ b/src/plugins/auto/src/plugin.cpp @@ -632,7 +632,7 @@ DeviceInformation Plugin::select_device(const std::vector& me } void Plugin::unregister_priority(const unsigned int& priority, const std::string& device_name) { - if (m_mtx.get() && m_priority_map) { + if (m_mtx && m_priority_map) { std::lock_guard lck(*m_mtx); auto& priority_devices = (*m_priority_map)[priority]; for (auto iter = priority_devices.begin(); iter != priority_devices.end();) {