OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core][GPU] Prefetch on model import for GPU plugin (#39261)
Oleg Pipikinda679fa13813
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hppindex fdeda45271a..f8b77e27815 100644--- a/src/common/util/include/openvino/util/memory.hpp+++ b/src/common/util/include/openvino/util/memory.hpp@@ -191,8 +191,11 @@ public:/** @brief Hint to release the underlying memory if possible (e.g. unmaps/decommits). */virtual void hint_evict() noexcept = 0;- /** @brief Hint to fetch the data to memory. */- virtual void hint_prefetch() noexcept = 0;+ /**+ * @brief Hint to start fetching the data to memory in the background, returns immediately.+ * @return A handle to wait for the data to be resident; an invalid handle means there is nothing to wait for.+ */+ virtual PrefetchToken hint_prefetch() noexcept = 0;};/**diff --git a/src/common/util/include/openvino/util/mmap_object.hpp b/src/common/util/include/openvino/util/mmap_object.hppindex 27b9f4034ae..c22de8bbf41 100644--- a/src/common/util/include/openvino/util/mmap_object.hpp+++ b/src/common/util/include/openvino/util/mmap_object.hpp@@ -69,7 +69,9 @@ public:* mapping when set to auto_size.*/virtual void hint_prefetch(size_t offset, size_t size) noexcept = 0;- void hint_prefetch() noexcept override final;++ /** @brief Starts asynchronous prefetching of the entire mapping and returns its completion handle. */+ util::PrefetchToken hint_prefetch() noexcept override final;/*** @brief Asynchronous variant of @ref hint_prefetch: starts populating the region in thediff --git a/src/common/util/src/mmap_object.cpp b/src/common/util/src/mmap_object.cppindex d37e802e48c..9414a41b2a5 100644--- a/src/common/util/src/mmap_object.cpp+++ b/src/common/util/src/mmap_object.cpp@@ -15,8 +15,8 @@ void MappedMemory::hint_evict() noexcept {hint_evict(0, auto_size);}-void MappedMemory::hint_prefetch() noexcept {- hint_prefetch(0, auto_size);+util::PrefetchToken MappedMemory::hint_prefetch() noexcept {+ return hint_prefetch_async(0, auto_size);}} // namespace ovdiff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txtindex 3a6fcf01d45..9f76073b635 100644--- a/src/core/CMakeLists.txt+++ b/src/core/CMakeLists.txt@@ -21,7 +21,8 @@ add_subdirectory(xml_util)set(MIXED_SRC"${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/allocator.cpp""${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/itensor.cpp"- "${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/tensor.cpp")+ "${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/tensor.cpp"+ "${CMAKE_CURRENT_SOURCE_DIR}/src/weights_prefetch.cpp")set_property(SOURCE ${MIXED_SRC}APPEND PROPERTY INCLUDE_DIRECTORIESdiff --git a/src/core/dev_api/openvino/core/weights_prefetch.hpp b/src/core/dev_api/openvino/core/weights_prefetch.hppindex 1f096b8f9cc..f13b013a51a 100644--- a/src/core/dev_api/openvino/core/weights_prefetch.hpp+++ b/src/core/dev_api/openvino/core/weights_prefetch.hpp@@ -12,6 +12,7 @@#include "openvino/core/core_visibility.hpp"#include "openvino/op/constant.hpp"+#include "openvino/runtime/tensor.hpp"#include "openvino/util/memory.hpp"namespace ov::util {@@ -88,4 +89,42 @@ private:size_t m_in_flight; // Number of constants currently prefetched and not released.size_t m_in_flight_bytes; // Bytes of the m_in_flight constants.};++/**+ * @brief Prefetches a tensor read front to back in a window ahead of the reader and evicts what was read.+ *+ * Hints take effect only for tensors having memory hints, see ov::get_memory_hints(). Not thread safe.+ */+class OPENVINO_API SequentialPrefetchScheduler {+public:+ /**+ * @param tensor 1D u8 tensor being read.+ * @param window Number of bytes kept prefetched ahead of the read position.+ * @param step Granularity of the hints; small hints are ignored by the file mappings.+ *+ * Default window and step showed the best import time and peak RSS in manual testing.+ */+ explicit SequentialPrefetchScheduler(const ov::Tensor& tensor,+ size_t window = 32 * one_mib,+ size_t step = 8 * one_mib);++ /** @brief Reports that [begin, end) is read next and the bytes before @p begin are consumed. */+ void advance(size_t begin, size_t end) noexcept;++ /** @brief Stops evicting, for when the read memory stays referenced (e.g. zero-copy import). */+ void keep_read_data() noexcept {+ m_evict = false;+ }++private:+ std::shared_ptr<IMemoryHints> chunk_hints(size_t begin, size_t end) const noexcept;++ const ov::Tensor m_tensor; // Tensor being read.+ size_t m_size; // Tensor size in bytes.+ size_t m_window; // Bytes to prefetch ahead of the read position.+ size_t m_step; // Granularity of prefetch and eviction chunks.+ size_t m_prefetched; // End offset of the prefetched region.+ size_t m_evicted; // End offset of the evicted region.+ bool m_evict; // Whether consumed data should be evicted.+};} // namespace ov::utildiff --git a/src/core/src/weights_prefetch.cpp b/src/core/src/weights_prefetch.cppindex c6d4c9cc6a5..841d58e341f 100644--- a/src/core/src/weights_prefetch.cpp+++ b/src/core/src/weights_prefetch.cpp@@ -4,15 +4,66 @@#include "openvino/core/weights_prefetch.hpp"+#include <algorithm>+#include <exception>+#include <tuple>#include <utility>+#include "openvino/core/except.hpp"#include "openvino/core/node.hpp"#include "openvino/core/type.hpp"#include "openvino/core/weight_sharing_util.hpp"#include "openvino/op/constant.hpp"+#include "openvino/runtime/make_tensor.hpp"namespace ov::util {+SequentialPrefetchScheduler::SequentialPrefetchScheduler(const ov::Tensor& tensor, size_t window, size_t step)+ : m_tensor(tensor),+ m_size(m_tensor.get_byte_size()),+ m_window(window),+ m_step(std::max<size_t>(step, 1)),+ m_prefetched(0),+ m_evicted(0),+ m_evict(true) {+ // Chunks are cut with a byte ROI, which needs a 1D u8 tensor.+ const bool is_1d_byte_tensor = m_tensor.get_element_type() == ov::element::u8 && m_tensor.get_shape().size() == 1;+ OPENVINO_ASSERT(is_1d_byte_tensor, "Prefetch tensor must be 1D u8");+ advance(0, 0);+}++std::shared_ptr<IMemoryHints> SequentialPrefetchScheduler::chunk_hints(size_t begin, size_t end) const noexcept {+ try {+ return ov::get_memory_hints(ov::Tensor{m_tensor, ov::Coordinate{begin}, ov::Coordinate{end}});+ } catch (const std::exception&) {+ return {};+ }+}++void SequentialPrefetchScheduler::advance(size_t begin, size_t end) noexcept {+ begin = std::min(begin, m_size);+ end = std::min(std::max(begin, end), m_size);+ if (m_evict) {+ // Whole steps only, the read position may be in the middle of a page.+ const auto evict_end = begin == m_size ? m_size : begin / m_step * m_step;+ if (evict_end > m_evicted) {+ if (const auto hints = chunk_hints(m_evicted, evict_end)) {+ hints->hint_evict();+ }+ m_evicted = evict_end;+ }+ }+ m_prefetched = std::max(m_prefetched, begin);+ const auto target = std::min(end + m_window, m_size);+ while (m_prefetched < target) {+ const auto chunk_end = std::min(m_prefetched + m_step, m_size);+ if (const auto hints = chunk_hints(m_prefetched, chunk_end)) {+ std::ignore = hints->hint_prefetch();+ }+ m_prefetched = chunk_end;+ }+}+OrderedPrefetchScheduler::OrderedPrefetchScheduler(const std::vector<std::shared_ptr<ov::Node>>& ops,size_t lookahead,size_t min_bytes)diff --git a/src/core/tests/file_load_benchmark.cpp b/src/core/tests/file_load_benchmark.cppindex 25e392b8406..875af203091 100644--- a/src/core/tests/file_load_benchmark.cpp+++ b/src/core/tests/file_load_benchmark.cpp@@ -199,7 +199,7 @@ namespace strategy {// Note: the mmap destructor (munmap + close) runs inside the timed window;void sync_vm_prefetch_mem_lock(const std::filesystem::path& path, size_t /*file_size*/) {auto mapped = load_mmap_object(path);- mapped->hint_prefetch();+ mapped->hint_prefetch(0, ov::auto_size);ensure_memory_resident(mapped); // should be near no-op and just lock/unlock resident pages}@@ -241,7 +241,7 @@ void mmap_then_compute(const std::filesystem::path& path, size_t /*file_size*/)void mmap_prefetch_then_compute(const std::filesystem::path& path, size_t /*file_size*/) {auto mapped = load_mmap_object(path);- mapped->hint_prefetch();+ mapped->hint_prefetch(0, ov::auto_size);compute_over_mapped(mapped);}diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cppindex 6ac07c1954c..a18c8fdbaa8 100644--- a/src/core/tests/ov_tensor_test.cpp+++ b/src/core/tests/ov_tensor_test.cpp@@ -4,10 +4,12 @@#include <gmock/gmock.h>+#include <chrono>#include <cstdint>#include <cstring>#include <filesystem>#include <fstream>+#include <future>#include <numeric>#include <utility>#include <vector>@@ -1381,13 +1383,33 @@ ov::Tensor make_buffer_tensor(const ov::element::Type& type,}} // namespace+TEST_F(OVTensorTest, mappedMemoryPrefetchForwardsCompletionToken) {+ const auto mapping = std::make_shared<MockMappedMemory>(64);+ std::shared_ptr<ov::MappedMemory> memory = mapping;+ std::shared_ptr<ov::util::IMemoryHints> hints = mapping;+ std::promise<void> completion;+ const auto expected = completion.get_future().share();++ EXPECT_CALL(*mapping, hint_prefetch_async(0, ov::auto_size)).Times(2).WillRepeatedly(testing::Return(expected));+ const auto memory_token = memory->hint_prefetch();+ const auto hints_token = hints->hint_prefetch();+ ASSERT_TRUE(memory_token.valid());+ ASSERT_TRUE(hints_token.valid());++ EXPECT_EQ(memory_token.wait_for(std::chrono::seconds(0)), std::future_status::timeout);+ EXPECT_EQ(hints_token.wait_for(std::chrono::seconds(0)), std::future_status::timeout);+ completion.set_value();+ EXPECT_EQ(memory_token.wait_for(std::chrono::seconds(0)), std::future_status::ready);+ EXPECT_EQ(hints_token.wait_for(std::chrono::seconds(0)), std::future_status::ready);+}+TEST_F(OVTensorTest, bufferTensorViewsBufferAndTakesItsId) {auto mapping = std::make_shared<MockMappedMemory>(64);const auto tensor = make_buffer_tensor(ov::element::u8, ov::Shape{64}, mapping);EXPECT_EQ(tensor.data(), static_cast<const void*>(mapping->data()));EXPECT_EQ(ov::get_tensor_source_id(tensor), std::optional<uint64_t>{7u});- EXPECT_CALL(*mapping, hint_prefetch(0, ov::auto_size));+ EXPECT_CALL(*mapping, hint_prefetch_async(0, ov::auto_size));auto hints = ov::get_memory_hints(tensor);EXPECT_EQ(hints.get(), static_cast<ov::util::IMemoryHints*>(mapping.get()));hints->hint_prefetch();@@ -1413,9 +1435,9 @@ TEST_F(OVTensorTest, memoryHintsCoverTensorRangeOfMapping) {const auto tensor = make_buffer_tensor(ov::element::u8, ov::Shape{1000}, mapping);const ov::Tensor roi{tensor, {100}, {300}};- EXPECT_CALL(*mapping, hint_prefetch(0, 1000));+ EXPECT_CALL(*mapping, hint_prefetch_async(0, 1000));EXPECT_CALL(*mapping, hint_evict(0, 1000));- EXPECT_CALL(*mapping, hint_prefetch(100, 200));+ EXPECT_CALL(*mapping, hint_prefetch_async(100, 200));EXPECT_CALL(*mapping, hint_evict(100, 200));auto hints = ov::get_memory_hints(tensor);diff --git a/src/core/tests/shared_buffer.cpp b/src/core/tests/shared_buffer.cppindex 201946323e2..dbb24003ce7 100644--- a/src/core/tests/shared_buffer.cpp+++ b/src/core/tests/shared_buffer.cpp@@ -16,6 +16,7 @@#include "common_test_utils/common_utils.hpp"#include "openvino/core/weights_prefetch.hpp"#include "openvino/op/constant.hpp"+#include "openvino/runtime/make_tensor.hpp"#include "openvino/util/mmap_object.hpp"namespace ov::test {@@ -723,4 +724,34 @@ TEST_F(SharedBufferTest, prefetch_scheduler_ignores_non_file_backed_constants_inov::util::OrderedPrefetchScheduler scheduler(ops, 2, 0);::testing::Mock::VerifyAndClearExpectations(mock.get());}++TEST_F(SharedBufferTest, sequential_prefetch_scheduler_slides_window) {+ constexpr size_t region_offset = 64;+ auto mock = std::make_shared<MockMappedMemory>(1024);+ const auto mapped = ov::make_tensor(ov::make_tensor(ov::element::u8, ov::Shape{1024}, mock));+ {+ // Region [64, 964) of the mapping, window = 200, step = 100; offsets below are relative to the mapping.+ ::testing::InSequence in_sequence;+ EXPECT_CALL(*mock, hint_prefetch_async(region_offset + 0, 100));+ EXPECT_CALL(*mock, hint_prefetch_async(region_offset + 100, 100));+ EXPECT_CALL(*mock, hint_prefetch_async(region_offset + 200, 100));+ EXPECT_CALL(*mock, hint_prefetch_async(region_offset + 300, 100));+ EXPECT_CALL(*mock, hint_prefetch_async(region_offset + 400, 100));+ EXPECT_CALL(*mock, hint_prefetch_async(region_offset + 500, 100));+ EXPECT_CALL(*mock, hint_evict_mock(region_offset + 0, 300));+ EXPECT_CALL(*mock, hint_evict_mock(region_offset + 300, 600));+ }++ ov::util::SequentialPrefetchScheduler scheduler(ov::Tensor{mapped, {region_offset}, {region_offset + 900}},+ 200,+ 100);+ scheduler.advance(0, 50); // window reaches 250+ scheduler.advance(50, 350); // a large read extends the window past its end+ scheduler.advance(350, 360); // whole steps before 350 are evicted+ scheduler.advance(900, 900); // the end evicts the tail+}++TEST_F(SharedBufferTest, sequential_prefetch_scheduler_rejects_non_byte_tensor) {+ EXPECT_THROW(ov::util::SequentialPrefetchScheduler(ov::Tensor{ov::element::f32, ov::Shape{4}}), ov::Exception);+}} // namespace ov::testdiff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cppindex de4806710c2..7fd2bb46e30 100644--- a/src/inference/src/dev/make_tensor.cpp+++ b/src/inference/src/dev/make_tensor.cpp@@ -54,8 +54,8 @@ public:m_memory->hint_evict(m_offset, m_size);}- void hint_prefetch() noexcept override {- m_memory->hint_prefetch(m_offset, m_size);+ util::PrefetchToken hint_prefetch() noexcept override {+ return m_memory->hint_prefetch_async(m_offset, m_size);}private:diff --git a/src/plugins/intel_gpu/src/graph/common_utils/prefetch_streambuf.cpp b/src/plugins/intel_gpu/src/graph/common_utils/prefetch_streambuf.cppnew file mode 100644index 00000000000..45b68077bd1--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/common_utils/prefetch_streambuf.cpp@@ -0,0 +1,66 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "prefetch_streambuf.hpp"++#include <algorithm>+#include <cstring>++#include "openvino/runtime/make_tensor.hpp"++namespace ov::intel_gpu {++PrefetchStreamBuf::PrefetchStreamBuf(const ov::Tensor& tensor, bool prefetch)+ : m_begin(static_cast<const char*>(tensor.data())),+ m_current(m_begin),+ m_end(m_begin + tensor.get_byte_size()) {+ if (prefetch && ov::get_memory_hints(tensor)) {+ m_scheduler = std::make_unique<ov::util::SequentialPrefetchScheduler>(tensor);+ }+}++PrefetchStreamBuf::int_type PrefetchStreamBuf::underflow() {+ return m_current < m_end ? traits_type::to_int_type(*m_current) : traits_type::eof();+}++PrefetchStreamBuf::int_type PrefetchStreamBuf::uflow() {+ return m_current < m_end ? traits_type::to_int_type(*m_current++) : traits_type::eof();+}++std::streamsize PrefetchStreamBuf::showmanyc() {+ return m_current < m_end ? m_end - m_current : -1;+}++std::streamsize PrefetchStreamBuf::xsgetn(char_type* dst, std::streamsize count) {+ const auto n = std::min<std::streamsize>(count, m_end - m_current);+ if (n <= 0) {+ return 0;+ }+ const auto pos = static_cast<size_t>(m_current - m_begin);+ if (m_scheduler) {+ m_scheduler->advance(pos, pos + static_cast<size_t>(n));+ }+ std::memcpy(dst, m_current, static_cast<size_t>(n));+ m_current += n;+ return n;+}++PrefetchStreamBuf::pos_type PrefetchStreamBuf::seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) {+ if (!(which & std::ios_base::in)) {+ return pos_type(off_type(-1));+ }+ const off_type base = dir == std::ios_base::beg ? 0 : dir == std::ios_base::cur ? m_current - m_begin : m_end - m_begin;+ const off_type pos = base + off;+ if (pos < 0 || pos > m_end - m_begin) {+ return pos_type(off_type(-1));+ }+ m_current = m_begin + pos;+ return pos_type(pos);+}++PrefetchStreamBuf::pos_type PrefetchStreamBuf::seekpos(pos_type pos, std::ios_base::openmode which) {+ return seekoff(off_type(pos), std::ios_base::beg, which);+}++} // namespace ov::intel_gpudiff --git a/src/plugins/intel_gpu/src/graph/common_utils/prefetch_streambuf.hpp b/src/plugins/intel_gpu/src/graph/common_utils/prefetch_streambuf.hppnew file mode 100644index 00000000000..20b420bffe2--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/common_utils/prefetch_streambuf.hpp@@ -0,0 +1,45 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <memory>+#include <streambuf>++#include "openvino/core/weights_prefetch.hpp"+#include "openvino/runtime/tensor.hpp"++namespace ov::intel_gpu {++/**+ * @brief Read-only stream buffer over a tensor that drives a SequentialPrefetchScheduler when @p prefetch is set and+ * the tensor has memory hints, and is a plain memory reader otherwise.+ */+class PrefetchStreamBuf : public std::streambuf {+public:+ explicit PrefetchStreamBuf(const ov::Tensor& tensor, bool prefetch = true);++ void keep_read_data() noexcept {+ if (m_scheduler) {+ m_scheduler->keep_read_data();+ }+ }++protected:+ int_type underflow() override;+ int_type uflow() override;+ std::streamsize showmanyc() override;+ std::streamsize xsgetn(char_type* dst, std::streamsize count) override;+ pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override;+ pos_type seekpos(pos_type pos, std::ios_base::openmode which) override;++private:+ // The base get area is unused: setg() takes non-const pointers and blobs may be read-only.+ const char* m_begin;+ const char* m_current;+ const char* m_end;+ std::unique_ptr<ov::util::SequentialPrefetchScheduler> m_scheduler;+};++} // namespace ov::intel_gpudiff --git a/src/plugins/intel_gpu/src/graph/program.cpp b/src/plugins/intel_gpu/src/graph/program.cppindex 8e02e0370f8..6ce55d38514 100644--- a/src/plugins/intel_gpu/src/graph/program.cpp+++ b/src/plugins/intel_gpu/src/graph/program.cpp@@ -8,6 +8,7 @@#include "registry/implementation_manager.hpp"#include "intel_gpu/runtime/internal_properties.hpp"#include "openvino/core/type.hpp"+#include "openvino/core/weights_prefetch.hpp"#include "openvino/runtime/system_conf.hpp"#include "openvino/runtime/threading/cpu_streams_info.hpp"#include "openvino/util/file_util.hpp"@@ -15,6 +16,7 @@#include "openvino/core/memory_util.hpp"#include "openvino/util/parallel_read_streambuf.hpp"#include "common_utils/parallel_mem_streambuf.hpp"+#include "common_utils/prefetch_streambuf.hpp"#include "intel_gpu/runtime/memory.hpp"#include "intel_gpu/runtime/engine.hpp"@@ -2034,6 +2036,10 @@ void program::load(cldnn::BinaryInputBuffer& ib,ib.get_stream_size(),allocation_type::usm_host,layout({{static_cast<tensor::value_type>(ib.get_stream_size()), 1, 1, 1}, data_types::u8, format::bfyx}));+ // Weights become sub-buffers of the blob, so its pages must stay resident.+ if (auto* prefetch_buf = dynamic_cast<ov::intel_gpu::PrefetchStreamBuf*>(ib.get_streambuf())) {+ prefetch_buf->keep_read_data();+ }}size_t num_nodes;diff --git a/src/plugins/intel_gpu/src/graph/sources.cmake b/src/plugins/intel_gpu/src/graph/sources.cmakeindex 549679a99f9..600c97ce3a8 100644--- a/src/plugins/intel_gpu/src/graph/sources.cmake+++ b/src/plugins/intel_gpu/src/graph/sources.cmake@@ -23,6 +23,8 @@ set(GPU_GRAPH_SOURCES${CMAKE_CURRENT_LIST_DIR}/common_utils/kernel_generator_base.hpp${CMAKE_CURRENT_LIST_DIR}/common_utils/parallel_mem_streambuf.cpp${CMAKE_CURRENT_LIST_DIR}/common_utils/parallel_mem_streambuf.hpp+ ${CMAKE_CURRENT_LIST_DIR}/common_utils/prefetch_streambuf.cpp+ ${CMAKE_CURRENT_LIST_DIR}/common_utils/prefetch_streambuf.hpp${CMAKE_CURRENT_LIST_DIR}/compilation_context.cpp${CMAKE_CURRENT_LIST_DIR}/concatenation.cpp${CMAKE_CURRENT_LIST_DIR}/condition.cppdiff --git a/src/plugins/intel_gpu/src/plugin/plugin.cpp b/src/plugins/intel_gpu/src/plugin/plugin.cppindex d9467605a33..b308bf83694 100644--- a/src/plugins/intel_gpu/src/plugin/plugin.cpp+++ b/src/plugins/intel_gpu/src/plugin/plugin.cpp@@ -15,6 +15,7 @@#include <vector>#include "common_utils/parallel_mem_streambuf.hpp"+#include "common_utils/prefetch_streambuf.hpp"#include "intel_gpu/graph/serialization/layout_serializer.hpp"#include "intel_gpu/graph/serialization/string_serializer.hpp"#include "intel_gpu/graph/serialization/utils.hpp"@@ -30,6 +31,7 @@#include "intel_gpu/runtime/profiling.hpp"#include "openvino/core/any.hpp"#include "openvino/core/deprecated.hpp"+#include "openvino/core/weights_prefetch.hpp"#include "openvino/op/util/op_types.hpp"#include "openvino/pass/manager.hpp"#include "openvino/pass/visualize_tree.hpp"@@ -537,8 +539,20 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& modelconst ov::AnyMap& config) const{// Store tensor base in thread-local storageg_current_tensor_base = static_cast<const size_t*>(model.data());- ov::intel_gpu::ParallelMemStreamBuf par_buf(model.data(), model.get_byte_size());- std::istream stream(&par_buf);+ std::unique_ptr<std::streambuf> buf;+#ifdef _WIN32+ // TODO: CVS-198510 enable on Windows once prefetch shows a compile-time gain there.+ const bool use_prefetch = false;+#else+ const bool use_prefetch = static_cast<bool>(ov::get_memory_hints(model));+#endif+ if (!use_prefetch) {+ // Memory without hints (e.g. user provided), keep the pread path for user file mappings.+ buf = std::make_unique<ov::intel_gpu::ParallelMemStreamBuf>(model.data(), model.get_byte_size());+ } else {+ buf = std::make_unique<ov::intel_gpu::PrefetchStreamBuf>(model);+ }+ std::istream stream(buf.get());auto compiled_model = import_model(stream, context, config);// Clear thread-local storagediff --git a/src/plugins/intel_gpu/tests/unit/sources.cmake b/src/plugins/intel_gpu/tests/unit/sources.cmakeindex 188ccacc57e..337e15508b4 100644--- a/src/plugins/intel_gpu/tests/unit/sources.cmake+++ b/src/plugins/intel_gpu/tests/unit/sources.cmake@@ -301,6 +301,7 @@ set(GPU_UNIT_TESTS_SRCS${CMAKE_CURRENT_LIST_DIR}/test_utils/network_test.h${CMAKE_CURRENT_LIST_DIR}/test_utils/opencl_helper_instance.hpp${CMAKE_CURRENT_LIST_DIR}/test_utils/parallel_mem_streambuf_test.cpp+ ${CMAKE_CURRENT_LIST_DIR}/test_utils/prefetch_streambuf_test.cpp${CMAKE_CURRENT_LIST_DIR}/test_utils/primitive_inst_test_helper.h${CMAKE_CURRENT_LIST_DIR}/test_utils/program_wrapper.h${CMAKE_CURRENT_LIST_DIR}/test_utils/random_gen.hdiff --git a/src/plugins/intel_gpu/tests/unit/test_utils/prefetch_streambuf_test.cpp b/src/plugins/intel_gpu/tests/unit/test_utils/prefetch_streambuf_test.cppnew file mode 100644index 00000000000..c0429a6a629--- /dev/null+++ b/src/plugins/intel_gpu/tests/unit/test_utils/prefetch_streambuf_test.cpp@@ -0,0 +1,42 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "common_utils/prefetch_streambuf.hpp"++#include <gtest/gtest.h>++#include <filesystem>+#include <fstream>+#include <istream>+#include <vector>++#include "common_test_utils/common_utils.hpp"+#include "openvino/runtime/tensor.hpp"++TEST(PrefetchStreamBufTest, ReadsReadOnlyMappedRoiTensor) {+ constexpr size_t k_size = 64 * 1024;+ constexpr size_t k_header = 16;+ std::vector<char> content(k_size);+ for (size_t i = 0; i < k_size; ++i) {+ content[i] = static_cast<char>(i % 251u);+ }++ const auto path = std::filesystem::path(ov::test::utils::generateTestFilePrefix() + "_prefetch_streambuf.bin");+ {+ std::ofstream ofs(path, std::ios::binary);+ ASSERT_TRUE(ofs.write(content.data(), static_cast<std::streamsize>(k_size)));+ }+ {+ // Same shape as a cache blob: a read-only mmap tensor with the header cut off by a ROI.+ const auto blob = ov::read_tensor_data(path);+ const ov::Tensor roi{blob, {k_header}, {k_size}};++ ov::intel_gpu::PrefetchStreamBuf buf(roi);+ std::istream stream(&buf);+ std::vector<char> got(k_size - k_header);+ ASSERT_TRUE(stream.read(got.data(), static_cast<std::streamsize>(got.size())));+ EXPECT_EQ(got, std::vector<char>(content.begin() + k_header, content.end()));+ }+ std::filesystem::remove(path);+}