Skip to content

Reduce IR Constant Memory Retention - #35457

Open
barnasm1 wants to merge 53 commits into
openvinotoolkit:masterfrom
barnasm1:separate_buffer_poc
Open

Reduce IR Constant Memory Retention#35457
barnasm1 wants to merge 53 commits into
openvinotoolkit:masterfrom
barnasm1:separate_buffer_poc

Conversation

@barnasm1

@barnasm1 barnasm1 commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Details:

Tickets:

AI Assistance:

  • AI assistance used: yes
  • If yes, summarize how AI was used and what human validation was performed (build/tests/manual checks).

@barnasm1
barnasm1 requested review from a team as code owners April 22, 2026 07:49
@barnasm1 barnasm1 self-assigned this Apr 22, 2026
@github-actions github-actions Bot added category: Core OpenVINO Core (aka ngraph) category: IR FE OpenVINO IR v10 / v11 FrontEnd labels Apr 22, 2026
@barnasm1 barnasm1 added do_not_merge and removed category: Core OpenVINO Core (aka ngraph) category: IR FE OpenVINO IR v10 / v11 FrontEnd labels Apr 22, 2026
@github-actions github-actions Bot added category: Core OpenVINO Core (aka ngraph) category: IR FE OpenVINO IR v10 / v11 FrontEnd labels Apr 22, 2026

const auto separate_const_weights_loading = use_separate_const_weights_loading() && !weights;
if (separate_const_weights_loading) {
weights_provider = std::make_shared<ov::util::FileWeightsProvider>(weights_path);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

@barnasm1
barnasm1 requested review from a team as code owners May 6, 2026 11:58
@github-actions github-actions Bot added the category: CPU OpenVINO CPU plugin label May 6, 2026
@t-jankowski t-jankowski self-assigned this May 15, 2026
Comment thread src/frontends/ir/src/input_model.cpp Outdated
Comment thread src/frontends/ir/src/frontend.cpp Outdated
Comment thread src/core/xml_util/src/xml_deserialize_util.cpp Outdated
Comment thread src/core/xml_util/include/openvino/xml_util/xml_deserialize_util.hpp Outdated
Comment thread src/core/xml_util/include/openvino/xml_util/xml_deserialize_util.hpp Outdated
Comment thread src/core/xml_util/src/xml_deserialize_util.cpp Outdated
Comment thread src/frontends/ir/src/frontend.cpp
Comment thread src/core/xml_util/src/xml_deserialize_util.cpp Outdated
Comment thread src/core/xml_util/src/xml_deserialize_util.cpp Outdated
Comment thread src/core/xml_util/src/xml_deserialize_util.cpp Outdated
Comment thread src/core/xml_util/src/xml_deserialize_util.cpp Outdated
std::filesystem::path m_weights_path;
size_t m_weights_size = 0;
size_t m_weights_source_id = 0;
std::shared_ptr<ov::AlignedBuffer> m_weights_source_handle;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It keeps the data source buffer + buffer in the map.
Is there no data duplication?
Will this provider will not live too long that data buffers set in Constant node will be not blocked to be removed if Constant node removed?

@praasz praasz self-assigned this May 18, 2026
@praasz
praasz requested review from praasz and t-jankowski June 24, 2026 07:09
size_t m_weights_source_id = 0;
std::shared_ptr<ov::AlignedBuffer> m_weights_source_handle;
// Cache of previously loaded weights regions, keyed by (offset, size) of the region in the weights file.
std::map<WeightsRegionKey, std::shared_ptr<ov::AlignedBuffer>> m_loaded_weights_regions;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For futher analysis why not use Constant ID (buffer) for it?

Comment thread src/core/xml_util/include/openvino/xml_util/weights_provider.hpp Outdated
}

std::shared_ptr<ov::AlignedBuffer> FileWeightsProvider::make_region(size_t offset, size_t size) {
OPENVINO_ASSERT(offset <= m_weights_size && size <= m_weights_size - offset, "Incorrect weights in bin file!");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like common validation pattern, should some helper be created to return bool if offset, size are valid?

Comment on lines +69 to +70
std::ifstream weights_stream(m_weights_path, std::ios::binary);
OPENVINO_ASSERT(weights_stream.is_open(), m_weights_path, " cannot be opened");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For analysis, should be opened each time and checked?
Or provider can keep it open?
The provider should be removed when frontend complete model parsing

if (m_weights->size() < offset + size)
OPENVINO_ASSERT(m_weights_provider, "Empty weights data in bin file or bin file cannot be found!");

if (m_weights_provider->size() < offset + size)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is done here and similar in provider are both required?

Comment thread src/core/xml_util/src/xml_deserialize_util.cpp Outdated
Comment thread src/core/xml_util/src/xml_deserialize_util.cpp
Comment thread src/frontends/ir/src/input_model.cpp Outdated
const size_t offset = item.second.second;
const char* data = weights->get_ptr<char>() + offset;
auto buffer = weights_provider->make_region(offset, item.second.first);
const char* data = buffer->get_ptr<char>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required, the const accept void*, or make_region/buffer is aligned buffer const also accept such buffer

Comment thread src/frontends/ir/src/input_model.cpp Outdated

@olpipi olpipi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend to write tests with weight_provder mocks to check if they are really used as planned

size_t m_weights_source_id = 0;
std::shared_ptr<ov::AlignedBuffer> m_weights_source_handle;
// Cache of previously loaded weights regions, keyed by (offset, size) of the region in the weights file.
std::map<WeightsRegionKey, std::shared_ptr<ov::AlignedBuffer>> m_loaded_weights_regions;

@olpipi olpipi Jul 13, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] you only add new buffers into m_loaded_weights_regions but never clean unused buffers. I'd suggest to store them as weak_ptr and clean expired ones regularly.

Suggested change
std::map<WeightsRegionKey, std::shared_ptr<ov::AlignedBuffer>> m_loaded_weights_regions;
std::map<WeightsRegionKey, std::weak_ptr<ov::AlignedBuffer>> m_loaded_weights_regions;

@praasz praasz modified the milestones: 2026.3, 2026.4 Jul 14, 2026
@github-actions github-actions Bot added the category: build OpenVINO cmake script / infra label Jul 29, 2026
@praasz
praasz requested review from olpipi and praasz July 30, 2026 08:13
Comment thread src/core/xml_util/include/openvino/xml_util/weights_provider.hpp

size_t get_mmap_region_threshold() {
const auto page_size = ov::util::get_system_page_size();
return page_size > 0 ? static_cast<size_t>(page_size) : 4096;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use some value if page size is 0? Will it be even possible?
Set threshold as 4k can be too small still a lot of overhead can be created.
Lets apply same logic as for ONNX FE where 1M is used . @t-jankowski could you check recommend threshold value?

if (!m_weights_path.empty())
model->get_rt_info()["__weights_path"] = ov::util::path_to_string(m_weights_path);
parse_pre_process(m_root, m_weights, model);
if (m_weights_provider) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future improvement, this check should not be required as weight is mandatory

Comment thread src/plugins/intel_cpu/src/utils/graph_serializer/deserializer.hpp
Comment thread src/core/xml_util/include/openvino/xml_util/weights_provider.hpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: build OpenVINO cmake script / infra category: Core OpenVINO Core (aka ngraph) category: CPU OpenVINO CPU plugin category: IR FE OpenVINO IR v10 / v11 FrontEnd

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants