-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Reduce IR Constant Memory Retention #35457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
a17e69d
a898eb6
b1e8e47
8b75d6f
cdc4489
a58a3f4
bb4e162
34c9047
4d26e5d
f64c4e3
a313c19
f1bbb4e
434b15f
0b1bb5a
8fac1d4
d48f7d6
5edf87c
22f6492
3febebc
b9374f5
a0258c8
f9acab0
df0ae2a
7776a19
8b63fd4
1be1fb9
4133e84
e89b25d
cb9a012
36322dd
22b5f2a
c94a6f9
67b68a2
7e06fcc
522ebf3
a77e0c0
ab6624c
5039491
979b018
d8dc9a2
e3af9ec
de95e5f
ec779b3
afb3465
671dc06
a9f499e
1ae4bb5
90e5ae7
d8cb394
3e5501d
d93206a
0190e63
6c88331
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| #include "openvino/frontend/ir/frontend.hpp" | ||
|
|
||
| #include <array> | ||
| #include <cstdlib> | ||
| #include <optional> | ||
| #include <pugixml.hpp> | ||
| #include <vector> | ||
|
|
@@ -18,6 +19,7 @@ | |
| #include "openvino/util/file_util.hpp" | ||
| #include "openvino/util/mmap_object.hpp" | ||
| #include "openvino/util/xml_parse_utils.hpp" | ||
| #include "openvino/xml_util/xml_deserialize_util.hpp" | ||
| #include "transformations/resolve_names_collisions.hpp" | ||
| #include "utils.hpp" | ||
|
|
||
|
|
@@ -41,6 +43,19 @@ size_t get_ir_version(const pugi::xml_document& doc) { | |
|
|
||
| constexpr size_t HEADER_SIZE_LIM = 512lu; | ||
|
|
||
| bool use_separate_const_weights_loading() { | ||
| const auto* flag = std::getenv("OV_IR_SEPARATE_CONST_WEIGHTS"); | ||
| if (!flag) { | ||
| return false; | ||
| } | ||
|
|
||
| std::string value(flag); | ||
| std::transform(value.begin(), value.end(), value.begin(), [](unsigned char character) { | ||
| return std::tolower(character); | ||
| }); | ||
| return value == "1" || value == "true" || value == "yes" || value == "on"; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Extracts IR version from model stream | ||
| * @param model Model's stream | ||
|
|
@@ -151,8 +166,9 @@ void FrontEnd::add_extension(const ov::Extension::Ptr& ext) { | |
| InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const { | ||
| std::ifstream local_model_stream; | ||
| std::istream* provided_model_stream = nullptr; | ||
| std::shared_ptr<ov::AlignedBuffer> model_buf; | ||
| std::shared_ptr<ov::AlignedBuffer> weights; | ||
| std::shared_ptr<ov::AlignedBuffer> model_buf; // | ||
|
barnasm1 marked this conversation as resolved.
Outdated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this buffer still needed? |
||
| std::shared_ptr<ov::AlignedBuffer> weights; // | ||
| std::shared_ptr<ov::util::WeightsProvider> weights_provider; | ||
|
|
||
| auto create_extensions_map = [&]() -> std::unordered_map<ov::DiscreteTypeInfo, ov::BaseOpExtension::Ptr> { | ||
| std::unordered_map<ov::DiscreteTypeInfo, ov::BaseOpExtension::Ptr> exts; | ||
|
|
@@ -168,16 +184,22 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const | |
| return std::make_shared<InputModel>(*provided_model_stream, | ||
| weights, | ||
| create_extensions_map(), | ||
| std::move(weights_path)); | ||
| std::move(weights_path), | ||
| weights_provider); | ||
| } else if (local_model_stream.is_open()) { | ||
| auto input_model = std::make_shared<InputModel>(local_model_stream, | ||
| weights, | ||
| create_extensions_map(), | ||
| std::move(weights_path)); | ||
| std::move(weights_path), | ||
| weights_provider); | ||
| local_model_stream.close(); | ||
| return input_model; | ||
| } else if (model_buf) { | ||
| return std::make_shared<InputModel>(model_buf, weights, create_extensions_map(), std::move(weights_path)); | ||
| return std::make_shared<InputModel>(model_buf, | ||
| weights, | ||
| create_extensions_map(), | ||
| std::move(weights_path), | ||
| weights_provider); | ||
| } | ||
| return nullptr; | ||
| }; | ||
|
|
@@ -215,31 +237,36 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const | |
| } | ||
|
|
||
| if (!weights_path.empty()) { | ||
| const auto enable_mmap = variants.back().is<bool>() ? variants.back().as<bool>() : false; | ||
| if (enable_mmap) { | ||
| auto mapped_memory = ov::load_mmap_object(weights_path); | ||
| weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<MappedMemory>>>(mapped_memory->data(), | ||
| mapped_memory->size(), | ||
| mapped_memory); | ||
| } else if (std::ifstream bin_stream(weights_path, std::ios::binary); bin_stream.is_open()) { | ||
| bin_stream.seekg(0, std::ios::end); | ||
| size_t file_size = bin_stream.tellg(); | ||
| bin_stream.seekg(0, std::ios::beg); | ||
|
|
||
| auto aligned_weights_buffer = std::make_shared<ov::AlignedBuffer>(file_size); | ||
| bin_stream.read(aligned_weights_buffer->get_ptr<char>(), aligned_weights_buffer->size()); | ||
|
|
||
| weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>( | ||
| aligned_weights_buffer->get_ptr<char>(), | ||
| aligned_weights_buffer->size(), | ||
| aligned_weights_buffer, | ||
| ov::create_base_descriptor( | ||
| std::hash<std::decay_t<decltype(weights_path.native())>>{}(weights_path.native()), | ||
| 0, | ||
| aligned_weights_buffer)); | ||
|
|
||
| const auto separate_const_weights_loading = use_separate_const_weights_loading() && !weights; | ||
| if (separate_const_weights_loading) { | ||
|
praasz marked this conversation as resolved.
|
||
| weights_provider = std::make_shared<ov::util::FileWeightsProvider>(weights_path); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same use should be if input is stream not only for path |
||
| } else { | ||
| OPENVINO_THROW("Weights file ", weights_path, " cannot be opened!"); | ||
| const auto enable_mmap = variants.back().is<bool>() ? variants.back().as<bool>() : false; | ||
| if (enable_mmap) { | ||
| auto mapped_memory = ov::load_mmap_object(weights_path); | ||
| weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<MappedMemory>>>(mapped_memory->data(), | ||
| mapped_memory->size(), | ||
| mapped_memory); | ||
| } else if (std::ifstream bin_stream(weights_path, std::ios::binary); bin_stream.is_open()) { | ||
| bin_stream.seekg(0, std::ios::end); | ||
| size_t file_size = bin_stream.tellg(); | ||
| bin_stream.seekg(0, std::ios::beg); | ||
|
|
||
| auto aligned_weights_buffer = std::make_shared<ov::AlignedBuffer>(file_size); | ||
| bin_stream.read(aligned_weights_buffer->get_ptr<char>(), aligned_weights_buffer->size()); | ||
|
|
||
| weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>( | ||
| aligned_weights_buffer->get_ptr<char>(), | ||
| aligned_weights_buffer->size(), | ||
| aligned_weights_buffer, | ||
| ov::create_base_descriptor( | ||
| std::hash<std::decay_t<decltype(weights_path.native())>>{}(weights_path.native()), | ||
| 0, | ||
| aligned_weights_buffer)); | ||
|
|
||
| } else { | ||
| OPENVINO_THROW("Weights file ", weights_path, " cannot be opened!"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.