libr4d: the windowed attention's split merge loads a group ahead, and its WMMA prefill does less work a key tile

The merge: the 31B's global layers merge their split-KV partials in merge_body
(r4d_attn_paged_window.hip), one wave a row, four splits at a time into one accumulator chain:
~9.4 us a layer on 4x R9700 in the decode timeline. Each group's loads went out only after the
previous group's FMAs, so a row paid one L2 round trip a group. Group g + 1's loads now go out
before group g's FMAs (two register groups in turn; 175 VGPRs at head_dim 512, no spill). The FMAs
and their order are unchanged, so the bytes are too.

The prefill kernel: attn_wmma_kernel (the 31B's global layers at prefill, ~1.1-1.5 ms a 1024-row
slice on 4x R9700) does three things a tile that change no byte:

- fp8 V widens to its f16 pairs with v_cvt_pkrtz: every e4m3 value is exact in f16, so the pack's
  rounding and f16_bits' clamp (unreachable under 448) leave the same bits, for 2 instructions a
  pair instead of ~10.
- A tile wholly inside [lo, hi) and, causal, at or before the block's first position is visible to
  every valid row, so it takes its scores without the per-key masks.
- With an fp8 cache, a wave whose rows all kept their running max (corr exactly 1) skips the
  rescale of O^T: x * 1 = x.

One-card microbenchmark of the three with the skip on both caches (the kernel against the shipped
one, random operands, a 1024-row slice at the end of the context): head_dim 512 gqa 8 fp8 cache
1481 -> 1295 us at context 4096, 657 -> 531 at 2048, 2220 -> 1785 at 6144; head_dim 256 gqa 2 fp8
675 -> 544, bf16 589 -> 511. Every output byte-identical to the shipped kernel's at all four
instantiations (512/8 and 256/2, fp8 and bf16). The skip measured ~4% at head_dim 512 with an fp8
cache, but with bf16 its split loop ran ~10% slower than the fused one (1634 against 1427 us at
context 4096; v_cvt and the interior path alone: 1318), so bf16 keeps the fused loop.
fp8x16_f16, left with no caller, is removed.

