serve: optionally reclaim host caches before SAVE admission

This commit is contained in:
tuandat3019 authored and Niko1221 committed 2026-10-09 03:27:49 +02:00
1 parent 8824e217c4
commit 73ed39ae8e
6 files changed
+230

No files matched your search

+17
View File
@@ -22,6 +22,7 @@
#include "strata/core/expert_cache.hpp"
#include "strata/core/conversation_snapshot.hpp"
#include "strata/core/conversation_file.hpp"
#include "strata/core/session_save_reclaim.hpp"
#include "strata/core/conversation_memory.hpp"
#include "strata/core/coupled_draft.hpp"
#include "strata/core/expert_source.hpp"
@@ -597,6 +598,7 @@ struct Options {
int64_t conversation_cache_min_tokens = 0;
/// --serve SAVE: disk space a session file must leave free where it is written (MiB; 0 = no check)
int64_t session_min_free_mib = 4096;
bool session_save_reclaim = false;
/// --serve: also keep a checkpoint every N freshly read prompt tokens (0 = only at the last turn boundary)
int64_t prompt_cache_every = 16384;
bool prompt_cache_tail = false; // optional extra checkpoint at an existing near-tail chunk boundary
@@ -742,6 +744,7 @@ void usage() {
" --conversation-cache-min-tokens N --serve: park only conversations of at least N tokens, so\n"
" short side requests do not take the slots a long one needs (default 0 = park any)\n"
" --session-min-free-mib N --serve: disk space a SAVE must leave free (default 4096; 0 = no check)\n"
" --session-save-reclaim --serve: reclaim unpinned host caches if SAVE needs RAM (opt-in)\n"
" --vram-elastic --serve (#533, opt-in, NVIDIA): the expert cache in segments (--vram-segment-mib,\n"
" default 512), so the command `VRAM <reserve_mib>` (the server's POST /v1/vram) can\n"
" give VRAM back to other programs between requests and take it back later\n"
@@ -1818,6 +1821,7 @@ int main(int argc, char** argv) {
else if (a == "--pcie-mode") o.pcie_mode = next("--pcie-mode");
else if (a == "--serve") o.serve = true;
else if (a == "--vision") o.vision = true;
else if (a == "--session-save-reclaim") o.session_save_reclaim = true;
else if (a == "--prompt-cache") o.prompt_cache = std::max(0, std::atoi(next("--prompt-cache")));
else if (a == "--conversation-cache-mib" || a == "--conversation-cache-slots" ||
a == "--conversation-cache-min-free-mib" || a == "--conversation-cache-min-tokens" ||
@@ -8809,6 +8813,19 @@ int main(int argc, char** argv) {
sl.kv_layers = (uint64_t) std::max<int64_t>(ss.qsa_alloc, 0) + 1;
}
const uint64_t floor = (uint64_t) o.conversation_cache_min_free_mib << 20;
if (o.session_save_reclaim) {
const auto freed = strata::core::session_save_reclaim(
checks, conversations, sl, floor, strata::core::conversation_available_memory);
if (freed.bytes || freed.checkpoints || freed.parked) {
// A removed tail must not mark a later checkpoint at this length as a tail.
if (tail_ckpt_len >= 0 && std::none_of(checks.begin(), checks.end(), [&](const auto& c) {
return int64_t(c.ids.size()) == tail_ckpt_len;
})) tail_ckpt_len = -1;
std::fprintf(stderr, "strata serve: save reclaimed %zu checkpoints, %zu parked, "
"%zu host bytes; checking RAM again\n",
freed.checkpoints, freed.parked, freed.bytes);
}
}
auto admit = [&](uint64_t need, std::string& why) {
const auto avail = strata::core::conversation_available_memory();
if (strata::core::conversation_memory_admit(avail, need, floor)) return true;