okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01f8c3255
main
1// src/prefill/wmma_gemm.cu - RDNA3 WMMA FP16 & BF16 GEMM for Strata prefill (gfx1100). 2// 3// Computes Y[t, n] = beta * Y[t, n] + sum_k W[n, k] * X[t, k] using RDNA3 WMMA intrinsics: 4// * X is T x K row-major (leading dim K), fp16 or bf16 5// * W is N x K row-major (leading dim K), fp16 or bf16 6// * Y is T x N row-major with leading dimension ldy >= N, fp32 7// 8// Hardware: AMD RDNA3 (gfx1100, e.g. RX 7900 XTX) 9// * v_wmma_f32_16x16x16_f16_w32 intrinsic (__builtin_amdgcn_wmma_f32_16x16x16_f16_w32) 10// * v_wmma_f32_16x16x16_bf16_w32 intrinsic (__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32) 11// * Wave32 doubled input fragment: lane t (lane_lo = t & 15) holds row lane_lo of A (16 elements along K) 12// and column lane_lo of B (16 elements along K). Lanes 16..31 duplicate lanes 0..15. 13// * Wave32 C output mapping: lane t holds column lane_lo of 16x16 output tile, 14// with 8 elements alternating rows: row m = 2*i + lane_hi (lane_hi = t >> 4). 15// * Tile variants: 16// 1. gemm_wmma_64x64_4w: 4 waves (128 threads), 64T x 64N x 16K tile, double-buffered LDS for W. 17// 2. gemm_wmma_16x16_1w: 1 wave (32 threads), 16T x 16N x 16K tile, zero LDS/barriers, for small T/N. 18 19#include "wmma_gemm.h" 20 21#include <hip/hip_runtime.h> 22#include <hip/hip_fp16.h> 23#include <hip/hip_bfloat16.h> 24 25#include <cstdlib> 26#include "strata/kernels/gfx_arch.hpp" 27 28#include <atomic> 29#include <cstring> 30 31// STRATA_WMMA_GFX11 is defined by the BUILD (CMakeLists.txt, from CMAKE_HIP_ARCHITECTURES), not inferred 32// from compiler macros: measured on this toolchain the HOST pass of a HIP compile does not define 33// __gfx1100__ but does define __HIP_DEVICE_COMPILE__, so a compiler-macro guard here silently selected the 34// "return false" stub at the bottom of this file for the very symbol the engine links - the WMMA path then 35// never ran, while the same file compiled with hipcc (as the probes do) took the real branch. One 36// build-defined macro is uniform across the host and device passes. 37#if defined(STRATA_WMMA_GFX11) 38 39using v8fp32 = float __attribute__((ext_vector_type(8))); 40 41// Does the CURRENT device run the gfx11 WMMA kernels of this file? Exactly the targets whose intrinsics are selected 42// below (gfx1100 / 1101 / 1102 / 1103 / 1150 / 1151): a gfx11 part outside the list (gfx1152) has no device code 43// here, so it takes the hipBLASLt / hipBLAS paths instead of running a kernel that is not there. Cached per device 44// (hipGetDeviceProperties is not cheap, and a mixed-GPU box has more than one answer). 45static bool strata_wmma_gfx11_device() { 46 static std::atomic<int> cache[64]; // 0 unknown, 1 yes, 2 no 47 int dev = 0; 48 if (hipGetDevice(&dev) != hipSuccess || dev < 0 || dev >= 64) return false; 49 int c = cache[dev].load(std::memory_order_acquire); 50 if (c == 0) { 51 hipDeviceProp_t prop{}; 52 const bool ok = hipGetDeviceProperties(&prop, dev) == hipSuccess && strata::kernels::gfx_arch_is_gfx11_wmma(prop.gcnArchName); 53 c = ok ? 1 : 2; 54 cache[dev].store(c, std::memory_order_release); 55 } 56 return c == 1; 57} 58 59// The same answer for host code that decides whether to pad an activation's row stride (prefill.cpp, STRATA_PF_PAD). 60bool strata_pf_padded_gemm_available() { return strata_wmma_gfx11_device(); } 61 62template <typename ElemT> 63struct WmmaTraits; 64 65template <> 66struct WmmaTraits<_Float16> { 67 using vec_t = _Float16 __attribute__((ext_vector_type(16))); 68 __device__ static inline v8fp32 mma(vec_t a, vec_t b, v8fp32 c) { 69#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || defined(__gfx1151__) 70 return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c); 71#else 72 (void) a; (void) b; (void) c; 73 __builtin_trap(); // a device pass without the gfx11 intrinsic: the runtime gate never launches it, and a stray launch must not return zeros 74#endif 75 } 76}; 77 78template <> 79struct WmmaTraits<__bf16> { 80 using vec_t = __bf16 __attribute__((ext_vector_type(16))); 81 __device__ static inline v8fp32 mma(vec_t a, vec_t b, v8fp32 c) { 82#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || defined(__gfx1151__) 83 return __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a, b, c); 84#else 85 (void) a; (void) b; (void) c; 86 __builtin_trap(); // see above 87#endif 88 } 89}; 90 91// =========================================================================== 92// Variant 1: 16x16_1w - Single-wave kernel for small T / N. 93// Zero LDS, zero barrier synchronization, full register residency. 94// =========================================================================== 95template <typename ElemT> 96__global__ void gemm_wmma_16x16_1w( 97 const uint16_t* __restrict__ X, 98 const uint16_t* __restrict__ W, 99 float* __restrict__ Y, 100 int64_t T, int64_t N, int64_t K, int64_t ldy, float beta) { 101 102 using vec_t = typename WmmaTraits<ElemT>::vec_t; 103 const int m_tile = blockIdx.y * 16; 104 const int n_tile = blockIdx.x * 16; 105 if (m_tile >= T || n_tile >= N) return; 106 107 const int lane = threadIdx.x; // 0..31 108 const int lane_lo = lane & 15; // 0..15 109 const int lane_hi = lane >> 4; // 0 or 1 110 111 v8fp32 c_acc = {0, 0, 0, 0, 0, 0, 0, 0}; 112 113 const int m_row = m_tile + lane_lo; 114 const int n_row = n_tile + lane_lo; 115 116 for (int k_tile = 0; k_tile < K; k_tile += 16) { 117 vec_t a_frag, b_frag; 118 119 if (m_row < T) { 120 __builtin_memcpy(&a_frag, X + (int64_t)m_row * K + k_tile, sizeof(a_frag)); 121 } else { 122 #pragma unroll 123 for (int i = 0; i < 16; ++i) a_frag[i] = 0; 124 } 125 126 if (n_row < N) { 127 __builtin_memcpy(&b_frag, W + (int64_t)n_row * K + k_tile, sizeof(b_frag)); 128 } else { 129 #pragma unroll 130 for (int i = 0; i < 16; ++i) b_frag[i] = 0; 131 } 132 133 c_acc = WmmaTraits<ElemT>::mma(a_frag, b_frag, c_acc); 134 } 135 136 const int out_n = n_tile + lane_lo; 137 if (out_n < N) { 138 #pragma unroll 139 for (int i = 0; i < 8; ++i) { 140 const int out_m = m_tile + 2 * i + lane_hi; 141 if (out_m < T) { 142 float* dst = Y + (int64_t)out_m * ldy + out_n; 143 if (beta == 0.0f) { 144 *dst = c_acc[i]; 145 } else { 146 *dst = beta * (*dst) + c_acc[i]; 147 } 148 } 149 } 150 } 151} 152 153// =========================================================================== 154// Variant 2: 64x64_4w - 4 waves per block (128 threads), 64T x 64N tile. 155// Double-buffered LDS tile for W (4 KB LDS total), cooperative 128-bit global loads. 156// =========================================================================== 157template <typename ElemT> 158__global__ void gemm_wmma_64x64_4w( 159 const uint16_t* __restrict__ X, 160 const uint16_t* __restrict__ W, 161 float* __restrict__ Y, 162 int64_t T, int64_t N, int64_t K, int64_t ldy, float beta) { 163 164 using vec_t = typename WmmaTraits<ElemT>::vec_t; 165 const int m_tile = blockIdx.y * 64; 166 const int n_tile = blockIdx.x * 64; 167 if (m_tile >= T || n_tile >= N) return; 168 169 const int tid = threadIdx.x; // 0..127 170 const int wave_id = tid >> 5; // 0..3 171 const int lane = tid & 31; // 0..31 172 const int lane_lo = lane & 15; // 0..15 173 const int lane_hi = lane >> 4; // 0 or 1 174 175 // 4 accumulators per wave covering 4 x 16 N-subtiles 176 v8fp32 c_acc0 = {0, 0, 0, 0, 0, 0, 0, 0}; 177 v8fp32 c_acc1 = {0, 0, 0, 0, 0, 0, 0, 0}; 178 v8fp32 c_acc2 = {0, 0, 0, 0, 0, 0, 0, 0}; 179 v8fp32 c_acc3 = {0, 0, 0, 0, 0, 0, 0, 0}; 180 181 // Double-buffered LDS tile: 64 rows of N x 16 elements of K (2 KB per buffer) 182 alignas(16) __shared__ ElemT b_lds[2][64][16]; 183 184 // Thread mapping for cooperative loading of W into LDS (128 threads load 64x16 elements) 185 // Each thread loads 8 halfs (16 bytes = uint4) 186 const int row_in_tile = tid >> 1; // 0..63 187 const int k_sub = (tid & 1) << 3; // 0 or 8 188 const int actual_n = n_tile + row_in_tile; 189 190 auto load_w_into_lds = [&](int buf, int k_tile) { 191 const int actual_k = k_tile + k_sub; 192 uint4 val = {0, 0, 0, 0}; 193 if (actual_n < N && actual_k < K) { 194 const void* ptr = reinterpret_cast<const void*>(W + (int64_t)actual_n * K + actual_k); 195 val = *reinterpret_cast<const uint4*>(ptr); 196 } 197 *reinterpret_cast<uint4*>(&b_lds[buf][row_in_tile][k_sub]) = val; 198 }; 199 200 // Pre-fill buffer 0 201 load_w_into_lds(0, 0); 202 __syncthreads(); 203 204 const int m_row = m_tile + wave_id * 16 + lane_lo; 205 int cur_buf = 0; 206 207 for (int k_tile = 0; k_tile < K; k_tile += 16) { 208 const int next_buf = 1 - cur_buf; 209 const int k_next = k_tile + 16; 210 211 // Prefetch next K-tile of W into LDS 212 if (k_next < K) { 213 load_w_into_lds(next_buf, k_next); 214 } 215 216 // Load A fragment from X for current wave's 16 M-rows 217 vec_t a_frag; 218 if (m_row < T) { 219 __builtin_memcpy(&a_frag, X + (int64_t)m_row * K + k_tile, sizeof(a_frag)); 220 } else { 221 #pragma unroll 222 for (int i = 0; i < 16; ++i) a_frag[i] = 0; 223 } 224 225 // Read B fragments from LDS and compute WMMA 226 vec_t b_frag0, b_frag1, b_frag2, b_frag3; 227 __builtin_memcpy(&b_frag0, &b_lds[cur_buf][0 + lane_lo][0], sizeof(vec_t)); 228 __builtin_memcpy(&b_frag1, &b_lds[cur_buf][16 + lane_lo][0], sizeof(vec_t)); 229 __builtin_memcpy(&b_frag2, &b_lds[cur_buf][32 + lane_lo][0], sizeof(vec_t)); 230 __builtin_memcpy(&b_frag3, &b_lds[cur_buf][48 + lane_lo][0], sizeof(vec_t)); 231 232 c_acc0 = WmmaTraits<ElemT>::mma(a_frag, b_frag0, c_acc0); 233 c_acc1 = WmmaTraits<ElemT>::mma(a_frag, b_frag1, c_acc1); 234 c_acc2 = WmmaTraits<ElemT>::mma(a_frag, b_frag2, c_acc2); 235 c_acc3 = WmmaTraits<ElemT>::mma(a_frag, b_frag3, c_acc3); 236 237 __syncthreads(); 238 cur_buf = next_buf; 239 } 240 241 // Store C to Y (coalesced 64-byte writes per wave half) 242 const int m_tile_wave = m_tile + wave_id * 16; 243 auto store_acc = [&](const v8fp32& acc, int n_base) { 244 const int out_n = n_base + lane_lo; 245 if (out_n < N) { 246 #pragma unroll 247 for (int i = 0; i < 8; ++i) { 248 const int out_m = m_tile_wave + 2 * i + lane_hi; 249 if (out_m < T) { 250 float* dst = Y + (int64_t)out_m * ldy + out_n; 251 if (beta == 0.0f) { 252 *dst = acc[i]; 253 } else { 254 *dst = beta * (*dst) + acc[i]; 255 } 256 } 257 } 258 } 259 }; 260 261 store_acc(c_acc0, n_tile + 0); 262 store_acc(c_acc1, n_tile + 16); 263 store_acc(c_acc2, n_tile + 32); 264 store_acc(c_acc3, n_tile + 48); 265} 266 267template <typename ElemT> 268static inline bool strata_wmma_gemm_dispatch(const uint16_t* X, const uint16_t* W, float* Y, 269 int64_t T, int64_t N, int64_t K, int64_t ldy, float beta, 270 void* stream) { 271 if (!X || !W || !Y) return false; 272 if (T <= 0 || N <= 0 || K <= 0) return false; 273 // Runtime gate (review): run only on gfx11 devices, whatever this build compiled for. The build-time 274 // STRATA_WMMA_GFX11 macro controls whether the intrinsics compile; this check controls whether they run. 275 if (!strata_wmma_gfx11_device()) return false; 276 if (K % 16 != 0) return false; 277 if (beta != 0.0f && beta != 1.0f) return false; 278 if (ldy <= 0) ldy = N; 279 if (ldy < N) return false; 280 281 hipStream_t s = static_cast<hipStream_t>(stream); 282 283 if (T >= 32 && N >= 32) { 284 dim3 block(128); 285 dim3 grid((uint32_t)((N + 63) / 64), (uint32_t)((T + 63) / 64), 1); 286 gemm_wmma_64x64_4w<ElemT><<<grid, block, 0, s>>>(X, W, Y, T, N, K, ldy, beta); 287 } else { 288 dim3 block(32); 289 dim3 grid((uint32_t)((N + 15) / 16), (uint32_t)((T + 15) / 16), 1); 290 gemm_wmma_16x16_1w<ElemT><<<grid, block, 0, s>>>(X, W, Y, T, N, K, ldy, beta); 291 } 292 return true; 293} 294 295bool strata_wmma_gemm_f16(const uint16_t* X, const uint16_t* W, float* Y, 296 int64_t T, int64_t N, int64_t K, int64_t ldy, float beta, 297 void* stream) { 298 return strata_wmma_gemm_dispatch<_Float16>(X, W, Y, T, N, K, ldy, beta, stream); 299} 300 301bool strata_wmma_gemm_bf16(const uint16_t* X, const uint16_t* W, float* Y, 302 int64_t T, int64_t N, int64_t K, int64_t ldy, float beta, 303 void* stream) { 304 return strata_wmma_gemm_dispatch<__bf16>(X, W, Y, T, N, K, ldy, beta, stream); 305} 306 307// =========================================================================== 308// S23 (opt-in STRATA_PF_GEMM=1): the prompt projections' FP16 GEMM on gfx11 (gfx1151: 30-33 TFLOPS on the 16K-row 309// projection shapes where the tuned hipBLASLt reaches 25-27; s23/gemm_probe4.hip). Block 128 x BN (BN 256 or 128), 310// BK 32, 8 waves of 64 x BN/4, LDS double buffer with the next stage prefetched into named registers (arrays there 311// went to scratch), grouped tile order (8 row tiles sweep all column tiles: the weights stream from DRAM once per 312// group). FP32 accumulation, K in another order than hipBLASLt: rounding-level, quality-gated. 313// =========================================================================== 314namespace pfg { 315typedef _Float16 h16 __attribute__((ext_vector_type(16))); 316typedef float f8 __attribute__((ext_vector_type(8))); 317constexpr int BM = 128, BK = 32, LDK = BK + 8, GM = 8; 318__device__ __forceinline__ h16 frag(const _Float16* p) { 319 const uint4 a = *reinterpret_cast<const uint4*>(p), b = *reinterpret_cast<const uint4*>(p + 8); 320 const uint32_t w[8] = {a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w}; 321 return __builtin_bit_cast(h16, w); 322} 323template <int BN, int WN> 324__global__ void __launch_bounds__(256) kernel(const _Float16* __restrict__ X, const _Float16* __restrict__ W, 325 float* __restrict__ Y, int M, int N, int K, int ldy, int accumulate, 326 int ldx, int ldw) { 327#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || defined(__gfx1151__) 328 constexpr int TN = WN / 16, NB = BN / 64; 329 __shared__ __align__(16) _Float16 sA[2][BM][LDK]; 330 __shared__ __align__(16) _Float16 sB[2][BN][LDK]; 331 const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4; 332 const int wm = wave & 1, wn = wave >> 1; 333 const int num_m = (M + BM - 1) / BM, num_n = (N + BN - 1) / BN, b = blockIdx.x; 334 const int group = b / (GM * num_n), first_m = group * GM, gsize = min(GM, num_m - first_m); 335 const int m0 = (first_m + (b % (GM * num_n)) % gsize) * BM, n0 = ((b % (GM * num_n)) / gsize) * BN; 336 const int sr = tid >> 2, sq = tid & 3; 337 auto ldA = [&](int k0, int r) -> uint4 { 338 return *reinterpret_cast<const uint4*>(X + (size_t) min(m0 + r, M - 1) * ldx + k0 + 8 * sq); 339 }; 340 auto ldB = [&](int k0, int r) -> uint4 { 341 return *reinterpret_cast<const uint4*>(W + (size_t) min(n0 + r, N - 1) * ldw + k0 + 8 * sq); 342 }; 343 f8 acc[4][TN]; 344 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}; 345 uint4 ra0 = ldA(0, sr), ra1 = ldA(0, sr + 64), rb0 = ldB(0, sr), rb1 = ldB(0, sr + 64), rb2 = rb0, rb3 = rb0; 346 if constexpr (NB > 2) { rb2 = ldB(0, sr + 128); rb3 = ldB(0, sr + 192); } 347 int buf = 0; 348 *reinterpret_cast<uint4*>(&sA[0][sr][8 * sq]) = ra0; *reinterpret_cast<uint4*>(&sA[0][sr + 64][8 * sq]) = ra1; 349 *reinterpret_cast<uint4*>(&sB[0][sr][8 * sq]) = rb0; *reinterpret_cast<uint4*>(&sB[0][sr + 64][8 * sq]) = rb1; 350 if constexpr (NB > 2) { *reinterpret_cast<uint4*>(&sB[0][sr + 128][8 * sq]) = rb2; *reinterpret_cast<uint4*>(&sB[0][sr + 192][8 * sq]) = rb3; } 351 __syncthreads(); 352 for (int k0 = 0; k0 < K; k0 += BK) { 353 const bool more = k0 + BK < K; 354 if (more) { 355 ra0 = ldA(k0 + BK, sr); ra1 = ldA(k0 + BK, sr + 64); rb0 = ldB(k0 + BK, sr); rb1 = ldB(k0 + BK, sr + 64); 356 if constexpr (NB > 2) { rb2 = ldB(k0 + BK, sr + 128); rb3 = ldB(k0 + BK, sr + 192); } 357 } 358#pragma unroll 359 for (int ks = 0; ks < BK; ks += 16) { 360 h16 a[4], bb[TN]; 361#pragma unroll 362 for (int i = 0; i < 4; ++i) a[i] = frag(&sA[buf][64 * wm + 16 * i + l16][ks]); 363#pragma unroll 364 for (int j = 0; j < TN; ++j) bb[j] = frag(&sB[buf][WN * wn + 16 * j + l16][ks]); 365#pragma unroll 366 for (int i = 0; i < 4; ++i) 367#pragma unroll 368 for (int j = 0; j < TN; ++j) acc[i][j] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a[i], bb[j], acc[i][j]); 369 } 370 if (more) { 371 const int nb = buf ^ 1; 372 *reinterpret_cast<uint4*>(&sA[nb][sr][8 * sq]) = ra0; *reinterpret_cast<uint4*>(&sA[nb][sr + 64][8 * sq]) = ra1; 373 *reinterpret_cast<uint4*>(&sB[nb][sr][8 * sq]) = rb0; *reinterpret_cast<uint4*>(&sB[nb][sr + 64][8 * sq]) = rb1; 374 if constexpr (NB > 2) { *reinterpret_cast<uint4*>(&sB[nb][sr + 128][8 * sq]) = rb2; *reinterpret_cast<uint4*>(&sB[nb][sr + 192][8 * sq]) = rb3; } 375 __syncthreads(); 376 buf = nb; 377 } 378 } 379#pragma unroll 380 for (int i = 0; i < 4; ++i) 381#pragma unroll 382 for (int j = 0; j < TN; ++j) { 383 const int n = n0 + WN * wn + 16 * j + l16; 384 if (n >= N) continue; 385#pragma unroll 386 for (int e = 0; e < 8; ++e) { 387 const int m = m0 + 64 * wm + 16 * i + 2 * e + hi; 388 if (m < M) { 389 float* y = Y + (size_t) m * ldy + n; 390 *y = accumulate ? *y + acc[i][j][e] : acc[i][j][e]; 391 } 392 } 393 } 394#endif 395} 396// The 128 x 256 kernel with a 64-k LDS tile (single buffer, the next tile in 12 named staging registers, 2 barriers 397// per tile: half the loop trips and barriers per k of the BK 32 double buffer). Each accumulator takes the same 398// WMMAs in the same k order as `kernel`: the same bits (s23/gemm_probe11.hip, memcmp). gfx1151, M 16384, both 399// operands padded: 38.0 -> 41.3 TFLOPS (N 2560 K 6144), 38.9 -> 40.9 (N 12288 K 2560), 40.3 -> 40.4 (N 10240). 400// K a multiple of 64. 401__global__ void __launch_bounds__(256) kernel_bk64(const _Float16* __restrict__ X, const _Float16* __restrict__ W, 402 float* __restrict__ Y, int M, int N, int K, int ldy, int accumulate, 403 int ldx, int ldw) { 404#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || defined(__gfx1151__) 405 constexpr int BN = 256, WN = 64, BK64 = 64, LDK64 = BK64 + 8; 406 __shared__ __align__(16) _Float16 sA[BM][LDK64]; 407 __shared__ __align__(16) _Float16 sB[BN][LDK64]; 408 const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4; 409 const int wm = wave & 1, wn = wave >> 1; 410 const int num_m = (M + BM - 1) / BM, num_n = (N + BN - 1) / BN, b = blockIdx.x; 411 const int group = b / (GM * num_n), first_m = group * GM, gsize = min(GM, num_m - first_m); 412 const int m0 = (first_m + (b % (GM * num_n)) % gsize) * BM, n0 = ((b % (GM * num_n)) / gsize) * BN; 413 const int sr = tid >> 3, sq = tid & 7; // 32 rows per pass, 8 x 8 halves per row 414 auto ldA = [&](int k0, int r) -> uint4 { 415 return *reinterpret_cast<const uint4*>(X + (size_t) min(m0 + r, M - 1) * ldx + k0 + 8 * sq); 416 }; 417 auto ldB = [&](int k0, int r) -> uint4 { 418 return *reinterpret_cast<const uint4*>(W + (size_t) min(n0 + r, N - 1) * ldw + k0 + 8 * sq); 419 }; 420 uint4 ra0, ra1, ra2, ra3, rb0, rb1, rb2, rb3, rb4, rb5, rb6, rb7; 421 auto load = [&](int k0) { 422 ra0 = ldA(k0, sr); ra1 = ldA(k0, sr + 32); ra2 = ldA(k0, sr + 64); ra3 = ldA(k0, sr + 96); 423 rb0 = ldB(k0, sr); rb1 = ldB(k0, sr + 32); rb2 = ldB(k0, sr + 64); rb3 = ldB(k0, sr + 96); 424 rb4 = ldB(k0, sr + 128); rb5 = ldB(k0, sr + 160); rb6 = ldB(k0, sr + 192); rb7 = ldB(k0, sr + 224); 425 }; 426 auto store = [&]() { 427 *reinterpret_cast<uint4*>(&sA[sr][8 * sq]) = ra0; *reinterpret_cast<uint4*>(&sA[sr + 32][8 * sq]) = ra1; 428 *reinterpret_cast<uint4*>(&sA[sr + 64][8 * sq]) = ra2; *reinterpret_cast<uint4*>(&sA[sr + 96][8 * sq]) = ra3; 429 *reinterpret_cast<uint4*>(&sB[sr][8 * sq]) = rb0; *reinterpret_cast<uint4*>(&sB[sr + 32][8 * sq]) = rb1; 430 *reinterpret_cast<uint4*>(&sB[sr + 64][8 * sq]) = rb2; *reinterpret_cast<uint4*>(&sB[sr + 96][8 * sq]) = rb3; 431 *reinterpret_cast<uint4*>(&sB[sr + 128][8 * sq]) = rb4; *reinterpret_cast<uint4*>(&sB[sr + 160][8 * sq]) = rb5; 432 *reinterpret_cast<uint4*>(&sB[sr + 192][8 * sq]) = rb6; *reinterpret_cast<uint4*>(&sB[sr + 224][8 * sq]) = rb7; 433 }; 434 f8 acc[4][4]; 435#pragma unroll 436 for (int i = 0; i < 4; ++i) 437#pragma unroll 438 for (int j = 0; j < 4; ++j) acc[i][j] = f8{0, 0, 0, 0, 0, 0, 0, 0}; 439 load(0); 440 store(); 441 __syncthreads(); 442 const int ar = 64 * wm + l16, br = WN * wn + l16; 443 for (int k0 = 0; k0 < K; k0 += BK64) { 444 const bool more = k0 + BK64 < K; 445 if (more) load(k0 + BK64); 446#pragma unroll 447 for (int ks = 0; ks < BK64; ks += 16) { 448 const h16 a0 = frag(&sA[ar][ks]), a1 = frag(&sA[ar + 16][ks]), a2 = frag(&sA[ar + 32][ks]), 449 a3 = frag(&sA[ar + 48][ks]); 450 const h16 b0 = frag(&sB[br][ks]), b1 = frag(&sB[br + 16][ks]), b2 = frag(&sB[br + 32][ks]), 451 b3 = frag(&sB[br + 48][ks]); 452#define PFG_ROW(i, a) \ 453 acc[i][0] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b0, acc[i][0]); \ 454 acc[i][1] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b1, acc[i][1]); \ 455 acc[i][2] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b2, acc[i][2]); \ 456 acc[i][3] = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b3, acc[i][3]); 457 PFG_ROW(0, a0) PFG_ROW(1, a1) PFG_ROW(2, a2) PFG_ROW(3, a3) 458#undef PFG_ROW 459 } 460 if (more) { 461 __syncthreads(); // every wave is done reading the tile 462 store(); 463 __syncthreads(); 464 } 465 } 466#pragma unroll 467 for (int i = 0; i < 4; ++i) 468#pragma unroll 469 for (int j = 0; j < 4; ++j) { 470 const int n = n0 + WN * wn + 16 * j + l16; 471 if (n >= N) continue; 472#pragma unroll 473 for (int e = 0; e < 8; ++e) { 474 const int m = m0 + 64 * wm + 16 * i + 2 * e + hi; 475 if (m < M) { 476 float* y = Y + (size_t) m * ldy + n; 477 *y = accumulate ? *y + acc[i][j][e] : acc[i][j][e]; 478 } 479 } 480 } 481#endif 482} 483// S23 (STRATA_PF_HCDOWN=1): the hyper-connection read's down (N nd = 320) and inject (N ni = 4) projections of xn16 484// (BF16, T x 10240, token stride ldx: 10240 + 64 avoids the 4 KB-multiple stride) as ONE GEMM - weight rows 485// [w_down; w_inject] read from the two tensors, the epilogue splitting the columns into lo (T x nd) and inj (T x ni), 486// so xn16 is read once. 128 x 128 blocks, the BK 64 single-buffer scheme of kernel_bk64. s23/hcdown_probe.hip, 487// 16K tokens: 3.60 / 3.77 ms (hipBLASLt in the engine: down 6.78 + inject 1.81). FP32 accumulation in another k 488// order than hipBLASLt: rounding-level. 489typedef __bf16 hb16 __attribute__((ext_vector_type(16))); 490__device__ __forceinline__ hb16 bfrag(const uint16_t* p) { 491 const uint4 a = *reinterpret_cast<const uint4*>(p), b = *reinterpret_cast<const uint4*>(p + 8); 492 const uint32_t w[8] = {a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w}; 493 return __builtin_bit_cast(hb16, w); 494} 495__global__ void __launch_bounds__(256) kernel_hcdown(const uint16_t* __restrict__ X, int ldx, 496 const uint16_t* __restrict__ Wd, const uint16_t* __restrict__ Wi, 497 int nd, int ni, float* __restrict__ lo, float* __restrict__ inj, 498 int M, int K) { 499#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || defined(__gfx1151__) 500 constexpr int BN = 128, WN = 32, TN = 2, BK64 = 64, LDK64 = BK64 + 8; 501 __shared__ __align__(16) uint16_t sA[BM][LDK64]; 502 __shared__ __align__(16) uint16_t sB[BN][LDK64]; 503 const int N = nd + ni; 504 const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4; 505 const int wm = wave & 1, wn = wave >> 1; 506 const int num_m = (M + BM - 1) / BM, num_n = (N + BN - 1) / BN, b = blockIdx.x; 507 const int group = b / (GM * num_n), first_m = group * GM, gsize = min(GM, num_m - first_m); 508 const int m0 = (first_m + (b % (GM * num_n)) % gsize) * BM, n0 = ((b % (GM * num_n)) / gsize) * BN; 509 const int sr = tid >> 3, sq = tid & 7; 510 auto ldA = [&](int k0, int r) -> uint4 { 511 return *reinterpret_cast<const uint4*>(X + (size_t) min(m0 + r, M - 1) * ldx + k0 + 8 * sq); 512 }; 513 auto ldB = [&](int k0, int r) -> uint4 { 514 const int n = min(n0 + r, N - 1); 515 const uint16_t* row = n < nd ? Wd + (size_t) n * K : Wi + (size_t) (n - nd) * K; 516 return *reinterpret_cast<const uint4*>(row + k0 + 8 * sq); 517 }; 518 uint4 ra0, ra1, ra2, ra3, rb0, rb1, rb2, rb3; 519 auto load = [&](int k0) { 520 ra0 = ldA(k0, sr); ra1 = ldA(k0, sr + 32); ra2 = ldA(k0, sr + 64); ra3 = ldA(k0, sr + 96); 521 rb0 = ldB(k0, sr); rb1 = ldB(k0, sr + 32); rb2 = ldB(k0, sr + 64); rb3 = ldB(k0, sr + 96); 522 }; 523 auto store = [&]() { 524 *reinterpret_cast<uint4*>(&sA[sr][8 * sq]) = ra0; *reinterpret_cast<uint4*>(&sA[sr + 32][8 * sq]) = ra1; 525 *reinterpret_cast<uint4*>(&sA[sr + 64][8 * sq]) = ra2; *reinterpret_cast<uint4*>(&sA[sr + 96][8 * sq]) = ra3; 526 *reinterpret_cast<uint4*>(&sB[sr][8 * sq]) = rb0; *reinterpret_cast<uint4*>(&sB[sr + 32][8 * sq]) = rb1; 527 *reinterpret_cast<uint4*>(&sB[sr + 64][8 * sq]) = rb2; *reinterpret_cast<uint4*>(&sB[sr + 96][8 * sq]) = rb3; 528 }; 529 f8 acc[4][TN]; 530#pragma unroll 531 for (int i = 0; i < 4; ++i) 532#pragma unroll 533 for (int j = 0; j < TN; ++j) acc[i][j] = f8{0, 0, 0, 0, 0, 0, 0, 0}; 534 load(0); 535 store(); 536 __syncthreads(); 537 const int ar = 64 * wm + l16, br = WN * wn + l16; 538 for (int k0 = 0; k0 < K; k0 += BK64) { 539 const bool more = k0 + BK64 < K; 540 if (more) load(k0 + BK64); 541#pragma unroll 542 for (int ks = 0; ks < BK64; ks += 16) { 543 const hb16 a0 = bfrag(&sA[ar][ks]), a1 = bfrag(&sA[ar + 16][ks]), a2 = bfrag(&sA[ar + 32][ks]), 544 a3 = bfrag(&sA[ar + 48][ks]); 545 const hb16 b0 = bfrag(&sB[br][ks]), b1 = bfrag(&sB[br + 16][ks]); 546 acc[0][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a0, b0, acc[0][0]); 547 acc[1][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a1, b0, acc[1][0]); 548 acc[2][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a2, b0, acc[2][0]); 549 acc[3][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a3, b0, acc[3][0]); 550 acc[0][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a0, b1, acc[0][1]); 551 acc[1][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a1, b1, acc[1][1]); 552 acc[2][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a2, b1, acc[2][1]); 553 acc[3][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a3, b1, acc[3][1]); 554 } 555 if (more) { 556 __syncthreads(); 557 store(); 558 __syncthreads(); 559 } 560 } 561#pragma unroll 562 for (int i = 0; i < 4; ++i) 563#pragma unroll 564 for (int j = 0; j < TN; ++j) { 565 const int n = n0 + WN * wn + 16 * j + l16; 566 if (n >= N) continue; 567#pragma unroll 568 for (int e = 0; e < 8; ++e) { 569 const int m = m0 + 64 * wm + 16 * i + 2 * e + hi; 570 if (m < M) { 571 if (n < nd) lo[(size_t) m * nd + n] = acc[i][j][e]; 572 else inj[(size_t) m * ni + (n - nd)] = acc[i][j][e]; 573 } 574 } 575 } 576#endif 577} 578 579// S23/S (STRATA_HCD_EXACT=1): the HC down projection (N 320, K 10240, BF16 in, FP32 out) bitwise equal to hipBLASLt 580// solution 1176 / 1177 (MT32x96 / MT96x96, StaggerU 32, stride 256 B): every output is the k tiles of 32 accumulated in 581// order, 16 per WMMA, starting at k tile 4 * (token tile of 96 % 32) and wrapping (hcd_bit / hcd_perm probes: 0 of 582// 5.2 M outputs differ at T 16384). 96 x 160 blocks (two per token tile), 6 waves of 32 x 80. 583typedef uint32_t hx_u4 __attribute__((ext_vector_type(4))); 584constexpr int HX_N = 320, HX_BN = 160, HX_BM = 96, HX_BK = 64, HX_LDK = HX_BK + 8, HX_NT = 192, HX_K = 10240; 585__device__ __forceinline__ hb16 hx_frag(const uint16_t* p) { 586 const hx_u4 a = *reinterpret_cast<const hx_u4*>(p), b = *reinterpret_cast<const hx_u4*>(p + 8); 587 const uint32_t w[8] = {a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w}; 588 return __builtin_bit_cast(hb16, w); 589} 590__global__ void __launch_bounds__(HX_NT) kernel_hcd_exact(const uint16_t* __restrict__ X, int ldx, 591 const uint16_t* __restrict__ W, float* __restrict__ Y, int M) { 592#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || defined(__gfx1151__) 593 constexpr int K = HX_K, BN = HX_BN, BMT = HX_BM, BK = HX_BK, NT = HX_NT; 594 __shared__ __align__(16) uint16_t sA[BMT][HX_LDK]; 595 __shared__ __align__(16) uint16_t sB[BN][HX_LDK]; 596 const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4; 597 const int wm = wave % 3, wn = wave / 3, nbase = BN * (blockIdx.x & 1); 598 const int tt = blockIdx.x >> 1, m0 = tt * BMT; 599 constexpr int nslab = K / BK; 600 const int rot = (2 * (tt & 31)) % nslab; // 4 k tiles of 32 = 2 slabs of 64 601 hx_u4 ra0, ra1, ra2, ra3, rb0, rb1, rb2, rb3, rb4, rb5, rb6; 602#define HX_LDA(v, i) { const int idx = tid + NT * (i), r = idx >> 3, q = idx & 7; v = *reinterpret_cast<const hx_u4*>(X + (size_t) min(m0 + r, M - 1) * ldx + k0 + 8 * q); } 603#define HX_LDB(v, i) { const int idx = min(tid + NT * (i), BN * 8 - 1), r = idx >> 3, q = idx & 7; v = *reinterpret_cast<const hx_u4*>(W + (size_t) (nbase + r) * K + k0 + 8 * q); } 604#define HX_STA(v, i) { const int idx = tid + NT * (i); *reinterpret_cast<hx_u4*>(&sA[idx >> 3][8 * (idx & 7)]) = v; } 605#define HX_STB(v, i) { const int idx = tid + NT * (i); if (idx < BN * 8) *reinterpret_cast<hx_u4*>(&sB[idx >> 3][8 * (idx & 7)]) = v; } 606#define HX_LOAD(k0v) { const int k0 = (k0v); HX_LDA(ra0, 0) HX_LDA(ra1, 1) HX_LDA(ra2, 2) HX_LDA(ra3, 3) HX_LDB(rb0, 0) HX_LDB(rb1, 1) HX_LDB(rb2, 2) HX_LDB(rb3, 3) HX_LDB(rb4, 4) HX_LDB(rb5, 5) HX_LDB(rb6, 6) } 607#define HX_STORE() { HX_STA(ra0, 0) HX_STA(ra1, 1) HX_STA(ra2, 2) HX_STA(ra3, 3) HX_STB(rb0, 0) HX_STB(rb1, 1) HX_STB(rb2, 2) HX_STB(rb3, 3) HX_STB(rb4, 4) HX_STB(rb5, 5) HX_STB(rb6, 6) } 608 f8 acc[2][5]; 609#pragma unroll 610 for (int i = 0; i < 2; ++i) 611#pragma unroll 612 for (int j = 0; j < 5; ++j) acc[i][j] = f8{0, 0, 0, 0, 0, 0, 0, 0}; 613 HX_LOAD(rot * BK); 614 HX_STORE(); 615 __syncthreads(); 616 const int ar = 32 * wm + l16, br = 80 * wn + l16; 617 for (int s = 0; s < nslab; ++s) { 618 const bool more = s + 1 < nslab; 619 if (more) HX_LOAD(((s + 1 + rot) % nslab) * BK); 620#pragma unroll 621 for (int ks = 0; ks < BK; ks += 16) { 622 const hb16 a0 = hx_frag(&sA[ar][ks]), a1 = hx_frag(&sA[ar + 16][ks]); 623 const hb16 b0 = hx_frag(&sB[br][ks]), b1 = hx_frag(&sB[br + 16][ks]), b2 = hx_frag(&sB[br + 32][ks]), 624 b3 = hx_frag(&sB[br + 48][ks]), b4 = hx_frag(&sB[br + 64][ks]); 625#define HX_W2(j, bj) acc[0][j] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a0, bj, acc[0][j]); acc[1][j] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a1, bj, acc[1][j]); 626 HX_W2(0, b0) HX_W2(1, b1) HX_W2(2, b2) HX_W2(3, b3) HX_W2(4, b4) 627#undef HX_W2 628 } 629 if (more) { 630 __syncthreads(); 631 HX_STORE(); 632 __syncthreads(); 633 } 634 } 635#undef HX_LDA 636#undef HX_LDB 637#undef HX_STA 638#undef HX_STB 639#undef HX_LOAD 640#undef HX_STORE 641#pragma unroll 642 for (int i = 0; i < 2; ++i) 643#pragma unroll 644 for (int j = 0; j < 5; ++j) { 645 const int n = nbase + 80 * wn + 16 * j + l16; 646#pragma unroll 647 for (int e = 0; e < 8; ++e) { 648 const int m = m0 + 32 * wm + 16 * i + 2 * e + hi; 649 if (m < M) Y[(size_t) m * HX_N + n] = acc[i][j][e]; 650 } 651 } 652#endif 653} 654} // namespace pfg 655 656bool strata_pf_hcdown_bf16(const uint16_t* X, int64_t ldx, const uint16_t* Wd, const uint16_t* Wi, int64_t nd, 657 int64_t ni, float* lo, float* inj, int64_t T, int64_t K, void* stream) { 658 if (!X || !Wd || !Wi || !lo || !inj || T < 64 || nd < 1 || ni < 0 || K % 64 != 0 || ldx < K || ldx % 8 != 0) return false; 659 if (T > (1LL << 30) || K > (1LL << 30) || ldx > (1LL << 30)) return false; 660 if (!strata_wmma_gfx11_device()) return false; 661 const int64_t mt = (T + pfg::BM - 1) / pfg::BM; 662 const unsigned grid = (unsigned) (mt * ((nd + ni + 127) / 128)); 663 pfg::kernel_hcdown<<<grid, 256, 0, static_cast<hipStream_t>(stream)>>>(X, (int) ldx, Wd, Wi, (int) nd, (int) ni, 664 lo, inj, (int) T, (int) K); 665 return hipGetLastError() == hipSuccess; 666} 667 668bool strata_pf_hcdown_exact_bf16(const uint16_t* X, int64_t ldx, const uint16_t* W, float* Y, int64_t T, int64_t N, 669 int64_t K, void* stream) { 670 if (!X || !W || !Y || T < 1 || N != pfg::HX_N || K != pfg::HX_K || ldx < K || ldx % 8 != 0 || ldx > (1LL << 30) || 671 T > (1LL << 30)) 672 return false; 673 if (!strata_wmma_gfx11_device()) return false; 674 const unsigned grid = (unsigned) (2 * ((T + pfg::HX_BM - 1) / pfg::HX_BM)); 675 pfg::kernel_hcd_exact<<<grid, pfg::HX_NT, 0, static_cast<hipStream_t>(stream)>>>(X, (int) ldx, W, Y, (int) T); 676 return hipGetLastError() == hipSuccess; 677} 678 679bool 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, 680 float beta, void* stream) { 681 return strata_pf_gemm_f16_ld(X, K, W, K, Y, T, N, K, ldy, beta, stream); 682} 683 684bool strata_pf_gemm_f16_ld(const uint16_t* X, int64_t ldx, const uint16_t* W, int64_t ldw, float* Y, int64_t T, 685 int64_t N, int64_t K, int64_t ldy, float beta, void* stream) { 686 if (!X || !W || !Y || T < 64 || N < 512 || K % pfg::BK != 0 || K < pfg::BK) return false; 687 if (beta != 0.0f && beta != 1.0f) return false; 688 if (ldy <= 0) ldy = N; 689 if (ldx < K || ldw < K || ldx % 8 != 0 || ldw % 8 != 0 || ldx > (1LL << 30) || ldw > (1LL << 30)) return false; 690 if (ldy < N || T > (1LL << 30) || N > (1LL << 30) || K > (1LL << 30)) return false; 691 if (!strata_wmma_gfx11_device()) return false; 692 hipStream_t s = static_cast<hipStream_t>(stream); 693 const int acc = beta == 1.0f ? 1 : 0; 694 const int64_t mt = (T + pfg::BM - 1) / pfg::BM; 695 // STRATA_PF_BK64=0: the BK 32 kernel for the wide shapes too (the A/B; the same bits) 696 static const bool bk64 = [] { const char* v = std::getenv("STRATA_PF_BK64"); return !v || v[0] != '0'; }(); 697 if (N >= 1024 && bk64 && K % 64 == 0) { 698 const unsigned grid = (unsigned) (mt * ((N + 255) / 256)); 699 pfg::kernel_bk64<<<grid, 256, 0, s>>>((const _Float16*) X, (const _Float16*) W, Y, (int) T, (int) N, (int) K, 700 (int) ldy, acc, (int) ldx, (int) ldw); 701 } else if (N >= 1024) { 702 const unsigned grid = (unsigned) (mt * ((N + 255) / 256)); 703 pfg::kernel<256, 64><<<grid, 256, 0, s>>>((const _Float16*) X, (const _Float16*) W, Y, (int) T, (int) N, (int) K, 704 (int) ldy, acc, (int) ldx, (int) ldw); 705 } else { 706 const unsigned grid = (unsigned) (mt * ((N + 127) / 128)); 707 pfg::kernel<128, 32><<<grid, 256, 0, s>>>((const _Float16*) X, (const _Float16*) W, Y, (int) T, (int) N, (int) K, 708 (int) ldy, acc, (int) ldx, (int) ldw); 709 } 710 return hipGetLastError() == hipSuccess; 711} 712 713#else 714// Non-HIP / Non-GFX11 compilation fallback 715bool strata_pf_gemm_f16(const uint16_t*, const uint16_t*, float*, int64_t, int64_t, int64_t, int64_t, float, void*) { 716 return false; 717} 718bool strata_pf_gemm_f16_ld(const uint16_t*, int64_t, const uint16_t*, int64_t, float*, int64_t, int64_t, int64_t, 719 int64_t, float, void*) { 720 return false; 721} 722// No padded product in this build (no gfx11 target): prefill.cpp does not pad an activation's row stride. 723bool strata_pf_padded_gemm_available() { return false; } 724bool strata_pf_hcdown_bf16(const uint16_t*, int64_t, const uint16_t*, const uint16_t*, int64_t, int64_t, float*, float*, 725 int64_t, int64_t, void*) { 726 return false; 727} 728bool strata_pf_hcdown_exact_bf16(const uint16_t*, int64_t, const uint16_t*, float*, int64_t, int64_t, int64_t, void*) { 729 return false; 730} 731bool strata_wmma_gemm_f16(const uint16_t*, const uint16_t*, float*, 732 int64_t, int64_t, int64_t, int64_t, float, 733 void*) { 734 return false; 735} 736 737bool strata_wmma_gemm_bf16(const uint16_t*, const uint16_t*, float*, 738 int64_t, int64_t, int64_t, int64_t, float, 739 void*) { 740 return false; 741} 742#endif