diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index b7f3983f..9aebb1ea 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -1153,6 +1153,16 @@ int Builder::declare_scores(rad_buf hidden, rad_weight table) { w.name.c_str()); return RAD_E_INVAL; } + /* The table must cover 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 + * would turn the last block of the vocabulary into a read off the end of a weight — a device + * fault, not a refusal. More rows than the vocabulary is padding and reads fine. */ + if (w.decl.shape[0] < prog_.meta.n_vocab) { + err(RAD_E_INVAL, "declare_scores: the table ('%s') has %lld rows, under the %lld-token " + "vocabulary the scored readout indexes it by", w.name.c_str(), + (long long)w.decl.shape[0], (long long)prog_.meta.n_vocab); + return RAD_E_INVAL; + } prog_.score_hidden = hidden; prog_.score_table = table; return RAD_OK; diff --git a/core/engine.cpp b/core/engine.cpp index 27788414..651074a8 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -634,7 +634,7 @@ int Engine::step_loop() { so.reqs.push_back(const_cast(e.req)); } so.n_rows = logit_row; - if (const int ss = stage_scored_rows(plan, so); ss < 0) return fail_loop(ss); + if (const int ss = stage_scored_rows(plan, so, cancel); ss < 0) return fail_loop(ss); /* The sampler's host work, run while the ranks enqueue (see build_mu_ in engine.h). It * touches only request state and host grammar automata; Sampler::run issues the uploads @@ -683,7 +683,9 @@ int Engine::step_loop() { return RAD_OK; }; - cancel.clear(); + /* No clear here: `cancel` is empty when the step's staging begins (applied and cleared at + * the join below), and stage_scored_rows may already have filled it — a scored request + * whose pinned rows could not be allocated rides the same deferred path. */ int bst = RAD_OK; /* RADIANCE_LOG_STEPS: one line per step with its phase and each sequence's slot, query @@ -1198,16 +1200,25 @@ int Engine::save_checkpoints() { * to the host before the sampler can rewrite it in place. stage_scored_rows picks the rows while * every rank is parked; issue_scored_rows copies one row per rank stream ahead of its sampler; * collect_scored_rows reads each request's ids after the step's wait, before the commit. A whole - * row is copied (512 KB at a 128K vocabulary), which is cheaper than one copy per id. */ -int Engine::stage_scored_rows(const StepPlan& plan, const StepOut& so) { + * row is copied (512 KB at a 128K vocabulary), which is cheaper than one copy per id. + * + * The pinned host buffer is grow-only and its growth is the one part of staging that can fail + * on resources rather than on a broken program: then the scored requests are the ones cancelled + * (into `cancel`, applied the way the sampler's cancellations are), not the engine — a request + * whose scores the box cannot hold is that request's failure, and every other row on this step + * and every other connection on the server keeps running. */ +int Engine::stage_scored_rows(const StepPlan& plan, const StepOut& so, + std::vector& cancel) { score_rows_.clear(); score_only_ = false; score_ids_.clear(); if (embedding_) return RAD_OK; + std::vector scored; for (size_t i = 0; i < plan.e.size(); ++i) { const Request* r = plan.e[i].req; if (so.first_row[i] < 0 || !r || !r->token_scores || !r->output.empty()) continue; score_rows_.push_back({ so.first_row[i], r->token_scores.get() }); + scored.push_back(r->id); } if (score_rows_.empty()) return RAD_OK; const int64_t n = (int64_t)score_rows_.size(); @@ -1224,6 +1235,8 @@ int Engine::stage_scored_rows(const StepPlan& plan, const StepOut& so) { std::sort(score_ids_.begin(), score_ids_.end()); score_ids_.erase(std::unique(score_ids_.begin(), score_ids_.end()), score_ids_.end()); } + bool host_failed = false; + int64_t failed_rows = 0; for (auto& rk : ranks_) { const int64_t width = rk->sampler.config().n_vocab_local; /* In floats: n logits rows, or the bf16 hidden and table rows of a scored step. */ @@ -1238,12 +1251,17 @@ int Engine::stage_scored_rows(const StepPlan& plan, const StepOut& so) { rk->score_host = static_cast( rad_dev_alloc(rows * width * (int64_t)sizeof(float), RAD_MEM_HOST_PINNED)); rk->score_rows_cap = rk->score_host ? rows : 0; - if (!rk->score_host) { - RAD_ERR("scored rows: no pinned memory for %lld logits rows", (long long)rows); - return RAD_E_NOMEM; - } + if (!rk->score_host) { host_failed = true; failed_rows = rows; break; } } if (ranks_.size() > 1) RAD_TRY(rad_dev_set(ranks_[0]->device)); + if (host_failed) { + RAD_ERR("scored rows: no pinned memory for %lld logits rows; cancelling the scored " + "requests, not the engine", (long long)failed_rows); + cancel.insert(cancel.end(), scored.begin(), scored.end()); + score_rows_.clear(); + score_only_ = false; + score_ids_.clear(); + } return RAD_OK; } diff --git a/core/engine.h b/core/engine.h index 3db2956e..13bd3d43 100644 --- a/core/engine.h +++ b/core/engine.h @@ -330,7 +330,7 @@ private: bool score_only_ = false; std::vector score_ids_; int64_t score_embd_ = 0; - int stage_scored_rows(const StepPlan& plan, const StepOut& so); + int stage_scored_rows(const StepPlan& plan, const StepOut& so, std::vector& cancel); int issue_scored_rows(Rank& r); int collect_scored_rows(); diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index a75809ee..7968acc5 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -334,9 +334,24 @@ int Scheduler::add_locked(std::unique_ptr 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; diff --git a/tests/build_test.cpp b/tests/build_test.cpp index 0f430b7b..2b23d63a 100644 --- a/tests/build_test.cpp +++ b/tests/build_test.cpp @@ -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()); diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 850b1966..7c8d1dff 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -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 rs[2]; + for (int i = 0; i < 2; ++i) { + rs[i] = std::make_unique(); + rs[i]->id = f.next_id++; + rs[i]->max_tokens = 1; + rs[i]->prompt.assign(16, 7); + rs[i]->token_scores = std::make_shared(); + 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