Skip to content
Open
Show file tree
Hide file tree
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 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
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
11 changes: 8 additions & 3 deletions src/core/tests/xml_util/custom_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ class XmlDeserializer : public ov::util::XmlDeserializer {
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),
: ov::util::XmlDeserializer(
node,
origin_weights ? std::make_shared<ov::util::BufferWeightsProvider>(origin_weights) : nullptr,
opsets,
extensions,
variables,
version),
m_origin_weights{origin_weights},
m_weights_map{std::ref(weights_map)} {}

Expand Down Expand Up @@ -349,13 +355,12 @@ 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::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,
m_origin_weights,
m_weights_map,
opsets,
extensions,
Expand Down
115 changes: 115 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,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 {
Comment thread
barnasm1 marked this conversation as resolved.
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "openvino/opsets/opset.hpp"
#include "openvino/runtime/aligned_buffer.hpp"
#include "openvino/util/common_util.hpp"
#include "openvino/xml_util/weights_provider.hpp"

namespace ov::util {
struct GenericLayerParams;
Expand Down Expand Up @@ -48,7 +49,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 @@ -77,8 +78,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 @@ -106,10 +107,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 @@ -119,7 +118,6 @@ 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 GenericLayerParams& params);

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

virtual std::unique_ptr<XmlDeserializer> make_visitor(
const pugi::xml_node& node,
const std::shared_ptr<ov::AlignedBuffer>& weights,
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, get_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 @@ -150,7 +147,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
72 changes: 72 additions & 0 deletions src/core/xml_util/src/weights_provider.cpp
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!");
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)),
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
Loading
Loading