From 31b2472a3feeab1dc3f8802ec4f0a5d042ca04f1 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 07:32:53 +0000 Subject: [PATCH 1/3] prefill: opt-in STRATA_HC_UPMIX=1 - the hyper-connection up projection with gr_mix_r as its epilogue (gfx11) The read's up GEMM wrote `gated` (T x 10240 FP32: 671 MB per 16K chunk) for gr_mix_r to read back. gr_upmix computes a tile of 128 tokens x 16 columns for the 4 streams on the matrix cores (BF16 in, FP32 accumulate, the block's 64 w_up rows staged in LDS once) and runs the mix in registers, in gr_mix_r's order. The grid is ordered column-tile fastest: the blocks in flight share one token tile, so the R reads of the epilogue stay on few pages (token-tile fastest touched 512 rows x 40 KB per block: 28 ms instead of 3.7 at 8K - address translation on this box goes through the IOMMU). STRATA_HC_UPMIX_CHECK=N runs the default pair on the first N reads and prints the difference. gfx1151, UD-IQ4_XS: `mixed` vs the default rel RMS 1.2-3.6e-7 on every read of an 8K prompt; prompt-timing "hc read" 1352 / 1313 -> 1080 / 1212 ms at 8K; 64K prompt 874.3 / 876.1 -> 902.7 / 903.0 tok/s. Rounding-level (the K order of the GEMM): teacher-forced KL on three text offsets 0.170 / 0.085 / 0.032 vs the accepted MMQ-vs-fused pair's 0.153 / 0.117 / 0.030; ledger and 8K / 64K retrieval pass. Off by default. --- include/strata/prefill/kernels.hpp | 4 ++ src/prefill/kernels.cu | 92 ++++++++++++++++++++++++++++++ src/prefill/prefill.cpp | 45 +++++++++++++-- 3 files changed, 137 insertions(+), 4 deletions(-) diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp index e10d8ba1..f643918a 100644 --- a/include/strata/prefill/kernels.hpp +++ b/include/strata/prefill/kernels.hpp @@ -25,6 +25,10 @@ void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* int64_t T, void* stream, uint16_t* mixed_h = nullptr, uint16_t* mixed16_lo = nullptr); /// F-2: gr_write, then gr_norm_rs of the next half (its norm weights) over the rows just written - the same bits as /// the two calls, without reading R back. +/// S23 (opt-in STRATA_HC_UPMIX=1): the up projection (lo16 x w_up^T, BF16, FP32 accumulate) with gr_mix_r as its +/// epilogue - `gated` is never written. False (nothing launched) off gfx11. +bool gr_upmix(const uint16_t* lo16, const uint16_t* w_up, const float* R, const float* rs, const float* w_norm, + float* mixed, uint16_t* mixed16, uint16_t* mixed_h, int64_t T, void* stream); void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* w_norm_next, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr); /// lo16[t, k] = bf16(silu(lo[t, k] / hc)) diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu index 6f1ec519..8322b9a8 100644 --- a/src/prefill/kernels.cu +++ b/src/prefill/kernels.cu @@ -11,6 +11,7 @@ #include #include #include +#include namespace strata::prefill { namespace { @@ -798,6 +799,77 @@ __global__ void to_bf16_kernel(const float* __restrict__ x, uint16_t* __restrict } } +// S23 (opt-in STRATA_HC_UPMIX=1, gfx11): the hyper-connection read's up projection with gr_mix_r as its epilogue. The +// up GEMM's FP32 output `gated` (T x 10240: 671 MB per 16K chunk) is never written or read back: a block computes a +// tile of 64 tokens x 64 columns for all 4 streams on the matrix cores (BF16 in, FP32 accumulate over K = 320; the +// lo16 and w_up tiles staged in LDS), and each lane already holds the 4 streams' values of its (token, column) - the +// mix (x_c = R * rs * w, sum of x_c * sigmoid(gated_c), / 4) runs in registers, in gr_mix_r_kernel's order. The +// GEMM sums K in another order than hipBLASLt: rounding-level, hence opt-in and quality-gated. +#if defined(__HIPCC__) +typedef __bf16 um_b16 __attribute__((ext_vector_type(16))); +typedef float um_f8 __attribute__((ext_vector_type(8))); +__device__ __forceinline__ um_f8 um_wmma(um_b16 a, um_b16 b, um_f8 c) { +#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1150__) || defined(__gfx1151__) + return __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a, b, c); +#else + (void) a; (void) b; return c; // not a gfx11 device pass: never launched (runtime gate) +#endif +} +constexpr int UM_T = 128, UM_D = 16, UM_LD = LR + 8; // a block: 128 tokens x 16 columns x the 4 streams +__device__ __forceinline__ um_b16 um_frag(const uint16_t* p) { // 16 BF16 (32 bytes, 16-byte aligned) + const uint4 a = *reinterpret_cast(p), b = *reinterpret_cast(p + 8); + const uint32_t w[8] = {a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w}; + return __builtin_bit_cast(um_b16, w); +} +// The block's w_up rows (16 columns x 4 streams x all 320 k) are staged in LDS once; each wave streams its own 16 +// tokens' lo16 rows from global memory (every A fragment feeds the 4 streams' WMMAs) - one barrier per block. +__global__ void __launch_bounds__(256) gr_upmix_kernel(const uint16_t* __restrict__ lo16, const uint16_t* __restrict__ wu, + const float* __restrict__ R, const float* __restrict__ rs, + const float* __restrict__ w, float* __restrict__ mixed, + uint16_t* __restrict__ mixed16, uint16_t* __restrict__ mixed_h, + int64_t T) { + __shared__ __align__(16) uint16_t sB[HC * UM_D][UM_LD]; + const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4; + // blocks ordered column-tile fastest: the blocks in flight share a token tile, so the epilogue's R reads run + // along rows (token-tile fastest scattered them over 512 rows x 40 KB per block: 28 ms instead of 3.7 at 8K) + const int64_t t0 = (int64_t) blockIdx.y * UM_T + 16 * wave; + const int d0 = blockIdx.x * UM_D; + for (int i = tid; i < HC * UM_D * (LR / 8); i += 256) { + const int rr = i / (LR / 8), q = i % (LR / 8); + const int c = rr / UM_D, dd = rr % UM_D; + *reinterpret_cast(&sB[rr][8 * q]) = *reinterpret_cast(wu + (size_t) (c * N + d0 + dd) * LR + 8 * q); + } + __syncthreads(); + if (t0 >= T) return; + const int64_t ta = t0 + l16 < T ? t0 + l16 : T - 1; + const uint16_t* arow = lo16 + ta * LR; + um_f8 acc[HC]; +#pragma unroll + for (int c = 0; c < HC; ++c) acc[c] = um_f8{0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; +#pragma unroll + for (int ks = 0; ks < LR; ks += 16) { + const um_b16 a = um_frag(arow + ks); +#pragma unroll + for (int c = 0; c < HC; ++c) acc[c] = um_wmma(a, um_frag(&sB[c * UM_D + l16][ks]), acc[c]); + } + const int d = d0 + l16; +#pragma unroll + for (int i = 0; i < 8; ++i) { + const int64_t t = t0 + 2 * i + hi; + if (t >= T) continue; + float s = 0.0f; +#pragma unroll + for (int c = 0; c < HC; ++c) { + const float x = R[t * D + c * N + d] * rs[t * HC + c] * w[c * N + d]; // gr_mix_r_kernel's order + s = fmaf(x, sigm(acc[c][i]), s); + } + s /= (float) HC; + mixed[t * N + d] = s; + if (mixed16) mixed16[t * N + d] = bf(s); + if (mixed_h) mixed_h[t * N + d] = hf(s); + } +} +#endif } // namespace void kv_append(const float* K, const float* V, int64_t T, int64_t pos0, const int32_t* page_table, int64_t page_size, @@ -841,6 +913,26 @@ void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* mixed_h, mixed16_lo); check("gr_mix_r"); } +bool gr_upmix(const uint16_t* lo16, const uint16_t* w_up, const float* R, const float* rs, const float* w_norm, + float* mixed, uint16_t* mixed16, uint16_t* mixed_h, int64_t T, void* stream) { +#if defined(__HIPCC__) + static const bool gfx11 = [] { + int dev = 0; + cudaDeviceProp p; + if (cudaGetDevice(&dev) != cudaSuccess || cudaGetDeviceProperties(&p, dev) != cudaSuccess) return false; + return std::strncmp(p.gcnArchName, "gfx11", 5) == 0; + }(); + if (!gfx11 || T <= 0) return false; + gr_upmix_kernel<<>>( + lo16, w_up, R, rs, w_norm, mixed, mixed16, mixed_h, T); + check("gr_upmix"); + return true; +#else + (void) lo16; (void) w_up; (void) R; (void) rs; (void) w_norm; (void) mixed; (void) mixed16; (void) mixed_h; + (void) T; (void) stream; + return false; +#endif +} void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* w_norm_next, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo) { gr_write_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, bo, inj, inj_ld, w_norm_next, eps, diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 54c9cb7e..79c9351f 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -167,6 +167,12 @@ inline int bf16x2_mode() { return v; } inline bool bf16x2() { return bf16x2_mode() != 0; } +// S23 (opt-in STRATA_HC_UPMIX=1): the hyper-connection read's up projection and gr_mix_r as one kernel (gr_upmix, gfx11); +// STRATA_HC_UPMIX_CHECK=N also runs the default pair on the first N reads and reports the difference of `mixed` +inline bool hc_upmix() { + static const bool v = [] { const char* e = std::getenv("STRATA_HC_UPMIX"); return e != nullptr && e[0] == '1'; }(); + return v; +} inline bool bf16x2_hc() { return bf16x2_mode() == 1; } // F-1: STRATA_GR_UNFUSED=1 keeps the FP32 copy of the normalized rows (gr_norm + gr_mix), the A/B arm @@ -1409,11 +1415,42 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e normed = false; if (!bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err, 0, m.xn16_lo)) return false; gr_silu(m.lo, m.lo16, T, m.cs, m.lo16_lo); - if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo)) return false; + bool upmixed = false; + if (hc_upmix() && !gr_unfused() && !m.lo16_lo && !m.mixed_bf_lo && + wu->kind == core::WeightKind::Bf16InF32 && wu->data && wu->ne0 == LR && wu->ne1 == D) { + static int checks = [] { const char* e = std::getenv("STRATA_HC_UPMIX_CHECK"); return e ? std::atoi(e) : 0; }(); + if (checks > 0) { // the default pair first, kept for the comparison + --checks; + if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err)) return false; + gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h); + std::vector ref((size_t) T * N), got((size_t) T * N); + cudaMemcpyAsync(ref.data(), m.mixed, ref.size() * 4, cudaMemcpyDeviceToHost, m.cs); + upmixed = gr_upmix(m.lo16, (const uint16_t*) wu->data, m.R, m.grs, (const float*) wn->data, + m.mixed, m.mixed_bf, m.mixed_h, T, m.cs); + cudaMemcpyAsync(got.data(), m.mixed, got.size() * 4, cudaMemcpyDeviceToHost, m.cs); + cudaStreamSynchronize(m.cs); + double e2 = 0, r2 = 0, emax = 0; + for (size_t i = 0; i < ref.size(); ++i) { + const double dd = (double) got[i] - ref[i]; + e2 += dd * dd; r2 += (double) ref[i] * ref[i]; emax = std::max(emax, std::fabs(dd)); + } + std::fprintf(stderr, "strata: STRATA_HC_UPMIX_CHECK layer %lld half %d T %lld: mixed rel RMS %.3e, " + "max |diff| %.3e (%s)\n", (long long) l, half, (long long) T, + std::sqrt(e2 / std::max(r2, 1e-30)), emax, upmixed ? "upmix" : "upmix refused"); + } else { + upmixed = gr_upmix(m.lo16, (const uint16_t*) wu->data, m.R, m.grs, (const float*) wn->data, + m.mixed, m.mixed_bf, m.mixed_h, T, m.cs); + } + } + if (!upmixed && !bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo)) return false; if (!bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo)) return false; - if (gr_unfused()) gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, m.mixed_bf_lo); - else gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, - m.mixed_bf_lo); + if (upmixed) { + } else if (gr_unfused()) { + gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, m.mixed_bf_lo); + } else { + gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, + m.mixed_bf_lo); + } if (half == 0 && !core::is_qsa_layer(g, l)) { // ======================= GDN ======================= From 7aaf466d188f69286fc9e79ef3b6fcd4dc00e8b9 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 07:32:53 +0000 Subject: [PATCH 2/3] qsa: opt-in STRATA_PA_FAST=1 - the gfx11 prompt attention with single FP16 q and p prompt_attn_wmma_kernel carries q and p as FP16 hi + lo pairs (two WMMAs each, FP32-level). SPLIT = false drops the lo halves: FP16 attention as the usual flash-attention kernels, half the matrix work. gfx1151, real 64K selections (16383 queries x 2051 cells, one QSA layer): 138.7 -> 117.8 ms; rel RMS vs FP64 4.8e-7 -> 2.7e-4. Engine 8K "qsa attn" 718 / 712 -> 589 / 590 ms. Teacher-forced KL on three text offsets 0.153 / 0.087 / 0.027 (the accepted MMQ-vs-fused pair: 0.153 / 0.117 / 0.030). Off by default. --- src/kernels/cuda/qsa_prompt_attn.cu | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cu index 30de4b34..6cfd8e43 100644 --- a/src/kernels/cuda/qsa_prompt_attn.cu +++ b/src/kernels/cuda/qsa_prompt_attn.cu @@ -754,6 +754,10 @@ __device__ __forceinline__ wh8 i8_to_h(const uint32_t* x) { // PA_KL codes, ex return h; } +// SPLIT = true (default): q and p carried as FP16 hi + lo pairs (two WMMAs each: FP32-level). S23 (opt-in +// STRATA_PA_FAST=1): SPLIT = false, one FP16 image each - half the matrix work, rounding-level (FP16 attention as the +// usual flash-attention kernels), quality-gated +template __global__ void __launch_bounds__(THREADS) prompt_attn_wmma_kernel(const float* __restrict__ q, QsaAttnPools p, const int32_t* __restrict__ ids, const int32_t* __restrict__ steps, int n_kv_heads, @@ -859,7 +863,7 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_wmma_kernel(const float* } const wh8 b = i8_to_h(kx); s = wmma_f16(qh[kk], b, s); - s = wmma_f16(ql[kk], b, s); + if constexpr (SPLIT) s = wmma_f16(ql[kk], b, s); } #pragma unroll for (int i = 0; i < 8; ++i) S.part[warp][PA_ROW(i, half)][nt * 16 + col] = s[i] * ksc; @@ -925,7 +929,7 @@ __global__ void __launch_bounds__(THREADS) prompt_attn_wmma_kernel(const float* #pragma unroll for (int i = 0; i < PA_KL; ++i) b[i] = (_Float16) (int) (int8_t) S.v[warp][cb + i][d]; tmp[j] = wmma_f16(ah, b, tmp[j]); - tmp[j] = wmma_f16(al, b, tmp[j]); + if constexpr (SPLIT) tmp[j] = wmma_f16(al, b, tmp[j]); } } float a[8]; @@ -988,9 +992,15 @@ bool launch_wmma(const float* q, const QsaAttnPools& pools, const int32_t* ids, const float scale_log2 = 1.4426950408889634f / sqrtf((float) HD); for (int64_t q0 = 0; q0 < n_q; q0 += 65535) { const int64_t nb = n_q - q0 < 65535 ? n_q - q0 : 65535; - prompt_attn_wmma_kernel<<>>( - q + q0 * s.n_head * HD, pools, ids + q0 * cap, steps + q0 * kStepCount, (int) s.n_head_kv, - (int) s.page_size, scale_log2, attn + q0 * s.n_head * HD, (int) cap); + static const bool fast = [] { const char* e = std::getenv("STRATA_PA_FAST"); return e != nullptr && e[0] == '1'; }(); + if (fast) + prompt_attn_wmma_kernel<<>>( + q + q0 * s.n_head * HD, pools, ids + q0 * cap, steps + q0 * kStepCount, (int) s.n_head_kv, + (int) s.page_size, scale_log2, attn + q0 * s.n_head * HD, (int) cap); + else + prompt_attn_wmma_kernel<<>>( + q + q0 * s.n_head * HD, pools, ids + q0 * cap, steps + q0 * kStepCount, (int) s.n_head_kv, + (int) s.page_size, scale_log2, attn + q0 * s.n_head * HD, (int) cap); } const cudaError_t e = cudaGetLastError(); if (e != cudaSuccess) { From 4c00d2e7a32771740678547d990c78084aa991c8 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 07:32:53 +0000 Subject: [PATCH 3/3] prefill: opt-in STRATA_PF_GEMM=1 - a gfx11 WMMA GEMM for the FP16 prompt projections strata_pf_gemm_f16 (wmma_gemm.cu), tried in Gemm::f16 before hipBLASLt for T >= 64, N >= 512, K % 32 == 0, beta 0 / 1: block 128 x 256 (128 x 128 below N 1024), BK 32, 8 waves of 64 x 64, LDS double buffer with the next stage prefetched into named registers, grouped tile order (8 row tiles sweep all column tiles, so the weights stream from DRAM once per group). FP32 accumulation, another K order than hipBLASLt: rounding-level. gfx1151, M 16384 (TFLOPS, tuned hipBLASLt in brackets): 12288 x 2560 33.0 (27.4), 10240 x 2560 31.9 (26.8), 6144 x 2560 31.1 (25.9), 2560 x 2560 30.1 (24.9), 640 x 2560 30.2 (25.3), 2560 x 6144 19.4 (19.1); the register-only WMMA ceiling is ~50. Engine 8K: gdn 1122 / 1112 -> 1083 / 1071 ms, qsa proj 813 / 716 -> 692 / 676, gdn out proj 753 -> 645 / 665. Teacher-forced KL 0.099 / 0.028 on two text offsets (accepted pair: 0.117 / 0.030). Off by default. With STRATA_HC_UPMIX + STRATA_PA_FAST, UD-IQ4_XS 64K prompt 872.8 / 880.6 -> 942.4 / 937.8 tok/s. --- src/prefill/gemm.cu | 2 + src/prefill/wmma_gemm.cu | 121 +++++++++++++++++++++++++++++++++++++++ src/prefill/wmma_gemm.h | 5 ++ 3 files changed, 128 insertions(+) diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu index 2cfd6de5..0e6b3758 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -413,6 +413,8 @@ void Gemm::f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_ #ifdef STRATA_USE_HIP // #313 (opt-in STRATA_WMMA_GEMM=1): RDNA3 / RDNA3.5 WMMA dense GEMM, before hipBLASLt (falls through when false) static const bool wmma_on = [] { const char* v = std::getenv("STRATA_WMMA_GEMM"); return v && v[0] != 0 && v[0] != '0'; }(); + static const bool pf_on = [] { const char* v = std::getenv("STRATA_PF_GEMM"); return v && v[0] == '1'; }(); + if (pf_on && strata_pf_gemm_f16(X, W, Y, T, N, K, ldy, beta, stream_)) return; // S23: opt-in if (wmma_on && T >= 16 && (beta == 0.0f || beta == 1.0f) && strata_wmma_gemm_f16(X, W, Y, T, N, K, ldy, beta, stream_)) { return; diff --git a/src/prefill/wmma_gemm.cu b/src/prefill/wmma_gemm.cu index 95af0828..cd379636 100644 --- a/src/prefill/wmma_gemm.cu +++ b/src/prefill/wmma_gemm.cu @@ -282,8 +282,129 @@ bool strata_wmma_gemm_bf16(const uint16_t* X, const uint16_t* W, float* Y, return strata_wmma_gemm_dispatch<__bf16>(X, W, Y, T, N, K, ldy, beta, stream); } +// =========================================================================== +// S23 (opt-in STRATA_PF_GEMM=1): the prompt projections' FP16 GEMM on gfx11 (gfx1151: 30-33 TFLOPS on the 16K-row +// projection shapes where the tuned hipBLASLt reaches 25-27; s23/gemm_probe4.hip). Block 128 x BN (BN 256 or 128), +// BK 32, 8 waves of 64 x BN/4, LDS double buffer with the next stage prefetched into named registers (arrays there +// went to scratch), grouped tile order (8 row tiles sweep all column tiles: the weights stream from DRAM once per +// group). FP32 accumulation, K in another order than hipBLASLt: rounding-level, quality-gated. +// =========================================================================== +namespace pfg { +typedef _Float16 h16 __attribute__((ext_vector_type(16))); +typedef float f8 __attribute__((ext_vector_type(8))); +constexpr int BM = 128, BK = 32, LDK = BK + 8, GM = 8; +__device__ __forceinline__ h16 frag(const _Float16* p) { + const uint4 a = *reinterpret_cast(p), b = *reinterpret_cast(p + 8); + const uint32_t w[8] = {a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w}; + return __builtin_bit_cast(h16, w); +} +template +__global__ void __launch_bounds__(256) kernel(const _Float16* __restrict__ X, const _Float16* __restrict__ W, + float* __restrict__ Y, int M, int N, int K, int ldy, int accumulate) { +#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1150__) || defined(__gfx1151__) + constexpr int TN = WN / 16, NB = BN / 64; + __shared__ __align__(16) _Float16 sA[2][BM][LDK]; + __shared__ __align__(16) _Float16 sB[2][BN][LDK]; + const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4; + const int wm = wave & 1, wn = wave >> 1; + const int num_m = (M + BM - 1) / BM, num_n = (N + BN - 1) / BN, b = blockIdx.x; + const int group = b / (GM * num_n), first_m = group * GM, gsize = min(GM, num_m - first_m); + const int m0 = (first_m + (b % (GM * num_n)) % gsize) * BM, n0 = ((b % (GM * num_n)) / gsize) * BN; + const int sr = tid >> 2, sq = tid & 3; + auto ldA = [&](int k0, int r) -> uint4 { + return *reinterpret_cast(X + (size_t) min(m0 + r, M - 1) * K + k0 + 8 * sq); + }; + auto ldB = [&](int k0, int r) -> uint4 { + return *reinterpret_cast(W + (size_t) min(n0 + r, N - 1) * K + k0 + 8 * sq); + }; + f8 acc[4][TN]; + for (int i = 0; i < 4; ++i) for (int j = 0; j < TN; ++j) acc[i][j] = f8{0, 0, 0, 0, 0, 0, 0, 0}; + uint4 ra0 = ldA(0, sr), ra1 = ldA(0, sr + 64), rb0 = ldB(0, sr), rb1 = ldB(0, sr + 64), rb2 = rb0, rb3 = rb0; + if constexpr (NB > 2) { rb2 = ldB(0, sr + 128); rb3 = ldB(0, sr + 192); } + int buf = 0; + *reinterpret_cast(&sA[0][sr][8 * sq]) = ra0; *reinterpret_cast(&sA[0][sr + 64][8 * sq]) = ra1; + *reinterpret_cast(&sB[0][sr][8 * sq]) = rb0; *reinterpret_cast(&sB[0][sr + 64][8 * sq]) = rb1; + if constexpr (NB > 2) { *reinterpret_cast(&sB[0][sr + 128][8 * sq]) = rb2; *reinterpret_cast(&sB[0][sr + 192][8 * sq]) = rb3; } + __syncthreads(); + for (int k0 = 0; k0 < K; k0 += BK) { + const bool more = k0 + BK < K; + if (more) { + ra0 = ldA(k0 + BK, sr); ra1 = ldA(k0 + BK, sr + 64); rb0 = ldB(k0 + BK, sr); rb1 = ldB(k0 + BK, sr + 64); + if constexpr (NB > 2) { rb2 = ldB(k0 + BK, sr + 128); rb3 = ldB(k0 + BK, sr + 192); } + } +#pragma unroll + for (int ks = 0; ks < BK; ks += 16) { + h16 a[4], bb[TN]; +#pragma unroll + for (int i = 0; i < 4; ++i) a[i] = frag(&sA[buf][64 * wm + 16 * i + l16][ks]); +#pragma unroll + for (int j = 0; j < TN; ++j) bb[j] = frag(&sB[buf][WN * wn + 16 * j + l16][ks]); +#pragma unroll + for (int i = 0; i < 4; ++i) +#pragma unroll + for (int j = 0; j < TN; ++j) acc[i][j] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a[i], bb[j], acc[i][j]); + } + if (more) { + const int nb = buf ^ 1; + *reinterpret_cast(&sA[nb][sr][8 * sq]) = ra0; *reinterpret_cast(&sA[nb][sr + 64][8 * sq]) = ra1; + *reinterpret_cast(&sB[nb][sr][8 * sq]) = rb0; *reinterpret_cast(&sB[nb][sr + 64][8 * sq]) = rb1; + if constexpr (NB > 2) { *reinterpret_cast(&sB[nb][sr + 128][8 * sq]) = rb2; *reinterpret_cast(&sB[nb][sr + 192][8 * sq]) = rb3; } + __syncthreads(); + buf = nb; + } + } +#pragma unroll + for (int i = 0; i < 4; ++i) +#pragma unroll + for (int j = 0; j < TN; ++j) { + const int n = n0 + WN * wn + 16 * j + l16; + if (n >= N) continue; +#pragma unroll + for (int e = 0; e < 8; ++e) { + const int m = m0 + 64 * wm + 16 * i + 2 * e + hi; + if (m < M) { + float* y = Y + (size_t) m * ldy + n; + *y = accumulate ? *y + acc[i][j][e] : acc[i][j][e]; + } + } + } +#endif +} +} // namespace pfg + +bool strata_pf_gemm_f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy, + float beta, void* stream) { + if (!X || !W || !Y || T < 64 || N < 512 || K % pfg::BK != 0 || K < pfg::BK) return false; + if (beta != 0.0f && beta != 1.0f) return false; + if (ldy <= 0) ldy = N; + if (ldy < N || T > (1LL << 30) || N > (1LL << 30) || K > (1LL << 30)) return false; + static const bool gfx11 = [] { + int dev = 0; + hipDeviceProp_t prop{}; + if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&prop, dev) != hipSuccess) return false; + return std::strncmp(prop.gcnArchName, "gfx11", 5) == 0; + }(); + if (!gfx11) return false; + hipStream_t s = static_cast(stream); + const int acc = beta == 1.0f ? 1 : 0; + const int64_t mt = (T + pfg::BM - 1) / pfg::BM; + if (N >= 1024) { + const unsigned grid = (unsigned) (mt * ((N + 255) / 256)); + pfg::kernel<256, 64><<>>((const _Float16*) X, (const _Float16*) W, Y, (int) T, (int) N, (int) K, + (int) ldy, acc); + } else { + const unsigned grid = (unsigned) (mt * ((N + 127) / 128)); + pfg::kernel<128, 32><<>>((const _Float16*) X, (const _Float16*) W, Y, (int) T, (int) N, (int) K, + (int) ldy, acc); + } + return hipGetLastError() == hipSuccess; +} + #else // Non-HIP / Non-GFX11 compilation fallback +bool strata_pf_gemm_f16(const uint16_t*, const uint16_t*, float*, int64_t, int64_t, int64_t, int64_t, float, void*) { + return false; +} bool strata_wmma_gemm_f16(const uint16_t*, const uint16_t*, float*, int64_t, int64_t, int64_t, int64_t, float, void*) { diff --git a/src/prefill/wmma_gemm.h b/src/prefill/wmma_gemm.h index 49474dfd..1a5085ca 100644 --- a/src/prefill/wmma_gemm.h +++ b/src/prefill/wmma_gemm.h @@ -17,6 +17,11 @@ bool strata_wmma_gemm_f16(const uint16_t* X, const uint16_t* W, float* Y, /// W: N x K row-major (leading dim K), bf16 (uint16_t) /// Y: T x N row-major with leading dimension ldy >= N, fp32 (float) /// Returns true if executed on WMMA, false if unsupported shape/parameters (fall back to hipblas). +/// S23 (opt-in STRATA_PF_GEMM=1): Y = beta * Y + X . W^T, FP16 in, FP32 out, beta 0 or 1, K a multiple of 32, +/// T >= 64, N >= 512; the 128 x 256 / 128 x 128 grouped-order WMMA kernel. False (nothing launched) otherwise / off gfx11. +bool strata_pf_gemm_f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy, + float beta, void* stream = nullptr); + bool strata_wmma_gemm_bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy, float beta, void* stream = nullptr);