From a9f950a897514f9bc1fec5f9c442160cfe497a69 Mon Sep 17 00:00:00 2001 From: Mustafa Cavus Date: Thu, 3 Dec 2026 23:09:58 +0100 Subject: [PATCH] openvino: add GGML_OPENVINO_EVICT_ON_UPLOAD to drop host weights as the GPU plugin uploads them Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-openvino/CMakeLists.txt | 25 +++ .../dev_doc/OpenVINO-ENV-VARS.md | 1 + .../src/ggml-openvino/ggml-openvino-extra.cpp | 1 + ggml/src/ggml-openvino/ggml-openvino.cpp | 157 +++++++++++++++++- .../ggml-openvino/openvino/aligned_buffer.hpp | 147 ++++++++++++++++ 5 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 ggml/src/ggml-openvino/openvino/aligned_buffer.hpp diff --git a/ggml/src/ggml-openvino/CMakeLists.txt b/ggml/src/ggml-openvino/CMakeLists.txt index a33da0677815..c75921f4fb12 100644 --- a/ggml/src/ggml-openvino/CMakeLists.txt +++ b/ggml/src/ggml-openvino/CMakeLists.txt @@ -27,3 +27,28 @@ endif() if (WIN32) target_link_libraries(ggml-openvino PRIVATE psapi) endif() + +# GGML_OPENVINO_EVICT_ON_UPLOAD derives from ov::AlignedBuffer, declared in openvino/aligned_buffer.hpp for the +# OpenVINO versions it was checked against. Build it only if a class derived from that declaration links against the +# OpenVINO found here: a declaration that does not match the library names symbols the library does not have. +if (CMAKE_SYSTEM_NAME STREQUAL "Linux") + unset(GGML_OPENVINO_HAS_EVICT_ON_UPLOAD CACHE) + set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/..") + set(CMAKE_REQUIRED_LIBRARIES openvino::runtime) + check_cxx_source_compiles("#include +#if !((OPENVINO_VERSION_MAJOR == 2026 && OPENVINO_VERSION_MINOR == 5) || (OPENVINO_VERSION_MAJOR == 2027 && OPENVINO_VERSION_MINOR == 0)) +#error not a mirrored OpenVINO version +#endif +#include \"ggml-openvino/openvino/aligned_buffer.hpp\" +struct view : ov::AlignedBuffer { + std::shared_ptr get_descriptor() const override { return ov::create_base_descriptor(0, 0, nullptr); } + void hint_evict() noexcept override {} +}; +int main() { return std::make_shared()->get_descriptor() != nullptr; }" + GGML_OPENVINO_HAS_EVICT_ON_UPLOAD) + unset(CMAKE_REQUIRED_INCLUDES) + unset(CMAKE_REQUIRED_LIBRARIES) + if (GGML_OPENVINO_HAS_EVICT_ON_UPLOAD) + target_compile_definitions(ggml-openvino PRIVATE GGML_OPENVINO_HAS_EVICT_ON_UPLOAD=1) + endif() +endif() diff --git a/ggml/src/ggml-openvino/dev_doc/OpenVINO-ENV-VARS.md b/ggml/src/ggml-openvino/dev_doc/OpenVINO-ENV-VARS.md index 243a40c5cf2a..b3f3808746fe 100644 --- a/ggml/src/ggml-openvino/dev_doc/OpenVINO-ENV-VARS.md +++ b/ggml/src/ggml-openvino/dev_doc/OpenVINO-ENV-VARS.md @@ -83,6 +83,7 @@ These variables lower the host memory peak. They do not change the output. Comma | `GGML_OPENVINO_REDUCE_COMPILE_MEM` | all | Boolean | value of `GGML_OPENVINO_MEMORY_OPTIMIZE` | Create fewer weight nodes during compilation, to lower the host memory in use. | | `GGML_OPENVINO_COMPILE_MEM_SPILL` | CPU, GPU | Float | `0.1` with `GGML_OPENVINO_MEMORY_OPTIMIZE=1`, else `0` | Back the host buffers of the backend (weights, KV cache, compute) with files in `GGML_OPENVINO_COMPILE_MEM_SPILL_DIR`, so the kernel can drop their pages. `0`: off. `1`: on, but the kernel drops the pages only when RAM runs out, so the memory peak does not change while RAM is free. A fraction in between: on, and while the model loads and compiles, keep at most this fraction of the spilled weights in RAM, also when RAM is free (`0.1` keeps 10%, Linux 5.4 or later). A fraction lowers the peak the most; the model file's pages are dropped too, and the first token can come later, while the weights are written to disk. Each start writes the buffers to disk: the load takes longer, and decoding can be a little slower. The disk space is reserved when the files are created (not for rewrites on btrfs or ZFS): more than the model size, on CPU also the KV cache. With too little space, the backend warns and uses RAM. NPU: off unless set, and only with `GGML_OPENVINO_NPU_L0_HOST_TENSORS=0`. Not part of the cache key. Ignored on Windows and in cache-only mode. | | `GGML_OPENVINO_COMPILE_MEM_SPILL_DIR` | CPU, GPU | String | `/var/tmp` | Directory for the `GGML_OPENVINO_COMPILE_MEM_SPILL` files. Use real storage, not tmpfs (`/tmp` on many systems): on tmpfs, the backend warns and uses RAM. | +| `GGML_OPENVINO_EVICT_ON_UPLOAD` | GPU | Boolean | `0` | Drop the host pages of each weight as soon as the GPU plugin has uploaded it, instead of after compilation. Lowers the peak of host plus GPU memory during `compile_model` (Qwen2.5-7B Q4_0 10.1 -> 6.6 GiB). Built on Linux with OpenVINO 2026.5, or a 2027.0 build from 2026-12 or later (CMake checks this); otherwise the backend warns and ignores it. Same conditions as `GGML_OPENVINO_RELEASE_WEIGHTS`. | | `GGML_OPENVINO_LAZY_HOST_THRESHOLD_MB` | NPU | Integer | `2048` | With `GGML_OPENVINO_SELF_CONTAINED_MMAP=1`: buffers of at least this size get demand-zero host memory instead of Level Zero host memory. | ## NPU plugin properties diff --git a/ggml/src/ggml-openvino/ggml-openvino-extra.cpp b/ggml/src/ggml-openvino/ggml-openvino-extra.cpp index 2ab49ba53300..c7df1d64dd63 100644 --- a/ggml/src/ggml-openvino/ggml-openvino-extra.cpp +++ b/ggml/src/ggml-openvino/ggml-openvino-extra.cpp @@ -182,6 +182,7 @@ constexpr env_var env_vars[] = { {"GGML_OPENVINO_EXACT_ZERO_POINT", env_type::BOOL}, {"GGML_OPENVINO_MEMORY_OPTIMIZE", env_type::BOOL}, {"GGML_OPENVINO_RELEASE_WEIGHTS", env_type::BOOL}, + {"GGML_OPENVINO_EVICT_ON_UPLOAD", env_type::BOOL}, {"GGML_OPENVINO_REDUCE_COMPILE_MEM", env_type::BOOL}, {"GGML_OPENVINO_LOG_UNSUPPORTED_OPS", env_type::BOOL}, {"GGML_OPENVINO_LOG_SUPPORTS_OP", env_type::INT}, diff --git a/ggml/src/ggml-openvino/ggml-openvino.cpp b/ggml/src/ggml-openvino/ggml-openvino.cpp index 848db4e0810d..b31c8f7e9727 100644 --- a/ggml/src/ggml-openvino/ggml-openvino.cpp +++ b/ggml/src/ggml-openvino/ggml-openvino.cpp @@ -34,6 +34,10 @@ #include #include #include +#ifdef GGML_OPENVINO_HAS_EVICT_ON_UPLOAD // set by CMakeLists.txt when the declaration below matches the library +# include "ggml-openvino/openvino/aligned_buffer.hpp" +# include +#endif #include #include #include @@ -313,6 +317,10 @@ struct ggml_backend_openvino_buffer_context { size_t spill_budget = 0; // source mappings already evicted once under the budget std::vector evicted_sources; +#ifdef GGML_OPENVINO_HAS_EVICT_ON_UPLOAD + // the buffer as the source of its weight Constants, see ggml_openvino_make_evictable() + std::shared_ptr weight_source; +#endif // One counter for both constructors: the id is part of the weight identity in the graph key, so a host-pointer // buffer and an allocated buffer must never share one. @@ -509,6 +517,8 @@ struct ov_weight_buffer { std::vector writes; bool restorable = true; bool released = false; + // only the ranges of uploaded constants were dropped (GGML_OPENVINO_EVICT_ON_UPLOAD), the rest is resident + bool partly_released = false; }; struct ov_weight_buffer_registry { @@ -600,6 +610,7 @@ bool ov_restore_weight_buffers(ov_weight_buffer_registry & reg) { total += w.size; } b.released = false; + b.partly_released = false; } close_fds(); if (total > 0) { @@ -673,7 +684,7 @@ void ggml_openvino_release_weight_buffers() { size_t count = 0; #ifdef __linux__ for (auto & b : reg.buffers) { - if (b.released || !b.restorable) { + if ((b.released && !b.partly_released) || !b.restorable) { continue; } // Align down/up to page boundaries so madvise only drops whole pages @@ -688,6 +699,7 @@ void ggml_openvino_release_weight_buffers() { if (madvise(astart, aligned_len, MADV_DONTNEED) == 0) { total += aligned_len; b.released = true; + b.partly_released = false; count++; } } @@ -700,6 +712,141 @@ void ggml_openvino_release_weight_buffers() { } } +#ifdef GGML_OPENVINO_HAS_EVICT_ON_UPLOAD +// The GPU plugin has uploaded a constant that lives in the weight buffer at |base|: drop the pages that hold only this +// constant, and mark the buffer released so that it is written again from the model file before the next compile +// (OpenVINO does not ask for evicted data back before reading it). Buffers that cannot be written again, or whose +// weights are read at run time, are kept. +static void ggml_openvino_evict_weight_range(void * base, char * first, size_t size) { + auto & reg = ov_weight_registry(); + std::lock_guard lock(reg.mutex); + if (reg.keep) { + return; + } + auto it = std::find_if(reg.buffers.begin(), reg.buffers.end(), + [base](const ov_weight_buffer & b) { return b.data == base; }); + if (it == reg.buffers.end() || !it->restorable) { + return; + } + const uintptr_t page = (uintptr_t) sysconf(_SC_PAGESIZE); + const uintptr_t lo = ((uintptr_t) first + page - 1) & ~(page - 1); + const uintptr_t hi = ((uintptr_t) first + size) & ~(page - 1); + if (hi <= lo) { + return; + } + if (!it->released) { + it->partly_released = true; + } + it->released = true; + madvise(reinterpret_cast(lo), hi - lo, MADV_DONTNEED); +} + +namespace { +// The address range of a weight buffer, as the source buffer of the views below. It does not own the memory. +class ggml_openvino_weight_source final : public ov::AlignedBuffer { + public: + ggml_openvino_weight_source(void * data, size_t size) { + m_aligned_buffer = static_cast(data); // char * in 2026.5, std::byte * in 2027.0 + m_byte_size = size; + } + ~ggml_openvino_weight_source() override { m_aligned_buffer = nullptr; } +}; + +// The data of one weight Constant. OpenVINO forwards the evict hint (the GPU plugin gives it after uploading a +// constant) only to buffers with a descriptor. +class ggml_openvino_weight_view final : public ov::AlignedBuffer { + public: + ggml_openvino_weight_view(const std::shared_ptr & source, void * data, size_t size) : + source_(source) { + m_aligned_buffer = static_cast(data); // char * in 2026.5, std::byte * in 2027.0 + m_byte_size = size; + const auto * base = static_cast(source->get_ptr()); + descriptor_ = ov::create_base_descriptor(reinterpret_cast(base), + static_cast(static_cast(data) - base), source); + } + ~ggml_openvino_weight_view() override { m_aligned_buffer = nullptr; } + + std::shared_ptr get_descriptor() const override { return descriptor_; } + + void hint_evict() noexcept override { + if (!evicted_.exchange(true)) { // the hint can come once per use of the constant + ggml_openvino_evict_weight_range(source_->get_ptr(), reinterpret_cast(m_aligned_buffer), + m_byte_size); + } + } + + private: + std::shared_ptr source_; + std::shared_ptr descriptor_; + std::atomic evicted_{false}; +}; +} // namespace + +// Rebuilds the Constants of a weight subgraph that point into the weight buffer as views of |source|. +static std::shared_ptr ggml_openvino_make_evictable(const std::shared_ptr & root, + const std::shared_ptr & source) { + const auto * lo = static_cast(source->get_ptr()); + const auto * hi = lo + source->size(); + auto rewrap = [&](const std::shared_ptr & c) -> std::shared_ptr { + const auto * p = static_cast(c->get_data_ptr()); + const size_t n = c->get_byte_size(); + if (p < lo || p + n > hi || n < (size_t) sysconf(_SC_PAGESIZE)) { + return nullptr; // not in the buffer, or too small to hold a whole page + } + auto view = std::make_shared(source, const_cast(p), n); + auto nc = std::make_shared(c->get_element_type(), c->get_shape(), view); + nc->set_friendly_name(c->get_friendly_name()); + nc->get_rt_info() = c->get_rt_info(); + return nc; + }; + if (auto c = ov::as_type_ptr(root)) { + auto nc = rewrap(c); + return nc ? nc : root; + } + std::vector> stack{root}; + std::set seen; + while (!stack.empty()) { + auto node = stack.back(); + stack.pop_back(); + if (!seen.insert(node.get()).second) { + continue; + } + for (auto & in : node->inputs()) { + auto src = in.get_source_output().get_node_shared_ptr(); + if (auto c = ov::as_type_ptr(src)) { + if (auto nc = rewrap(c)) { + in.replace_source_output(nc->output(0)); + } + } else { + stack.push_back(src); + } + } + } + return root; +} +#endif + +// GGML_OPENVINO_EVICT_ON_UPLOAD, where this build and device support it +static bool ggml_openvino_evict_on_upload_enabled() { + static const bool enabled = [] { + if (!ggml_openvino_getenv_int("GGML_OPENVINO_EVICT_ON_UPLOAD")) { + return false; + } +#ifdef GGML_OPENVINO_HAS_EVICT_ON_UPLOAD + if (ggml_openvino_is_gpu()) { + return true; + } + GGML_LOG_WARN("ggml-openvino: GGML_OPENVINO_EVICT_ON_UPLOAD works on GPU only, ignoring\n"); +#else + GGML_LOG_WARN("ggml-openvino: GGML_OPENVINO_EVICT_ON_UPLOAD is not available in this build: it needs Linux and " + "OpenVINO 2026.5, or a 2027.0 build from 2026-12 or later (built with %d.%d), ignoring\n", + OPENVINO_VERSION_MAJOR, OPENVINO_VERSION_MINOR); +#endif + return false; + }(); + return enabled; +} + bool ggml_openvino_restore_weight_buffers() { auto & reg = ov_weight_registry(); std::lock_guard lock(reg.mutex); @@ -1039,6 +1186,14 @@ static void ggml_backend_openvino_buffer_set_tensor(ggml_backend_buffer_t buffer try { auto result = process_weight_tensor(tensor, data, tensor->data); result.weight_node->set_friendly_name(tensor->name); + if (ggml_openvino_evict_on_upload_enabled() && !ctx->is_remote && !ctx->external_memory) { +#ifdef GGML_OPENVINO_HAS_EVICT_ON_UPLOAD + if (!ctx->weight_source) { + ctx->weight_source = std::make_shared(ctx->data, ctx->size); + } + result.weight_node = ggml_openvino_make_evictable(result.weight_node, ctx->weight_source); +#endif + } ggml_openvino_extra_base * extra; diff --git a/ggml/src/ggml-openvino/openvino/aligned_buffer.hpp b/ggml/src/ggml-openvino/openvino/aligned_buffer.hpp new file mode 100644 index 000000000000..33e4f7e7ed25 --- /dev/null +++ b/ggml/src/ggml-openvino/openvino/aligned_buffer.hpp @@ -0,0 +1,147 @@ +// Copyright (C) 2018-2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// +// Local mirror of OpenVINO's ov::AlignedBuffer, ov::IBufferDescriptor and ov::create_base_descriptor, for +// GGML_OPENVINO_EVICT_ON_UPLOAD. Two layouts: OpenVINO 2026.5, and 2027.0 where ov::AlignedBuffer derives from +// ov::util::IMutableBuffer (#40120). +// +// The bodies are provided by the linked libopenvino.so; only the declarations are needed here, which avoids building +// with OpenVINO's developer headers (same approach as the internal ops in op/). The backend derives from +// ov::AlignedBuffer, so the data members and the order of the virtual functions must stay in sync with +// openvino/src/core/dev_api/openvino/runtime/aligned_buffer.hpp +// openvino/src/core/dev_api/openvino/runtime/shared_buffer.hpp +// openvino/src/common/util/include/openvino/util/memory.hpp (2027.0) +// Include it only with the OpenVINO versions it was checked against (see ggml-openvino.cpp). 2027.0 nightlies built +// before #40120 (2026-11-30) still have the 2026.5 layout and do not load a backend built with this one. + +#pragma once + +#include +#include +#include +#include + +#include "openvino/core/core_visibility.hpp" +#include "openvino/core/version.hpp" + +#if OPENVINO_VERSION_MAJOR >= 2027 +namespace ov::util { + +struct MemoryProperties { + size_t offset = 0; +}; + +class IMemoryHints { + public: + virtual ~IMemoryHints() = default; + virtual void hint_evict() noexcept = 0; + virtual std::shared_future hint_prefetch() noexcept = 0; +}; + +class IBuffer : public IMemoryHints { + public: + virtual ~IBuffer() = default; + virtual const std::byte * data() const noexcept = 0; + virtual size_t size() const noexcept = 0; + virtual const MemoryProperties & get_properties() const noexcept = 0; + virtual std::optional get_id() const noexcept { return std::nullopt; } +}; + +class IMutableBuffer : public IBuffer { + public: + using IBuffer::data; + virtual std::byte * data() noexcept = 0; +}; + +} // namespace ov::util +#endif + +namespace ov { + +class AlignedBuffer; + +class OPENVINO_API IBufferDescriptor { + public: + virtual size_t get_id() const = 0; + virtual size_t get_offset() const = 0; + virtual std::shared_ptr get_source_buffer() const = 0; + virtual ~IBufferDescriptor(); +}; + +#if OPENVINO_VERSION_MAJOR >= 2027 +class OPENVINO_API AlignedBuffer : public util::IMutableBuffer { + public: + AlignedBuffer(size_t byte_size, size_t alignment = 64); + AlignedBuffer(); + ~AlignedBuffer() override; + + AlignedBuffer(AlignedBuffer && other); + AlignedBuffer & operator=(AlignedBuffer && other); + + const std::byte * data() const noexcept override final { return m_aligned_buffer; } + std::byte * data() noexcept override final { return m_aligned_buffer; } + size_t size() const noexcept override final { return m_byte_size; } + const util::MemoryProperties & get_properties() const noexcept override; + + void * get_ptr() { return m_aligned_buffer; } + const void * get_ptr() const { return m_aligned_buffer; } + + virtual std::shared_ptr get_descriptor() const; + + AlignedBuffer(const AlignedBuffer &) = delete; + AlignedBuffer & operator=(const AlignedBuffer &) = delete; + + void hint_evict() noexcept override; + std::shared_future hint_prefetch() noexcept override; + + protected: + virtual void hint_evict(size_t offset, size_t size) noexcept; + virtual std::shared_future hint_prefetch_async(size_t offset, size_t size) const noexcept; + + std::byte * m_aligned_buffer; + size_t m_byte_size; +}; +#else +class OPENVINO_API AlignedBuffer { + public: + AlignedBuffer(size_t byte_size, size_t alignment = 64); + AlignedBuffer(); + virtual ~AlignedBuffer(); + + AlignedBuffer(AlignedBuffer && other); + AlignedBuffer & operator=(AlignedBuffer && other); + + size_t size() const { return m_byte_size; } + + void * get_ptr() { + hint_prefetch(); + return m_aligned_buffer; + } + + const void * get_ptr() const { + hint_prefetch(); + return m_aligned_buffer; + } + + virtual std::shared_ptr get_descriptor() const; + + AlignedBuffer(const AlignedBuffer &) = delete; + AlignedBuffer & operator=(const AlignedBuffer &) = delete; + + virtual void hint_evict() noexcept; + virtual void hint_prefetch() const; + virtual std::shared_future hint_prefetch_async() const noexcept; + + protected: + virtual void hint_evict(size_t offset, size_t size) noexcept; + virtual std::shared_future hint_prefetch_async(size_t offset, size_t size) const noexcept; + + char * m_aligned_buffer; + size_t m_byte_size; +}; +#endif + +OPENVINO_API std::shared_ptr create_base_descriptor( + size_t id, size_t offset, const std::shared_ptr & source_buffer); + +} // namespace ov