OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Add sync on existing extensions in read_model. (#29893)
Oleg Pipikin6d23ba5aae91
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cppindex 2be954455f7..cc77ff88d4b 100644--- a/src/inference/src/dev/core_impl.cpp+++ b/src/inference/src/dev/core_impl.cpp@@ -908,7 +908,8 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::string& modstd::unique_ptr<CacheGuardEntry> lock = cacheGuard.get_hash_lock(cacheContent.blobId);compiled_model =load_model_from_cache(cacheContent, plugin, parsed._config, ov::SoPtr<ov::IRemoteContext>{}, [&]() {- const auto model = util::read_model(model_path, "", extensions, parsed._core_config.get_enable_mmap());+ const auto model =+ util::read_model(model_path, "", get_extensions_copy(), parsed._core_config.get_enable_mmap());return compile_model_and_cache(plugin, model, parsed._config, {}, cacheContent);});} else {@@ -1430,6 +1431,12 @@ void ov::CoreImpl::add_extensions_unsafe(const std::vector<ov::Extension::Ptr>&}}+std::vector<ov::Extension::Ptr> ov::CoreImpl::get_extensions_copy() const {+ std::lock_guard<std::mutex> lock(get_mutex());+ std::vector<ov::Extension::Ptr> extensions_copy(extensions.begin(), extensions.end());+ return extensions_copy;+};+void ov::CoreImpl::add_extension(const std::vector<ov::Extension::Ptr>& extensions) {std::lock_guard<std::mutex> lock(get_mutex());add_extensions_unsafe(extensions);@@ -1753,20 +1760,20 @@ std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::string& modelPathOV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "CoreImpl::read_model from file");auto local_core_config = coreConfig;local_core_config.set(properties);- return ov::util::read_model(modelPath, binPath, extensions, local_core_config.get_enable_mmap());+ return ov::util::read_model(modelPath, binPath, get_extensions_copy(), local_core_config.get_enable_mmap());}std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::string& model,const ov::Tensor& weights,bool frontendMode) const {OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "CoreImpl::read_model from memory");- return ov::util::read_model(model, weights, extensions, frontendMode);+ return ov::util::read_model(model, weights, get_extensions_copy(), frontendMode);}std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::shared_ptr<AlignedBuffer>& model,const std::shared_ptr<AlignedBuffer>& weights) const {OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "CoreImpl::read_model from memory");- return ov::util::read_model(model, weights, extensions);+ return ov::util::read_model(model, weights, get_extensions_copy());}std::map<std::string, ov::Version> ov::CoreImpl::get_versions(const std::string& deviceName) const {diff --git a/src/inference/src/dev/core_impl.hpp b/src/inference/src/dev/core_impl.hppindex c3eafaa6a6b..362124591ad 100644--- a/src/inference/src/dev/core_impl.hpp+++ b/src/inference/src/dev/core_impl.hpp@@ -243,6 +243,8 @@ private:}void add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& extensions) const;+ std::vector<ov::Extension::Ptr> get_extensions_copy() const;+public:CoreImpl();diff --git a/src/inference/tests/functional/ov_core_threading.cpp b/src/inference/tests/functional/ov_core_threading.cppindex 972cb3b5aca..3d9e5bd16d8 100644--- a/src/inference/tests/functional/ov_core_threading.cpp+++ b/src/inference/tests/functional/ov_core_threading.cpp@@ -8,7 +8,6 @@#include <chrono>#include <condition_variable>#include <fstream>-#include <mutex>#include <thread>#include "common_test_utils/common_utils.hpp"@@ -169,61 +168,16 @@ TEST_F(CoreThreadingTests, GetAvailableDevices) {}#if defined(ENABLE_OV_IR_FRONTEND)--namespace ov {-namespace test {-namespace util {-class Barrier {-private:- std::mutex m_mutex;- std::condition_variable m_cv;- size_t m_count;- const size_t m_expected;- size_t m_wait_id;--public:- explicit Barrier(std::size_t count) : m_count{count}, m_expected{count}, m_wait_id{} {}-- void arrive_and_wait() {- std::unique_lock<std::mutex> lock(m_mutex);-- if (--m_count == 0) {- ++m_wait_id;- m_count = m_expected;- m_cv.notify_all();- } else {- const auto wait_id = m_wait_id;- m_cv.wait(lock, [this, wait_id] {- return wait_id != m_wait_id;- });- }- }-};-} // namespace util-} // namespace test-} // namespace ov-// tested function: read_model and add_extensionTEST_F(CoreThreadingTests, ReadModel) {ov::Core core;auto model = core.read_model(modelName, weightsName);-constexpr size_t threads_num = 12;- ov::test::util::Barrier sync_point(threads_num);runParallel([&]() {safeAddExtension(core);- // Add the extension and read model are thread-safe when use separately.- // The barrier is required here to wait until all threads add extensions to core before read model.- // The read_model loads Frontend which check extension vector and assume it want change. If extension vector- // is expanded then all iterators are invalidated and can result in segfault when frontend check extensions- // to be added in frontend.- sync_point.arrive_and_wait();std::ignore = core.read_model(modelName, weightsName);-- // sync before next iteration (modification of extensions vector)- sync_point.arrive_and_wait();},100,threads_num);