Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ggml/src/ggml-openvino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <openvino/core/version.hpp>
#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<ov::IBufferDescriptor> get_descriptor() const override { return ov::create_base_descriptor(0, 0, nullptr); }
void hint_evict() noexcept override {}
};
int main() { return std::make_shared<view>()->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()
1 change: 1 addition & 0 deletions ggml/src/ggml-openvino/dev_doc/OpenVINO-ENV-VARS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-openvino/ggml-openvino-extra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
157 changes: 156 additions & 1 deletion ggml/src/ggml-openvino/ggml-openvino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <openvino/runtime/intel_npu/level_zero/level_zero.hpp>
#include <openvino/runtime/properties.hpp>
#include <openvino/runtime/tensor.hpp>
#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 <set>
#endif
#include <string>
#include <system_error>
#include <thread>
Expand Down Expand Up @@ -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<uintptr_t> 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<ov::AlignedBuffer> 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.
Expand Down Expand Up @@ -509,6 +517,8 @@ struct ov_weight_buffer {
std::vector<ov_weight_write> 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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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++;
}
}
Expand All @@ -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<std::mutex> 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<void *>(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<decltype(m_aligned_buffer)>(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<ov::AlignedBuffer> & source, void * data, size_t size) :
source_(source) {
m_aligned_buffer = static_cast<decltype(m_aligned_buffer)>(data); // char * in 2026.5, std::byte * in 2027.0
m_byte_size = size;
const auto * base = static_cast<const char *>(source->get_ptr());
descriptor_ = ov::create_base_descriptor(reinterpret_cast<uintptr_t>(base),
static_cast<size_t>(static_cast<char *>(data) - base), source);
}
~ggml_openvino_weight_view() override { m_aligned_buffer = nullptr; }

std::shared_ptr<ov::IBufferDescriptor> 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<char *>(m_aligned_buffer),
m_byte_size);
}
}

private:
std::shared_ptr<ov::AlignedBuffer> source_;
std::shared_ptr<ov::IBufferDescriptor> descriptor_;
std::atomic<bool> evicted_{false};
};
} // namespace

// Rebuilds the Constants of a weight subgraph that point into the weight buffer as views of |source|.
static std::shared_ptr<ov::Node> ggml_openvino_make_evictable(const std::shared_ptr<ov::Node> & root,
const std::shared_ptr<ov::AlignedBuffer> & source) {
const auto * lo = static_cast<const char *>(source->get_ptr());
const auto * hi = lo + source->size();
auto rewrap = [&](const std::shared_ptr<ov::op::v0::Constant> & c) -> std::shared_ptr<ov::op::v0::Constant> {
const auto * p = static_cast<const char *>(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<ggml_openvino_weight_view>(source, const_cast<char *>(p), n);
auto nc = std::make_shared<ov::op::v0::Constant>(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<ov::op::v0::Constant>(root)) {
auto nc = rewrap(c);
return nc ? nc : root;
}
std::vector<std::shared_ptr<ov::Node>> stack{root};
std::set<ov::Node *> 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<ov::op::v0::Constant>(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<std::mutex> lock(reg.mutex);
Expand Down Expand Up @@ -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<ggml_openvino_weight_source>(ctx->data, ctx->size);
}
result.weight_node = ggml_openvino_make_evictable(result.weight_node, ctx->weight_source);
#endif
}

ggml_openvino_extra_base * extra;

Expand Down
Loading