Session files: save and restore a conversation to disk (SAVE/RESTORE, POST /slots/0?action=save|restore)
A conversation the engine holds (what a parked snapshot holds) goes to one file and comes back, also after a
restart, so a long prompt is not read again:
- engine: `SAVE <path>` / `RESTORE <path>` between requests, answered `SAVED|RESTORED <tokens> <bytes> <ms>` or
`ERR <reason>`; single GPU only (refused with --layer-split) and needs --prompt-cache > 0.
- format v1 (src/core/conversation_file.cpp): 64-byte header bound to a model fingerprint (name, size, first and
last MiB of each weight file) and a config fingerprint (engine version, --kv, STRATA_KV_ROT, --kv-resident,
--max-context, --mtp-window, control vectors), the payload, a payload hash and an end marker; written to
`<path>.tmp` and renamed over `path` (MoveFileExW on Windows).
- fail-closed restore: size, header, both fingerprints, every count against the bytes left, the payload hash and
the usual snapshot validation all run before any device write; a refused file leaves the session untouched.
- only the deepest checkpoint is saved (the next turn's resume point) and the K/V is streamed from the pools
straight into the file's 16 MiB blocks, without a full host capture; Linux uses O_DIRECT when the filesystem
takes it (buffered otherwise or with STRATA_SESSION_BUFFERED=1), Windows buffered Win32 I/O.
- serve/server.py: llama-server's `POST /slots/0?action=save|restore {"filename": NAME}` with --slot-save-path DIR
(or "slot_save_path" in the config); NAME must be a plain file name inside DIR.
RTX 4070 Ti, IQ3_XXS, a 63,025-token conversation: file 1.20 GB, save 0.72 s, restore 0.89 s, the next 32-token
turn after a restore 1.15 s (first token 0.55 s) with the same output tokens as without saving.
Tests: conversation_file_test (CPU, no model; also built for Windows and run under Wine),
conversation_snapshot_test, serve/test_slots.py, serve/test_security.py.
This commit is contained in:
1 parent
f81876ecef
commit
aa17fe8648
12 files changed
+1453
No files matched your search
@@ -19,6 +19,7 @@
|
||||
#include "strata/core/remote_expert_opt.hpp"
|
||||
#include "strata/core/expert_cache.hpp"
|
||||
#include "strata/core/conversation_snapshot.hpp"
|
||||
#include "strata/core/conversation_file.hpp"
|
||||
#include "strata/core/conversation_memory.hpp"
|
||||
#include "strata/core/coupled_draft.hpp"
|
||||
#include "strata/core/expert_source.hpp"
|
||||
@@ -6091,6 +6092,28 @@ int main(int argc, char** argv) {
|
||||
strata::core::ConversationCache conversations(
|
||||
o.prompt_cache > 0 ? (size_t) o.conversation_cache_mib * 1024 * 1024 : 0,
|
||||
(size_t) o.conversation_cache_slots);
|
||||
// Disk sessions: what a session file is bound to. The model fingerprint samples the weight files once
|
||||
// (conversation_file.hpp); the config fingerprint covers the options that change what the saved bytes mean.
|
||||
std::optional<uint64_t> model_fp;
|
||||
auto session_identity = [&](strata::core::SessionFileIdentity& id, std::string& e) -> bool {
|
||||
if (!model_fp) {
|
||||
std::vector<std::string> files = o.native_shards;
|
||||
for (const std::string* p : {&o.pack, &o.ple_gguf, &o.mtp}) if (!p->empty()) files.push_back(*p);
|
||||
uint64_t fp = 0;
|
||||
if (!strata::core::session_model_fingerprint(files, fp, e)) return false;
|
||||
model_fp = fp;
|
||||
}
|
||||
const char* rot = std::getenv("STRATA_KV_ROT");
|
||||
std::string cfg = std::string("engine=") + STRATA_VERSION + ";kv=" + o.kv +
|
||||
";max_context=" + std::to_string(o.max_context) +
|
||||
";kv_resident=" + std::to_string(o.kv_resident) +
|
||||
";mtp_window=" + std::to_string(o.mtp_window) +
|
||||
";kv_rot=" + (rot != nullptr && rot[0] == '1' ? "1" : "0") + ";cvec=";
|
||||
for (const auto& c : o.cvec_files) cfg += c.first + ":" + std::to_string(c.second) + ",";
|
||||
id.model = *model_fp;
|
||||
id.config = strata::core::session_hash64(cfg.data(), cfg.size(), 0x434f4e464947ull);
|
||||
return true;
|
||||
};
|
||||
// Save only on a switch/rewind, not on each continuing request. No graph
|
||||
// addresses change: all parked images live in ordinary host vectors.
|
||||
auto park_current_body = [&](size_t held) -> bool {
|
||||
@@ -7481,6 +7504,85 @@ int main(int argc, char** argv) {
|
||||
} busy_scope;
|
||||
stop_req.store(false); // a STOP that arrived between requests is stale
|
||||
err.clear();
|
||||
// Disk sessions: SAVE <path> | RESTORE <path>, between requests. The file holds
|
||||
// what a parked conversation holds (conversation_file.hpp); a refused file leaves the session untouched.
|
||||
if (line.rfind("SAVE ", 0) == 0 || line.rfind("RESTORE ", 0) == 0) {
|
||||
const bool save = line[0] == 'S';
|
||||
const std::string path = line.substr(save ? 5 : 8);
|
||||
const auto t0 = Clock::now();
|
||||
auto ms = [&] { return std::chrono::duration<double, std::milli>(Clock::now() - t0).count(); };
|
||||
auto refuse = [&](const std::string& why) {
|
||||
std::fprintf(stderr, "strata serve: session %s %s: %s\n", save ? "save" : "restore", path.c_str(),
|
||||
why.c_str());
|
||||
std::printf("ERR %s\n", why.c_str());
|
||||
std::fflush(stdout);
|
||||
err.clear();
|
||||
};
|
||||
if (path.empty()) { refuse("missing path"); continue; }
|
||||
if (!stages.empty() || multi_gpu) { refuse("session files do not support --layer-split"); continue; }
|
||||
if (o.prompt_cache <= 0) { refuse("session files need --prompt-cache > 0"); continue; }
|
||||
if (!ver.wait_commit(err)) {
|
||||
std::printf("ERR %s\n", err.c_str());
|
||||
return 1;
|
||||
}
|
||||
strata::core::SessionFileIdentity id;
|
||||
if (!session_identity(id, err)) { refuse(err); continue; }
|
||||
if (save) {
|
||||
if (!live_ok || live.empty()) { refuse("no complete session to save"); continue; }
|
||||
// only the deepest checkpoint goes to disk (the next turn's resume point); the K/V is streamed
|
||||
// from the authoritative pools straight into the file, without a full host capture
|
||||
const auto disk_checks = strata::core::session_checkpoints_to_save(checks);
|
||||
const strata::core::ConversationView view{live, live_imgs, disk_checks, cvec_cached};
|
||||
strata::core::SavedConversation meta;
|
||||
std::vector<strata::core::SessionKvSource> sources;
|
||||
size_t bytes = 0;
|
||||
try {
|
||||
if (!strata::core::conversation_snapshot_sources(meta, sources, view, ss, g, mtp.kv_state(),
|
||||
err)) {
|
||||
refuse(err);
|
||||
continue;
|
||||
}
|
||||
} catch (const std::bad_alloc&) { refuse("not enough RAM for the snapshot"); continue; }
|
||||
const double capture_ms = ms();
|
||||
if (!strata::core::session_file_write(path, meta, sources, id, bytes, err)) { refuse(err); continue; }
|
||||
std::fprintf(stderr, "strata serve: session saved %zu tokens, %zu of %zu checkpoints, %zu bytes to %s "
|
||||
"in %.1f ms (state %.1f ms)\n", live.size(), disk_checks.size(), checks.size(), bytes,
|
||||
path.c_str(), ms(), capture_ms);
|
||||
std::printf("SAVED %zu %zu %.1f\n", live.size(), bytes, ms());
|
||||
} else {
|
||||
strata::core::SavedConversation image;
|
||||
size_t bytes = 0;
|
||||
try {
|
||||
if (!strata::core::session_file_read(path, id, image, bytes, err)) { refuse(err); continue; }
|
||||
} catch (const std::bad_alloc&) { refuse("not enough RAM to read the session"); continue; }
|
||||
const double read_ms = ms();
|
||||
// the whole image against this engine, still without any CUDA write
|
||||
if (!strata::core::conversation_snapshot_validate(image, ss, g, mtp.kv_state(), err)) {
|
||||
refuse(err);
|
||||
continue;
|
||||
}
|
||||
conversations.take_reuse(); // retained K/V described the outgoing session
|
||||
live_ok = false;
|
||||
if (strata::core::conversation_snapshot_restore(image, ss, g, mtp.kv_state(), err) !=
|
||||
strata::core::ConversationRestore::restored) {
|
||||
// validated above: a failure here is a transfer failure - never decode from a partial state
|
||||
std::printf("ERR restoring session file: %s\n", err.c_str());
|
||||
return 1;
|
||||
}
|
||||
live = std::move(image.live.ids);
|
||||
live_imgs = std::move(image.live.imgs);
|
||||
checks = std::move(image.checkpoints);
|
||||
for (const ConvCheckpoint& c : checks) check_clock = std::max(check_clock, c.used);
|
||||
cvec_cached = image.cvec;
|
||||
live_ok = true;
|
||||
std::fprintf(stderr, "strata serve: session restored %zu tokens, %zu checkpoints, %zu bytes from %s "
|
||||
"in %.1f ms (read+check %.1f ms)\n", live.size(), checks.size(), bytes, path.c_str(),
|
||||
ms(), read_ms);
|
||||
std::printf("RESTORED %zu %zu %.1f\n", live.size(), bytes, ms());
|
||||
}
|
||||
std::fflush(stdout);
|
||||
continue;
|
||||
}
|
||||
const bool geni = line.rfind("GENI ", 0) == 0;
|
||||
if (!geni && line.rfind("GEN ", 0) != 0) {
|
||||
std::printf("ERR expected: GEN <max_new> <id,id,...> or GENI <max_new> <file> <id,id,...>\n");
|
||||
|
||||
Reference in new issue
Block a user