diff --git a/core/engine_draft.cpp b/core/engine_draft.cpp index edf78fbc..99d8a667 100644 --- a/core/engine_draft.cpp +++ b/core/engine_draft.cpp @@ -30,6 +30,8 @@ int Engine::step_state_init() { if (vocab_) eog = vocab_->eog(); std::sort(eog.begin(), eog.end()); eog.erase(std::unique(eog.begin(), eog.end()), eog.end()); + /* The scheduler's rule for a step that ends its requests reads the same ids (step_next). */ + sched_.set_eog(eog); const int64_t n_slot = cfg_.max_seqs > 0 ? cfg_.max_seqs : 1; const int64_t slot_bytes = n_slot * (int64_t)sizeof(StepSlot); for (auto& rk : ranks_) { diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 2196413a..7a8e4af5 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -1823,6 +1823,42 @@ const RadBatch* Scheduler::step_next() { return b; } +void Scheduler::set_eog(std::vector ids) { + std::lock_guard lk(mu_); + std::sort(ids.begin(), ids.end()); + ids.erase(std::unique(ids.begin(), ids.end()), ids.end()); + eog_ = std::move(ids); +} + +bool Scheduler::is_eog(int32_t id) const { + return std::binary_search(eog_.begin(), eog_.end(), id); +} + +/* Whether the step just issued ends every request it carries: when it can bring each to its + * max_tokens -- certainly with the token it emits, possibly with the drafts it verifies, since it + * commits that token and every draft it accepts -- or, most likely, when those drafts hold an + * end-of-generation id (not for an ignore_eos request). A step issued ahead behind it would verify + * nothing, its rows' requests finished by the commit in between, yet the card runs its whole + * forward pass and the drafter's passes behind it, and a request that arrives once the last token + * is out (a client's next turn, sent as this one ends) queues behind them: a decode step of TTFT + * (#225). A guess that misses (a drafted end the verify rejects, or too few drafts accepted to + * reach max_tokens) costs one step built after the commit instead of before it. */ +bool Scheduler::ends_every_request() const { + for (const StepEntry& e : plan_.e) { + if (!e.produces_token || e.slot < 0 || (size_t)e.slot >= reqs_.size()) return false; + const SchedReq& s = *reqs_[(size_t)e.slot]; + if (!s.req || s.req->state != ReqState::Running) return false; + const Request& r = *s.req; + const int32_t n_drafts = e.draft ? std::min(e.n_spec, e.n_draft) : 0; + if (r.max_tokens > 0 && (int64_t)r.output.size() + 1 + n_drafts >= r.max_tokens) continue; + if (r.ignore_eos || n_drafts == 0) return false; + bool end = false; + for (int32_t k = 0; k < n_drafts && !end; ++k) end = is_eog(e.draft[k]); + if (!end) return false; + } + return !plan_.e.empty(); +} + const RadBatch* Scheduler::step_next_locked() { if (!step_open_ || next_open_ || plan_.e.empty()) return nullptr; /* An admission is a decision about the pool and the queue that the full step makes. */ @@ -1830,6 +1866,9 @@ const RadBatch* Scheduler::step_next_locked() { /* Once the hold is spent, the held chunk goes into the next full step rather than waiting * behind decode steps issued ahead. */ if (held_prefill_ > 0 && interleave_left_ == 0) return nullptr; + /* Nothing behind a step that ends every request it carries (ends_every_request). Before the + * loop below, which reserves the next step's KV. */ + if (ends_every_request()) return nullptr; plan_next_.reset(); for (const StepEntry& e : plan_.e) { @@ -2119,6 +2158,20 @@ int Scheduler::commit(const StepResult& res) { } else if (eos) { close_request(s, ReqState::Finished, "stop", carried); } + /* -v: a step issued ahead behind a request this commit ended verifies nothing; why the + * rule in step_next_locked let it through (ends_every_request). */ + if (carried && r.state != ReqState::Running && (int)log_level() >= (int)Log::Debug) { + const int32_t acc = res.n_accepted ? res.n_accepted[i] : 0; + bool drafted_end = false; + for (int32_t k = 0; e.draft && k < std::min(e.n_spec, e.n_draft); ++k) + drafted_end = drafted_end || is_eog(e.draft[k]); + RAD_DEBUG("request %llu ended (%s) with a step issued ahead behind it: %d token(s) " + "this step, %d of %d draft(s) accepted, %zu of max_tokens %d, %s", + (unsigned long long)r.id, r.finish_reason.c_str(), (int)acc + 1, (int)acc, + (int)e.n_spec, r.output.size(), (int)r.max_tokens, + drafted_end ? "an end-of-generation id among the drafts" + : "no end-of-generation id among the drafts"); + } committed_.push_back({e.seq, (uint64_t)r.output.size(), r.state == ReqState::Running}); } diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index 67607661..1bab24e7 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -274,6 +274,10 @@ public: * cache. Takes the step lock because the step path inserts into the map (live sequences hold * their own block references). See IScheduler::reset_prefix_cache. */ int64_t reset_prefix_cache(); + /* The vocabulary's end-of-generation ids, the ones the card's acceptance reads: step_next() + * issues no step ahead behind one whose drafts end every request it carries. Set once at + * start; none (the default) leaves only the max_tokens half of that rule. */ + void set_eog(std::vector ids); /* Forget one stored conversation, by label or engine id, keeping its first `keep_tokens` * (IPrefixCache::release_session). Refused with RAD_E_STATE while a request of that * conversation is queued or running: its blocks would be skipped as live and its close @@ -307,6 +311,8 @@ public: void expect_draft(uint64_t id, int n); const RadBatch* step_next(); const RadBatch* step_next_locked(); + /* Whether the step just issued ends every request it carries (step_next_locked). */ + bool ends_every_request() const; void promote_next(); /* Fold the step's outcome back in: append tokens, roll back rejected speculation, retire * finished requests, feed the draft controller. */ @@ -475,6 +481,9 @@ private: * meta.n_layers; drafter layers bind from `base_layer` up) that the cache does not index? * Unlike a drafter hole, recomputing the tail cannot repair it; see admit_one. */ bool hit_holes_trunk_ = false; + /* The end-of-generation ids, sorted and unique (set_eog), and a lookup in them. */ + std::vector eog_; + bool is_eog(int32_t id) const; /* R-SWA: the groups whose KV manager carries per-sequence runtime windows, with the W a * sequence is armed with (IKVManager::runtime_window_tokens). Empty for every model that does * not declare one, in which case admission and commit are exactly as before. A sequence with diff --git a/tests/engine_e2e_test.cpp b/tests/engine_e2e_test.cpp index d8350ecc..7435ccfb 100644 --- a/tests/engine_e2e_test.cpp +++ b/tests/engine_e2e_test.cpp @@ -1813,6 +1813,29 @@ TEST(decoding_without_a_drafter_issues_steps_ahead) { CHECK_EQ(spec.stop(), 0); } +/* The step that emits a request's last token issues nothing behind it: a step issued ahead for a + * request the commit then finishes verifies nothing, and the next request would wait out its + * forward pass (#225). A reply of 32 tokens is the prefill's token and 31 decode steps, each + * issued ahead of the one before it, and no 32nd. Twice, so the second request starts behind the + * first's last step. */ +TEST(a_requests_last_step_issues_no_step_ahead) { + REQUIRE(world().ok); + Server plain; + REQUIRE(plain.start(world().lfm2, kPlain)); + for (uint64_t seed : { 6, 7 }) { + const std::vector t = + tokens_of(post(plain.port, "/completion", completion(prompt_ids(seed, 80), 32))); + CHECK_EQ(t.size(), (size_t)32); + } + const double decode = metric(plain.port, "radiance:engine_steps_decode_total"); + const double ahead = metric(plain.port, "radiance:engine_steps_ahead_total"); + if (decode != 62.0 || ahead != 62.0) + fprintf(stderr, " decode steps %g, issued ahead %g\n", decode, ahead); + CHECK_EQ(decode, 62.0); + CHECK_EQ(ahead, 62.0); + CHECK_EQ(plain.stop(), 0); +} + /* Decoding a token runs the recurrent kernels over one row; prefilling it runs the chunked ones * over the whole prompt. Each decoded position's probabilities must be what a cold prefill of the * prompt and the tokens before it gives: exactly where both paths compute each row the same way diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index a039004e..b5bf9f74 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -4012,6 +4012,64 @@ TEST(a_step_is_issued_ahead_only_once_the_requests_speculation_is_fixed) { CHECK(f.sch.step_next() != nullptr); } +/* No step is issued ahead behind one that ends every request it carries (#225): its rows would + * belong to finished requests, and a request arriving after the last token would wait out its + * forward pass. The verify ends a request most likely when its drafts hold an end-of-generation + * id, certainly when its first token is the request's last (max_tokens), and possibly when that + * token and its accepted drafts reach max_tokens. An ignore_eos request's drafted end ends + * nothing, nor do drafts without one, and a request that even every draft accepted leaves short + * of max_tokens goes on. The refusal reserves nothing for the step it does not issue. */ +TEST(no_step_is_issued_ahead_behind_one_that_ends_its_requests) { + constexpr int32_t kEog = 2; + int32_t ends[4] = { 21, kEog, 23, 24 }, goes_on[4] = { 21, 22, 23, 24 }; + /* One request: its prompt's token committed, then the verify of `draft`. */ + auto ahead = [&](const int32_t* draft, int max_tokens, bool ignore_eos) -> bool { + Fixture f; + f.build(); + f.cfg.n_spec = 4; + CHECK_OK(f.start()); + f.sch.set_eog({ kEog, 3 }); + const uint64_t id = f.submit(160, max_tokens, 0, ignore_eos); + CHECK(f.sch.step() != nullptr); + f.commit_plain(11); + CHECK_OK(f.sch.set_draft(id, draft, 4)); + const bool verify = f.sch.step() != nullptr; + CHECK(verify); + if (!verify) return false; + CHECK_EQ(f.sch.plan().e[0].n_spec, 4); + f.sch.find(id)->speculation_fixed = true; + f.sch.expect_draft(id, 4); + const int64_t free_before = f.kv.free_blocks(0); + const bool issued = f.sch.step_next() != nullptr; + if (!issued) CHECK_EQ(f.kv.free_blocks(0), free_before); + return issued; + }; + CHECK(ahead(goes_on, 64, false)); + CHECK(!ahead(ends, 64, false)); /* a drafted end */ + CHECK(ahead(ends, 64, true)); /* ignore_eos: an end-of-text like any token */ + CHECK(!ahead(goes_on, 2, false)); /* one token committed, its next the last */ + CHECK(!ahead(goes_on, 6, false)); /* 1 + its token + 4 accepted drafts reach it */ + CHECK(ahead(goes_on, 7, false)); /* all four accepted leave it a token short */ + + /* Two requests: one ends, the other goes on, so the step is issued ahead for both. */ + Fixture f; + f.build(); + f.cfg.n_spec = 4; + CHECK_OK(f.start()); + f.sch.set_eog({ kEog }); + const uint64_t a = f.submit(160, 64), b = f.submit(160, 64); + CHECK(f.sch.step() != nullptr); + f.commit_plain(11); + CHECK_OK(f.sch.set_draft(a, ends, 4)); + CHECK_OK(f.sch.set_draft(b, goes_on, 4)); + REQUIRE(f.sch.step() != nullptr); + f.sch.find(a)->speculation_fixed = true; + f.sch.find(b)->speculation_fixed = true; + const RadBatch* nb = f.sch.step_next(); + REQUIRE(nb != nullptr); + CHECK_EQ(nb->n_seq, 2); +} + /* The context bounds a sequence whatever max_tokens says, "no limit" included. Positions past the * last one the deployment can address have no rotary row or block-table column; a sequence * running into them would corrupt memory instead of stopping. */ @@ -4966,13 +5024,15 @@ TEST(a_cancel_while_a_step_runs_ahead_keeps_the_request_until_that_step_commits) } /* A request that finishes at the commit of a step whose successor is already issued keeps its - * blocks for that successor, which writes them, and returns them at its commit. */ + * blocks for that successor, which writes them, and returns them at its commit. The end is one + * the scheduler could not see coming (an end-of-text it did not draft): a step known to end the + * request issues nothing ahead (no_step_is_issued_ahead_behind_one_that_ends_its_requests). */ TEST(a_request_finishing_under_a_step_issued_ahead_keeps_its_blocks_for_that_step) { Fixture f; f.build(/*blocks=*/20, /*states=*/64); CHECK_OK(f.start()); const int64_t before = f.kv.free_blocks(0); - auto r = f.request(40, /*max_tokens=*/2); + auto r = f.request(40, /*max_tokens=*/64); r->speculation_fixed = true; const uint64_t a = f.add(std::move(r)); REQUIRE(f.sch.step() != nullptr); @@ -4980,7 +5040,7 @@ TEST(a_request_finishing_under_a_step_issued_ahead_keeps_its_blocks_for_that_ste REQUIRE(f.sch.step() != nullptr); REQUIRE(f.sch.step_next() != nullptr); const int64_t held = f.kv.free_blocks(0); - f.commit_plain(); /* the second token: finished */ + CHECK_OK(f.commit(7, /*eos=*/1)); /* the second token, an end of text: finished */ CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Finished); CHECK_EQ(f.kv.free_blocks(0), held); f.sch.promote_next();