diff --git a/libr4d/r4d_attn_paged_h256_gqa6.hip b/libr4d/r4d_attn_paged_h256_gqa6.hip index 8ea978a6..5198fb70 100644 --- a/libr4d/r4d_attn_paged_h256_gqa6.hip +++ b/libr4d/r4d_attn_paged_h256_gqa6.hip @@ -19,6 +19,7 @@ #include "r4d_attn_args.h" #include "r4d_common.h" #include +#include #include using std::max; #include "r4d_attn_prefill_h256_gqa6.hip" @@ -56,9 +57,25 @@ static int prefill_launch(const R4DArgs* a, hipStream_t s) { if (a->q_heads / a->kv_heads != A_GQA) return -2; constexpr int BQ = (P_NWARPS * 16) / A_GQA; dim3 grid((a->q_len + BQ - 1) / BQ, a->kv_heads, a->num_seqs); - hipLaunchKernelGGL((r4d_attn_prefill_kernel), - grid, dim3(P_NWARPS * 32), 0, s, *a); + // R4D_ATTN_FP8: 0 = f16 (default), 1 = O_QK8, 2 = O_PV8, 3 = both; fp8 KV without V4/K6 only. + int fp8mode = 0; + if constexpr (KVP == 0 && XOPT == 0) { + static const int m = [] { const char* e = getenv("R4D_ATTN_FP8"); return e ? atoi(e) : 0; }(); + fp8mode = m; + } +#define P_LAUNCH(OPTX) \ + hipLaunchKernelGGL((r4d_attn_prefill_kernel), \ + grid, dim3(P_NWARPS * 32), 0, s, *a) + if constexpr (KVP == 0 && XOPT == 0) { + if (fp8mode == 3) P_LAUNCH(P_OPT | O_QK8 | O_PV8); + else if (fp8mode == 2) P_LAUNCH(P_OPT | O_PV8); + else if (fp8mode == 1) P_LAUNCH(P_OPT | O_QK8); + else P_LAUNCH(P_OPT | XOPT); + } else { + P_LAUNCH(P_OPT | XOPT); + } +#undef P_LAUNCH CHK(); return 0; } diff --git a/libr4d/r4d_attn_prefill_h256_gqa6.hip b/libr4d/r4d_attn_prefill_h256_gqa6.hip index 73c071d0..7012095c 100644 --- a/libr4d/r4d_attn_prefill_h256_gqa6.hip +++ b/libr4d/r4d_attn_prefill_h256_gqa6.hip @@ -29,6 +29,10 @@ #define O_F16 1 #define O_MSKIP 2 +// QK8/PV8 (ported from radiance-vllm-mxfp4 r4d_radiance_extras.patch): Q and P as e4m3, +// K and V stay raw bytes in LDS, both matmuls as fp8 WMMA. fp8 KV only, not with V4/K6. +#define O_QK8 32 +#define O_PV8 64 #define O_FOLDQ 8 #define O_CONTIG 512 #define O_PF2 1024 @@ -57,6 +61,13 @@ #define O_V4 33554432 #define O_K6 67108864 +typedef int v2i32_r4d __attribute__((ext_vector_type(2))); // one fp8 WMMA operand (8 e4m3) +// pack 4 floats -> 4 e4m3 bytes in element order (RNE, saturating), 2 instructions +__device__ __forceinline__ uint32_t pk_fp8x4(float a, float b, float c, float d) { + const uint32_t w = __builtin_amdgcn_cvt_pk_fp8_f32(a, b, 0u, false); + return __builtin_amdgcn_cvt_pk_fp8_f32(c, d, w, true); +} + // bf16 dword -> 16-bit dword in the target format, optionally scaled. Used once per kernel on Q. template __device__ __forceinline__ uint32_t r4d_qcvt(uint32_t w, float s) { @@ -78,7 +89,7 @@ void r4d_attn_prefill_kernel(const R4DArgs a) constexpr int CTG = (OPT & O_CONTIG) ? 1 : 0; constexpr int PF = 1 << ((OPT >> 10) & 3); constexpr int SGB = (OPT & O_SGB) ? 1 : 0; - constexpr int DOT2 = ((OPT & O_DOT2) && (OPT & O_F16)) ? 1 : 0; + constexpr int DOT2 = ((OPT & O_DOT2) && (OPT & O_F16) && !(OPT & O_PV8)) ? 1 : 0; constexpr int BTS = (OPT & O_BTS) ? 1 : 0; constexpr int V4 = ((OPT & O_V4) && !KVP) ? 1 : 0; constexpr int K6 = ((OPT & O_K6) && V4) ? 1 : 0; @@ -90,6 +101,9 @@ void r4d_attn_prefill_kernel(const R4DArgs a) constexpr int GPREV = ((OPT & O_GPREV) && !V4) ? 1 : 0; constexpr int LDSB = (OPT & O_LDSB) ? 1 : 0; constexpr int KWIDE = (OPT & O_KWIDE) ? 1 : 0; + constexpr int QK8 = ((OPT & O_QK8) && !KVP && !V4) ? 1 : 0; + constexpr int PV8 = ((OPT & O_PV8) && !KVP && !V4) ? 1 : 0; + static_assert(!QK8 || KWIDE, "QK8 raw-byte staging is written for the wide K fetch"); typedef DT16 D; typedef typename D::frag frag16; @@ -108,12 +122,16 @@ void r4d_attn_prefill_kernel(const R4DArgs a) constexpr int NTR = (TILE / 8) * (HEAD_DIM / (KVP ? 32 : 64)); constexpr int VREGS = (NTR + NWARPS - 1) / NWARPS; // uint4 of raw V per thread constexpr float PSHIFT = D::SHIFT; - constexpr float PGROW = D::GROW; + constexpr float PGROW = PV8 ? 8.0f : D::GROW; // e4m3 max is 448 = 2^8.8 static_assert(!KWIDE || KPASS == 1, "the wide K fetch is one straight-line pass"); static_assert(!K6 || KWIDE, "k6_v4 K is staged sixteen dims a thread"); + static_assert((!QK8 && !PV8) || CTG, "the 8-bit fragment maps are written for CONTIG"); - __shared__ uint16_t sK[TILE * KSTR]; - __shared__ uint16_t sV16[HEAD_DIM * VSTR]; + constexpr int KSTR8 = HEAD_DIM + 8, VSTR8 = TILE + 8; + __shared__ uint16_t sK[QK8 ? (TILE * KSTR8 + 1) / 2 : TILE * KSTR]; + __shared__ uint16_t sV16[PV8 ? (HEAD_DIM * VSTR8 + 1) / 2 : HEAD_DIM * VSTR]; + uint8_t* const sK8 = (uint8_t*)sK; + uint8_t* const sV8 = (uint8_t*)sV16; const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5; const int c = lane & 15, h = lane >> 4; @@ -142,6 +160,7 @@ void r4d_attn_prefill_kernel(const R4DArgs a) const float mul = a.scale * kdesc * 1.44269504089f; frag16 qf[NKS]; + v2i32_r4d qf8[QK8 ? NKS : 1]; { const uint16_t* qp = (const uint16_t*)a.q + ((size_t)(qoff + qrow) * a.q_heads + qhead) * HEAD_DIM; @@ -150,10 +169,19 @@ void r4d_attn_prefill_kernel(const R4DArgs a) // CONTIG reads the 8 dims as one run; the split map takes them 4+4. const uint2 l0 = *(const uint2*)(qp + 16 * t + (CTG ? 8 * h : 4 * h)); const uint2 h0 = *(const uint2*)(qp + 16 * t + (CTG ? 8 * h + 4 : 8 + 4 * h)); - qf[t] = D::mk(make_uint2(r4d_qcvt(l0.x, mul), - r4d_qcvt(l0.y, mul)), - make_uint2(r4d_qcvt(h0.x, mul), - r4d_qcvt(h0.y, mul))); + if constexpr (QK8) { + const float m_ = FOLDQ ? mul : 1.0f; + #define R4D_QB(w) __builtin_bit_cast(float, (w) << 16) * m_, \ + __builtin_bit_cast(float, (w) & 0xffff0000u) * m_ + qf8[t] = (v2i32_r4d){(int)pk_fp8x4(R4D_QB(l0.x), R4D_QB(l0.y)), + (int)pk_fp8x4(R4D_QB(h0.x), R4D_QB(h0.y))}; + #undef R4D_QB + } else { + qf[t] = D::mk(make_uint2(r4d_qcvt(l0.x, mul), + r4d_qcvt(l0.y, mul)), + make_uint2(r4d_qcvt(h0.x, mul), + r4d_qcvt(h0.y, mul))); + } } } @@ -308,6 +336,10 @@ void r4d_attn_prefill_kernel(const R4DArgs a) const int key = KWIDE ? (2 * (i >> 5) + (i & 1)) : (i / (HEAD_DIM / KEL)); const int ch = KWIDE ? ((i & 31) >> 1) : (i - key * (HEAD_DIM / KEL)); if (KWIDE && key >= TILE) continue; + if constexpr (QK8) { + *(uint4*)(&sK8[key * KSTR8 + ch * KEL]) = kreg[p]; + continue; + } uint16_t* kd = &sK[key * KSTR + ch * KEL]; #pragma unroll for (int q = 0; q < KEL / 8; ++q) { @@ -346,6 +378,11 @@ void r4d_attn_prefill_kernel(const R4DArgs a) // dims; v_perm_b32 splits them into two dim rows. const uint32_t lo0 = byte_gather(v.x, v.y, 0), lo1 = byte_gather(v.z, v.w, 0); const uint32_t hi0 = byte_gather(v.x, v.y, 1), hi1 = byte_gather(v.z, v.w, 1); + if constexpr (PV8) { + *(uint2*)(&sV8[(d0 + 2 * vj) * VSTR8 + kg * 8]) = make_uint2(lo0, lo1); + *(uint2*)(&sV8[(d0 + 2 * vj + 1) * VSTR8 + kg * 8]) = make_uint2(hi0, hi1); + continue; + } uint32_t wl[4], wh[4]; fp8x8_to_16x4w(make_uint2(lo0, lo1), wl); fp8x8_to_16x4w(make_uint2(hi0, hi1), wh); @@ -399,7 +436,20 @@ void r4d_attn_prefill_kernel(const R4DArgs a) #pragma unroll 1 for (int mt = 0; mt < MT; ++mt) { v8f s = (v8f){0,0,0,0,0,0,0,0}; - { + if constexpr (QK8) { + const uint8_t* krow8 = &sK8[(mt * 16 + c) * KSTR8] + 8 * h; + #pragma unroll + for (int g = 0; g < NKS; g += PF) { + v2i32_r4d kb8[PF]; + #pragma unroll + for (int j = 0; j < PF; ++j) + kb8[j] = *(const v2i32_r4d*)(krow8 + 16 * (g + j)); + if (SGB) sched_barrier(); + #pragma unroll + for (int j = 0; j < PF; ++j) + s = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(kb8[j], qf8[g + j], s); + } + } else { const uint16_t* krow = &sK[(mt * 16 + c) * KSTR] + (CTG ? 8 * h : 0); // Load PF fragments before issuing their WMMAs, so each WMMA waits on // s_wait_dscnt PF-1 instead of a full drain. @@ -459,7 +509,22 @@ void r4d_attn_prefill_kernel(const R4DArgs a) } if (!DOT2) l_i += lsum; - { + if constexpr (PV8) { + const v2i32_r4d p8 = (v2i32_r4d){(int)pk_fp8x4(pf[0], pf[1], pf[2], pf[3]), + (int)pk_fp8x4(pf[4], pf[5], pf[6], pf[7])}; + const uint8_t* vbase8 = &sV8[c * VSTR8 + mt * 16 + 8 * h]; + #pragma unroll + for (int g = 0; g < NKS; g += PF) { + v2i32_r4d vb8[PF]; + #pragma unroll + for (int j = 0; j < PF; ++j) + vb8[j] = *(const v2i32_r4d*)(vbase8 + (size_t)(g + j) * 16 * VSTR8); + if (SGB) sched_barrier(); + #pragma unroll + for (int j = 0; j < PF; ++j) + acc[g + j] = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(vb8[j], p8, acc[g + j]); + } + } else { uint32_t pw[4]; #pragma unroll for (int e = 0; e < 8; e += 2) pw[e >> 1] = D::pk(pf[e], pf[e + 1]);