Skip to content

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

[NPUW] Map weights from the handle provider's seek position (#39345)

Anoob6817c71433fe
diff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hpp
index de715717d3f..d9777cb1eaa 100644
--- a/src/common/util/include/openvino/util/file_util.hpp
+++ b/src/common/util/include/openvino/util/file_util.hpp
@@ -9,6 +9,7 @@
#include <fstream>
#include <functional>
#include <initializer_list>
+#include <optional>
#include <string>
#include <type_traits>
#include <vector>
@@ -349,4 +350,18 @@ FileHandle open_file(const std::filesystem::path& path, FileMode mode = FileMode
*/
void close_file(FileHandle handle);
+/**
+ * @brief Read the current file position of an open file handle, without moving it.
+ *
+ * Maps to @c lseek(fd, 0, SEEK_CUR) on POSIX and @c SetFilePointerEx(h, 0, FILE_CURRENT) on Windows.
+ * Lets a handle carry "where the interesting data starts" to its consumer: the producer seeks the
+ * handle before handing it over (see @ref ov::FileHandleProvider) and the consumer maps/reads from
+ * that position on.
+ *
+ * @param handle An open file handle.
+ * @return Byte offset of the current position from the start of the file, or @c std::nullopt when it
+ * cannot be determined - an invalid handle, or a non-seekable object such as a pipe.
+ */
+std::optional<uint64_t> get_file_position(FileHandle handle);
+
} // namespace ov::util
diff --git a/src/common/util/src/os/lin/lin_file_util.cpp b/src/common/util/src/os/lin/lin_file_util.cpp
index e8a93271f47..3f146f2cd6b 100644
--- a/src/common/util/src/os/lin/lin_file_util.cpp
+++ b/src/common/util/src/os/lin/lin_file_util.cpp
@@ -40,4 +40,17 @@ void close_file(FileHandle handle) {
}
}
+std::optional<uint64_t> get_file_position(FileHandle handle) {
+ if (handle == invalid_handle) {
+ return std::nullopt;
+ }
+ // SEEK_CUR with a zero delta is the standard "tell": it reports the offset without moving it.
+ // Fails with ESPIPE on pipes/sockets/FIFOs, which have no position at all.
+ const auto pos = ::lseek(handle, 0, SEEK_CUR);
+ if (pos < 0) {
+ return std::nullopt;
+ }
+ return static_cast<uint64_t>(pos);
+}
+
} // namespace ov::util
diff --git a/src/common/util/src/os/win/win_file_util.cpp b/src/common/util/src/os/win/win_file_util.cpp
index 2f8aa6adbed..32f12797588 100644
--- a/src/common/util/src/os/win/win_file_util.cpp
+++ b/src/common/util/src/os/win/win_file_util.cpp
@@ -41,4 +41,18 @@ void close_file(FileHandle handle) {
}
}
+std::optional<uint64_t> get_file_position(FileHandle handle) {
+ if (handle == INVALID_HANDLE_VALUE || handle == nullptr) {
+ return std::nullopt;
+ }
+ // A zero move relative to FILE_CURRENT reports the position without changing it.
+ // Fails for non-seekable handles (pipes, consoles).
+ LARGE_INTEGER distance{};
+ LARGE_INTEGER position{};
+ if (!SetFilePointerEx(handle, distance, &position, FILE_CURRENT) || position.QuadPart < 0) {
+ return std::nullopt;
+ }
+ return static_cast<uint64_t>(position.QuadPart);
+}
+
} // namespace ov::util
diff --git a/src/core/include/openvino/runtime/file_handle.hpp b/src/core/include/openvino/runtime/file_handle.hpp
index 4799211e5a7..59a3ea23666 100644
--- a/src/core/include/openvino/runtime/file_handle.hpp
+++ b/src/core/include/openvino/runtime/file_handle.hpp
@@ -23,6 +23,12 @@ using FileHandle = int;
* 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.
+ *
+ * The data of interest does not have to start at byte 0 of the file: the provider may seek the
+ * handle it returns (lseek / SetFilePointerEx) to the first byte it wants the consumer to see.
+ * Consumers read that position back with ov::util::get_file_position() and treat it as the base
+ * of the region, so a file embedding the data at some offset needs no extra out-of-band offset.
+ * A handle left at position 0 - the usual case - means "the whole file".
* @ingroup ov_runtime_cpp_api
*/
using FileHandleProvider = std::function<FileHandle()>;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
index f8b2de93bb8..e15886b8251 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -171,8 +171,7 @@ ov::npuw::s11n::WeightsContext make_import_weights_ctx(const ov::AnyMap& propert
if (is_weightless) {
std::shared_ptr<ov::MappedMemory> mapped_memory;
if (handle_provider) {
- ov::FileHandle handle = handle_provider();
- mapped_memory = ov::load_mmap_object(handle);
+ mapped_memory = mmap_weights(handle_provider);
} else if (!weights_path.empty()) {
mapped_memory = ov::load_mmap_object(ov::util::make_path(weights_path));
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
index 71c352272f3..792596d3000 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
@@ -4,6 +4,7 @@
#include "lazy_tensor.hpp"
+#include <cstdint>
#include <limits>
#include <tuple>
#include <type_traits>
@@ -97,8 +98,7 @@ ov::Tensor Const::eval() const {
std::shared_ptr<ov::MappedMemory> mapped_memory;
// Use handle_provider if available, otherwise use default mmap
if (m_handle_provider) {
- ov::FileHandle handle = m_handle_provider();
- mapped_memory = ov::load_mmap_object(handle);
+ mapped_memory = ov::npuw::s11n::mmap_weights(m_handle_provider);
} else {
mapped_memory = ov::load_mmap_object(ov::util::make_path(m_weights_path));
}
@@ -191,7 +191,14 @@ static void drop_file_range(const std::string& path,
if (fd < 0) {
return;
}
- ::posix_fadvise(fd, static_cast<off_t>(offset), static_cast<off_t>(size), POSIX_FADV_DONTNEED);
+ // A provider may position the handle at the start of a weights region embedded in a larger
+ // file (see ov::FileHandleProvider); `offset` is relative to that position, posix_fadvise is
+ // not. Re-base it, and skip the hint rather than dropping the wrong range on overflow.
+ const auto base = provider ? ov::util::get_file_position(fd).value_or(0u) : 0u;
+ const auto max_off = static_cast<std::uint64_t>(std::numeric_limits<off_t>::max());
+ if (base <= max_off - offset - size) {
+ ::posix_fadvise(fd, static_cast<off_t>(base + offset), static_cast<off_t>(size), POSIX_FADV_DONTNEED);
+ }
::close(fd);
#else
static_cast<void>(path);
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
index 30779130de2..5af52589150 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
@@ -17,11 +17,24 @@
#include "openvino/op/util/op_types.hpp"
#include "openvino/reference/convert.hpp"
#include "openvino/runtime/shared_buffer.hpp"
+#include "openvino/util/file_util.hpp"
#include "openvino/util/mmap_object.hpp"
#include "pyramid_attention.hpp"
#include "spatial.hpp"
#include "util.hpp"
+std::shared_ptr<ov::MappedMemory> ov::npuw::s11n::mmap_weights(const ov::FileHandleProvider& handle_provider) {
+ NPUW_ASSERT(handle_provider && "No weights handle provider to map the weights from!");
+ const ov::FileHandle handle = handle_provider();
+ // Where the provider left the handle is where the weights begin (see the declaration).
+ // Unknown position (a non-seekable handle) is the same as "from the start".
+ const auto region_offset = ov::util::get_file_position(handle).value_or(0u);
+ LOG_DEBUG("Mapping weights from the provided file handle at offset " << region_offset << " up to EOF.");
+ // auto_size: to EOF. The region is only an upper bound on the weights - individual weights are
+ // range-checked against it in Const::validate_weight_range().
+ return ov::load_mmap_object(handle, static_cast<std::size_t>(region_offset));
+}
+
// NOTE: This constructor should only be used when exporting blobs.
ov::npuw::s11n::WeightsContext::WeightsContext(bool _is_weightless,
const std::unordered_map<const void*, std::size_t>& _const_to_offset)
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index 4d7d860fa74..c585ecc91f3 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -129,6 +129,18 @@ using BF16Cache = std::unordered_set<std::pair<std::size_t, std::size_t>, ov::np
using Weights = ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>;
using WeightsPtr = std::shared_ptr<Weights>;
+// Maps the weights a FileHandleProvider points at.
+//
+// The weights do not have to be a file of their own: they may be a region embedded in a larger
+// file, e.g. a runtime that keeps its weights pool inside the model file it already has open.
+// Such a provider seeks the handle it returns to the first byte of the pool, so the mapping runs
+// from that position to EOF and the pool start - not the file start - becomes the mapping base.
+// Weight offsets stored in the blob are relative to it either way. A handle at position 0 (the
+// usual case) maps the whole file, as before.
+//
+// Ownership of the handle follows the FileHandleProvider contract - it is consumed here.
+std::shared_ptr<ov::MappedMemory> mmap_weights(const ov::FileHandleProvider& handle_provider);
+
struct CompiledContext {
CompiledContext(bool _encrypted,
const std::function<std::string(const std::string&)>& _encrypt,