okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/cuda/qsa_pattn.cu - see include/strata/kernels/qsa_pattn.hpp. 2// 3// Every value below is computed by the operation the production path uses, on the same operands, in the same order: 4// - score of (cell, head): each lane l's partial k8[0] * q[8l] + ... + k8[7] * q[8l + 7] with k8[e] = (float) code * sc, 5// the 32 partials summed in warp_sum's pairs (reduce12's tree), times `scale`; 6// - chunk softmax: m = warp_max over fmaxf(s[lane], s[lane + 32]); e = __expf(s - m) (0 for a masked or absent cell); 7// l = warp_sum(e[lane] + e[lane + 32]); 8// - chunk value: acc[h] = fmaf(e[c][h], v, acc[h]) over the chunk's cells in ascending order, masked cells skipped, 9// v = (float) code * sc; 10// - merge (attn_merge_kernel): M = fmaxf over the chunks' m; for each chunk in order with m != -FLT_MAX: 11// w = __expf(m - M), L = fmaf(l, w, L), acc = fmaf(acc_c, w, acc); out = L > 0 ? acc / L : 0. 12// qsa_pattn_parity compares the outputs with qsa_decode_attn_batch's byte for byte on synthetic selections. 13// Only the default variant of the contributor's PR (#3943) is kept: ps_score_x_kernel + ps_value_d4_kernel. 14#include "strata/kernels/qsa_pattn.hpp" 15#include "strata/kernels/kv_q8.hpp" 16#include "strata/core/geometry.hpp" 17 18#include <cuda_runtime.h> 19 20#include <cfloat> 21#include <cstdio> 22#include <cstdlib> 23#include <cstring> 24 25#if !defined(__HIPCC__) && defined(STRATA_EXPERIMENTAL_SM60) 26#define STRATA_PATTN_BUILT 1 // the CUDA build with STRATA_EXPERIMENTAL_SM60 only (not HIP, not Metal) 27#include <cuda_fp16.h> 28#else 29#define STRATA_PATTN_BUILT 0 30#endif 31 32namespace strata::kernels { 33 34// STRATA_PATTN_SM60=1 turns it on (a tester switch, read once). In a build without the sm_6x kernels it says so and stays off. 35int pattn_mode() { 36 static const int m = [] { 37 const char* v = std::getenv("STRATA_PATTN_SM60"); 38 const int x = v != nullptr && *v != '\0' ? std::atoi(v) : 0; 39 if (x == 0) return 0; 40#if STRATA_PATTN_BUILT 41 std::fprintf(stderr, "strata pattn: STRATA_PATTN_SM60=%d: the prompt path's QSA attention on the sm_6x score / " 42 "value kernels (bit for bit qsa_decode_attn_batch; int8 K/V)\n", x); 43 return 1; 44#else 45 std::fprintf(stderr, "strata pattn: STRATA_PATTN_SM60 is set, but this build has no sm_6x kernels " 46 "(CUDA with STRATA_EXPERIMENTAL_SM60 only); the usual attention runs\n"); 47 return 0; 48#endif 49 }(); 50 return m; 51} 52 53#if STRATA_PATTN_BUILT 54namespace { 55 56constexpr int HD = 256; 57constexpr int G = 12; 58static_assert(HD == strata::core::kCompiledGeometry.head_dim && 59 G == strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv, 60 "qsa_pattn is compiled for core/geometry.hpp"); 61constexpr int CHUNK = 64; 62constexpr int THREADS = 256; 63constexpr int WARPS = THREADS / 32; 64constexpr int NSC = HD / KV_Q8_GROUP; // scales per row (4) 65 66__device__ __forceinline__ float warp_sum_x(float v) { 67 v += __shfl_xor_sync(0xffffffffu, v, 16); v += __shfl_xor_sync(0xffffffffu, v, 8); 68 v += __shfl_xor_sync(0xffffffffu, v, 4); v += __shfl_xor_sync(0xffffffffu, v, 2); 69 v += __shfl_xor_sync(0xffffffffu, v, 1); 70 return v; 71} 72__device__ __forceinline__ float warp_max_x(float v) { 73 v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, 16)); v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, 8)); 74 v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, 4)); v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, 2)); 75 v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, 1)); 76 return v; 77} 78 79// pool row of cell `c0 + t` of query selection `ids` (attn_chunk_kernel_pre75's srow), -1 masked / absent 80__device__ __forceinline__ long long cell_row(const QsaAttnPools& p, const int32_t* __restrict__ ids, int c0, int t, 81 int n_here, int n_kv_heads, int kvh, int page_size) { 82 long long r = -1; 83 if (t < n_here) { 84 const int cell = __ldg(ids + c0 + t); 85 const long long page = (long long) __ldg(p.page_table + cell / page_size); 86 if (page >= 0) r = (page * n_kv_heads + kvh) * page_size + (cell % page_size); 87 } 88 return r; 89} 90 91// the chunk's K codes (16-byte loads) and scales (as float) into shared memory; masked / absent cells read row 0 92__device__ __forceinline__ void stage_rows(const int8_t* __restrict__ codes, const uint16_t* __restrict__ scales, 93 const long long* srow, int n_here, int t, int8_t (*sk)[HD], 94 float (*sks)[NSC]) { 95 uint4 kv[4]; 96#pragma unroll 97 for (int j = 0; j < 4; ++j) { 98 const int idx = t + THREADS * j, c = idx >> 4, part = idx & 15; 99 const long long row = srow[c]; 100 const long long rr = (c < n_here && row >= 0) ? row : 0; 101 kv[j] = __ldg(reinterpret_cast<const uint4*>(codes + rr * HD) + part); 102 } 103 float sc; 104 { 105 const int c = t >> 2, g = t & 3; 106 const long long row = srow[c]; 107 const long long rr = (c < n_here && row >= 0) ? row : 0; 108 sc = __half2float(__ushort_as_half(__ldg(scales + rr * NSC + g))); 109 } 110#pragma unroll 111 for (int j = 0; j < 4; ++j) reinterpret_cast<uint4*>(&sk[0][0])[t + THREADS * j] = kv[j]; 112 sks[t >> 2][t & 3] = sc; 113} 114 115constexpr int PS_BLK = G * CHUNK + 2 * CHUNK + 2 * G; // floats per (query, KV head, chunk): e, rows (int64), m, l 116constexpr int NCM = 33; // chunks the value kernel keeps (cap <= 2112) 117 118// --------------------------------------------------------------------------------------------------------------------- 119// ps_score_x_kernel: the score phase two cells at a time with a select-free reduce-scatter. reduce12's selects exist 120// because the two lanes of an exchange send different slots; here every lane keeps the same slots and sends the same 121// slots, and the lane-dependent part is moved into which query head each register slot holds (q is loaded once, so the 122// permutation costs nothing per cell) and which of the two cells is "first": 123// slot i (0..11) of lane L holds head P_L(i) = 6 ((i / 6) ^ b3) + ((i % 6 + 3 b2) % 6) (b_k: bit k of L) 124// first = cell B if b4 else cell A, second = the other one; part1[i] / part2[i] = lane partials of (first / second, 125// head P_L(i)) - the same expression as attn_chunk_kernel_pre75. 126// level 16: r[i] = part1[i] + shfl(part2[i], 16) (partner: b4 flipped, so its second is my first, same P) 127// level 8 : r6[i] = r[i] + shfl(r[i + 6], 8) (P_{L^8}(i) = P_L(i + 6)) 128// level 4 : r3[i] = r6[i] + shfl(r6[i + 3], 4) (P_{L^4}(i) = P_L(i + 3), i < 3) 129// level 2 / 1: reduce24's (heads 6 b3 + 3 b2 + {0, 1, 2}; o0 head +2 b1, o1 head +1 where b1 = 0) 130// At every level the value at lane L is its partial at L plus its partial at L ^ o, the same operands warp_sum adds, 131// so every score is reduce12's bit for bit. A pair with an absent / masked cell runs the same code on row 0's staged 132// codes and that cell's outputs are not stored (it gets -FLT_MAX). 25 shuffles, 25 adds and 4 selects per pair 133// instead of 2 x (15 + 15 + ~30). 134__device__ __forceinline__ int px_head(int i, int lane) { 135 const int b3 = (lane >> 3) & 1, b2 = (lane >> 2) & 1; 136 return 6 * ((i / 6) ^ b3) + ((i % 6 + 3 * b2) % 6); 137} 138__device__ __forceinline__ void load_q_perm(const float* __restrict__ qh, int lane, float4 (&qa)[G], float4 (&qb)[G]) { 139#pragma unroll 140 for (int i = 0; i < G; ++i) { 141 const float4* p4 = reinterpret_cast<const float4*>(qh + (size_t) px_head(i, lane) * HD + lane * 8); 142 qa[i] = __ldg(p4); 143 qb[i] = __ldg(p4 + 1); 144 } 145} 146__device__ __forceinline__ void dequant8(const int8_t* krow, float sc, int lane, float (&k8)[8]) { 147 const uint2 raw = *reinterpret_cast<const uint2*>(krow + lane * 8); 148 const int8_t* cb = reinterpret_cast<const int8_t*>(&raw); 149#pragma unroll 150 for (int e = 0; e < 8; ++e) k8[e] = (float) cb[e] * sc; 151} 152__device__ __forceinline__ float dot8(const float (&k8)[8], const float4& qa, const float4& qb) { 153 return k8[0] * qa.x + k8[1] * qa.y + k8[2] * qa.z + k8[3] * qa.w + k8[4] * qb.x + k8[5] * qb.y + k8[6] * qb.z + 154 k8[7] * qb.w; 155} 156template <int UNR = 1> 157__device__ __forceinline__ void score_chunk_x(const float4 (&qa)[G], const float4 (&qb)[G], const int8_t (*sk)[HD], 158 const float (*sks)[NSC], const long long* srow, float (*sp)[CHUNK], 159 int n_here, float scale, int warp, int lane) { 160 const bool h16 = (lane & 16) != 0, h2 = (lane & 2) != 0, even = (lane & 1) == 0; 161 const int hb = 6 * ((lane >> 3) & 1) + 3 * ((lane >> 2) & 1); 162 const int h0 = hb + (h2 ? 2 : 0), h1 = hb + 1; 163#pragma unroll UNR 164 for (int i = 0; i < CHUNK / WARPS; i += 2) { 165 const int ca = warp + WARPS * i, cb = warp + WARPS * (i + 1); 166 const bool va = ca < n_here && srow[ca] >= 0, vb = cb < n_here && srow[cb] >= 0; 167 if (!va && !vb) { 168 if (lane < G) { sp[lane][ca] = -FLT_MAX; sp[lane][cb] = -FLT_MAX; } 169 continue; 170 } 171 const int c1 = h16 ? cb : ca, c2 = h16 ? ca : cb; // first / second (the staged rows of absent cells are row 0's) 172 float r[G]; 173 { 174 float k8[8]; 175 dequant8(sk[c2], sks[c2][lane >> 3], lane, k8); 176 float p2[G]; 177#pragma unroll 178 for (int s = 0; s < G; ++s) p2[s] = dot8(k8, qa[s], qb[s]); 179 dequant8(sk[c1], sks[c1][lane >> 3], lane, k8); 180#pragma unroll 181 for (int s = 0; s < G; ++s) r[s] = dot8(k8, qa[s], qb[s]) + __shfl_xor_sync(0xffffffffu, p2[s], 16); 182 } 183 float r6[6]; 184#pragma unroll 185 for (int s = 0; s < 6; ++s) r6[s] = r[s] + __shfl_xor_sync(0xffffffffu, r[s + 6], 8); 186 float r3[3]; 187#pragma unroll 188 for (int s = 0; s < 3; ++s) r3[s] = r6[s] + __shfl_xor_sync(0xffffffffu, r6[s + 3], 4); 189 const float x0 = (h2 ? r3[2] : r3[0]) + __shfl_xor_sync(0xffffffffu, h2 ? r3[0] : r3[2], 2); 190 const float x1 = r3[1] + __shfl_xor_sync(0xffffffffu, r3[1], 2); 191 const float o0 = x0 + __shfl_xor_sync(0xffffffffu, x0, 1); 192 const float o1 = x1 + __shfl_xor_sync(0xffffffffu, x1, 1); 193 const bool vmine = h16 ? vb : va; 194 if (even && vmine) { 195 sp[h0][c1] = o0 * scale; 196 if (!h2) sp[h1][c1] = o1 * scale; 197 } 198 if (lane < G) { 199 if (!va) sp[lane][ca] = -FLT_MAX; 200 if (!vb) sp[lane][cb] = -FLT_MAX; 201 } 202 } 203} 204 205// ps_score_kernel with score_chunk_x (q loaded in the permuted slot order) 206template <int UNR> 207__global__ void __launch_bounds__(THREADS, 2) ps_score_x_kernel(const float* __restrict__ q, QsaAttnPools p, 208 const int32_t* __restrict__ ids, 209 const int32_t* __restrict__ step, int n_kv_heads, 210 int page_size, float scale, float* __restrict__ blocks, 211 int n_chunks, int cap) { 212 q += (size_t) blockIdx.z * (size_t) (n_kv_heads * G) * HD; 213 ids += (size_t) blockIdx.z * (size_t) cap; 214 step += (size_t) blockIdx.z * kStepCount; 215 const int chunk = blockIdx.x, kvh = blockIdx.y; 216 float* const blk = blocks + (((size_t) blockIdx.z * n_kv_heads + kvh) * n_chunks + chunk) * PS_BLK; 217 __shared__ __align__(16) int8_t sk[CHUNK][HD]; 218 __shared__ __align__(16) float sks[CHUNK][NSC]; 219 __shared__ float sp[G][CHUNK]; 220 __shared__ long long srow[CHUNK]; 221 const int n_ids = __ldg(step + kStepWidth); 222 const int t = threadIdx.x, lane = t & 31, warp = t >> 5; 223 const int c0 = chunk * CHUNK; 224 const int n_here = min(CHUNK, n_ids - c0); 225 if (n_here <= 0) { 226 if (t < G) { blk[G * CHUNK + 2 * CHUNK + t] = -FLT_MAX; blk[G * CHUNK + 2 * CHUNK + G + t] = 0.0f; } 227 return; 228 } 229 if (t < CHUNK) { 230 const long long r = cell_row(p, ids, c0, t, n_here, n_kv_heads, kvh, page_size); 231 srow[t] = r; 232 reinterpret_cast<long long*>(blk + G * CHUNK)[t] = r; 233 } 234 float4 qa[G], qb[G]; 235 load_q_perm(q + (size_t) (kvh * G) * HD, lane, qa, qb); 236 __syncthreads(); 237 stage_rows(p.k_q, p.k_scale, srow, n_here, t, sk, sks); 238 __syncthreads(); 239 score_chunk_x<UNR>(qa, qb, sk, sks, srow, sp, n_here, scale, warp, lane); 240 __syncthreads(); 241 for (int h = warp; h < G; h += WARPS) { 242 const float a = sp[h][lane], b = sp[h][lane + 32]; 243 const float m = warp_max_x(fmaxf(a, b)); 244 const float ea = (lane < n_here && srow[lane] >= 0) ? __expf(a - m) : 0.0f; 245 const float eb = (lane + 32 < n_here && srow[lane + 32] >= 0) ? __expf(b - m) : 0.0f; 246 blk[h * CHUNK + lane] = ea; 247 blk[h * CHUNK + lane + 32] = eb; 248 const float l = warp_sum_x(ea + eb); 249 if (lane == 0) { blk[G * CHUNK + 2 * CHUNK + h] = m; blk[G * CHUNK + 2 * CHUNK + G + h] = l; } 250 } 251} 252 253// ps_value_d4_kernel: the value kernel with four dims a thread (64 threads, one CTA per (KV head, query)): a cell's four 254// codes are one 32-bit load (a warp reads 128 contiguous bytes), its 12 weights three broadcast float4 reads for 48 255// fmaf. V loads run four cells ahead. Per (dim, head) the fold and the merge are ps_value_kernel's. 256__global__ void __launch_bounds__(64) ps_value_d4_kernel(QsaAttnPools p, const int32_t* __restrict__ steps, 257 const float* __restrict__ blocks, int n_kv_heads, int n_chunks, 258 float* __restrict__ attn) { 259 constexpr int NT = 64; 260 const int kvh = blockIdx.x, z = blockIdx.y; 261 const int t = threadIdx.x; 262 const float* const blk0 = blocks + ((size_t) z * n_kv_heads + kvh) * n_chunks * PS_BLK; 263 __shared__ float sm_m[NCM][G], sm_w[NCM][G], sm_l[NCM][G]; 264 __shared__ float sL[G]; 265 __shared__ __align__(16) float spt[CHUNK][G]; 266 __shared__ long long srow[CHUNK]; 267 __shared__ int s_masked; 268 const int width = __ldg(steps + (size_t) z * kStepCount + kStepWidth); 269 const int n_act = width > 0 ? min(n_chunks, (width - 1) / CHUNK + 1) : 0; 270 for (int i = t; i < n_act * G; i += NT) { 271 const int c = i / G, h = i % G; 272 sm_m[c][h] = __ldg(blk0 + (size_t) c * PS_BLK + G * CHUNK + 2 * CHUNK + h); 273 sm_l[c][h] = __ldg(blk0 + (size_t) c * PS_BLK + G * CHUNK + 2 * CHUNK + G + h); 274 } 275 __syncthreads(); 276 if (t < G) { 277 float M = -FLT_MAX; 278 for (int c = 0; c < n_act; ++c) M = fmaxf(M, sm_m[c][t]); 279 float L = 0.0f; 280 for (int c = 0; c < n_act; ++c) { 281 const float m = sm_m[c][t]; 282 const float w = __expf(m - M); 283 sm_w[c][t] = w; 284 const float L_next = fmaf(sm_l[c][t], w, L); 285 L = (m == -FLT_MAX) ? L : L_next; 286 } 287 sL[t] = L; 288 } 289 const int d0 = 4 * t, g = d0 / KV_Q8_GROUP; 290 float acc[4][G]; 291#pragma unroll 292 for (int h = 0; h < G; ++h) 293#pragma unroll 294 for (int d = 0; d < 4; ++d) acc[d][h] = 0.0f; 295 for (int c = 0; c < n_act; ++c) { 296 const float* blk = blk0 + (size_t) c * PS_BLK; 297 const int n_here = min(CHUNK, width - c * CHUNK); 298 __syncthreads(); 299 if (t == 0) s_masked = 0; 300 __syncthreads(); 301 { 302 const long long r = __ldg(reinterpret_cast<const long long*>(blk + G * CHUNK) + t); 303 srow[t] = r; 304 if (t < n_here && r < 0) s_masked = 1; 305 } 306#pragma unroll 307 for (int j = 0; j < G * CHUNK / NT; ++j) { 308 const int idx = t + NT * j, h = idx / CHUNK, cell = idx % CHUNK; 309 spt[cell][h] = __ldg(blk + idx); 310 } 311 __syncthreads(); 312 float a[4][G]; 313#pragma unroll 314 for (int h = 0; h < G; ++h) 315#pragma unroll 316 for (int d = 0; d < 4; ++d) a[d][h] = 0.0f; 317 const auto fold4 = [&](int cl, uint32_t codes, float sc) { 318 const float v0 = (float) (int8_t) (codes & 0xFF) * sc, v1 = (float) (int8_t) ((codes >> 8) & 0xFF) * sc; 319 const float v2 = (float) (int8_t) ((codes >> 16) & 0xFF) * sc, v3 = (float) (int8_t) (codes >> 24) * sc; 320 const float4* w4 = reinterpret_cast<const float4*>(spt[cl]); 321 const float4 w0 = w4[0], w1 = w4[1], w2 = w4[2]; 322 const float ww[G] = {w0.x, w0.y, w0.z, w0.w, w1.x, w1.y, w1.z, w1.w, w2.x, w2.y, w2.z, w2.w}; 323#pragma unroll 324 for (int h = 0; h < G; ++h) { 325 a[0][h] = fmaf(ww[h], v0, a[0][h]); 326 a[1][h] = fmaf(ww[h], v1, a[1][h]); 327 a[2][h] = fmaf(ww[h], v2, a[2][h]); 328 a[3][h] = fmaf(ww[h], v3, a[3][h]); 329 } 330 }; 331 const auto vld = [&](int cl, uint32_t& codes, float& sc) { 332 const long long r = srow[cl]; 333 const long long rr = r >= 0 ? r : 0; 334 codes = __ldg(reinterpret_cast<const uint32_t*>(p.v_q + rr * HD + d0)); 335 sc = __half2float(__ushort_as_half(__ldg(p.v_scale + rr * NSC + g))); 336 }; 337 if (!s_masked) { 338 int cl = 0; 339 for (; cl + 4 <= n_here; cl += 4) { 340 uint32_t k0, k1, k2, k3; 341 float s0, s1, s2, s3; 342 vld(cl, k0, s0); vld(cl + 1, k1, s1); vld(cl + 2, k2, s2); vld(cl + 3, k3, s3); 343 fold4(cl, k0, s0); 344 fold4(cl + 1, k1, s1); 345 fold4(cl + 2, k2, s2); 346 fold4(cl + 3, k3, s3); 347 } 348 for (; cl < n_here; ++cl) { 349 uint32_t k0; 350 float s0; 351 vld(cl, k0, s0); 352 fold4(cl, k0, s0); 353 } 354 } else { 355 for (int cl = 0; cl < n_here; ++cl) { 356 if (srow[cl] < 0) continue; 357 uint32_t k0; 358 float s0; 359 vld(cl, k0, s0); 360 fold4(cl, k0, s0); 361 } 362 } 363#pragma unroll 364 for (int h = 0; h < G; ++h) { 365 const bool live = !(sm_m[c][h] == -FLT_MAX); 366 const float w = sm_w[c][h]; 367#pragma unroll 368 for (int d = 0; d < 4; ++d) { 369 const float acc_next = fmaf(a[d][h], w, acc[d][h]); 370 acc[d][h] = live ? acc_next : acc[d][h]; 371 } 372 } 373 } 374 __syncthreads(); 375 float* out = attn + ((size_t) z * n_kv_heads + kvh) * G * HD; 376#pragma unroll 377 for (int h = 0; h < G; ++h) { 378 const float L = sL[h]; 379 *reinterpret_cast<float4*>(out + (size_t) h * HD + d0) = 380 make_float4(L > 0.0f ? acc[0][h] / L : 0.0f, L > 0.0f ? acc[1][h] / L : 0.0f, 381 L > 0.0f ? acc[2][h] / L : 0.0f, L > 0.0f ? acc[3][h] / L : 0.0f); 382 } 383} 384 385bool sm6x_device() { 386 static int cc[64] = {}; 387 int dev = 0; 388 if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return false; } 389 if (cc[dev] == 0) { 390 int major = 0; 391 if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess) { 392 cudaGetLastError(); 393 return false; 394 } 395 cc[dev] = major; 396 } 397 return cc[dev] == 6; 398} 399 400} // namespace 401 402bool qsa_pattn_batch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, 403 const QsaShapes& s, float* scratch, uint64_t scratch_floats, float* attn, int64_t n_q, 404 void* stream) { 405 return sm6x_device() && qsa_pattn_batch_any_arch(q, pools, ids, steps, cap, s, scratch, scratch_floats, attn, n_q, stream); 406} 407 408bool qsa_pattn_batch_any_arch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, 409 int64_t cap, const QsaShapes& s, float* scratch, uint64_t scratch_floats, float* attn, 410 int64_t n_q, void* stream) { 411 if (n_q <= 0) return true; 412 if (s.head_dim != HD || s.n_head != (int64_t) G * s.n_head_kv || cap <= 0 || !scratch || !ids || !steps || 413 !pools.page_table || pools.k_q == nullptr || pools.v_q == nullptr || pools.k_q4 != nullptr || 414 pools.v_q4 != nullptr || !pools.k_scale || !pools.v_scale) 415 return false; 416 const cudaStream_t st = (cudaStream_t) stream; 417 const int n_chunks = (int) ((cap + CHUNK - 1) / CHUNK); 418 const float scale = 1.0f / sqrtf((float) HD); 419 if (n_chunks > NCM) return false; // a longer selection keeps the usual path 420 // the scratch holds the blocks of one batch of queries (probabilities, rows, m and l of every chunk), evenly split 421 const uint64_t per_q = (uint64_t) s.n_head_kv * (uint64_t) n_chunks * PS_BLK; 422 const int64_t max_batch = (int64_t) (scratch_floats / per_q); 423 if (max_batch < 1) return false; 424 const int64_t n_batches = (n_q + max_batch - 1) / max_batch; 425 const int64_t batch = (n_q + n_batches - 1) / n_batches; 426 for (int64_t t0 = 0; t0 < n_q; t0 += batch) { 427 const int64_t nb = n_q - t0 < batch ? n_q - t0 : batch; 428 const dim3 g1((unsigned) n_chunks, (unsigned) s.n_head_kv, (unsigned) nb); 429 const dim3 g2((unsigned) s.n_head_kv, (unsigned) nb); 430 const float* qb = q + (size_t) t0 * s.n_head * HD; 431 const int32_t* ib = ids + (size_t) t0 * cap; 432 const int32_t* sb = steps + (size_t) t0 * kStepCount; 433 ps_score_x_kernel<1><<<g1, THREADS, 0, st>>>(qb, pools, ib, sb, (int) s.n_head_kv, (int) s.page_size, scale, 434 scratch, n_chunks, (int) cap); 435 ps_value_d4_kernel<<<g2, 64, 0, st>>>(pools, sb, scratch, (int) s.n_head_kv, n_chunks, 436 attn + (size_t) t0 * s.n_head * HD); 437 } 438 const cudaError_t e = cudaGetLastError(); 439 if (e != cudaSuccess) { 440 // a launch that did not start: say so once, and let the caller run the usual attention over the whole chunk 441 static bool warned = false; 442 if (!warned) { 443 warned = true; 444 std::fprintf(stderr, "strata pattn: launch failed (%s); the usual attention runs instead\n", cudaGetErrorString(e)); 445 } 446 return false; 447 } 448 return true; 449} 450 451#else // not the experimental sm_60 CUDA build 452bool qsa_pattn_batch(const float*, const QsaAttnPools&, const int32_t*, const int32_t*, int64_t, const QsaShapes&, 453 float*, uint64_t, float*, int64_t, void*) { return false; } 454bool qsa_pattn_batch_any_arch(const float*, const QsaAttnPools&, const int32_t*, const int32_t*, int64_t, const QsaShapes&, 455 float*, uint64_t, float*, int64_t, void*) { return false; } 456#endif 457 458} // namespace strata::kernels