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

+2 -2
View File
@@ -1476,9 +1476,9 @@ int Engine::serve(std::unique_ptr<StallWatch> watch) {
const uint64_t t0 = rad_mono_ns();
PrefixCache& pc = ranks_[0]->prefix;
std::vector<MMContentKey> mk;
media_keys(req, &mk);
media_keys(req, &mk, cvec_salt_for(req.sp.use_control_vector));
const std::vector<BlockHash> want =
pc.restorable_from(req.prompt, mk, cvec_salt_for(req.sp.use_control_vector));
pc.restorable_from(req.prompt, mk);
if (want.empty()) return;
/* The whole chain in one restore. With 4-token blocks a long conversation is tens of
* thousands of entries; batched, it is one launch per (group, layer) per staging batch
+31
View File
@@ -2,6 +2,8 @@
*/
#include "derive.h"
#include "rad_core.h"
#include <cstring>
namespace rad {
@@ -81,6 +83,32 @@ static int d_state_chunk_seq(const DeriveInput& in, int32_t* out, int64_t cap, i
return chunk_walk<true>(in, out, cap, n_out);
}
/* ------------------------------------------------------- projection_on_rows */
/* [n_tok] -- whether each token's residual stream takes the control vector this step.
*
* The switch itself is a per-request choice (SamplingParams::use_control_vector) and the step
* kernel reads one row's flag, so the mask is expanded here once per step from the plan rather
* than searched per layer: an architecture issues one projection op after every block's write, and
* a per-layer search over the sequence bounds would repeat on every one of them.
*
* All zeros is the ordinary model, byte for byte: the op skips a zero row without writing, so a
* request that did not ask for the vector computes what it computes with no vector loaded. */
static int d_projection_on_rows(const DeriveInput& in, int32_t* out, int64_t cap, int64_t* n_out) {
if (in.n_tok > cap) return RAD_E_FULL;
/* Zero first. The ranges below cover every row a step carries today, but the region is reused
* across steps and a row outside them would otherwise keep the previous step's flag -- which
* would project a request that did not ask for it. The fill is a memset of a few KiB. */
std::memset(out, 0, (size_t) in.n_tok * sizeof(int32_t));
for (int64_t i = 0; i < in.n_seq; ++i) {
const StepEntry& e = in.plan->e[(size_t) i];
const int32_t on = (e.req && e.req->sp.use_control_vector) ? 1 : 0;
const int64_t b = in.cu_seqlens[i], end = in.cu_seqlens[i + 1];
for (int64_t k = b; k < end && k < in.n_tok; ++k) out[k] = on;
}
*n_out = in.n_tok;
return RAD_OK;
}
/* ------------------------------------------------------------------ the table */
static const DeriveRow g_rows[] = {
{ "seq_start_index", d_seq_start_index,
@@ -91,6 +119,9 @@ static const DeriveRow g_rows[] = {
"[n_chunks+1] token indices bounding each linear-state chunk; never crosses a sequence" },
{ "state_chunk_seq", d_state_chunk_seq,
"[n_chunks] the sequence each chunk belongs to" },
{ "projection_on_rows", d_projection_on_rows,
"[n_tok] 1 where this token's request applies the loaded control vector, 0 for the stock "
"model" },
};
int derive_index(const char* name) {
+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;