sched: the bidirectional plane trusts its runs no further than it checks them

h_bidi carries one slot per run that intersects the chunk, over a plane
four times the step's token capacity wide -- sound only because
RopeLayout::runs is documented ascending and pairwise disjoint, so every
accepted run can bill at least one real token of the step. Until 2.0 the
layout was built solely from the tokenizer's own placeholder scan;
request-side placement writes it now, and a layout with overlapping runs
would bill one token twice, run n_bidi past the plane, and hand the card
an index plane its shape validation waved through.

The build now checks the promise where it consumes it: a run starting
before the last accepted one's end is dropped -- those media rows simply
lose bidirectionality for the step, which is an ordinary miss -- and a
hard stop guards the plane width itself, unreachable unless the check is
wrong. Both events speak once per process, not per step.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:10 +00:00
1 parent 90b1229bfc
commit 5736dd11c3
1 file changed
+28
+28
View File
@@ -8,6 +8,7 @@
#include "util/simd.h"
#include <algorithm>
#include <atomic>
#include <cassert>
#include <cstring>
#include <cstdlib>
@@ -1068,10 +1069,37 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
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_;
/* RopeLayout::runs is documented ascending and pairwise disjoint, and the slice
* bound (h_bidi is 4*max_tok_ wide, one slot per token this step can carry) leans
* on exactly that: overlapping runs would count one token twice. The layout is now
* fed from request-side placement, so the promise is checked here rather than
* trusted -- an overlapping run drops its media rows' bidirectionality, which the
* acceptance path treats as an ordinary miss, where a slot overrun is a fault. */
int64_t prev_b = c0;
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;
if (a < prev_b) {
static std::atomic<bool> said{false};
bool expected = false;
if (said.compare_exchange_strong(expected, true))
RAD_WARN("step batch: a rope layout overlaps at token %lld; that run is "
"not staged bidirectionally (first of possibly many)",
(long long)a);
continue;
}
prev_b = b;
if (n_bidi >= T) {
/* Belt: the disjoint check makes this unreachable; the slice says so
* rather than letting an overrun reach the card. */
static std::atomic<bool> said2{false};
bool expected = false;
if (said2.compare_exchange_strong(expected, true))
RAD_WARN("step batch: bidi runs overflowed the %lld-token plane "
"(first of possibly many)", (long long)T);
break;
}
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);