Archived
sched: a bound on the preempted-prefill gate's wait
This commit is contained in:
1 parent
0792d86375
commit
33108e488b
16 files changed
+1522
-150
No files matched your search
+486
-75
@@ -11,7 +11,7 @@
|
||||
#include "server/sink.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
@@ -98,9 +98,7 @@ struct Fixture {
|
||||
g_gdn_row = KernelRow{ &g_gdn_ki, "fake", 0, 0 };
|
||||
|
||||
prog.meta.n_ctx_train = 4096;
|
||||
/* Above the 1000 + i the requests below spell: admission refuses ids past the
|
||||
* vocabulary, and these prompts must be admissible. */
|
||||
prog.meta.n_vocab = 65536;
|
||||
prog.meta.n_vocab = 512;
|
||||
|
||||
/* index 0 is the sentinel every handle table in Program carries */
|
||||
prog.ops.emplace_back();
|
||||
@@ -1283,30 +1281,6 @@ TEST(a_scored_request_names_real_vocabulary_rows) {
|
||||
CHECK(!kept->may_speculate);
|
||||
}
|
||||
|
||||
/* 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
|
||||
* device fault stops the engine (spec §17). One bad number must not be a server kill. */
|
||||
TEST(a_prompt_names_real_vocabulary_rows) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/20, /*states=*/64);
|
||||
CHECK_OK(f.start());
|
||||
const int32_t n_vocab = (int32_t)f.prog.meta.n_vocab;
|
||||
std::unique_ptr<Request> rs[5];
|
||||
const std::vector<std::vector<int32_t>> ids = { { 1, 2, n_vocab - 1 }, { n_vocab }, { -1 },
|
||||
{ 7, INT32_MAX }, { 0, 1, 2, n_vocab + 9 } };
|
||||
int rc[5] = { -1, -1, -1, -1, -1 };
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
rs[i] = std::make_unique<Request>();
|
||||
rs[i]->id = f.next_id++;
|
||||
rs[i]->max_tokens = 1;
|
||||
rs[i]->prompt = ids[(size_t)i];
|
||||
}
|
||||
f.sch.add_all(rs, 5, rc);
|
||||
CHECK_EQ(rc[0], RAD_OK);
|
||||
for (int i = 1; i < 5; ++i) CHECK_EQ(rc[i], RAD_E_INVAL);
|
||||
}
|
||||
|
||||
TEST(a_prefix_hit_starts_the_request_at_the_last_usable_checkpoint) {
|
||||
Fixture f;
|
||||
f.build();
|
||||
@@ -1704,6 +1678,7 @@ TEST(preemption_takes_the_lowest_priority_victim_and_requeues_it_to_the_front) {
|
||||
TEST(a_preempted_prefill_waits_for_the_whole_prompt_and_running_headroom) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/26, /*states=*/64); /* 26 x 16 = 416 tokens */
|
||||
f.cfg.preempt_wait_ms = 60000; /* old gate test: no fallback in its 80 steps */
|
||||
CHECK_OK(f.start());
|
||||
|
||||
uint64_t a = f.submit(287, 64, /*priority=*/5);
|
||||
@@ -1750,6 +1725,63 @@ TEST(a_preempted_prefill_waits_for_the_whole_prompt_and_running_headroom) {
|
||||
f.commit_plain();
|
||||
}
|
||||
|
||||
/* The gate's wait is bounded: after --preempt-wait-ms an overdue preempted prefill gets room by
|
||||
* preempting the lowest-priority running request before the step is planned, so it cannot queue
|
||||
* forever behind a long decode (the 710 s stall in the daily's log). */
|
||||
TEST(a_preempted_prefill_falls_back_to_a_running_victim_after_the_wait) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/26, /*states=*/64); /* 26 x 16 = 416 tokens */
|
||||
f.cfg.preempt_wait_ms = 0; /* fallback on the next step */
|
||||
CHECK_OK(f.start());
|
||||
|
||||
uint64_t a = f.submit(287, 64, /*priority=*/5);
|
||||
uint64_t b = f.submit(128, 64, /*priority=*/0);
|
||||
|
||||
/* A and B fill the pool; B's next token needs a block and B is the victim. */
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
CHECK_EQ(f.sch.plan().e.size(), 2);
|
||||
f.commit_plain();
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Preempted);
|
||||
f.commit_plain();
|
||||
|
||||
/* B's blocks are back in the pool; the old gate wants B's whole prompt plus one block of
|
||||
* headroom beside A's next token, so it cannot admit B without a victim. */
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Running);
|
||||
CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Running);
|
||||
CHECK_EQ(f.sch.metrics().preemptions, 2);
|
||||
CHECK_EQ(f.sch.metrics().preempted_waiting, 0);
|
||||
f.commit_plain();
|
||||
}
|
||||
|
||||
/* Several overdue preempted prefills: reclaiming for the head alone is not enough. Keep forcing
|
||||
* the lowest-priority runner until one overdue request can prefill, so the rest of the queue
|
||||
* cannot sit in Preempted behind a head that still does not fit. */
|
||||
TEST(overdue_preempted_prefills_keep_forcing_until_one_fits) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/20, /*states=*/64); /* 20 x 16 = 320 tokens */
|
||||
f.cfg.preempt_wait_ms = 0;
|
||||
f.cfg.max_seqs = 3;
|
||||
CHECK_OK(f.start());
|
||||
|
||||
uint64_t a = f.submit(160, 16, /*priority=*/5);
|
||||
uint64_t b = f.submit(80, 16, /*priority=*/3);
|
||||
uint64_t c = f.submit(48, 16, /*priority=*/0);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain();
|
||||
|
||||
/* Fill until the pool refuses someone. Then the overdue path must not leave every preempted
|
||||
* request waiting while a runner keeps the blocks. */
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (!f.sch.step()) break;
|
||||
f.commit_plain();
|
||||
}
|
||||
CHECK(f.sch.metrics().preempted_waiting == 0 || f.sch.metrics().preempt_forced > 0);
|
||||
CHECK(f.sch.metrics().running + f.sch.metrics().preempted_waiting > 0);
|
||||
(void)a; (void)b; (void)c;
|
||||
}
|
||||
|
||||
TEST(a_mixed_step_has_the_right_boundaries_and_slot_mapping) {
|
||||
Fixture f;
|
||||
f.build();
|
||||
@@ -3340,9 +3372,9 @@ TEST(the_adaptive_window_chooses_the_head_depth_over_the_whole_batch) {
|
||||
CHECK_EQ(h.plan_step(hone, 0, 3), 2);
|
||||
}
|
||||
|
||||
/* The flag, for a drafting head (serial or block). --draft-window adaptive with prompt lookup
|
||||
* alone, or with no speculation, keeps the configured depth and says so at startup; the proposal
|
||||
* then reaches the step uncut. The default is the fixed window. */
|
||||
/* The flag, for a drafting head (serial or block) or prompt lookup alone. --draft-window adaptive
|
||||
* with no speculation keeps the configured depth and says so at startup. The default is the fixed
|
||||
* window. */
|
||||
TEST(the_adaptive_window_is_a_flag_for_a_drafting_head_and_said_when_there_is_none) {
|
||||
{
|
||||
Fixture f;
|
||||
@@ -3395,6 +3427,8 @@ TEST(the_adaptive_window_is_a_flag_for_a_drafting_head_and_said_when_there_is_no
|
||||
CHECK(f.sch.metrics().draft_verify_depth[4] > 0);
|
||||
CHECK(f.sch.metrics().draft_verify_depth[1] > 0);
|
||||
}
|
||||
/* Prompt lookup as the only drafter takes the policy as a block drafter does: each of its
|
||||
* drafts is a verify row (prompt_lookup_alone_follows_its_own_acceptance). */
|
||||
{
|
||||
Fixture f;
|
||||
adaptive_fixture(f, 4);
|
||||
@@ -3403,9 +3437,9 @@ TEST(the_adaptive_window_is_a_flag_for_a_drafting_head_and_said_when_there_is_no
|
||||
int st = RAD_E_STATE;
|
||||
const std::string log = info_of([&] { st = f.start(); });
|
||||
CHECK_OK(st);
|
||||
CHECK(!f.sch.draft().adaptive());
|
||||
CHECK(log.find("--draft-window adaptive: the window is fixed at 4") != std::string::npos);
|
||||
CHECK(log.find("prompt lookup only") != std::string::npos);
|
||||
CHECK(f.sch.draft().adaptive());
|
||||
CHECK(log.find("--draft-window adaptive with prompt lookup only") != std::string::npos);
|
||||
CHECK(log.find("the window is fixed") == std::string::npos);
|
||||
|
||||
const uint64_t a = f.submit(160, 64);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
@@ -3413,7 +3447,7 @@ TEST(the_adaptive_window_is_a_flag_for_a_drafting_head_and_said_when_there_is_no
|
||||
int32_t draft[4] = { 21, 22, 23, 24 };
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 4));
|
||||
CHECK_EQ(f.sch.find(a)->n_draft, 4);
|
||||
CHECK(!f.sch.metrics().draft_adaptive);
|
||||
CHECK(f.sch.metrics().draft_adaptive);
|
||||
}
|
||||
{
|
||||
Fixture f;
|
||||
@@ -3613,6 +3647,423 @@ TEST(the_adaptive_window_is_not_fed_by_prompt_lookup) {
|
||||
CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 1.0, 1e-6);
|
||||
}
|
||||
|
||||
/* With prompt lookup the only drafter, its drafts are what the adaptive window measures: refused
|
||||
* lookups bring each sequence's window, and the lookup's proposals, down to one draft. */
|
||||
TEST(prompt_lookup_alone_follows_its_own_acceptance) {
|
||||
Fixture f;
|
||||
adaptive_fixture(f, 4);
|
||||
f.prog.drafter.kind = RAD_DRAFT_NONE;
|
||||
f.prog.drafter.depth = 0;
|
||||
CHECK_OK(f.start());
|
||||
REQUIRE(f.sch.draft().adaptive());
|
||||
|
||||
auto r = std::make_unique<Request>();
|
||||
r->id = f.next_id++;
|
||||
r->max_tokens = 2000;
|
||||
r->prompt.resize(160);
|
||||
for (int i = 0; i < 160; ++i) r->prompt[(size_t)i] = 100 + i % 8;
|
||||
CHECK_OK(f.sch.add(std::move(r)));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
f.commit_plain(100);
|
||||
|
||||
int first = 0, last = 0, lookups = 0;
|
||||
for (int t = 0; t < 6 * DraftController::kHold; ++t) {
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
const StepEntry& e = f.sch.plan().e[0];
|
||||
if (e.n_spec > 0) {
|
||||
CHECK(e.ngram);
|
||||
/* What the pattern says comes next, the period continued. */
|
||||
CHECK_EQ(e.draft[0], 100 + (t + 1) % 8);
|
||||
if (!first) first = e.n_spec;
|
||||
last = e.n_spec;
|
||||
++lookups;
|
||||
}
|
||||
f.commit_plain(100 + (t + 1) % 8); /* every lookup refused */
|
||||
}
|
||||
CHECK_EQ(lookups, 6 * DraftController::kHold);
|
||||
CHECK_EQ(first, 4);
|
||||
CHECK_EQ(last, 1);
|
||||
CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 1.0, 1e-6);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ prompt lookup's verify gate */
|
||||
/* A held proposal is scored against the text that follows, as a verify would have scored it: its
|
||||
* accepted count is the common prefix. One proposal at a time; the next is taken once it ends. */
|
||||
TEST(the_lookup_gate_scores_a_held_proposal_against_the_text_that_follows) {
|
||||
LookupGate g;
|
||||
g.configure(4);
|
||||
LookupSeq q;
|
||||
/* The priors: a short match waits, a long one drafts. */
|
||||
CHECK_NEAR(g.expect(q, 0, 4), 4 * LookupGate::kPriorShort, 1e-6);
|
||||
CHECK_NEAR(g.expect(q, 1, 4), 4 * LookupGate::kPriorLong, 1e-6);
|
||||
CHECK_EQ(LookupGate::class_of(LookupGate::kLongMatch - 1), 0);
|
||||
CHECK_EQ(LookupGate::class_of(LookupGate::kLongMatch), 1);
|
||||
|
||||
std::vector<int32_t> text(10, 1); /* the sequence so far: 10 tokens */
|
||||
TokenSeq ts;
|
||||
auto view = [&]() {
|
||||
ts.a = text.data();
|
||||
ts.na = (int64_t)text.size();
|
||||
};
|
||||
const int32_t prop[4] = { 5, 6, 7, 8 };
|
||||
g.offer(q, 10, prop, 4, 2);
|
||||
g.held(q, 4);
|
||||
const int32_t other[4] = { 40, 41, 42, 43 };
|
||||
g.offer(q, 10, other, 4, 9); /* pending already: counted, not taken */
|
||||
CHECK_EQ(q.proposed, (int64_t)2);
|
||||
CHECK_EQ(q.pend_cls, 0);
|
||||
|
||||
text.push_back(5); /* the first came true; no verdict yet */
|
||||
view();
|
||||
g.advance(q, ts);
|
||||
CHECK(q.pend_pos >= 0);
|
||||
CHECK_NEAR(q.s[0][0], 0.0, 1e-6);
|
||||
text.push_back(6);
|
||||
text.push_back(9); /* two came true, the third did not */
|
||||
view();
|
||||
g.advance(q, ts);
|
||||
CHECK(q.pend_pos < 0);
|
||||
/* The prior weighs two proposals: the first evaluated moves each position a third. */
|
||||
const double w = 1.0 / (LookupGate::kPriorWeight + 1.0);
|
||||
CHECK_NEAR(q.s[0][0], w, 1e-6);
|
||||
CHECK_NEAR(q.s[0][1], w, 1e-6);
|
||||
CHECK_NEAR(q.s[0][2], 0.0, 1e-6);
|
||||
CHECK_NEAR(q.s[0][3], 0.0, 1e-6);
|
||||
CHECK_NEAR(q.s[1][0], LookupGate::kPriorLong, 1e-6); /* the other class untouched */
|
||||
CHECK_EQ(q.held_hits, (int64_t)2);
|
||||
CHECK_EQ(g.held_hits(), (int64_t)2);
|
||||
CHECK_EQ(g.held_count(), (int64_t)1);
|
||||
CHECK_EQ(g.held_drafts(), (int64_t)4);
|
||||
|
||||
/* A proposal that comes true whole ends at its last token; a verified one counts no held
|
||||
* hits but feeds the estimate the same way. */
|
||||
const int64_t at = (int64_t)text.size();
|
||||
const int32_t whole[4] = { 11, 12, 13, 14 };
|
||||
g.offer(q, at, whole, 4, 3);
|
||||
for (int32_t t : whole) text.push_back(t);
|
||||
view();
|
||||
g.advance(q, ts);
|
||||
CHECK(q.pend_pos < 0);
|
||||
CHECK(q.s[0][3] > 0.0f);
|
||||
CHECK_EQ(q.held_hits, (int64_t)2);
|
||||
|
||||
/* Evidence is what moves a short match into drafting: a run of proposals that come true. */
|
||||
LookupGate g2;
|
||||
g2.configure(4);
|
||||
LookupSeq r;
|
||||
CHECK(!g2.pays(g2.expect(r, 0, 4), 4, 1, 1.0));
|
||||
std::vector<int32_t> s2(4, 1);
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
const int32_t p2[4] = { 2, 3, 4, 5 };
|
||||
g2.offer(r, (int64_t)s2.size(), p2, 4, 2);
|
||||
for (int32_t t : p2) s2.push_back(t);
|
||||
TokenSeq t2;
|
||||
t2.a = s2.data();
|
||||
t2.na = (int64_t)s2.size();
|
||||
g2.advance(r, t2);
|
||||
}
|
||||
CHECK(g2.expect(r, 0, 4) > 2.9); /* 6 / (6 + 2) a position */
|
||||
CHECK(g2.pays(g2.expect(r, 0, 4), 4, 1, 1.0));
|
||||
}
|
||||
|
||||
/* What a verify costs: the measured step time at the step's rows with the drafts against without
|
||||
* them; between timed row counts a line, outside them the nearest scaled by the prior's shape. */
|
||||
TEST(the_lookup_gate_prices_a_verify_by_measured_step_times) {
|
||||
LookupGate g;
|
||||
g.configure(4);
|
||||
/* Untimed: the prior shape, 6% a row, so a whole window pays at a quarter of a token. */
|
||||
CHECK_NEAR(g.step_cost(5) / g.step_cost(1), 1.0 + 4 * LookupGate::kRowCost, 1e-9);
|
||||
CHECK(g.pays(0.3, 4, 1, 1.0));
|
||||
CHECK(!g.pays(0.2, 4, 1, 1.0));
|
||||
CHECK(!g.pays(0.0, 4, 1, 1.0));
|
||||
|
||||
/* One row timed: a verify is priced off it on the prior's shape. */
|
||||
for (int i = 0; i < 8; ++i) g.step_time(1, 10.0);
|
||||
CHECK_EQ(g.cost_samples(1), (int64_t)8);
|
||||
CHECK_NEAR(g.step_cost(5), 10.0 * (1.0 + 4 * LookupGate::kRowCost), 1e-9);
|
||||
|
||||
/* Both timed: 2.5x for four drafts (a MoE reading each row's experts). 1 + E must beat it. */
|
||||
for (int i = 0; i < 8; ++i) g.step_time(5, 25.0);
|
||||
CHECK_NEAR(g.step_cost(5), 25.0, 1e-9);
|
||||
CHECK_NEAR(g.step_cost(3), 17.5, 1e-9); /* between: the line */
|
||||
CHECK(!g.pays(1.4, 4, 1, 1.0));
|
||||
CHECK(g.pays(1.6, 4, 1, 1.0));
|
||||
/* Past the last timed count: the prior's shape from there. */
|
||||
CHECK_NEAR(g.step_cost(9), 25.0 * (1.0 + 8 * LookupGate::kRowCost) /
|
||||
(1.0 + 4 * LookupGate::kRowCost), 1e-9);
|
||||
/* In a step of eight rows already expecting eight tokens, the same drafts weigh against the
|
||||
* whole step: marginal cost over the step's rate. */
|
||||
for (int i = 0; i < 8; ++i) g.step_time(8, 30.0);
|
||||
for (int i = 0; i < 8; ++i) g.step_time(12, 36.0);
|
||||
CHECK(!g.pays(1.5, 4, 8, 8.0)); /* 1.5 / 8 < 36 / 30 - 1 */
|
||||
CHECK(g.pays(1.7, 4, 8, 8.0));
|
||||
|
||||
/* A step that waited on something else counts as at most four times the mean. */
|
||||
g.step_time(1, 1000.0);
|
||||
CHECK(g.step_cost(1) < 10.0 + 40.0 / 8.0);
|
||||
/* Junk is ignored. */
|
||||
g.step_time(0, 5.0);
|
||||
g.step_time(2, -1.0);
|
||||
CHECK_EQ(g.cost_samples(2), (int64_t)0);
|
||||
/* Wide steps share keys in bands of 32 rows past 128. */
|
||||
g.step_time(200, 80.0);
|
||||
g.step_time(210, 90.0);
|
||||
CHECK_EQ(g.cost_samples(205), (int64_t)2);
|
||||
}
|
||||
|
||||
/* The index says how far the match behind a proposal runs back, up to the cap. */
|
||||
TEST(prompt_lookup_reports_how_far_its_match_runs_back) {
|
||||
NgramIndex ix;
|
||||
/* 1 2 3 4 5 6 7 8 9 | 30 31 | 5 6 7 8 9: the last four match 5 6 7 8 9 at its first place,
|
||||
* which runs five back; 30 31 then follows. */
|
||||
std::vector<int32_t> s = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 30, 31, 5, 6, 7, 8, 9 };
|
||||
TokenSeq ts;
|
||||
ts.a = s.data();
|
||||
ts.na = (int64_t)s.size();
|
||||
int32_t out[4];
|
||||
int m = -1;
|
||||
CHECK_EQ(ix.propose(ts, 2, 4, 4, out, &m, 8), 4);
|
||||
CHECK_EQ(out[0], 30);
|
||||
CHECK_EQ(m, 5);
|
||||
/* A copy that runs back further is cut at the cap. */
|
||||
std::vector<int32_t> c;
|
||||
for (int i = 0; i < 20; ++i) c.push_back(100 + i);
|
||||
for (int i = 0; i < 12; ++i) c.push_back(100 + i);
|
||||
TokenSeq tc;
|
||||
tc.a = c.data();
|
||||
tc.na = (int64_t)c.size();
|
||||
NgramIndex ic;
|
||||
CHECK_EQ(ic.propose(tc, 2, 4, 4, out, &m, 8), 4);
|
||||
CHECK_EQ(out[0], 112);
|
||||
CHECK_EQ(m, 8);
|
||||
/* Without the out-parameter nothing changes. */
|
||||
NgramIndex id;
|
||||
CHECK_EQ(id.propose(tc, 2, 4, 4, out), 4);
|
||||
}
|
||||
|
||||
namespace {
|
||||
/* A lookup-only deployment (no draft head) at window `depth`, the fixed window (the default). */
|
||||
void lookup_fixture(Fixture& f, int depth) {
|
||||
f.build(/*blocks=*/4096, /*states=*/64);
|
||||
f.cfg.n_spec = depth;
|
||||
f.prog.drafter.kind = RAD_DRAFT_NONE;
|
||||
f.prog.drafter.depth = 0;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
/* Prompt lookup alone, the gate on by default. Text that copies a stretch it has seen drafts at
|
||||
* once and keeps drafting while its drafts come true; text that only repeats bigrams it has seen,
|
||||
* whose proposals do not come true, is never verified, yet every proposal is still scored; and a
|
||||
* reply that turns from the second kind into the first drafts as soon as it does. The text is
|
||||
* the same either way: the emitted token is the fixture's, whatever was verified. */
|
||||
TEST(prompt_lookup_alone_holds_back_proposals_that_do_not_pay) {
|
||||
Fixture f;
|
||||
lookup_fixture(f, 4);
|
||||
std::string log = info_of([&]() { CHECK_OK(f.start()); });
|
||||
CHECK(log.find("prompt lookup: a proposal is verified when") != std::string::npos);
|
||||
CHECK(!f.sch.draft().adaptive());
|
||||
|
||||
/* A copy: an 8-token period, 160 tokens of it; the reply goes on with it. */
|
||||
auto r = std::make_unique<Request>();
|
||||
r->id = f.next_id++;
|
||||
r->max_tokens = 2000;
|
||||
r->prompt.resize(160);
|
||||
for (int i = 0; i < 160; ++i) r->prompt[(size_t)i] = 100 + i % 8;
|
||||
CHECK_OK(f.sch.add(std::move(r)));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
f.commit_plain(100);
|
||||
f.sch.step_finished(); /* a prefill step: not a sample */
|
||||
for (int64_t rows = 1; rows <= 5; ++rows)
|
||||
CHECK_EQ(f.sch.lookup_gate().cost_samples(rows), (int64_t)0);
|
||||
int64_t pos = 161; /* the next token's place in the period */
|
||||
auto truth = [&](int64_t p) { return (int32_t)(100 + p % 8); };
|
||||
int verified = 0;
|
||||
for (int t = 0; t < 24; ++t) {
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
const StepEntry& e = f.sch.plan().e[0];
|
||||
int32_t a = 0;
|
||||
if (e.n_spec > 0) {
|
||||
CHECK(e.ngram);
|
||||
++verified;
|
||||
while (a < e.n_spec && e.draft[a] == truth(pos + a)) ++a;
|
||||
}
|
||||
CHECK_OK(f.commit(truth(pos + a), 0, [a](const StepEntry&) { return a; }));
|
||||
f.sch.step_finished(); /* a decode step of 1 + 4 rows, timed */
|
||||
pos += a + 1;
|
||||
}
|
||||
CHECK_EQ(verified, 24);
|
||||
CHECK_EQ(f.sch.lookup_gate().cost_samples(5), (int64_t)24);
|
||||
SchedMetrics m = f.sch.metrics();
|
||||
CHECK_EQ(m.lookup_held, (int64_t)0);
|
||||
CHECK_EQ(m.lookup_proposals, (int64_t)24);
|
||||
CHECK_EQ(m.draft_accepted, m.draft_tokens);
|
||||
|
||||
/* Recurring bigrams: the prompt holds every pair of the reply 200, 201, 202, ... followed by
|
||||
* a 50, so after each token the lookup proposes 50 on a two-token match, and it never comes
|
||||
* true. */
|
||||
Fixture g;
|
||||
lookup_fixture(g, 4);
|
||||
CHECK_OK(g.start());
|
||||
auto q = std::make_unique<Request>();
|
||||
q->id = g.next_id++;
|
||||
q->max_tokens = 2000;
|
||||
for (int i = 0; i < 60; ++i) {
|
||||
q->prompt.push_back(200 + i);
|
||||
q->prompt.push_back(201 + i);
|
||||
q->prompt.push_back(50);
|
||||
}
|
||||
q->prompt.push_back(199);
|
||||
CHECK_OK(g.sch.add(std::move(q)));
|
||||
REQUIRE(g.sch.step() != nullptr);
|
||||
g.commit_plain(200);
|
||||
int proposals = 0;
|
||||
for (int t = 0; t < 40; ++t) {
|
||||
REQUIRE(g.sch.step() != nullptr);
|
||||
CHECK_EQ(g.sch.plan().e[0].n_spec, 0); /* held, every one */
|
||||
g.commit_plain(201 + t);
|
||||
++proposals;
|
||||
}
|
||||
m = g.sch.metrics();
|
||||
CHECK_EQ(m.draft_tokens, (int64_t)0);
|
||||
CHECK(m.lookup_proposals >= proposals - 1);
|
||||
CHECK_EQ(m.lookup_held, m.lookup_proposals);
|
||||
CHECK_EQ(m.lookup_held_drafts, 4 * m.lookup_held);
|
||||
CHECK_EQ(m.lookup_held_hits, (int64_t)0);
|
||||
|
||||
/* The reply now copies its own stretch 201.. from the start: a long match, drafted at once. */
|
||||
std::vector<int32_t> copy;
|
||||
for (int i = 0; i < 40; ++i) copy.push_back(200 + i);
|
||||
size_t k = 0;
|
||||
int drafted_steps = 0, steps = 0;
|
||||
while (k < copy.size() && steps < 40) {
|
||||
REQUIRE(g.sch.step() != nullptr);
|
||||
const StepEntry& e = g.sch.plan().e[0];
|
||||
int32_t a = 0;
|
||||
if (e.n_spec > 0) {
|
||||
++drafted_steps;
|
||||
while (a < e.n_spec && k + (size_t)a < copy.size() &&
|
||||
e.draft[a] == copy[k + (size_t)a])
|
||||
++a;
|
||||
}
|
||||
CHECK_OK(g.commit(copy[std::min(k + (size_t)a, copy.size() - 1)], 0,
|
||||
[a](const StepEntry&) { return a; }));
|
||||
k += (size_t)a + 1;
|
||||
++steps;
|
||||
}
|
||||
CHECK(drafted_steps > 0);
|
||||
CHECK(g.sch.metrics().draft_accepted > 0);
|
||||
}
|
||||
|
||||
namespace {
|
||||
/* Prompt lookup by scanning: the longest pattern first, its latest earlier occurrence, the
|
||||
* proposal continued past the end with its own tokens. The index must answer exactly this. */
|
||||
int scan_propose(const std::vector<int32_t>& s, int nmin, int nmax, int depth, int32_t* out) {
|
||||
const int64_t L = (int64_t)s.size();
|
||||
if (L < 2) return 0;
|
||||
for (int n = (int)std::min<int64_t>(nmax, L - 1); n >= nmin; --n)
|
||||
for (int64_t e = L - 2; e >= n - 1; --e) {
|
||||
bool ok = true;
|
||||
for (int k = 0; k < n && ok; ++k) ok = s[(size_t)(e - k)] == s[(size_t)(L - 1 - k)];
|
||||
if (!ok) continue;
|
||||
for (int d = 0; d < depth; ++d) {
|
||||
const int64_t i = e + 1 + d;
|
||||
out[d] = i < L ? s[(size_t)i] : out[i - L];
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
/* The index against the scan, over sequences grown a few tokens at a time across the prompt /
|
||||
* output seam, on alphabets small enough to repeat often and large enough to miss, at several
|
||||
* pattern ranges; then a change of range and a shorter sequence, which rebuild it. */
|
||||
TEST(prompt_lookup_index_answers_what_a_scan_answers) {
|
||||
uint32_t rng = 12345;
|
||||
auto next = [&](uint32_t mod) { rng = rng * 1664525u + 1013904223u; return (rng >> 8) % mod; };
|
||||
const int kRanges[][2] = { { 1, 1 }, { 1, 4 }, { 2, 4 }, { 3, 7 }, { 15, 15 } };
|
||||
int64_t hits = 0, misses = 0;
|
||||
for (const uint32_t alphabet : { 2u, 5u, 40u, 3000u }) {
|
||||
for (const auto& rg : kRanges) {
|
||||
std::vector<int32_t> seq;
|
||||
const int64_t np = 50 + next(200);
|
||||
NgramIndex ix;
|
||||
ix.reset(7);
|
||||
while (seq.size() < 1500) {
|
||||
const int add = 1 + (int)next(6);
|
||||
for (int k = 0; k < add; ++k) {
|
||||
/* Runs and copies as well as noise, so long patterns recur. */
|
||||
if (seq.size() > 20 && next(4) == 0) {
|
||||
const size_t from = next((uint32_t)seq.size() - 10);
|
||||
for (int c = 0; c < 6; ++c) seq.push_back(seq[from + (size_t)c]);
|
||||
} else {
|
||||
seq.push_back((int32_t)next(alphabet));
|
||||
}
|
||||
}
|
||||
TokenSeq ts;
|
||||
ts.a = seq.data();
|
||||
ts.na = std::min<int64_t>(np, (int64_t)seq.size());
|
||||
ts.b = seq.data() + ts.na;
|
||||
ts.nb = (int64_t)seq.size() - ts.na;
|
||||
int32_t want[8] = {}, got[8] = {};
|
||||
const int depth = 1 + (int)next(8);
|
||||
const int w = scan_propose(seq, rg[0], rg[1], depth, want);
|
||||
const int g = ix.propose(ts, rg[0], rg[1], depth, got);
|
||||
CHECK_EQ(g, w);
|
||||
if (g == w) CHECK(std::memcmp(got, want, sizeof(int32_t) * (size_t)w) == 0);
|
||||
(w ? hits : misses) += 1;
|
||||
}
|
||||
CHECK_EQ(ix.indexed(), (int64_t)seq.size() - 1);
|
||||
CHECK(ix.bytes() <= (size_t)(16 * (rg[1] - rg[0] + 1)) * seq.size() + 8192);
|
||||
}
|
||||
}
|
||||
CHECK(hits > 100);
|
||||
CHECK(misses > 100);
|
||||
|
||||
/* Another range, then a shorter sequence (another request in the slot): both rebuild. */
|
||||
std::vector<int32_t> a = { 1, 2, 3, 9, 1, 2, 3 };
|
||||
TokenSeq ta{ a.data(), (int64_t)a.size(), nullptr, 0 };
|
||||
NgramIndex ix;
|
||||
int32_t out[4] = {};
|
||||
CHECK_EQ(ix.propose(ta, 2, 4, 2, out), 2);
|
||||
CHECK_EQ(out[0], 9);
|
||||
CHECK_EQ(out[1], 1);
|
||||
CHECK_EQ(ix.propose(ta, 4, 4, 2, out), 0); /* no 4-gram recurs */
|
||||
std::vector<int32_t> b = { 5, 6, 5 };
|
||||
TokenSeq tb{ b.data(), (int64_t)b.size(), nullptr, 0 };
|
||||
CHECK_EQ(ix.propose(tb, 1, 4, 3, out), 3); /* period 2, continued: 6 5 6 */
|
||||
CHECK_EQ(out[0], 6);
|
||||
CHECK_EQ(out[1], 5);
|
||||
CHECK_EQ(out[2], 6);
|
||||
}
|
||||
|
||||
/* A lookup costs the n-grams the last commit added, not a pass over the context: over a 400k-token
|
||||
* prompt that never repeats a 2-gram, the first lookup indexes it once and the next thousand
|
||||
* lookups, a token apart, together take a small fraction of that. */
|
||||
TEST(prompt_lookup_does_not_scan_the_context) {
|
||||
std::vector<int32_t> seq;
|
||||
seq.reserve(401000);
|
||||
for (int i = 0; i < 400000; ++i) seq.push_back(i); /* every token distinct: always a miss */
|
||||
NgramIndex ix;
|
||||
int32_t out[8];
|
||||
TokenSeq ts{ seq.data(), (int64_t)seq.size(), nullptr, 0 };
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
CHECK_EQ(ix.propose(ts, 2, 4, 8, out), 0);
|
||||
const auto t1 = std::chrono::steady_clock::now();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
seq.push_back(1000000 + i);
|
||||
TokenSeq t2{ seq.data(), (int64_t)seq.size(), nullptr, 0 };
|
||||
CHECK_EQ(ix.propose(t2, 2, 4, 8, out), 0);
|
||||
}
|
||||
const auto t2 = std::chrono::steady_clock::now();
|
||||
const double build = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
||||
const double steps = std::chrono::duration<double, std::milli>(t2 - t1).count();
|
||||
fprintf(stderr, " index 400k tokens: %.1f ms; 1000 lookups after: %.3f ms (%.1f MiB)\n", build,
|
||||
steps, (double)ix.bytes() / 1048576.0);
|
||||
CHECK(steps < build);
|
||||
CHECK_EQ(ix.indexed(), (int64_t)seq.size() - 1);
|
||||
}
|
||||
|
||||
/* The scheduler's half of the cost: a decode step that verified drafts is timed at its deepest
|
||||
* window and row count, and the next cycle's head depth reads it. Measured dear past one round at
|
||||
* two rows, both windows (and the rounds) come down to one. */
|
||||
@@ -5843,46 +6294,6 @@ TEST(a_request_finishing_under_a_step_issued_ahead_keeps_its_blocks_for_that_ste
|
||||
CHECK_EQ((int)f.sch.find(a)->output.size(), 2); /* the step ahead emitted nothing */
|
||||
}
|
||||
|
||||
/* A SCORED row (Request::token_scores) the step in flight brings to max_tokens gets no step issued
|
||||
* ahead: it would be a forward pass for a request that finishes at the commit. The step after the
|
||||
* commit is planned as usual, and a decode that is not scored keeps its step of overlap. */
|
||||
TEST(no_step_is_issued_ahead_for_a_scored_request_the_step_in_flight_finishes) {
|
||||
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);
|
||||
r->speculation_fixed = true;
|
||||
r->token_scores = std::make_shared<TokenScores>();
|
||||
r->token_scores->tokens = { 1, 2 };
|
||||
const uint64_t a = f.add(std::move(r));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
f.commit_plain();
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
CHECK(f.sch.step_next() == nullptr);
|
||||
f.commit_plain(); /* the second token: finished */
|
||||
CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Finished);
|
||||
CHECK_EQ(f.kv.free_blocks(0), before);
|
||||
CHECK_EQ((int)f.sch.find(a)->output.size(), 2);
|
||||
CHECK(f.sch.step() == nullptr);
|
||||
}
|
||||
|
||||
/* The same row without token_scores is an ordinary decode: the step ahead is issued, as upstream. */
|
||||
TEST(a_non_scored_row_keeps_the_step_issued_ahead) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/20, /*states=*/64);
|
||||
CHECK_OK(f.start());
|
||||
auto r = f.request(40, /*max_tokens=*/2);
|
||||
r->speculation_fixed = true;
|
||||
const uint64_t a = f.add(std::move(r));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
f.commit_plain();
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
CHECK(f.sch.step_next() != nullptr);
|
||||
f.commit_plain();
|
||||
CHECK_EQ((int)f.sch.find(a)->output.size(), 2);
|
||||
}
|
||||
|
||||
/* ================================================================== terminal paths */
|
||||
/* A lone request that outgrows the whole pool fails with the reason the client sees and gives its
|
||||
* blocks back; nothing is preempted, since there is nobody to preempt and preempting itself would
|
||||
|
||||
Reference in new issue
Block a user