engine: the KV plan split, the host mirror, and --kv-resident bring-up

This commit is contained in:
C_Tamas committed 2026-10-24 19:19:19 +07:00
1 parent cce687bf2c
commit a9e627b7ca
44 files changed
+3118 -51

No files matched your search

+64 -2
View File
@@ -474,6 +474,7 @@ void Scheduler::trim_prefix_cache() {
void Scheduler::close_request(SchedReq& s, ReqState st, const char* reason, bool defer_free,
bool publish, int status, const char* why) {
++step_finished_;
/* Publish before freeing: this is the only moment the whole prefix is both computed and still
* owned. insert() takes its own KV references, so the blocks survive the free below. Cancelled
* and failed requests are published too; their prompts were computed, and a client that hangs
@@ -554,6 +555,47 @@ void Scheduler::reserve_ckpt(StepEntry& e, const Request& r, int64_t pos, int32_
++m_.checkpoints_written;
}
int Scheduler::fail_prefilling(const char* why) {
std::lock_guard<std::mutex> lk(mu_);
/* Stable for the client: the sink keeps this pointer past the call (Sink::why_). */
stream_why_ = why ? why : "kv streaming overflow";
why = stream_why_.c_str();
int n = 0;
/* The step just committed named these; when it produced no token row (a middle prefill chunk
* issues none) no list was filled, and every request still laying down its prompt is the set
* whose next chunk would read the failed table. */
const bool by_step = !last_prefill_ids_.empty();
auto fail = [&](SchedReq& s) {
if (s.req->state != ReqState::Running && s.req->state != ReqState::Preempted &&
s.req->state != ReqState::Waiting)
return;
/* The next step may already be issued (a decode row of a request whose prompt just
* finished), so its blocks stay until that step's commit frees them -- the same rule
* cancel() uses for a request in flight. */
const bool in_flight = in_step(s) || block_holds(s.req->id);
/* RAD_E_FULL, not RAD_E_STATE: the ring is a pool that ran out, and RAD_E_STATE
* stringifies as "called in the wrong phase", which would follow the real message. */
close_request(s, ReqState::Failed, "kv_stream_overflow", in_flight,
/*publish=*/false, RAD_E_FULL, why);
++n;
};
if (by_step) {
for (uint64_t id : last_prefill_ids_) {
auto it = by_id_.find(id);
if (it != by_id_.end()) fail(*reqs_[(size_t)it->second]);
}
} else {
std::vector<int32_t> live(running_);
live.insert(live.end(), waiting_.begin(), waiting_.end());
for (int32_t slot : live) {
if (slot < 0 || (size_t)slot >= reqs_.size() || !reqs_[(size_t)slot]) continue;
SchedReq& s = *reqs_[(size_t)slot];
if ((int64_t)s.req->n_computed < (int64_t)s.req->prompt.size()) fail(s);
}
}
return n;
}
int Scheduler::cancel(uint64_t id) {
std::lock_guard<std::mutex> lk(mu_);
auto it = by_id_.find(id);
@@ -1927,9 +1969,12 @@ void Scheduler::promote_next() {
}
/* ------------------------------------------------------------------ commit */
int Scheduler::commit(const StepResult& res) {
int Scheduler::commit(StepResult& res) {
std::lock_guard<std::mutex> lk(mu_);
if (!step_open_) return RAD_E_STATE;
/* The engine's reports describe this step only (StepResult::n_prefill_done, n_finished). */
step_finished_ = 0;
last_prefill_ids_.clear();
if (res.n_seq != (int)plan_.e.size()) {
RAD_ERR("commit of %d sequences against a step of %d", res.n_seq, (int)plan_.e.size());
return RAD_E_INVAL;
@@ -1992,8 +2037,12 @@ int Scheduler::commit(const StepResult& res) {
r.n_cached);
/* Once, when the prompt is fully placed: the usage block's admission accounting.
* `n_cached` is what the prefix cache supplied. */
if (r.n_computed >= (int64_t)r.prompt.size())
if (r.n_computed >= (int64_t)r.prompt.size()) {
server::sink_prompt_stats(r.sink, (int32_t)r.prompt.size(), r.n_cached);
/* The prompt is fully placed: the point past which a decode step reads the ring,
* and the point at which the engine checks a streamed resolve's overflow. */
last_prefill_ids_.push_back(r.id);
}
if (e.produces_token && res.token && r.embed) {
/* The answer is the pooled vector the engine already put in the sink; there is
* no token to emit, and `eos` ends the request below. */
@@ -2130,6 +2179,8 @@ int Scheduler::commit(const StepResult& res) {
* block_end() / serial_end() has since cleared, can be released: every pass naming them was
* issued before this step's readback, which waited for the compute stream. */
sweep_deferred();
res.n_prefill_done = (int)last_prefill_ids_.size();
res.n_finished = step_finished_;
return RAD_OK;
}
@@ -2344,6 +2395,11 @@ const RadBatch* Scheduler::serial_batch(int round, int step) {
serial_plan_.reset();
serial_plan_.phase = RAD_PHASE_DECODE;
/* The deployment's speculative window, though no entry of a draft pass verifies: the history
* pass redoes up to 1 + n_spec verified positions a sequence, and a prefill chunk's history is
* wider. Its KV view is chosen from that shape (kvstream/bind.cpp), so the batch must carry the
* window the ceiling was built from. */
serial_plan_.n_spec = cfg_.n_spec > 0 ? cfg_.n_spec : 0;
/* Negative is the history pass; positive is draft round r. The sign is what the plugin reads
* to decide whether the pass ends in the lm_head (rad_runtime.h). */
serial_plan_.draft_pass = round == 0 ? -1 : round;
@@ -2778,6 +2834,12 @@ SchedMetrics Scheduler::metrics_locked() const {
m.kv[i].bytes_used = kv_->pool_bytes_used(i);
m.kv[i].bytes_carved = kv_->pool_bytes_carved(i);
}
/* Streaming's totals beside the per-group rows: the ring's depth is geometry, the misses and
* bytes are what resolve launches have moved, so all three read zero on a server whose groups
* are fully resident. */
m.kv_resident_blocks = kv_->stream_resident_blocks();
m.kv_stream_misses = kv_->stream_misses();
m.kv_stream_bytes_in = kv_->stream_bytes_in();
if (pc_) { m.prefix_evictions = pc_->evictions();
m.prefix_ckpt_evictions = pc_->ckpt_evictions();