From da7b911c640c8537b7af300225c26448cf75f347 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Tue, 27 Oct 2026 01:16:54 +0100 Subject: [PATCH] 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. --- abi/rad_abi_layout_rows.h | 6 ++ abi/rad_builder.h | 7 +++ abi/rad_runtime.h | 17 ++++++ core/build/rad_builder.cpp | 5 ++ core/runtime/ctx.cpp | 7 ++- core/sched/batch.cpp | 25 ++++++++ core/sched/batch.h | 5 ++ core/sched/scheduler.cpp | 61 +++++++++++++++++--- core/sched/scheduler.h | 10 +++- tests/sched_test.cpp | 115 +++++++++++++++++++++++++++++++++++++ 10 files changed, 246 insertions(+), 12 deletions(-) diff --git a/abi/rad_abi_layout_rows.h b/abi/rad_abi_layout_rows.h index 2477beee..c0ce084a 100644 --- a/abi/rad_abi_layout_rows.h +++ b/abi/rad_abi_layout_rows.h @@ -99,6 +99,11 @@ ABI_FIELD(RadBatch, plugin_cu_h) ABI_FIELD(RadBatch, sgen_b) ABI_FIELD(RadBatch, kv_used_max_h) ABI_FIELD(RadBatch, positions_h) +ABI_FIELD(RadBatch, n_bidi) +ABI_FIELD(RadBatch, bidi_row) +ABI_FIELD(RadBatch, bidi_n) +ABI_FIELD(RadBatch, bidi_seq) +ABI_FIELD(RadBatch, bidi_used) ABI_STRUCT(RadBufDecl) ABI_FIELD(RadBufDecl, dtype) ABI_FIELD(RadBufDecl, rank) @@ -317,6 +322,7 @@ ABI_FIELD(RadEncoderDecl, max_patches) ABI_FIELD(RadEncoderDecl, n_embd) ABI_FIELD(RadEncoderDecl, out) ABI_FIELD(RadEncoderDecl, one_grid) +ABI_FIELD(RadEncoderDecl, bidirectional) ABI_STRUCT(RadEncoding) ABI_FIELD(RadEncoding, scheme) ABI_FIELD(RadEncoding, transform) diff --git a/abi/rad_builder.h b/abi/rad_builder.h index 5bd81017..849b0b84 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -886,6 +886,13 @@ typedef struct RadEncoderDecl { * position tables or kernels are per geometry; the core ends a pass where the grid changes. * 0 packs segments of any grid into a pass. */ int32_t one_grid; + + /* ---- appended in ABI 32 (runs the language model reads both ways) */ + /* 1: the language model attends the placeholders of one picture or video frame to each other + * both ways (Gemma 4's use_bidirectional_attention "vision"), text before and after them + * causally. The core then ends no prefill chunk inside such a run and states each trunk pass's + * runs (RadBatch::n_bidi). 0 for an audio encoder. */ + int32_t bidirectional; } RadEncoderDecl; int rad_declare_encoder(RadBuilder* b, const RadEncoderDecl* d); diff --git a/abi/rad_runtime.h b/abi/rad_runtime.h index 865dbcb5..ffddb3a8 100644 --- a/abi/rad_runtime.h +++ b/abi/rad_runtime.h @@ -339,6 +339,23 @@ typedef struct RadBatch { /* ABI 35: host mirror of positions, for a one-sequence prefill op whose exact KV bound is the * position immediately after its first query piece. Valid through the batch's issue pass. */ const int32_t* positions_h; + /* ---- appended in ABI 35 (runs read both ways) */ + /* The runs of a trunk pass whose rows attend to each other both ways + * (RadEncoderDecl::bidirectional): a picture's or a video frame's placeholders inside one + * sequence's chunk. The pass attends them causally like every other row; the plugin attends a + * run's rows again without the causal bound, over the keys before `bidi_used` (the position + * after the run's last token), in place of that result. The scheduler ends no chunk inside such + * a run, so its keys are this pass's or, after a prefix hit, the cache's; a run longer than any + * step is cut where its chunk ends, and that chunk's rows see the run up to the cut. + * + * host: n_bidi and, per run, its first row in this pass, its rows and the index of its sequence + * in the per-sequence arrays; device: [n_bidi] the keys a run's rows see. 0 runs on every other + * pass. */ + int64_t n_bidi; + const int32_t* bidi_row; /* host */ + const int32_t* bidi_n; /* host */ + const int32_t* bidi_seq; /* host */ + const int32_t* bidi_used; /* device */ } RadBatch; /* What a generator pass carries beside the step batch's own fields, which keep their meanings: diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index 4c4cfb94..7b99f01d 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -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 */ diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index 8e6a56d0..fdb324b3 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -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. */ diff --git a/core/sched/batch.cpp b/core/sched/batch.cpp index 75c7ca4e..481fabea 100644 --- a/core/sched/batch.cpp +++ b/core/sched/batch.cpp @@ -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(run.tok, c0); + const int64_t b = std::min((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. */ diff --git a/core/sched/batch.h b/core/sched/batch.h index 52f9b8e6..e1ae678e 100644 --- a/core/sched/batch.h +++ b/core/sched/batch.h @@ -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. */ diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index bfba64f1..cc1762ee 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -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(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(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; diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index c01ead55..092e321d 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -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). diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index df5dfa90..3eec65ba 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -7085,6 +7085,121 @@ TEST(media_rows_are_staged_by_chunk_at_the_tokens_that_take_them) { } } +/* ================================================================== runs read both ways */ +/* A picture whose placeholders the model reads both ways (RadEncoderDecl::bidirectional) is never + * cut by a chunk: the chunk ends before it on the quantum (64 here, the linear group's chunk), or, + * starting at it, runs past its end, and the pass names the run's rows and the keys they see + * (RadBatch::n_bidi). Tokens 200..299 here; without the flag the first chunk would end at the + * checkpoint interval (256), inside the picture. A run longer than any step is cut where its chunk + * falls, and its rows then see the run up to the cut. An audio clip is read causally and cut + * anywhere. */ +namespace { + +std::unique_ptr with_run(Fixture& f, int n_prompt, int tok, int n, mm::Kind kind) { + auto item = std::make_shared(); + item->kind = kind; + item->n_seg = 1; + item->seg_rows = n; + item->encoded = true; + item->embd.assign((size_t)n * 4, 0); + auto req = f.request(n_prompt, 4); + req->media = std::make_shared(); + req->media->items.push_back(mm::Placed{ item, tok - 1 }); + mm::RopeLayout::Run run; + run.tok = tok; run.n = n; run.w = n; run.base = tok; run.shift = 0; run.item = 0; run.flat = true; + req->media->rope.runs.push_back(run); + return req; +} + +void media_fixture(Fixture& f, int64_t max_tok, int32_t bidi) { + f.build(/*blocks=*/4096, /*states=*/64, max_tok); + f.prog.encoder.modalities = RAD_MM_IMAGE; + f.prog.encoder.n_embd = 4; + f.prog.encoder.patch_dim = 4; + f.prog.encoder.merge = 1; + f.prog.encoder.max_patches = 1024; + f.prog.encoder.bidirectional = bidi; +} + +} /* namespace */ + +TEST(a_run_read_both_ways_is_never_cut_by_a_chunk) { + Fixture f; + media_fixture(f, 256, 1); + CHECK_OK(f.start()); + f.add(with_run(f, 400, 200, 100, mm::Kind::Image)); + const RadBatch* b = f.sch.step(); + REQUIRE(b != nullptr); + CHECK_EQ(b->n_tok, (int64_t)192); + CHECK_EQ(b->n_bidi, (int64_t)0); + f.commit_plain(); + b = f.sch.step(); + REQUIRE(b != nullptr); + CHECK_EQ(b->n_tok, (int64_t)128); /* 192 .. 319, past the run's end */ + REQUIRE_EQ(b->n_bidi, (int64_t)1); + CHECK_EQ(b->bidi_row[0], 8); + CHECK_EQ(b->bidi_n[0], 100); + CHECK_EQ(b->bidi_seq[0], 0); + CHECK_EQ(b->bidi_used[0], 300); + CHECK_EQ(b->n_mm_rows, (int64_t)100); + f.commit_plain(); + b = f.sch.step(); + REQUIRE(b != nullptr); + CHECK_EQ(b->n_tok, (int64_t)64); /* to the tail checkpoint, text only */ + CHECK_EQ(b->n_bidi, (int64_t)0); + + /* bidi_fit itself: inside the run, from before it, from its start, and with too small a + * budget (wait) or a run no step holds (cut as it falls). */ + auto r = with_run(f, 400, 200, 100, mm::Kind::Image); + CHECK_EQ(f.sch.bidi_fit(*r, 0, 400, 256, 256), 192); + CHECK_EQ(f.sch.bidi_fit(*r, 0, 400, 192, 256), 192); + CHECK_EQ(f.sch.bidi_fit(*r, 192, 400, 64, 256), 128); + CHECK_EQ(f.sch.bidi_fit(*r, 192, 400, 64, 100), 0); + CHECK_EQ(f.sch.bidi_fit(*r, 0, 250, 250, 256), 250); /* the sequence ends inside it */ + auto big = with_run(f, 800, 200, 400, mm::Kind::Image); + CHECK_EQ(f.sch.bidi_fit(*big, 192, 800, 64, 256), 64); + /* Two video frames, apart: back past both, or on past both. */ + auto vid = with_run(f, 400, 100, 70, mm::Kind::Video); + mm::RopeLayout::Run second = vid->media->rope.runs[0]; + second.tok = 172; + vid->media->rope.runs.push_back(second); + CHECK_EQ(f.sch.bidi_fit(*vid, 0, 400, 200, 256), 64); + CHECK_EQ(f.sch.bidi_fit(*vid, 64, 400, 64, 256), 192); + CHECK_EQ(f.sch.bidi_fit(*vid, 64, 400, 64, 128), 0); +} + +TEST(a_run_longer_than_a_step_is_cut_and_reads_up_to_the_cut) { + Fixture f; + media_fixture(f, 256, 1); + CHECK_OK(f.start()); + f.add(with_run(f, 800, 200, 400, mm::Kind::Image)); + const RadBatch* b = f.sch.step(); + REQUIRE(b != nullptr); + CHECK_EQ(b->n_tok, (int64_t)192); + f.commit_plain(); + b = f.sch.step(); + REQUIRE(b != nullptr); + CHECK_EQ(b->n_tok, (int64_t)64); + REQUIRE_EQ(b->n_bidi, (int64_t)1); + CHECK_EQ(b->bidi_row[0], 8); + CHECK_EQ(b->bidi_n[0], 56); + CHECK_EQ(b->bidi_used[0], 256); +} + +TEST(runs_are_cut_anywhere_without_the_flag_and_for_audio) { + for (int k = 0; k < 2; ++k) { + Fixture f; + media_fixture(f, 256, k == 0 ? 0 : 1); + CHECK_OK(f.start()); + f.add(with_run(f, 400, 200, 100, k == 0 ? mm::Kind::Image : mm::Kind::Audio)); + const RadBatch* b = f.sch.step(); + REQUIRE(b != nullptr); + CHECK_EQ(b->n_tok, (int64_t)256); + CHECK_EQ(b->n_bidi, (int64_t)0); + CHECK_EQ(b->n_mm_rows, (int64_t)56); + } +} + /* ================================================================== scored rows (KL mode) */ /* In KL mode a scored prompt names a logits row at every position it prefills, after the * sampler's rows and in entry order, which is the order Engine::score_rows maps back. A row