Archived
abi, sched: placeholder runs read both ways
An encoder states that the language model reads a picture's or video frame's placeholders both ways (RadEncoderDecl::bidirectional). The scheduler then ends no prefill chunk inside such a run, and a trunk pass names its runs and the keys their rows see (RadBatch::n_bidi). Fields appended; such a pass is not recorded.
This commit is contained in:
1 parent
81be3cb63d
commit
da7b911c64
10 files changed
+246
-12
No files matched your search
@@ -1339,6 +1339,11 @@ int Builder::declare_encoder(const RadEncoderDecl* d) {
|
||||
(int)d->one_grid);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (d->bidirectional != 0 && (d->bidirectional != 1 || audio)) {
|
||||
err(RAD_E_INVAL, "declare_encoder '%s': bidirectional %d -- 0, or 1 for a picture and video "
|
||||
"encoder", nm, (int)d->bidirectional);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
slot = *d;
|
||||
slot_name = nm;
|
||||
slot.name = nullptr; /* the plugin's storage; the name is the copy */
|
||||
|
||||
@@ -951,15 +951,16 @@ int Ctx::lane_join(int from, int to) {
|
||||
* geometries fail closed via the pass-key contract, while Qwen keys its full-group maximum in the
|
||||
* architecture callback. `agen_b`, `vgen_b` and `sgen_b` are set only on generator passes, which are
|
||||
* never recorded (below). */
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 432,
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 472,
|
||||
"RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on");
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 88,
|
||||
"RadKVGroupBatch changed: check that Ctx::pass_key reads every new field");
|
||||
|
||||
uint64_t Ctx::pass_key(const RadBatch* b) const {
|
||||
/* Not recordable: encoder passes are shaped by their media, and passes carrying encoder rows
|
||||
* issue the scatter only when they have some. */
|
||||
if (b->enc || b->n_mm_rows > 0) return 0;
|
||||
* issue the scatter only when they have some (and the second attention over a run read both
|
||||
* ways, n_bidi, only on such a pass). */
|
||||
if (b->enc || b->n_mm_rows > 0 || b->n_bidi > 0) return 0;
|
||||
/* Legacy architecture plugins have no pass-key callback. Only refuse them when the resolved
|
||||
* program contains a launcher explicitly marked as using live prefill splits, and this batch
|
||||
* can issue a prefill, mixed, speculative-verify or draft query through it. */
|
||||
|
||||
@@ -135,6 +135,7 @@ void BatchBuilder::carve(Set& s, char* h) {
|
||||
* moves a rotary position off the token index. */
|
||||
pair(&s.h_rope, &s.d_rope, mm_on_ ? 3 * T : 0);
|
||||
pair(&s.h_mmrow, &s.d_mmrow, mm_on_ ? T : 0);
|
||||
pair(&s.h_bidi, &s.d_bidi, bidi_on_ ? 4 * T : 0);
|
||||
/* Each row's adapter slot, carved only when the engine serves adapters, so an engine without
|
||||
* them lays the slab out as if the array did not exist. */
|
||||
pair(&s.h_lora, &s.d_lora, lora_on_ ? T : 0);
|
||||
@@ -481,6 +482,7 @@ int BatchBuilder::layout(Program& prog, const Config& cfg) {
|
||||
});
|
||||
|
||||
mm_on_ = prog.encoder.modalities != 0 || prog.audio_encoder.modalities != 0;
|
||||
bidi_on_ = prog.encoder.modalities != 0 && prog.encoder.bidirectional != 0;
|
||||
lora_on_ = cfg.lora.enabled();
|
||||
pw_cap_ = prog.plugin_words;
|
||||
return RAD_OK;
|
||||
@@ -945,6 +947,7 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
int64_t n_out = 0, n_dout = 0;
|
||||
int32_t rope_mixed = 0;
|
||||
int64_t n_mm = 0;
|
||||
int64_t n_bidi = 0;
|
||||
bool lora_rows = false;
|
||||
int32_t cvec_rows = 0;
|
||||
int64_t n_pw = 0;
|
||||
@@ -1034,6 +1037,23 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
n_mm += got;
|
||||
}
|
||||
}
|
||||
/* The part of each picture's or video frame's run inside this chunk, read both ways
|
||||
* (RadBatch::n_bidi), on a trunk pass: its rows see the keys up to the run's end, or the
|
||||
* chunk's where a run longer than any step was cut (Scheduler::bidi_fit). */
|
||||
if (bidi_on_ && plan.draft_pass == 0 && shift == 0 && e.req && e.req->media) {
|
||||
const mm::PromptMedia& pm = *e.req->media;
|
||||
const int64_t c0 = e.ctx_len, c1 = c0 + e.n_tokens, T = max_tok_;
|
||||
for (const mm::RopeLayout::Run& run : pm.rope.runs) {
|
||||
const int64_t a = std::max<int64_t>(run.tok, c0);
|
||||
const int64_t b = std::min<int64_t>((int64_t)run.tok + run.n, c1);
|
||||
if (a >= b || pm.items[(size_t)run.item].item->kind == mm::Kind::Audio) continue;
|
||||
s.h_bidi[n_bidi] = (int32_t)b;
|
||||
s.h_bidi[T + n_bidi] = (int32_t)(k + a - c0);
|
||||
s.h_bidi[2 * T + n_bidi] = (int32_t)(b - a);
|
||||
s.h_bidi[3 * T + n_bidi] = (int32_t)i;
|
||||
++n_bidi;
|
||||
}
|
||||
}
|
||||
if (e.ckpt_tok >= 0 && e.ckpt_slot >= 0) {
|
||||
/* checkpoint_tok indexes this step's tokens: the sequence's base plus the offset the
|
||||
* scheduler chose, the last token of a checkpoint interval (§7.3). */
|
||||
@@ -1248,6 +1268,11 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
B.rope_mixed = rope_mixed;
|
||||
B.n_mm_rows = n_mm;
|
||||
B.mm_rows = n_mm ? on_rank(base, b0, s.d_mmrow) : nullptr;
|
||||
B.n_bidi = n_bidi;
|
||||
B.bidi_used = n_bidi ? on_rank(base, b0, s.d_bidi) : nullptr;
|
||||
B.bidi_row = n_bidi ? s.h_bidi + max_tok_ : nullptr;
|
||||
B.bidi_n = n_bidi ? s.h_bidi + 2 * max_tok_ : nullptr;
|
||||
B.bidi_seq = n_bidi ? s.h_bidi + 3 * max_tok_ : nullptr;
|
||||
B.mm_embd = n_mm ? mm_dev_[r] : nullptr;
|
||||
/* Null on a step with no adapter row: the core then issues no adapter op, and the step
|
||||
* runs the same pass as an engine serving no adapters. */
|
||||
|
||||
@@ -226,6 +226,10 @@ private:
|
||||
* mm_rows): carved only when the program declared an encoder, empty otherwise. */
|
||||
int32_t *h_rope = nullptr, *d_rope = nullptr;
|
||||
int32_t *h_mmrow = nullptr, *d_mmrow = nullptr;
|
||||
/* [4][max_tok] the runs read both ways (RadBatch::n_bidi): their key counts (the one plane
|
||||
* the device reads), first rows, rows and sequences; carved only when the program's
|
||||
* encoder asks for them (RadEncoderDecl::bidirectional). */
|
||||
int32_t *h_bidi = nullptr, *d_bidi = nullptr;
|
||||
/* [n_tok] each row's runtime LoRA adapter slot (RadBatch::lora_slot): carved only when the
|
||||
* engine serves adapters, empty otherwise. */
|
||||
int32_t *h_lora = nullptr, *d_lora = nullptr;
|
||||
@@ -345,6 +349,7 @@ private:
|
||||
* (see RadBatch::mm_embd). The staging is refilled only after its last upload has run
|
||||
* (`mm_ev_`). */
|
||||
bool mm_on_ = false;
|
||||
bool bidi_on_ = false; /* RadEncoderDecl::bidirectional */
|
||||
/* The engine serves runtime LoRA adapters (Config::lora): the batch carries each row's slot. */
|
||||
bool lora_on_ = false;
|
||||
/* Program::plugin_words: the words a sequence may carry, 0 for none. */
|
||||
|
||||
@@ -975,7 +975,7 @@ int64_t Scheduler::chunk_end(const Request& r, int64_t n_target) const {
|
||||
* so a chunk clamped to it is already aligned; in the other order a chunk past the checkpoint
|
||||
* would be rounded down and miss the boundary. */
|
||||
int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budget,
|
||||
int64_t* checkpoint_pos, int64_t keep_at) const {
|
||||
int64_t* checkpoint_pos, int64_t keep_at, const Request* r) const {
|
||||
if (checkpoint_pos) *checkpoint_pos = -1;
|
||||
int64_t remaining = n_target - n_computed;
|
||||
if (remaining <= 0 || budget <= 0) return 0;
|
||||
@@ -1100,9 +1100,48 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
|
||||
if (checkpoint_pos && *checkpoint_pos > aligned) *checkpoint_pos = -1;
|
||||
}
|
||||
}
|
||||
if (r) {
|
||||
const int64_t fit = bidi_fit(*r, n_computed, n_target, want, budget);
|
||||
/* A trimmed chunk keeps a window snapshot it still carries (as above); a grown one drops it. */
|
||||
if (fit != want && checkpoint_pos && (fit > want || *checkpoint_pos > n_computed + fit))
|
||||
*checkpoint_pos = -1;
|
||||
want = fit;
|
||||
}
|
||||
return want;
|
||||
}
|
||||
|
||||
/* A chunk that ended inside a run read both ways would give the run's rows of this chunk keys the
|
||||
* next chunk writes. So it ends before the run, on a quantum (and before every run that lands in,
|
||||
* as a video's frames do); a chunk that starts at or inside the run (nothing fits before it) runs
|
||||
* to the run's end, rounded up to a quantum unless the sequence ends first, past every run that
|
||||
* lands in, or waits for a step that holds that. A run no step holds is cut where the chunk falls,
|
||||
* and its rows see the run up to the cut (RadBatch::bidi_used). */
|
||||
int64_t Scheduler::bidi_fit(const Request& r, int64_t n_computed, int64_t n_target, int64_t want,
|
||||
int64_t budget) const {
|
||||
if (!prog_ || !prog_->encoder.bidirectional || !r.media || want <= 0) return want;
|
||||
const mm::PromptMedia& pm = *r.media;
|
||||
const int64_t Q = std::max<int64_t>(geo_.quantum, 1);
|
||||
/* The run read both ways that a chunk ending at `end` would cut, or null. */
|
||||
auto cut = [&](int64_t end) -> const mm::RopeLayout::Run* {
|
||||
if (end >= n_target) return nullptr;
|
||||
const mm::RopeLayout::Run* run = pm.rope.run_at(end);
|
||||
if (!run || run->tok == end || pm.items[(size_t)run->item].item->kind == mm::Kind::Audio)
|
||||
return nullptr;
|
||||
return run;
|
||||
};
|
||||
int64_t end = n_computed + want;
|
||||
for (const mm::RopeLayout::Run* run = cut(end); run; run = cut(end)) {
|
||||
const int64_t before = (int64_t)run->tok / Q * Q;
|
||||
if (before <= n_computed) break;
|
||||
end = before;
|
||||
}
|
||||
if (!cut(end)) return end - n_computed;
|
||||
for (const mm::RopeLayout::Run* run = cut(end); run; run = cut(end))
|
||||
end = std::min<int64_t>(n_target, ((int64_t)run->tok + run->n + Q - 1) / Q * Q);
|
||||
if (end - n_computed <= budget) return end - n_computed;
|
||||
return end - n_computed > cfg_.max_tok ? want : 0;
|
||||
}
|
||||
|
||||
/* The tail checkpoint of a `target`-token prompt, or -1. Under --prefix-resume exact, where its
|
||||
* last chunk starts (below). Under nearest, its last quantum boundary, only past the first
|
||||
* interval (a shorter prompt is cheap to redo) and only where it gains at least a quarter
|
||||
@@ -1478,9 +1517,12 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
|
||||
* to what really has to run. */
|
||||
const int64_t cap = busy_cap(r.n_computed, target, budget, reserve, !plan_.e.empty());
|
||||
const int64_t end = chunk_end(r, target);
|
||||
want = plan_chunk(r.n_computed, end, cap, &ckpt, r.checkpoint_at);
|
||||
if (cap < budget && want > 0)
|
||||
admit_full_ = plan_chunk(r.n_computed, end, budget, nullptr, r.checkpoint_at);
|
||||
want = plan_chunk(r.n_computed, end, cap, &ckpt, r.checkpoint_at, &r);
|
||||
/* A run read both ways (bidi_fit) the share cannot hold takes the step instead. */
|
||||
if (want <= 0 && cap < budget && prog_->encoder.bidirectional)
|
||||
want = plan_chunk(r.n_computed, end, budget, &ckpt, r.checkpoint_at, &r);
|
||||
else if (cap < budget && want > 0)
|
||||
admit_full_ = plan_chunk(r.n_computed, end, budget, nullptr, r.checkpoint_at, &r);
|
||||
}
|
||||
int rc = RAD_OK;
|
||||
if (want <= 0) rc = RAD_E_FULL;
|
||||
@@ -1829,10 +1871,13 @@ const RadBatch* Scheduler::step() {
|
||||
const int64_t cap = busy_cap(r.n_computed, target, budget, reserve, !plan_.e.empty(),
|
||||
/*rows_only=*/shares == 0);
|
||||
const int64_t end = chunk_end(r, target);
|
||||
want = plan_chunk(r.n_computed, end, cap, &ckpt, r.checkpoint_at);
|
||||
if (cap < budget && want > 0 && capped_at < 0) {
|
||||
want = plan_chunk(r.n_computed, end, cap, &ckpt, r.checkpoint_at, &r);
|
||||
/* A run read both ways (bidi_fit) the share cannot hold takes the step instead. */
|
||||
if (want <= 0 && cap < budget && prog_->encoder.bidirectional)
|
||||
want = plan_chunk(r.n_computed, end, budget, &ckpt, r.checkpoint_at, &r);
|
||||
else if (cap < budget && want > 0 && capped_at < 0) {
|
||||
const int64_t full = plan_chunk(r.n_computed, end, budget, nullptr,
|
||||
r.checkpoint_at);
|
||||
r.checkpoint_at, &r);
|
||||
if (want < full) { capped_at = (int32_t)plan_.e.size(); capped_full = full; }
|
||||
}
|
||||
/* A prefill with no room left does not end the walk: it takes nothing this step, but
|
||||
@@ -2090,7 +2135,7 @@ const RadBatch* Scheduler::step() {
|
||||
if (cfg_.busy_prefill_chunk > 0) limit = std::min(limit, std::max(had, cfg_.busy_prefill_chunk));
|
||||
int64_t ckpt = -1;
|
||||
const int64_t grown = e.ckpt_slot < 0 && limit > had
|
||||
? plan_chunk(r.n_computed, chunk_end(r, s.seq_len()), limit, &ckpt, r.checkpoint_at)
|
||||
? plan_chunk(r.n_computed, chunk_end(r, s.seq_len()), limit, &ckpt, r.checkpoint_at, &r)
|
||||
: had;
|
||||
if (grown > had && kv_->ensure_from(r.id, r.n_computed + grown, r.n_computed) >= 0) {
|
||||
e.n_tokens = (int32_t)grown;
|
||||
|
||||
@@ -470,7 +470,15 @@ public:
|
||||
* token budget, and the checkpoint position it will land on (-1 for none). `keep_at` is the
|
||||
* request's own checkpoint (Request::checkpoint_at), 0 for none. */
|
||||
int64_t plan_chunk(int64_t n_computed, int64_t n_target, int64_t budget,
|
||||
int64_t* checkpoint_pos, int64_t keep_at = 0) const;
|
||||
int64_t* checkpoint_pos, int64_t keep_at = 0,
|
||||
const Request* r = nullptr) const;
|
||||
/* Visible for tests: `want` tokens of `r` from `n_computed` cut so the chunk ends inside no run
|
||||
* the language model reads both ways (RadEncoderDecl::bidirectional): before the run on a
|
||||
* quantum, or at its end when the chunk starts in it and `budget` holds that, else 0. A run
|
||||
* longer than any step keeps the cut where it falls. `want` unchanged for a program with no
|
||||
* such runs. */
|
||||
int64_t bidi_fit(const Request& r, int64_t n_computed, int64_t n_target, int64_t want,
|
||||
int64_t budget) const;
|
||||
|
||||
/* The smallest step the prefill stager overlaps, in tokens (0 = no stager), set once by the
|
||||
* engine after init. --busy-prefill-chunk keeps a held-back chunk at or above it (busy_cap).
|
||||
|
||||
Reference in new issue
Block a user