mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 07:18:50 +00:00
The engine's GEN line takes pin=N: the first N prompt tokens are a shared read-only prefix (a long document many
short questions follow). The prompt is read in two parts at N and the checkpoint there is pinned: retention never
evicts it, a parked conversation holding it stays parked, the session file keeps it, and a later request that resumes
from it does not park the branch it leaves, so N queries keep one prefix. One pin at a time. Without pin= nothing
changes.
The server takes "strata_prefix" in a chat, messages or responses request: {"messages": K}, {"message": I,
"chars": N} or {"tokens": N}, resolved to the longest common start of the prompt's ids. A prefix that cannot be
marked is said in the log and ignored; a malformed field is a 400.
Tests: conv_cache_test (pinned eviction), conversation_cache_test (a pinned parked conversation is not evicted),
conversation_file_test (the pinned checkpoint is the one saved), serve.test_server.SharedPrefix.
81 lines
4.6 KiB
C++
81 lines
4.6 KiB
C++
// include/strata/program/conv_cache.hpp - the conversation cache's retention policy: which checkpoint
|
|
// leaves when the cache is over its slot budget.
|
|
//
|
|
// The cache holds conversation checkpoints: snapshots of the running state (the GDN recurrence, the PLE
|
|
// history, the QSA indexer tails - `ConvCheckpoint` in generate.cpp) that a request resumes its prompt
|
|
// from instead of reading those tokens again. The request path keeps only the checkpoints whose tokens
|
|
// are a prefix of the current prompt - any other checkpoint's positional cells have been overwritten,
|
|
// because the KV cache is one arena that holds one branch of history at a time - so at any moment the
|
|
// retained checkpoints are a CHAIN: sorted by length, each a prefix of the next. That is a radix cache's
|
|
// tree collapsed onto the one branch of history the session can hold.
|
|
//
|
|
// The chain's root is therefore the deepest point every request so far has shared - in practice the end
|
|
// of the system prompt, which every new chat of the same client mounts through - and it is exactly the
|
|
// node a radix cache keeps alive while its leaves rotate. First-in-first-out kept it only by accident of
|
|
// being first: after `prompt_cache` newer checkpoints it was gone, and the next chat read the whole
|
|
// prefix again at prefill speed. So:
|
|
//
|
|
// * the root is PINNED: dropping it frees one slot's ~118 MB and costs every future conversation that
|
|
// shares the prefix a full re-read of it (a 30K system prompt is ~30 s at ~1,000 tok/s);
|
|
// * every other slot rotates by least recent use. A checkpoint's stamp advances when it is created,
|
|
// when a new checkpoint lands on its length, and when a request mounts through it.
|
|
//
|
|
// With a budget below two slots there is no room to keep root and leaf apart, so the pin switches off and
|
|
// the oldest checkpoint leaves - the previous behaviour, one slot = the newest point only.
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
namespace strata::program::conv_cache {
|
|
|
|
/// The index in `stamps` of the chain item to drop once the chain holds more than `cap` items. `stamps`
|
|
/// are the items' last-use stamps; the caller owns the chain and erases the returned index. Pure and
|
|
/// deterministic so conv_cache_test.cpp can walk the scenarios by hand.
|
|
inline size_t eviction_victim(const uint64_t* stamps, size_t n, int64_t cap) {
|
|
if (cap < 2 || n < 2) return 0; // no room for root and leaf: the pin is off, the oldest leaves
|
|
size_t v = 1; // the root (0) is pinned; least recent use among the rest
|
|
for (size_t i = 2; i < n; ++i)
|
|
if (stamps[i] < stamps[v]) v = i;
|
|
return v;
|
|
}
|
|
|
|
/// --prompt-cache-tail: the extra checkpoint near the prompt's end (`tail[i]`) is the first to go - it serves
|
|
/// only a branch of the last request - unless it is the newest item (the one just saved: `n > 1` and the newest
|
|
/// stamp). With no such item this is the plain policy. Never the root.
|
|
inline size_t eviction_victim(const uint64_t* stamps, size_t n, int64_t cap, const bool* tail) {
|
|
if (tail != nullptr && n >= 2 && cap >= 2) {
|
|
size_t newest = 0;
|
|
for (size_t i = 1; i < n; ++i)
|
|
if (stamps[i] > stamps[newest]) newest = i;
|
|
for (size_t i = 1; i < n; ++i)
|
|
if (tail[i] && i != newest) return i;
|
|
}
|
|
return eviction_victim(stamps, n, cap);
|
|
}
|
|
|
|
/// The pin=N shared prefixes (`pinned[i]`): they are never the victim, whatever their stamp - the queries that branch
|
|
/// from one keep coming back to it, and a least-recently-used rule would drop it while they still do. The root and
|
|
/// the tail rule are as above. When every other item is pinned too, the policy falls back to the oldest pinned one:
|
|
/// the cache still keeps to its `cap` (the caller limits how many pins it takes, so this is a backstop).
|
|
inline size_t eviction_victim(const uint64_t* stamps, size_t n, int64_t cap, const bool* tail, const bool* pinned) {
|
|
if (pinned == nullptr) return eviction_victim(stamps, n, cap, tail);
|
|
if (cap < 2 || n < 2) return 0;
|
|
size_t newest = 0;
|
|
for (size_t i = 1; i < n; ++i)
|
|
if (stamps[i] > stamps[newest]) newest = i;
|
|
if (tail != nullptr)
|
|
for (size_t i = 1; i < n; ++i)
|
|
if (tail[i] && !pinned[i] && i != newest) return i;
|
|
size_t v = n; // the least recently used among the unpinned non-root items
|
|
for (size_t i = 1; i < n; ++i)
|
|
if (!pinned[i] && (v == n || stamps[i] < stamps[v])) v = i;
|
|
if (v != n) return v;
|
|
v = 1; // all pinned: the oldest of them (never the root, index 0)
|
|
for (size_t i = 2; i < n; ++i)
|
|
if (stamps[i] < stamps[v]) v = i;
|
|
return v;
|
|
}
|
|
|
|
} // namespace strata::program::conv_cache
|