From aa14eea76c3ab68815090e19a7ad9e5bcd45e65c Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 2 Nov 2026 17:34:24 -0500 Subject: [PATCH] prefill: fail closed when live split geometry is unknown Head-256 split-prefill launchers now declare that their grid depends on live KV. Without an architecture geometry key, those passes stay live instead of replaying a stale tape. Qwen keys only a proven one-sequence prefill. Plugin ABI 34. --- abi/rad_abi.h | 11 +++++-- abi/rad_abi_layout_rows.h | 2 ++ arch/qwen4exp_fp8/qwen4exp_fp8.cpp | 14 +++++++-- core/runtime/ctx.cpp | 11 +++++++ libr4d/r4d_rows.cpp | 36 +++++++++++----------- tests/abi_layout_table.h | 8 +++-- tests/abi_layout_test.cpp | 6 ++-- tests/arch_fakes.h | 1 + tests/arch_test.cpp | 49 ++++++++++++++++++++++++++++++ tests/runtime_test.cpp | 40 +++++++++++++++++++++++- 10 files changed, 148 insertions(+), 30 deletions(-) diff --git a/abi/rad_abi.h b/abi/rad_abi.h index 8a28681c..d45766f2 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 33u +#define RAD_ABI_VERSION 34u /* ================================================================== plugin identity */ /* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin: @@ -465,15 +465,20 @@ typedef struct RadKernelInfo { * against: a named skip, not a failure. */ RadUnrelayoutFn unrelayout; /* The chain of conventional ops this kernel computes in one launch, or null if it is not a - * fusion. Non-null also counts in rad-kbench's fusion-claim column. + * fusion. Non-null also counts in rad-kbench's fusion-claim column. */ + RadFuseFn replaces; + /* Flags describing runtime behavior relevant to core safety decisions. Zero means none. * * Keep this field last. Kernel tables are positional aggregate initialisers * (`{..., nullptr, nullptr, r4d_rad_embed_lookup, nullptr, ...}`), so a field inserted * anywhere else shifts every later initialiser, and since `nullptr` converts to any function * pointer type, part of that shift would still compile. Append new fields after this one. */ - RadFuseFn replaces; + uint64_t capabilities; } RadKernelInfo; +/* This row's launch grid can depend on live seqused during prefill. */ +#define RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT (1ull << 0) + /* ================================================================== kernel plugin exports */ /* Every kernel plugin exports the whole set. The first plugin in hierarchy order to declare an op * fixes its schema; a later plugin declaring the same op name with a different schema is refused diff --git a/abi/rad_abi_layout_rows.h b/abi/rad_abi_layout_rows.h index 8966469e..ee0cd751 100644 --- a/abi/rad_abi_layout_rows.h +++ b/abi/rad_abi_layout_rows.h @@ -95,6 +95,7 @@ ABI_FIELD(RadBatch, plugin_cu) 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_STRUCT(RadBufDecl) ABI_FIELD(RadBufDecl, dtype) ABI_FIELD(RadBufDecl, rank) @@ -543,6 +544,7 @@ ABI_FIELD(RadKernelInfo, relayout) ABI_FIELD(RadKernelInfo, opd_shape) ABI_FIELD(RadKernelInfo, unrelayout) ABI_FIELD(RadKernelInfo, replaces) +ABI_FIELD(RadKernelInfo, capabilities) ABI_STRUCT(RadLayout) ABI_FIELD(RadLayout, tag) ABI_FIELD(RadLayout, dtype) diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index 59dbe537..9572fd5e 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -1703,6 +1703,10 @@ uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) { /* MTP owns another attention path on the same KV group. The present batch key cannot prove * whether that head's attention row takes a split grid, so keep only those passes live. */ if (batch->draft_pass != 0) return 0; + /* Mixed batches split the query rows into decode and prefill subsets before attention, and a + * speculative verify can choose another q_len band. The whole-batch host summary cannot key + * those subset geometries exactly. */ + if (batch->phase == RAD_PHASE_MIXED || batch->n_spec > 0) return 0; if (batch->phase != RAD_PHASE_PREFILL) return 1; Model& m = g_model[rad_rank(c)]; @@ -1722,12 +1726,18 @@ uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) { if (batch->kv[i].group == m.kv_full) { group = i; break; } if (group < 0) return 0; - const int q_len = (int)batch->max_q_len; + /* 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; + const int q_len = (int)batch->n_tok; const int total_q = (int)batch->n_tok; - const int num_seqs = (int)batch->n_seq; + const int num_seqs = 1; const int kv_heads = (int)m.g.n_head_kv; const int used = used_max_h[group]; + if (used <= 0) return 0; // Missing host summary is not the same as an empty KV cache. const int wgps = rad_ctx_wgp_count(c); + if (wgps <= 0) return 0; 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, diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index bdd48fb1..47a7c185 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -957,6 +957,17 @@ 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; + /* 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. */ + if (!arch_pass_key_ && + (b->phase == RAD_PHASE_PREFILL || b->phase == RAD_PHASE_MIXED || b->draft_pass != 0 || + b->n_spec > 0)) { + for (const IssuePlan& ip : issue_table_) + if (ip.row && ip.row->info && + (ip.row->info->capabilities & RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT)) + return 0; + } /* A pass carrying a request's words for the plugin is shaped by them (a head issues per * sequence from their counts), and such a step samples a prompt's first token, which is not * the repeating decode pass. */ diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 00b03d8a..b87c4bb8 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -6642,7 +6642,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillFp8), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8, r4d_rad_attn_prefill_h256_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, /* Lossy: they match only a caller whose `prefill_fp8` accepts their legs. */ { "attn_prefill_h256_gqa6_fp8kv_qk8", "attn_paged", "attn", @@ -6683,7 +6683,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillFp8v4), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8v4, r4d_rad_attn_prefill_h256_v4_scratch, nullptr, nullptr, - nullptr }, + nullptr, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_prefill_h256_gqa6_fp8v4_qk8", "attn_paged", "attn", "paged causal attention, query-tiled (prefill / chunked prefill), Q.K^T as e4m3 WMMA", @@ -6726,7 +6726,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillK6v4), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_k6v4, r4d_rad_attn_prefill_h256_k6_scratch, nullptr, nullptr, - nullptr }, + nullptr, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_prefill_h256_gqa6_k6v4_qk8", "attn_paged", "attn", "paged causal attention, query-tiled (prefill / chunked prefill), Q.K^T as e4m3 WMMA", @@ -6769,7 +6769,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillBf16), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_bf16, r4d_rad_attn_prefill_h256_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa6_fp8kv", "attn_paged", "attn", "paged causal attention, split-KV (decode / speculative verify)", @@ -6842,7 +6842,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillH256Gqa2Fp8), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa2_fp8, r4d_rad_attn_prefill_h256_gqa2_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_prefill_h256_gqa2_bf16kv", "attn_paged", "attn", "paged causal attention with a sliding window, query-tiled (prefill / chunked prefill)", @@ -6853,7 +6853,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillH256Gqa2Bf16), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa2_bf16, r4d_rad_attn_prefill_h256_gqa2_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa2_fp8kv", "attn_paged", "attn", "paged causal attention with a sliding window, split-KV (decode / verify)", @@ -7031,7 +7031,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillH256G8Fp8), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa8_fp8, r4d_rad_attn_prefill_h256_gqa8_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_prefill_h256_gqa8_bf16kv", "attn_paged", "attn", "paged causal attention, query-tiled (prefill / chunked prefill)", @@ -7041,7 +7041,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillH256G8Bf16), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa8_bf16, r4d_rad_attn_prefill_h256_gqa8_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa8_fp8kv", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", @@ -7071,7 +7071,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillH256G4Fp8), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_fp8, r4d_rad_attn_prefill_h256_gqa4_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_prefill_h256_gqa4_bf16kv", "attn_paged", "attn", "paged causal attention, query-tiled (prefill / chunked prefill)", @@ -7081,7 +7081,7 @@ static const RadKernelInfo kKernels[] = { ROW(cAttnPrefillH256G4Bf16), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_bf16, r4d_rad_attn_prefill_h256_gqa4_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa4_fp8kv", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", @@ -7182,7 +7182,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G12Fp8), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_fp8, r4d_rad_attn_prefill_h256_gqa12_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_prefill_h256_gqa12_bf16kv", "attn_paged", "attn", "paged causal attention, query-tiled (prefill / chunked prefill)", @@ -7192,7 +7192,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G12Bf16), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_bf16, r4d_rad_attn_prefill_h256_gqa12_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa12_fp8kv", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", @@ -7250,7 +7250,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G6Bs4Fp8), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_fp8, r4d_rad_attn_prefill_h256_gqa6_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_prefill_h256_gqa6_bs4_bf16kv", "attn_paged", "attn", "paged causal attention, query-tiled (prefill / chunked prefill)", @@ -7260,7 +7260,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G6Bs4Bf16), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_bf16, r4d_rad_attn_prefill_h256_gqa6_scratch, nullptr, nullptr, - r4d_shape_attn_paged }, + r4d_shape_attn_paged, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa6_bs4_fp8kv", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", @@ -7316,7 +7316,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G12Fp8v4), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_fp8v4, r4d_rad_attn_prefill_h256_gqa12_fp8v4_scratch, nullptr, nullptr, - nullptr }, + nullptr, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa12_fp8v4", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", @@ -7347,7 +7347,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G12K6v4), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_k6v4, r4d_rad_attn_prefill_h256_gqa12_k6v4_scratch, nullptr, nullptr, - nullptr }, + nullptr, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa12_k6v4", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", @@ -7378,7 +7378,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G6Bs4Fp8v4), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_fp8v4, r4d_rad_attn_prefill_h256_gqa6_fp8v4_scratch, nullptr, nullptr, - nullptr }, + nullptr, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa6_bs4_fp8v4", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", @@ -7409,7 +7409,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) ROW(cAttnPrefillH256G6Bs4K6v4), NOTUNE, nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_k6v4, r4d_rad_attn_prefill_h256_gqa6_k6v4_scratch, nullptr, nullptr, - nullptr }, + nullptr, nullptr, nullptr, RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT }, { "attn_decode_h256_gqa6_bs4_k6v4", "attn_paged", "attn", "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", diff --git a/tests/abi_layout_table.h b/tests/abi_layout_table.h index 2a5bdae9..22449f28 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 33 and +/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 34 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. */ @@ -33,7 +33,7 @@ { "RadArgs", "instance", 48, 8 }, { "RadArgs", "rank", 56, 4 }, { "RadArgs", "world_size", 60, 4 }, -{ "RadBatch", "", 0, 416 }, +{ "RadBatch", "", 0, 424 }, { "RadBatch", "phase", 0, 4 }, { "RadBatch", "step", 4, 4 }, { "RadBatch", "n_tok", 8, 8 }, @@ -91,6 +91,7 @@ { "RadBatch", "plugin_words_h", 392, 8 }, { "RadBatch", "plugin_cu_h", 400, 8 }, { "RadBatch", "sgen_b", 408, 8 }, +{ "RadBatch", "kv_used_max_h", 416, 8 }, { "RadBufDecl", "", 0, 80 }, { "RadBufDecl", "dtype", 0, 4 }, { "RadBufDecl", "rank", 4, 4 }, @@ -516,7 +517,7 @@ { "RadKVStream", "host_layer_stride", 48, 8 }, { "RadKVStream", "slots", 56, 8 }, { "RadKVStream", "host_bytes", 64, 8 }, -{ "RadKernelInfo", "", 0, 168 }, +{ "RadKernelInfo", "", 0, 176 }, { "RadKernelInfo", "name", 0, 8 }, { "RadKernelInfo", "op", 8, 8 }, { "RadKernelInfo", "family", 16, 8 }, @@ -539,6 +540,7 @@ { "RadKernelInfo", "opd_shape", 144, 8 }, { "RadKernelInfo", "unrelayout", 152, 8 }, { "RadKernelInfo", "replaces", 160, 8 }, +{ "RadKernelInfo", "capabilities", 168, 8 }, { "RadLayout", "", 0, 80 }, { "RadLayout", "tag", 0, 8 }, { "RadLayout", "dtype", 8, 4 }, diff --git a/tests/abi_layout_test.cpp b/tests/abi_layout_test.cpp index fa626180..d651722a 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 32 and RAD_FORMAT_VER 2. Under + * the layout agrees with the table below, taken at RAD_ABI_VERSION 34 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 32 / RAD_FORMAT_VER 2 (see the file header). */ -constexpr uint32_t kTableAbi = 33, kTableFormat = 2; +/* The layout at RAD_ABI_VERSION 34 / RAD_FORMAT_VER 2 (see the file header). */ +constexpr uint32_t kTableAbi = 34, kTableFormat = 2; const AbiRow kTable[] = { #include "abi_layout_table.h" }; diff --git a/tests/arch_fakes.h b/tests/arch_fakes.h index c30d3419..ecc1ae4e 100644 --- a/tests/arch_fakes.h +++ b/tests/arch_fakes.h @@ -524,6 +524,7 @@ extern "C" int rad_step_fail(RadCtx* c, const char* what) { extern "C" const RadBatch* rad_batch(RadCtx* c) { return c->batch; } extern "C" int rad_rank(RadCtx* c) { return c->rank; } extern "C" int rad_world_size(RadCtx* c) { return c->world; } +extern "C" int rad_ctx_wgp_count(RadCtx*) { return 32; } /* The context itself, so a copy issued on the step's stream is recorded in it (rad_memcpy_async). */ extern "C" RadStream rad_stream(RadCtx* c) { return (RadStream)c; } extern "C" int rad_stream_sync(RadStream s) { (void)s; return RAD_OK; } diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 96fcc005..c2b6da08 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -1882,6 +1882,55 @@ RadModelMeta qwen4exp_meta() { } } /* namespace */ +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; + RadBuilder graph; + REQUIRE_EQ(qwen4exp_fp8::declare(&graph, &meta, &build), RAD_OK); + rad_kvgroup kv_full = 0; + for (size_t i = 0; i < graph.kv_groups.size(); ++i) + if (graph.kv_groups[i].name.find("kv_attn") != std::string::npos) { + kv_full = (rad_kvgroup)(i + 1); + break; + } + REQUIRE(kv_full != 0); + + RadCtx ctx{}; + RadKVGroupBatch group{}; + group.group = kv_full; + 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; + int32_t used[1] = {2304}; + const uint64_t key6 = qwen4exp_fp8::pass_key(&ctx, &batch, used); + CHECK(key6 != 0); + used[0] = 2305; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), key6); + used[0] = 3072; + CHECK(qwen4exp_fp8::pass_key(&ctx, &batch, used) != key6); + + /* 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; + const uint64_t key_chunk = qwen4exp_fp8::pass_key(&ctx, &batch, used); + CHECK(key_chunk != 0); + CHECK(key_chunk != key6); + batch.n_tok = 64; batch.max_q_len = 64; + + batch.phase = RAD_PHASE_MIXED; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), (uint64_t)0); + batch.phase = RAD_PHASE_DECODE; batch.n_spec = 1; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), (uint64_t)0); + batch.n_spec = 0; batch.draft_pass = -1; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), (uint64_t)0); + batch.draft_pass = 0; batch.phase = RAD_PHASE_PREFILL; batch.n_seq = 2; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), (uint64_t)0); + batch.n_seq = 1; used[0] = 0; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), (uint64_t)0); +} + TEST(qwen4exp_yarn_reaches_attention_indexer_and_compressed_keys) { RadModelMeta meta = qwen4exp_meta(); std::vector keys(meta.kv_key, meta.kv_key + meta.n_kv); diff --git a/tests/runtime_test.cpp b/tests/runtime_test.cpp index b9f160be..f02e8473 100644 --- a/tests/runtime_test.cpp +++ b/tests/runtime_test.cpp @@ -2178,12 +2178,14 @@ int k_copy(const RadArgs* a, RadStream s) { } /* A host row for every band of `op`, from a library that is not the reference. */ -void attach_host_row(Fixture& f, rad_op op, RadLaunchFn fn, const char* name) { +void attach_host_row(Fixture& f, rad_op op, RadLaunchFn fn, const char* name, + uint64_t capabilities = 0) { for (rad::Band& band : f.P.ops[op].bands) { f.infos.push_back(RadKernelInfo{}); RadKernelInfo& ki = f.infos.back(); ki.name = name; ki.op = "rope"; ki.family = "test"; ki.domain = RAD_DOMAIN_HOST; ki.launch = fn; + ki.capabilities = capabilities; f.rows.push_back(rad::KernelRow{}); rad::KernelRow& kr = f.rows.back(); kr.info = &ki; @@ -2341,6 +2343,42 @@ static uint64_t test_prefill_geometry_key(RadCtx* c, const RadBatch* b, const in return rad_prefill_grid_key((int)b->max_q_len, bq, splits, kv_heads, (int)b->n_seq, threads); } +TEST(legacy_prefill_keying_only_fails_closed_for_dynamic_split_rows) { + { + Fixture f; + f.add_buffer("x", RAD_BF16, 64, 4, 0); + rad_op op = loose_op(f); + attach_host_row(f, op, k_host_read, "unaffected_prefill"); + CHECK_OK(f.start()); + + RadBatch b{}; + b.phase = RAD_PHASE_PREFILL; + b.n_tok = 128; b.n_seq = 1; b.max_q_len = 128; + CHECK(f.ctx.pass_key(&b) != 0); // Unaffected legacy prefill retains reuse. + } + { + Fixture f; + f.add_buffer("x", RAD_BF16, 64, 4, 0); + rad_op op = loose_op(f); + attach_host_row(f, op, k_host_read, "live_split_prefill", + RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT); + CHECK_OK(f.start()); + + RadBatch b{}; + b.phase = RAD_PHASE_PREFILL; + b.n_tok = 128; b.n_seq = 1; b.max_q_len = 128; + CHECK_EQ(f.ctx.pass_key(&b), (uint64_t)0); + b.phase = RAD_PHASE_MIXED; + CHECK_EQ(f.ctx.pass_key(&b), (uint64_t)0); + b.phase = RAD_PHASE_DECODE; b.n_spec = 1; + CHECK_EQ(f.ctx.pass_key(&b), (uint64_t)0); + b.n_spec = 0; b.draft_pass = -1; + CHECK_EQ(f.ctx.pass_key(&b), (uint64_t)0); + b.draft_pass = 0; + CHECK(f.ctx.pass_key(&b) != 0); // Ordinary decode remains recordable. + } +} + TEST(the_prefill_geometry_key_reuses_equal_grids_and_separates_changed_grids) { Fixture f; f.add_buffer("x", RAD_BF16, 64, 4, 0);