Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Core] Remove LazyBuffer (#38484)

Tomasz Jankowski2f8457d559ea
diff --git a/src/core/dev_api/dev_headers.cmake b/src/core/dev_api/dev_headers.cmake
index 6ccd95602d6..76008975186 100644
--- a/src/core/dev_api/dev_headers.cmake
+++ b/src/core/dev_api/dev_headers.cmake
@@ -53,7 +53,6 @@ set(DEV_HEADERS
${CMAKE_CURRENT_LIST_DIR}/openvino/runtime/aligned_buffer.hpp
${CMAKE_CURRENT_LIST_DIR}/openvino/runtime/compute_hash.hpp
${CMAKE_CURRENT_LIST_DIR}/openvino/runtime/itensor.hpp
- ${CMAKE_CURRENT_LIST_DIR}/openvino/runtime/lazy_buffer.hpp
${CMAKE_CURRENT_LIST_DIR}/openvino/runtime/shared_buffer.hpp
${CMAKE_CURRENT_LIST_DIR}/openvino/runtime/string_aligned_buffer.hpp
${CMAKE_CURRENT_LIST_DIR}/openvino/xml_util/constant_writer.hpp
diff --git a/src/core/dev_api/openvino/runtime/lazy_buffer.hpp b/src/core/dev_api/openvino/runtime/lazy_buffer.hpp
deleted file mode 100644
index 9060bc80b5d..00000000000
--- a/src/core/dev_api/openvino/runtime/lazy_buffer.hpp
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#pragma once
-
-#include <atomic>
-#include <filesystem>
-#include <memory>
-#include <mutex>
-
-#include "openvino/core/deprecated.hpp"
-#include "openvino/runtime/aligned_buffer.hpp"
-
-namespace ov {
-
-/** \brief LazyBuffer is lazy loaded AlignedBuffer which provides a view on a file w/o memory mapping. */
-class OPENVINO_API OPENVINO_DEPRECATED("LazyBuffer is deprecated and will be removed in 2026.5 release") LazyBuffer
- : public AlignedBuffer {
-public:
- /**
- * @brief Constructs a LazyBuffer which provides a view on a file. The file content is loaded to memory when
- * get_ptr() is called for the first time after object creation. The file content is loaded at aligned addresses,
- * so the actual allocated memory may be larger than the requested byte size.
- * @param file_path Path to the file to load
- * @param offset Offset in the file to start the view
- * @param byte_size Size of the view in bytes
- * @throws AssertFailure if the file does not exist or the file size is smaller than the requested view.
- */
- LazyBuffer(std::filesystem::path file_path, size_t offset, size_t byte_size);
-
- LazyBuffer(LazyBuffer&&) noexcept;
- LazyBuffer& operator=(LazyBuffer&&) noexcept;
- ~LazyBuffer() override;
-
- LazyBuffer(const LazyBuffer&) = delete;
- LazyBuffer& operator=(const LazyBuffer&) = delete;
-
- /**
- * @brief Gets aligned pointer to reserved buffer without loading data into it.
- */
- void* get_reserved_ptr() const noexcept {
- return m_aligned_buffer;
- }
-
- /**
- * @brief No-op by design: once loaded, LazyBuffer keeps file content resident until destroyed.
- */
- void hint_evict() noexcept override;
-
- /**
- * @brief Loads the file content if it is not loaded yet. The content is loaded at aligned addresses,
- * so the actual allocated memory may be larger than the requested byte size.
- * @throws AssertFailure if the file cannot be opened or read. In this case, the buffer remains unloaded.
- */
- void hint_prefetch() const override;
-
-protected:
- void hint_evict(size_t offset, size_t size) noexcept override;
-
-private:
- std::filesystem::path m_file_path;
- size_t m_offset{0};
-
- mutable std::atomic<bool> m_loaded{false};
- mutable std::mutex m_loading;
-};
-} // namespace ov
diff --git a/src/core/src/runtime/lazy_buffer.cpp b/src/core/src/runtime/lazy_buffer.cpp
deleted file mode 100644
index 5025bc9f59d..00000000000
--- a/src/core/src/runtime/lazy_buffer.cpp
+++ /dev/null
@@ -1,100 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "openvino/runtime/lazy_buffer.hpp"
-
-#include <istream>
-#include <mutex>
-#include <utility>
-
-#include "openvino/core/deprecated.hpp"
-#include "openvino/core/except.hpp"
-#include "openvino/core/memory_util.hpp"
-#include "openvino/util/file_util.hpp"
-#include "openvino/util/memory.hpp"
-#include "openvino/util/parallel_read_streambuf.hpp"
-
-namespace ov {
-OPENVINO_SUPPRESS_DEPRECATED_START
-LazyBuffer::LazyBuffer(std::filesystem::path file_path, size_t offset, size_t byte_size)
- : AlignedBuffer(),
- m_file_path{std::move(file_path)},
- m_offset{offset},
- m_loaded{false} {
- m_byte_size = byte_size;
-
- const auto file_size = util::file_size(m_file_path);
- OPENVINO_ASSERT(file_size >= 0, "Failed to get file size for ", m_file_path);
-
- const bool offset_fits = m_offset <= static_cast<size_t>(file_size);
- const bool size_fits = m_byte_size <= static_cast<size_t>(file_size) - m_offset;
- OPENVINO_ASSERT(offset_fits && size_fits,
- "Requested region ",
- m_offset,
- "+",
- m_byte_size,
- " exceeds file size ",
- file_size,
- " for file: ",
- m_file_path);
-
- std::error_code ec;
- m_aligned_buffer = static_cast<char*>(util::vm_reserve(m_byte_size, ec));
- OPENVINO_ASSERT(m_aligned_buffer != nullptr, "Failed to reserve memory for LazyBuffer. Error: ", ec.message());
-}
-
-LazyBuffer::~LazyBuffer() {
- if (m_aligned_buffer) {
- util::vm_release(m_aligned_buffer, m_byte_size);
- m_aligned_buffer = nullptr;
- }
- m_byte_size = 0;
-}
-
-LazyBuffer::LazyBuffer(LazyBuffer&& other) noexcept
- : AlignedBuffer(std::move(other)),
- m_file_path{std::move(other.m_file_path)},
- m_offset{std::exchange(other.m_offset, 0)},
- m_loaded{other.m_loaded.exchange(false, std::memory_order_relaxed)} {}
-
-LazyBuffer& LazyBuffer::operator=(LazyBuffer&& other) noexcept {
- if (this != &other) {
- AlignedBuffer::operator=(std::move(other));
- m_file_path = std::move(other.m_file_path);
- m_offset = std::exchange(other.m_offset, 0);
- m_loaded = other.m_loaded.exchange(false, std::memory_order_relaxed);
- }
- return *this;
-}
-
-void LazyBuffer::hint_prefetch() const {
- if (!m_loaded.load(std::memory_order_acquire)) {
- std::lock_guard lock{m_loading};
- if (m_loaded.load(std::memory_order_relaxed)) {
- return;
- }
-
- std::error_code ec;
- util::vm_commit(m_aligned_buffer, m_byte_size, ec);
- OPENVINO_ASSERT(!ec, "Failed to commit memory for LazyBuffer. Error: ", ec.message());
-
- try {
- util::ParallelReadStreamBuf par_buf(m_file_path, static_cast<std::streamoff>(m_offset));
- std::istream file(&par_buf);
- OPENVINO_ASSERT(file, "Failed to open file: ", m_file_path);
- file.read(m_aligned_buffer, m_byte_size);
- OPENVINO_ASSERT(file, "Failed to read data from file: ", m_file_path);
- m_loaded.store(true, std::memory_order_release);
- } catch (...) {
- util::vm_decommit(m_aligned_buffer, m_byte_size);
- throw;
- }
- }
-}
-
-void LazyBuffer::hint_evict() noexcept {}
-
-void LazyBuffer::hint_evict(size_t offset, size_t size) noexcept {}
-OPENVINO_SUPPRESS_DEPRECATED_END
-} // namespace ov
diff --git a/src/core/src/sources.cmake b/src/core/src/sources.cmake
index 9d85222fa3c..af17061ebdc 100644
--- a/src/core/src/sources.cmake
+++ b/src/core/src/sources.cmake
@@ -369,7 +369,6 @@ set(LIBRARY_SRC
${CMAKE_CURRENT_LIST_DIR}/runtime/allocator.cpp
${CMAKE_CURRENT_LIST_DIR}/runtime/compute_hash.cpp
${CMAKE_CURRENT_LIST_DIR}/runtime/itensor.cpp
- ${CMAKE_CURRENT_LIST_DIR}/runtime/lazy_buffer.cpp
${CMAKE_CURRENT_LIST_DIR}/runtime/shared_buffer.cpp
${CMAKE_CURRENT_LIST_DIR}/runtime/string_aligned_buffer.cpp
${CMAKE_CURRENT_LIST_DIR}/runtime/tensor.cpp
diff --git a/src/core/tests/CMakeLists.txt b/src/core/tests/CMakeLists.txt
index 59575d0a224..39ae93459b1 100644
--- a/src/core/tests/CMakeLists.txt
+++ b/src/core/tests/CMakeLists.txt
@@ -54,7 +54,6 @@ set(OV_CORE_TESTS_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/int4.cpp
${CMAKE_CURRENT_SOURCE_DIR}/intervals.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layout.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/lazy_buffer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/logger.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/matcher_pass.cpp
diff --git a/src/core/tests/lazy_buffer.cpp b/src/core/tests/lazy_buffer.cpp
deleted file mode 100644
index 6df1296d203..00000000000
--- a/src/core/tests/lazy_buffer.cpp
+++ /dev/null
@@ -1,139 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "openvino/runtime/lazy_buffer.hpp"
-
-#include <gtest/gtest.h>
-
-#include <fstream>
-#include <string_view>
-
-#include "common_test_utils/common_utils.hpp"
-#include "common_test_utils/test_assertions.hpp"
-#include "openvino/core/deprecated.hpp"
-
-using namespace testing;
-
-namespace ov::test {
-OPENVINO_SUPPRESS_DEPRECATED_START
-class LazyBufferTest : public Test {
-protected:
- std::filesystem::path m_file_path;
- std::vector<char> m_test_data;
-
- void SetUp() override {
- m_file_path = utils::generateTestFilePrefix();
- }
-
- void TearDown() override {
- std::filesystem::remove(m_file_path);
- }
-
- void write_test_data(size_t size) {
- m_test_data.resize(size);
- std::iota(m_test_data.begin(), m_test_data.end(), 0);
- std::ofstream os(m_file_path, std::ios::binary);
- os.write(m_test_data.data(), m_test_data.size());
- }
-
- void overwrite_test_data(size_t offset, const std::vector<char>& data) {
- ASSERT_LE(offset + data.size(), m_test_data.size());
- std::copy(data.begin(), data.end(), m_test_data.begin() + offset);
-
- std::fstream fs(m_file_path, std::ios::binary | std::ios::in | std::ios::out);
- ASSERT_TRUE(fs.is_open());
- fs.seekp(offset);
- fs.write(data.data(), data.size());
- ASSERT_TRUE(fs.good());
- }
-};
-
-TEST_F(LazyBufferTest, incorrect_file) {
- OV_EXPECT_THROW(std::ignore = std::make_unique<LazyBuffer>(std::filesystem::path{"no_file"}, 1, 2),
- AssertFailure,
- HasSubstr("Failed to get file size"));
-
- write_test_data(4);
-
- const auto test_params =
- std::vector<std::tuple<size_t, size_t>>{{0, 5}, {1, 4}, {4, 2}, {0, std::numeric_limits<size_t>::max()}};
- for (const auto& [offset, size] : test_params) {
- OV_EXPECT_THROW(std::ignore = std::make_unique<LazyBuffer>(m_file_path, offset, size),
- AssertFailure,
- HasSubstr("exceeds file size"));
- }
-}
-
-TEST_F(LazyBufferTest, read_file) {
- write_test_data(457);
- const auto test_params = std::vector<std::tuple<size_t, size_t>>{{0, 10},
- {5, 20},
- {50, 100},
- {0, m_test_data.size()},
- {14, 15},
- {128, 256}};
- for (const auto& [offset, size] : test_params) {
- auto lazy_b = LazyBuffer{m_file_path, offset, size};
- AlignedBuffer& buffer = lazy_b;
- char* data_ptr = nullptr;
- ASSERT_NO_THROW((data_ptr = buffer.get_ptr<char>()));
- ASSERT_NE(data_ptr, nullptr);
- ASSERT_EQ(buffer.size(), size);
- EXPECT_THAT(std::string_view(data_ptr, size), ElementsAreArray(m_test_data.data() + offset, size));
- }
-}
-
-TEST_F(LazyBufferTest, load_on_first_get_ptr) {
- write_test_data(128);
-
- constexpr size_t offset = 37;
- constexpr size_t size = 9;
- const std::vector<char> first_rewrite{'L', 'A', 'Z', 'Y', ' ', 'D', 'A', 'T', 'A'};
- const std::vector<char> second_rewrite{'O', 'V', 'E', 'R', 'W', 'R', 'I', 'T', 'E'};
-
- std::unique_ptr<AlignedBuffer> buffer = std::make_unique<LazyBuffer>(m_file_path, offset, size);
-
- // If constructor eagerly reads file data, get_ptr() would return original bytes instead of first_rewrite.
- overwrite_test_data(offset, first_rewrite);
-
- char* first_ptr = nullptr;
- ASSERT_NO_THROW((first_ptr = buffer->get_ptr<char>()));
- ASSERT_NE(first_ptr, nullptr);
- ASSERT_TRUE(std::equal(first_ptr, first_ptr + size, first_rewrite.begin()));
-
- // Once loaded, subsequent get_ptr() calls should return cached memory and ignore later file overwrites.
- overwrite_test_data(offset, second_rewrite);
-
- char* second_ptr = nullptr;
- ASSERT_NO_THROW((second_ptr = buffer->get_ptr<char>()));
- ASSERT_EQ(second_ptr, first_ptr);
- EXPECT_THAT(first_rewrite, ElementsAreArray(second_ptr, size));
-}
-
-TEST_F(LazyBufferTest, hint_evict_is_noop) {
- write_test_data(128);
-
- constexpr size_t offset = 31;
- constexpr size_t size = 9;
- const std::vector<char> first_rewrite{'L', 'A', 'Z', 'Y', ' ', 'D', 'A', 'T', 'A'};
- const std::vector<char> second_rewrite{'O', 'V', 'E', 'R', 'W', 'R', 'I', 'T', 'E'};
-
- const auto buffer = std::make_unique<LazyBuffer>(m_file_path, offset, size);
-
- overwrite_test_data(offset, first_rewrite);
- char* first_ptr = nullptr;
- ASSERT_NO_THROW((first_ptr = buffer->get_ptr<char>()));
- ASSERT_NE(first_ptr, nullptr);
- ASSERT_THAT(first_rewrite, ElementsAreArray(first_ptr, size));
-
- // hint_evict() is a no-op: cached content and pointer must stay the same even after a later file overwrite.
- buffer->hint_evict();
- overwrite_test_data(offset, second_rewrite);
- char* second_ptr = nullptr;
- ASSERT_NO_THROW((second_ptr = buffer->get_ptr<char>()));
- ASSERT_EQ(second_ptr, first_ptr);
- EXPECT_THAT(first_rewrite, ElementsAreArray(second_ptr, size));
-}
-OPENVINO_SUPPRESS_DEPRECATED_END
-} // namespace ov::test
diff --git a/src/frontends/onnx/frontend/src/utils/tensor_external_data.cpp b/src/frontends/onnx/frontend/src/utils/tensor_external_data.cpp
index 479f3ccaae6..32c501461a6 100644
--- a/src/frontends/onnx/frontend/src/utils/tensor_external_data.cpp
+++ b/src/frontends/onnx/frontend/src/utils/tensor_external_data.cpp
@@ -9,9 +9,9 @@
#include "exceptions.hpp"
#include "openvino/core/deprecated.hpp"
-#include "openvino/runtime/lazy_buffer.hpp"
#include "openvino/util/file_util.hpp"
#include "openvino/util/log.hpp"
+#include "openvino/util/native_stream.hpp"
namespace ov::frontend::onnx::detail {
TensorExternalData::TensorExternalData(const TensorProto& tensor) {
@@ -84,33 +84,21 @@ Buffer<ov::AlignedBuffer> TensorExternalData::load_external_data(const std::file
}
uint64_t read_data_length = m_data_length > 0 ? m_data_length : static_cast<uint64_t>(file_size) - m_offset;
- const auto get_now_buffer = [&]() {
- std::ifstream external_data_stream(full_path, std::ios::binary | std::ios::in | std::ios::ate);
- if (external_data_stream.fail()) {
- throw error::invalid_external_data{*this};
- }
- // default value of m_offset is 0
- external_data_stream.seekg(m_offset, std::ios::beg);
+ auto read_data = std::make_shared<ov::AlignedBuffer>(read_data_length);
- auto read_data = std::make_shared<ov::AlignedBuffer>(read_data_length);
- external_data_stream.read(read_data->get_ptr<char>(), read_data_length);
- external_data_stream.close();
- return std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(read_data->get_ptr<char>(),
- read_data->size(),
- read_data);
- };
- const auto get_lazy_buffer = [&]() {
- OPENVINO_SUPPRESS_DEPRECATED_START
- const auto lazy = std::make_shared<LazyBuffer>(full_path, m_offset, read_data_length);
- return std::make_shared<SharedBuffer<std::shared_ptr<AlignedBuffer>>>(
- static_cast<char*>(lazy->get_reserved_ptr()),
- lazy->size(),
- lazy);
- OPENVINO_SUPPRESS_DEPRECATED_END
- };
+ if (read_data_length > 0) {
+ util::NativeIfstream external_data_stream(full_path);
+ OPENVINO_ASSERT(!external_data_stream.fail(), "Failed to open external data file: ", full_path);
- constexpr size_t lazy_loading_threshold = 0x100000; // 1MB
- return read_data_length >= lazy_loading_threshold ? get_lazy_buffer() : get_now_buffer();
+ external_data_stream.seekg(m_offset, std::ios::beg);
+ external_data_stream.read(read_data->get_ptr<char>(), read_data_length);
+ const auto read_valid =
+ external_data_stream && static_cast<size_t>(external_data_stream.gcount()) == read_data_length;
+ OPENVINO_ASSERT(read_valid, "Failed to read external data from ", full_path);
+ }
+ return std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(read_data->get_ptr<char>(),
+ read_data->size(),
+ read_data);
}
Buffer<ov::AlignedBuffer> TensorExternalData::load_external_mem_data() const {