diff --git a/core/engine.cpp b/core/engine.cpp index f3532071..719aad7b 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -354,7 +354,7 @@ int Engine::issue_ahead(const RadBatch** out) { for (int i = 0; i < serial_rows_ && serial_done_ > 0; ++i) sched_.expect_draft(serial_row_[(size_t)i].seq, serial_done_); for (int i = 0; i < block_rows_; ++i) - sched_.expect_draft(block_row_[(size_t)i].seq, drafter_.depth); + sched_.expect_draft(block_row_[(size_t)i].seq, block_steps_); const RadBatch* nb = sched_.step_next(); if (!nb) return RAD_OK; /* Its rows' tokens, positions and slots, advanced on the card behind this step's acceptance diff --git a/core/engine.h b/core/engine.h index 46524c87..ff62e4cb 100644 --- a/core/engine.h +++ b/core/engine.h @@ -341,7 +341,7 @@ private: /* The block drafter's shorter block (rad_drafter_short_block), 0 when it has none or * --draft-short-from is 0. */ int64_t drafter_short_ = 0; - bool short_on_ = false; /* the short block runs now (run_block_drafter) */ + bool short_on_ = false; /* the short block runs now (block_shape) */ /* Serial driver (spec §10): `depth` dependent passes after the step commits, each embedding * the previous proposal. MTP, EAGLE and Medusa-serial have this shape. */ @@ -417,10 +417,16 @@ private: int block_pass(const RadBatch* db, const char* what, int step); int issue_block_drafter(int step, StepOut& so); int finish_block_drafter(const StepOut& so); + /* The block and proposal count this step's query pass runs at: the drafter's own, or its short + * block once --draft-short-from sequences decode, back to its own only two below. */ + void block_shape(int* block, int* steps); /* Handed from issue_block_drafter to finish_block_drafter and issue_ahead: the drafting rows, * in the query pass's order, and how many there are (0 when no query pass ran). */ std::vector block_row_; int block_rows_ = 0; + /* The proposals a row of the query pass took (block_shape), for finish_block_drafter and + * issue_ahead's expect_draft. */ + int block_steps_ = 0; /* Encoder driver (spec §11, core/engine_mm.cpp): encodes every media item the scheduler waits * on, in passes of whole segments, and reads it back into the item. Runs between steps, never * inside one. */ diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 02b8105d..d7051f58 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -1312,7 +1312,7 @@ int Engine::declare() { : "is a serial head, which proposes no block"); if (drafter_short_) RAD_INFO("speculation: from %lld decoding sequences on, '%s' runs its short block of %lld " - "row(s), %lld token(s), on the host's block driver", (long long)cfg_.draft_short_from, + "row(s), %lld token(s)", (long long)cfg_.draft_short_from, drafter_name_.c_str(), (long long)drafter_short_, (long long)(drafter_short_ - ((drafter_.block > 0 ? drafter_.block diff --git a/core/engine_draft.cpp b/core/engine_draft.cpp index 8ecdbd66..c1cec578 100644 --- a/core/engine_draft.cpp +++ b/core/engine_draft.cpp @@ -313,6 +313,25 @@ bool Engine::block_drafting() const { * and its proposals go into the slots behind the anchor (ADV_BTAKE): exactly the token row the * next verify reads (ADV_TRUNK), so the step issued ahead verifies them with no host round trip. * collect_step's wait covers both passes and the slots' readback. */ +void Engine::block_shape(int* block, int* steps) { + *steps = drafter_.depth; + /* The row count is the drafter's (defaulted and checked by declare_drafter). */ + *block = drafter_.block > 0 ? (int)drafter_.block : drafter_.depth + 1; + /* Its short block under load (rad_drafter_short_block): the same anchor convention, so as many + * fewer proposals as rows. The load is the step just run's, which the next step, the one these + * proposals are for, mostly repeats. On from --draft-short-from decoding sequences, off again + * only two below it, so a load wavering at the threshold does not switch the block (a new pass + * shape) every step. */ + if (drafter_short_ <= 0) return; + const int64_t n = sched_.decoded_seqs(); + if (n >= cfg_.draft_short_from) short_on_ = true; + else if (n + 2 <= cfg_.draft_short_from) short_on_ = false; + if (short_on_) { + *steps -= *block - (int)drafter_short_; + *block = (int)drafter_short_; + } +} + int Engine::issue_block_drafter(int step, StepOut& so) { block_rows_ = 0; if (!block_drafting()) return RAD_OK; @@ -321,8 +340,12 @@ int Engine::issue_block_drafter(int step, StepOut& so) { /* The sampled ids before either pass, as the serial driver reads them. */ RAD_TRY(collect_issue(so)); - const int steps = drafter_.depth; - const int block = drafter_.block > 0 ? (int)drafter_.block : drafter_.depth + 1; + /* The short block too (block_shape): its query pass is a shorter batch the card positions + * the same way, its proposals the front of each row at the drafter's pitch (ADV_BTAKE), and + * the step issued ahead verifies that many (issue_ahead). */ + int block = 0, steps = 0; + block_shape(&block, &steps); + block_steps_ = steps; block_row_.resize((size_t)cfg_.max_seqs); const int n = sched_.block_begin(block, steps, (int)drafter_.window, drafter_.mask_token, block_row_.data(), (int)block_row_.size(), /*on_card=*/true); @@ -368,7 +391,7 @@ int Engine::finish_block_drafter(const StepOut& so) { if (block_rows_ <= 0) return RAD_OK; Rank& r0 = *ranks_[0]; - const int steps = drafter_.depth; + const int steps = block_steps_; draft_out_.assign((size_t)block_rows_ * (size_t)steps, 0); for (int i = 0; i < block_rows_; ++i) { const StepSlot& S = slots_host_[block_row_[(size_t)i].card]; diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index c3f5a2a4..eb1ca04f 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -4083,7 +4083,7 @@ constexpr int kBlk = 8, kBlkDepth = 7; constexpr int32_t kBlkMask = 99; /* The host's driver: both passes built after the commit, from the committed sequence. */ -BlockRun block_reference(int steps) { +BlockRun block_reference(int steps, int blk = kBlk, int depth = kBlkDepth) { Fixture f; std::vector ids; ahead_setup(f, kBlkDepth, ids); @@ -4094,7 +4094,7 @@ BlockRun block_reference(int steps) { Scheduler::BlockRow rows[4]; for (int t = 0; t < steps; ++t) { CHECK_OK(ahead_commit(f, t)); - CHECK_EQ(f.sch.block_begin(kBlk, kBlkDepth, 0, kBlkMask, rows, 4), 3); + CHECK_EQ(f.sch.block_begin(blk, depth, 0, kBlkMask, rows, 4), 3); const RadBatch* cb = f.sch.block_context(t + 1); if (!cb) { CHECK(cb != nullptr); return run; } run.context.push_back(read_rows(cb, f.sch.plan(), f.kv)); @@ -4105,7 +4105,7 @@ BlockRun block_reference(int steps) { for (int i = 0; i < 3; ++i) { StepEntry e; e.seq = rows[i].seq; qp.e.push_back(e); } run.query.push_back(read_rows(qb, qp, f.kv)); f.sch.block_end(); - ahead_publish(f, ids, kBlkDepth, t + 1); + ahead_publish(f, ids, depth, t + 1); b = f.sch.step(); if (!b) break; run.trunk.push_back(read_rows(b, f.sch.plan(), f.kv)); @@ -4118,7 +4118,7 @@ BlockRun block_reference(int steps) { * passes built before the commit with the query rows placed by ADV_BLOCK, the proposals into the * slots (ADV_BTAKE), expect_draft and step_next with ADV_TRUNK, then the commit, block_end, the * drafts published and promote_next. */ -BlockRun block_pipelined(int steps) { +BlockRun block_pipelined(int steps, int blk = kBlk, int depth = kBlkDepth) { Fixture f; std::vector ids; ahead_setup(f, kBlkDepth, ids); @@ -4148,7 +4148,7 @@ BlockRun block_pipelined(int steps) { ++run.card_host_disagree; } - CHECK_EQ(f.sch.block_begin(kBlk, kBlkDepth, 0, kBlkMask, rows, 4, /*on_card=*/true), 3); + CHECK_EQ(f.sch.block_begin(blk, depth, 0, kBlkMask, rows, 4, /*on_card=*/true), 3); const RadBatch* cb = f.sch.block_context(t + 1); if (!cb) { CHECK(cb != nullptr); return run; } run.context.push_back(read_rows(cb, f.sch.plan(), f.kv)); @@ -4161,19 +4161,20 @@ BlockRun block_pipelined(int steps) { StepPlan qp; for (int i = 0; i < 3; ++i) { StepEntry e; e.seq = rows[i].seq; qp.e.push_back(e); } run.query.push_back(read_rows(qb, qp, f.kv)); - /* The block's proposals for step t + 1, taken behind the anchor. */ + /* The block's proposals for step t + 1, taken behind the anchor, at the drafter's pitch + * (its own block's depth): a short block fills a row's front. */ std::vector prop((size_t)3 * kBlkDepth); for (int i = 0; i < 3; ++i) - for (int j = 0; j < kBlkDepth; ++j) + for (int j = 0; j < depth; ++j) prop[(size_t)(i * kBlkDepth + j)] = ahead_draft(rows[i].seq, t + 1, j); AdvanceArgs tk = f.sch.advance_args(0); tk.mode = ADV_BTAKE; - tk.round = kBlkDepth; + tk.round = depth; tk.src = prop.data(); tk.src_pitch = kBlkDepth; tk.st = slots.data(); for (int i = 0; i < tk.n_seq; ++i) adv_row(tk, i); - for (int i = 0; i < 3; ++i) f.sch.expect_draft(rows[i].seq, kBlkDepth); + for (int i = 0; i < 3; ++i) f.sch.expect_draft(rows[i].seq, depth); const RadBatch* nb = f.sch.step_next(); if (nb) { @@ -4184,7 +4185,7 @@ BlockRun block_pipelined(int steps) { } CHECK_OK(ahead_commit(f, t)); f.sch.block_end(); - ahead_publish(f, ids, kBlkDepth, t + 1); + ahead_publish(f, ids, depth, t + 1); if (nb) { f.sch.promote_next(); b = nb; @@ -4242,6 +4243,42 @@ TEST(a_block_drafter_on_the_card_runs_the_passes_the_hosts_driver_runs) { CHECK(a.out == b.out); } +/* The same at DFlash2's short block (--draft-short-from: 4 rows, 3 proposals), which the card's driver + * runs once enough sequences decode (Engine::block_shape): the query pass of the short block, its + * proposals taken from the front of each row at the drafter's pitch, and the step issued ahead + * verifying 3 of them. */ +TEST(a_block_drafters_short_block_on_the_card_runs_the_passes_the_hosts_driver_runs) { + const int T = 24; + const BlockRun a = block_reference(T, 4, 3); + const BlockRun b = block_pipelined(T, 4, 3); + CHECK_EQ(b.ahead, T); + CHECK_EQ(b.card_host_disagree, 0); + REQUIRE_EQ(a.trunk.size(), (size_t)T + 1); + REQUIRE_EQ(a.trunk.size(), b.trunk.size()); + REQUIRE_EQ(a.context.size(), b.context.size()); + REQUIRE_EQ(a.query.size(), b.query.size()); + for (size_t t = 0; t < a.trunk.size(); ++t) { + CHECK(a.trunk[t] == b.trunk[t]); + CHECK_EQ(b.trunk[t].bad_slots, 0); + } + for (size_t t = 0; t < a.context.size(); ++t) { + BatchRows ca = a.context[t], cb = b.context[t]; + ca.tok.clear(); + cb.tok.clear(); + const bool same_c = ca == cb, same_q = a.query[t] == b.query[t]; + CHECK(same_c); + CHECK(same_q); + if (!same_c || !same_q) + std::fprintf(stderr, " step %zu: context %s, query %s\n", t, + same_c ? "same" : "differs", same_q ? "same" : "differs"); + CHECK_EQ(a.context[t].bad_slots, 0); + CHECK_EQ(b.context[t].bad_slots, 0); + CHECK_EQ(a.query[t].bad_slots, 0); + CHECK_EQ(b.query[t].bad_slots, 0); + } + CHECK(a.out == b.out); +} + /* ================================================================== the token budget per token */ /* A verify step emits up to 1 + n_spec tokens at once. The budget (max_tokens, and the context) * is applied token by token before each reaches the sink: trimming `output` afterwards would leave