Skip to content
Open
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
a17e69d
Implement WeightsProvider and FileWeightsProvider for flexible weight…
barnasm1 Apr 22, 2026
a898eb6
Merge branch 'master' into separate_buffer_poc
barnasm1 Apr 22, 2026
b1e8e47
fix win build
barnasm1 Apr 22, 2026
8b75d6f
fix win buildweight provider + prevend double budder alocatino
barnasm1 May 6, 2026
cdc4489
code format
barnasm1 May 6, 2026
a58a3f4
Merge branch 'master' into separate_buffer_poc
barnasm1 May 7, 2026
bb4e162
Merge branch 'master' into separate_buffer_poc
barnasm1 May 8, 2026
34c9047
do not use env flag
barnasm1 May 8, 2026
4d26e5d
weights loading logic if no enable mmap
barnasm1 May 8, 2026
f64c4e3
Add assertion to check if weights file can be opened in FileWeightsPr…
barnasm1 May 11, 2026
a313c19
Add FileRegionBuffer class and enhance FileWeightsProvider with sourc…
barnasm1 May 13, 2026
f1bbb4e
Merge branch 'master' into separate_buffer_poc
barnasm1 May 15, 2026
434b15f
Fix formatting
barnasm1 May 20, 2026
0b1bb5a
Remove const qualifier from load_region
barnasm1 May 20, 2026
8fac1d4
Remove unnecessary null check in BufferWeightsProvider::size()
barnasm1 May 20, 2026
d48f7d6
Merge branch 'master' into separate_buffer_poc
barnasm1 May 25, 2026
5edf87c
skip ate flag
barnasm1 May 27, 2026
22f6492
move WeightsProvider to separate file
barnasm1 May 28, 2026
3febebc
use ov util to file size
barnasm1 May 28, 2026
b9374f5
remove assertion for empty weights provider
barnasm1 May 28, 2026
a0258c8
refactor: remove load_weights_region and get_available_weights_size m…
barnasm1 May 28, 2026
f9acab0
use simpler hash function
barnasm1 May 28, 2026
df0ae2a
documentation for WeightsProvider interface
barnasm1 May 28, 2026
7776a19
refactor: remove FileRegionBuffer class and simplify load_region meth…
barnasm1 Jun 3, 2026
8b63fd4
Merge branch 'master' into separate_buffer_poc
barnasm1 Jun 3, 2026
1be1fb9
Revert "refactor: remove FileRegionBuffer class and simplify load_reg…
barnasm1 Jun 8, 2026
4133e84
Merge branch 'master' into separate_buffer_poc
barnasm1 Jun 10, 2026
e89b25d
add assertions for weights provider in XmlDeserializer. fix test: mod…
barnasm1 Jun 11, 2026
cb9a012
rename load_region to make_region
barnasm1 Jun 17, 2026
36322dd
add comment to m_loaded_weights_regions key
barnasm1 Jun 17, 2026
22b5f2a
simplify assert
barnasm1 Jun 17, 2026
c94a6f9
rename load_region to make_region - fix build
barnasm1 Jun 17, 2026
67b68a2
revert weight size checks
barnasm1 Jun 18, 2026
7e06fcc
update weight assertion message and add check for weights size
barnasm1 Jun 18, 2026
522ebf3
Merge branch 'master' into separate_buffer_poc
barnasm1 Jun 23, 2026
a77e0c0
Fix error message formatting in weight file assertions
barnasm1 Jun 23, 2026
ab6624c
use lazy_buffer
barnasm1 Jul 3, 2026
5039491
Merge branch 'master' into separate_buffer_poc
mlukasze Jul 6, 2026
979b018
FileWeightsProvider fix CI
barnasm1 Jul 6, 2026
d8dc9a2
Merge branch 'master' into separate_buffer_poc
barnasm1 Jul 7, 2026
e3af9ec
Merge branch 'master' into separate_buffer_poc
barnasm1 Jul 27, 2026
de95e5f
init all or none
barnasm1 Jul 29, 2026
ec779b3
avoid double check
barnasm1 Jul 29, 2026
afb3465
Refactor XmlDeserializer to use WeightsProvider for weight management
barnasm1 Jul 29, 2026
671dc06
Refactor FileWeightsProvider to remove FileRegionBuffer and optimize …
barnasm1 Jul 29, 2026
a9f499e
Add weights_provider source and header to xml_util CMakeLists
barnasm1 Jul 29, 2026
1ae4bb5
Refactor InputModelIRImpl constructor to initialize weights_provider …
barnasm1 Jul 29, 2026
90e5ae7
Refactor InputModelIRImpl to remove weights_path parameter and utiliz…
barnasm1 Jul 29, 2026
d8cb394
Merge branch 'master' into separate_buffer_poc
barnasm1 Aug 4, 2026
3e5501d
Merge branch 'master' into separate_buffer_poc
barnasm1 Aug 11, 2026
d93206a
Refactor XmlDeserializer to use const reference for weights_provider
barnasm1 Aug 12, 2026
0190e63
Change loaded_weights_regions from shared_ptr to weak_ptr
barnasm1 Aug 13, 2026
6c88331
Refactor parse_pre_process to use shared_ptr for Constant creation
barnasm1 Aug 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/core/tests/xml_util/custom_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,18 @@ class CustomIRTest : public testing::Test {
}();

