Skip to content

Commit

Permalink
[AUTO] Fix the exception while freeing resources (openvinotoolkit#27497)
Browse files Browse the repository at this point in the history
### Details:
- Fix the exception caused by freeing resources order of static
variables.
### Tickets:
 - CVS-153980

---------

Co-authored-by: yanlan song <[email protected]>
  • Loading branch information
yangwang201911 and songbell authored Nov 26, 2024
1 parent 4c5228b commit 149edd3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
4 changes: 4 additions & 0 deletions src/plugins/auto/src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ class ScheduleContext : public std::enable_shared_from_this<ScheduleContext> {
std::mutex m_fallback_mutex;
SoCompiledModel m_hw_compiled_model;
std::string m_model_precision;
// hold the resource of static variable to avoid the unexpected destruction.
std::shared_ptr<std::mutex> m_mtx;
std::shared_ptr<std::map<unsigned int, std::list<std::string>>> m_priority_map;
std::shared_ptr<Log> m_logger = Log::instance();
virtual ~ScheduleContext() = default;
};

Expand Down
72 changes: 42 additions & 30 deletions src/plugins/auto/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ namespace {
namespace ov {
namespace auto_plugin {

std::mutex Plugin::m_mtx;
std::map<unsigned int, std::list<std::string>> Plugin::m_priority_map;
std::shared_ptr<std::mutex> Plugin::m_mtx = std::make_shared<std::mutex>();
std::shared_ptr<std::map<unsigned int, std::list<std::string>>> Plugin::m_priority_map =
std::make_shared<std::map<unsigned int, std::list<std::string>>>();

ov::SoPtr<ov::IRemoteContext> Plugin::create_context(const ov::AnyMap& remote_properties) const {
OPENVINO_NOT_IMPLEMENTED;
Expand Down Expand Up @@ -440,9 +441,12 @@ std::shared_ptr<ov::ICompiledModel> 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<ov::ICompiledModel> impl;
std::shared_ptr<Schedule> scheduler = is_cumulative ? std::static_pointer_cast<Schedule>(std::make_shared<CumuSchedule>()) :
std::static_pointer_cast<Schedule>(std::make_shared<AutoSchedule>());
std::shared_ptr<Schedule> scheduler =
is_cumulative ? std::static_pointer_cast<Schedule>(std::make_shared<CumuSchedule>())
: std::static_pointer_cast<Schedule>(std::make_shared<AutoSchedule>());
scheduler->launch(auto_s_context);
ov::SoPtr<ov::IRemoteContext> device_context;
try {
Expand Down Expand Up @@ -591,19 +595,25 @@ DeviceInformation Plugin::select_device(const std::vector<DeviceInformation>& me
DeviceInformation last_device = valid_devices.back();
{
// begin to filter devices
std::lock_guard<std::mutex> lck(m_mtx);
for (auto && kvp : m_priority_map) {
if (kvp.first >= priority) {
continue;
if (m_mtx && m_priority_map) {
std::lock_guard<std::mutex> 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());
}
}

Expand All @@ -621,24 +631,26 @@ DeviceInformation Plugin::select_device(const std::vector<DeviceInformation>& me
return *ptr_select_device;
}

void Plugin::unregister_priority(const unsigned int& priority,
const std::string& device_name) {
std::lock_guard<std::mutex> 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 && m_priority_map) {
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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 {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/auto/src/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class Plugin : public ov::IPlugin {
const std::shared_ptr<const ov::Model>& model,
PluginConfig& load_config) const;
std::string get_log_tag() const noexcept;
static std::mutex m_mtx;
static std::map<unsigned int, std::list<std::string>> m_priority_map;
static std::shared_ptr<std::mutex> m_mtx;
static std::shared_ptr<std::map<unsigned int, std::list<std::string>>> m_priority_map;
PluginConfig m_plugin_config;
};

Expand Down

0 comments on commit 149edd3

Please sign in to comment.