okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Strata. The prompt sets of ds4_iq.cu on the gfx12 (RDNA4, R9700 = gfx1201) matrix cores: v_wmma_f32_16x16x16_f16, F16 operands, 2// F32 accumulation. Included by ds4_iq.cu inside its anonymous namespace (it uses its Ty, decode<>, load_tab<>, swiglu, SetArgs). 3// 4// The idea is the CUDA prompt tiles' (set_h16ws_kernel on f-dsv41-cuk-int2): a block-tile of the expert's rows x weight rows, K in 5// steps of 64; each i-quant block is decoded ONCE into shared memory as F16 (code * the sub-block's scale: the product is exact up 6// to the F16 rounding of the scale and the product) and then shared by every row of the tile; x as F16 rows next to it, both 7// double-buffered, one barrier a step. The F32 FMA tile of today (set_kernel) decodes the weights once per 64 rows and does 8// scalar FMAs. Default ON for gfx12, STRATA_DS4_HIPK=0 switches it off (gfx12 only, the types below, experts of more than 16 rows). 9#pragma once 10#if defined(STRATA_USE_HIP) 11 12namespace hipk { 13// test hooks: force_mode -1 = the default rules, 0 = never, 1 = on wherever the device has the WMMA tiles (gfx11 / gfx12); launches counts the tiles taken 14inline std::atomic<int>& force_mode() { static std::atomic<int> f{-1}; return f; } 15inline std::atomic<long long>& launches() { static std::atomic<long long> n{0}; return n; } 16constexpr int kStH = 72; // halves a shared row: 64 + 8 (the pad spreads the banks; rows stay 16-byte aligned) 17using hv2 = _Float16 __attribute__((ext_vector_type(2))); 18using hv8 = _Float16 __attribute__((ext_vector_type(8))); 19using fv8 = float __attribute__((ext_vector_type(8))); 20 21template <int T> 22constexpr bool kTypes = T == kIQ2_XXS || T == kIQ2_XS || T == kIQ2_S || T == kIQ3_XXS || T == kIQ3_S || T == kIQ4_XS || 23 T == kIQ1_S || T == kIQ1_M || T == kIQ4_NL; 24 25__device__ __forceinline__ fv8 wmma16(hv8 a, hv8 b, fv8 c) { 26#if defined(__gfx1200__) || defined(__gfx1201__) 27 return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a, b, c); 28#else 29 return c; 30#endif 31} 32using hv16 = _Float16 __attribute__((ext_vector_type(16))); 33// RDNA3 / RDNA3.5 (gfx11): the same v_wmma_f32_16x16x16_f16 on wave32, but a lane holds ALL 16 k of its row / column (the two 34// half-waves the same values) and the D rows a lane holds are 2 q + hi (gfx12: 8 hi + q). DS4_HIPK_G11 is set in the device pass only. 35#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || \ 36 defined(__gfx1151__) || defined(__gfx1152__) 37#define DS4_HIPK_G11 1 38#endif 39__device__ __forceinline__ fv8 wmma16g11(hv16 a, hv16 b, fv8 c) { 40#if defined(DS4_HIPK_G11) 41 return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c); 42#else 43 return c; 44#endif 45} 46__device__ __forceinline__ uint32_t as_u32(hv2 v) { return __builtin_bit_cast(uint32_t, v); } 47__device__ __forceinline__ hv2 as_hv2(uint32_t v) { return __builtin_bit_cast(hv2, v); } 48// 4 int8 codes (a word) -> 4 halves times the scale: 1024 + (code + 128) is exact in a half's mantissa, less 1152 49__device__ __forceinline__ uint2 codes4(int w, hv2 sc) { 50 const uint32_t u = (uint32_t) w ^ 0x80808080u; 51 const uint32_t lo = __byte_perm(u, 0x64646464u, 0x5140), hi = __byte_perm(u, 0x64646464u, 0x5342); 52 const hv2 m = {(_Float16) 1152.0f, (_Float16) 1152.0f}; 53 const hv2 a = (as_hv2(lo) - m) * sc, b = (as_hv2(hi) - m) * sc; 54 return make_uint2(as_u32(a), as_u32(b)); 55} 56 57// BM rows x BN weight rows (a matrix: gate and up: two) per block, 8 waves WM x WN, each wave (BM/WM) x (BN/WN) 58template <int T, bool GU, int BM, int BN, int WM, int WN> 59__global__ void __launch_bounds__(256) set_kernel(const SetArgs a, int dbg) { 60 static_assert(WM * WN == 8, "8 waves"); 61 constexpr int NM = GU ? 2 : 1, MT = BM / WM / 16, NT = BN / WN / 16; 62 static_assert(MT >= 1 && NT >= 1 && MT * WM * 16 == BM && NT * WN * 16 == BN, "wave tiling"); 63 __shared__ uint32_t tab[Ty<T>::TAB > 0 ? Ty<T>::TAB : 1]; 64 __shared__ int srow[BM]; 65 __shared__ __align__(16) _Float16 xs[2][BM][kStH]; 66 __shared__ __align__(16) _Float16 ws[2][NM][BN][kStH]; 67 const int e = blockIdx.z; 68 const int lo = a.bounds[e] - a.row0, hi_r = a.bounds[e + 1] - a.row0; 69 const int r0 = lo + blockIdx.x * BM; 70 if (r0 >= hi_r || hi_r - lo <= a.skip_le) return; // (uniform) 71 load_tab<T>(tab); 72 const int nr = min(BM, hi_r - r0); 73 const int c0 = blockIdx.y * BN; 74 const uint8_t* blob = a.wbase + (size_t) e * a.stride; 75 const int K = GU ? a.n_embd : a.n_ff; 76 const size_t rb = row_bytes_t<T>(K); 77 const int tid = (int) threadIdx.x, warp = tid >> 5, lane = tid & 31, l16 = lane & 15, hi = lane >> 4; 78 if (tid < BM) srow[tid] = GU ? a.idx[r0 + min(tid, nr - 1)] : r0 + min(tid, nr - 1); 79 __syncthreads(); 80 const int nst = K / 64; 81 82 // a step's operands are fetched and decoded into registers (the global loads and the codebook's lookups) while the matrix 83 // cores work on the step before, and written to shared memory after them 84 constexpr int NI = (NM * BN * 2 + 255) / 256; // weight items a thread 85 constexpr int NX = GU ? (BM * 16 + 255) / 256 : (BM * 8 + 255) / 256; // row chunks a thread 86 struct Regs { 87 int q[NI][8]; 88 _Float16 sc0[NI], sc1[NI]; 89 float4 xv[GU ? NX : 1]; 90 uint4 xu[GU ? 1 : NX]; 91 }; 92 auto fetch = [&](int s, Regs& R) { 93#pragma unroll 94 for (int ii = 0; ii < NI; ++ii) { 95 const int it = tid + ii * 256; 96 if (it < NM * BN * 2) { 97 const int kbl = it & 1, col = (it >> 1) % BN, mat = it / (2 * BN); 98 Sub sb; 99 if (dbg & 2) { for (int w = 0; w < 8; ++w) sb.q[w] = tid + w; sb.d = 1.0f; sb.s0 = sb.s1 = 1; } else 100 decode<T>(blob + a.tr[GU ? mat : 2] + (size_t) (c0 + col) * rb, s * 2 + kbl, tab, sb); 101#pragma unroll 102 for (int w = 0; w < 8; ++w) R.q[ii][w] = sb.q[w]; 103 R.sc0[ii] = (_Float16) (sb.d * (float) sb.s0); 104 R.sc1[ii] = (_Float16) (sb.d * (float) sb.s1); 105 } 106 } 107#pragma unroll 108 for (int xi = 0; xi < NX; ++xi) { 109 const int c = tid + xi * 256; 110 if constexpr (GU) { 111 if (c < BM * 16) { 112 const int row = c >> 4, ch = c & 15; 113 R.xv[xi] = *(const float4*) (a.xf + (size_t) srow[row] * a.ldx + (size_t) s * 64 + ch * 4); 114 } 115 } else { 116 if (c < BM * 8) { 117 const int row = c >> 3, ch = c & 7; 118 R.xu[xi] = *(const uint4*) (a.xh + (size_t) srow[row] * a.ldx + (size_t) s * 64 + ch * 8); 119 } 120 } 121 } 122 }; 123 auto store = [&](const Regs& R, int b) { 124#pragma unroll 125 for (int ii = 0; ii < NI; ++ii) { 126 const int it = tid + ii * 256; 127 if (it < NM * BN * 2) { 128 const int kbl = it & 1, col = (it >> 1) % BN, mat = it / (2 * BN); 129 const hv2 h0 = {R.sc0[ii], R.sc0[ii]}, h1 = {R.sc1[ii], R.sc1[ii]}; 130 uint2* dst = (uint2*) &ws[b][mat][col][kbl * 32]; // (values 4 w .. 4 w + 3 of the half at dst[w], 16 + 4 w .. at dst[4 + w]) 131#pragma unroll 132 for (int w = 0; w < 4; ++w) { 133 dst[w] = codes4(R.q[ii][w], h0); 134 dst[4 + w] = codes4(R.q[ii][4 + w], h1); 135 } 136 } 137 } 138#pragma unroll 139 for (int xi = 0; xi < NX; ++xi) { 140 const int c = tid + xi * 256; 141 if constexpr (GU) { 142 if (c < BM * 16) { 143 const int row = c >> 4, ch = c & 15; 144 const float4 v = R.xv[xi]; 145 const hv2 p = {(_Float16) v.x, (_Float16) v.y}, q = {(_Float16) v.z, (_Float16) v.w}; 146 *(uint2*) &xs[b][row][ch * 4] = make_uint2(as_u32(p), as_u32(q)); 147 } 148 } else { 149 if (c < BM * 8) { 150 const int row = c >> 3, ch = c & 7; 151 *(uint4*) &xs[b][row][ch * 8] = R.xu[xi]; 152 } 153 } 154 } 155 }; 156 157 const int wm = warp / WN, wn = warp % WN; 158 fv8 acc[NM][MT][NT]; 159#pragma unroll 160 for (int m = 0; m < NM; ++m) 161#pragma unroll 162 for (int i = 0; i < MT; ++i) 163#pragma unroll 164 for (int j = 0; j < NT; ++j) acc[m][i][j] = fv8{0, 0, 0, 0, 0, 0, 0, 0}; 165 166 // read-ahead: the first and last byte of the weights' block and a word of the rows' line of a step PF steps on, summed into a 167 // value that is never used (the loads only warm the caches; their latency is not waited for until the end) 168 int touched = 0; 169 auto touch = [&](int s) { 170 if (dbg & 4) return; 171#pragma unroll 172 for (int ii = 0; ii < NI; ++ii) { 173 const int it = tid + ii * 256; 174 if (it < NM * BN * 2) { 175 const int col = (it >> 1) % BN, mat = it / (2 * BN); 176 const uint8_t* p = blob + a.tr[GU ? mat : 2] + (size_t) (c0 + col) * rb + (size_t) ((s * 2) >> 3) * Ty<T>::B; 177 touched += p[0] + p[Ty<T>::B - 1]; 178 } 179 } 180 if constexpr (GU) { 181 for (int c = tid; c < BM * 2; c += 256) touched += (int) a.xf[(size_t) srow[c >> 1] * a.ldx + (size_t) s * 64 + (c & 1) * 32]; 182 } else { 183 for (int c = tid; c < BM * 2; c += 256) touched += (int) a.xh[(size_t) srow[c >> 1] * a.ldx + (size_t) s * 64 + (c & 1) * 32]; 184 } 185 }; 186 constexpr int PF = 4; 187 for (int s = 1; s <= PF && s < nst; ++s) touch(s + 1 < nst ? s + 1 : s); 188 { 189 Regs R0; 190 fetch(0, R0); 191 store(R0, 0); 192 } 193 __syncthreads(); 194 for (int s = 0; s < nst; ++s) { 195 const int b = s & 1; 196 Regs R; 197 if (s + 1 < nst) fetch(s + 1, R); 198 if (s + 1 + PF < nst) touch(s + 1 + PF); 199#pragma unroll 200 for (int kk = 0; kk < 4; ++kk) { 201 hv8 af[MT]; 202#pragma unroll 203 for (int i = 0; i < MT; ++i) af[i] = *(const hv8*) &xs[b][wm * (16 * MT) + i * 16 + l16][kk * 16 + hi * 8]; 204#pragma unroll 205 for (int m = 0; m < NM; ++m) 206#pragma unroll 207 for (int j = 0; j < NT; ++j) { 208 const hv8 bf = *(const hv8*) &ws[b][m][wn * (16 * NT) + j * 16 + l16][kk * 16 + hi * 8]; 209#pragma unroll 210 for (int i = 0; i < MT; ++i) 211 if (!(dbg & 1) && wm * (16 * MT) + i * 16 < nr) acc[m][i][j] = wmma16(af[i], bf, acc[m][i][j]); // (a tile past the rows: skipped, uniform) 212 } 213 } 214 if (s + 1 < nst) store(R, b ^ 1); 215 __syncthreads(); 216 } 217 if (touched == 0x7fffffff && tid == 12345) a.y[0] = 0.0f; 218 // D[row = 8 hi + i][col = l16] of every tile 219#pragma unroll 220 for (int i = 0; i < MT; ++i) 221#pragma unroll 222 for (int q = 0; q < 8; ++q) { 223 const int row = wm * (16 * MT) + i * 16 + 8 * hi + q; 224 if (row >= nr) continue; 225 const size_t lr = (size_t) (r0 + row); 226#pragma unroll 227 for (int j = 0; j < NT; ++j) { 228 const int col = c0 + wn * (16 * NT) + j * 16 + l16; 229 if (GU) a.h[lr * a.ldo + col] = __half_as_ushort(__float2half_rn(swiglu(acc[0][i][j][q], acc[1][i][j][q], a.limit))); 230 else a.y[lr * a.ldo + col] = acc[0][i][j][q]; 231 } 232 } 233} 234 235#include "ds4_iq_hip_r.inc" 236 237inline bool ok_device() { // gfx12 unless STRATA_DS4_HIPK=0 238 static std::atomic<int> state[16]; 239 static std::atomic<int> capable[16]; // 1: gfx11 / gfx12 240 int dev = 0; 241 cudaGetDevice(&dev); 242 if (dev < 0 || dev >= 16) return false; 243 if (const int f = force_mode().load(std::memory_order_relaxed); f >= 0) { 244 if (f == 0) return false; 245 int c = capable[dev].load(std::memory_order_relaxed); 246 if (c == 0) { 247 cudaDeviceProp pr{}; 248 const bool g = cudaGetDeviceProperties(&pr, dev) == cudaSuccess && (std::strncmp(pr.gcnArchName, "gfx12", 5) == 0 || std::strncmp(pr.gcnArchName, "gfx11", 5) == 0); 249 c = g ? 1 : 2; 250 capable[dev].store(c, std::memory_order_relaxed); 251 } 252 return c == 1; 253 } 254 int v = state[dev].load(std::memory_order_relaxed); 255 if (v == 0) { 256 cudaDeviceProp prop{}; 257 const bool ok = cudaGetDeviceProperties(&prop, dev) == cudaSuccess; 258 const bool gfx12 = ok && std::strncmp(prop.gcnArchName, "gfx12", 5) == 0; 259 const bool gfx11 = ok && std::strncmp(prop.gcnArchName, "gfx11", 5) == 0; 260 const char* e = std::getenv("STRATA_DS4_HIPK"); 261 v = gfx12 && !(e != nullptr && e[0] == '0') ? 1 : 2; // (on by default on gfx12; STRATA_DS4_HIPK=0 off) 262 // gfx11: the register-weights tiles only. On by default on gfx1151 (Strix Halo: prompts +22..29% at the same KL, measured), opt-in 263 // (STRATA_DS4_HIPK=1) on the other gfx11 parts, which were not measured; STRATA_DS4_HIPK=0 turns it off everywhere 264 if (gfx11) { 265 const bool halo = std::strncmp(prop.gcnArchName, "gfx1151", 7) == 0; 266 v = (e != nullptr ? e[0] == '1' : halo) ? 3 : 2; 267 } 268 state[dev].store(v, std::memory_order_relaxed); 269 } 270 return v == 1 || v == 3; 271} 272inline bool is_g11() { 273 int dev = 0; 274 cudaGetDevice(&dev); 275 cudaDeviceProp prop{}; 276 static std::atomic<int> g[16]; 277 if (dev < 0 || dev >= 16) return false; 278 int v = g[dev].load(std::memory_order_relaxed); 279 if (v == 0) { 280 v = cudaGetDeviceProperties(&prop, dev) == cudaSuccess && std::strncmp(prop.gcnArchName, "gfx11", 5) == 0 ? 1 : 2; 281 g[dev].store(v, std::memory_order_relaxed); 282 } 283 return v == 1; 284} 285 286template <int T, bool GU, int BM, int BN, int WM, int WN> 287inline bool run(const SetArgs& a, cudaStream_t s) { 288 const int N = GU ? a.n_ff : a.n_embd; 289 const dim3 grid((unsigned) ((a.max_rows + BM - 1) / BM), (unsigned) (N / BN), (unsigned) a.n_exp); 290 static const int dbg = std::getenv("HIPK_DBG") ? std::atoi(std::getenv("HIPK_DBG")) : 0; 291 set_kernel<T, GU, BM, BN, WM, WN><<<grid, 256, 0, s>>>(a, dbg); 292 launches().fetch_add(1, std::memory_order_relaxed); 293 return cudaPeekAtLastError() == cudaSuccess; 294} 295// false: not taken (the caller's tiles do it) 296template <int T, bool GU> 297inline bool launch(const SetArgs& a, cudaStream_t s) { 298 if constexpr (!kTypes<T>) { 299 return false; 300 } else { 301 if (!ok_device() || a.max_rows <= 16) return false; 302 const int K = GU ? a.n_embd : a.n_ff, N = GU ? a.n_ff : a.n_embd; 303 if (K % 256 != 0 || N % 64 != 0 || a.ldx % 8 != 0 || a.ldo % 2 != 0) return false; 304 if (((uintptr_t) (GU ? (const void*) a.xf : (const void*) a.xh) & 15) != 0) return false; 305 static const int var = std::getenv("STRATA_DS4_HIPK") ? std::atoi(std::getenv("STRATA_DS4_HIPK")) : 0; 306 if (is_g11() && var == 2) return false; // (the shared-memory weights kernel is gfx12 only) 307 if (var != 2) { // 2: the shared-memory weights kernel 308 static const int cfg = std::getenv("HIPK_CFG") ? std::atoi(std::getenv("HIPK_CFG")) : 0; 309 if (cfg == 1 && a.max_rows <= 32) return runr<T, GU, 32, 4, 1>(a, s); 310 if (cfg == 2 && a.max_rows <= 32) return runr<T, GU, 32, 8, 1>(a, s); 311 if (cfg == 3 && a.max_rows <= 32) return runr<T, GU, 32, 2, 2>(a, s); 312 if (cfg == 4 && a.max_rows <= 32) return runr<T, GU, 32, 8, 2>(a, s); 313 if (cfg == 5 && a.max_rows <= 32) return runr<T, GU, 32, 2, 1>(a, s); 314 if (a.max_rows <= 32) return runr<T, GU, 32, 4, 2>(a, s); 315 if (a.max_rows <= 64) return runr<T, GU, 64, 4, 2>(a, s); 316 return runr<T, GU, 128, 8, 1>(a, s); 317 } 318 if (a.max_rows <= 32) return run<T, GU, 32, 64, 2, 4>(a, s); 319 if (a.max_rows <= 64) return run<T, GU, 64, 64, 2, 4>(a, s); 320 if constexpr (GU) return run<T, GU, 128, 32, 4, 2>(a, s); 321 else return run<T, GU, 128, 64, 4, 2>(a, s); 322 } 323} 324} // namespace hipk 325 326#endif