libr4d: the paged prefill attention (head 256, gqa 6) without its exposed global waits

The tile loop waited out a global round trip every tile with every wave of the workgroup (the only
one on its WGP) stalled: the wide K fetch sat in a branch whose join made the compiler drain every
load in flight right after it, and the fp8_v4 / k6_v4 V scales were packed right after their
prefetch was issued, so it was waited on at once. Now the K fetch is branch-free (the launch's
thread count is stated to the compiler), the scales are packed at the store, and on the fp8 and
k6_v4 caches tile t+1's K is fetched once tile t's last m-tile has its scores (O_KLATE), so the
staging phase between the barriers only widens and stores. Load order only: the bytes are the same
(greedy texts of a server identical, r4d_selftest 8124 ok / 0 FAIL on both trees).

r4d_selftest --perfpaged on the R9700, three interleaved rounds, medians, k6_v4: 2048 queries over
100k keys 25.91 -> 21.47 ms (-17.2 %), over 160k 41.43 -> 33.97 (-18.0 %), over 25k 5.63 -> 5.45
(-3.3 %), 1024 queries over 50k 5.69 -> 5.48 (-3.7 %). fp8: -7.8 % at 100k and 160k, within noise
at 25k and 50k. bf16 (the stated thread count alone): 0 to -3.5 %. fp8_v4 keeps the old order on
all three counts: with them it gained 19 % at 100k but lost 3 % at 25k and 50k.

The gap is widest where the cache no longer holds the KV (k6_v4 at 100k is 70 MB against 64 MB of
Infinity Cache), so the round trip is a DRAM one. Qwen3.8-27B MXFP4 at tp 2, production flags,
cold prefill, three rounds: 100k 3830..3856 -> 3932..3935 tok/s, 25k 5255..5292 -> 5292.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 committed 2026-10-25 07:33:17 +00:00
1 parent 248ecf5858
commit 75b4c92e7e
2 files changed
+35 -8

No files matched your search

+3 -2
View File
@@ -10,9 +10,10 @@
// taken only when the caller's `prefill_fp8` asks for them.
//
// `opt` is a bitmask of optimisation steps:
// prefill 3954187 = f16 operands | mask skip | folded query scale | contiguous-k fragments
// prefill 3954187 | KLATE = f16 operands | mask skip | folded query scale | contiguous-k fragments
// | prefetch 2 + scheduling barrier | dot2 row sum | block table in SGPRs
// | wide K fetch | V prefetch across the m-tile loop | LDS-scoped barrier
// | 8-bit K fetched a tile ahead (KLATE)
// decode 3429947 = the same minus the wide K fetch (its K never reaches LDS), plus K read
// straight from the paged cache (bit 16) and f16 split-KV partials (32).
// Each bit is defined where it is used (r4d_attn_prefill_h256_gqa6.hip,
@@ -49,7 +50,7 @@ extern "C" void r4d_attn_dims(int* head_dim, int* gqa, int* block_size, int* max
#define P_TILE 48
// K prefetch 2, not 4: four fragments in flight make the m-tile loop spill at six waves per SIMD
// (36-48 bytes per lane, ~4% of the prefill). At 2 the spill is 20 bytes, outside the WMMA groups.
#define P_OPT 3954187
#define P_OPT (3954187 | O_KLATE)
// KVP: 0 = an 8-bit cache (XOPT names fp8_v4 or k6_v4 and the e4m3 legs), 1 = bf16 cache.
template <int KVP, int XOPT = 0>