llama: KV host shadow of a released cache holds only the used cells (F4)

On an arbiter release with LLAMA_SLEEP_EVICT_KV the KV cache was copied
whole to the host shadow (about 2.6 GB for the 35B at ctx 131072) and
uploaded whole on restore. Only the rows of the cells below
used_max_p1() of each stream are captured now (K, and V when not
transposed); the restore clears the new buffer first, so the other cells
read as zero as after the first allocation. The to_host restore of the
disk cache scatters the ranges into a full-layout buffer.

Knob: LLAMA_SLEEP_KV_USED_ONLY=0 captures every cell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PZz44SLQvTXMyWGio6t9DZ
This commit is contained in:
LumpiastyandClaude Opus 5.5 committed 2026-11-23 17:37:07 +01:00
1 parent 6a6c6c4e40
commit 9d8a4ffc60
2 files changed
+67 -12

No files matched your search

+56 -12
View File
@@ -2118,17 +2118,16 @@ void llama_kv_cache::release_device_buffers(bool keep_shadow) {
// capture live contents compactly in stable iteration order (skip views, which alias a base)
if (keep_shadow) {
size_t total = 0;
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
if (t->view_src == nullptr) { total += ggml_nbytes(t); }
if (t->view_src == nullptr) { shadow_ranges(t, sh.ranges); }
}
size_t total = 0;
for (const auto & r : sh.ranges) { total += r.size; }
sh.data.resize(total);
size_t off = 0;
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
if (t->view_src != nullptr) { continue; }
const size_t n = ggml_nbytes(t);
ggml_backend_tensor_get(t, sh.data.data() + off, 0, n);
off += n;
for (const auto & r : sh.ranges) {
ggml_backend_tensor_get(r.t, sh.data.data() + off, r.offs, r.size);
off += r.size;
}
}
@@ -2156,6 +2155,26 @@ bool llama_kv_cache::restore_device_buffers(bool to_host) {
}
ggml_context * ctx = ctxs_bufs.bufs[i].first.get();
if (to_host) {
// the host buffer holds every tensor whole: scatter the captured ranges, the other cells read as zero
size_t total = 0;
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
if (t->view_src == nullptr) { total += ggml_nbytes(t); }
}
std::vector<uint8_t> full(total);
size_t base = 0;
size_t off = 0;
size_t j = 0;
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
if (t->view_src != nullptr) { continue; }
for (; j < sh.ranges.size() && sh.ranges[j].t == t; ++j) {
memcpy(full.data() + base + sh.ranges[j].offs, sh.data.data() + off, sh.ranges[j].size);
off += sh.ranges[j].size;
}
base += ggml_nbytes(t);
}
GGML_ASSERT(j == sh.ranges.size() && off == sh.data.size());
sh.data.swap(full);
sh.ranges.clear();
ctxs_bufs.bufs[i].second.reset(llama_memory_wrap_host(ctx, sh.data));
continue;
}
@@ -2164,12 +2183,12 @@ bool llama_kv_cache::restore_device_buffers(bool to_host) {
LLAMA_LOG_ERROR("%s: failed to reallocate KV cache device buffer (out of VRAM?)\n", __func__);
return false;
}
// the cells that were not captured read as zero, as after the first allocation
ggml_backend_buffer_clear(buf, 0);
size_t off = 0;
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
if (t->view_src != nullptr) { continue; }
const size_t n = ggml_nbytes(t);
ggml_backend_tensor_set(t, sh.data.data() + off, 0, n);
off += n;
for (const auto & r : sh.ranges) {
ggml_backend_tensor_set(r.t, sh.data.data() + off, r.offs, r.size);
off += r.size;
}
ctxs_bufs.bufs[i].second.reset(buf);
}
@@ -2180,6 +2199,31 @@ bool llama_kv_cache::restore_device_buffers(bool to_host) {
return true;
}
void llama_kv_cache::shadow_ranges(ggml_tensor * t, std::vector<device_buffer_shadow::range> & ranges) const {
// LLAMA_SLEEP_KV_USED_ONLY=0: capture every cell
static const bool used_only = [] {
const char * env = getenv("LLAMA_SLEEP_KV_USED_ONLY");
return env == nullptr || strcmp(env, "0") != 0;
}();
// the cells are the rows of K, and of V when V is not transposed
bool cells = false;
for (const auto & layer : layers) {
cells = cells || t == layer.k || (t == layer.v && !v_trans);
}
if (!used_only || !cells || !ggml_is_contiguous(t) || (size_t) t->ne[2] != v_cells.size()) {
ranges.push_back({ t, 0, ggml_nbytes(t) });
return;
}
for (int64_t s = 0; s < t->ne[2]; ++s) {
GGML_ASSERT(v_cells[s].size() == (uint32_t) t->ne[1]);
const uint32_t n = v_cells[s].used_max_p1();
if (n > 0) {
ranges.push_back({ t, s*t->nb[2], n*t->nb[1] });
}
}
}
size_t llama_kv_cache::size_k_bytes() const {
size_t size_k_bytes = 0;
+11
View File
@@ -330,7 +330,18 @@ private:
ggml_backend_buffer_type_t buft = nullptr;
bool releasable = false; // device (VRAM) buffer that was freed
std::vector<uint8_t> data; // captured tensor bytes (compact, iteration order)
// [fork] the captured byte ranges, in the order of data; empty: every tensor whole
struct range {
ggml_tensor * t;
size_t offs;
size_t size;
};
std::vector<range> ranges;
};
// [fork] the byte ranges of a KV tensor that a shadow captures: only the used cells of K and V
void shadow_ranges(ggml_tensor * t, std::vector<device_buffer_shadow::range> & ranges) const;
std::vector<device_buffer_shadow> dev_shadows; // parallel to ctxs_bufs
bool dev_released = false;