sched, mem: the per-request switch reaches the device, and the cache keeps the modes apart

`projection_on_rows` expands each request's choice across its tokens once per step, into the int32
mask hc_project reads; rows outside every sequence are zeroed first, so the reused arena region can
never carry a previous step's flag.

The prefix cache keys a projected request's blocks on the vector itself, through the same extension
point the runtime LoRA adapter uses: one content key spanning the whole sequence, pushed into
`media_keys` beside the adapter's, so every block overlaps it. Blocks and linear-state checkpoints
(whose keys are the same chained hash) therefore never cross modes; an unprojected request, or a
deployment with no vector, adds no span and hashes exactly as before, and an adapter and a vector
compose.
This commit is contained in:
C_Tamas authored and StillDeadcode committed 2026-10-21 09:26:17 +02:00
1 parent 809257e393
commit 66c6d71254
3 files changed
+57 -7

No files matched your search

+24 -5
View File
@@ -282,7 +282,8 @@ struct KVBinding final : IKVManager {
/* Content keys for a request's media runs. Placeholder ids are identical for every picture, so
* the prefix cache also hashes each run's item content key (core/mm, over the processed patches)
* and segment: the same picture at the same place hits, two pictures never alias. */
inline void media_keys(const Request& r, std::vector<MMContentKey>* out) {
inline void media_keys(const Request& r, std::vector<MMContentKey>* out,
uint64_t control_vector_salt = 0) {
out->clear();
/* The runtime LoRA adapter is part of every block's key: KV written under an adapter is not
* the base's. It enters the chain as one span over the whole sequence, keyed by the adapter's
@@ -295,6 +296,18 @@ inline void media_keys(const Request& r, std::vector<MMContentKey>* out) {
k.hash = (r.lora_uid * 0x9e3779b97f4a7c15ull) ^ 0x4c6f5241ull; /* "LoRA" */
out->push_back(k);
}
/* A control vector is part of every block's key for the same reason: a projected step's hidden
* states are not the stock model's, so its KV is not the stock model's either. The vector's own
* identity rides in as one span over the whole sequence, so every block overlaps it, and a
* request that runs without the vector (or a deployment with none) adds no span and hashes
* exactly as it did. An adapter and a vector are independent spans and compose. */
if (control_vector_salt != 0 && r.sp.use_control_vector) {
MMContentKey k;
k.tok_begin = 0;
k.tok_end = INT32_MAX;
k.hash = control_vector_salt ^ 0x43455643ull; /* "CVEC" */
out->push_back(k);
}
if (!r.media) return;
const mm::PromptMedia& pm = *r.media;
for (const mm::RopeLayout::Run& run : pm.rope.runs) {
@@ -311,13 +324,19 @@ inline void media_keys(const Request& r, std::vector<MMContentKey>* out) {
template <class PC>
struct PCBinding final : IPrefixCache {
PC* m;
/* The control vector's identity (Engine::cvec_salt_for), or 0 with none loaded. A request that
* runs with it on keys its blocks and its linear-state snapshots under a chain that includes it,
* so its KV is never handed to a request that runs without it: the hidden states differ, and
* the cache stores hidden states' consequences. Per request, not per cache: two sequences of one
* step may differ. */
uint64_t salt_id = 0;
/* Scratch for a request's media keys, reused so a text request's lookup allocates nothing. */
mutable std::vector<MMContentKey> mm_;
explicit PCBinding(PC* p) : m(p) {}
CacheHit lookup(const Request& r, std::vector<std::vector<int32_t>>* out,
const HitCap* cap) const override {
media_keys(r, &mm_);
media_keys(r, &mm_, salt_id);
/* r.id: the admission takes its conversation's snapshots over. */
return m->lookup(r.prompt, mm_, out, cap, r.id);
}
@@ -329,7 +348,7 @@ struct PCBinding final : IPrefixCache {
}
void begin_step() override { m->begin_step(); }
void hold(const Request& r) override {
media_keys(r, &mm_);
media_keys(r, &mm_, salt_id);
m->hold(r.id, r.prompt, mm_);
}
void release(const Request& r) override { m->release(r.id); }
@@ -345,7 +364,7 @@ struct PCBinding final : IPrefixCache {
toks.insert(toks.end(), r.output.begin(), r.output.end());
if (n_tokens > (int64_t)toks.size()) n_tokens = (int64_t)toks.size();
/* r.id is the session, the same id reserve_checkpoint keys its snapshots by. */
media_keys(r, &mm_);
media_keys(r, &mm_, salt_id);
return m->insert(r.id, toks, mm_, blocks, n_tokens, r.session_label);
}
/* Installed by the engine when the idle tiers are on: lands every pending host copy now and
@@ -409,7 +428,7 @@ private:
/* The chain reserve_checkpoint and commit_checkpoint both key on; shared so they cannot
* drift apart. */
BlockHash prefix_hash(const Request& r, int64_t pos) const {
media_keys(r, &mm_);
media_keys(r, &mm_, salt_id);
const int64_t bs = m->block_size();
BlockHash h{};
if (bs <= 0) return h;