diff --git a/libr4d/r4d_attn_prefill_split.h b/libr4d/r4d_attn_prefill_split.h index 02d44856..1b367708 100644 --- a/libr4d/r4d_attn_prefill_split.h +++ b/libr4d/r4d_attn_prefill_split.h @@ -36,8 +36,11 @@ static inline int r4d_prefill_wgps() { // rounds / S walks, so take one round (S1 = wgps / units) or two (S2 = 2 wgps / units), whichever // is faster, the smaller S on a tie (fewer partials). At 32 WGPs, gqa 6 with 2 KV heads: 53 rows // -> 16 splits, 80 -> 8, 345 -> 5, 576 -> 3, 1024 and up -> 1; gqa 12 with 1 KV head: up to 32 -// rows -> 32, 53 -> 16, 345 -> 5. Each split keeps at least 8 tiles of max_ctx, the only context -// the host sees. At most 64, the vectorised merge's limit. +// rows -> 32, 53 -> 16, 345 -> 5. Each split keeps at least 8 tiles of the launch's live KV +// (seqused_k, falling back to max_ctx). Using the model bound here over-splits short prefills: +// a 128-token chunk at max_ctx 2048 became 4 splits of ~32 keys, while a 977-token unsplit +// launch of the same prompt did not, and the different reduction order moved logits. At most 64, +// the vectorised merge's limit. static inline int r4d_prefill_splits_law(int bq, int tile, int q_len, int total_q, int num_seqs, int kv_heads, int max_ctx, int wgps) { const long n = std::max(1, num_seqs); @@ -56,14 +59,42 @@ static inline int r4d_prefill_splits_law(int bq, int tile, int q_len, int total_ // tells r4d_selftest a split launch from an unsplit one with the same bytes. Not atomic. static int g_prefill_last_splits = 0; +/* Live KV length for the 8-tile floor. seqused_k is device-side (r4d_attn_args.h); max_ctx is + * the model bound and is too wide for a short chunk. A failed copy keeps max_ctx, which only + * over-splits. Occupancy already returned 1 skips the copy. */ +static inline int r4d_prefill_used_ctx(const R4DArgs* r) { + if (!r->seqused_k || r->num_seqs <= 0) return 0; + const int n = r->num_seqs; + int used = 0; + int chunk[64]; + for (int off = 0; off < n; off += 64) { + const int m = n - off < 64 ? n - off : 64; + if (hipMemcpy(chunk, r->seqused_k + off, (size_t)m * sizeof(int), + hipMemcpyDefault) != hipSuccess) { + (void)hipGetLastError(); + return 0; + } + for (int i = 0; i < m; ++i) + if (chunk[i] > used) used = chunk[i]; + } + return used; +} + /* The KV split's count before the scratch bound: the case's `splits` parameter when it names one * (r4d_selftest; the prefill rows declare no such axis, so the engine passes none), else the law, - * at most 64. */ + * at most 64. The 8-tile floor uses live seqused_k, not the model max_ctx. */ static inline int r4d_prefill_splits_want(const RadArgs* a, const R4DArgs* r, int bq, int tile) { - const int sp = split_of(a); - if (sp > 0) return std::min(sp, 64); - return r4d_prefill_splits_law(bq, tile, r->q_len, r4d_q_total(*r), r->num_seqs, r->kv_heads, - r->max_ctx, r4d_prefill_wgps()); + const int sp0 = split_of(a); + if (sp0 > 0) return std::min(sp0, 64); + const int wgps = r4d_prefill_wgps(); + const int qtot = r4d_q_total(*r); + int sp = r4d_prefill_splits_law(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, r->max_ctx, + wgps); + if (sp <= 1) return 1; + const int used = r4d_prefill_used_ctx(r); + if (used > 0 && (r->max_ctx <= 0 || used < r->max_ctx)) + sp = r4d_prefill_splits_law(bq, tile, r->q_len, qtot, r->num_seqs, r->kv_heads, used, wgps); + return sp; } /* The launch's split count, 1 for the plain form: what r4d_prefill_splits_want asks, cut to the diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 30d0ff50..0e46baf2 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -1371,7 +1371,8 @@ static const SplitGeo kSplitG6 = { 6, 2, 16, "r4d_attn_prefill_h256_gqa6" }; struct SplitSeq { int64_t q, ctx; }; static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* name, const char* kv_dtype, const std::vector& seqs, int force, - bool law_splits, int64_t lead = 0, int64_t tail = 0) { + bool law_splits, int64_t lead = 0, int64_t tail = 0, + int64_t decl_max_ctx = 0) { if (!sel(name) && !sel("attn_prefill_split")) return; const RadKernelInfo* r = find(pl, name); if (!r) { ok(name, false, "row absent"); return; } @@ -1473,6 +1474,7 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam PI("block_size", bs), PI("causal", 1), PI("window", 0), PS("q_dtype", "bf16"), PS("kv_dtype", kv_dtype), }; + if (decl_max_ctx > 0) ps.push_back(PI("max_ctx", decl_max_ctx)); if (sp > 0) ps.push_back(PI("splits", sp)); /* run_case, with the scratch poisoned: 0x7f7f7f7f is m = 3.4e38, so a segment the merge * reads but nobody wrote outweighs every real one and the row comes out wrong. Fresh @@ -1498,8 +1500,14 @@ static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* nam const int s_un = run(1, d_o[0]); const int s_fo = run(force, d_o[1]); const int s_lw = run(0, d_o[2]); + int law_ctx = (int)(mb * bs); + if (decl_max_ctx > 0) { + law_ctx = 0; + for (const SplitSeq& s : seqs) + if ((int)s.ctx > law_ctx) law_ctx = (int)s.ctx; + } const int want = law((int)(ragged ? std::min(2048, total_q) : qmax), (int)total_q, - (int)ns, (int)kv_heads, (int)(mb * bs)); + (int)ns, (int)kv_heads, law_ctx); ok((tag + ": splits 1 / forced / law").c_str(), s_un == 1 && s_fo == force && s_lw == want && (!law_splits || want > 1), fmt("launched %d / %d / %d, the law says %d%s", s_un, s_fo, s_lw, want, @@ -29941,6 +29949,8 @@ static void device_phase(const Plugin& pl) { attn_split_case(pl, g, n, kv, { { 345, 345 } }, 16, false); attn_split_case(pl, g, n, kv, { { 345, 1000 } }, 16, true); attn_split_case(pl, g, n, kv, { { 640, 2000 } }, 5, true); + /* Model max_ctx 2048 with a 128-token KV: occupancy would split, live seqused must not. */ + attn_split_case(pl, g, n, kv, { { 128, 128 } }, 4, false, 0, 0, 2048); attn_split_case(pl, g, n, kv, { { 53, 12000 }, { 1, 20000 } }, 16, true); attn_split_case(pl, g, n, kv, { { 11, 8192 }, { 64, 9000 }, { 1, 12000 } }, 6, true); attn_split_case(pl, g, n, kv, { { 300, 600 }, { 1, 10000 } }, 16, true);