Archived
sched: no step issued ahead behind a step that ends every request it carries
A pipelined engine issues each step's successor before the step is read back: block drafters (DFlash, DFlash2) since 1.11.0, serial MTP and decoding without a drafter since 1.0.0. The step that emits a request's last token therefore had one more step queued behind it, for a request its commit then finishes. That step verifies nothing, yet the card runs its whole forward pass and the drafter's context pass behind it, and a request that arrives once the last token is out -- a single-stream client's next turn, sent as the reply ends -- waits for all of it before its prefill starts. On Qwen3.8-27B ParoQuant int5 with DFlash2 on one R9700 that is about one decode step (34-53 ms) of TTFT on every back-to-back request (#225: 105 -> 139 ms); for 127 requests 1.12.0 ran 806 decode steps, all issued ahead, against 1.8.1's 680. Scheduler::step_next_locked, beside the other rules for issuing ahead and before the next step's KV is reserved, now issues nothing when 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: output + 1 + drafts >= max_tokens), or, most likely, when those drafts hold an end-of-generation id (ignore_eos requests excepted). The engine hands the scheduler the end-of-generation ids at start (set_eog), the sorted list the card's acceptance reads. A guess that misses (a drafted end the verify rejects, too few drafts accepted to reach max_tokens) costs one step built after the commit instead of before it. Ends it does not predict still leave one step behind: an end the verify chose itself, a stop string, the context limit. Without a drafter only max_tokens ends are caught, exactly. At -v the commit names each request that ended with a step issued ahead behind it (finish reason, its last step's tokens and accepted drafts, output against max_tokens, whether its drafts held an end). Tests: sched_test pins both halves (a drafted end issues and reserves nothing; a request whose next token is its last, or whose token and accepted drafts could reach max_tokens, issues nothing; an ignore_eos request's drafted end, drafts without an end, and a request that every draft accepted would leave a token short still issue ahead; of two requests one going on, both issue ahead); the test of a request finishing under a step issued ahead now ends it on an end of text the scheduler cannot see coming. engine_e2e_test: two 32-token replies without a drafter are 62 decode steps, all issued ahead, not 64. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
55cf057a30
commit
f55570e351
5 files changed
+150
-3
No files matched your search
@@ -1823,6 +1823,42 @@ const RadBatch* Scheduler::step_next() {
|
||||
return b;
|
||||
}
|
||||
|
||||
void Scheduler::set_eog(std::vector<int32_t> ids) {
|
||||
std::lock_guard<std::mutex> 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});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user