-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Open
barnasm1
wants to merge
25
commits into
openvinotoolkit:master
Choose a base branch
from
barnasm1:separate_buffer_poc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+317
−91
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a17e69d
Implement WeightsProvider and FileWeightsProvider for flexible weight…
barnasm1 a898eb6
Merge branch 'master' into separate_buffer_poc
barnasm1 b1e8e47
fix win build
barnasm1 8b75d6f
fix win buildweight provider + prevend double budder alocatino
barnasm1 cdc4489
code format
barnasm1 a58a3f4
Merge branch 'master' into separate_buffer_poc
barnasm1 bb4e162
Merge branch 'master' into separate_buffer_poc
barnasm1 34c9047
do not use env flag
barnasm1 4d26e5d
weights loading logic if no enable mmap
barnasm1 f64c4e3
Add assertion to check if weights file can be opened in FileWeightsPr…
barnasm1 a313c19
Add FileRegionBuffer class and enhance FileWeightsProvider with sourc…
barnasm1 f1bbb4e
Merge branch 'master' into separate_buffer_poc
barnasm1 434b15f
Fix formatting
barnasm1 0b1bb5a
Remove const qualifier from load_region
barnasm1 8fac1d4
Remove unnecessary null check in BufferWeightsProvider::size()
barnasm1 d48f7d6
Merge branch 'master' into separate_buffer_poc
barnasm1 5edf87c
skip ate flag
barnasm1 22f6492
move WeightsProvider to separate file
barnasm1 3febebc
use ov util to file size
barnasm1 b9374f5
remove assertion for empty weights provider
barnasm1 a0258c8
refactor: remove load_weights_region and get_available_weights_size m…
barnasm1 f9acab0
use simpler hash function
barnasm1 df0ae2a
documentation for WeightsProvider interface
barnasm1 7776a19
refactor: remove FileRegionBuffer class and simplify load_region meth…
barnasm1 8b63fd4
Merge branch 'master' into separate_buffer_poc
barnasm1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/core/xml_util/include/openvino/xml_util/weights_provider.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <map> | ||
| #include <memory> | ||
|
|
||
| namespace ov { | ||
| class AlignedBuffer; | ||
| } // namespace ov | ||
|
|
||
| namespace ov::util { | ||
|
|
||
| /** | ||
| * @brief Interface for loading weight data regions from an underlying storage. | ||
| * | ||
| * Implementations may serve weights either from an in-memory buffer or from a | ||
| * file-backed source. | ||
| */ | ||
| class WeightsProvider { | ||
| public: | ||
| virtual ~WeightsProvider() = default; | ||
|
|
||
| /** | ||
| * @brief Loads a contiguous region of weights. | ||
| * | ||
| * @param offset Byte offset from the beginning of the weights source. | ||
| * @param size Number of bytes to load. | ||
| * @return Buffer containing the requested weights region. | ||
| */ | ||
| virtual std::shared_ptr<ov::AlignedBuffer> load_region(size_t offset, size_t size) = 0; | ||
|
|
||
| /** | ||
| * @brief Returns the total size of the weights source in bytes. | ||
| * | ||
| * @return Size of the underlying weights source. | ||
| */ | ||
| virtual size_t size() const = 0; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Weights provider implementation backed by an already allocated buffer. | ||
| */ | ||
| class BufferWeightsProvider : public WeightsProvider { | ||
| public: | ||
| /** | ||
| * @brief Constructs a weights provider over an existing buffer. | ||
| * | ||
| * @param weights Buffer containing the full weights blob. | ||
| */ | ||
| explicit BufferWeightsProvider(std::shared_ptr<ov::AlignedBuffer> weights); | ||
|
|
||
| /** | ||
| * @brief Returns a view of the requested region from the backing buffer. | ||
| * | ||
| * @param offset Byte offset from the beginning of the weights buffer. | ||
| * @param size Number of bytes to expose. | ||
| * @return Buffer referencing the requested region. | ||
| */ | ||
| std::shared_ptr<ov::AlignedBuffer> load_region(size_t offset, size_t size) override; | ||
|
|
||
| /** | ||
| * @brief Returns the total size of the backing weights buffer in bytes. | ||
| * | ||
| * @return Size of the underlying buffer. | ||
| */ | ||
| size_t size() const override; | ||
|
|
||
| private: | ||
| std::shared_ptr<ov::AlignedBuffer> m_weights; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Weights provider implementation backed by a weights file on disk. | ||
| */ | ||
| class FileWeightsProvider : public WeightsProvider { | ||
| public: | ||
| /** | ||
| * @brief Constructs a weights provider for the specified file. | ||
| * | ||
| * @param weights_path Path to the weights file. | ||
| */ | ||
| explicit FileWeightsProvider(std::filesystem::path weights_path); | ||
|
|
||
| /** | ||
| * @brief Loads the requested region from the weights file. | ||
| * | ||
| * Implementations may cache previously loaded regions. | ||
| * | ||
| * @param offset Byte offset from the beginning of the weights file. | ||
| * @param size Number of bytes to load. | ||
| * @return Buffer containing the requested file region. | ||
| */ | ||
| std::shared_ptr<ov::AlignedBuffer> load_region(size_t offset, size_t size) override; | ||
|
|
||
| /** | ||
| * @brief Returns the total size of the weights file in bytes. | ||
| * | ||
| * @return Size of the file-backed weights source. | ||
| */ | ||
| size_t size() const override; | ||
|
|
||
| private: | ||
| using WeightsRegionKey = std::pair<size_t, size_t>; | ||
|
|
||
| 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; | ||
| std::map<WeightsRegionKey, std::shared_ptr<ov::AlignedBuffer>> m_loaded_weights_regions; | ||
| }; | ||
| } // namespace ov::util | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "openvino/xml_util/weights_provider.hpp" | ||
|
|
||
| #include <fstream> | ||
|
|
||
| #include "openvino/runtime/aligned_buffer.hpp" | ||
| #include "openvino/runtime/shared_buffer.hpp" | ||
| #include "openvino/util/common_util.hpp" | ||
| #include "openvino/util/file_util.hpp" | ||
|
|
||
| namespace ov::util { | ||
|
|
||
| BufferWeightsProvider::BufferWeightsProvider(std::shared_ptr<ov::AlignedBuffer> weights) | ||
| : m_weights(std::move(weights)) {} | ||
|
|
||
| std::shared_ptr<ov::AlignedBuffer> BufferWeightsProvider::load_region(size_t offset, size_t size) { | ||
| OPENVINO_ASSERT(m_weights != nullptr, "Empty weights data in bin file or bin file cannot be found!"); | ||
|
t-jankowski marked this conversation as resolved.
|
||
| OPENVINO_ASSERT(offset <= m_weights->size() && size <= m_weights->size() - offset, | ||
| "Incorrect weights in bin file!"); | ||
|
|
||
| auto* data = m_weights->get_ptr<char>() + offset; | ||
| return std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(data, size, m_weights); | ||
| } | ||
|
|
||
| size_t BufferWeightsProvider::size() const { | ||
| return m_weights->size(); | ||
| } | ||
|
|
||
| FileWeightsProvider::FileWeightsProvider(std::filesystem::path weights_path) | ||
| : m_weights_path(std::move(weights_path)), | ||
| m_weights_size(ov::util::file_size(m_weights_path)), | ||
| m_weights_source_id(std::filesystem::hash_value(weights_path)), | ||
| m_weights_source_handle(std::make_shared<ov::AlignedBuffer>()) { | ||
| std::ifstream weights_stream(m_weights_path, std::ios::binary); | ||
| OPENVINO_ASSERT(weights_stream.is_open(), "Weights file ", m_weights_path, " cannot be opened!"); | ||
| } | ||
|
|
||
| std::shared_ptr<ov::AlignedBuffer> FileWeightsProvider::load_region(size_t offset, size_t size) { | ||
| OPENVINO_ASSERT(offset <= m_weights_size && size <= m_weights_size - offset, "Incorrect weights in bin file!"); | ||
|
|
||
| const FileWeightsProvider::WeightsRegionKey key{offset, size}; | ||
| if (const auto found = m_loaded_weights_regions.find(key); found != m_loaded_weights_regions.end()) { | ||
| return found->second; | ||
| } | ||
|
|
||
| std::ifstream weights_stream(m_weights_path, std::ios::binary); | ||
| OPENVINO_ASSERT(weights_stream.is_open(), "Weights file ", m_weights_path, " cannot be opened!"); | ||
|
|
||
| weights_stream.seekg(static_cast<std::streamoff>(offset), std::ios::beg); | ||
| OPENVINO_ASSERT(weights_stream.good(), "Failed to seek weights file ", m_weights_path, " to offset ", offset); | ||
|
|
||
| weights_stream.read(m_weights_source_handle->get_ptr<char>(), static_cast<std::streamsize>(size)); | ||
| OPENVINO_ASSERT(static_cast<size_t>(weights_stream.gcount()) == size, | ||
| "Failed to read ", | ||
| size, | ||
| " bytes from weights file ", | ||
| m_weights_path, | ||
| " at offset ", | ||
| offset); | ||
|
|
||
| m_loaded_weights_regions.emplace(key, m_weights_source_handle); | ||
|
|
||
| return m_weights_source_handle; | ||
| } | ||
|
|
||
| size_t FileWeightsProvider::size() const { | ||
| return m_weights_size; | ||
| } | ||
| } // namespace ov::util | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.