Archived
core/mem, core/sched: a prefix hit on a model whose KV groups have different block sizes, and a hybrid's window in the checkpoint slot
DeepSeek-V4.1 declares its compressed attention at one KV row per r tokens: kv_index_r2 and
kv_compressed_r2 at 2-token blocks, kv_index_r1 and kv_compressed_r1 at 1-token blocks, beside a
16-token sliding window (kv_window), a linear compressor state (kv_comp_tail_r2) and a conv window
(kv_engram_tokens). On that model no request ever reused a token, for two reasons:
- PrefixCache::configure took the first full group's block size as its granularity and left every
paged group with another block size out ("block 1 != cache granularity 2: not cached").
- KVManager put a trunk's window in the checkpoint slot only on a model without linear state, and
the scheduler refuses a hit that leaves a trunk group uncovered. With linear state beside the
window, every hit was zeroed (HitCut::Window).
Either alone leaves the hit at zero. On an agent's growing conversation that is the whole context
prefilled again every turn.
The cache now holds every paged group but a windowed one. An entry covers the lcm of their block
sizes and holds lcm / B blocks of a group with B-token blocks (PrefixCache::entry_groups,
blocks_per_entry). A lookup hands each group its own blocks for adopt(); insert, eviction, trim,
rebind, restore_room, release_session and unshared_tokens count per block; trim() and trim_for()
count entries. The chunk quantum is already a multiple of every paged group's block, so
checkpoints and hits stay on the entry grid. With equal block sizes an entry is one block a group,
as before.
A trunk's window rides in the checkpoint slot whether or not linear state is beside it; the slot is
the state, then the window pages, the layout KVManager::checkpoint_copy already walked. The
scheduler lets a hit stand on a windowed trunk group when it resumes at a window snapshot
(win_hit no longer needs a model without linear state); any other trunk group the cache does not
hold refuses the hit, as before. The reserve's trim asks for entries, from the group furthest
short. The idle tiers move an entry as its blocks: TierExec and the slot layout take
entry_groups(), where a group appears once for each block it holds in an entry, and the VRAM
estimate counts the same.
No plugin on main has a window beside linear state or paged groups of different block sizes, so
every model here keeps its cache and slot as they were. DeepSeek-V4.1 (#266) has both; its slot
grows by the window's pages (10 blocks of kv_window, 6.25 MiB a rank at TP4).
Tests: mem_test -- an entry at the lcm over block sizes 2/1, 2/4 and 16/2 (lookup blocks, adopt,
refcounts, eviction and trim in entries, restore_room, unshared_tokens), a publish short of one
group's blocks refused and unwound, checkpoints on the entry grid (and the binding's key), an
interval off the grid refused, a hybrid with a window and mixed blocks, an entry over mixed block
sizes through the host tier byte for byte (one to three ranks, replicated beside zero), and a
checkpoint carrying linear state and the window together; the window-snapshot rule's test now
expects the window beside linear state. sched_test -- a hit on a hybrid whose slot carries the
window resumes at the snapshot, a trunk group neither cached nor in the slot refuses the hit, a
hit over mixed block sizes adopts each group's own blocks, and the reserve trim asks for entries.
This commit is contained in:
1 parent
64c3dc9893
commit
f43929ccb8
14 files changed
+672
-142
No files matched your search
+40
-18
@@ -10,6 +10,7 @@
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <numeric>
|
||||
|
||||
namespace rad {
|
||||
|
||||
@@ -97,9 +98,9 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
|
||||
if (kv->block_tokens((int32_t)i) <= 0) continue;
|
||||
++n_paged;
|
||||
const auto& cg = pc->cached_groups();
|
||||
/* Snapshots are taken only where the trunk has a window to snapshot too
|
||||
* (ChunkGeometry::window_ckpt); without them no drafter window rides in one. */
|
||||
if (cfg.prefix_cache && geo_.window_ckpt && kv->window_snapshot((int32_t)i) &&
|
||||
/* Snapshots are taken only on a checkpoint grid (ChunkGeometry::checkpoints()), the
|
||||
* window's alone or beside linear state; without one no window rides in a slot. */
|
||||
if (cfg.prefix_cache && geo_.checkpoints() && kv->window_snapshot((int32_t)i) &&
|
||||
std::find(cg.begin(), cg.end(), (int32_t)i) == cg.end())
|
||||
++n_snap;
|
||||
}
|
||||
@@ -110,19 +111,27 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
|
||||
}
|
||||
/* The trunk's share: a paged group a trunk layer reads (a bound layer below the trunk's layer
|
||||
* count; a drafter borrows from base_layer up) that the cache does not index. Recomputing the
|
||||
* tail cannot repair that hole; see scheduler.h. */
|
||||
* tail cannot repair that hole; see scheduler.h. A windowed group is restored by a hit that
|
||||
* resumes at a window snapshot (a checkpoint slot carries the trunk's window, with or without
|
||||
* linear state beside it); any other such group refuses every hit. */
|
||||
hit_holes_trunk_ = false;
|
||||
hit_needs_window_ = false;
|
||||
for (size_t i = 0; i < prog.kv_groups.size(); ++i) {
|
||||
if (kv->block_tokens((int32_t)i) <= 0) continue;
|
||||
bool trunk = false;
|
||||
for (int l : prog.kv_groups[i].layers) if (l < prog.meta.n_layers) { trunk = true; break; }
|
||||
if (!trunk) continue;
|
||||
const std::vector<int32_t>& cg = pc->cached_groups();
|
||||
if (std::find(cg.begin(), cg.end(), (int32_t)i) == cg.end()) { hit_holes_trunk_ = true; break; }
|
||||
if (std::find(cg.begin(), cg.end(), (int32_t)i) != cg.end()) continue;
|
||||
if (prog.kv_groups[i].decl.kind == RAD_KV_WINDOW) hit_needs_window_ = true;
|
||||
else hit_holes_trunk_ = true;
|
||||
}
|
||||
if (hit_holes_trunk_ && cfg.prefix_cache)
|
||||
RAD_INFO("prefix cache: a trunk layer's paged group is neither prefix-cached nor carried "
|
||||
"by a checkpoint slot -- a hit re-prefills");
|
||||
else if (hit_needs_window_ && cfg.prefix_cache)
|
||||
RAD_INFO("prefix cache: a trunk layer's windowed group is not prefix-cached -- a hit "
|
||||
"resumes from a window snapshot (a checkpoint slot) or re-prefills");
|
||||
"resumes from a window snapshot (a checkpoint slot)");
|
||||
if (cfg.prefill_interleave > 0)
|
||||
RAD_INFO("scheduler: --prefill-interleave %lld: after a prefill chunk that leaves its "
|
||||
"prompt unfinished while other sequences decode, the next %lld steps carry "
|
||||
@@ -470,14 +479,26 @@ void Scheduler::trim_prefix_cache() {
|
||||
if (!pc_ || !cfg_.prefix_cache) return;
|
||||
const std::vector<int32_t>& cg = pc_->cached_groups();
|
||||
if (cg.empty()) return;
|
||||
const int32_t g = cg[0];
|
||||
const int64_t total = kv_->total_blocks(g);
|
||||
if (total <= 0) return;
|
||||
const int64_t reserve = total / 4; /* a quarter of the pool stays free */
|
||||
/* Free against the carve, not free_blocks(): on an elastic pool free_blocks() is bounded by
|
||||
* the live mark, so a pool that returned its unused top would read as nearly full and the
|
||||
* trim would drain the cache for room the pool can grow back into. */
|
||||
const int64_t short_by = reserve - (total - kv_->held_blocks(g));
|
||||
/* An entry covers the lcm of the cached groups' blocks and holds that over a group's block of
|
||||
* its blocks (PrefixCache::blocks_per_entry); the trim asks for entries, as many as the group
|
||||
* furthest short needs. */
|
||||
int64_t entry = 0;
|
||||
for (int32_t g : cg) {
|
||||
const int64_t bs = kvgeom_.g[(size_t)g].block_size;
|
||||
if (bs > 0) entry = entry > 0 ? std::lcm(entry, bs) : bs;
|
||||
}
|
||||
int64_t short_by = 0;
|
||||
for (int32_t g : cg) {
|
||||
const int64_t total = kv_->total_blocks(g), bs = kvgeom_.g[(size_t)g].block_size;
|
||||
if (total <= 0 || bs <= 0) continue;
|
||||
const int64_t reserve = total / 4; /* a quarter of the pool stays free */
|
||||
/* Free against the carve, not free_blocks(): on an elastic pool free_blocks() is bounded
|
||||
* by the live mark, so a pool that returned its unused top would read as nearly full and
|
||||
* the trim would drain the cache for room the pool can grow back into. */
|
||||
const int64_t blocks = reserve - (total - kv_->held_blocks(g));
|
||||
const int64_t per = std::max<int64_t>(entry / bs, 1);
|
||||
if (blocks > 0) short_by = std::max(short_by, (blocks + per - 1) / per);
|
||||
}
|
||||
if (short_by > 0) (void)pc_->trim(short_by);
|
||||
}
|
||||
|
||||
@@ -1158,9 +1179,10 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
|
||||
(unsigned long long)r.id, (long long)h.n_attn_tokens, (long long)reuse);
|
||||
}
|
||||
/* A window snapshot covers the trunk's windowed groups at the checkpoint only. The lookup
|
||||
* clamped the hit to it (a window counts as state there), so a hit with a slot is exactly
|
||||
* at a snapshot; one without is no hit. */
|
||||
const bool win_hit = geo_.window_ckpt && kv_->has_window_snapshot() && reuse > 0 &&
|
||||
* clamped the hit to it (a window counts as state there, and linear state beside it
|
||||
* resumes there too), so a hit with a slot is exactly at a snapshot; one without is no
|
||||
* hit. */
|
||||
const bool win_hit = geo_.checkpoints() && kv_->has_window_snapshot() && reuse > 0 &&
|
||||
h.checkpoint_slot >= 0 && reuse == h.n_linear_tokens;
|
||||
|
||||
/* Windowed-group clamp: the trunk's own KV includes a paged group the cache does not
|
||||
@@ -1173,7 +1195,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
|
||||
*
|
||||
* A drafter's own group (bound at base_layer and above) is not this clamp's business:
|
||||
* draft refill caps the hit so the recompute covers the drafter's window. */
|
||||
if (reuse > 0 && hit_holes_trunk_ && !win_hit) {
|
||||
if (reuse > 0 && (hit_holes_trunk_ || (hit_needs_window_ && !win_hit))) {
|
||||
reuse = 0;
|
||||
s.hit_cut = SchedReq::HitCut::Window;
|
||||
RAD_DEBUG("admit %llu: prefix hit %lld zeroed -- a trunk paged group is not "
|
||||
|
||||
Reference in new issue
Block a user