From f8e69c4825e713dd598a863f2d9cb2e3c5ff4faf Mon Sep 17 00:00:00 2001 From: mici81 Date: Fri, 30 Oct 2026 09:16:16 +0100 Subject: [PATCH] libr4d decode split law: bound the three-wave verify floor by workgroups (#342) 49ca5ff made a three-wave verify past 16k keep at least 64 segments a sequence. The declared scratch is the worst over every sequence count up to max_seqs, so at 256 sequences it became 64x the law's: h256/gqa12 at max_ctx 200000 went from 7.62 to 487.5 MiB a card, and the KV pool lost that (#342: 5-8 % prefill on an expert-tiered MoE). The floor is now min(64, 1024 / units), units = KV heads x sequences. Up to eight sequences of two KV heads it stays 64; at 16 it is 32, at 256 two, and the declared region stays at what eight sequences need. Measured on 2x R9700, Qwen3.8-27B MXFP4 TP2, k6_v4, speculation 7 without the short block (every step an 8-row verify), forced segment counts, ms a decode step: 45k x8 64: 41.24, 32: 41.20, 16: 42.29 45k x12 64: 57.42, 32: 57.38, 16: 59.39 45k x16 64: 74.83, 32: 74.68, 16: 74.79 90k x8 64: 51.79, 32: 52.02, 16: 54.42 Up to 64 segments past eight sequences changes nothing; the new floor gives x12 42 and x16 32. r4d_selftest's split-law case pins the region at 16 sequences (100k and 32k: 32 segments) and at 256 (2); it read 64 for all three before. Co-Authored-By: Claude Opus 5.5 --- libr4d/r4d_attn_decode_h256_gqa6.hip | 11 ++++++++++- libr4d/r4d_selftest.cpp | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/libr4d/r4d_attn_decode_h256_gqa6.hip b/libr4d/r4d_attn_decode_h256_gqa6.hip index 80952e4a..ff3f161c 100644 --- a/libr4d/r4d_attn_decode_h256_gqa6.hip +++ b/libr4d/r4d_attn_decode_h256_gqa6.hip @@ -1062,6 +1062,12 @@ void r4d_attn_splitkv_combine_gq_kernel(const R4DArgs a, int splits, int tile, // plain decode, the 4-row short block (16k x16 38.94 -> 38.90) and a verify up to 16k (1k x1-x8) // keep their segment count. Greedy output is unchanged. static constexpr int kVerifySplitsMin = 64; +// The floor is also bounded by the batch: at most kVerifyFloorWorkgroups workgroups (segments x KV +// heads x sequences). The declared scratch is the worst over every sequence count up to max_seqs, +// and 64 a sequence at 256 sequences made it 64x the law's (h256/gqa12 at max_ctx 200000: 7.62 -> +// 487.5 MiB a card, issue #342). Up to eight sequences of two KV heads the floor stays 64; at 16 it +// is 32, at 256 two, and the region stays at what eight sequences need. +static constexpr int kVerifyFloorWorkgroups = 1024; static constexpr int kVerifyMinFrom = 16384; static constexpr int kVerifyDeepFrom = 65536; static constexpr int kVerifyDeepMax = 256; @@ -1089,7 +1095,10 @@ static inline int r4d_decode_splits(const R4DArgs* a, int tile) { const int depth = r4d_decode_depth(a); s = (depth > kVerifyDeepFrom ? 512 : 128) / units; if (s > kVerifyDeepMax) s = kVerifyDeepMax; - if (depth > kVerifyMinFrom && s < kVerifySplitsMin) s = kVerifySplitsMin; + if (depth > kVerifyMinFrom) { + const int floor = std::min(kVerifySplitsMin, kVerifyFloorWorkgroups / units); + if (s < floor) s = floor; + } } const int cap = tiles / 2; if (s > cap) s = cap; diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 87eaebc3..2e2f7869 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -1091,6 +1091,9 @@ static void decode_split_law_case(const Plugin& pl) { { 4, 8, 16384, 16, 20 }, { 2, 8, 16384, 32, 40 }, { 4, 8, 1024, 16, 20 }, { 1, 8, 1024, 32, 32 }, { 4, 8, 640, 16, 16 }, { 8, 4, 100000, 16, 16 }, { 16, 4, 100000, 8, 8 }, { 4, 1, 100000, 32, 32 }, { 1, 1, 100000, 128, 128 }, + /* Past eight sequences the floor shrinks to kVerifyFloorWorkgroups over the batch, so the region + * stays at what eight sequences need (issue #342: 64 a sequence at max_seqs 256 made it 64x). */ + { 16, 8, 100000, 32, 32 }, { 16, 8, 32768, 32, 32 }, { 256, 8, 100000, 2, 2 }, }; int bad = 0; for (const Want& w : want) {