The pack is f16x2_exact: v_cvt_pkrtz on gfx12, f16_bits elsewhere (the host SIMT emulator,
tests/r4d_emu, compiles this unit as C++), the same bits since neither rounds a value exact in f16;
r4d_emu_attn passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-21 09:25:04 +02:00
1 parent dffec0b770
commit 82aec1766c
1 file changed
+77 -27
+77 -27
View File
@@ -499,15 +499,14 @@ __device__ __forceinline__ unsigned f16_bits(float f) {
if (f == f) f = __builtin_fminf(__builtin_fmaxf(f, -65504.0f), 65504.0f);
return (unsigned)__builtin_bit_cast(unsigned short, (_Float16)f);
}
__device__ __forceinline__ void fp8x16_f16(const uint4 w, unsigned (&h)[16]) {
const unsigned in[4] = {w.x, w.y, w.z, w.w};
#pragma unroll
for (int q = 0; q < 4; ++q) {
float f[4];
fp8x4(in[q], f);
#pragma unroll
for (int e = 0; e < 4; ++e) h[q * 4 + e] = f16_bits(f[e]);
}
/* Two f32 values exact in f16 as an f16 pair, `a` low: v_cvt_pkrtz on gfx12, f16_bits elsewhere
* (the host emulator), the same bits either way since neither rounds an exact value. */
__device__ __forceinline__ unsigned f16x2_exact(float a, float b) {
#if defined(__GFX12__)
return __builtin_bit_cast(unsigned, __builtin_amdgcn_cvt_pkrtz(a, b));
#else
return f16_bits(a) | f16_bits(b) << 16;
#endif
}
__device__ __forceinline__ void bf16x8_f16(const uint4 w, unsigned (&h)[8]) {
const unsigned in[4] = {w.x, w.y, w.z, w.w};
@@ -684,11 +683,17 @@ __global__ __launch_bounds__(kWThreads) void attn_wmma_kernel(AttnArgs A) {
const uint4 wb = (tok && kb1 >= lo && kb1 < hi) ? pv[j][1] : make_uint4(0u, 0u, 0u, 0u);
unsigned d[CW]; /* f16 pairs: key 2kp low, 2kp + 1 high */
if constexpr (KV8) {
unsigned ha[16], hb2[16];
fp8x16_f16(wa, ha);
fp8x16_f16(wb, hb2);
/* every e4m3 value is exact in f16 (|v| <= 448, subnormals 2^-9 up), so the
* pack's round-toward-zero and f16_bits' clamp leave the same bits */
const unsigned ia[4] = {wa.x, wa.y, wa.z, wa.w}, ib[4] = {wb.x, wb.y, wb.z, wb.w};
#pragma unroll
for (int e = 0; e < CW; ++e) d[e] = ha[e] | hb2[e] << 16;
for (int q = 0; q < 4; ++q) {
float fa[4], fb[4];
fp8x4(ia[q], fa);
fp8x4(ib[q], fb);
#pragma unroll
for (int e = 0; e < 4; ++e) d[q * 4 + e] = f16x2_exact(fa[e], fb[e]);
}
} else {
unsigned ha[8], hb2[8];
bf16x8_f16(wa, ha);
@@ -747,14 +752,26 @@ __global__ __launch_bounds__(kWThreads) void attn_wmma_kernel(AttnArgs A) {
/* ---- online softmax, row n: eight keys here, eight in lane ^ 16 */
float sv[8], mx = -__builtin_inff();
/* A tile wholly inside [lo, hi) and, causal, at or before the block's first position
* (every row's pos >= qbase) is visible to every valid row: the same scores, unmasked. */
const bool interior = window <= 0 && k0 >= lo && k0 + 16 <= hi && (!causal || k0 + 15 <= qbase);
if (interior) {
const bool vis = rval && tok;
#pragma unroll
for (int e = 0; e < 8; ++e) {
const int kk = k0 + 8 * h + e;
bool vis = rval && tok && kk >= lo && kk < hi;
if (causal) vis = vis && kk <= pos;
if (window > 0) vis = vis && (pos - kk < window) && (causal || kk - pos < window);
sv[e] = vis ? tot[e] * sscale : -__builtin_inff();
mx = __builtin_fmaxf(mx, sv[e]);
for (int e = 0; e < 8; ++e) {
sv[e] = vis ? tot[e] * sscale : -__builtin_inff();
mx = __builtin_fmaxf(mx, sv[e]);
}
} else {
#pragma unroll
for (int e = 0; e < 8; ++e) {
const int kk = k0 + 8 * h + e;
bool vis = rval && tok && kk >= lo && kk < hi;
if (causal) vis = vis && kk <= pos;
if (window > 0) vis = vis && (pos - kk < window) && (causal || kk - pos < window);
sv[e] = vis ? tot[e] * sscale : -__builtin_inff();
mx = __builtin_fmaxf(mx, sv[e]);
}
}
mx = __builtin_fmaxf(mx, shfl_xor(mx, 16));
const float mn = __builtin_fmaxf(m, mx);
@@ -773,10 +790,23 @@ __global__ __launch_bounds__(kWThreads) void attn_wmma_kernel(AttnArgs A) {
/* ---- O^T += V^T * P^T in f16: A = V^T rows (dim n of the chunk), keys 8h .. 8h + 7 */
const unsigned char* va = vb + (sl * kDQ + n) * VS + h * 16;
if constexpr (KV8) {
/* x * 1 = x, so a wave whose rows all kept their max skips the rescale. With an fp8
* cache that is ~4% at head_dim 512; with bf16 the split loop measured ~10% slower,
* so it keeps the fused one. */
if (__builtin_amdgcn_ballot_w32(corr != 1.0f) != 0) {
#pragma unroll
for (int mc = 0; mc < kDQ / 16; ++mc) {
oacc[mc] *= corr;
oacc[mc] = wmma_f16(__builtin_bit_cast(win_v8h, *(const uint4*)(va + mc * 16 * VS)), pf, oacc[mc]);
for (int mc = 0; mc < kDQ / 16; ++mc) oacc[mc] *= corr;
}
#pragma unroll
for (int mc = 0; mc < kDQ / 16; ++mc)
oacc[mc] = wmma_f16(__builtin_bit_cast(win_v8h, *(const uint4*)(va + mc * 16 * VS)), pf, oacc[mc]);
} else {
#pragma unroll
for (int mc = 0; mc < kDQ / 16; ++mc) {
oacc[mc] *= corr;
oacc[mc] = wmma_f16(__builtin_bit_cast(win_v8h, *(const uint4*)(va + mc * 16 * VS)), pf, oacc[mc]);
}
}
}
@@ -840,9 +870,9 @@ __global__ __launch_bounds__(kThreads) void merge_kernel(AttnArgs A) {
* (a written split, so 0 * finite). An empty split's accumulator is never read. */
uint64_t live = (uint64_t)__builtin_amdgcn_ballot_w32(w0 != 0.0f) |
((uint64_t)__builtin_amdgcn_ballot_w32(w1 != 0.0f) << 32);
while (live) {
int zi[4];
float wj[4];
/* Group g + 1's loads go out before group g's FMAs, so a group's wait overlaps the one
* before it instead of following it; the FMAs keep their order, so the bytes do too. */
auto take = [&](int (&zi)[4], float (&wj)[4]) {
#pragma unroll
for (int j = 0; j < 4; ++j) {
const bool have = live != 0;
@@ -851,13 +881,16 @@ __global__ __launch_bounds__(kThreads) void merge_kernel(AttnArgs A) {
wj[j] = readlane(zi[j] < 32 ? w0 : w1, zi[j] & 31);
if (!have) wj[j] = 0.0f;
}
float4 v[4][NC];
};
auto load = [&](const int (&zi)[4], float4 (&v)[4][NC]) {
#pragma unroll
for (int j = 0; j < 4; ++j) {
const float* pp = A.part + (base + (long long)zi[j] * R) * (HD + 4);
#pragma unroll
for (int c = 0; c < NC; ++c) v[j][c] = *(const float4*)(pp + c * 128 + lane * 4);
}
};
auto fold = [&](const float (&wj)[4], const float4 (&v)[4][NC]) {
#pragma unroll
for (int j = 0; j < 4; ++j)
#pragma unroll
@@ -867,6 +900,23 @@ __global__ __launch_bounds__(kThreads) void merge_kernel(AttnArgs A) {
acc[c][2] = __builtin_fmaf(wj[j], v[j][c].z, acc[c][2]);
acc[c][3] = __builtin_fmaf(wj[j], v[j][c].w, acc[c][3]);
}
};
if (live) {
int za[4], zb[4];
float wa[4], wb[4];
float4 va[4][NC], vb[4][NC];
take(za, wa);
load(za, va);
while (true) {
const bool more_b = live != 0;
if (more_b) { take(zb, wb); load(zb, vb); }
fold(wa, va);
if (!more_b) break;
const bool more_a = live != 0;
if (more_a) { take(za, wa); load(za, va); }
fold(wb, vb);
if (!more_a) break;
}
}
#pragma unroll
for (int c = 0; c < NC; ++c)