Skip to content

Commit

Permalink
Use alternative from PR #27981 instead for memory mapped buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
MirceaDan99 committed Dec 11, 2024
1 parent 5dea314 commit 8cfb954
Show file tree
Hide file tree
Showing 22 changed files with 95 additions and 283 deletions.
8 changes: 2 additions & 6 deletions src/core/dev_api/openvino/runtime/aligned_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ class OPENVINO_API AlignedBuffer {
size_t size() const {
return m_byte_size;
}
void updateOffset(size_t offset) {
m_offset = offset;
}
void* get_ptr(size_t offset) const {
return m_aligned_buffer + offset;
}
void* get_ptr() {
return m_aligned_buffer + m_offset;
return m_aligned_buffer;
}
const void* get_ptr() const {
return m_aligned_buffer + m_offset;
return m_aligned_buffer;
}
template <typename T>
T* get_ptr() {
Expand All @@ -64,7 +61,6 @@ class OPENVINO_API AlignedBuffer {
char* m_allocated_buffer;
char* m_aligned_buffer;
size_t m_byte_size;
size_t m_offset = 0;
};

template <>
Expand Down
21 changes: 0 additions & 21 deletions src/core/dev_api/openvino/runtime/shared_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class SharedBuffer : public ov::AlignedBuffer {
m_allocated_buffer = data;
m_aligned_buffer = data;
m_byte_size = size;
m_offset = 0;
}

virtual ~SharedBuffer() {
Expand Down Expand Up @@ -82,26 +81,6 @@ class OwningSharedStreamBuffer : public SharedStreamBuffer {
return m_shared_obj;
}

std::streamsize xsgetn(char* s, std::streamsize count) override {
auto streamSize = SharedStreamBuffer::xsgetn(s, count);
m_shared_obj->updateOffset(m_offset);
return streamSize;
}

int_type uflow() override {
auto val = SharedStreamBuffer::uflow();
m_shared_obj->updateOffset(m_offset);
return val;
}

pos_type seekoff(off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode which = std::ios_base::in) override {
auto pos = SharedStreamBuffer::seekoff(off, dir, which);
m_shared_obj->updateOffset(m_offset);
return pos;
}

protected:
std::shared_ptr<ov::AlignedBuffer> m_shared_obj;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ class CompiledBlobHeader final {
std::string m_ieVersion;
std::string m_fileInfo;
std::string m_runtimeInfo;
std::shared_ptr<ov::AlignedBuffer> m_model_buffer;

public:
CompiledBlobHeader(std::shared_ptr<ov::AlignedBuffer> model_buffer);
CompiledBlobHeader();
CompiledBlobHeader(const std::string& ieVersion, const std::string& fileInfo, const std::string& runtimeInfo);

const std::string& get_openvino_version() const {
Expand All @@ -50,10 +49,6 @@ class CompiledBlobHeader final {
return m_runtimeInfo;
}

const std::shared_ptr<ov::AlignedBuffer> get_model_buffer() const {
return m_model_buffer;
}

friend std::istream& operator>>(std::istream& stream, CompiledBlobHeader& header);

friend std::ostream& operator<<(std::ostream& stream, const CompiledBlobHeader& header);
Expand Down
27 changes: 0 additions & 27 deletions src/inference/dev_api/openvino/runtime/iplugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,33 +185,6 @@ class OPENVINO_RUNTIME_API IPlugin : public std::enable_shared_from_this<IPlugin
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const = 0;

/**
* @brief Creates an compiled model from an previously exported model using plugin implementation
* and removes OpenVINO Runtime magic and plugin name
* @param model Reference to model output stream
* @param weights_buffer AlignedBuffer with cached model
* @param properties A ov::AnyMap of properties
* @return An Compiled model
*/
virtual std::shared_ptr<ov::ICompiledModel> import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::AnyMap& properties) const;

/**
* @brief Creates an compiled model from an previously exported model using plugin implementation
* and removes OpenVINO Runtime magic and plugin name
* @param model Reference to model output stream
* @param weights_buffer AlignedBuffer with cached model
* @param context A pointer to plugin context derived from RemoteContext class used to
* execute the network
* @param properties A ov::AnyMap of properties
* @return An Compiled model
*/
virtual std::shared_ptr<ov::ICompiledModel> import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const;

/**
* @brief Queries a plugin about supported layers in model
* @param model Model object to query.
Expand Down
1 change: 0 additions & 1 deletion src/inference/src/cache_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ class FileStorageCacheManager final : public ICacheManager {
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);
#if 0
OwningSharedStreamBuffer buf(shared_buffer);
std::istream stream(&buf);
reader(stream, shared_buffer);
Expand Down
7 changes: 1 addition & 6 deletions src/inference/src/dev/compilation_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ std::string ModelCache::compute_hash(const std::string& modelStr,

//////////////////////////////////////////////////

CompiledBlobHeader::CompiledBlobHeader(std::shared_ptr<ov::AlignedBuffer> model_buffer)
: m_model_buffer(model_buffer) {}
CompiledBlobHeader::CompiledBlobHeader() {}

CompiledBlobHeader::CompiledBlobHeader(const std::string& ieVersion,
const std::string& fileInfo,
Expand All @@ -169,10 +168,6 @@ CompiledBlobHeader::CompiledBlobHeader(const std::string& ieVersion,
std::istream& operator>>(std::istream& stream, CompiledBlobHeader& header) {
std::string xmlStr;
std::getline(stream, xmlStr);
auto model_buffer = header.get_model_buffer();
if (model_buffer != nullptr) {
model_buffer->updateOffset(stream.tellg());
}

pugi::xml_document document;
pugi::xml_parse_result res = document.load_string(xmlStr.c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/inference/src/dev/core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
ov::itt::domains::LoadTime,
"Core::load_model_from_cache::ReadStreamAndImport");
try {
ov::CompiledBlobHeader header(model_buffer);
ov::CompiledBlobHeader header;
networkStream >> header;
if (header.get_file_info() != ov::ModelCache::calculate_file_info(cacheContent.modelPath)) {
// Original file is changed, don't use cache
Expand Down
13 changes: 0 additions & 13 deletions src/inference/src/dev/iplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ const std::string& ov::IPlugin::get_device_name() const {
return m_plugin_name;
}

std::shared_ptr<ov::ICompiledModel> ov::IPlugin::import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::AnyMap& properties) const {
OPENVINO_THROW_NOT_IMPLEMENTED("This method is not implemented");
}

std::shared_ptr<ov::ICompiledModel> ov::IPlugin::import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const {
OPENVINO_THROW_NOT_IMPLEMENTED("This method is not implemented");
}

void ov::IPlugin::set_core(const std::weak_ptr<ov::ICore>& core) {
OPENVINO_ASSERT(!core.expired());
m_core = core;
Expand Down
13 changes: 0 additions & 13 deletions src/inference/src/dev/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,6 @@ ov::SoPtr<ov::ICompiledModel> ov::Plugin::import_model(std::istream& model,
OV_PLUGIN_CALL_STATEMENT(return {m_ptr->import_model(model, context, config), m_so});
}

ov::SoPtr<ov::ICompiledModel> ov::Plugin::import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::AnyMap& properties) const {
OV_PLUGIN_CALL_STATEMENT(return {m_ptr->import_model(model, model_buffer, properties), m_so});
}

ov::SoPtr<ov::ICompiledModel> ov::Plugin::import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& config) const {
OV_PLUGIN_CALL_STATEMENT(return {m_ptr->import_model(model, model_buffer, context, config), m_so});
}

ov::SoPtr<ov::IRemoteContext> ov::Plugin::create_context(const AnyMap& params) const {
OV_PLUGIN_CALL_STATEMENT({
auto remote = m_ptr->create_context(params);
Expand Down
10 changes: 1 addition & 9 deletions src/inference/src/dev/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ class Plugin {
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& config) const;

SoPtr<ov::ICompiledModel> import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::AnyMap& properties) const;

SoPtr<ov::ICompiledModel> import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& config) const;

ov::SoPtr<ov::IRemoteContext> create_context(const AnyMap& params) const;

ov::SoPtr<ov::IRemoteContext> get_default_context(const AnyMap& params) const;
Expand All @@ -87,3 +78,4 @@ class Plugin {
};

} // namespace ov

28 changes: 10 additions & 18 deletions src/plugins/intel_cpu/src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "cpu_streams_calculation.hpp"
#include "internal_properties.hpp"
#include "itt.h"
#include "openvino/op/paged_attention.hpp"
#include "openvino/runtime/intel_cpu/properties.hpp"
#include "openvino/runtime/internal_properties.hpp"
#include "openvino/runtime/properties.hpp"
Expand All @@ -20,6 +19,7 @@
#include "utils/precision_support.h"
#include "utils/serialize.hpp"
#include "weights_cache.hpp"
#include "openvino/op/paged_attention.hpp"

#if defined(__linux__)
# include <signal.h>
Expand Down Expand Up @@ -200,7 +200,7 @@ static Config::ModelType getModelType(const std::shared_ptr<const Model>& model)
return Config::ModelType::CNN;

if ((op::util::has_op_with_type<op::v13::ScaledDotProductAttention>(model) && model->get_variables().size() > 0) ||
op::util::has_op_with_type<ov::op::PagedAttentionExtension>(model))
op::util::has_op_with_type<ov::op::PagedAttentionExtension>(model))
return Config::ModelType::LLM;

return Config::ModelType::Unknown;
Expand Down Expand Up @@ -446,17 +446,15 @@ ov::Any Plugin::get_ro_property(const std::string& name, const ov::AnyMap& optio

return decltype(ov::supported_properties)::value_type(std::move(supportedProperties));
} else if (ov::internal::supported_properties == name) {
return decltype(ov::internal::supported_properties)::value_type {
return decltype(ov::internal::supported_properties)::value_type{
ov::PropertyName{ov::internal::caching_properties.name(), ov::PropertyMutability::RO},
#if !defined(OPENVINO_ARCH_ARM) && !(defined(__APPLE__) || defined(__MACOSX))
ov::PropertyName{ov::internal::caching_with_mmap.name(), ov::PropertyMutability::RO},
ov::PropertyName{ov::internal::caching_with_mmap.name(), ov::PropertyMutability::RO},
#endif
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::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}};
} else if (name == ov::device::full_name) {
return decltype(ov::device::full_name)::value_type(deviceFullName);
} else if (name == ov::available_devices) {
Expand Down Expand Up @@ -555,16 +553,11 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>&
return res;
}

std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& model_stream, const ov::AnyMap& config) const {
return import_model(model_stream, nullptr, config);
}

std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& model_stream,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::AnyMap& config) const {
OV_ITT_SCOPE(FIRST_INFERENCE, itt::domains::intel_cpu_LT, "import_model");

CacheDecrypt decrypt{codec_xor};
CacheDecrypt decrypt{ codec_xor };
bool decript_from_string = false;
if (config.count(ov::cache_encryption_callbacks.name())) {
auto encryption_callbacks = config.at(ov::cache_encryption_callbacks.name()).as<EncryptionCallbacks>();
Expand All @@ -585,8 +578,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& model_str
[this](const std::shared_ptr<ov::AlignedBuffer>& model, const std::shared_ptr<ov::AlignedBuffer>& weights) {
return get_core()->read_model(model, weights);
},
decrypt,
decript_from_string);
decrypt, decript_from_string);

std::shared_ptr<ov::Model> model;
deserializer >> model;
Expand Down
16 changes: 4 additions & 12 deletions src/plugins/intel_cpu/src/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Plugin : public ov::IPlugin {
std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>& model,
const ov::AnyMap& properties,
const ov::SoPtr<ov::IRemoteContext>& context) const override {
OPENVINO_THROW_NOT_IMPLEMENTED("compile_model with RemoteContext is not supported by CPU plugin!");
OPENVINO_THROW_NOT_IMPLEMENTED(
"compile_model with RemoteContext is not supported by CPU plugin!");
};

void set_property(const ov::AnyMap& properties) override;
Expand All @@ -29,17 +30,8 @@ class Plugin : public ov::IPlugin {
std::shared_ptr<ov::ICompiledModel> import_model(std::istream& model,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const override {
OPENVINO_THROW_NOT_IMPLEMENTED("import_model with RemoteContext is not supported by CPU plugin!");
};

std::shared_ptr<ov::ICompiledModel> import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::AnyMap& properties) const override;
std::shared_ptr<ov::ICompiledModel> import_model(std::istream& model,
std::shared_ptr<ov::AlignedBuffer> model_buffer,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const override {
OPENVINO_THROW_NOT_IMPLEMENTED("import_model with RemoteContext is not supported by CPU plugin!");
OPENVINO_THROW_NOT_IMPLEMENTED(
"import_model with RemoteContext is not supported by CPU plugin!");
};

ov::SupportedOpsMap query_model(const std::shared_ptr<const ov::Model>& model,
Expand Down
Loading

0 comments on commit 8cfb954

Please sign in to comment.