okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c0e245c8b
main
1// metal/kernels/qsa2.metal - QSA decode v2 (after qsa.metal): the same selection and attention math, more parallel. 2// 3// qsa_attn2 chunks of 32 cells, the 12 query heads of a kv head in 2 groups of 6: 4x the threadgroups of 4// qsa_attn (at a 1K context 128 instead of 32); V loads 8 cells at a time. 5// qsa_merge2 qsa_merge for that chunking. 6// qsa_topk2 the digit of each radix pass found by a parallel suffix scan of the 256 bins (simdgroup prefix 7// sums) instead of one thread; cells emitted from contiguous block ranges per thread after one scan. 8 9#define QCH2 32 10#define QHG 2 11 12// partials: [n_tok][chunks2][QH][QD + 2]. grid (chunks2, QKV * QHG, n_tok), 256 threads. 13kernel void qsa_attn2(device const float* qcur [[buffer(0)]], device const char* kq [[buffer(1)]], 14 device const char* vq [[buffer(2)]], device const half* ksc [[buffer(3)]], 15 device const half* vsc [[buffer(4)]], device const int* ids [[buffer(5)]], 16 device float* part [[buffer(6)]], constant QsaArgs& a [[buffer(7)]], 17 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 18 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 19 constexpr int HPG = QH / QKV / QHG; // 6 20 threadgroup float qs[HPG][QD]; 21 threadgroup float s[HPG][QCH2]; 22 threadgroup float mh[HPG], lh[HPG]; 23 threadgroup int cid[QCH2]; 24 const int chunk = int(tg.x), g = int(tg.y) / QHG, hg = int(tg.y) % QHG, t = int(tg.z); 25 const int h0 = g * (QH / QKV) + hg * HPG; 26 const uint n_kv = a.pos0 + uint(t) + 1, width = min(n_kv, uint(QBUDGET)); 27 const int c0 = chunk * QCH2; 28 const int nchunks = (QBUDGET + QCH2 - 1) / QCH2; 29 if (uint(c0) >= width) return; 30 device float* pp = part + ((uint(t) * nchunks + chunk) * QH) * (QD + 2); 31 const int nc = min(QCH2, int(width) - c0); 32 for (int h = 0; h < HPG; ++h) qs[h][tid] = qcur[(t * QH + h0 + h) * QD + tid]; 33 if (tid < QCH2) cid[tid] = tid < nc ? ids[t * QBUDGET + c0 + tid] : 0; 34 threadgroup_barrier(mem_flags::mem_threadgroup); 35 for (int c = sg; c < nc; c += 8) { 36 const uint rowi = uint(cid[c]) * QKV + uint(g); 37 device const char4* kr = (device const char4*)(kq + rowi * QD + lane * 8); 38 const char4 k0 = kr[0], k1 = kr[1]; 39 const float scl = float(ksc[rowi * 4 + (lane >> 3)]); 40 const float kv[8] = {float(k0.x) * scl, float(k0.y) * scl, float(k0.z) * scl, float(k0.w) * scl, 41 float(k1.x) * scl, float(k1.y) * scl, float(k1.z) * scl, float(k1.w) * scl}; 42 for (int h = 0; h < HPG; ++h) { 43 float d = 0.0f; 44 for (int i = 0; i < 8; ++i) d = fma(qs[h][lane * 8 + i], kv[i], d); 45 d = simd_sum(d); 46 if (lane == 0) s[h][c] = d * 0.0625f; 47 } 48 } 49 threadgroup_barrier(mem_flags::mem_threadgroup); 50 if (sg < HPG) { 51 const int h = sg; 52 const float sv = lane < nc ? s[h][lane] : -INFINITY; 53 const float m = simd_max(sv); 54 const float p = lane < nc ? exp(sv - m) : 0.0f; 55 const float l = simd_sum(p); 56 if (lane < nc) s[h][lane] = p; 57 if (lane == 0) { mh[h] = m; lh[h] = l; } 58 } 59 threadgroup_barrier(mem_flags::mem_threadgroup); 60 float acc[HPG]; 61 for (int h = 0; h < HPG; ++h) acc[h] = 0.0f; 62 for (int cb = 0; cb < nc; cb += 8) { 63 float v[8]; 64 const int m = min(8, nc - cb); 65 for (int c = 0; c < 8; ++c) { 66 v[c] = 0.0f; 67 if (c < m) { 68 const uint rowi = uint(cid[cb + c]) * QKV + uint(g); 69 v[c] = float(vq[rowi * QD + tid]) * float(vsc[rowi * 4 + (tid >> 6)]); 70 } 71 } 72 for (int c = 0; c < m; ++c) 73 for (int h = 0; h < HPG; ++h) acc[h] = fma(s[h][cb + c], v[c], acc[h]); 74 } 75 for (int h = 0; h < HPG; ++h) { 76 device float* o = pp + (h0 + h) * (QD + 2); 77 o[tid] = acc[h]; 78 if (tid == 0) { o[QD] = mh[h]; o[QD + 1] = lh[h]; } 79 } 80} 81 82kernel void qsa_merge2(device const float* part [[buffer(0)]], device const float* qfull [[buffer(1)]], 83 device float* xp [[buffer(2)]], constant QsaArgs& a [[buffer(3)]], 84 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]]) { 85 threadgroup float msum[256]; 86 const int h = int(tg.x), t = int(tg.y); 87 const int nchunks = (QBUDGET + QCH2 - 1) / QCH2; 88 const uint n_kv = a.pos0 + uint(t) + 1, width = min(n_kv, uint(QBUDGET)); 89 const int nc = int((width + QCH2 - 1) / QCH2); 90 device const float* pp = part + (uint(t) * nchunks * QH + uint(h)) * (QD + 2); 91 float M = -INFINITY; 92 for (int k = 0; k < nc; ++k) M = max(M, pp[k * QH * (QD + 2) + QD]); 93 float L = 0.0f, o = 0.0f; 94 for (int k = 0; k < nc; ++k) { 95 device const float* q = pp + k * QH * (QD + 2); 96 const float e = exp(q[QD] - M); 97 L = fma(q[QD + 1], e, L); 98 o = fma(q[tid], e, o); 99 } 100 o = o / L; 101 const float gate = qfull[(t * QH + h) * 2 * QD + QD + tid]; 102 const float y = o * (1.0f / (1.0f + exp(-gate))); 103 const uint n = QH * QD, plane = a.n_tok * n, d = uint(h) * QD + tid; 104 const int j = tid & 15; 105 const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f}; 106 const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f}; 107 device float* raw = xp + uint(t) * n + d; 108 raw[0] = y; 109 raw[plane] = y * q2s[j & 7]; 110 raw[2 * plane] = y * p4s[j & 3]; 111 raw[3 * plane] = (j & 1) ? y * 0.00390625f : y; 112 msum[tid] = y; 113 threadgroup_barrier(mem_flags::mem_threadgroup); 114 if (j == 0) { 115 float s0 = 0.0f, s1 = 0.0f; 116 for (int k = 0; k < 16; k += 2) { s0 += msum[tid + k]; s1 += msum[tid + k + 1]; } 117 xp[4 * plane + uint(t) * (n / 16) + d / 16] = s0 + s1; 118 } 119} 120 121kernel void qsa_topk2(device const float* scores [[buffer(0)]], device int* ids [[buffer(1)]], 122 constant QsaArgs& a [[buffer(2)]], 123 device const uint* rinfo [[buffer(3), function_constant(qsa_rows)]], 124 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 125 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 126 threadgroup atomic_uint hist[256]; 127 threadgroup uint sgs[32], sgs2[32]; 128 threadgroup uint sel[2]; 129 const int t = int(tg.x); 130 const uint n_kv = (qsa_rows ? rinfo[8 + 4 * t + 1] : a.pos0 + uint(t)) + 1, n_bid = n_kv >> 2, nb = n_bid + 1; 131 device int* out = ids + t * QBUDGET; 132 device const float* sc = scores + uint(t) * (a.max_cells / 4 + 2); 133 if (n_kv <= QBUDGET) { 134 for (uint c = tid; c < n_kv; c += 1024) out[c] = int(c); 135 return; 136 } 137 uint prefix = 0, pmask = 0, need = QBUDGET; 138 for (int pass = 0; pass < 4; ++pass) { 139 const int shift = 24 - 8 * pass; 140 if (tid < 256) atomic_store_explicit(&hist[tid], 0u, memory_order_relaxed); 141 threadgroup_barrier(mem_flags::mem_threadgroup); 142 for (uint b = tid; b < nb; b += 1024) { 143 const uint k = okey(sc[b]); 144 if ((k & pmask) == prefix) { 145 const uint w = b < n_bid ? 4u : (n_kv & 3u); 146 if (w) atomic_fetch_add_explicit(&hist[(k >> shift) & 255u], w, memory_order_relaxed); 147 } 148 } 149 threadgroup_barrier(mem_flags::mem_threadgroup); 150 // thread i < 256 holds bin 255 - i: an exclusive prefix over i = the cells of the higher digits 151 uint cnt = 0, ex = 0; 152 if (tid < 256) { 153 cnt = atomic_load_explicit(&hist[255 - tid], memory_order_relaxed); 154 ex = simd_prefix_exclusive_sum(cnt); 155 if (lane == 31) sgs[sg] = ex + cnt; 156 } 157 threadgroup_barrier(mem_flags::mem_threadgroup); 158 if (tid < 256) { 159 uint above = ex; 160 for (int i = 0; i < int(sg); ++i) above += sgs[i]; 161 if (above < need && above + cnt >= need) { sel[0] = 255u - tid; sel[1] = need - above; } 162 } 163 threadgroup_barrier(mem_flags::mem_threadgroup); 164 prefix |= sel[0] << shift; 165 pmask |= 255u << shift; 166 need = sel[1]; 167 threadgroup_barrier(mem_flags::mem_threadgroup); 168 } 169 const uint T = prefix; 170 const uint per = (nb + 1023) / 1024, b0 = min(nb, uint(tid) * per), b1 = min(nb, b0 + per); 171 uint gt = 0, eq = 0; 172 for (uint b = b0; b < b1; ++b) { 173 const uint k = okey(sc[b]); 174 const uint cells = b < n_bid ? 4u : (n_kv & 3u); 175 if (k > T) gt += cells; 176 else if (k == T) eq += cells; 177 } 178 const uint eqb = simd_prefix_exclusive_sum(eq); 179 if (lane == 31) sgs2[sg] = eqb + eq; 180 threadgroup_barrier(mem_flags::mem_threadgroup); 181 uint eq_before = eqb; 182 for (int i = 0; i < int(sg); ++i) eq_before += sgs2[i]; 183 const uint eq_take = eq_before >= need ? 0u : min(eq, need - eq_before); 184 const uint mine = gt + eq_take; 185 const uint mb = simd_prefix_exclusive_sum(mine); 186 if (lane == 31) sgs[sg] = mb + mine; 187 threadgroup_barrier(mem_flags::mem_threadgroup); 188 uint off = mb; 189 for (int i = 0; i < int(sg); ++i) off += sgs[i]; 190 uint left = eq_take; 191 for (uint b = b0; b < b1; ++b) { 192 const uint k = okey(sc[b]); 193 const uint cells = b < n_bid ? 4u : (n_kv & 3u); 194 uint take = 0; 195 if (k > T) take = cells; 196 else if (k == T) { take = min(cells, left); left -= take; } 197 for (uint c = 0; c < take; ++c) out[off + c] = int(4 * b + c); 198 off += take; 199 } 200} 201 202// qsa_attn3: qsa_attn's chunking and partial layout (qsa_merge reads it), without a reduction per (cell, head): 203// for q.k a lane owns a cell (8 simdgroups = 2 halves of the 64 cells x 4 groups of 3 heads; q from threadgroup 204// memory, read by every lane alike), for p.v a thread owns a dimension. grid (33, QKV, n_tok), 256 threads. 205kernel void qsa_attn3(device const float* qcur [[buffer(0)]], device const char* kq [[buffer(1)]], 206 device const char* vq [[buffer(2)]], device const half* ksc [[buffer(3)]], 207 device const half* vsc [[buffer(4)]], device const int* ids [[buffer(5)]], 208 device float* part [[buffer(6)]], constant QsaArgs& a [[buffer(7)]], 209 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 210 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 211 constexpr int HPG = QH / QKV; // 12 212 threadgroup float qs[HPG][QD]; 213 threadgroup float s[HPG][QCH]; 214 threadgroup float mh[HPG], lh[HPG]; 215 threadgroup int cid[QCH]; 216 const int chunk = int(tg.x), g = int(tg.y), t = int(tg.z); 217 const uint n_kv = a.pos0 + uint(t) + 1, width = min(n_kv, uint(QBUDGET)); 218 const int c0 = chunk * QCH; 219 const int nchunks = (QBUDGET + QCH - 1) / QCH; 220 device float* pp = part + ((uint(t) * nchunks + chunk) * QH) * (QD + 2); 221 if (uint(c0) >= width) return; 222 const int nc = min(QCH, int(width) - c0); 223 for (int h = 0; h < HPG; ++h) qs[h][tid] = qcur[(t * QH + g * HPG + h) * QD + tid]; 224 if (tid < QCH) cid[tid] = tid < nc ? ids[t * QBUDGET + c0 + tid] : 0; 225 threadgroup_barrier(mem_flags::mem_threadgroup); 226 { 227 const int half_ = sg & 1, hgrp = sg >> 1; // cells 32 * half_ + lane, heads 3 * hgrp .. + 3 228 const int c = 32 * half_ + lane; 229 if (c < nc) { 230 const uint rowi = uint(cid[c]) * QKV + uint(g); 231 device const char4* kr = (device const char4*)(kq + rowi * QD); 232 float d0 = 0.0f, d1 = 0.0f, d2 = 0.0f; 233 for (int gq = 0; gq < 4; ++gq) { 234 const float scl = float(ksc[rowi * 4 + gq]); 235 float e0 = 0.0f, e1 = 0.0f, e2 = 0.0f; 236 for (int i = 0; i < 16; ++i) { 237 const char4 k4 = kr[gq * 16 + i]; 238 const int base = gq * 64 + 4 * i; 239 const float kv[4] = {float(k4.x), float(k4.y), float(k4.z), float(k4.w)}; 240 for (int e = 0; e < 4; ++e) { 241 e0 = fma(qs[3 * hgrp][base + e], kv[e], e0); 242 e1 = fma(qs[3 * hgrp + 1][base + e], kv[e], e1); 243 e2 = fma(qs[3 * hgrp + 2][base + e], kv[e], e2); 244 } 245 } 246 d0 = fma(e0, scl, d0); 247 d1 = fma(e1, scl, d1); 248 d2 = fma(e2, scl, d2); 249 } 250 s[3 * hgrp][c] = d0 * 0.0625f; 251 s[3 * hgrp + 1][c] = d1 * 0.0625f; 252 s[3 * hgrp + 2][c] = d2 * 0.0625f; 253 } 254 } 255 threadgroup_barrier(mem_flags::mem_threadgroup); 256 for (int h = sg; h < HPG; h += 8) { 257 float m = -INFINITY; 258 for (int c = lane; c < nc; c += 32) m = max(m, s[h][c]); 259 m = simd_max(m); 260 float l = 0.0f; 261 for (int c = lane; c < nc; c += 32) { 262 const float p = exp(s[h][c] - m); 263 s[h][c] = p; 264 l += p; 265 } 266 l = simd_sum(l); 267 if (lane == 0) { mh[h] = m; lh[h] = l; } 268 } 269 threadgroup_barrier(mem_flags::mem_threadgroup); 270 float acc[HPG]; 271 for (int h = 0; h < HPG; ++h) acc[h] = 0.0f; 272 for (int cb = 0; cb < nc; cb += 8) { 273 float v[8]; 274 const int m = min(8, nc - cb); 275 for (int c = 0; c < 8; ++c) { 276 v[c] = 0.0f; 277 if (c < m) { 278 const uint rowi = uint(cid[cb + c]) * QKV + uint(g); 279 v[c] = float(vq[rowi * QD + tid]) * float(vsc[rowi * 4 + (tid >> 6)]); 280 } 281 } 282 for (int c = 0; c < m; ++c) 283 for (int h = 0; h < HPG; ++h) acc[h] = fma(s[h][cb + c], v[c], acc[h]); 284 } 285 for (int h = 0; h < HPG; ++h) { 286 device float* o = pp + (g * HPG + h) * (QD + 2); 287 o[tid] = acc[h]; 288 if (tid == 0) { o[QD] = mh[h]; o[QD + 1] = lh[h]; } 289 } 290}