Skip to content

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

Overloading to read_tensor_data to read from file handle (#33317)

Oleg Pipikine05784d1ac29
diff --git a/src/common/util/include/openvino/util/mmap_object.hpp b/src/common/util/include/openvino/util/mmap_object.hpp
index 9dae3bd725e..56a0608c6ef 100644
--- a/src/common/util/include/openvino/util/mmap_object.hpp
+++ b/src/common/util/include/openvino/util/mmap_object.hpp
@@ -50,9 +50,15 @@ std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::filesystem::path&
* @brief Returns mapped memory for a file from provided file handle (cross-platform).
* Uses mmap on Linux/Unix (with file descriptor) or MapViewOfFile on Windows (with HANDLE).
* This allows external control of file access through a callback function.
+ * Do not call load_mmap_object_from_handle directly, use the template wrapper instead.
*
* @param handle Platform-specific file handle (int fd on Linux, HANDLE on Windows).
* @return MappedMemory shared ptr object which keep mmaped memory and control the lifetime.
*/
-std::shared_ptr<ov::MappedMemory> load_mmap_object(FileHandle handle);
+std::shared_ptr<ov::MappedMemory> load_mmap_object_from_handle(FileHandle handle);
+
+template <typename T, std::enable_if_t<std::is_same<T, FileHandle>::value, int> = 0>
+std::shared_ptr<ov::MappedMemory> load_mmap_object(T handle) {
+ return load_mmap_object_from_handle(static_cast<FileHandle>(handle));
+}
} // namespace ov
diff --git a/src/common/util/src/os/lin/lin_mmap_object.cpp b/src/common/util/src/os/lin/lin_mmap_object.cpp
index 791fdf326eb..252cdcbe3dd 100644
--- a/src/common/util/src/os/lin/lin_mmap_object.cpp
+++ b/src/common/util/src/os/lin/lin_mmap_object.cpp
@@ -113,7 +113,7 @@ std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::filesystem::path&
return holder;
}
-std::shared_ptr<ov::MappedMemory> load_mmap_object(FileHandle handle) {
+std::shared_ptr<ov::MappedMemory> load_mmap_object_from_handle(FileHandle handle) {
// On Linux, FileHandle is int (file descriptor)
auto holder = std::make_shared<MapHolder>();
holder->set_from_fd(static_cast<int>(handle));
diff --git a/src/common/util/src/os/win/win_mmap_object.cpp b/src/common/util/src/os/win/win_mmap_object.cpp
index e2e37f106c2..b887e711e02 100644
--- a/src/common/util/src/os/win/win_mmap_object.cpp
+++ b/src/common/util/src/os/win/win_mmap_object.cpp
@@ -129,7 +129,7 @@ std::shared_ptr<ov::MappedMemory> load_mmap_object(const std::filesystem::path&
return holder;
}
-std::shared_ptr<ov::MappedMemory> load_mmap_object(FileHandle handle) {
+std::shared_ptr<ov::MappedMemory> load_mmap_object_from_handle(FileHandle handle) {
// On Windows, FileHandle is void* (HANDLE)
HANDLE h = static_cast<HANDLE>(handle);
if (h == INVALID_HANDLE_VALUE || h == nullptr) {
diff --git a/src/core/include/openvino/runtime/file_handle.hpp b/src/core/include/openvino/runtime/file_handle.hpp
new file mode 100644
index 00000000000..4799211e5a7
--- /dev/null
+++ b/src/core/include/openvino/runtime/file_handle.hpp
@@ -0,0 +1,29 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <functional>
+
+namespace ov {
+#ifdef _WIN32
+// Windows uses HANDLE (void*) for file handles
+using FileHandle = void*;
+#else
+// Linux/Unix uses int for file descriptors
+using FileHandle = int;
+#endif
+
+/**
+ * @brief Type definition for file handle provider callback (cross-platform).
+ * Function that takes no arguments and returns a platform-specific file handle.
+ * The callback implementation must release ownership, caller should close the FileHandle.
+ * On Linux/Unix: returns int (file descriptor)
+ * On Windows: returns void* (HANDLE cast to void*)
+ * This is useful for scenarios where file access needs to be controlled externally,
+ * such as Android content providers or Windows restricted file access scenarios.
+ * @ingroup ov_runtime_cpp_api
+ */
+using FileHandleProvider = std::function<FileHandle()>;
+} // namespace ov
diff --git a/src/core/include/openvino/runtime/tensor.hpp b/src/core/include/openvino/runtime/tensor.hpp
index 245e95e71f0..bb481655e49 100644
--- a/src/core/include/openvino/runtime/tensor.hpp
+++ b/src/core/include/openvino/runtime/tensor.hpp
@@ -18,6 +18,7 @@
#include "openvino/core/shape.hpp"
#include "openvino/core/type/element_type.hpp"
#include "openvino/runtime/allocator.hpp"
+#include "openvino/runtime/file_handle.hpp"
namespace ov {
@@ -325,4 +326,18 @@ Tensor read_tensor_data(const std::filesystem::path& file_name,
const PartialShape& shape = PartialShape::dynamic(1),
std::size_t offset_in_bytes = 0,
bool mmap = true);
+
+/// \brief Read a tensor content from an externally provided file handle. Only raw data is loaded.
+/// \param file_handle File handle to read. File handle should be opened by the caller OpenVINO takes ownership of the
+/// file
+/// handle and is responsible for closing it. File should be opened with GENERIC_READ,
+/// FILE_SHARE_READ flags on Windows and O_RDONLY on Linux.
+/// \param element_type Element type, when not specified the it is assumed as element::u8.
+/// \param shape Shape for resulting tensor.
+/// \param offset_in_bytes Read file starting from specified offset.
+OPENVINO_API
+Tensor read_tensor_data(ov::FileHandle file_handle,
+ const element::Type& element_type = element::u8,
+ const PartialShape& shape = PartialShape::dynamic(1),
+ std::size_t offset_in_bytes = 0);
} // namespace ov
diff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cpp
index 2d5a1e10d81..d213f04c628 100644
--- a/src/core/src/runtime/tensor.cpp
+++ b/src/core/src/runtime/tensor.cpp
@@ -4,6 +4,7 @@
#include "openvino/runtime/tensor.hpp"
+#include <fstream>
#include <numeric>
#include "openvino/core/except.hpp"
@@ -134,11 +135,10 @@ bool Tensor::is_continuous() const {
}
namespace {
-ov::Shape calc_static_shape_for_file(const std::filesystem::path& file_name,
+ov::Shape calc_static_shape_for_file(size_t file_size,
const ov::element::Type& element_type,
const ov::PartialShape& partial_shape,
size_t offset) {
- auto file_size = std::filesystem::file_size(file_name);
if (partial_shape.is_static()) {
auto static_shape = partial_shape.get_shape();
OPENVINO_ASSERT((ov::shape_size(static_shape)) * element_type.bitwidth() + offset * 8 == file_size * 8,
@@ -181,19 +181,11 @@ ov::Shape calc_static_shape_for_file(const std::filesystem::path& file_name,
return partial_shape_copy.get_shape();
}
-void read_tensor_data(const std::filesystem::path& file_name, Tensor& tensor, size_t offset) {
+void read_tensor_via_ifstream(const std::filesystem::path& file_name, Tensor& tensor, size_t offset) {
OPENVINO_ASSERT(tensor.get_element_type() != ov::element::string);
std::ifstream fin(file_name, std::ios::binary);
fin.seekg(offset);
auto bytes_to_read = tensor.get_byte_size();
-
- OPENVINO_ASSERT(bytes_to_read <= static_cast<size_t>(std::numeric_limits<std::streamsize>::max()),
- "Cannot read ",
- bytes_to_read,
- " bytes from ",
- file_name,
- ", because the value exceeds std::streamsize limit");
-
fin.read(static_cast<char*>(tensor.data()), bytes_to_read);
OPENVINO_ASSERT(static_cast<size_t>(fin.gcount()) == bytes_to_read,
"Cannot read ",
@@ -212,6 +204,19 @@ ov::Tensor wrap_obj_to_viewtensor(const std::shared_ptr<void>& shared_obj,
view_tensor = make_tensor(impl);
return view_tensor;
}
+
+Tensor read_tensor_data_mmap_impl(std::shared_ptr<ov::MappedMemory> mapped_memory,
+ const ov::element::Type& element_type,
+ const ov::PartialShape& partial_shape,
+ size_t offset_in_bytes) {
+ auto static_shape = calc_static_shape_for_file(mapped_memory->size(), element_type, partial_shape, offset_in_bytes);
+ auto shared_buffer =
+ std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(mapped_memory->data() + offset_in_bytes,
+ mapped_memory->size() - offset_in_bytes,
+ mapped_memory);
+
+ return wrap_obj_to_viewtensor(shared_buffer, shared_buffer->get_ptr(), element_type, static_shape);
+}
} // namespace
Tensor read_tensor_data(const std::filesystem::path& file_name,
@@ -220,18 +225,24 @@ Tensor read_tensor_data(const std::filesystem::path& file_name,
size_t offset_in_bytes,
bool mmap) {
OPENVINO_ASSERT(element_type != ov::element::string);
- auto static_shape = calc_static_shape_for_file(file_name, element_type, partial_shape, offset_in_bytes);
if (mmap) {
auto mapped_memory = ov::load_mmap_object(file_name);
- auto shared_buffer = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(
- mapped_memory->data() + offset_in_bytes,
- mapped_memory->size() - offset_in_bytes,
- mapped_memory);
- return wrap_obj_to_viewtensor(shared_buffer, shared_buffer->get_ptr(), element_type, static_shape);
+ return read_tensor_data_mmap_impl(mapped_memory, element_type, partial_shape, offset_in_bytes);
} else {
+ auto file_size = std::filesystem::file_size(file_name);
+ auto static_shape = calc_static_shape_for_file(file_size, element_type, partial_shape, offset_in_bytes);
auto tensor = std::make_shared<ov::Tensor>(element_type, static_shape);
- read_tensor_data(file_name, *tensor.get(), offset_in_bytes);
+ read_tensor_via_ifstream(file_name, *tensor.get(), offset_in_bytes);
return wrap_obj_to_viewtensor(tensor, tensor->data(), element_type, static_shape);
}
}
+
+Tensor read_tensor_data(ov::FileHandle file_handle,
+ const ov::element::Type& element_type,
+ const ov::PartialShape& partial_shape,
+ size_t offset_in_bytes) {
+ OPENVINO_ASSERT(element_type != ov::element::string);
+ auto mapped_memory = ov::load_mmap_object(file_handle);
+ return read_tensor_data_mmap_impl(mapped_memory, element_type, partial_shape, offset_in_bytes);
+}
} // namespace ov
diff --git a/src/inference/include/openvino/runtime/common.hpp b/src/inference/include/openvino/runtime/common.hpp
index eb597f28b44..601f6eeb474 100644
--- a/src/inference/include/openvino/runtime/common.hpp
+++ b/src/inference/include/openvino/runtime/common.hpp
@@ -48,27 +48,6 @@ namespace ov {
* - `value` means device name supporting this operation
*/
using SupportedOpsMap = std::map<std::string, std::string>;
-
-#ifdef _WIN32
-// Windows uses HANDLE (void*) for file handles
-using FileHandle = void*;
-#else
-// Linux/Unix uses int for file descriptors
-using FileHandle = int;
-#endif
-
-/**
- * @brief Type definition for file handle provider callback (cross-platform).
- * Function that takes no arguments and returns a platform-specific file handle.
- * The callback implementation must release ownership, caller should close the FileHandle.
- * On Linux/Unix: returns int (file descriptor)
- * On Windows: returns void* (HANDLE cast to void*)
- * This is useful for scenarios where file access needs to be controlled externally,
- * such as Android content providers or Windows restricted file access scenarios.
- * @ingroup ov_runtime_cpp_api
- */
-using FileHandleProvider = std::function<FileHandle()>;
-
} // namespace ov
#if defined(_WIN32) && !defined(__GNUC__)
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
index 68f8fef9a6e..8eebcd4281b 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/npuw_private_properties.hpp
@@ -4,6 +4,7 @@
#pragma once
+#include "openvino/runtime/file_handle.hpp"
#include "openvino/runtime/intel_npu/properties.hpp"
namespace ov {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index 676b19e5c0a..9cc34bac0bb 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -17,7 +17,7 @@
#include <unordered_set>
#include <vector>
-#include "openvino/runtime/common.hpp"
+#include "openvino/runtime/file_handle.hpp"
namespace ov {
namespace npuw {