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
[core] Migrate store cache dir and ModelCache to use path (#33178)
Pawel Raasz229cd70d4bba
diff --git a/src/inference/dev_api/openvino/runtime/compilation_context.hpp b/src/inference/dev_api/openvino/runtime/compilation_context.hppindex 14799620db5..9e3f1a505fe 100644--- a/src/inference/dev_api/openvino/runtime/compilation_context.hpp+++ b/src/inference/dev_api/openvino/runtime/compilation_context.hpp@@ -18,17 +18,18 @@ namespace ov {class OPENVINO_RUNTIME_API ModelCache {public:- static std::string calculate_file_info(const std::string& filePath);+ static std::string calculate_file_info(const std::filesystem::path& file_path);- static std::string compute_hash(const std::shared_ptr<const ov::Model>& model, const ov::AnyMap& compileOptions);+ static std::string compute_hash(const std::shared_ptr<const ov::Model>& model, const ov::AnyMap& compile_options);- static std::string compute_hash(const std::string& modelName, const ov::AnyMap& compileOptions);- static std::string compute_hash(const std::string& modeStr,+ static std::string compute_hash(const std::filesystem::path& model_path, const ov::AnyMap& compile_options);++ static std::string compute_hash(const std::string& mode_str,const ov::Tensor& data,- const ov::AnyMap& compileOptions);+ const ov::AnyMap& compile_options);static std::string compute_hash(const std::shared_ptr<const ov::Model>& model,const std::filesystem::path& model_path,- const ov::AnyMap& compileOptions);+ const ov::AnyMap& compile_options);};class CompiledBlobHeader final {diff --git a/src/inference/src/cache_manager.hpp b/src/inference/src/cache_manager.hppindex b6fd78175e0..b1a50f0b53d 100644--- a/src/inference/src/cache_manager.hpp+++ b/src/inference/src/cache_manager.hpp@@ -108,14 +108,10 @@ public:**/class FileStorageCacheManager final : public ICacheManager {- std::string m_cachePath;-- std::filesystem::path getBlobFile(const std::string& blobHash) const {-#if defined(_WIN32) && defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)- return ov::util::string_to_wstring(ov::util::make_path(m_cachePath, blobHash + ".blob"));-#else- return ov::util::make_path(m_cachePath, blobHash + ".blob");-#endif+ std::filesystem::path m_cache_path;++ std::filesystem::path get_blob_file(const std::string& blob_hash) const {+ return m_cache_path / (blob_hash + ".blob");}public:@@ -123,7 +119,7 @@ public:* @brief Constructor**/- FileStorageCacheManager(std::string cachePath) : m_cachePath(std::move(cachePath)) {}+ FileStorageCacheManager(std::filesystem::path cache_path) : m_cache_path(std::move(cache_path)) {}/*** @brief Destructor@@ -135,7 +131,7 @@ private:void write_cache_entry(const std::string& id, StreamWriter writer) override {// Fix the bug caused by pugixml, which may return unexpected results if the locale is different from "C".ScopedLocale plocal_C(LC_ALL, "C");- const auto blob_path = getBlobFile(id);+ const auto blob_path = get_blob_file(id);std::ofstream stream(blob_path, std::ios_base::binary);writer(stream);stream.close();@@ -146,13 +142,13 @@ private:void read_cache_entry(const std::string& id, bool enable_mmap, StreamReader reader) override {// Fix the bug caused by pugixml, which may return unexpected results if the locale is different from "C".ScopedLocale plocal_C(LC_ALL, "C");- const auto blob_file_name = getBlobFile(id);- if (std::filesystem::exists(blob_file_name)) {+ const auto blob_path = get_blob_file(id);+ if (std::filesystem::exists(blob_path)) {if (enable_mmap) {- CompiledBlobVariant compiled_blob{std::in_place_index<0>, ov::read_tensor_data(blob_file_name)};+ CompiledBlobVariant compiled_blob{std::in_place_index<0>, ov::read_tensor_data(blob_path)};reader(compiled_blob);} else {- std::ifstream stream(blob_file_name, std::ios_base::binary);+ std::ifstream stream(blob_path, std::ios_base::binary);CompiledBlobVariant compiled_blob{std::in_place_index<1>, std::ref(stream)};reader(compiled_blob);}@@ -160,10 +156,10 @@ private:}void remove_cache_entry(const std::string& id) override {- auto blobFileName = getBlobFile(id);+ const auto blob_path = get_blob_file(id);- if (std::filesystem::exists(blobFileName)) {- std::ignore = std::filesystem::remove(blobFileName);+ if (std::filesystem::exists(blob_path)) {+ std::ignore = std::filesystem::remove(blob_path);}}};diff --git a/src/inference/src/dev/compilation_context.cpp b/src/inference/src/dev/compilation_context.cppindex fbb3579e337..29958096a55 100644--- a/src/inference/src/dev/compilation_context.cpp+++ b/src/inference/src/dev/compilation_context.cpp@@ -5,7 +5,9 @@#include <sys/stat.h>#include <sys/types.h>-#ifndef _WIN32+#ifdef _WIN32+# define stat _stat+#else# include <unistd.h>#endif@@ -20,10 +22,6 @@#include "transformations/rt_info/fused_names_attribute.hpp"#include "transformations/rt_info/primitives_priority_attribute.hpp"-#ifdef _WIN32-# define stat _stat-#endif-namespace ov {template <typename T>static uint64_t hash_combine(uint64_t seed, const T& a) {@@ -31,36 +29,48 @@ static uint64_t hash_combine(uint64_t seed, const T& a) {return seed ^ (std::hash<T>()(a) + 0x9e3779b9 + (seed << 6) + (seed >> 2));}-std::string ModelCache::calculate_file_info(const std::string& filePath) {- uint64_t seed = 0;- auto absPath = filePath;- if (filePath.size() > 0) {- try {- absPath = ov::util::get_absolute_file_path(filePath);- } catch (std::runtime_error&) {- // can't get absolute path, will use filePath for hash- }+namespace {+std::filesystem::path abs_path_or_input(const std::filesystem::path& path) {+ std::error_code ec;+ if (path.empty() || path.is_absolute()) {+ return path;+ } else if (auto abs_path = std::filesystem::absolute(std::filesystem::weakly_canonical(path), ec); ec) {+ return path;+ } else {+ return abs_path;}+}- seed = hash_combine(seed, absPath);+uint64_t hash_combine_options(uint64_t seed, const ov::AnyMap& compile_options) {+ for (const auto& [name, option] : compile_options) {+ seed = hash_combine(seed, name + option.as<std::string>());+ }+ return seed;+}+} // namespace++std::string ModelCache::calculate_file_info(const std::filesystem::path& file_path) {+ const auto& abs_path = abs_path_or_input(file_path);+ const auto& abs_path_str = util::path_to_string(abs_path);+ // Convert to string as std::hash<std::filesystem::path> could be not supported+ auto seed = hash_combine(0U, abs_path_str);- std::string res;- struct stat result;- if (stat(absPath.c_str(), &result) == 0) {+ if (struct stat result; stat(abs_path_str.c_str(), &result) == 0) {seed = hash_combine(seed, result.st_mtime);seed = hash_combine(seed, result.st_size);}+return std::to_string(seed);}-std::string ModelCache::compute_hash(const std::shared_ptr<const ov::Model>& model, const ov::AnyMap& compileOptions) {+std::string ModelCache::compute_hash(const std::shared_ptr<const ov::Model>& model, const ov::AnyMap& compile_options) {OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "ModelCache::compute_hash - Model");- return compute_hash(model, {}, compileOptions);+ return compute_hash(model, {}, compile_options);}std::string ModelCache::compute_hash(const std::shared_ptr<const ov::Model>& model,const std::filesystem::path& model_path,- const ov::AnyMap& compileOptions) {+ const ov::AnyMap& compile_options) {OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "ModelCache::compute_hash - Model and path");OPENVINO_ASSERT(model);@@ -72,9 +82,7 @@ std::string ModelCache::compute_hash(const std::shared_ptr<const ov::Model>& modm.run_passes(std::const_pointer_cast<ov::Model>(model));// 2. Compute hash on serialized data and options- for (const auto& [name, option] : compileOptions) {- seed = hash_combine(seed, name + option.as<std::string>());- }+ seed = hash_combine_options(seed, compile_options);// 3. Add runtime information which may not be serializedfor (const auto& op : model->get_ordered_ops()) {@@ -91,34 +99,29 @@ std::string ModelCache::compute_hash(const std::shared_ptr<const ov::Model>& mod// 4. If model path is provided add file info to the hashif (!model_path.empty()) {- seed = hash_combine(seed, compute_hash(model_path.string(), compileOptions));+ seed = hash_combine(seed, compute_hash(model_path, compile_options));}return std::to_string(seed);}-std::string ModelCache::compute_hash(const std::string& modelName, const ov::AnyMap& compileOptions) {+std::string ModelCache::compute_hash(const std::filesystem::path& model_path, const ov::AnyMap& compile_options) {OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "ModelCache::compute_hash - Model");- uint64_t seed = 0;- try {- seed = hash_combine(seed, ov::util::get_absolute_file_path(modelName));- } catch (...) {- // can't get absolute path, use modelName for hash calculation- seed = hash_combine(seed, modelName);- }- for (const auto& kvp : compileOptions) {- seed = hash_combine(seed, kvp.first + kvp.second.as<std::string>());- }++ const auto& abs_path = abs_path_or_input(model_path);+ // Convert to string as std::hash<std::filesystem::path> could be not supported+ auto seed = hash_combine(0U, util::path_to_string(abs_path));+ seed = hash_combine_options(seed, compile_options);return std::to_string(seed);}-std::string ModelCache::compute_hash(const std::string& modelStr,+std::string ModelCache::compute_hash(const std::string& model_str,const ov::Tensor& tensor,- const ov::AnyMap& compileOptions) {+ const ov::AnyMap& compile_options) {OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "ModelCache::compute_hash - Model");uint64_t seed = 0;// model string- seed = hash_combine(seed, modelStr);+ seed = hash_combine(seed, model_str);// tensor dataif (tensor) {@@ -163,9 +166,7 @@ std::string ModelCache::compute_hash(const std::string& modelStr,}// compile options- for (const auto& kvp : compileOptions) {- seed = hash_combine(seed, kvp.first + kvp.second.as<std::string>());- }+ seed = hash_combine_options(seed, compile_options);return std::to_string(seed);}diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cppindex e1ff566525f..37796553919 100644--- a/src/inference/src/dev/core_impl.cpp+++ b/src/inference/src/dev/core_impl.cpp@@ -396,9 +396,11 @@ ov::Parsed parse_device_config(const std::string& device_name,return parsed;}-void emplace_cache_dir_if_supported(ov::AnyMap& config, const ov::Plugin& plugin, const std::string& cache_dir) {+void emplace_cache_dir_if_supported(ov::AnyMap& config,+ const ov::Plugin& plugin,+ const std::filesystem::path& cache_dir) {if (ov::util::contains(plugin.get_property(ov::supported_properties), ov::cache_dir)) {- config.emplace(ov::cache_dir(cache_dir));+ config.emplace(ov::cache_dir(ov::util::path_to_string(cache_dir)));}}} // namespace@@ -851,9 +853,7 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<// hint::compiled_blob is set and imported skip compilation} else if (cache_manager && device_supports_model_caching(plugin, parsed.m_config) && !is_proxy_device(plugin)) {emplace_cache_dir_if_supported(parsed.m_config, plugin, cache_dir);- CacheContent cache_content{cache_manager,- parsed.m_core_config.get_enable_mmap(),- get_cache_model_path(config).string()};+ CacheContent cache_content{cache_manager, parsed.m_core_config.get_enable_mmap(), get_cache_model_path(config)};const auto compiled_config = create_compile_config(plugin, parsed.m_config);cache_content.m_blob_id = ModelCache::compute_hash(model, cache_content.m_model_path, compiled_config);cache_content.model = model;@@ -898,9 +898,7 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<// hint::compiled_blob is set and imported skip compilation} else if (cache_manager && device_supports_model_caching(plugin, parsed.m_config) && !is_proxy_device(plugin)) {emplace_cache_dir_if_supported(parsed.m_config, plugin, cache_dir);- CacheContent cache_content{cache_manager,- parsed.m_core_config.get_enable_mmap(),- get_cache_model_path(config).string()};+ CacheContent cache_content{cache_manager, parsed.m_core_config.get_enable_mmap(), get_cache_model_path(config)};const auto compiled_config = create_compile_config(plugin, parsed.m_config);cache_content.m_blob_id = ModelCache::compute_hash(model, cache_content.m_model_path, compiled_config);cache_content.model = model;@@ -929,9 +927,9 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::string& mod// Skip caching for proxy plugin. HW plugin will load network from the cacheCoreConfig::remove_core(parsed.m_config);emplace_cache_dir_if_supported(parsed.m_config, plugin, cache_dir);- CacheContent cache_content{cache_manager, parsed.m_core_config.get_enable_mmap(), model_path};+ CacheContent cache_content{cache_manager, parsed.m_core_config.get_enable_mmap(), util::make_path(model_path)};cache_content.m_blob_id =- ov::ModelCache::compute_hash(model_path, create_compile_config(plugin, parsed.m_config));+ ov::ModelCache::compute_hash(cache_content.m_model_path, create_compile_config(plugin, parsed.m_config));const auto lock = m_cache_guard.get_hash_lock(cache_content.m_blob_id);compiled_model = load_model_from_cache(cache_content, plugin, parsed.m_config, {}, [&]() {const auto model =@@ -1266,7 +1264,7 @@ ov::Any ov::CoreImpl::get_property_for_core(const std::string& name) const {const auto flag = ov::threading::executor_manager()->get_property(name).as<bool>();return decltype(ov::force_tbb_terminate)::value_type(flag);} else if (name == ov::cache_dir.name()) {- return ov::Any(m_core_config.get_cache_dir());+ return ov::Any(util::path_to_string(m_core_config.get_cache_dir()));} else if (name == ov::enable_mmap.name()) {const auto flag = m_core_config.get_enable_mmap();return decltype(ov::enable_mmap)::value_type(flag);@@ -1296,7 +1294,8 @@ ov::Any ov::CoreImpl::get_property(const std::string& device_name,if (parsed.m_device_name.empty()) {return get_property_for_core(name);} else if (name == ov::cache_dir.name()) {- return m_core_config.get_cache_config_for_device(get_plugin(parsed.m_device_name)).m_cache_dir;+ return util::path_to_string(+ m_core_config.get_cache_config_for_device(get_plugin(parsed.m_device_name)).m_cache_dir);}return get_plugin(parsed.m_device_name).get_property(name, parsed.m_config);}@@ -1495,7 +1494,6 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_and_cache(ov::Plugin&ov::ModelCache::calculate_file_info(cacheContent.m_model_path),compiled_model_runtime_properties,header_size_alignment);-compiled_model->export_model(networkStream);});} catch (...) {@@ -1584,7 +1582,7 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(weights_path.replace_extension(".bin");if (ov::util::file_exists(weights_path)) {- update_config[ov::weights_path.name()] = weights_path.string();+ update_config[ov::weights_path.name()] = util::path_to_string(weights_path);}}@@ -1666,7 +1664,7 @@ ov::CoreConfig::CoreConfig(const CoreConfig& other) {void ov::CoreConfig::set(const ov::AnyMap& config, const std::string& device_name) {if (const auto cfg_entry = config.find(ov::cache_dir.name()); cfg_entry != config.end()) {- const auto& cache_dir = cfg_entry->second.as<std::string>();+ const auto cache_dir = util::make_path(cfg_entry->second.as<std::string>());if (std::lock_guard<std::mutex> lock(m_cache_config_mutex); device_name.empty()) {// fill global cache configm_cache_config = CoreConfig::CacheConfig::create(cache_dir);@@ -1699,7 +1697,7 @@ void ov::CoreConfig::remove_core(ov::AnyMap& config) {}}-std::string ov::CoreConfig::get_cache_dir() const {+std::filesystem::path ov::CoreConfig::get_cache_dir() const {std::lock_guard<std::mutex> lock(m_cache_config_mutex);return m_cache_config.m_cache_dir;}@@ -1714,10 +1712,10 @@ ov::CoreConfig::CacheConfig ov::CoreConfig::get_cache_config_for_device(const ov: m_cache_config;}-ov::CoreConfig::CacheConfig ov::CoreConfig::CacheConfig::create(const std::string& dir) {+ov::CoreConfig::CacheConfig ov::CoreConfig::CacheConfig::create(const std::filesystem::path& dir) {CacheConfig cache_config{dir, nullptr};if (!dir.empty()) {- ov::util::create_directory_recursive(ov::util::make_path(dir));+ ov::util::create_directory_recursive(dir);cache_config.m_cache_manager = std::make_shared<ov::FileStorageCacheManager>(dir);}return cache_config;diff --git a/src/inference/src/dev/core_impl.hpp b/src/inference/src/dev/core_impl.hppindex 588877378a1..85fb380c564 100644--- a/src/inference/src/dev/core_impl.hpp+++ b/src/inference/src/dev/core_impl.hpp@@ -28,10 +28,10 @@ public:CoreConfig& operator=(const CoreConfig&) = delete;struct CacheConfig {- std::string m_cache_dir;+ std::filesystem::path m_cache_dir;std::shared_ptr<ov::ICacheManager> m_cache_manager;- static CacheConfig create(const std::string& dir);+ static CacheConfig create(const std::filesystem::path& dir);};void set(const ov::AnyMap& config, const std::string& device_name);@@ -43,7 +43,7 @@ public:*/void set_and_update(ov::AnyMap& config, const std::string& device_name);- std::string get_cache_dir() const;+ std::filesystem::path get_cache_dir() const;bool get_enable_mmap() const;@@ -148,13 +148,13 @@ private:struct CacheContent {explicit CacheContent(const std::shared_ptr<ov::ICacheManager>& cache_manager,bool mmap_enabled = false,- const std::string model_path = {})+ const std::filesystem::path model_path = {}): m_cache_manager(cache_manager),m_model_path(model_path),m_mmap_enabled{mmap_enabled} {}std::shared_ptr<ov::ICacheManager> m_cache_manager{};std::string m_blob_id{};- std::string m_model_path{};+ std::filesystem::path m_model_path{};std::shared_ptr<const ov::Model> model{};bool m_mmap_enabled{};};diff --git a/src/plugins/auto/src/plugin.cpp b/src/plugins/auto/src/plugin.cppindex d97745a31e9..630148de35f 100644--- a/src/plugins/auto/src/plugin.cpp+++ b/src/plugins/auto/src/plugin.cpp@@ -766,10 +766,9 @@ std::string Plugin::get_device_list(ov::AnyMap& properties,std::string blobId;if (model)- blobId = ov::ModelCache::compute_hash(std::const_pointer_cast<const ov::Model>(model),- dev_properties);+ blobId = ov::ModelCache::compute_hash(model, dev_properties);else- blobId = ov::ModelCache::compute_hash(model_path, dev_properties);+ blobId = ov::ModelCache::compute_hash(util::make_path(model_path), dev_properties);const auto cached_model_path = ov::util::make_path(cache_dir) / (blobId + ".blob");bool is_blob_file_exist = ov::util::file_exists(cached_model_path);num_blob_files += is_blob_file_exist;diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cppindex 0a9296d849d..92cfe47926f 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cpp@@ -115,7 +115,7 @@ namespace cldnn {std::mutex kernels_cache::_mutex;std::string kernels_cache::get_cache_path() const {- auto path = _config.get_cache_dir();+ auto path = ov::util::path_to_string(_config.get_cache_dir());if (path.empty()) {return {};}