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
[GPU] create tensor from file (#37153)
Michal Miotk7c4d2733dc05
diff --git a/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp b/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cppindex b62af47e349..e435a124c0b 100644--- a/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp+++ b/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp@@ -69,6 +69,17 @@ int main() {//! [wrap_cpu_pointer]}+{+ //! [wrap_file]+ // The plugin memory-maps the file and keeps the mapping alive for the tensor lifetime,+ // so the file must not be modified until the returned tensor is destroyed.+ ov::intel_gpu::FileDescriptor file_descriptor{"input.bin",+ /*offset_in_bytes=*/0,+ ov::intel_gpu::AccessMode::READ};+ auto remote_tensor = gpu_context.create_tensor(in_element_type, in_shape, file_descriptor);+ //! [wrap_file]+}+{//! [wrap_cl_mem]cl_mem shared_buffer = allocate_cl_mem(input_size);diff --git a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rst b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rstindex 8a014404459..af79aeeef33 100644--- a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rst+++ b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rst@@ -233,6 +233,19 @@ For more details, see the code snippets below::language: cpp:fragment: [wrap_usm_pointer]+ .. tab-item:: CPU pointer+ :sync: cpu-pointer++ Use this overload when your application owns a CPU virtual address, for example memory+ allocated with ``ov::util::aligned_alloc`` or memory mapped from a file. On the OpenCL+ backend, the pointer address and allocation size must be aligned to+ ``ov::intel_gpu::cacheline_size``. The memory must remain valid for the whole lifetime of+ the created remote tensor.++ .. doxygensnippet:: docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp+ :language: cpp+ :fragment: [wrap_cpu_pointer]+.. tab-item:: cl_mem:sync: cl-mem@@ -267,6 +280,20 @@ For more details, see the code snippets below:The ``shape`` and ``element type`` must describe the same memory layout as the external buffer.The handle must remain valid for the whole lifetime of the created remote tensor.+ .. tab-item:: file+ :sync: file++ Use this overload to wrap tensor data stored in a file. The access mode is declared in the+ descriptor: ``AccessMode::READ`` maps the file read-only and may only be used as an inference+ input, while ``AccessMode::READ_WRITE`` requires a writable file, may be used as an inference+ output, and makes changes done through the tensor visible in the file. The plugin keeps the+ mapping alive for the whole lifetime of the created remote tensor, so the file must not+ otherwise be modified until the tensor is destroyed.++ .. doxygensnippet:: docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp+ :language: cpp+ :fragment: [wrap_file]+.. tab-item:: biplanar NV12 surface:sync: biplanar-nv12-surfacediff --git a/src/common/util/include/openvino/util/mmap_object.hpp b/src/common/util/include/openvino/util/mmap_object.hppindex 93a5803ffe3..a1a0c979265 100644--- a/src/common/util/include/openvino/util/mmap_object.hpp+++ b/src/common/util/include/openvino/util/mmap_object.hpp@@ -22,6 +22,11 @@ namespace ov {namespace util {int64_t get_system_page_size();++/**+ * @brief Alignment required for a memory mapping offset: allocation granularity on Windows, page size elsewhere.+ */+size_t get_system_alloc_granularity();} // namespace util/**@@ -29,6 +34,19 @@ int64_t get_system_page_size();*/inline constexpr auto auto_size = std::numeric_limits<size_t>::max();+/**+ * @brief Access mode of a memory mapping.+ */+enum class MmapMode {+ READ, //!< Read-only mapping.+ READ_WRITE //!< Read-write mapping, modifications are written back to the file.+};++/**+ * @brief Id reported by mappings whose content is mutable, so they must never be substituted for one another.+ */+inline constexpr uint64_t no_mapping_id = 0;+/*** @brief This class represents a mapped memory.* Instead of reading files, we can map the memory via mmap for Linux or MapViewOfFile for Windows.@@ -73,12 +91,15 @@ public:* @param no_placeholder When true, skip the Windows 10+ placeholder/VEH mechanism and use the legacy* single-call MapViewOfFile path instead. This guarantees a uniform AllocationBase* across the whole mapping, required for NPU zero-copy blob import. On Linux ignored.+ * @param mode Access mode of the mapping. A read_write mapping requires the file to be writable and reports+ * no_mapping_id, because get_id() marks an immutable data source that consumers may share.* @return MappedMemory shared ptr object which keep mmaped memory and control the lifetime.*/std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::filesystem::path& path,size_t offset = 0,size_t size = auto_size,- bool no_placeholder = false);+ bool no_placeholder = false,+ MmapMode mode = MmapMode::READ);/*** @brief Returns mapped memory for a file from provided file handle (cross-platform).diff --git a/src/common/util/src/os/lin/lin_mmap_object.cpp b/src/common/util/src/os/lin/lin_mmap_object.cppindex b1a43bc0596..59d3ec06a3e 100644--- a/src/common/util/src/os/lin/lin_mmap_object.cpp+++ b/src/common/util/src/os/lin/lin_mmap_object.cpp@@ -30,6 +30,10 @@ int64_t get_system_page_size() {return page_size;}+size_t get_system_alloc_granularity() {+ return static_cast<size_t>(get_system_page_size());+}+/*** @brief Creates a memory region for mmap operations.*@@ -141,18 +145,18 @@ class MapHolder final : public MappedMemory {public:MapHolder() = default;- void set(const std::filesystem::path& path, const size_t offset, const size_t size) {- int mode = O_RDONLY;+ void set(const std::filesystem::path& path, const size_t offset, const size_t size, const MmapMode mmap_mode) {+ int mode = (mmap_mode == MmapMode::READ_WRITE) ? O_RDWR : O_RDONLY;int fd = open(path.c_str(), mode);if (fd == -1) {throw std::runtime_error("Can not open file " + util::path_to_string(path) +" for mapping. Ensure that file exists and has appropriate permissions.");}- set_from_fd(fd, offset, size);- m_id = util::get_id_for_file(path, offset, size);+ set_from_fd(fd, offset, size, mmap_mode);+ m_id = (mmap_mode == MmapMode::READ_WRITE) ? no_mapping_id : util::get_id_for_file(path, offset, size);}- void set_from_fd(const int fd, const size_t offset, const size_t size) {+ void set_from_fd(const int fd, const size_t offset, const size_t size, const MmapMode mmap_mode = MmapMode::READ) {m_handle = HandleHolder(fd);struct stat sb = {};@@ -166,17 +170,21 @@ public:}if (m_size > 0) {+ const auto prot = (mmap_mode == MmapMode::READ_WRITE) ? (PROT_READ | PROT_WRITE) : PROT_READ;const auto& [aligned_offset, length, gap] = util::make_mmap_region(offset, m_size);m_mapped_view_size = length;- m_mapped_view = mmap(nullptr, length, PROT_READ, MAP_SHARED, fd, aligned_offset);+ m_mapped_view = mmap(nullptr, length, prot, MAP_SHARED, fd, aligned_offset);if (m_mapped_view == MAP_FAILED) {throw std::runtime_error("Can not create file mapping for " + std::to_string(fd) +", err=" + std::strerror(errno));}m_data = static_cast<char*>(m_mapped_view) + gap;}- m_id =- util::u64_hash_combine(static_cast<uint64_t>(sb.st_ino), {static_cast<uint64_t>(sb.st_dev), offset, size});+ // A read-write mapping is not an immutable data source, so it must not be shared through id-based caches.+ m_id = (mmap_mode == MmapMode::READ_WRITE)+ ? no_mapping_id+ : util::u64_hash_combine(static_cast<uint64_t>(sb.st_ino),+ {static_cast<uint64_t>(sb.st_dev), offset, size});}uint64_t get_id() const noexcept override {@@ -231,9 +239,10 @@ public:std::shared_ptr<MappedMemory> load_mmap_object(const std::filesystem::path& path,size_t offset,size_t size,- bool /* no_placeholder */) {+ bool /* no_placeholder */,+ MmapMode mode) {auto holder = std::make_shared<MapHolder>();- holder->set(path, offset, size);+ holder->set(path, offset, size, mode);return holder;}diff --git a/src/common/util/src/os/win/win_mmap_object.cpp b/src/common/util/src/os/win/win_mmap_object.cppindex c78fd40c24d..d8d9e793260 100644--- a/src/common/util/src/os/win/win_mmap_object.cpp+++ b/src/common/util/src/os/win/win_mmap_object.cpp@@ -288,7 +288,11 @@ public:MapHolder() = default;~MapHolder() override;- void set(const std::filesystem::path& path, size_t offset, size_t size, bool no_placeholder = false);+ void set(const std::filesystem::path& path,+ size_t offset,+ size_t size,+ bool no_placeholder = false,+ MmapMode mode = MmapMode::READ);void set_from_handle(FileHandle handle, size_t offset, size_t size);bool try_remap_slot(uintptr_t fault_addr);@@ -334,7 +338,7 @@ private:void set_id(HANDLE h, size_t offset, size_t size);/** @brief Core setup shared by set() and set_from_handle(). */- void setup(HANDLE file_handle, size_t offset, size_t size, bool no_placeholder);+ void setup(HANDLE file_handle, size_t offset, size_t size, bool no_placeholder, MmapMode mode);/** @brief Try to establish the placeholder mapping.* Returns true on success; caller falls back to legacy path on false.@@ -342,7 +346,7 @@ private:bool try_placeholder_setup(size_t aligned_offset, size_t head_pad, size_t total_va_size, size_t file_size);/** @brief Legacy single-call MapViewOfFile path (no partial-release support). */- void legacy_setup(size_t aligned_offset, size_t head_pad, size_t size);+ void legacy_setup(size_t aligned_offset, size_t head_pad, size_t size, MmapMode mode);/*** @brief Computes the clamped, gran-aligned VA range to evict.@@ -614,9 +618,10 @@ bool MapHolder::try_placeholder_setup(size_t aligned_offset, size_t head_pad, sireturn true;}-void MapHolder::legacy_setup(size_t aligned_offset, size_t head_pad, size_t size) {+void MapHolder::legacy_setup(size_t aligned_offset, size_t head_pad, size_t size, MmapMode mode) {+ const DWORD access = (mode == MmapMode::READ_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ;if (auto view = ::MapViewOfFile(m_handle.get(),- FILE_MAP_READ,+ access,static_cast<DWORD>(aligned_offset >> 32),static_cast<DWORD>(aligned_offset & 0xFFFFFFFF),head_pad + size)) {@@ -627,7 +632,7 @@ void MapHolder::legacy_setup(size_t aligned_offset, size_t head_pad, size_t size}}-void MapHolder::setup(HANDLE file_handle, size_t offset, size_t size, bool no_placeholder) {+void MapHolder::setup(HANDLE file_handle, size_t offset, size_t size, bool no_placeholder, MmapMode mode) {LARGE_INTEGER file_size_li{};if (!::GetFileSizeEx(file_handle, &file_size_li)) {throw std::runtime_error{"GetFileSizeEx failed: " + std::to_string(::GetLastError())};@@ -646,39 +651,47 @@ void MapHolder::setup(HANDLE file_handle, size_t offset, size_t size, bool no_plconst size_t total_va_size = util::align_size_up(r_length, gran);set_id(file_handle, offset, size);+ if (mode == MmapMode::READ_WRITE) {+ // A read-write mapping is not an immutable data source, so it must not be shared through id-based caches.+ m_id = no_mapping_id;+ }if (m_size == 0) {return;}- // Create a read-only file-mapping object for the whole file.- m_handle = HandleHolder{::CreateFileMappingW(file_handle, nullptr, PAGE_READONLY, 0, 0, nullptr)};+ const DWORD protect = (mode == MmapMode::READ_WRITE) ? PAGE_READWRITE : PAGE_READONLY;+ m_handle = HandleHolder{::CreateFileMappingW(file_handle, nullptr, protect, 0, 0, nullptr)};if (!m_handle.valid()) {throw std::runtime_error{"CreateFileMappingW failed: " + std::to_string(::GetLastError())};}// When no_placeholder is set, skip the placeholder/VEH path to guarantee a single uniform AllocationBase// (required for NPU zero-copy blob import). Otherwise prefer placeholder for RSS reduction.- if (no_placeholder || !try_placeholder_setup(m_aligned_offset, head_pad, total_va_size, file_size)) {- legacy_setup(m_aligned_offset, head_pad, m_size);+ // RW mappings are ignored by the current VEH registration: the handler only remaps read faults.+ if (no_placeholder || mode == MmapMode::READ_WRITE ||+ !try_placeholder_setup(m_aligned_offset, head_pad, total_va_size, file_size)) {+ legacy_setup(m_aligned_offset, head_pad, m_size, mode);}}-void MapHolder::set(const std::filesystem::path& path, size_t offset, size_t size, bool no_placeholder) {- auto fh = ::CreateFileW(path.c_str(),- GENERIC_READ,- FILE_SHARE_READ | FILE_SHARE_DELETE,- nullptr,- OPEN_EXISTING,- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,- nullptr);+void MapHolder::set(const std::filesystem::path& path, size_t offset, size_t size, bool no_placeholder, MmapMode mode) {+ const bool writable = mode == MmapMode::READ_WRITE;+ auto fh = ::CreateFileW(+ path.c_str(),+ writable ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_READ,+ writable ? (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE) : (FILE_SHARE_READ | FILE_SHARE_DELETE),+ nullptr,+ OPEN_EXISTING,+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,+ nullptr);if (fh == INVALID_HANDLE_VALUE) {throw std::runtime_error{"Cannot open file: " + ov::util::path_to_string(path) +" error: " + std::to_string(::GetLastError())};}HandleHolder fh_holder{fh};- setup(fh, offset, size, no_placeholder);+ setup(fh, offset, size, no_placeholder, mode);// Keep the file handle alive so the section object can always resolve page faults// back to the original file data, even if the caller deletes or renames the file.// FILE_SHARE_DELETE allows std::filesystem::remove() to succeed while the mapping is alive.@@ -702,7 +715,7 @@ void MapHolder::set_from_handle(FileHandle handle, size_t offset, size_t size) {throw std::runtime_error{"DuplicateHandle failed: " + std::to_string(::GetLastError())};}HandleHolder owned{dup};- setup(owned.get(), offset, size, false);+ setup(owned.get(), offset, size, false, MmapMode::READ);// owned goes out of scope here: file handle closed.// m_handle (section object) keeps the file data accessible independently.}@@ -947,9 +960,10 @@ void MapHolder::hint_evict(size_t offset, size_t size) noexcept {std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::filesystem::path& path,size_t offset,size_t size,- bool no_placeholder) {+ bool no_placeholder,+ MmapMode mode) {auto holder = std::make_shared<MapHolder>();- holder->set(path, offset, size, no_placeholder);+ holder->set(path, offset, size, no_placeholder, mode);return holder;}diff --git a/src/core/tests/mmap_object.cpp b/src/core/tests/mmap_object.cppindex 468912428e3..5c63bccddde 100644--- a/src/core/tests/mmap_object.cpp+++ b/src/core/tests/mmap_object.cpp@@ -240,6 +240,57 @@ INSTANTIATE_TEST_SUITE_P(MappedMemory,::testing::ValuesIn(std::vector<bool>{true, false})),RangedMappingTest::test_name);+class ReadWriteMappingTest : public ::testing::Test {+protected:+ std::filesystem::path m_file_path;+ std::vector<uint8_t> m_content;+ static constexpr size_t k_file_size = 128 * 1024;++ void SetUp() override {+ m_content = utils::make_modulo_sequence_pattern(k_file_size);+ m_file_path = utils::generateTestFilePrefix() + "_rw_mapping";+ ov::util::save_binary(m_file_path, m_content.data(), m_content.size());+ }++ void TearDown() override {+ std::filesystem::remove(m_file_path);+ }++ std::vector<uint8_t> read_file() const {+ std::vector<uint8_t> data(static_cast<size_t>(std::filesystem::file_size(m_file_path)));+ std::ifstream is(m_file_path, std::ios::binary);+ is.read(reinterpret_cast<char*>(data.data()), static_cast<std::streamsize>(data.size()));+ return data;+ }+};++TEST_F(ReadWriteMappingTest, writes_at_offset_leave_other_bytes_intact) {+ constexpr size_t k_offset = 64 * 1024;+ constexpr size_t k_size = 512;++ auto expected = m_content;+ std::fill_n(expected.begin() + k_offset, k_size, uint8_t{0x5A});+ ASSERT_NE(expected, m_content);++ {+ auto mm = load_mmap_object(m_file_path, k_offset, k_size, false, MmapMode::READ_WRITE);+ std::fill_n(reinterpret_cast<uint8_t*>(mm->data()), k_size, uint8_t{0x5A});+ }++ EXPECT_THAT(read_file(), ElementsAreArray(expected));+}++TEST_F(ReadWriteMappingTest, read_write_mappings_report_no_mapping_id) {+ auto rw_whole = load_mmap_object(m_file_path, 0, auto_size, false, MmapMode::READ_WRITE);+ auto rw_part = load_mmap_object(m_file_path, 128, 256, false, MmapMode::READ_WRITE);++ ASSERT_NE(rw_whole, nullptr);+ ASSERT_NE(rw_part, nullptr);++ EXPECT_EQ(rw_whole->get_id(), no_mapping_id);+ EXPECT_EQ(rw_part->get_id(), no_mapping_id);+}+class HintEvictTest : public ::testing::Test {protected:std::filesystem::path m_file_path;diff --git a/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hpp b/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hppindex 82dbc0f5c97..8fbae900cff 100644--- a/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hpp+++ b/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hpp@@ -10,7 +10,6 @@*/#pragma once-#include <memory>#include <string>#include "openvino/runtime/core.hpp"@@ -43,6 +42,7 @@ using gpu_handle_param = void*;using SharedBufferHandle = ov::intel_gpu::SharedBufferHandle;using VirtualAddressMemory = ov::intel_gpu::VirtualAddressMemory;+using FileDescriptor = ov::intel_gpu::FileDescriptor;/*** @brief This class represents an abstraction for GPU plugin remote tensor@@ -334,7 +334,7 @@ public:* @brief This function is used to obtain a remote tensor object from a user-supplied VirtualAddressMemory* @param type Tensor element type* @param shape Tensor shape- * @param buff A VirtualAddressMemory object that contains cpu pointer and size(optional)+ * @param buff A VirtualAddressMemory object that contains cpu pointer and size(optional) and access mode* @return A remote tensor instance*/ClBufferTensor create_tensor(const element::Type type, const Shape& shape, VirtualAddressMemory buff) {@@ -342,7 +342,27 @@ public:AnyMap params = {{ov::intel_gpu::shared_mem_type.name(), ov::intel_gpu::SharedMemType::CPU_VA},{ov::intel_gpu::cpu_va.name(), static_cast<gpu_handle_param>(buff.ptr)},- {ov::intel_gpu::cpu_va_size.name(), buff.size}}; // if -1 then use shape to get the size+ {ov::intel_gpu::cpu_va_size.name(), buff.size}, // if -1 then use shape to get the size+ {ov::intel_gpu::cpu_va_access.name(), buff.access}};+ return create_tensor(type, shape, params).as<ClBufferTensor>();+ }++ /**+ * @brief This function is used to obtain a remote tensor object from a file.+ * The plugin memory-maps the file and keeps the mapping alive for the whole tensor lifetime,+ * so the file must not be modified until the returned tensor is destroyed.+ * @param type Tensor element type+ * @param shape Tensor shape+ * @param file_descriptor Descriptor with the path, offset and access mode of the file containing tensor data.+ * The offset must be a multiple of the system memory mapping alignment: the page size on Linux+ * (typically 4 KiB) and the allocation granularity on Windows (typically 64 KiB).+ * AccessMode::READ_WRITE additionally requires the file to be writable by the calling process+ * and makes the tensor writes visible in the file.+ * @return A remote tensor instance+ */+ ClBufferTensor create_tensor(const element::Type type, const Shape& shape, const FileDescriptor& file_descriptor) {+ AnyMap params = {{ov::intel_gpu::shared_mem_type.name(), ov::intel_gpu::SharedMemType::MMAPED_FILE},+ {ov::intel_gpu::file_descriptor.name(), file_descriptor}};return create_tensor(type, shape, params).as<ClBufferTensor>();}diff --git a/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hpp b/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hppindex c6cd21d1140..e488a35f4d8 100644--- a/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hpp+++ b/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hpp@@ -10,6 +10,8 @@*/#pragma once+#include <filesystem>+#include "openvino/runtime/properties.hpp"namespace ov {@@ -119,6 +121,7 @@ enum class SharedMemType {BUFFER_FROM_HANDLE = 7, //!< OS-level external memory handle (e.g. DX12 NT handle on Windows,//!< DMA-BUF fd on Linux) imported by the plugin into a cl_memCPU_VA = 8, //!< Shared mmap-backed/aligned allocated host pointer mapped by plugin+ MMAPED_FILE = 9, //!< Memory-mapped file buffer read and wrapped by the plugin};/**@@ -151,6 +154,8 @@ inline std::ostream& operator<<(std::ostream& os, const SharedMemType& share_memreturn os << "DX_BUFFER";case SharedMemType::BUFFER_FROM_HANDLE:return os << "BUFFER_FROM_HANDLE";+ case SharedMemType::MMAPED_FILE:+ return os << "MMAPED_FILE";default:OPENVINO_THROW("Unsupported memory type");}@@ -177,6 +182,8 @@ inline std::istream& operator>>(std::istream& is, SharedMemType& share_mem_type)share_mem_type = SharedMemType::DX_BUFFER;} else if (str == "BUFFER_FROM_HANDLE") {share_mem_type = SharedMemType::BUFFER_FROM_HANDLE;+ } else if (str == "MMAPED_FILE") {+ share_mem_type = SharedMemType::MMAPED_FILE;} else {OPENVINO_THROW("Unsupported memory type: ", str);}@@ -204,6 +211,42 @@ static constexpr Property<gpu_handle_param> mem_handle{"MEM_HANDLE"};*/static constexpr Property<os_handle_param> os_handle{"OS_HANDLE"};+/**+ * @brief Enum to define access mode of memory shared with the plugin (host virtual-address buffers,+ * memory-mapped files, etc.)+ * @ingroup ov_runtime_ocl_gpu_cpp_api+ */+enum class AccessMode {+ READ = 0, //!< Tensor data is only read+ READ_WRITE = 1 //!< Tensor data is also written back; requires writable memory (or a writable file)+};++/** @cond INTERNAL */+inline std::ostream& operator<<(std::ostream& os, const AccessMode& access_mode) {+ switch (access_mode) {+ case AccessMode::READ:+ return os << "READ";+ case AccessMode::READ_WRITE:+ return os << "READ_WRITE";+ default:+ OPENVINO_THROW("Unsupported access mode");+ }+}++inline std::istream& operator>>(std::istream& is, AccessMode& access_mode) {+ std::string str;+ is >> str;+ if (str == "READ") {+ access_mode = AccessMode::READ;+ } else if (str == "READ_WRITE") {+ access_mode = AccessMode::READ_WRITE;+ } else {+ OPENVINO_THROW("Unsupported access mode: ", str);+ }+ return is;+}+/** @endcond */+/*** @brief This key identifies cpu pointer* @ingroup ov_runtime_ocl_gpu_cpp_api@@ -216,6 +259,12 @@ static constexpr Property<void*> cpu_va{"CPU_VA"};*/static constexpr Property<int64_t> cpu_va_size{"CPU_VA_SIZE"};+/**+ * @brief This key identifies access mode (read or read-write) of the memory pointed to by cpu_va+ * @ingroup ov_runtime_ocl_gpu_cpp_api+ */+static constexpr Property<AccessMode> cpu_va_access{"CPU_VA_ACCESS"};+/*** @brief This key identifies video decoder surface handle* in a shared memory blob parameter map@@ -255,10 +304,42 @@ struct SharedBufferHandle {* @ingroup ov_runtime_ocl_gpu_cpp_api*/struct VirtualAddressMemory {- explicit VirtualAddressMemory(void* ptr_, int64_t size_ = -1) : ptr(ptr_), size(size_) {}+ explicit VirtualAddressMemory(void* ptr_, int64_t size_ = -1, AccessMode access_ = AccessMode::READ_WRITE)+ : ptr(ptr_),+ size(size_),+ access(access_) {}void* ptr = nullptr;- int64_t size = -1; ///< Buffer size in bytes; -1 means "derive from tensor shape"+ int64_t size = -1; ///< Buffer size in bytes; -1 means "derive from tensor shape"+ AccessMode access = AccessMode::READ_WRITE; ///< Whether the plugin may only read this memory or also write to it};++/**+ * @brief File descriptor for wrapping tensor data memory-mapped from a file as a GPU plugin tensor.+ * The plugin memory-maps the file and keeps the mapping alive for the whole tensor lifetime,+ * so the file must not be modified until the returned tensor is destroyed.+ * @ingroup ov_runtime_ocl_gpu_cpp_api+ */+struct FileDescriptor { // need to be merged with ov::intel_npu::FileDescriptor in future+ explicit FileDescriptor(const std::filesystem::path& file_path,+ std::size_t offset_in_bytes = 0,+ AccessMode file_access = AccessMode::READ)+ : path(file_path),+ offset(offset_in_bytes),+ access(file_access) {+ OPENVINO_ASSERT(!file_path.empty(), "[GPU] Provided file path is empty.");+ }++ std::filesystem::path path; ///< File path+ std::size_t offset = 0; ///< Offset in bytes to read from the file+ AccessMode access = AccessMode::READ; ///< Access mode of the mapping+};++/**+ * @brief This key identifies the file descriptor+ * in a memory-mapped tensor parameter map.+ * @ingroup ov_runtime_ocl_gpu_cpp_api+ */+static constexpr Property<FileDescriptor> file_descriptor{"FILE_DESCRIPTOR"};} // namespace intel_gpu} // namespace ovdiff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hppindex 449db2e850b..dbe60b84ac3 100644--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hpp@@ -18,6 +18,7 @@#include "intel_gpu/plugin/common_utils.hpp"#include <string>+#include <filesystem>#include <map>#include <memory>@@ -85,6 +86,7 @@ private:std::shared_ptr<ov::IRemoteTensor> reuse_memory(const ov::element::Type type, const ov::Shape& shape, cldnn::shared_handle mem, TensorType tensor_type);std::shared_ptr<ov::IRemoteTensor> reuse_memory_from_cpu_va(const ov::element::Type type, const ov::Shape& shape, VirtualAddressMemory cpu_va, TensorType tensor_type);std::shared_ptr<ov::IRemoteTensor> reuse_memory_from_handle(const ov::element::Type type, const ov::Shape& shape, SharedBufferHandle handle, TensorType tensor_type);+ std::shared_ptr<ov::IRemoteTensor> reuse_memory_from_file(const ov::element::Type type, const ov::Shape& shape, const std::filesystem::path& file_path, size_t offset, ov::intel_gpu::AccessMode access);std::shared_ptr<ov::IRemoteTensor> create_buffer(const ov::element::Type type, const ov::Shape& shape);std::shared_ptr<ov::IRemoteTensor> create_usm(const ov::element::Type type, const ov::Shape& shape, TensorType alloc_type);void check_if_shared() const;diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hppindex 0b1378dc87f..364163d9731 100644--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hpp@@ -19,6 +19,8 @@#endif#include "openvino/runtime/iremote_tensor.hpp"#include "openvino/runtime/intel_gpu/remote_properties.hpp"+#include "openvino/runtime/tensor.hpp"+#include "openvino/util/mmap_object.hpp"#include "intel_gpu/runtime/memory_caps.hpp"#include "intel_gpu/runtime/memory.hpp"@@ -43,7 +45,8 @@ public:cldnn::shared_surface surf = 0,uint32_t plane = 0,ov::intel_gpu::SharedBufferHandle shared_buffer_handle = {},- ov::intel_gpu::VirtualAddressMemory va_mem = ov::intel_gpu::VirtualAddressMemory(nullptr));+ ov::intel_gpu::VirtualAddressMemory va_mem = ov::intel_gpu::VirtualAddressMemory(nullptr),+ std::shared_ptr<ov::MappedMemory> mapped_memory = nullptr);~RemoteTensorImpl() override;const AnyMap& get_properties() const override;@@ -88,6 +91,7 @@ private:uint32_t m_plane;ov::intel_gpu::SharedBufferHandle m_shared_buffer_handle;ov::intel_gpu::VirtualAddressMemory m_va_mem;+ std::shared_ptr<ov::MappedMemory> m_mapped_memory; // keeps the file mapping alive for the whole tensor lifetimesize_t m_hash = 0;bool supports_caching() const;diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hppindex 103ce57df87..9dad5551ff4 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp@@ -70,7 +70,9 @@ public:/// Created memory object by wrapping a read-only host-allocated layout region./// Backends that support access flags should use read-only permissions.- virtual memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) = 0;+ /// @param host_read_only When true, additionally hints that the host side will never write this+ /// memory either (e.g. it originates from a read-only file mapping)+ virtual memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout, bool host_read_only = false) = 0;/// Created memory object from the other @p memory and reinterpred the data using specified @p new_layoutvirtual memory_ptr reinterpret_buffer(const memory& memory, const layout& new_layout) = 0;diff --git a/src/plugins/intel_gpu/src/plugin/remote_context.cpp b/src/plugins/intel_gpu/src/plugin/remote_context.cppindex d09bc5bccc1..639dbab0ae6 100644--- a/src/plugins/intel_gpu/src/plugin/remote_context.cpp+++ b/src/plugins/intel_gpu/src/plugin/remote_context.cpp@@ -4,11 +4,15 @@#include "openvino/runtime/intel_gpu/remote_properties.hpp"#include "openvino/runtime/make_tensor.hpp"+#include "openvino/runtime/tensor.hpp"+#include "openvino/core/memory_util.hpp"+#include "openvino/util/mmap_object.hpp"#include "intel_gpu/plugin/remote_context.hpp"#include "intel_gpu/plugin/remote_tensor.hpp"#include "intel_gpu/plugin/usm_host_tensor.hpp"#include "intel_gpu/runtime/itt.hpp"#include "intel_gpu/runtime/device_query.hpp"+#include "intel_gpu/runtime/utils.hpp"#include <memory>namespace ov::intel_gpu {@@ -23,6 +27,17 @@ Type extract_object(const ov::AnyMap& params, const ov::Property<Type>& p) {return res.as<Type>();}++ov::MmapMode to_util_mmap_mode(ov::intel_gpu::AccessMode access) {+ switch (access) {+ case ov::intel_gpu::AccessMode::READ:+ return ov::MmapMode::READ;+ case ov::intel_gpu::AccessMode::READ_WRITE:+ return ov::MmapMode::READ_WRITE;+ }+ OPENVINO_THROW("[GPU] Unsupported file access mode");+}+ContextType get_default_context_type() {#ifdef OV_GPU_WITH_ZE_RTreturn ContextType::ZE;@@ -173,6 +188,7 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:if (ov::intel_gpu::SharedMemType::USM_DEVICE_BUFFER == mem_type) {return { create_usm(type, shape, TensorType::BT_USM_DEVICE_INTERNAL), nullptr };}+TensorType tensor_type;cldnn::shared_handle mem = nullptr;@@ -186,26 +202,34 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:tensor_type = TensorType::BT_CPU_VA;mem = extract_object(params, ov::intel_gpu::cpu_va);auto size = extract_object(params, ov::intel_gpu::cpu_va_size);- return {reuse_memory_from_cpu_va(type, shape, VirtualAddressMemory{mem, size}, tensor_type), nullptr};+ // cpu_va_access is optional for backward compatibility with callers that build params manually+ auto access = ov::intel_gpu::AccessMode::READ_WRITE;+ if (auto it = params.find(ov::intel_gpu::cpu_va_access.name()); it != params.end()) {+ access = it->second.as<ov::intel_gpu::AccessMode>();+ }+ return { reuse_memory_from_cpu_va(type, shape, VirtualAddressMemory{mem, size, access}, tensor_type), nullptr };+ } else if (ov::intel_gpu::SharedMemType::MMAPED_FILE == mem_type) {+ const auto fd = extract_object(params, ov::intel_gpu::file_descriptor);+ return { reuse_memory_from_file(type, shape, fd.path, fd.offset, fd.access), nullptr };} else if (ov::intel_gpu::SharedMemType::OCL_IMAGE2D == mem_type) {tensor_type = TensorType::BT_IMG_SHARED;mem = extract_object(params, ov::intel_gpu::mem_handle);#ifdef _WIN32- } else if (ov::intel_gpu::SharedMemType::DX_BUFFER == mem_type) {- tensor_type = TensorType::BT_DX_BUF_SHARED;- mem = extract_object(params, ov::intel_gpu::dev_object_handle);- check_if_shared();+ } else if (ov::intel_gpu::SharedMemType::DX_BUFFER == mem_type) {+ tensor_type = TensorType::BT_DX_BUF_SHARED;+ mem = extract_object(params, ov::intel_gpu::dev_object_handle);+ check_if_shared();#endif- } else if (ov::intel_gpu::SharedMemType::BUFFER_FROM_HANDLE == mem_type) {- tensor_type = TensorType::BT_BUF_SHARED_FROM_HANDLE;- const auto os_handle = extract_object(params, ov::intel_gpu::os_handle);- SharedBufferHandle handle{os_handle};- return { reuse_memory_from_handle(type, shape, handle, tensor_type), nullptr };- } else {- OPENVINO_THROW("[GPU] Unsupported shared object type ", mem_type);- }+ } else if (ov::intel_gpu::SharedMemType::BUFFER_FROM_HANDLE == mem_type) {+ tensor_type = TensorType::BT_BUF_SHARED_FROM_HANDLE;+ const auto os_handle = extract_object(params, ov::intel_gpu::os_handle);+ SharedBufferHandle handle{os_handle};+ return { reuse_memory_from_handle(type, shape, handle, tensor_type), nullptr };+ } else {+ OPENVINO_THROW("[GPU] Unsupported shared object type ", mem_type);+ }- return { reuse_memory(type, shape, mem, tensor_type), nullptr };+ return { reuse_memory(type, shape, mem, tensor_type), nullptr };}// For external contexts we try to match underlying handles with default contexts created by plugin to find device name@@ -271,6 +295,43 @@ std::shared_ptr<ov::IRemoteTensor> RemoteContextImpl::reuse_memory_from_handle(creturn std::make_shared<RemoteTensorImpl>(get_this_shared_ptr(), shape, type, tensor_type, nullptr, 0, 0, handle);}+std::shared_ptr<ov::IRemoteTensor> RemoteContextImpl::reuse_memory_from_file(const ov::element::Type type,+ const ov::Shape& shape,+ const std::filesystem::path& file_path,+ size_t offset,+ ov::intel_gpu::AccessMode access) {+ const auto byte_size = ov::util::get_memory_size_safe(type, shape);+ OPENVINO_ASSERT(byte_size, "[GPU] Cannot calculate memory size for element type ", type, " and shape ", shape);++ const auto alignment = ov::util::get_system_alloc_granularity();+ OPENVINO_ASSERT(alignment != 0 && offset % alignment == 0,+ "[GPU] Offset ",+ offset,+ " must be a multiple of ",+ alignment);+ // Memory-map the file. The mapping is retained inside the RemoteTensorImpl so it stays+ // alive for the whole tensor lifetime (GPU wraps the host pointer via CL_MEM_USE_HOST_PTR).+ auto mapped_memory =+ ov::load_mmap_object(file_path, offset, *byte_size, /*no_placeholder=*/false, to_util_mmap_mode(access));++ auto import_size = *byte_size;+ const auto cacheline_size = static_cast<size_t>(get_engine().get_device_info().cacheline_size.value_or(0));+ if (cacheline_size > 0 && alignment % cacheline_size == 0) {+ import_size = cldnn::align_to(import_size, cacheline_size);+ }++ return std::make_shared<RemoteTensorImpl>(get_this_shared_ptr(),+ shape,+ type,+ TensorType::BT_CPU_VA,+ nullptr,+ 0,+ 0,+ ov::intel_gpu::SharedBufferHandle{},+ VirtualAddressMemory{mapped_memory->data(), static_cast<int64_t>(import_size), access},+ mapped_memory);+}+std::shared_ptr<ov::IRemoteTensor> RemoteContextImpl::create_buffer(const ov::element::Type type, const ov::Shape& shape) {return std::make_shared<RemoteTensorImpl>(get_this_shared_ptr(), shape, type, TensorType::BT_BUF_INTERNAL);}diff --git a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp b/src/plugins/intel_gpu/src/plugin/remote_tensor.cppindex c5492e1fde1..99a8d33ea8b 100644--- a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp+++ b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp@@ -23,10 +23,12 @@ struct std::hash<ov::intel_gpu::SharedBufferHandle> {template <>struct std::hash<ov::intel_gpu::VirtualAddressMemory> {size_t operator()(const ov::intel_gpu::VirtualAddressMemory& mem) const noexcept {- // Hash both pointer and size to distinguish different allocations- size_t h1 = std::hash<const void*>{}(mem.ptr);- size_t h2 = std::hash<int64_t>{}(mem.size);- return h1 ^ (h2 << 1);+ // Hash pointer, size and access mode to distinguish different allocations/imports+ size_t seed = 0;+ seed = cldnn::hash_combine(seed, mem.ptr);+ seed = cldnn::hash_combine(seed, mem.size);+ seed = cldnn::hash_combine(seed, mem.access);+ return seed;}};@@ -172,7 +174,8 @@ RemoteTensorImpl::RemoteTensorImpl(RemoteContextImpl::Ptr context,cldnn::shared_surface surf,uint32_t plane,ov::intel_gpu::SharedBufferHandle shared_buffer_handle,- ov::intel_gpu::VirtualAddressMemory va_mem)+ ov::intel_gpu::VirtualAddressMemory va_mem,+ std::shared_ptr<ov::MappedMemory> mapped_memory): m_context(context), m_element_type(element_type), m_shape(shape)@@ -182,7 +185,8 @@ RemoteTensorImpl::RemoteTensorImpl(RemoteContextImpl::Ptr context,, m_surf(surf), m_plane(plane), m_shared_buffer_handle(shared_buffer_handle)- , m_va_mem(va_mem) {+ , m_va_mem(va_mem)+ , m_mapped_memory(std::move(mapped_memory)) {update_hash();allocate();}@@ -407,10 +411,20 @@ void RemoteTensorImpl::allocate() {break;}case TensorType::BT_CPU_VA: {- m_memory_object = engine.create_hostbuffer(m_va_mem.ptr,- m_va_mem.size > -1 ? m_va_mem.size : m_layout.bytes_count(),- cldnn::allocation_type::cl_mem,- m_layout);+ const auto buffer_size = m_va_mem.size > -1 ? m_va_mem.size : m_layout.bytes_count();+ if (m_va_mem.access == ov::intel_gpu::AccessMode::READ) {+ // host_read_only is set for the file-mmap case, since the host side will never write it either.+ m_memory_object = engine.create_hostbuffer(static_cast<const void*>(m_va_mem.ptr),+ buffer_size,+ cldnn::allocation_type::cl_mem,+ m_layout,+ /*host_read_only=*/m_mapped_memory != nullptr);+ } else {+ m_memory_object = engine.create_hostbuffer(m_va_mem.ptr,+ buffer_size,+ cldnn::allocation_type::cl_mem,+ m_layout);+ }break;}#ifdef _WIN32@@ -459,9 +473,9 @@ bool RemoteTensorImpl::is_shared() const noexcept {bool RemoteTensorImpl::supports_caching() const {#ifdef _WIN32- return is_shared();+ return is_shared() && !m_mapped_memory;#else- return is_shared() && m_mem_type != TensorType::BT_SURF_SHARED;+ return is_shared() && !m_mapped_memory && m_mem_type != TensorType::BT_SURF_SHARED;#endif}diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cppindex 5c50a998bcb..e8f50c05d86 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp@@ -247,8 +247,13 @@ memory_ptr ocl_engine::create_hostbuffer(void* cpu_address,return create_hostbuffer_impl(cpu_address, data_size, _allocation_type, output_layout, CL_MEM_READ_WRITE);}-memory_ptr ocl_engine::create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) {- return create_hostbuffer_impl(const_cast<void*>(cpu_address), data_size, _allocation_type, output_layout, CL_MEM_READ_ONLY);+memory_ptr ocl_engine::create_hostbuffer(const void* cpu_address,+ size_t data_size,+ allocation_type _allocation_type,+ const layout output_layout,+ bool host_read_only) {+ cl_mem_flags flags = host_read_only ? (CL_MEM_READ_ONLY | CL_MEM_HOST_READ_ONLY) : CL_MEM_READ_ONLY;+ return create_hostbuffer_impl(const_cast<void*>(cpu_address), data_size, _allocation_type, output_layout, flags);}memory::ptr ocl_engine::reinterpret_buffer(const memory& memory, const layout& new_layout) {diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hppindex 2d39a5013a5..0d25e4c4003 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp@@ -27,7 +27,7 @@ public:memory_ptr reinterpret_handle(const layout& new_layout, shared_mem_params params) override;memory_ptr create_subbuffer(const memory& memory, const layout& new_layout, size_t offset) override;memory_ptr create_hostbuffer(void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;- memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;+ memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout, bool host_read_only = false) override;memory_ptr reinterpret_buffer(const memory& memory, const layout& new_layout) override;memory_ptr import_buffer(const layout&, ov::intel_gpu::os_handle_param external_handle) override;bool is_the_same_buffer(const memory& mem1, const memory& mem2) override;diff --git a/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.cpp b/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.cppindex d9ef60fe797..9fafc6a35c6 100644--- a/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.cpp+++ b/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.cpp@@ -155,7 +155,7 @@ memory_ptr sycl_engine::create_hostbuffer(void* cpu_address, size_t data_size, aOPENVINO_NOT_IMPLEMENTED;}-memory_ptr sycl_engine::create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) {+memory_ptr sycl_engine::create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout, bool host_read_only) {OPENVINO_NOT_IMPLEMENTED;}diff --git a/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.hpp b/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.hppindex a440d4a7c6a..89a1489e38a 100644--- a/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.hpp+++ b/src/plugins/intel_gpu/src/runtime/sycl/sycl_engine.hpp@@ -31,7 +31,7 @@ public:memory_ptr reinterpret_handle(const layout& new_layout, shared_mem_params params) override;memory_ptr create_subbuffer(const memory& memory, const layout& new_layout, size_t offset) override;memory_ptr create_hostbuffer(void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;- memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;+ memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout, bool host_read_only = false) override;memory_ptr reinterpret_buffer(const memory& memory, const layout& new_layout) override;memory_ptr import_buffer(const layout& layout, ov::intel_gpu::os_handle_param external_handle) override;bool is_the_same_buffer(const memory& mem1, const memory& mem2) override;diff --git a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cpp b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cppindex 3047d902982..5dd09dd8836 100644--- a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cpp+++ b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cpp@@ -175,7 +175,7 @@ memory_ptr ze_engine::create_hostbuffer(void* cpu_address, size_t data_size, allOPENVINO_NOT_IMPLEMENTED;}-memory_ptr ze_engine::create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) {+memory_ptr ze_engine::create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout, bool host_read_only) {OPENVINO_NOT_IMPLEMENTED;}diff --git a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hpp b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hppindex 51c2067b10c..28744cdce67 100644--- a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hpp+++ b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hpp@@ -28,7 +28,7 @@ public:memory_ptr reinterpret_handle(const layout& new_layout, shared_mem_params params) override;memory_ptr create_subbuffer(const memory& memory, const layout& new_layout, size_t byte_offset) override;memory_ptr create_hostbuffer(void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;- memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;+ memory_ptr create_hostbuffer(const void* cpu_address, size_t data_size, allocation_type _allocation_type, const layout output_layout, bool host_read_only = false) override;memory_ptr reinterpret_buffer(const memory& memory, const layout& new_layout) override;bool is_the_same_buffer(const memory& mem1, const memory& mem2) override;diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cppindex ff3f6f7e3d0..e6b554a82de 100644--- a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp@@ -5,6 +5,8 @@#ifdef OV_GPU_WITH_OCL_RT#include <algorithm>+#include <filesystem>+#include <fstream>#include "openvino/core/preprocess/pre_post_process.hpp"#include "openvino/op/add.hpp"@@ -15,6 +17,7 @@#include "openvino/runtime/intel_gpu/properties.hpp"#include "openvino/runtime/remote_tensor.hpp"#include "openvino/util/memory.hpp"+#include "openvino/util/mmap_object.hpp"#include "remote_tensor_tests/helpers.hpp"#include "common_test_utils/ov_tensor_utils.hpp"@@ -3081,9 +3084,10 @@ TEST(GpuRemoteTensorFromCpu, smoke_allocAlignedCPUMemory) {std::fill_n(static_cast<float*>(output_ptr), element_count, 0.0f);{- auto remote_input_tensor = ctx.create_tensor(ov::element::f32,- shape,- ov::intel_gpu::VirtualAddressMemory(input_ptr));+ auto remote_input_tensor = ctx.create_tensor(+ ov::element::f32,+ shape,+ ov::intel_gpu::VirtualAddressMemory(input_ptr, -1, ov::intel_gpu::AccessMode::READ));auto remote_output_tensor = ctx.create_tensor(ov::element::f32,shape,ov::intel_gpu::VirtualAddressMemory(output_ptr));@@ -3104,4 +3108,159 @@ TEST(GpuRemoteTensorFromCpu, smoke_allocAlignedCPUMemory) {ov::util::aligned_free(output_ptr);}++using MmapFileMemoryParams = std::tuple<std::size_t, std::size_t>;++class GpuRemoteTensorFromFile : public ::testing::TestWithParam<MmapFileMemoryParams> {+public:+ static std::string getTestCaseName(const testing::TestParamInfo<MmapFileMemoryParams>& obj) {+ const auto& [offset, bytes_after_offset] = obj.param;+ return "offset_" + std::to_string(offset) + "_bytes_after_offset_" + std::to_string(bytes_after_offset);+ }++protected:+ std::filesystem::path m_file_path;++ void SetUp() override {+ m_file_path = ov::test::utils::generateTestFilePrefix() + ".bin";+ }++ void TearDown() override {+ std::error_code ec;+ std::filesystem::remove(m_file_path, ec);+ }++ static void write_data_at_offset(const std::filesystem::path& path,+ std::size_t offset,+ const std::vector<float>& values) {+ std::ofstream file(path, std::ios::binary);+ if (offset > 0) {+ const std::vector<char> padding(offset, 0);+ file.write(padding.data(), padding.size());+ }+ file.write(reinterpret_cast<const char*>(values.data()), values.size() * sizeof(float));+ }++ static std::vector<float> make_values(std::size_t element_count) {+ std::vector<float> values(element_count);+ for (std::size_t i = 0; i < element_count; ++i) {+ values[i] = static_cast<float>(i + 1);+ }+ return values;+ }+};++TEST_P(GpuRemoteTensorFromFile, smoke_mmapFileMemoryAsInput) {+ const auto& [offset, bytes_after_offset] = GetParam();++ ov::Core core;+ std::string target_device = ov::test::utils::DEVICE_GPU;+ const ov::Shape shape{bytes_after_offset / sizeof(float)};+ const size_t element_count = ov::shape_size(shape);+ auto ctx = core.get_default_context(target_device).as<ov::intel_gpu::ocl::ClContext>();++ const auto input_values = make_values(element_count);+ write_data_at_offset(m_file_path, offset, input_values);+ ASSERT_EQ(std::filesystem::file_size(m_file_path), offset + bytes_after_offset);++ const std::size_t cacheline = core.get_property(target_device, ov::intel_gpu::cacheline_size);+ const std::size_t output_buffer_size = ((bytes_after_offset + cacheline - 1) / cacheline) * cacheline;+ void* output_ptr = ov::util::aligned_alloc(output_buffer_size, cacheline);+ std::fill_n(static_cast<char*>(output_ptr), output_buffer_size, 0);++ {+ auto remote_input_tensor =+ ctx.create_tensor(ov::element::f32, shape, ov::intel_gpu::FileDescriptor{m_file_path, offset});+ ASSERT_TRUE(remote_input_tensor.is<ov::intel_gpu::ocl::ClBufferTensor>());+ auto remote_output_tensor =+ ctx.create_tensor(ov::element::f32,+ shape,+ ov::intel_gpu::VirtualAddressMemory(output_ptr, static_cast<int64_t>(output_buffer_size)));++ auto model = make_copy_model(shape);+ auto compiled = core.compile_model(model, ctx);+ auto infer_req = compiled.create_infer_request();+ infer_req.set_tensor(compiled.input(), remote_input_tensor);+ infer_req.set_tensor(compiled.output(), remote_output_tensor);+ infer_req.infer();++ for (size_t i = 0; i < element_count; ++i) {+ EXPECT_FLOAT_EQ(static_cast<float*>(output_ptr)[i], input_values[i]) << "Mismatch at index " << i;+ }+ }++ ov::util::aligned_free(output_ptr);+}++TEST_P(GpuRemoteTensorFromFile, smoke_mmapFileMemoryAsOutput) {+ const auto& [offset, bytes_after_offset] = GetParam();++ ov::Core core;+ std::string target_device = ov::test::utils::DEVICE_GPU;+ const ov::Shape shape{bytes_after_offset / sizeof(float)};+ const size_t element_count = ov::shape_size(shape);+ auto ctx = core.get_default_context(target_device).as<ov::intel_gpu::ocl::ClContext>();++ const auto input_values = make_values(element_count);+ write_data_at_offset(m_file_path, offset, std::vector<float>(element_count, 0.0f));+ ASSERT_EQ(std::filesystem::file_size(m_file_path), offset + bytes_after_offset);++ {+ auto remote_output_tensor = ctx.create_tensor(+ ov::element::f32,+ shape,+ ov::intel_gpu::FileDescriptor{m_file_path, offset, ov::intel_gpu::AccessMode::READ_WRITE});++ auto model = make_copy_model(shape);+ auto compiled = core.compile_model(model, ctx);+ auto infer_req = compiled.create_infer_request();+ infer_req.set_tensor(compiled.input(),+ ov::Tensor(ov::element::f32, shape, const_cast<float*>(input_values.data())));+ infer_req.set_tensor(compiled.output(), remote_output_tensor);+ infer_req.infer();+ }++ std::vector<float> file_content(element_count, 0.0f);+ std::ifstream file(m_file_path, std::ios::binary);+ file.seekg(offset);+ file.read(reinterpret_cast<char*>(file_content.data()), bytes_after_offset);+ for (size_t i = 0; i < element_count; ++i) {+ EXPECT_FLOAT_EQ(file_content[i], input_values[i]) << "Mismatch in file at index " << i;+ }+}++static std::vector<MmapFileMemoryParams> generate_mmap_file_memory_params() {+#ifdef _WIN32+ // Windows maps file views with 64K allocation granularity+ const std::size_t mmap_granularity = 65536;+#else+ // Page size varies per platform: 4K on x86-64, 16K or 64K on some ARM64 Linux distributions.+ const auto mmap_granularity = static_cast<std::size_t>(ov::util::get_system_page_size());+#endif+ const std::vector<std::pair<std::size_t, std::size_t>> layouts{+ // Sizes smaller than / not divisible by the device cacheline size, e.g. f32 with shape {1}.+ {0, sizeof(float)},+ {mmap_granularity, 3 * sizeof(float)},+ {0, 256},+ {0, 4 * mmap_granularity},+ {mmap_granularity, 256},+ {2 * mmap_granularity, 256},+ {16 * mmap_granularity, 256},+ {mmap_granularity, mmap_granularity},+ {3 * mmap_granularity, 4 * mmap_granularity},+ {8 * mmap_granularity, 16 * mmap_granularity}};++ std::vector<MmapFileMemoryParams> params;+ params.reserve(layouts.size());+ for (const auto& [offset, bytes_after_offset] : layouts) {+ params.emplace_back(offset, bytes_after_offset);+ }+ return params;+}++INSTANTIATE_TEST_SUITE_P(smoke_mmapFileMemory,+ GpuRemoteTensorFromFile,+ ::testing::ValuesIn(generate_mmap_file_memory_params()),+ GpuRemoteTensorFromFile::getTestCaseName);+#endif // OV_GPU_WITH_OCL_RT