std::unordered_map<std::string, std::shared_ptr<ov::op::util::Variable>> variables;
std::shared_ptr<ov::util::WeightsProvider> weights_provider;
if (org_weights) {
weights_provider = std::make_shared<ov::util::BufferWeightsProvider>(org_weights);
}
// deserialize model
std::shared_ptr<ov::Model> model;
if constexpr (std::is_same_v<Deserializer, ov::util::XmlDeserializer>) {
Deserializer visitor(root, org_weights, opsets, create_extensions_map, variables, version);
Deserializer visitor(root, weights_provider, opsets, create_extensions_map, variables, version);
visitor.on_attribute("net", model);
} else {
Deserializer visitor(root, org_weights, weights_map, opsets, create_extensions_map, variables, version);
Deserializer
visitor(root, weights_provider, weights_map, opsets, create_extensions_map, variables, version);
visitor.on_attribute("net", model);
}

Expand Down Expand Up @@ -251,14 +256,14 @@ class XmlDeserializer : public ov::util::XmlDeserializer {

public:
explicit XmlDeserializer(const pugi::xml_node& node,
const std::shared_ptr<ov::AlignedBuffer>& origin_weights,
const std::shared_ptr<ov::util::WeightsProvider>& weights_provider,
const WeightsMap& weights_map,
const std::unordered_map<std::string, ov::OpSet>& opsets,
const std::unordered_map<ov::DiscreteTypeInfo, ov::BaseOpExtension::Ptr>& extensions,
std::unordered_map<std::string, std::shared_ptr<ov::op::util::Variable>>& variables,
size_t version)
: ov::util::XmlDeserializer(node, origin_weights, opsets, extensions, variables, version),
m_origin_weights{origin_weights},
: ov::util::XmlDeserializer(node, weights_provider, opsets, extensions, variables, version),
m_weights_provider{weights_provider},
m_weights_map{std::ref(weights_map)} {}

protected:
Expand Down Expand Up @@ -313,9 +318,7 @@ class XmlDeserializer : public ov::util::XmlDeserializer {
actual_size = wl.original_size;
offset = wl.bin_offset;
auto original_dtype = wl.original_dtype;
char* data = m_origin_weights->get_ptr<char>() + offset;
auto w_size = m_origin_weights->size();
auto w_so = m_origin_weights;
auto w_size = m_weights_provider->size();

OPENVINO_ASSERT(w_size >= offset + actual_size, "Incorrect weights in bin file!");
if (original_dtype != el_type) {
Expand All @@ -335,9 +338,7 @@ class XmlDeserializer : public ov::util::XmlDeserializer {
ov::util::get_memory_size(el_type, ov::shape_size(shape)));
}

auto buffer =
std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(data, actual_size, w_so);
adapter.set(buffer);
adapter.set(m_weights_provider->make_region(offset, actual_size));
}
} else {
auto& buff = m_weights_map.get().at(offset);
Expand All @@ -349,21 +350,21 @@ class XmlDeserializer : public ov::util::XmlDeserializer {
private:
std::unique_ptr<ov::util::XmlDeserializer> make_visitor(
const pugi::xml_node& node,
const std::shared_ptr<ov::AlignedBuffer>& origin_weights,
const std::shared_ptr<ov::util::WeightsProvider>& weights_provider,
const std::unordered_map<std::string, ov::OpSet>& opsets,
const std::unordered_map<ov::DiscreteTypeInfo, ov::BaseOpExtension::Ptr>& extensions,
std::unordered_map<std::string, std::shared_ptr<ov::op::util::Variable>>& variables,
size_t version) const override {
return std::make_unique<XmlDeserializer>(node,
origin_weights,
weights_provider,
m_weights_map,
opsets,
extensions,
variables,
version);
}

std::shared_ptr<ov::AlignedBuffer> m_origin_weights;
std::shared_ptr<ov::util::WeightsProvider> m_weights_provider;
std::reference_wrapper<const WeightsMap> m_weights_map;
};

Expand Down
2 changes: 2 additions & 0 deletions src/core/xml_util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ set(TARGET_NAME "openvino_xml_util")
set(TARGET_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")

set(LIBRARY_SRC
${CMAKE_CURRENT_SOURCE_DIR}/src/weights_provider.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xml_deserialize_util.cpp
)
set(PUBLIC_HEADERS
${TARGET_INCLUDE_DIR}/openvino/xml_util/weights_provider.hpp
${TARGET_INCLUDE_DIR}/openvino/xml_util/xml_deserialize_util.hpp
)

Expand Down
128 changes: 128 additions & 0 deletions src/core/xml_util/include/openvino/xml_util/weights_provider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// 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 {
Comment thread
barnasm1 marked this conversation as resolved.
public:
virtual ~WeightsProvider() = default;

/**
* @brief Make 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> make_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 Returns the path to the weights source when the provider is file-backed.
*
* @return Path to the weights source, or an empty path if the provider is not file-backed.
*/
virtual std::filesystem::path path() const;
Comment thread
praasz marked this conversation as resolved.
};

/**
* @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> make_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> make_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;

/**
* @brief Returns the path to the backing weights file.
*
* @return Path to the backing weights file.
*/
std::filesystem::path path() const override;

private:
using WeightsRegionKey = std::pair<size_t, size_t>;
Comment thread
praasz marked this conversation as resolved.

std::filesystem::path m_weights_path{};
size_t m_weights_size{};
// 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?

@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;

};
} // namespace ov::util
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "openvino/runtime/aligned_buffer.hpp"
#include "openvino/util/common_util.hpp"
#include "openvino/util/string_view_streambuf.hpp"
#include "openvino/xml_util/weights_provider.hpp"

