diff --git a/abi/rad_builder.h b/abi/rad_builder.h index fc2cfddc..2ba6e945 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -446,6 +446,17 @@ int rad_op_writes(RadBuilder* b, rad_op h, const rad_buf* bufs, int n); * A no-op for a PERSIST or DERIVED buffer, which is live everywhere already. */ int rad_buf_concurrent(RadBuilder* b, rad_buf buf); +/* The second lane's window: every op the architecture issues on lane 1 is declared between + * rad_lane1_window_begin and rad_lane1_window_end, and lane 1 is joined into lane 0 before any op + * declared after the window runs. With a window a rad_buf_concurrent buffer is live over the window + * and its own declared uses instead of the whole program, so buffers used only outside the window + * (a drafter's, the heads') can share its bytes. The runtime holds the architecture to it: an op + * issued on lane 1 outside the window fails the step by name, and the first op past the window + * issued on lane 0 joins lane 1 first when lane 1 has work pending. Optional, one window a program; + * call both at declare, in order. Additive export; RAD_ABI_VERSION does not move. */ +int rad_lane1_window_begin(RadBuilder* b); +int rad_lane1_window_end(RadBuilder* b); + /* An uneven shard: this rank's slice of a RAD_SHARD_ROW or RAD_SHARD_COL weight's split dimension * is [lo, hi) rather than an even 1/tp. Rows of each stacked row part for ROW, columns for COL, in * the container's unsharded units. The declared extent must already be `hi - lo` (per row part, diff --git a/abi/rad_runtime.h b/abi/rad_runtime.h index 1416109f..3a21a315 100644 --- a/abi/rad_runtime.h +++ b/abi/rad_runtime.h @@ -681,7 +681,8 @@ int rad_issue(RadCtx* c, rad_op op, const RadOperand* opd, int n_opd, int64_t n) * the all-reduce numbers its scratch slots per launch and the ranks must agree; a kernel with a * device-wide arrival counter must key it on the stream (libr4d's split-K GEMM does), or it may * not run here; and every buffer the second lane touches must be named to rad_buf_concurrent, - * because the planner packs transients from one linear order. */ + * because the planner packs transients from one linear order (live for the whole program, or across + * the second lane's window where the architecture declares one, abi/rad_builder.h). */ int rad_lane(RadCtx* c, int lane); int rad_lane_join(RadCtx* c, int from, int to); diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index 99576fde..1ff4c75c 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -1180,6 +1180,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_F64("mult", emult)), RAD_WEIGHTS(m.w_tok)), {}, {m.b_x}); + /* The second lane's window (rad_lane1_window_begin): from the embedding's reduction to the final + * norm, where the pipelined prefill runs its boundaries; the drafter and the heads lie outside it + * and can share the concurrent buffers' bytes. */ + RAD_ARCH_TRY(rad_lane1_window_begin(b)); if (g.world > 1) m.op_embed_ar = rw(b, RAD_OP(b, "all_reduce", RAD_PARAMS(RAD_INT("world_size", g.world), @@ -1355,6 +1359,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)), RAD_WEIGHTS(m.w_out)), {m.b_x}, {m.b_h}); + RAD_ARCH_TRY(rad_lane1_window_end(b)); m.op_gather = rw(b, RAD_OP(b, "gather_rows", RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_embd), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index cb1696b7..a3530bb3 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -875,7 +875,13 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c if (m.media) RAD_ARCH_TRY(m.mrows.declare(b, g.max_tok, g.n_embd, m.b_x)); /* Always exact, not g.wire_exact: this all_reduce sums disjoint rows (each token's embedding * comes from one rank, the others contribute zero), so a lossy wire would only add error. - * min_bytes stays 0 for the same reason. */ + * min_bytes stays 0 for the same reason. + * + * The second lane's window opens here and closes after the final norm (rad_lane1_window_begin): + * the embedding's reduction, the pipelined prefill's boundaries, the GDN a|b lane and the MoE + * shared arm are all declared inside it, so the concurrent buffers stay live only there and the + * drafter and the heads can share their bytes. */ + RAD_ARCH_TRY(rad_lane1_window_begin(b)); if (g.world > 1) m.op_embed_ar = rw(b, RAD_OP(b, "all_reduce", RAD_PARAMS(RAD_INT("world_size", g.world), @@ -1016,6 +1022,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_F64("eps", g.eps), RAD_F64("wadd", final_wadd), RAD_STR("dtype", g.dtype)), RAD_WEIGHTS(m.w_out_norm)), {m.b_x}, {m.a_h.x}); + RAD_ARCH_TRY(rad_lane1_window_end(b)); m.op_gather = rw(b, RAD_OP(b, "gather_rows", RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), diff --git a/core/build/rad_build.h b/core/build/rad_build.h index 55beeb7d..3bacf446 100644 --- a/core/build/rad_build.h +++ b/core/build/rad_build.h @@ -140,6 +140,7 @@ public: int op_reads (rad_op h, const rad_buf* b, int n); int op_writes(rad_op h, const rad_buf* b, int n); int buf_concurrent(rad_buf b); + int lane1_window(bool end); int weight_shard_span(rad_weight w, int64_t lo, int64_t hi); int weight_shard_span_parts(rad_weight w, const int64_t* lo, const int64_t* hi, const int64_t* total, int n); @@ -220,6 +221,8 @@ private: /* Buffers touched from the second lane (rad_buf_concurrent). Their declared op range does not * bound when their bytes are in use, so they are live for the whole program. */ std::set concurrent_bufs_; + /* The second lane's window (rad_lane1_window_begin), first and last op handle; -1 when none. */ + int32_t lane1_w0_ = -1, lane1_w1_ = -1; /* Parallel to prog_.kv_groups: where in the op list the group was declared, and the op found * to consume it. Declaration order is the only link between an op and a group; see diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index b72a8a3e..f08f0138 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -841,6 +841,26 @@ int Builder::buf_concurrent(rad_buf b) { return RAD_OK; } +/* See rad_lane1_window_begin. The window is the ops declared between the two calls. */ +int Builder::lane1_window(bool end) { + if (finished_) { err(RAD_E_STATE, "rad_lane1_window after declare finished"); return RAD_E_STATE; } + const int32_t next = (int32_t)prog_.ops.size(); /* the next op's handle */ + if (!end) { + if (lane1_w0_ >= 0) { + err(RAD_E_STATE, "rad_lane1_window_begin called twice: one window a program"); + return RAD_E_STATE; + } + lane1_w0_ = next; + } else { + if (lane1_w0_ < 0 || lane1_w1_ >= 0 || next - 1 < lane1_w0_) { + err(RAD_E_STATE, "rad_lane1_window_end without a begin, twice, or around no op"); + return RAD_E_STATE; + } + lane1_w1_ = next - 1; + } + return RAD_OK; +} + /* See rad_weight_shard_span. Checked against the declaration here, where the plugin can be named; * the loader checks it against the container. */ int Builder::weight_shard_span(rad_weight h, int64_t lo, int64_t hi) { @@ -2178,6 +2198,10 @@ void Builder::finish_buffer_liveness() { /* A second lane, signalled by any concurrent buffer, gets a second scratch region * (Program::scratch_regions). */ prog_.scratch_regions = concurrent_bufs_.empty() ? 1 : 2; + if (lane1_w0_ >= 0 && lane1_w1_ < 0) + err(RAD_E_STATE, "rad_lane1_window_begin without rad_lane1_window_end"); + prog_.lane1_w0 = lane1_w1_ >= 0 ? lane1_w0_ : -1; + prog_.lane1_w1 = lane1_w1_; /* Each buffer's first and last declaring op, in one pass over the op lists. */ const size_t nb = prog_.buffers.size(); @@ -2195,11 +2219,19 @@ void Builder::finish_buffer_liveness() { * rule); a buffer on the second lane, whose declared range does not bound when its bytes * are in use; and one with no declared use, since an op may issue any handle in any * order (--debug-graph reports the cost). */ - if (b.decl.kind != RAD_BUF_TRANSIENT || concurrent_bufs_.count((rad_buf)i) || first[i] < 0) { + if (b.decl.kind != RAD_BUF_TRANSIENT || first[i] < 0 || + (concurrent_bufs_.count((rad_buf)i) && lane1_w1_ < 0)) { b.first_def = 0; b.last_use = last_op; continue; } + /* A concurrent buffer under a declared window (rad_lane1_window_begin): live across the + * window, when lane 1 may touch it, and over its own uses. */ + if (concurrent_bufs_.count((rad_buf)i)) { + b.first_def = std::min(first[i], lane1_w0_); + b.last_use = std::max(last[i], lane1_w1_); + continue; + } b.first_def = first[i]; b.last_use = last[i]; @@ -2361,6 +2393,16 @@ int rad_buf_concurrent(RadBuilder* b, rad_buf buf) { return B(b)->buf_concurrent(buf); } +int rad_lane1_window_begin(RadBuilder* b) { + if (!b) return RAD_E_INVAL; + return B(b)->lane1_window(false); +} + +int rad_lane1_window_end(RadBuilder* b) { + if (!b) return RAD_E_INVAL; + return B(b)->lane1_window(true); +} + int rad_weight_shard_span(RadBuilder* b, rad_weight w, int64_t lo, int64_t hi) { if (!b) return RAD_E_INVAL; return B(b)->weight_shard_span(w, lo, hi); diff --git a/core/rad_core.h b/core/rad_core.h index 2c6da1e5..bbb3d411 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -463,6 +463,9 @@ public: * with any other transient buffer never runs there; one naming no transient buffer counts. * At most scratch_bytes; -1 (a program not built by the Builder) means scratch_bytes. */ int64_t scratch_bytes_lane1 = -1; + /* The second lane's window (rad_lane1_window_begin): the first and last op handle lane 1 may run; + * -1 when the architecture declared none (concurrent buffers then live for the whole program). */ + int32_t lane1_w0 = -1, lane1_w1 = -1; /* Everything that resolved to nothing, reported whole rather than stopping at the first * failure (spec §3.1). Structured because the domains are reported differently: a device miss diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index b9eaad36..ca8e322b 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -331,6 +331,8 @@ int Ctx::bind_ops() { p.billed = (oi.op == "all_reduce"); p.collective = p.billed; p.lora_site = oi.lora_site; + p.order = oi.anchor > 0 && (size_t)oi.anchor < P.ops.size() ? P.ops[(size_t)oi.anchor].index + : oi.index; for (const Band& b : oi.bands) band_table_.push_back(b.hi); @@ -906,6 +908,7 @@ int Ctx::lane_join(int from, int to) { RAD_TRY(rad_event_record(lane_ev_[from], fs)); RAD_TRY(rad_event_wait(ts, lane_ev_[from])); scratch_open_[from] = false; /* `to` is now behind every scratch user `from` issued */ + if (from == 1 && to == 0) lane1_pending_ = false; return RAD_OK; } diff --git a/core/runtime/ctx.h b/core/runtime/ctx.h index 36f4057b..a943e72c 100644 --- a/core/runtime/ctx.h +++ b/core/runtime/ctx.h @@ -88,6 +88,9 @@ struct OpPlan { bool billed = false; /* Runtime LoRA: this gemm's row in Program::lora_sites, -1 for every op that is not one. */ int32_t lora_site = -1; + /* Its position in the declared order, or its anchor's for a core-appended op: what the second + * lane's window (Program::lane1_w0..lane1_w1) is checked against. */ + int32_t order = 0; }; /* ------------------------------------------------------------------ routing handoff */ @@ -619,7 +622,8 @@ private: * 3. The buffer planner packs from one linear order, so two lanes can alias arena bytes * through buffers with disjoint declared lifetimes. The architecture passes every buffer * its second lane touches to `rad_buf_concurrent`, which makes it live for the whole - * program. + * program, or, where the architecture declares the second lane's window + * (rad_lane1_window_begin / _end), across the window and its own declared uses. * * 4. A slice must land in the same bucket-table band as the whole, or it picks a kernel with * a different summation order and silently changes output. `--debug-graph` prints a model's @@ -641,6 +645,8 @@ private: bool own_lane1_ = false; int lane_ = 0; RadEvent lane_ev_[2] = { nullptr, nullptr }; + /* An op was issued on lane 1 since lane 1 was last joined into lane 0 (the window's join). */ + bool lane1_pending_ = false; /* The last device op on each lane that touched host-domain memory. A host op runs at issue on * this thread and must be ordered behind every earlier device op that touched the host arena; * on each lane those complete no later than the last one. So a device op with a host-domain diff --git a/core/runtime/issue.cpp b/core/runtime/issue.cpp index 520fb107..bd364ec4 100644 --- a/core/runtime/issue.cpp +++ b/core/runtime/issue.cpp @@ -592,6 +592,23 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) { ? "has no host kernel for this band, and the placement planner put " "it on the host anyway" : "has no device kernel for this band"); + /* The second lane's window (rad_lane1_window_begin): its concurrent buffers are live only across + * it, so lane 1 runs nothing outside it, and lane 1 is joined before lane 0 runs past it. */ + if (program_ && program_->lane1_w1 >= 0) { + if (lane_ == 1 && (p.order < program_->lane1_w0 || p.order > program_->lane1_w1)) + return abort_step_msg(h, RAD_E_STATE, + fmt("issued on the second lane outside its declared window (ops " + "%d..%d, this one %d): the buffers it may touch there are " + "live only across the window", + (int)program_->lane1_w0, (int)program_->lane1_w1, (int)p.order)); + if (lane_ == 0 && lane1_pending_ && p.order > program_->lane1_w1) { + const int js = lane_join(1, 0); + if (js < 0) + return abort_step_msg(h, js, "joining the second lane at its window's end failed"); + } + } + if (lane_ == 1) lane1_pending_ = true; + /* The row's DIV on the ranged parameter held at the band's upper bound, maybe not here. */ if (ip.ranged_div > 1 && n % ip.ranged_div != 0) return abort_step_msg(h, RAD_E_SHAPE, diff --git a/tests/arch_fakes.h b/tests/arch_fakes.h index 970d1b04..d9de06ed 100644 --- a/tests/arch_fakes.h +++ b/tests/arch_fakes.h @@ -120,6 +120,8 @@ struct RadBuilder { /* The buffers named to rad_buf_concurrent: the ones a second lane touches. */ std::vector concurrent; + /* The second lane's window (rad_lane1_window_begin), first and last op index; -1 when none. */ + int32_t lane1_w0 = -1, lane1_w1 = -1; /* The vision and audio encoder declarations (rad_declare_encoder), if any. */ RadEncoderDecl encoder{}; @@ -297,6 +299,19 @@ extern "C" int rad_buf_concurrent(RadBuilder* b, rad_buf buf) { b->concurrent.push_back(buf); return RAD_OK; } +/* Recorded: the second lane's window (qwen35_fp8, gemma3), applied by liveness() as the real builder + * applies it. Ops are handed out from 1 in declaration order, so the next op's index is size + 1. */ +extern "C" int rad_lane1_window_begin(RadBuilder* b) { + if (!b || b->lane1_w0 >= 0) return RAD_E_STATE; + b->lane1_w0 = (int32_t)b->ops.size() + 1; + return RAD_OK; +} +extern "C" int rad_lane1_window_end(RadBuilder* b) { + if (!b || b->lane1_w0 < 0 || b->lane1_w1 >= 0 || (int32_t)b->ops.size() < b->lane1_w0) + return RAD_E_STATE; + b->lane1_w1 = (int32_t)b->ops.size(); + return RAD_OK; +} extern "C" int rad_weight_shard_span(RadBuilder* b, rad_weight w, int64_t lo, int64_t hi) { if (!b || w == 0 || w > b->weights.size()) return RAD_E_INVAL; if (lo < 0 || hi <= lo) return RAD_E_INVAL; @@ -700,7 +715,8 @@ int count_issued(const std::vector& v, const std::string& name) { namespace rad_fake { /* One buffer's planned lifetime. `planned` is false for a buffer live over the whole program - * (PERSIST, DERIVED, rad_buf_concurrent, or no declared use), which shares bytes with nothing. */ + * (PERSIST, DERIVED, rad_buf_concurrent without a second-lane window, or no declared use), which + * shares bytes with nothing. */ struct Live { int32_t lo = 0, hi = 0; bool planned = false; @@ -729,11 +745,17 @@ std::vector liveness(const RadBuilder& b) { Live& l = L[h]; l.transient = d.kind == RAD_BUF_TRANSIENT; l.domain = d.domain; - if (!l.transient || conc.count((rad_buf)h) || first[h] < 0) { + if (!l.transient || first[h] < 0 || (conc.count((rad_buf)h) && b.lane1_w1 < 0)) { l.lo = 0; l.hi = last_op; continue; } + if (conc.count((rad_buf)h)) { /* under the window: across it and over its own uses */ + l.planned = true; + l.lo = std::min(first[h], b.lane1_w0); + l.hi = std::max(last[h], b.lane1_w1); + continue; + } l.planned = true; l.lo = first[h]; l.hi = last[h]; diff --git a/tests/build_test.cpp b/tests/build_test.cpp index b0f6fa81..1d520a35 100644 --- a/tests/build_test.cpp +++ b/tests/build_test.cpp @@ -1904,6 +1904,53 @@ TEST(the_logits_pooled_and_concurrent_buffers_live_past_their_declared_uses) { } } +/* Under a declared window (rad_lane1_window_begin) a concurrent buffer is live across the window and + * its own uses, not the whole program: a buffer used only past the window can take its bytes. */ +TEST(a_concurrent_buffer_under_a_window_lives_across_the_window_only) { + NEED_PLUGIN(reg) + Builder b(reg, make_meta(), make_ctx()); + RadBufDecl bd{}; + bd.dtype = RAD_I8; + bd.rank = 1; + bd.shape[0] = 4096; + bd.kind = RAD_BUF_TRANSIENT; + bd.domain = RAD_DOMAIN_DEVICE; + const rad_buf y = b.decl_buffer("y", &bd); + const rad_buf z = b.decl_buffer("z", &bd); + const rad_weight w = decl_w(b, "w0"); + CHECK_OK(rad_lane1_window_begin(&b)); + rad_op o1 = decl_gemm(b, w), o2 = decl_gemm(b, w); + CHECK_OK(rad_lane1_window_end(&b)); + rad_op o3 = decl_gemm(b, w); + CHECK_OK(b.op_writes(o1, &y, 1)); + CHECK_OK(b.op_reads(o2, &y, 1)); + CHECK_OK(b.op_writes(o3, &z, 1)); + CHECK_OK(b.buf_concurrent(y)); + CHECK_OK(b.finish()); + Program& p = b.program(); + CHECK_EQ(p.lane1_w0, (int32_t)o1); + CHECK_EQ(p.lane1_w1, (int32_t)o2); + const BufferInfo* by = p.buffer(y); + const BufferInfo* bz = p.buffer(z); + CHECK_EQ(by->first_def, (int32_t)o1); + CHECK_EQ(by->last_use, (int32_t)o2); + /* z lives past the window only, so it shares y's bytes (whole program, it could not). */ + CHECK_EQ(bz->first_def, (int32_t)o3); + CHECK(bz->arena_offset < by->arena_offset + by->bytes && + by->arena_offset < bz->arena_offset + bz->bytes); +} + +/* A window opened and never closed is refused at finish. */ +TEST(a_second_lane_window_must_be_closed) { + NEED_PLUGIN(reg) + Builder b(reg, make_meta(), make_ctx()); + const rad_weight w = decl_w(b, "w0"); + CHECK_OK(rad_lane1_window_begin(&b)); + decl_gemm(b, w); + b.finish(); + CHECK_EQ(b.status(), RAD_E_STATE); +} + /* ================================================================== the buffer plan, at random */ /* The packer is greedy, so its output depends on sizes, and a liveness or offset mistake aliases diff --git a/tests/runtime_test.cpp b/tests/runtime_test.cpp index b74201d1..3f488ff6 100644 --- a/tests/runtime_test.cpp +++ b/tests/runtime_test.cpp @@ -937,6 +937,34 @@ TEST(a_second_lane_scratch_request_past_its_region_is_refused_by_name) { CHECK(std::strstr(f.ctx.step_error(), "rad_buf_concurrent") != nullptr); } +/* The second lane's window holds at issue: an op on lane 1 outside it fails the step by name; inside + * it runs. */ +TEST(an_op_on_the_second_lane_outside_its_window_is_refused_by_name) { + Fixture f; + f.P.scratch_regions = 2; + rad_buf x = f.add_buffer("x", RAD_BF16, 64, 4, 0); + rad_op a0 = f.add_op("gemm_nt", "M", { 8192 }, { k_scr_seen0 }, { "s0" }, nullptr, {}); + rad_op a1 = f.add_op("gemm_nt", "M", { 8192 }, { k_scr_seen1 }, { "s1" }, nullptr, {}); + f.P.lane1_w0 = f.P.lane1_w1 = (int32_t)a0; + CHECK_OK(f.start()); + + CHECK_OK(f.step(4, 0, [&](Ctx& c) { + RadOperand opd[1] = { RAD_B(x) }; + CHECK_OK(c.set_lane(1)); + CHECK_OK(c.issue(a0, opd, 1, RAD_N_BATCH)); + CHECK_OK(c.set_lane(0)); + CHECK_OK(c.issue(a1, opd, 1, RAD_N_BATCH)); /* past the window: joins lane 1 first */ + })); + const int s = f.step(4, 1, [&](Ctx& c) { + RadOperand opd[1] = { RAD_B(x) }; + CHECK_OK(c.set_lane(1)); + c.issue(a1, opd, 1, RAD_N_BATCH); + CHECK_OK(c.set_lane(0)); + }); + CHECK_EQ(s, RAD_E_STATE); + CHECK(std::strstr(f.ctx.step_error(), "outside its declared window") != nullptr); +} + /* Operands are positional (spec §2.3), so a miscount is silent numerical garbage at the kernel, * not a diagnosable failure. The runtime checks the given arity against the schema. */ TEST(operand_arity_is_checked_against_the_schema) {