diff --git a/arch/spark25_bf16/spark25_bf16.cpp b/arch/spark25_bf16/spark25_bf16.cpp index dee6a0a3..c9cc80b0 100644 --- a/arch/spark25_bf16/spark25_bf16.cpp +++ b/arch/spark25_bf16/spark25_bf16.cpp @@ -12,6 +12,7 @@ */ #include #include "chat.h" +#include "spark25_prefill_pass_key.h" #include #include @@ -432,9 +433,41 @@ static void step(RadCtx* c, const RadBatch* batch) { } } +#ifdef SPARK25_HAVE_PREFILL_SPLIT +static uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) { + if (!c || !batch) return 0; + Model& m = models[rad_rank(c)]; + int group = -1; + if (batch->kv) for (int i = 0; i < batch->n_kv_groups; ++i) + if (batch->kv[i].group == m.kv) { group = i; break; } + spark25_prefill_key::Request r; + r.phase = batch->phase; + r.n_tok = batch->n_tok; + r.n_seq = batch->n_seq; + r.max_q_len = batch->max_q_len; + r.max_ctx = m.g.max_ctx; + r.n_spec = batch->n_spec; + r.draft_pass = batch->draft_pass; + r.enc = batch->enc != 0; + r.head_dim = m.g.head_dim; + r.gqa = m.g.gqa(); + r.kv_heads = m.g.n_head_kv; + r.kv_group_found = group >= 0; + r.used = used_max_h && group >= 0 ? used_max_h[group] : 0; + r.wgps = rad_ctx_wgp_count(c); + return spark25_prefill_key::pass_key(r); +} +#endif + static const RadChatFormat* chat_format(const RadModelMeta*) { return spark_chat_format(); } } #ifndef RAD_ARCH_NO_EXPORTS RAD_ARCH_PLUGIN(spark25_bf16, "spark25", "", "0.1.0", "Spark X2.6 BF16") RAD_ARCH_CHAT_FORMAT(spark25_bf16) +#ifdef SPARK25_HAVE_PREFILL_SPLIT +extern "C" uint64_t rad_arch_pass_key(RadCtx* c, const RadBatch* batch, + const int32_t* used_max_h) { + return spark25_bf16::pass_key(c, batch, used_max_h); +} +#endif #endif diff --git a/arch/spark25_bf16/spark25_prefill_pass_key.h b/arch/spark25_bf16/spark25_prefill_pass_key.h new file mode 100644 index 00000000..a733f4ca --- /dev/null +++ b/arch/spark25_bf16/spark25_prefill_pass_key.h @@ -0,0 +1,67 @@ +/* Spark's prefill pass-key geometry. Keep this host-pure and mirror the h256/GQA4 + * R4D launcher exactly; unsupported batches fail closed. */ +#pragma once + +#include + +#if defined(__has_include) +# if __has_include() +# include +# define SPARK25_HAVE_PREFILL_SPLIT 1 +# endif +#endif + +namespace spark25_prefill_key { + +constexpr int kNwarps = 24; +constexpr int kTile = 48; +constexpr int kGqa = 4; +constexpr int kHeadDim = 256; +constexpr int kBq = kNwarps * 16 / kGqa; +constexpr int kThreads = kNwarps * 32; +constexpr int kDecodeRowCap = 64; +constexpr int kPhasePrefill = 0; +constexpr int kPhaseDecode = 1; +constexpr int kPhaseMixed = 2; + +static_assert(kBq == 96, "Spark GQA4 prefill BQ must match the R4D launcher"); +static_assert(kThreads == 768, "Spark prefill launcher uses 768 threads"); + +struct Request { + int phase = kPhasePrefill; + int64_t n_tok = 0, n_seq = 0, max_q_len = 0, max_ctx = 0; + int64_t n_spec = 0, draft_pass = 0; + int enc = 0; + int64_t head_dim = 0, gqa = 0, kv_heads = 0; + int kv_group_found = 0, used = 0, wgps = 0; +}; + +#ifdef SPARK25_HAVE_PREFILL_SPLIT +inline uint64_t pass_key(const Request& r) { + if (r.enc) return 1; + if (r.draft_pass != 0) return 0; + /* Mixed and speculative passes can launch attention on sub-batches whose exact geometry + * is not represented by the whole-batch summary. */ + if (r.phase == kPhaseMixed || r.n_spec > 0) return 0; + if (r.phase != kPhasePrefill) return 1; + if (r.head_dim != kHeadDim || r.gqa != kGqa || r.kv_heads <= 0 || !r.kv_group_found) + return 0; + /* For a singleton, these equalities prove the R4D q_len band and total_q used by its launch. + * Do not infer geometry for multi-sequence/ragged batches. */ + if (r.n_seq != 1 || r.n_tok <= 0 || r.max_q_len != r.n_tok || r.max_ctx <= 0 || + r.used <= 0 || r.wgps <= 0) + return 0; + /* The short-query path resolves to the decode launcher, with a different split law. */ + if (r.max_q_len * r.gqa <= kDecodeRowCap) return 0; + + const int q_len = (int)r.max_q_len; + const int splits = rad_prefill_splits_for_live(kBq, kTile, q_len, (int)r.n_tok, 1, + (int)r.kv_heads, (int)r.max_ctx, r.used, + r.wgps); + return rad_prefill_grid_key(q_len, kBq, splits, (int)r.kv_heads, 1, kThreads); +} +#else +inline uint64_t pass_key(const Request&) { return 0; } +#endif + +} // namespace spark25_prefill_key diff --git a/tests/spark25_arch_test.cpp b/tests/spark25_arch_test.cpp index 6860e85b..001ea614 100644 --- a/tests/spark25_arch_test.cpp +++ b/tests/spark25_arch_test.cpp @@ -278,6 +278,39 @@ TEST(tp1_works_without_collectives) { CHECK(std::find(issued.begin(), issued.end(), "all_reduce") == issued.end()); } +TEST(spark_prefill_keys_match_live_r4d_split_grid) { +#ifdef SPARK25_HAVE_PREFILL_SPLIT + spark25_prefill_key::Request r; + r.phase = RAD_PHASE_PREFILL; r.n_tok = r.max_q_len = 256; r.n_seq = 1; + r.max_ctx = 65536; r.head_dim = 256; r.gqa = 4; r.kv_heads = 4; + r.kv_group_found = 1; r.used = 1536; r.wgps = 32; + const uint64_t k4 = spark25_prefill_key::pass_key(r); + CHECK_EQ(rad_prefill_splits_for_live(96, 48, 256, 256, 1, 4, 65536, 1536, 32), 4); + r.used = 2048; + CHECK_EQ(rad_prefill_splits_for_live(96, 48, 256, 256, 1, 4, 65536, 2048, 32), 5); + CHECK(spark25_prefill_key::pass_key(r) != k4); + r = {}; r.phase = RAD_PHASE_PREFILL; r.n_tok = r.max_q_len = 256; r.n_seq = 2; + r.max_ctx = 65536; r.head_dim = 256; r.gqa = 4; r.kv_heads = 4; + r.kv_group_found = 1; r.used = 1536; r.wgps = 32; + CHECK_EQ(spark25_prefill_key::pass_key(r), (uint64_t)0); + + MetaKV meta = spark_meta(); + RadBuilder b; + RadBuildCtx ctx = build_ctx(); + REQUIRE_EQ(spark25_bf16::declare(&b, meta.meta(), &ctx), RAD_OK); + RadKVGroupBatch kv{}; kv.group = spark25_bf16::models[0].kv; + RadBatch batch{}; batch.phase = RAD_PHASE_PREFILL; batch.n_tok = batch.max_q_len = 256; + batch.n_seq = batch.n_kv_groups = 1; batch.kv = &kv; + int32_t used[1] = {1536}; RadCtx c{}; + const uint64_t plugin_k4 = spark25_bf16::pass_key(&c, &batch, used); + REQUIRE(plugin_k4 != 0); + used[0] = 2048; + CHECK(spark25_bf16::pass_key(&c, &batch, used) != plugin_k4); +#else + CHECK_EQ(spark25_prefill_key::pass_key({}), (uint64_t)0); +#endif +} + TEST(refuses_unimplemented_variants) { RadBuildCtx ctx = build_ctx(); for (const auto& change : {std::pair{"tie_word_embeddings", "0"},