diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index 7494f98d4..221e0db18 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -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 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 & 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; diff --git a/src/llama-kv-cache.h b/src/llama-kv-cache.h index 500d67187..30c35ac86 100644 --- a/src/llama-kv-cache.h +++ b/src/llama-kv-cache.h @@ -330,7 +330,18 @@ private: ggml_backend_buffer_type_t buft = nullptr; bool releasable = false; // device (VRAM) buffer that was freed std::vector 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 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 & ranges) const; std::vector dev_shadows; // parallel to ctxs_bufs bool dev_released = false;