mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 23:38:47 +00:00
conversation cache: the SSD tier - evicted conversations go to disk and come back (#5697, P2 step 2b)
Step 2b of P2 in the cache admission design (one admission: RAM first, then SSD). The ConversationSpill
store (577e1287a, the session-file format, its identity and read limits) is wired into the serve loop.
Where it lives: by default `<data>/conversation-spill`, where <data> is the folder --pack sits in
(`<data>/packs/<tag>`), in this process's own namespace (dead ones are cleaned at start). Budget:
min(32 GiB, a quarter of the free disk), never into --session-min-free-mib. Tester flags, no help line
(text pending): --conversation-spill-dir DIR, --conversation-spill-mib N (0 = off). A single-stage serve
only; with no data folder or a full disk the engine says so in one line and runs without it.
What goes there (only under pressure; a park that fits in RAM never touches the disk):
- every conversation the RAM tier evicts (its byte budget, its slot count, the RAM gate):
ConversationCache::set_evict_sink hands the victim over instead of destroying it;
- a captured conversation the RAM floor refuses after the capture (entry c), written out instead of
thrown away;
- but only when it pays: SpillCost compares re-reading the conversation (its tokens over this engine's
measured prompt speed) with 1.25 x its write + read time (this engine's measured disk speeds). A
conversation's image has a fixed-size running state (about 236 MB on this model), so short ones stay
off the disk. On l620, under 100 tokens is never spilled, 8K-32K always is, and about 2K is decided by
the measured speeds; on a slower card more is worth it. The prompt speed is learned from passes of 512
tokens or more, timed from after the park and the restore.
What comes back: a request whose prompt a spilled conversation serves better than the session, a batch
slot and every RAM entry reads it under the session RESTORE's limits and RAM check, and resumes at that
entry's prefix. A file that does not read back is dropped and the prompt is read again; context is never
cut. #879 (non-finite logits) clears the SSD tier with the RAM tier.
Tests:
- Unit: conversation_cache_test (the sink gets every eviction whole; not superseded copies, not clear())
13,530 checks; conversation_spill_test 58 checks (clear(), SpillCost on l620's speeds and on a slower
card, samples that say nothing ignored). Boston, CUDA 13.1 sm_86 + sm_120, the whole conversation test
set plus the #6059 probe: E1116-WT-OK (boston-20261116-1651-4783), generate.cpp warnings 4 = head.
- Alternating replays on l620 (RTX PRO 5000, IQ3_S, the shared BASE-05 setup), 2 rounds after the first
turn, with the SSD tier, without it, and with a RAM tier large enough to keep everything:
- The first replay found the restore resuming at the RAM match's prefix instead of the SSD entry's (all
SSD reads reused 0); fixed here before the commit.
- Tokens: a reused state and a freshly read prompt can differ in this engine without any SSD tier (the
RAM-only run against the no-cache run differs at 8 long sessions): a computation-path difference.
Compared strictly (a request counts only when its session had the same reuse and the same replies at
every earlier turn in both runs), all 12 pairs are identical, 0 differences, including the SSD tier
against RAM-only (l620-20261116-1706-6baa).
- Wall time, with / without the SSD tier (ms; short = 2K / 8K / short chats, long = 2K / 8K / 16K / 32K):
short 4: p50 501 / 430, p95 729 / 1,846, sum 4,168 / 5,014
short 8: p50 605 / 607, p95 1,014 / 1,878, sum 10,417 / 13,763
long 4: p50 538 / 413, p95 1,313 / 3,326, sum 5,515 / 6,565
long 8: p50 1,138 / 2,632, p95 1,374 / 6,069, sum 17,672 / 48,454
The p50 at 4 sessions is worse: the spill write runs in the request (about 290-400 ms for a 16-32K
conversation). Step 2c moves it to a background writer; the default is gated on 2b + 2c together.
This commit is contained in:
1 parent
019f3e2a3a
commit
69857af168
5 files changed
+249
-8
No files matched your search
@@ -9,6 +9,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -277,6 +278,10 @@ public:
|
||||
/// an eviction, a refused park or a failed RAM check that the cache would not have had without it. Not owned; the
|
||||
/// pool outlives the cache. bytes() stays the images' (what the logs and every caller have always meant).
|
||||
void set_pool(SegmentPoolBudget* pool) { pool_ = pool; }
|
||||
/// The SSD tier (P2 of the cache admission design): a conversation the budget or the RAM gate evicts is handed to `sink`
|
||||
/// instead of being destroyed (its RAM is freed when the sink returns). Not for the copies drop_superseded() drops (the
|
||||
/// outgoing chain holds them) or for clear() (#879: after non-finite logits nothing is kept). Empty: as before.
|
||||
void set_evict_sink(std::function<void(SavedConversation&&)> sink) { sink_ = std::move(sink); }
|
||||
size_t pool_idle_bytes() const { return pool_ != nullptr ? pool_->idle_bytes() : 0; }
|
||||
/// What the cache holds in RAM: its images, the retained K/V and the pool's idle bytes
|
||||
size_t resident_bytes() const { return bytes() + pool_idle_bytes(); }
|
||||
@@ -579,8 +584,10 @@ private:
|
||||
for (size_t i = 0; i < entries_.size(); ++i) {
|
||||
if (entries_[i].pinned() || (guarded_ != 0 && serials_[i] == guarded_)) continue;
|
||||
bytes_ -= entries_[i].bytes();
|
||||
SavedConversation victim = sink_ ? std::move(entries_[i]) : SavedConversation{};
|
||||
erase(i);
|
||||
++evictions_;
|
||||
if (sink_) sink_(std::move(victim)); // (its RAM is freed when the sink is done with it)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -597,6 +604,7 @@ private:
|
||||
bool guard_blocked_ = false;
|
||||
ConversationKvReuse reuse_;
|
||||
SegmentPoolBudget* pool_ = nullptr; // STRATA_PARK_SEGPOOL_MIB (not owned)
|
||||
std::function<void(SavedConversation&&)> sink_; // P2: where an evicted conversation goes (set_evict_sink)
|
||||
};
|
||||
|
||||
} // namespace strata::core
|
||||
@@ -96,6 +96,29 @@ inline unsigned long own_pid() { return (unsigned long) ::getpid(); }
|
||||
#endif
|
||||
} // namespace spill_detail
|
||||
|
||||
/// Whether a conversation is worth writing to the SSD tier: reading it back (and writing it first) must cost less than reading its
|
||||
/// prompt again. A parked image has a fixed-size running state (about 236 MB on Qwen3.8-Flash-Next) whatever its length, so a short
|
||||
/// conversation is cheaper to re-read than to round-trip through the disk, and a long one is not (l620, RTX PRO 5000: a 51-token
|
||||
/// conversation re-read in about 220 ms against 120 ms written + 220 ms read back; an 8K one 1.6 s against about 0.5 s). The speeds
|
||||
/// are this engine's own, measured as it runs (moving averages; defaults until the first measurement: 2 tokens per ms - a mid-range
|
||||
/// card's prompt speed - and 1 GB/s each way). The prompt speed is learned only from passes of at least kMinTokens fresh tokens, so
|
||||
/// a request's fixed costs do not make re-reading look slow.
|
||||
struct SpillCost {
|
||||
static constexpr int64_t kMinTokens = 512;
|
||||
static constexpr double kAlpha = 0.3, kMargin = 1.25;
|
||||
double reread_tokens_per_ms = 2.0;
|
||||
double write_bytes_per_ms = 1.0e6, read_bytes_per_ms = 1.0e6;
|
||||
static void blend(double& avg, double sample) {
|
||||
if (sample > 0.0 && sample < 1e300) avg = avg * (1.0 - kAlpha) + sample * kAlpha;
|
||||
}
|
||||
void saw_prompt(int64_t tokens, double ms) { if (tokens >= kMinTokens && ms > 0.0) blend(reread_tokens_per_ms, (double) tokens / ms); }
|
||||
void saw_write(uint64_t bytes, double ms) { if (bytes > 0 && ms > 0.0) blend(write_bytes_per_ms, (double) bytes / ms); }
|
||||
void saw_read(uint64_t bytes, double ms) { if (bytes > 0 && ms > 0.0) blend(read_bytes_per_ms, (double) bytes / ms); }
|
||||
double reread_ms(int64_t tokens) const { return tokens > 0 ? (double) tokens / reread_tokens_per_ms : 0.0; }
|
||||
double round_trip_ms(uint64_t bytes) const { return (double) bytes / write_bytes_per_ms + (double) bytes / read_bytes_per_ms; }
|
||||
bool worth(int64_t tokens, uint64_t bytes) const { return reread_ms(tokens) > kMargin * round_trip_ms(bytes); }
|
||||
};
|
||||
|
||||
/// This process's spill folder. open() makes `<root>`, cleans the dead folders and takes its own; the destructor gives the
|
||||
/// lock back and removes its own folder (an orderly end; after a crash the next start's clean_dead does it).
|
||||
class SpillNamespace {
|
||||
@@ -264,8 +287,14 @@ public:
|
||||
budget_.drop(entries_[i].bytes);
|
||||
entries_.erase(entries_.begin() + (std::ptrdiff_t) i);
|
||||
}
|
||||
void close() {
|
||||
/// Drops every entry and keeps the tier open (#879: after non-finite logits no parked conversation is kept). Returns how many.
|
||||
size_t clear() {
|
||||
const size_t n = entries_.size();
|
||||
while (!entries_.empty()) drop(entries_.size() - 1);
|
||||
return n;
|
||||
}
|
||||
void close() {
|
||||
clear();
|
||||
ns_.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -744,6 +744,28 @@ int main() {
|
||||
check(slots.put(std::move(p4)) && !slots.make_room(one) && slots.size() == 1 && slots.evictions() == 0,
|
||||
"one slot, held by a pin: refused, nothing evicted");
|
||||
}
|
||||
{ // P2: an evicted conversation goes to the sink (the SSD tier), whole; superseded copies and clear() do not
|
||||
const size_t one = image({1,2,3}).bytes();
|
||||
ConversationCache c(one * 2, 4);
|
||||
std::vector<SavedConversation> sunk;
|
||||
c.set_evict_sink([&](SavedConversation&& s) { sunk.push_back(std::move(s)); });
|
||||
check(c.put(image({1,2,3})) && c.put(image({4,5,6})), "two parked");
|
||||
check(c.put(image({7,8,9})) && sunk.size() == 1 && sunk[0].live.ids == std::vector<int32_t>({1,2,3}) &&
|
||||
sunk[0].live.gdn == std::vector<uint8_t>(64, 7), "the budget's eviction hands the oldest over whole");
|
||||
check(c.evict_oldest() && sunk.size() == 2 && sunk[1].live.ids == std::vector<int32_t>({4,5,6}),
|
||||
"the RAM gate's eviction too");
|
||||
check(c.size() == 1 && c.evictions() == 2, "the cache keeps its own count as before");
|
||||
auto chained = image({7,8,9,10});
|
||||
chained.checkpoints.push_back(ConversationCheckpoint{});
|
||||
chained.checkpoints.back().ids = {7,8,9};
|
||||
check(c.drop_superseded(chained.live.ids, chained.live.imgs, chained.checkpoints, true) == 0 && sunk.size() == 2,
|
||||
"(a parked entry with no checkpoint is never superseded)");
|
||||
c.put(image({11,12,13}));
|
||||
const size_t before = sunk.size();
|
||||
check(c.clear() > 0 && sunk.size() == before, "#879's clear() hands nothing over");
|
||||
ConversationCache plain(one, 4);
|
||||
check(plain.put(image({1,2,3})) && plain.put(image({4,5,6})) && plain.size() == 1, "without a sink: evicted as before");
|
||||
}
|
||||
{
|
||||
// #4420: a batch slot's chain holds one checkpoint (its admission's): parking it must not drop (#342) the main session's copy of the
|
||||
// same conversation that also holds the root a sibling borrows from (an agent's system prompt and tools) - supersedes_richer()
|
||||
|
||||
@@ -137,8 +137,38 @@ int main() {
|
||||
for (auto& x : fs::directory_iterator(small.dir())) if (x.path().filename() != ".lock") ++files;
|
||||
check(files == 0, "nothing of a refused put is left on the disk");
|
||||
small.close();
|
||||
// #879: clear() drops every entry and its file, the tier stays open
|
||||
check(sp.put(img({30, 31}, 2), id, err) && sp.put(img({40, 41}, 2), id, err) && sp.size() == 2, "two more entries");
|
||||
const fs::path c0 = sp.entry(0).file, c1 = sp.entry(1).file;
|
||||
check(sp.clear() == 2 && sp.size() == 0 && sp.bytes() == 0 && !fs::exists(c0) && !fs::exists(c1), "clear() drops them and their files");
|
||||
check(sp.enabled() && fs::is_directory(sp.dir()) && sp.put(img({50}, 1), id, err) && sp.size() == 1, "the tier stays open after clear()");
|
||||
sp.close();
|
||||
}
|
||||
{ // the cost rule, with l620's measured speeds (a 236 MB image written in 120 ms, read back in 220 ms; 5 prompt tokens per ms)
|
||||
SpillCost c;
|
||||
check(c.reread_tokens_per_ms == 2.0 && c.round_trip_ms(2000000) == 4.0, "the defaults: 2 tokens per ms, 1 GB/s each way");
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
c.saw_prompt(8000, 1600.0);
|
||||
c.saw_write(236000000, 120.0);
|
||||
c.saw_read(236000000, 220.0);
|
||||
}
|
||||
check(c.reread_tokens_per_ms > 4.99 && c.reread_tokens_per_ms < 5.01, "the prompt speed converges to the measured one");
|
||||
check(c.round_trip_ms(236000000) > 339.0 && c.round_trip_ms(236000000) < 341.0, "the round trip converges to 120 + 220 ms");
|
||||
check(!c.worth(51, 236000000), "a 51-token conversation is not worth the disk (re-read about 10 ms of tokens)");
|
||||
check(!c.worth(2000, 264000000), "a 2K one is not either here (400 ms against about 1.25 x 380 ms)");
|
||||
check(c.worth(8000, 350000000), "an 8K one is (1600 ms against about 1.25 x 504 ms)");
|
||||
const SpillCost before = c;
|
||||
c.saw_prompt(100, 1.0); // fewer than kMinTokens: a request's fixed costs, not the prompt speed
|
||||
c.saw_prompt(4000, 0.0); // no time: ignored
|
||||
c.saw_write(0, 5.0); // nothing written: ignored
|
||||
c.saw_read(1000, -1.0); // a clock that went back: ignored
|
||||
check(c.reread_tokens_per_ms == before.reread_tokens_per_ms && c.write_bytes_per_ms == before.write_bytes_per_ms &&
|
||||
c.read_bytes_per_ms == before.read_bytes_per_ms, "samples that say nothing about the speeds are ignored");
|
||||
SpillCost slow; // a card that reads prompts at 1 token per ms: the same 2K conversation is worth spilling
|
||||
for (int i = 0; i < 40; ++i) { slow.saw_prompt(2000, 2000.0); slow.saw_write(264000000, 130.0); slow.saw_read(264000000, 250.0); }
|
||||
check(slow.worth(2000, 264000000), "on a slower card a 2K conversation is worth the disk");
|
||||
check(!c.worth(0, 1) && !slow.worth(0, 1), "an empty conversation never is");
|
||||
}
|
||||
fs::remove_all(root, ec);
|
||||
std::printf("conversation_spill_test: %d checks, %d failed\n", checks, failures);
|
||||
return failures == 0 ? 0 : 1;
|
||||
|
||||
+159
-7
@@ -47,6 +47,7 @@
|
||||
#include "strata/core/conversation_file.hpp"
|
||||
#include "strata/core/session_save_reclaim.hpp"
|
||||
#include "strata/core/conversation_memory.hpp"
|
||||
#include "strata/core/conversation_spill.hpp"
|
||||
#include "strata/core/park_pool_policy.hpp"
|
||||
#include "strata/core/auto_cache.hpp"
|
||||
#include "strata/core/ple_residency.hpp"
|
||||
@@ -1081,6 +1082,12 @@ struct Options {
|
||||
int64_t memory_limit_mib = -1;
|
||||
/// --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;
|
||||
/// --serve: the conversation cache's SSD tier (conversation_spill.hpp): parked conversations the RAM tier evicts or cannot
|
||||
/// hold are written there instead of dropped. The folder: --conversation-spill-dir, else `<data>/conversation-spill` of the
|
||||
/// data folder --pack sits in (`<data>/packs/<tag>`); none: off. --conversation-spill-mib N caps it (0 = off; unset = min(32
|
||||
/// GiB, a quarter of the free disk), never into --session-min-free-mib). Tester flags; the tier is on by default.
|
||||
std::string conversation_spill_dir;
|
||||
int64_t conversation_spill_mib = -1;
|
||||
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;
|
||||
@@ -3908,7 +3915,7 @@ int main(int argc, char** argv) {
|
||||
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" ||
|
||||
a == "--conversation-cache-park-gain" ||
|
||||
a == "--conversation-cache-park-gain" || a == "--conversation-spill-mib" ||
|
||||
a == "--session-min-free-mib" || a == "--memory-limit-mib") {
|
||||
const std::string value = next(a.c_str());
|
||||
int64_t number = 0;
|
||||
@@ -3925,9 +3932,11 @@ int main(int argc, char** argv) {
|
||||
else if (a == "--conversation-cache-min-tokens") o.conversation_cache_min_tokens = number;
|
||||
else if (a == "--conversation-cache-park-gain") o.conversation_cache_park_gain = number;
|
||||
else if (a == "--session-min-free-mib") o.session_min_free_mib = number;
|
||||
else if (a == "--conversation-spill-mib") o.conversation_spill_mib = number;
|
||||
else if (a == "--memory-limit-mib") o.memory_limit_mib = number;
|
||||
else o.conversation_cache_slots = (int) number;
|
||||
}
|
||||
else if (a == "--conversation-spill-dir") o.conversation_spill_dir = next("--conversation-spill-dir");
|
||||
else if (a == "--prompt-cache-tail") o.prompt_cache_tail = true;
|
||||
else if (a == "--prompt-cache-spread") o.prompt_cache_spread = true;
|
||||
else if (a == "--prompt-cache-every") o.prompt_cache_every = std::max(0LL, std::atoll(next("--prompt-cache-every")));
|
||||
@@ -13138,6 +13147,94 @@ int main(int argc, char** argv) {
|
||||
id.config = *config_fp;
|
||||
return true;
|
||||
};
|
||||
// The conversation cache's SSD tier (P2 of the cache admission design; conversation_spill.hpp). A parked conversation the
|
||||
// RAM tier evicts (its byte budget, its slot count, the RAM gate) or a captured one the RAM floor refuses is written as a
|
||||
// session file into this process's own folder under the data folder, instead of being dropped; a later request whose
|
||||
// prompt it serves best reads it back. Only on pressure: a park that fits in RAM never touches the disk. A single-stage
|
||||
// serve only (a layer split's stage images are not written). The file format, its identity and its read limits are the
|
||||
// session SAVE / RESTORE's.
|
||||
strata::core::ConversationSpill spill;
|
||||
std::optional<strata::core::SessionFileIdentity> spill_id;
|
||||
strata::core::SpillCost spill_cost; // only conversations whose re-read costs more than the disk round trip go there
|
||||
if (conversations.enabled() && stages.empty() && o.conversation_spill_mib != 0) {
|
||||
std::filesystem::path root;
|
||||
if (!o.conversation_spill_dir.empty()) {
|
||||
root = o.conversation_spill_dir;
|
||||
} else if (!o.pack.empty()) {
|
||||
std::filesystem::path p = std::filesystem::path(o.pack).lexically_normal();
|
||||
if (p.filename().empty()) p = p.parent_path(); // (a trailing separator)
|
||||
if (p.parent_path().filename() == "packs") root = p.parent_path().parent_path() / "conversation-spill";
|
||||
}
|
||||
std::string why;
|
||||
uint64_t cap = 0;
|
||||
if (root.empty()) {
|
||||
why = "no data folder (--pack is not under <data>/packs; --conversation-spill-dir sets one)";
|
||||
} else {
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(root, ec);
|
||||
const std::filesystem::space_info sp = std::filesystem::space(root, ec);
|
||||
const uint64_t floor = (uint64_t) std::max<int64_t>(o.session_min_free_mib, 0) << 20;
|
||||
if (ec) why = "its disk cannot be read (" + ec.message() + ")";
|
||||
else if (o.conversation_spill_mib > 0) cap = std::min<uint64_t>((uint64_t) o.conversation_spill_mib << 20,
|
||||
sp.available > floor ? sp.available - floor : 0);
|
||||
else cap = strata::core::spill_default_budget(sp.available, floor);
|
||||
if (!ec && cap < (uint64_t(64) << 20)) { why = "the disk is full"; cap = 0; }
|
||||
}
|
||||
size_t cleaned = 0;
|
||||
if (cap > 0 && !spill.open(root, cap, why, &cleaned)) cap = 0;
|
||||
if (cap > 0) {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: SSD tier in %s, up to %llu MiB%s\n", spill.dir().string().c_str(),
|
||||
(unsigned long long) (cap >> 20),
|
||||
cleaned ? (", " + std::to_string(cleaned) + " folder" + (cleaned == 1 ? "" : "s") +
|
||||
" of ended engines removed").c_str() : "");
|
||||
} else {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: no SSD tier: %s\n", why.c_str());
|
||||
}
|
||||
}
|
||||
// writes one parked conversation to the SSD tier; false: it stays where it was (or is dropped, for a victim). Never throws.
|
||||
auto spill_put = [&](const strata::core::SavedConversation& img, const char* why) -> bool {
|
||||
if (!spill.enabled() || !img.stage_images.empty()) return false;
|
||||
const int64_t tokens = (int64_t) img.live.ids.size();
|
||||
const uint64_t bytes = (uint64_t) img.bytes();
|
||||
if (!spill_cost.worth(tokens, bytes)) {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: not spilling %lld tokens (%s): reading them again "
|
||||
"(about %.0f ms here) costs less than the SSD round trip of %llu MiB (about %.0f ms)\n",
|
||||
(long long) tokens, why, spill_cost.reread_ms(tokens), (unsigned long long) (bytes >> 20),
|
||||
spill_cost.round_trip_ms(bytes));
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
std::string e;
|
||||
if (!spill_id) {
|
||||
strata::core::SessionFileIdentity id;
|
||||
if (!session_identity(id, e, {})) {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: no SSD tier from now on: the model cannot be "
|
||||
"fingerprinted (%s)\n", e.c_str());
|
||||
spill.close();
|
||||
return false;
|
||||
}
|
||||
spill_id = id;
|
||||
}
|
||||
const auto t0 = Clock::now();
|
||||
const bool ok = spill.put(img, *spill_id, e);
|
||||
if (ok) spill_cost.saw_write(spill.entry(spill.size() - 1).bytes,
|
||||
std::chrono::duration<double, std::milli>(Clock::now() - t0).count());
|
||||
if (ok)
|
||||
std::fprintf(stderr, "strata serve: conversation cache: spilled %zu tokens to the SSD tier (%s) in %.1f ms; "
|
||||
"on SSD %zu, %llu MiB\n", img.live.ids.size(), why,
|
||||
std::chrono::duration<double, std::milli>(Clock::now() - t0).count(), spill.size(),
|
||||
(unsigned long long) (spill.bytes() >> 20));
|
||||
else
|
||||
std::fprintf(stderr, "strata serve: conversation cache: could not spill %zu tokens to the SSD tier (%s): %s\n",
|
||||
img.live.ids.size(), why, e.c_str());
|
||||
return ok;
|
||||
} catch (...) {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: could not spill to the SSD tier (%s): out of memory\n", why);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
if (spill.enabled())
|
||||
conversations.set_evict_sink([&](strata::core::SavedConversation&& v) { spill_put(v, "evicted from RAM"); });
|
||||
// What a park captures: the main session's conversation (park_current) or an idle batch slot's (park_slot:
|
||||
// the reply it decoded, before an admission overwrites it). One budget / admission / snapshot path for both.
|
||||
struct ParkSource {
|
||||
@@ -13415,6 +13512,8 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
if (!strata::core::conversation_memory_admit(
|
||||
mem_after ? std::optional<uint64_t>(mem_after->available) : std::nullopt, 0, floor)) {
|
||||
// the SSD tier: the captured image is written out and freed instead of thrown away (P2, entry c)
|
||||
if (spill_put(image, "the RAM floor after the capture")) return true;
|
||||
std::fprintf(stderr, "strata serve: conversation cache: skip parking (RAM floor after capture: %lld MiB needed, %s)\n",
|
||||
(long long) o.conversation_cache_min_free_mib, ram_note(mem_after).c_str());
|
||||
return true;
|
||||
@@ -17037,7 +17136,7 @@ int main(int argc, char** argv) {
|
||||
live_ok = true;
|
||||
if (nan_flush) { // #879: the file replaced the poisoned session; the parked ones go (no slots here)
|
||||
nan_flush = false;
|
||||
const size_t n_parked = conversations.clear();
|
||||
const size_t n_parked = conversations.clear() + spill.clear(); // (the SSD tier's too)
|
||||
std::fprintf(stderr, "strata serve: after non-finite logits (#879): the restored session "
|
||||
"replaces the live one; dropped %zu parked conversation%s\n", n_parked,
|
||||
n_parked == 1 ? "" : "s");
|
||||
@@ -17351,6 +17450,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
cur = ids;
|
||||
const Clock::time_point r0 = Clock::now();
|
||||
Clock::time_point read_t0 = r0; // where the prompt pass itself starts (after a park and a restore): the SSD tier's cost rule
|
||||
tr("r0");
|
||||
strata::core::conversation_checkpoint_join(nullptr); // STRATA_CKPT_ASYNC: the last save's copy has landed
|
||||
const int64_t streamed0 = sp.chain_experts_streamed(); // the DONE line's experts_streamed= is the difference
|
||||
@@ -17448,7 +17548,7 @@ int main(int argc, char** argv) {
|
||||
live_ok = false;
|
||||
checks.clear();
|
||||
tail_ckpt_len = -1;
|
||||
const size_t n_parked = conversations.clear();
|
||||
const size_t n_parked = conversations.clear() + spill.clear(); // (the SSD tier's too)
|
||||
size_t n_slots = 0, n_running = 0;
|
||||
for (BSlot& sl : bs)
|
||||
if (sl.active) { // a running slot finishes its reply, but is not kept as a cache after it
|
||||
@@ -17576,9 +17676,58 @@ int main(int argc, char** argv) {
|
||||
// own next turn (ConversationCache::borrows). Everything else is taken whole, as before.
|
||||
// (a layer split restores whole stage images only, so it never borrows; a batch slot that holds a longer
|
||||
// start of this prompt wins as before)
|
||||
bool borrow = stages.empty() && parked.tokens > std::max(resume, slot_tokens) &&
|
||||
// The SSD tier (P2): a spilled conversation that gives this prompt a longer start than the session, a batch slot and
|
||||
// every RAM entry is read back (the session RESTORE's limits and RAM check); one that does not read back is dropped
|
||||
// and the RAM tier's choice stands. Never borrowed from (it comes back whole).
|
||||
bool from_ssd = false;
|
||||
strata::core::ConversationCache::Match taken = parked; // the match whose image is restored: RAM's, or the SSD tier's
|
||||
if (spill.enabled() && spill.size() > 0 && stages.empty()) {
|
||||
const auto sp = spill.best(ids, req_imgs, want_cvec);
|
||||
if (sp.tokens > std::max({resume, slot_tokens, parked.tokens}) && spill_id) {
|
||||
const auto t0 = Clock::now();
|
||||
const size_t sp_tokens = spill.entry(sp.index).live.ids.size();
|
||||
const uint64_t sp_bytes = spill.entry(sp.index).bytes;
|
||||
strata::core::SavedConversation image;
|
||||
std::string why;
|
||||
try {
|
||||
strata::core::SessionReadLimits limits;
|
||||
const uint64_t floor = (uint64_t) o.conversation_cache_min_free_mib << 20;
|
||||
pools_budget.shrink((size_t) -1);
|
||||
limits.admit = [floor, &o](uint64_t need, std::string& w) {
|
||||
const auto mem = strata::core::sample_host_memory();
|
||||
if (strata::core::conversation_memory_admit(mem ? std::optional<uint64_t>(mem->available) : std::nullopt,
|
||||
need, floor)) return true;
|
||||
w = "not enough RAM to read it (" + std::to_string(need >> 20) + " MiB plus a floor of " +
|
||||
std::to_string((long long) o.conversation_cache_min_free_mib) + " MiB needed, " + ram_note(mem) + ")";
|
||||
return false;
|
||||
};
|
||||
if (strata::core::conversation_session_read_limits(limits, ss, g, use_mtp ? &mtp.kv_state() : nullptr,
|
||||
(uint64_t) o.max_context, (uint64_t) std::max(o.prompt_cache, 1), why) &&
|
||||
spill.take(sp.index, *spill_id, limits, image, why))
|
||||
from_ssd = true;
|
||||
else if (sp.index < spill.size() && spill.entry(sp.index).serial == sp.serial)
|
||||
spill.drop(sp.index); // (limits that cannot be formed: the entry cannot be read here either)
|
||||
} catch (const std::bad_alloc&) {
|
||||
why = "not enough RAM to read it";
|
||||
if (sp.index < spill.size() && spill.entry(sp.index).serial == sp.serial) spill.drop(sp.index);
|
||||
}
|
||||
if (from_ssd) {
|
||||
spill_cost.saw_read(sp_bytes, std::chrono::duration<double, std::milli>(Clock::now() - t0).count());
|
||||
std::fprintf(stderr, "strata serve: conversation cache: read %zu tokens back from the SSD tier in %.1f ms "
|
||||
"(this prompt starts with %lld of them); on SSD %zu, %llu MiB\n", sp_tokens,
|
||||
std::chrono::duration<double, std::milli>(Clock::now() - t0).count(), (long long) sp.tokens,
|
||||
spill.size(), (unsigned long long) (spill.bytes() >> 20));
|
||||
incoming.emplace(std::move(image));
|
||||
taken = sp; // (the restore below resumes at the SSD entry's prefix, not at the RAM tier's)
|
||||
} else {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: could not read %zu tokens back from the SSD tier "
|
||||
"(%s); the prompt is read again\n", sp_tokens, why.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
bool borrow = !from_ssd && stages.empty() && parked.tokens > std::max(resume, slot_tokens) &&
|
||||
conversations.borrows(parked);
|
||||
if (parked.tokens > std::max(resume, slot_tokens) && !borrow)
|
||||
if (!from_ssd && parked.tokens > std::max(resume, slot_tokens) && !borrow)
|
||||
incoming.emplace(conversations.take(parked.index));
|
||||
if (incoming || borrow) slot_source = -1;
|
||||
// Reject the entire image before parking/overwriting the outgoing
|
||||
@@ -17909,8 +18058,8 @@ int main(int argc, char** argv) {
|
||||
if (!strata::core::conversation_checkpoints_merge(std::move(cs), checks)) checks.clear();
|
||||
}
|
||||
cvec_cached = incoming->cvec;
|
||||
resume = parked.tokens;
|
||||
from_live = parked.live;
|
||||
resume = taken.tokens; // (parked.tokens unless the image came from the SSD tier)
|
||||
from_live = taken.live;
|
||||
if (std::getenv("STRATA_SNAPSHOT_FULL_CAPTURE") == nullptr) {
|
||||
std::vector<std::vector<strata::core::ConversationKv>> stage_kv; // every stage's, for its next park
|
||||
for (auto& si : incoming->stage_images) stage_kv.push_back(std::move(si.kv));
|
||||
@@ -18086,6 +18235,7 @@ int main(int argc, char** argv) {
|
||||
pp_reached = read_from;
|
||||
pp_exact = true; // a restore, a checkpoint mount or a zeroed session: the state is exactly at read_from
|
||||
pp_t0 = r0;
|
||||
read_t0 = Clock::now();
|
||||
pp_check_anchor = resume;
|
||||
pp_next_check = reread_to > 0 || !req_ckpt ? INT64_MAX : resume + o.prompt_cache_every; // ckpt=0: none
|
||||
pp_tail_saved = reread_to > 0 || !req_ckpt; // ckpt=0 (#861): no tail checkpoint either
|
||||
@@ -19480,6 +19630,7 @@ int main(int argc, char** argv) {
|
||||
std::fprintf(stderr, "strata batch: the prompt was read in %lld parts, the slots decoding %.0f ms between "
|
||||
"them\n", (long long) il_parts + 1, il_ms);
|
||||
const double prompt_ms = std::chrono::duration<double, std::milli>(Clock::now() - r0).count();
|
||||
const double read_pass_ms = std::chrono::duration<double, std::milli>(Clock::now() - read_t0).count();
|
||||
std::printf("REUSED %lld\n", (long long) resume); // the prompt is read; the first window comes next
|
||||
std::fflush(stdout);
|
||||
// the verify windows: the first holds the last prompt token alone (STRATA_ADMIT_MERGE_FIRST: the prompt
|
||||
@@ -21211,6 +21362,7 @@ int main(int argc, char** argv) {
|
||||
// cancel stopped it part-way (a cancelled request used to be logged and counted as having read them all)
|
||||
const int64_t fresh = n - resume;
|
||||
const int64_t read_n = cancelled ? std::clamp<int64_t>(pp_reached - resume, 0, fresh) : fresh;
|
||||
if (!cancelled) spill_cost.saw_prompt(read_n, read_pass_ms); // (the SSD tier's cost rule learns this engine's prompt speed)
|
||||
// DONE <generated> <prompt> <prompt ms> <decode ms> <finish> <drafts accepted> <drafts offered> <reused> [hits] [lookups]
|
||||
// [RAM blobs] [file blobs] [file MB] (CS-T tiers; appended, so an older server reads the rest)
|
||||
// [prompt tokens read] (#471: fewer than <prompt> - <reused> when a cancel stopped the read)
|
||||
|
||||
Reference in new issue
Block a user