diff --git a/libr4d/r4d_attn_decode_h256_gqa6.hip b/libr4d/r4d_attn_decode_h256_gqa6.hip index 23c04c46..93e8662a 100644 --- a/libr4d/r4d_attn_decode_h256_gqa6.hip +++ b/libr4d/r4d_attn_decode_h256_gqa6.hip @@ -1004,12 +1004,55 @@ void r4d_attn_splitkv_combine_gq_kernel(const R4DArgs a, int splits, int tile, // target were worse than 2x). At three waves (an 8-row verify) more workgroups only add // contention (+9 %). The split count already moves with the batch, so the merge sees no new // partition. +// +// Except that a three-wave workgroup reads the step's depth (r4d_decode_depth): past +// kVerifyMinFrom it keeps at least kVerifySplitsMin segments, what 128 gives one sequence of two +// KV heads, and past kVerifyDeepFrom it targets 512 workgroups (at most kVerifyDeepMax segments). +// At depth the 128 target starves an 8-row verify, each segment walking 2-18k tokens. Qwen3.8-27B +// at TP2 on the R9700, k6_v4, speculation 7, ms a decode step, forced segment counts +// (RADIANCE_TUNE=attn_paged:splits), the 128 target (16-21 at x3-x4, 10 at x6) -> 64: x3 45k +// 27.83 -> 27.56, 90k 32.61 -> 31.79, 180k 42.31 -> 40.47; x4 45k 29.96 -> 29.48, 90k 36.03 -> +// 34.89, 180k 48.74 -> 46.26; x6 45k 37.39 -> 36.11, 90k 47.74 -> 44.70. Deeper still two +// sequences want 128 (90k 26.73 -> 26.36, 180k 33.00 -> 32.01) and one 256 (180k 25.25 -> 24.93, +// 90k level), while four stay at 64 (128: 180k 45.94 vs 46.32, 90k 35.06 vs 35.00) and six lose at +// 128 (90k 44.91 vs 44.70): about 512 workgroups. Shallow the minimum costs up to half a percent +// (1k: x2 20.98 -> 21.01, x4 24.01 -> 24.15, x6 28.71 -> 28.86; x4 16k 25.80 -> 25.85), hence +// the floor. The kernel benchmark above (+9 % at x2) did not carry to the server. 64 is also the +// vectorised merge's limit and the count one sequence already splits at. +// Served against the 128 target, ms a decode step: x1 90k 22.07 -> 22.03, 180k 25.35 -> 24.88; +// x2 90k 26.83 -> 26.36, 180k 33.18 -> 31.99; x3 180k 42.41 -> 40.33; x4 90k 36.20 -> 34.96. A +// 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; +static constexpr int kVerifyMinFrom = 16384; +static constexpr int kVerifyDeepFrom = 65536; +static constexpr int kVerifyDeepMax = 256; + +// The deepest context the step can reach. At issue the block table is the step's: its width moves +// with the batch's longest sequence, rounded up (core/sched/batch.cpp, bound_bucket), while +// `max_ctx` is the declared bound, the model's context. Declare passes no table (max_blocks 0), so +// the scratch is sized at the declared bound, the deepest the law can be asked for; the law grows +// with depth, so an issue never needs more. +static inline int r4d_decode_depth(const R4DArgs* a) { + if (a->max_blocks > 0 && a->block_size > 0) { + const long long t = (long long)a->max_blocks * a->block_size; + if (t < a->max_ctx) return (int)t; + } + return a->max_ctx; +} + static inline int r4d_decode_splits(const R4DArgs* a, int tile) { if (a->splits > 0) return a->splits; const int tiles = (a->max_ctx + tile - 1) / tile; const int units = std::max(1, a->kv_heads * a->num_seqs); const int gqa = a->kv_heads > 0 ? std::max(1, a->q_heads / a->kv_heads) : 1; - int s = (a->q_len * gqa <= 32 ? 256 : 128) / units; + int s = 256 / units; + if (a->q_len * gqa > 32) { + 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; + } const int cap = tiles / 2; if (s > cap) s = cap; if (s > tiles) s = tiles; diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 95bee301..cb167be2 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -1051,6 +1051,56 @@ static void attn_oracle_one(const uint16_t* qh, const uint8_t* kv, int kv_is_fp8 * band's upper bound (RadArgs.p), so `trim` here is 4096 while the issued count, and `tail`'s * extent, is 1024. 90ede42 read the parameter, trimmed a 2048-key context to 0, and the attention * over slice A's rows wrote nothing. */ +/* The full-attention decode split law (r4d_decode_splits), read through the scratch hook's declare + * path, where the depth is the declared max_ctx: the region the law sizes over the region at one + * forced segment is the law's segment count (the region is the worst over 1..max_seqs sequences, + * which is the last count wherever n * splits(n) peaks there or ties, as in every batch below). + * Qwen3.8-27B's TP2 rank: 12 query heads over 2 KV heads, head_dim 256. An 8-row verify (three + * waves at gqa 6) splits by depth: up to 16384 tokens 128 / units (x4: 16), then at least 64, and + * past 65536 512 / units capped at 256 (x1: 256, x2: 128, x4 and more: 64). A plain decode and + * the short block's 4-row verify keep the 256-workgroup target, and the two-tiles-a-segment cap + * still binds a short context (1024 tokens: 64 tiles of 16, at most 32 segments). */ +static void decode_split_law_case(const Plugin& pl) { + const char* name = "attn_decode_h256_gqa6_bf16kv"; + if (!sel(name)) return; + const RadKernelInfo* r = find(pl, name); + if (!r || !r->scratch) { ok("decode split law", false, "row or its scratch hook absent"); return; } + auto bytes = [&](int64_t seqs, int64_t q_len, int64_t ctx, int64_t splits) { + std::vector ps = { + PI("n_head", 12), PI("gqa", 6), PI("head_dim", 256), PI("block_size", 16), + PI("max_seqs", seqs), PI("q_len", q_len), PI("max_ctx", ctx), + }; + if (splits > 0) ps.push_back(PI("splits", splits)); + RadArgs a; + std::memset(&a, 0, sizeof(a)); + a.p = ps.data(); a.n_p = (int)ps.size(); + a.world_size = 1; + return r->scratch(&a); + }; + struct Want { int64_t seqs, q_len, ctx, splits; }; + const Want want[] = { + { 1, 8, 100000, 256 }, { 2, 8, 100000, 128 }, { 4, 8, 100000, 64 }, { 6, 8, 100000, 64 }, + { 8, 8, 100000, 64 }, { 2, 8, 65536, 64 }, { 2, 8, 65552, 128 }, { 1, 8, 65536, 64 }, + { 2, 8, 32768, 64 }, { 4, 8, 32768, 64 }, { 7, 8, 32768, 64 }, { 4, 8, 16400, 64 }, + { 4, 8, 16384, 16 }, { 2, 8, 16384, 32 }, { 4, 8, 1024, 16 }, { 1, 8, 1024, 32 }, + { 4, 8, 640, 16 }, { 8, 4, 100000, 16 }, { 16, 4, 100000, 8 }, { 4, 1, 100000, 32 }, + { 1, 1, 100000, 128 }, + }; + int bad = 0; + for (const Want& w : want) { + const int64_t law = bytes(w.seqs, w.q_len, w.ctx, 0), one = bytes(w.seqs, w.q_len, w.ctx, 1); + const int64_t got = one > 0 && law > 0 ? law / one : -1; + if (got != w.splits) { + std::printf(" FAIL decode split law: %lld x %lld rows over %lld: %lld segments, want %lld\n", + (long long)w.seqs, (long long)w.q_len, (long long)w.ctx, (long long)got, + (long long)w.splits); + ++bad; + } + } + g_fail += bad; + if (!bad) std::printf(" ok decode split law: %zu batches\n", sizeof(want) / sizeof(want[0])); +} + static void kv_len_trim_case(const Plugin& pl) { const char* name = "kv_len_trim"; if (!sel(name)) return; @@ -28660,6 +28710,7 @@ static void device_phase(const Plugin& pl) { attn_case(pl, "attn_window_h512_gqa8_fp8kv", 512, 8, 32, "fp8_e4m3", 1); /* The 31B's prefill overlap at tp 4: sliding h256 gqa 2 (4 kv heads a rank, window 1024) and * global h512 gqa 8 (1 kv head), a 2048-row chunk cut at 1024, first chunk and a later one. */ + decode_split_law_case(pl); kv_len_trim_case(pl); for (const int64_t ctx : { (int64_t)2048, (int64_t)4096 }) { attn_slice_case(pl, "attn_prefill_h256_gqa2_fp8kv", 256, 2, 4, 2048, 1024, ctx, 1024);