diff --git a/abi/rad_abi.h b/abi/rad_abi.h index d45766f2..0337fe44 100644 --- a/abi/rad_abi.h +++ b/abi/rad_abi.h @@ -19,7 +19,7 @@ extern "C" { * version is refused at load with both versions printed. Each also exports the layout's fingerprint * (rad_abi_layout.h), so two layouts given one number are refused as well. A new optional export * (e.g. rad_plugin_reference) is not a change of shape: a plugin without it gets the default. */ -#define RAD_ABI_VERSION 34u +#define RAD_ABI_VERSION 35u /* ================================================================== plugin identity */ /* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin: @@ -60,6 +60,14 @@ typedef struct RadArgs { void* instance; /* whatever init() returned for this resolved instance */ int rank; /* tensor-parallel rank and world size, for collectives */ int world_size; + /* ABI 35: host-side maximum KV length represented by this exact launch operand (a seqused + * span or a supported one-sequence prefill boundary), or -1 when it cannot be proven. Plugins + * must not infer it from a device pointer or synchronously read device memory to get it. */ + int32_t kv_used_max_h; + /* The current rank's WGP count (HIP multiProcessorCount on RDNA); lets split launchers use + * exactly the same per-device occupancy as architecture pass-key callbacks without querying + * the process-global current device on the hot path. */ + int32_t wgp_count; } RadArgs; /* Read a parameter by key. Inline definitions, because a plugin links only the ABI headers and diff --git a/abi/rad_abi_layout_rows.h b/abi/rad_abi_layout_rows.h index ee0cd751..2477beee 100644 --- a/abi/rad_abi_layout_rows.h +++ b/abi/rad_abi_layout_rows.h @@ -37,6 +37,8 @@ ABI_FIELD(RadArgs, scratch_bytes) ABI_FIELD(RadArgs, instance) ABI_FIELD(RadArgs, rank) ABI_FIELD(RadArgs, world_size) +ABI_FIELD(RadArgs, kv_used_max_h) +ABI_FIELD(RadArgs, wgp_count) ABI_STRUCT(RadBatch) ABI_FIELD(RadBatch, phase) ABI_FIELD(RadBatch, step) @@ -96,6 +98,7 @@ ABI_FIELD(RadBatch, plugin_words_h) ABI_FIELD(RadBatch, plugin_cu_h) ABI_FIELD(RadBatch, sgen_b) ABI_FIELD(RadBatch, kv_used_max_h) +ABI_FIELD(RadBatch, positions_h) ABI_STRUCT(RadBufDecl) ABI_FIELD(RadBufDecl, dtype) ABI_FIELD(RadBufDecl, rank) @@ -503,6 +506,7 @@ ABI_FIELD(RadKVGroupBatch, state_index_pitch) ABI_FIELD(RadKVGroupBatch, block_size) ABI_FIELD(RadKVGroupBatch, max_blocks) ABI_FIELD(RadKVGroupBatch, stream) +ABI_FIELD(RadKVGroupBatch, seqused_h) ABI_STRUCT(RadKVGroupDecl) ABI_FIELD(RadKVGroupDecl, kind) ABI_FIELD(RadKVGroupDecl, dtype) diff --git a/abi/rad_runtime.h b/abi/rad_runtime.h index 49dbd67d..865dbcb5 100644 --- a/abi/rad_runtime.h +++ b/abi/rad_runtime.h @@ -68,8 +68,10 @@ typedef struct RadKVGroupBatch { int64_t state_index_pitch; int64_t block_size; int64_t max_blocks; - /* Null unless this group streams (RadKVStream). Trailing and zero-defaulted. */ + /* Null unless this group streams (RadKVStream). */ const RadKVStream* stream; + /* ABI 35: host mirror of seqused, retained through this batch for exact sliced-launch bounds. */ + const int32_t* seqused_h; } RadKVGroupBatch; typedef struct RadBatch { @@ -334,6 +336,9 @@ typedef struct RadBatch { /* ---- appended in ABI 34: host-side max seqused per entry of kv[], for an architecture's * optional recorded-pass geometry key. Null when the batch has no host summary. */ const int32_t* kv_used_max_h; + /* 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; } RadBatch; /* What a generator pass carries beside the step batch's own fields, which keep their meanings: diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index 9572fd5e..0856af99 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -1729,7 +1729,7 @@ uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) { /* Qwen always passes cu_seqlens, so fill_paged takes q_len = min(band, total_q). That equals * n_tok only for one sequence; a ragged or multi-sequence launch's band is not the host group * max, and mixed decode-prefix / prefill-suffix launches use the suffix's own KV lengths. */ - if (batch->n_seq != 1 || batch->n_tok <= 0 || batch->max_ctx_len <= 0) return 0; + if (batch->n_seq != 1 || batch->n_tok <= 0 || m.g.max_ctx <= 0) return 0; const int q_len = (int)batch->n_tok; const int total_q = (int)batch->n_tok; const int num_seqs = 1; @@ -1741,7 +1741,7 @@ uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) { constexpr int tile = 48, nwarps = 24; const int bq = nwarps * 16 / m.g.gqa(); const int splits = rad_prefill_splits_for_live(bq, tile, q_len, total_q, num_seqs, - kv_heads, batch->max_ctx_len, used, wgps); + kv_heads, m.g.max_ctx, used, wgps); return rad_prefill_grid_key(q_len, bq, splits, kv_heads, num_seqs, nwarps * 32); } diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index 47a7c185..8e6a56d0 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -371,6 +371,7 @@ int Ctx::bind_ops() { ip.args.instance = r.instance; ip.args.rank = rank_; ip.args.world_size = world_size_; + ip.args.wgp_count = wgp_count_; /* A launch that ends its own link hold gets the hold word as an extra * `link_gate` parameter after the geometry's. */ if (r.row->releases_link) @@ -946,11 +947,13 @@ int Ctx::lane_join(int from, int to) { /* The key below reads every RadBatch field a pass's shape or arguments can depend on, by hand. A * field added to RadBatch changes its size: decide whether the key must read it, then update the - * size here. `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) == 424, + * size here. The ABI-35 host mirrors are used by issue-time split selection; unsupported dynamic + * 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, "RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on"); -static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 80, +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 { diff --git a/core/runtime/issue.cpp b/core/runtime/issue.cpp index bb129afe..3d326762 100644 --- a/core/runtime/issue.cpp +++ b/core/runtime/issue.cpp @@ -10,6 +10,7 @@ * the host. See ctx.h for the cost budget (spec ยง3.2). */ #include +#include #include #include #include @@ -62,6 +63,66 @@ std::string tensor_shape_str(const RadTensor& t) { return s + "]"; } +/* A device launch may use a host-side KV bound only when its exact operand maps to the host mirror + * of seqused (including a sequence slice), or to the position just after a one-sequence prefill + * piece. Never copy seqused back from a device queue here: the AQL upload may still be pending on + * another queue. */ +static bool issue_vector_operand(const RadTensor& t) { + return (t.rank == 1 && t.stride[0] == 1) || + (t.rank == 2 && t.shape[1] == 1 && t.stride[0] == 1 && t.stride[1] == 1); +} + +static bool issue_host_span_max(const RadTensor& t, const int32_t* device, const int32_t* host, + int64_t total, int32_t* out) { + if (!t.data || !issue_vector_operand(t) || t.shape[0] <= 0 || !device || !host || total <= 0 || !out) + return false; + const std::uintptr_t p = reinterpret_cast(t.data); + const std::uintptr_t base = reinterpret_cast(device); + if (p < base) return false; + const std::uintptr_t delta = p - base; + if (delta % sizeof(int32_t)) return false; + const std::uint64_t first = delta / sizeof(int32_t); + const std::uint64_t count = (std::uint64_t)t.shape[0]; + if (first > (std::uint64_t)total || count > (std::uint64_t)total - first) return false; + int32_t maximum = host[first]; + for (std::uint64_t i = 1; i < count; ++i) maximum = std::max(maximum, host[first + i]); + *out = maximum; + return true; +} + +static int32_t issue_kv_used_max_h(const RadBatch* b, const RadTensor* t, int n) { + if (!b || !t || b->n_seq <= 0) return -1; + bool have = false; + int32_t found = -1; + auto add = [&](int32_t value) { + if (!have) { found = value; have = true; } + else if (found != value) return false; + return true; + }; + for (int i = 0; i < n; ++i) { + if (!t[i].data || !issue_vector_operand(t[i]) || t[i].shape[0] <= 0) continue; + if (b->kv) for (int g = 0; g < b->n_kv_groups; ++g) { + if (b->kv_used_max_h && t[i].data == b->kv[g].seqused && + t[i].shape[0] == b->n_seq) { + if (!add(b->kv_used_max_h[g])) return -1; + continue; + } + int32_t used = -1; + if (issue_host_span_max(t[i], b->kv[g].seqused, b->kv[g].seqused_h, + b->n_seq, &used) && !add(used)) return -1; + } + /* ArPieces' first attention uses positions[R] as its exact KV end: it is the position after + * the first query piece. This special operand is one length for one ordinary prefill seq. */ + if (b->phase == RAD_PHASE_PREFILL && b->n_seq == 1 && b->n_spec == 0 && + b->draft_pass == 0 && t[i].shape[0] == 1) { + int32_t used = -1; + if (issue_host_span_max(t[i], b->positions, b->positions_h, b->n_tok, &used) && + !add(used)) return -1; + } + } + return have ? found : -1; +} + /* Element offset to byte offset. False when it cannot be expressed: a mid-byte offset into a * packed sub-byte weight (e.g. an odd W4 element), or a plugin-private dtype with no core-known * size. */ @@ -854,6 +915,7 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) { /* The rest of RadArgs was filled in at prepare; args.t already points at tbuf_. There is one * entry per schema operand, the trailing optional ones the issue left out null. */ ip.args.n_t = std::max(n_opd, (int)p.n_opd_schema); + ip.args.kv_used_max_h = issue_kv_used_max_h(batch_, t, ip.args.n_t); p.issued = true; ++n_issues_; diff --git a/core/sched/batch.cpp b/core/sched/batch.cpp index 6b0e6130..75c7ca4e 100644 --- a/core/sched/batch.cpp +++ b/core/sched/batch.cpp @@ -857,6 +857,7 @@ int BatchBuilder::build_groups(const StepPlan& plan, const IKVManager& kv, Set& b.block_table = on_rank(base, s.dev[0], g.d_bt); b.block_table_pitch = pitch; b.seqused = on_rank(base, s.dev[0], g.d_used); + b.seqused_h = g.h_used; b.state_index = on_rank(base, s.dev[0], g.d_sidx); b.state_index_pitch = g.sidx_pitch; b.block_size = L.block_size; @@ -1216,6 +1217,7 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv, B.n_seq = n_seq; B.token_ids = on_rank(base, b0, s.d_tok); B.positions = on_rank(base, b0, s.d_pos); + B.positions_h = s.h_pos; B.cu_seqlens = on_rank(base, b0, s.d_cu); B.seq_ids = on_rank(base, b0, s.d_sid); B.n_out = n_out; diff --git a/libr4d/r4d_attn_prefill_split.h b/libr4d/r4d_attn_prefill_split.h index e37ccafb..6f86caf3 100644 --- a/libr4d/r4d_attn_prefill_split.h +++ b/libr4d/r4d_attn_prefill_split.h @@ -14,18 +14,29 @@ #pragma once #include "rad_prefill_split.h" +#include -// Workgroups the card runs at once: one a WGP. HIP's multiProcessorCount counts WGPs on RDNA (32 on -// the R9700, see want_wg in r4d_hc_bf16.hip); 32 if it cannot be read. -static inline int r4d_prefill_wgps() { - static const int v = [] { - int dev = 0; - hipDeviceProp_t pr{}; - if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&pr, dev) != hipSuccess) - return 32; - return pr.multiProcessorCount > 0 ? pr.multiProcessorCount : 32; - }(); - return v; +// Workgroups the card runs at once: one a WGP. Kernel launches use RadArgs::wgp_count, the same +// per-rank property used by pass-key callbacks. Declaration-time scratch sizing has no Ctx yet, so +// cache that query per HIP device rather than once process-wide (TP ranks can bind unlike cards). +static inline int r4d_prefill_wgps(const RadArgs* a = nullptr) { + if (a && a->wgp_count > 0) return a->wgp_count; + int dev = 0; + if (hipGetDevice(&dev) != hipSuccess || dev < 0) return 32; + static std::atomic cached[64]{}; + if (dev < (int)(sizeof(cached) / sizeof(cached[0]))) { + const int old = cached[dev].load(std::memory_order_relaxed); + if (old > 0) return old; + } + hipDeviceProp_t pr{}; + if (hipGetDeviceProperties(&pr, dev) != hipSuccess) return 32; + const int count = pr.multiProcessorCount > 0 ? pr.multiProcessorCount : 32; + if (dev < (int)(sizeof(cached) / sizeof(cached[0]))) { + int expected = 0; + cached[dev].compare_exchange_strong(expected, count, std::memory_order_relaxed); + return cached[dev].load(std::memory_order_relaxed); + } + return count; } // The split law, at the launch's own geometry: `bq` query positions a workgroup, `tile` keys a KV @@ -52,43 +63,25 @@ static inline int r4d_prefill_splits_law(int bq, int tile, int q_len, int total_ // tells r4d_selftest a split launch from an unsplit one with the same bytes. Not atomic. static int g_prefill_last_splits = 0; -/* Live KV length for the 8-tile floor. seqused_k is device-side (r4d_attn_args.h); max_ctx is - * the model bound and is too wide for a short chunk. A failed copy keeps max_ctx, which only - * over-splits. Occupancy already returned 1 skips the copy. */ -static inline int r4d_prefill_used_ctx(const R4DArgs* r) { - if (!r->seqused_k || r->num_seqs <= 0) return 0; - const int n = r->num_seqs; - int used = 0; - int chunk[64]; - for (int off = 0; off < n; off += 64) { - const int m = n - off < 64 ? n - off : 64; - if (hipMemcpy(chunk, r->seqused_k + off, (size_t)m * sizeof(int), - hipMemcpyDefault) != hipSuccess) { - (void)hipGetLastError(); - return 0; - } - for (int i = 0; i < m; ++i) - if (chunk[i] > used) used = chunk[i]; - } - return used; -} +/* The runtime supplies the live group maximum only when it can prove that the launch's full + * seqused operand is the just-built, non-speculative prefill batch. Other shapes use max_ctx below; + * never synchronously copy device seqused from a HIP queue independent of the AQL upload queue. */ /* The KV split's count before the scratch bound: the case's `splits` parameter when it names one * (r4d_selftest; the prefill rows declare no such axis, so the engine passes none), else the law, - * at most 64. The 8-tile floor uses live seqused_k, not the model max_ctx. */ + * at most 64. Use the runtime's host seqused maximum when exact for this launch; otherwise use the + * declared model bound. The launcher and recording callback share that same rule. */ static inline int r4d_prefill_splits_want(const RadArgs* a, const R4DArgs* r, int bq, int tile) { const int sp0 = split_of(a); if (sp0 > 0) return std::min(sp0, 64); - const int wgps = r4d_prefill_wgps(); + const int wgps = r4d_prefill_wgps(a); const int qtot = r4d_q_total(*r); - int sp = r4d_prefill_splits_law(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, r->max_ctx, - wgps); - if (sp <= 1) return 1; - const int used = r4d_prefill_used_ctx(r); + const int used = a->kv_used_max_h; if (used > 0) - sp = rad_prefill_splits_for_live(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, - r->max_ctx, used, wgps); - return sp; + return rad_prefill_splits_for_live(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, + r->max_ctx, used, wgps); + return r4d_prefill_splits_law(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, + r->max_ctx, wgps); } /* The launch's split count, 1 for the plain form: what r4d_prefill_splits_want asks, cut to the @@ -125,7 +118,7 @@ static inline int64_t r4d_prefill_scratch(const RadArgs* a, int head_dim, long l const int kv = (int)std::max(1LL, gqa > 0 ? n_head / gqa : n_head); const int band = (int)std::max(1LL, r4d_geti_or(a, "q_len", 1)); const int mctx = (int)r4d_geti_or(a, "max_ctx", 0); - const int w = r4d_prefill_wgps(); + const int w = r4d_prefill_wgps(a); const int64_t per_row = (int64_t)n_head * (head_dim + 2) * 4; int64_t worst = 0; for (long long n = 1; n <= max_seqs; ++n) diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 0e46baf2..38b862a2 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -1410,6 +1410,18 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam const int64_t span0 = lead, span1 = cu[(size_t)ns]; /* the launch's own rows */ const int64_t total_q = span1 + tail; const bool ragged = ns > 1 || lead || tail; + /* This direct-kernel fixture supplies an exact host maximum for a full sequence operand. Its + * synthetic partial-row operands do not carry the runtime's host slice/position view, so they + * exercise the safe declared-context fallback. */ + int32_t used_max_h = -1; + if (!lead && !tail) + for (int32_t x : used) used_max_h = std::max(used_max_h, x); + int wgp_count = 32, current_device = 0; + hipDeviceProp_t current_props{}; + if (hipGetDevice(¤t_device) == hipSuccess && + hipGetDeviceProperties(¤t_props, current_device) == hipSuccess && + current_props.multiProcessorCount > 0) + wgp_count = current_props.multiProcessorCount; /* one disjoint run of mb blocks per sequence */ const std::vector bt = iota_i32(ns * mb); @@ -1484,6 +1496,8 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam a.t = ts.data(); a.n_t = (int)ts.size(); a.p = ps.data(); a.n_p = (int)ps.size(); a.world_size = 1; + a.kv_used_max_h = used_max_h; + a.wgp_count = wgp_count; const int64_t need = r->scratch ? r->scratch(&a) : 0; if (need > 0) { a.scratch = dalloc((size_t)need); @@ -1500,12 +1514,8 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam const int s_un = run(1, d_o[0]); const int s_fo = run(force, d_o[1]); const int s_lw = run(0, d_o[2]); - int law_ctx = (int)(mb * bs); - if (decl_max_ctx > 0) { - law_ctx = 0; - for (const SplitSeq& s : seqs) - if ((int)s.ctx > law_ctx) law_ctx = (int)s.ctx; - } + int law_ctx = decl_max_ctx > 0 ? (int)decl_max_ctx : (int)(mb * bs); + if (used_max_h > 0 && used_max_h < law_ctx) law_ctx = used_max_h; const int want = law((int)(ragged ? std::min(2048, total_q) : qmax), (int)total_q, (int)ns, (int)kv_heads, law_ctx); ok((tag + ": splits 1 / forced / law").c_str(), diff --git a/tests/abi_layout_table.h b/tests/abi_layout_table.h index 22449f28..acc0c27a 100644 --- a/tests/abi_layout_table.h +++ b/tests/abi_layout_table.h @@ -1,4 +1,4 @@ -/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 34 and +/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 35 and * RAD_FORMAT_VER 2, as { type, field, offset, size } rows ("" is the struct itself). A snapshot * of the headers on x86-64 Linux, which abi_layout_test.cpp holds the layout to; regenerate it * from the test's printout only together with a version bump. */ @@ -23,7 +23,7 @@ { "RadArchProbe", "gen_max_pixels", 104, 8 }, { "RadArchProbe", "gen_max_refs", 112, 8 }, { "RadArchProbe", "gen_shape", 120, 4 }, -{ "RadArgs", "", 0, 64 }, +{ "RadArgs", "", 0, 72 }, { "RadArgs", "t", 0, 8 }, { "RadArgs", "n_t", 8, 4 }, { "RadArgs", "p", 16, 8 }, @@ -33,7 +33,9 @@ { "RadArgs", "instance", 48, 8 }, { "RadArgs", "rank", 56, 4 }, { "RadArgs", "world_size", 60, 4 }, -{ "RadBatch", "", 0, 424 }, +{ "RadArgs", "kv_used_max_h", 64, 4 }, +{ "RadArgs", "wgp_count", 68, 4 }, +{ "RadBatch", "", 0, 432 }, { "RadBatch", "phase", 0, 4 }, { "RadBatch", "step", 4, 4 }, { "RadBatch", "n_tok", 8, 8 }, @@ -92,6 +94,7 @@ { "RadBatch", "plugin_cu_h", 400, 8 }, { "RadBatch", "sgen_b", 408, 8 }, { "RadBatch", "kv_used_max_h", 416, 8 }, +{ "RadBatch", "positions_h", 424, 8 }, { "RadBufDecl", "", 0, 80 }, { "RadBufDecl", "dtype", 0, 4 }, { "RadBufDecl", "rank", 4, 4 }, @@ -488,7 +491,7 @@ { "RadGeneratorDecl", "chunk_bos", 364, 4 }, { "RadGeneratorDecl", "chunk_eos", 368, 4 }, { "RadGeneratorDecl", "cond_opts", 372, 4 }, -{ "RadKVGroupBatch", "", 0, 80 }, +{ "RadKVGroupBatch", "", 0, 88 }, { "RadKVGroupBatch", "group", 0, 4 }, { "RadKVGroupBatch", "slot_mapping", 8, 8 }, { "RadKVGroupBatch", "block_table", 16, 8 }, @@ -499,6 +502,7 @@ { "RadKVGroupBatch", "block_size", 56, 8 }, { "RadKVGroupBatch", "max_blocks", 64, 8 }, { "RadKVGroupBatch", "stream", 72, 8 }, +{ "RadKVGroupBatch", "seqused_h", 80, 8 }, { "RadKVGroupDecl", "", 0, 56 }, { "RadKVGroupDecl", "kind", 0, 4 }, { "RadKVGroupDecl", "dtype", 4, 4 }, diff --git a/tests/abi_layout_test.cpp b/tests/abi_layout_test.cpp index d651722a..c45040d9 100644 --- a/tests/abi_layout_test.cpp +++ b/tests/abi_layout_test.cpp @@ -8,7 +8,7 @@ * * Checked: a C translation unit (abi_c_test.c, strict C11) lays every struct out as C++ does; every * struct's listed fields account for its size, so the list in rad_abi_layout_rows.h is complete; and - * the layout agrees with the table below, taken at RAD_ABI_VERSION 34 and RAD_FORMAT_VER 2. Under + * the layout agrees with the table below, taken at RAD_ABI_VERSION 35 and RAD_FORMAT_VER 2. Under * those versions a listed field may not move or change size, an ABI struct may only grow by fields * past its old end, and an on-disk struct may not change at all. The table is a snapshot of the * headers: nothing else states the layout. When it disagrees the test prints the live table. @@ -58,8 +58,8 @@ std::vector cpp_rows() { }; } -/* The layout at RAD_ABI_VERSION 34 / RAD_FORMAT_VER 2 (see the file header). */ -constexpr uint32_t kTableAbi = 34, kTableFormat = 2; +/* The layout at RAD_ABI_VERSION 35 / RAD_FORMAT_VER 2 (see the file header). */ +constexpr uint32_t kTableAbi = 35, kTableFormat = 2; const AbiRow kTable[] = { #include "abi_layout_table.h" }; diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index c2b6da08..fd679e10 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -1885,7 +1885,7 @@ RadModelMeta qwen4exp_meta() { TEST(qwen4exp_pass_key_reuses_only_proven_uniform_prefill_grids) { RadModelMeta meta = qwen4exp_meta(); RadBuildCtx build = build_ctx(0, 1); - build.max_tok = 64; build.max_seqs = 2; build.max_spec = 0; build.max_ctx = 4096; + build.max_tok = 64; build.max_seqs = 2; build.max_spec = 0; build.max_ctx = 65536; RadBuilder graph; REQUIRE_EQ(qwen4exp_fp8::declare(&graph, &meta, &build), RAD_OK); rad_kvgroup kv_full = 0; @@ -1902,7 +1902,8 @@ TEST(qwen4exp_pass_key_reuses_only_proven_uniform_prefill_grids) { RadBatch batch{}; batch.phase = RAD_PHASE_PREFILL; batch.n_tok = 64; batch.n_seq = 1; batch.max_q_len = 64; - batch.max_ctx_len = 4096; batch.n_kv_groups = 1; batch.kv = &group; + /* The split law uses the declared launch context, not this rounded live-batch bound. */ + batch.max_ctx_len = 1024; batch.n_kv_groups = 1; batch.kv = &group; int32_t used[1] = {2304}; const uint64_t key6 = qwen4exp_fp8::pass_key(&ctx, &batch, used); CHECK(key6 != 0); @@ -1911,6 +1912,12 @@ TEST(qwen4exp_pass_key_reuses_only_proven_uniform_prefill_grids) { used[0] = 3072; CHECK(qwen4exp_fp8::pass_key(&ctx, &batch, used) != key6); + /* Same rounded live bound, different exact grids under the launcher's model context. */ + used[0] = 896; + const uint64_t key_896 = qwen4exp_fp8::pass_key(&ctx, &batch, used); + used[0] = 1152; + CHECK(qwen4exp_fp8::pass_key(&ctx, &batch, used) != key_896); + /* A shorter one-sequence chunk still proves q_len = n_tok; do not require the model max_tok. */ used[0] = 2304; batch.n_tok = 32; batch.max_q_len = 32; diff --git a/tests/runtime_test.cpp b/tests/runtime_test.cpp index f02e8473..12746401 100644 --- a/tests/runtime_test.cpp +++ b/tests/runtime_test.cpp @@ -50,6 +50,7 @@ struct Rec { RadTensor t[8]{}; long long M = -1; int rank = 0, world = 0; + int32_t kv_used_max_h = -1; void* scratch = nullptr; int64_t scratch_bytes = 0; void* instance = nullptr; @@ -67,6 +68,7 @@ static int record(const char* name, const RadArgs* a, RadStream s) { rad_args_geti(a, "M", &r.M); r.rank = a->rank; r.world = a->world_size; + r.kv_used_max_h = a->kv_used_max_h; r.scratch = a->scratch; r.scratch_bytes = a->scratch_bytes; r.instance = a->instance; @@ -2337,9 +2339,10 @@ static uint64_t test_prefill_geometry_key(RadCtx* c, const RadBatch* b, const in if (!c || !b || !used) return 0; const int bq = 192, tile = 48, kv_heads = 4, threads = 768; const int wgps = rad_ctx_wgp_count(c); + constexpr int declared_max_ctx = 65536; const int splits = rad_prefill_splits_for_live(bq, tile, (int)b->max_q_len, (int)b->n_tok, (int)b->n_seq, kv_heads, - b->max_ctx_len, used[0], wgps); + declared_max_ctx, used[0], wgps); return rad_prefill_grid_key((int)b->max_q_len, bq, splits, kv_heads, (int)b->n_seq, threads); } @@ -2379,6 +2382,53 @@ TEST(legacy_prefill_keying_only_fails_closed_for_dynamic_split_rows) { } } +TEST(issue_passes_only_proven_kv_host_summaries) { + Fixture f; + rad_op op = f.add_op("rope", "M", { 8192 }, { k_pre }, { "prefill" }, nullptr, {}); + CHECK_OK(f.start()); + + int32_t used_device[2] = { 896, 1152 }, used_host[2] = { 896, 1152 }; + int32_t positions_device[128]{}, positions_host[128]{}; + for (int i = 0; i < 128; ++i) positions_host[i] = 2048 + i; + RadKVGroupBatch kv{}; + kv.group = 1; + kv.seqused = used_device; + kv.seqused_h = used_host; + int32_t used_max[1] = { 1152 }; + f.batch.n_kv_groups = 1; + f.batch.kv = &kv; + f.batch.kv_used_max_h = used_max; + f.batch.positions = positions_device; + f.batch.positions_h = positions_host; + + auto issue = [&](const int32_t* ptr, int rows, int batch_seqs, int phase, int n_spec) -> int32_t { + f.batch.phase = phase; + f.batch.n_tok = 128; + f.batch.n_seq = batch_seqs; + f.batch.n_spec = n_spec; + g_rec.clear(); + g_body = [&](Ctx& c) { + const RadOperand arg[1] = { RAD_P_T2(ptr, RAD_I32, rows, 1) }; + (void)c.issue(op, arg, 1, RAD_N_BATCH); + }; + const int status = f.ctx.run_step(&f.batch); + g_body = nullptr; + if (status < 0 || g_rec.size() != 1) return -999; + return g_rec[0].kv_used_max_h; + }; + + CHECK_EQ(issue(used_device, 2, 2, RAD_PHASE_PREFILL, 0), 1152); // full group maximum + CHECK_EQ(issue(used_device + 1, 1, 2, RAD_PHASE_PREFILL, 0), 1152); // exact sequence slice + CHECK_EQ(issue(used_device, 1, 2, RAD_PHASE_PREFILL, 0), 896); // first sequence only + CHECK_EQ(issue(used_device, 3, 2, RAD_PHASE_PREFILL, 0), -1); // out-of-range span + CHECK_EQ(issue(used_device + 1, 1, 2, RAD_PHASE_MIXED, 0), 1152); // mixed prefill slice + CHECK_EQ(issue(used_device, 2, 2, RAD_PHASE_PREFILL, 1), 1152); // exact speculative operand + CHECK_EQ(issue(positions_device + 64, 1, 1, RAD_PHASE_PREFILL, 0), positions_host[64]); + CHECK_EQ(issue(positions_device + 128, 1, 1, RAD_PHASE_PREFILL, 0), -1); // past step end + CHECK_EQ(issue(positions_device + 64, 1, 1, RAD_PHASE_MIXED, 0), -1); + CHECK_EQ(issue(positions_device + 64, 1, 1, RAD_PHASE_PREFILL, 1), -1); +} + TEST(the_prefill_geometry_key_reuses_equal_grids_and_separates_changed_grids) { Fixture f; f.add_buffer("x", RAD_BF16, 64, 4, 0); diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index a5b770a4..df5dfa90 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -6977,6 +6977,10 @@ TEST(a_preempted_ring_sequence_replays_its_output_a_token_a_step) { REQUIRE(chunk != nullptr); REQUIRE_EQ(f.sch.plan().e.size(), (size_t)1); const RadKVGroupBatch& paged = chunk->kv[0]; + REQUIRE(paged.seqused_h != nullptr); + REQUIRE(chunk->positions_h != nullptr); + CHECK_EQ(paged.seqused_h[0], paged.seqused[0]); + CHECK_EQ(chunk->positions_h[0], chunk->positions[0]); int32_t used_max = 0; for (int64_t i = 0; i < chunk->n_seq; ++i) used_max = std::max(used_max, paged.seqused[i]); @@ -6993,6 +6997,10 @@ TEST(a_preempted_ring_sequence_replays_its_output_a_token_a_step) { const RadBatch* rb = f.sch.step(); REQUIRE(rb != nullptr); CHECK_EQ(rb->kv_used_max_h[0], rb->kv[0].seqused[0]); + REQUIRE(rb->kv[0].seqused_h != nullptr); + REQUIRE(rb->positions_h != nullptr); + CHECK_EQ(rb->kv[0].seqused_h[0], rb->kv[0].seqused[0]); + CHECK_EQ(rb->positions_h[0], rb->positions[0]); CHECK_EQ(f.sch.plan().e[0].ctx_len, 160); CHECK_EQ(f.sch.plan().e[0].n_tokens, 1); CHECK(f.sch.plan().e[0].produces_token);