namespace ov::util {
struct GenericLayerParams;
Expand Down Expand Up @@ -49,7 +50,7 @@ void str_to_container<std::vector<std::string>>(const std::string& value, std::v
class XmlDeserializer : public ov::AttributeVisitor {
public:
explicit XmlDeserializer(const pugi::xml_node& node,
const std::shared_ptr<ov::AlignedBuffer>& weights,
std::shared_ptr<WeightsProvider> weights_provider,
const std::unordered_map<std::string, ov::OpSet>& opsets,
const std::unordered_map<ov::DiscreteTypeInfo, ov::BaseOpExtension::Ptr>& extensions,
std::unordered_map<std::string, std::shared_ptr<ov::op::util::Variable>>& variables,
Expand Down Expand Up @@ -78,8 +79,8 @@ class XmlDeserializer : public ov::AttributeVisitor {
virtual void set_constant_num_buffer(ov::AttributeAdapter<std::shared_ptr<ov::AlignedBuffer>>& adapter);

const pugi::xml_node& get_node() const;
const std::shared_ptr<ov::AlignedBuffer>& get_weights() const {
return m_weights;
const std::shared_ptr<WeightsProvider>& get_weights_provider() const {
Comment thread
barnasm1 marked this conversation as resolved.
return m_weights_provider;
}

private:
Expand Down Expand Up @@ -107,10 +108,8 @@ class XmlDeserializer : public ov::AttributeVisitor {

/// \brief Traverses xml node representation in order to create ov function for it.
/// \param node xml node representation
/// \param weights weights attached to current node
/// \return shared pointer to function representing input node
std::shared_ptr<ov::Model> parse_function(const pugi::xml_node& root,
const std::shared_ptr<ov::AlignedBuffer>& weights);
std::shared_ptr<ov::Model> parse_function(const pugi::xml_node& root);
/// \brief Traverses xml node representation in order to get the purpose attribute of
/// inputs/outputs in the body of Loop op. \param node xml node representation \return struct
/// with value of purpuse attribute
Expand All @@ -120,7 +119,7 @@ class XmlDeserializer : public ov::AttributeVisitor {

std::shared_ptr<ov::Node> create_node(const ov::OutputVector& inputs,
const pugi::xml_node& node,
const std::shared_ptr<ov::AlignedBuffer>& weights,
const std::shared_ptr<ov::util::WeightsProvider>& weights_provider,
const GenericLayerParams& params);

void read_meta_data(const std::shared_ptr<ov::Model>& model, const pugi::xml_node& meta_section);
Expand All @@ -131,17 +130,17 @@ class XmlDeserializer : public ov::AttributeVisitor {

virtual std::unique_ptr<XmlDeserializer> make_visitor(
const pugi::xml_node& node,
const std::shared_ptr<ov::AlignedBuffer>& weights,
Comment thread
praasz marked this conversation as resolved.
const std::shared_ptr<WeightsProvider>& weights_provider,
const std::unordered_map<std::string, ov::OpSet>& opsets,
const std::unordered_map<ov::DiscreteTypeInfo, ov::BaseOpExtension::Ptr>& extensions,
std::unordered_map<std::string, std::shared_ptr<ov::op::util::Variable>>& variables,
size_t version) const {
return std::make_unique<XmlDeserializer>(node, weights, opsets, extensions, variables, version);
return std::make_unique<XmlDeserializer>(node, weights_provider, opsets, extensions, variables, version);
}

// -- DATA --
const pugi::xml_node m_node;
const std::shared_ptr<ov::AlignedBuffer>& m_weights;
const std::shared_ptr<WeightsProvider> m_weights_provider;
const std::unordered_map<std::string, ov::OpSet>& m_opsets;
const std::unordered_map<ov::DiscreteTypeInfo, ov::BaseOpExtension::Ptr>& m_extensions;
std::unordered_map<std::string, std::shared_ptr<ov::op::util::Variable>>& m_variables;
Expand All @@ -151,7 +150,6 @@ class XmlDeserializer : public ov::AttributeVisitor {
/// it will be used during Inputs/Outputs Description creation in SubGraph processing
///
IoMap io_map;

int64_t m_version;
};

Expand Down
93 changes: 93 additions & 0 deletions src/core/xml_util/src/weights_provider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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"
#include "openvino/util/mmap_object.hpp"

namespace ov::util {

namespace {

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?

}

} // namespace

std::filesystem::path WeightsProvider::path() const {
return {};
}

BufferWeightsProvider::BufferWeightsProvider(std::shared_ptr<ov::AlignedBuffer> weights)
: m_weights(std::move(weights)) {}

std::shared_ptr<ov::AlignedBuffer> BufferWeightsProvider::make_region(size_t offset, size_t size) {
OPENVINO_ASSERT(m_weights != nullptr, "Empty weights data in bin file or bin file cannot be found!");
Comment thread
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)) {
std::ifstream weights_stream(m_weights_path, std::ios::binary);
OPENVINO_ASSERT(weights_stream.is_open(), m_weights_path, " cannot be opened");
}

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?


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::shared_ptr<ov::AlignedBuffer> buffer;
if (size >= get_mmap_region_threshold()) {
auto mapped_memory = ov::load_mmap_object(m_weights_path, offset, size);
buffer = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(mapped_memory->data(),
mapped_memory->size(),
mapped_memory);
} else {
auto file_region = std::make_shared<ov::AlignedBuffer>(size);
if (size > 0) {
std::ifstream weights_stream(m_weights_path, std::ios::binary);
OPENVINO_ASSERT(weights_stream.is_open(), m_weights_path, " cannot be opened");
weights_stream.seekg(static_cast<std::streamoff>(offset), std::ios::beg);
weights_stream.read(file_region->get_ptr<char>(), static_cast<std::streamsize>(size));
OPENVINO_ASSERT(weights_stream, "Failed to read weights from ", m_weights_path);
}
buffer = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(file_region->get_ptr<char>(),
size,
file_region);
}

m_loaded_weights_regions.emplace(key, buffer);

return buffer;
}

size_t FileWeightsProvider::size() const {
return m_weights_size;
}

std::filesystem::path FileWeightsProvider::path() const {
return m_weights_path;
}
} // namespace ov::util
Loading
Loading