From 9119ec6e89540091980b86b2dcf7fbbf795d105a Mon Sep 17 00:00:00 2001 From: Lumpiasty Date: Sat, 25 Jul 2026 14:36:17 +0200 Subject: [PATCH] llama: guard buffer iteration against released device buffers release_device_weights()/release_device_buffers() leave a null buffer in ctxs_bufs while the device memory is released for on-demand VRAM sharing. The memory-breakdown / total_size / clear paths iterated these and called ggml_backend_buffer_get_size()/get_type()/clear() on the null buffer, tripping GGML_ASSERT(buffer) and aborting on shutdown of a cold model. Skip null buffers in llama_model::memory_breakdown and llama_kv_cache::{memory_breakdown, total_size,clear}. Assisted-by: Claude --- src/llama-kv-cache.cpp | 8 ++++++-- src/llama-memory.cpp | 3 +++ src/llama-model.cpp | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index a24f56354..c167a79a5 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -429,7 +429,9 @@ void llama_kv_cache::clear(bool data) { if (data) { for (auto & [_, buf] : ctxs_bufs) { - ggml_backend_buffer_clear(buf.get(), 0); + if (buf) { // may be null if evicted for on-demand VRAM sharing + ggml_backend_buffer_clear(buf.get(), 0); + } } } } @@ -2084,7 +2086,9 @@ size_t llama_kv_cache::total_size() const { size_t size = 0; for (const auto & [_, buf] : ctxs_bufs) { - size += ggml_backend_buffer_get_size(buf.get()); + if (buf) { // may be null if evicted for on-demand VRAM sharing + size += ggml_backend_buffer_get_size(buf.get()); + } } return size; diff --git a/src/llama-memory.cpp b/src/llama-memory.cpp index 80099e76a..27528eea4 100644 --- a/src/llama-memory.cpp +++ b/src/llama-memory.cpp @@ -100,6 +100,9 @@ ggml_backend_buffer_t llama_memory_bufs::emplace(ggml_context_ptr ctx, ggml_back std::map llama_memory_bufs::memory_breakdown() const { std::map ret; for (const auto & [ctx, buf] : bufs) { + if (!buf) { // may be null if evicted for on-demand VRAM sharing + continue; + } ggml_backend_buffer_type_t buft = ggml_backend_buffer_get_type(buf.get()); if (no_alloc) { diff --git a/src/llama-model.cpp b/src/llama-model.cpp index 589ee8c83..f54434141 100644 --- a/src/llama-model.cpp +++ b/src/llama-model.cpp @@ -2780,6 +2780,11 @@ std::map llama_model::memory_breakdown() con ret[buft] += ggml_backend_alloc_ctx_tensors_from_buft_size(ctx.get(), buft); } else { for (const auto & buf : bufs) { + // buf may be null if the device weights were released for on-demand VRAM sharing + // (see release_device_weights); skip it in the breakdown + if (!buf) { + continue; + } // GGML_ASSERT(ggml_backend_buffer_get_base(buf.get()) != nullptr); // multi_buffer does not have a defined base ret[ggml_backend_buffer_get_type(buf.get())] += ggml_backend_buffer_get_size(buf.get()); }