Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Enable mmap for reading model from cache (#26696)

Oleg Pipikin005152aafa3b
diff --git a/src/common/util/include/openvino/util/mmap_object.hpp b/src/common/util/include/openvino/util/mmap_object.hpp
index 364e1eed4ca..3aba8e69c09 100644
--- a/src/common/util/include/openvino/util/mmap_object.hpp
+++ b/src/common/util/include/openvino/util/mmap_object.hpp
@@ -50,18 +50,4 @@ std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::string& path);
std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::wstring& path);
#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
-
-class MmapStream final : public std::ifstream {
-public:
- MmapStream(const std::string& path) : std::ifstream(path, std::ios_base::binary) {
- m_memory = ov::load_mmap_object(path);
- }
-
-#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
- MmapStream(const std::wstring& path);
-#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
-
- std::shared_ptr<ov::MappedMemory> m_memory;
-};
-
} // namespace ov
diff --git a/src/common/util/src/os/win/win_mmap_object.cpp b/src/common/util/src/os/win/win_mmap_object.cpp
index 0b14d7ac774..6f251512427 100644
--- a/src/common/util/src/os/win/win_mmap_object.cpp
+++ b/src/common/util/src/os/win/win_mmap_object.cpp
@@ -141,11 +141,6 @@ std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::wstring& path) {
holder->set(path);
return holder;
}
-
-MmapStream::MmapStream(const std::wstring& path) : std::ifstream(path.data(), std::ios_base::binary) {
- m_memory = ov::load_mmap_object(path);
-}
-
#endif
} // namespace ov
diff --git a/src/core/dev_api/openvino/runtime/shared_buffer.hpp b/src/core/dev_api/openvino/runtime/shared_buffer.hpp
index 7f1e2e9ba76..2c784ef6081 100644
--- a/src/core/dev_api/openvino/runtime/shared_buffer.hpp
+++ b/src/core/dev_api/openvino/runtime/shared_buffer.hpp
@@ -8,7 +8,7 @@
namespace ov {
-/// \brief SharedBuffer class to store pointer to pre-allocated buffer.
+/// \brief SharedBuffer class to store pointer to pre-allocated buffer. Own the shared object.
template <typename T>
class SharedBuffer : public ov::AlignedBuffer {
public:
@@ -28,4 +28,60 @@ private:
T _shared_object;
};
+/// \brief SharedStreamBuffer class to store pointer to pre-acclocated buffer and provide streambuf interface.
+/// Can return ptr to shared memory and its size
+class SharedStreamBuffer : public std::streambuf {
+public:
+ SharedStreamBuffer(char* data, size_t size) : m_data(data), m_size(size), m_offset(0) {}
+
+protected:
+ // override std::streambuf methods
+ std::streamsize xsgetn(char* s, std::streamsize count) override {
+ auto real_count = std::min<std::streamsize>(m_size - m_offset, count);
+ std::memcpy(s, m_data + m_offset, real_count);
+ m_offset += real_count;
+ return real_count;
+ }
+
+ int_type underflow() override {
+ return (m_size == m_offset) ? traits_type::eof() : traits_type::to_int_type(*(m_data + m_offset));
+ }
+
+ int_type uflow() override {
+ return (m_size == m_offset) ? traits_type::eof() : traits_type::to_int_type(*(m_data + m_offset++));
+ }
+
+ std::streamsize showmanyc() override {
+ return m_size - m_offset;
+ }
+
+ pos_type seekoff(off_type off,
+ std::ios_base::seekdir dir,
+ std::ios_base::openmode which = std::ios_base::in) override {
+ if (dir != std::ios_base::cur || which != std::ios_base::in) {
+ return pos_type(off_type(-1));
+ }
+ m_offset += off;
+ return pos_type(m_offset);
+ }
+
+ char* m_data;
+ size_t m_size;
+ size_t m_offset;
+};
+
+/// \brief OwningSharedStreamBuffer is a SharedStreamBuffer which owns its shared object.
+class OwningSharedStreamBuffer : public SharedStreamBuffer {
+public:
+ OwningSharedStreamBuffer(std::shared_ptr<ov::AlignedBuffer> buffer)
+ : SharedStreamBuffer(static_cast<char*>(buffer->get_ptr()), buffer->size()),
+ m_shared_obj(buffer) {}
+
+ std::shared_ptr<ov::AlignedBuffer> get_buffer() {
+ return m_shared_obj;
+ }
+
+protected:
+ std::shared_ptr<ov::AlignedBuffer> m_shared_obj;
+};
} // namespace ov
diff --git a/src/inference/src/cache_manager.hpp b/src/inference/src/cache_manager.hpp
index 9e9ebd3ddcb..c441811c3cf 100644
--- a/src/inference/src/cache_manager.hpp
+++ b/src/inference/src/cache_manager.hpp
@@ -14,6 +14,7 @@
#include <memory>
#include <string>
+#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/util/file_util.hpp"
#include "openvino/util/mmap_object.hpp"
@@ -77,9 +78,10 @@ public:
* Otherwise, model will not be read from cache and will be loaded as usual
*
* @param id Id of cache (hash of the model)
+ * @param enable_mmap use mmap or ifstream to read model file
* @param reader Lambda function to be called when input stream is created
*/
- virtual void read_cache_entry(const std::string& id, StreamReader reader, bool mmap = false) = 0;
+ virtual void read_cache_entry(const std::string& id, bool enable_mmap, StreamReader reader) = 0;
/**
* @brief Callback when OpenVINO intends to remove cache entry
@@ -130,13 +132,17 @@ private:
writer(stream);
}
- void read_cache_entry(const std::string& id, StreamReader reader, bool mmap = false) override {
+ 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");
auto blob_file_name = getBlobFile(id);
if (ov::util::file_exists(blob_file_name)) {
- if (mmap) {
- MmapStream stream(blob_file_name);
+ if (enable_mmap) {
+ auto mmap = ov::load_mmap_object(blob_file_name);
+ auto shared_buffer =
+ std::make_shared<ov::SharedBuffer<std::shared_ptr<MappedMemory>>>(mmap->data(), mmap->size(), mmap);
+ OwningSharedStreamBuffer buf(shared_buffer);
+ std::istream stream(&buf);
reader(stream);
} else {
std::ifstream stream(blob_file_name, std::ios_base::binary);
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cpp
index 9f55dc53ccd..32b43f346e9 100644
--- a/src/inference/src/dev/core_impl.cpp
+++ b/src/inference/src/dev/core_impl.cpp
@@ -1397,19 +1397,12 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_and_cache(ov::Plugin&
return compiled_model;
}
-static bool does_plugin_support_model_caching_with_mmap(const ov::Plugin& plugin) {
- bool supported = plugin.supports_model_caching();
- supported &=
- ov::util::contains(plugin.get_property(ov::internal::supported_properties), ov::internal::caching_with_mmap);
- return supported;
-}
-
ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
const CacheContent& cacheContent,
ov::Plugin& plugin,
const ov::AnyMap& config,
const ov::SoPtr<ov::IRemoteContext>& context,
- std::function<ov::SoPtr<ov::ICompiledModel>()> compile_model_lambda) {
+ std::function<ov::SoPtr<ov::ICompiledModel>()> compile_model_lambda) const {
ov::SoPtr<ov::ICompiledModel> compiled_model;
struct HeaderException {};
@@ -1418,6 +1411,8 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
try {
cacheContent.cacheManager->read_cache_entry(
cacheContent.blobId,
+ coreConfig.get_enable_mmap() && ov::util::contains(plugin.get_property(ov::internal::supported_properties),
+ ov::internal::caching_with_mmap),
[&](std::istream& networkStream) {
OV_ITT_SCOPE(FIRST_INFERENCE,
ov::itt::domains::LoadTime,
@@ -1454,8 +1449,7 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
update_config[ov::loaded_from_cache.name()] = true;
compiled_model = context ? plugin.import_model(networkStream, context, update_config)
: plugin.import_model(networkStream, update_config);
- },
- does_plugin_support_model_caching_with_mmap(plugin));
+ });
} catch (const HeaderException&) {
// For these exceptions just remove old cache and set that import didn't work
cacheContent.cacheManager->remove_cache_entry(cacheContent.blobId);
diff --git a/src/inference/src/dev/core_impl.hpp b/src/inference/src/dev/core_impl.hpp
index 79b1b96d57a..7cf12f3ba32 100644
--- a/src/inference/src/dev/core_impl.hpp
+++ b/src/inference/src/dev/core_impl.hpp
@@ -149,12 +149,12 @@ private:
const ov::SoPtr<ov::IRemoteContext>& context,
const CacheContent& cacheContent) const;
- static ov::SoPtr<ov::ICompiledModel> load_model_from_cache(
+ ov::SoPtr<ov::ICompiledModel> load_model_from_cache(
const CacheContent& cacheContent,
ov::Plugin& plugin,
const ov::AnyMap& config,
const ov::SoPtr<ov::IRemoteContext>& context,
- std::function<ov::SoPtr<ov::ICompiledModel>()> compile_model_lambda);
+ std::function<ov::SoPtr<ov::ICompiledModel>()> compile_model_lambda) const;
bool device_supports_model_caching(const ov::Plugin& plugin) const;
diff --git a/src/plugins/intel_cpu/src/plugin.cpp b/src/plugins/intel_cpu/src/plugin.cpp
index fa1810ff604..5c88772eeed 100644
--- a/src/plugins/intel_cpu/src/plugin.cpp
+++ b/src/plugins/intel_cpu/src/plugin.cpp
@@ -446,7 +446,7 @@ ov::Any Plugin::get_ro_property(const std::string& name, const ov::AnyMap& optio
} else if (ov::internal::supported_properties == name) {
return decltype(ov::internal::supported_properties)::value_type{
ov::PropertyName{ov::internal::caching_properties.name(), ov::PropertyMutability::RO},
-#if !defined(OPENVINO_ARCH_ARM)
+#if !defined(OPENVINO_ARCH_ARM) && !(defined(__APPLE__) || defined(__MACOSX))
ov::PropertyName{ov::internal::caching_with_mmap.name(), ov::PropertyMutability::RO},
#endif
ov::PropertyName{ov::internal::exclusive_async_requests.name(), ov::PropertyMutability::RW},
diff --git a/src/plugins/intel_cpu/src/utils/serialize.cpp b/src/plugins/intel_cpu/src/utils/serialize.cpp
index 6666d42fb4f..f7fd337afa9 100644
--- a/src/plugins/intel_cpu/src/utils/serialize.cpp
+++ b/src/plugins/intel_cpu/src/utils/serialize.cpp
@@ -58,19 +58,20 @@ void ModelDeserializer::set_info(pugi::xml_node& root, std::shared_ptr<ov::Model
}
void ModelDeserializer::operator>>(std::shared_ptr<ov::Model>& model) {
- if (auto mmap_stream = dynamic_cast<MmapStream*>(&m_istream)) {
- process_mmap(model, mmap_stream->m_memory);
+ if (auto mmap_buffer = dynamic_cast<OwningSharedStreamBuffer*>(m_istream.rdbuf())) {
+ auto buffer = mmap_buffer->get_buffer();
+ process_mmap(model, buffer);
} else {
process_stream(model);
}
}
void ModelDeserializer::process_mmap(std::shared_ptr<ov::Model>& model,
- const std::shared_ptr<ov::MappedMemory>& mmemory) {
+ const std::shared_ptr<ov::AlignedBuffer>& mmemory) {
// Note: Don't use seekg with mmaped stream. This may affect the performance of some models.
// Get file size before seek content.
// Blob from cache may have other header, so need to skip this.
- auto buffer_base = mmemory->data();
+ auto buffer_base = reinterpret_cast<char*>(mmemory->get_ptr());
const auto file_size = mmemory->size();
const size_t hdr_pos = m_istream.tellg();
@@ -98,9 +99,7 @@ void ModelDeserializer::process_mmap(std::shared_ptr<ov::Model>& model,
// Map blob content
std::shared_ptr<ov::AlignedBuffer> weights_buf;
if (hdr.consts_size) {
- weights_buf = std::make_shared<ov::SharedBuffer<std::shared_ptr<MappedMemory>>>(buffer_base + hdr.consts_offset,
- hdr.consts_size,
- mmemory);
+ weights_buf = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(buffer_base + hdr.consts_offset, hdr.consts_size, mmemory);
}
// XML content
diff --git a/src/plugins/intel_cpu/src/utils/serialize.hpp b/src/plugins/intel_cpu/src/utils/serialize.hpp
index 817041452c9..897a2c2e52f 100644
--- a/src/plugins/intel_cpu/src/utils/serialize.hpp
+++ b/src/plugins/intel_cpu/src/utils/serialize.hpp
@@ -40,7 +40,7 @@ public:
protected:
static void set_info(pugi::xml_node& root, std::shared_ptr<ov::Model>& model);
- void process_mmap(std::shared_ptr<ov::Model>& model, const std::shared_ptr<ov::MappedMemory>& memory);
+ void process_mmap(std::shared_ptr<ov::Model>& model, const std::shared_ptr<ov::AlignedBuffer>& memory);
void process_stream(std::shared_ptr<ov::Model>& model);
diff --git a/src/plugins/intel_gpu/src/plugin/plugin.cpp b/src/plugins/intel_gpu/src/plugin/plugin.cpp
index 7ee587e612a..4ea7851b3f8 100644
--- a/src/plugins/intel_gpu/src/plugin/plugin.cpp
+++ b/src/plugins/intel_gpu/src/plugin/plugin.cpp
@@ -575,7 +575,8 @@ std::vector<ov::PropertyName> Plugin::get_supported_internal_properties() const
ov::PropertyName{ov::internal::exclusive_async_requests.name(), ov::PropertyMutability::RW},
ov::PropertyName{ov::internal::compiled_model_runtime_properties.name(), ov::PropertyMutability::RO},
ov::PropertyName{ov::internal::compiled_model_runtime_properties_supported.name(), ov::PropertyMutability::RO},
- ov::PropertyName{ov::internal::query_model_ratio.name(), PropertyMutability::RW}};
+ ov::PropertyName{ov::internal::query_model_ratio.name(), PropertyMutability::RW},
+ ov::PropertyName{ov::internal::caching_with_mmap.name(), PropertyMutability::RO}};
return supported_internal_properties;
}