From f692c3f446b1e416930ae571ed957cd2c2bd89d1 Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 2 Nov 2026 21:55:15 -0500 Subject: [PATCH] spark: key BF16 and IQ8R prefill tape geometry --- arch/spark25_w8a8/spark25_w8a8.cpp | 33 ++++++++++++++++++++++++++++++ tests/spark25_arch_test.cpp | 23 ++++++++++++++++----- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/arch/spark25_w8a8/spark25_w8a8.cpp b/arch/spark25_w8a8/spark25_w8a8.cpp index c3113ced..f81f1e65 100644 --- a/arch/spark25_w8a8/spark25_w8a8.cpp +++ b/arch/spark25_w8a8/spark25_w8a8.cpp @@ -13,6 +13,7 @@ #include #include #include "../spark25_bf16/chat.h" +#include "../spark25_bf16/spark25_prefill_pass_key.h" #include #include @@ -536,6 +537,32 @@ 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 spark25_bf16::spark_chat_format(); } } #ifndef RAD_ARCH_NO_EXPORTS @@ -543,4 +570,10 @@ RAD_ARCH_PLUGIN(spark25_w8a8, "spark25", "w8a8_fwht128", "0.1.0", "Spark X2.6 with an int8 trunk: i8 weights with a bf16 scale a 128 of K, rotated " "by a 128-wide Hadamard, against int8 activations the same rotation quantises") RAD_ARCH_CHAT_FORMAT(spark25_w8a8) +#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_w8a8::pass_key(c, batch, used_max_h); +} +#endif #endif diff --git a/tests/spark25_arch_test.cpp b/tests/spark25_arch_test.cpp index 77254877..44701ef1 100644 --- a/tests/spark25_arch_test.cpp +++ b/tests/spark25_arch_test.cpp @@ -386,17 +386,30 @@ TEST(spark_prefill_keys_match_live_r4d_split_grid) { 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); + RadBuilder bf16_builder; + REQUIRE_EQ(spark25_bf16::declare(&bf16_builder, 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); + const uint64_t bf16_k4 = spark25_bf16::pass_key(&c, &batch, used); + REQUIRE(bf16_k4 != 0); used[0] = 2048; - CHECK(spark25_bf16::pass_key(&c, &batch, used) != plugin_k4); + CHECK(spark25_bf16::pass_key(&c, &batch, used) != bf16_k4); + + MetaKV iqmeta = spark_meta(); + iqmeta.m.quant = "w8a8_fwht128"; + iqmeta.set("quantization_config.quant_method", "w8a8") + .set("quantization_config.fmt", "fwht128"); + RadBuilder iq_builder; spark_iq8r_encodings(iq_builder); + REQUIRE_EQ(spark25_w8a8::declare(&iq_builder, iqmeta.meta(), &ctx), RAD_OK); + kv.group = spark25_w8a8::models[0].kv; + used[0] = 1536; + const uint64_t iq_k4 = spark25_w8a8::pass_key(&c, &batch, used); + REQUIRE(iq_k4 != 0); + used[0] = 2048; + CHECK(spark25_w8a8::pass_key(&c, &batch, used) != iq_k4); #else CHECK_EQ(spark25_prefill_key::pass_key({}), (uint64_t)0); #endif