sched+engine: keep a scored request's size from reaching the pinned pool

A scored request asks the engine for the logits of chosen candidate ids
at its prompt's last position (Request::token_scores). Three places
where that readout's memory was someone else's decision:

The candidate list was bounded by value, never by count, and the count
is what a scored step pays for: the step stages the union of every
staged request's candidates as hidden and table rows in one pinned host
buffer, before anything is admitted or accounted. One request naming a
million ids -- any caller of the raw request seam -- is a multi-gigabyte
pinned allocation the box either loses or dies on. add_locked caps the
list at 4096, orders above what a decision request names (its prompt's
written answers, a few dozen), and says so in the refusal.

And when the pinned allocation is lost anyway -- the cap bounds one
request's rows, not the batch's union, and the pool can be low for
reasons of its own -- stage_scored_rows returned E_NOMEM into the step
loop's fail path: the whole engine stopped, every connection with it.
It cancels the scored requests instead, on the step loop's existing
deferred-cancel vector the sampler's grammar failures already use: a
request whose scores the box cannot hold is that request's failure. No
alloc-failure seam exists to test the branch directly; its cancel path
is the sampler stage's, applied at the same join.

And declare_scores now refuses a head table shorter than the
vocabulary: the scored readout indexes it by token id, the scheduler
bounds candidate ids by meta.n_vocab, and a table a few thousand rows
short turns the last block of the vocabulary into a read off the end of
a weight -- a device fault, not a refusal. Padding past the vocabulary
reads fine and stays fine.

sched_test: a_scored_request_names_at_most_the_candidate_cap.
build_test: declare_scores_wants_a_table_that_covers_the_vocabulary.
ASan+UBSan: sched/build/server/systemone/config/diag, 6/6.
This commit is contained in:
fl0l0u authored and StillDeadcode committed 2026-10-31 07:50:43 +01:00
1 parent 5f7244da4d
commit 1c967b2f14
6 files changed
+110 -10

No files matched your search

+16 -1
View File
@@ -334,9 +334,24 @@ int Scheduler::add_locked(std::unique_ptr<Request> r) {
return RAD_E_FULL;
}
/* A scored prompt names real vocabulary rows and needs one step's decision; drafts would be
* proposed for a continuation nobody reads. */
* proposed for a continuation nobody reads.
*
* The candidate COUNT is capped as well as their values: a scored step stages the union of
* every staged request's candidate list as hidden and table rows in one pinned host buffer,
* so the lists are that buffer's size and must not be a client's choice. d1's questions name
* their prompt's written answers -- a few dozen at most -- so the cap leaves every real
* decision request far under it and refuses only the pathological. A step whose union still
* outgrows the pinned pool survives: Engine::stage_scored_rows cancels the scored requests
* rather than the engine. */
static const size_t kMaxScoredCandidates = 4096;
if (r->token_scores) {
if (r->embed || r->token_scores->tokens.empty()) return RAD_E_INVAL;
if (r->token_scores->tokens.size() > kMaxScoredCandidates) {
RAD_WARN("request %llu refused: %zu scored candidate ids, over the cap of %zu",
(unsigned long long)r->id, r->token_scores->tokens.size(),
kMaxScoredCandidates);
return RAD_E_INVAL;
}
for (int32_t t : r->token_scores->tokens)
if (t < 0 || t >= prog_->meta.n_vocab) return RAD_E_INVAL;
r->may_speculate = false;