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

+34
View File
@@ -467,6 +467,40 @@ TEST(schema_mismatches_are_named) {
CHECK_EQ(b.status(), RAD_E_SCHEMA);
}
/* declare_scores binds the scored readout: bf16 hidden rows and a bf16 head table of the same
* width, and the table must COVER the vocabulary — the readout indexes it by token id, and the
* scheduler's admission bound is meta.n_vocab, so a short table is a device fault waiting in the
* last block of the vocabulary, not a refusal. Rows past the vocabulary are padding and read
* fine. */
TEST(declare_scores_wants_a_table_that_covers_the_vocabulary) {
NEED_PLUGIN(reg)
RadModelMeta m = make_meta();
m.n_vocab = 8192;
Builder b(reg, m, make_ctx());
RadBufDecl hd{};
hd.dtype = RAD_BF16;
hd.rank = 2;
hd.shape[0] = 512;
hd.shape[1] = 4096;
hd.kind = RAD_BUF_TRANSIENT;
hd.domain = RAD_DOMAIN_DEVICE;
const rad_buf hidden = b.decl_buffer("hout", &hd);
REQUIRE(hidden > 0);
const rad_weight short_t = decl_w(b, "tok.short", 4096, 4096); /* rows < n_vocab */
CHECK_EQ(b.declare_scores(hidden, short_t), RAD_E_INVAL);
CHECK(has(b.errors().back(), "under the 8192-token vocabulary"));
const rad_weight narrow = decl_w(b, "tok.narrow", 8192, 2048); /* width != hidden's */
CHECK_EQ(b.declare_scores(hidden, narrow), RAD_E_INVAL);
const rad_weight full = decl_w(b, "tok.full", 8192, 4096);
CHECK_OK(b.declare_scores(hidden, full));
const rad_weight padded = decl_w(b, "tok.padded", 9216, 4096); /* rows > n_vocab: fine */
CHECK_OK(b.declare_scores(hidden, padded));
}
TEST(no_schema_names_the_near_miss) {
NEED_PLUGIN(reg)
Builder b(reg, make_meta(), make_ctx());
+23
View File
@@ -1284,6 +1284,29 @@ TEST(a_scored_request_names_real_vocabulary_rows) {
CHECK(!kept->may_speculate);
}
/* The candidate COUNT is capped as well as their values: a scored step stages the union of
* these lists as pinned host rows, so their size must not be a client's choice at the seam.
* 4096 is orders above what a decision request names (its written answers, a few dozen), so
* exactly the cap admits and one past it is refused by name. */
TEST(a_scored_request_names_at_most_the_candidate_cap) {
Fixture f;
f.build(/*blocks=*/20, /*states=*/64);
CHECK_OK(f.start());
std::unique_ptr<Request> rs[2];
for (int i = 0; i < 2; ++i) {
rs[i] = std::make_unique<Request>();
rs[i]->id = f.next_id++;
rs[i]->max_tokens = 1;
rs[i]->prompt.assign(16, 7);
rs[i]->token_scores = std::make_shared<TokenScores>();
rs[i]->token_scores->tokens.assign(4096 + i, 1);
}
int rc[2] = { -1, -1 };
f.sch.add_all(rs, 2, rc);
CHECK_EQ(rc[0], RAD_OK); /* exactly the cap */
CHECK_EQ(rc[1], RAD_E_INVAL); /* one past it */
}
/* The prompt itself names real vocabulary rows too: raw id arrays arrive verbatim from the
* request body, and an id past the embedding table must die at admission, never staged into a
* pass -- the embedding gather would fail the step, a failed step is a device fault, and a