From 7f5218ee5fd3e78629866195c4cc9c890f6cddf0 Mon Sep 17 00:00:00 2001 From: mici81 Date: Thu, 5 Nov 2026 08:49:52 +0100 Subject: [PATCH] qwen4exp: decode, verify and draft passes are recorded, keyed by their split cap (issue #402) pass_key() answered 0 for every draft and verify pass, and with a callback present the core neither records such a pass nor gives its launches the keyed context bound, so with the MTP drafter nearly every decode pass was issued op by op (1.17.0: 206 passes played and 508,236 issued unkeyed, where 1.15.1 played 492,894). Dropping the export would record them with the cores bucketed bound, which --- arch/qwen4exp_fp8/qwen4exp_fp8.cpp | 34 +++++++++++++----- tests/arch_test.cpp | 57 +++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index d111352d..da49a3c0 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -1700,16 +1700,34 @@ static void step(RadCtx* c, const RadBatch* batch) { uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) { if (!c || !batch) return 0; if (batch->enc) return 1; - /* 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; + /* Mixed batches split the query rows into decode and prefill subsets before attention; the + * whole-batch host summary cannot key those subset geometries exactly. */ + if (batch->phase == RAD_PHASE_MIXED) return 0; Model& m = g_model[rad_rank(c)]; + /* Decode, verify and draft passes (the MTP head attends over the same KV group): every + * attention launch covers all the pass's sequences, and the shape fields the core keys (n_tok, + * n_seq, max_q_len, n_spec, draft_pass) fix each launch's q_len band and units. The live KV + * length enters a split launcher's count only through the cap of eight 48-key tiles a split + * (rad_prefill_splits_law; every head_dim 256 launcher has P_TILE 48), and from 64 splits up + * the cap no longer binds. Keying that cap level lets these passes be recorded while each launch + * still splits by its exact live length: answering 0 left them unrecorded (issue #402), and the + * core's keyed bound for a plugin without the callback rounds the length up a bucket, which split + * a short single-stream verify into several partials with a merge (x1 203 against 219 tok/s). */ + if (batch->phase != RAD_PHASE_PREFILL || batch->draft_pass != 0 || batch->n_spec > 0) { + if (!m.kv_full || !used_max_h || !batch->kv) return 0; + int group = -1; + for (int i = 0; i < batch->n_kv_groups; ++i) + if (batch->kv[i].group == m.kv_full) { group = i; break; } + if (group < 0) return 2; // no full-attention KV in this pass: nothing splits by length + const int used = used_max_h[group]; + if (used <= 0) return 0; // Missing host summary is not the same as an empty KV cache. + constexpr int tile = 48; + const int ctx = (m.g.max_ctx > 0 && used >= m.g.max_ctx) ? (int)m.g.max_ctx : used; + const int cap = std::min(((ctx + tile - 1) / tile) / 8, 64); + return 3 + (uint64_t)cap; + } + bool has_full_attention = false; for (const Layer& layer : m.layers) { if (!layer.full) continue; diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 86f54837..f4e7c2db 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -1928,16 +1928,63 @@ TEST(qwen4exp_pass_key_reuses_only_proven_uniform_prefill_grids) { 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; + batch.phase = RAD_PHASE_PREFILL; batch.draft_pass = 0; 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); } +/* Decode, verify and draft passes are recorded, keyed by the cap their live KV length puts on a + * split launcher (eight 48-key tiles a split, binding below 64 splits): one key within a level, another + * across it, the same past the last level, and none without a host summary or in a mixed pass. A pass + * left unkeyed (0) is issued op by op on every step (issue #402). */ +TEST(qwen4exp_pass_key_keys_decode_verify_and_draft_by_the_split_cap) { + RadModelMeta meta = qwen4exp_meta(); + RadBuildCtx build = build_ctx(0, 1); + build.max_tok = 64; build.max_seqs = 2; build.max_spec = 5; build.max_ctx = 65536; + 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_DECODE; batch.n_spec = 5; + batch.n_tok = 6; batch.n_seq = 1; batch.max_q_len = 6; + batch.max_ctx_len = 1024; batch.n_kv_groups = 1; batch.kv = &group; + int32_t used[1] = {400}; /* 9 tiles: level 1 (337-720 keys) */ + const uint64_t verify1 = qwen4exp_fp8::pass_key(&ctx, &batch, used); + CHECK(verify1 != 0); + used[0] = 720; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), verify1); + used[0] = 721; /* 16 tiles: level 2 */ + CHECK(qwen4exp_fp8::pass_key(&ctx, &batch, used) != verify1); + used[0] = 300; /* 7 tiles: level 0 */ + CHECK(qwen4exp_fp8::pass_key(&ctx, &batch, used) != verify1); + used[0] = 30000; /* past 64 splits' 24,576 keys: one key */ + const uint64_t verify_long = qwen4exp_fp8::pass_key(&ctx, &batch, used); + used[0] = 60000; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), verify_long); + + used[0] = 400; + batch.n_spec = 0; batch.n_tok = 1; batch.max_q_len = 1; + CHECK(qwen4exp_fp8::pass_key(&ctx, &batch, used) != 0); /* plain decode */ + batch.draft_pass = -1; + CHECK(qwen4exp_fp8::pass_key(&ctx, &batch, used) != 0); /* the MTP head's draft pass */ + + used[0] = 0; + CHECK_EQ(qwen4exp_fp8::pass_key(&ctx, &batch, used), (uint64_t)0); + used[0] = 400; batch.draft_pass = 0; batch.phase = RAD_PHASE_MIXED; + 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);