okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1// metal/kernels/qsab.metal - qsa_attn (qsa.metal) with its loads batched (after qsa.metal). The score loop and the V loop each 2// waited one memory latency per cell (64 cells a chunk: ~30 us a layer in a 1,000-cell context); here the K rows of NS cells 3// and the V values of NB cells are loaded before they are used. Every dot, sum and accumulation is done with the same 4// operands in the same order as qsa_attn, so the partials are bitwise the same. 5kernel void qsa_attn_b(device const float* qcur [[buffer(0)]], device const char* kq [[buffer(1)]], 6 device const char* vq [[buffer(2)]], device const half* ksc [[buffer(3)]], 7 device const half* vsc [[buffer(4)]], device const int* ids [[buffer(5)]], 8 device float* part [[buffer(6)]], constant QsaArgs& a [[buffer(7)]], 9 device const uint* rinfo [[buffer(8), function_constant(qsa_rows)]], 10 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 11 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 12 constexpr int HPG = QH / QKV; 13 constexpr int NS = 4, NB = 8; 14 threadgroup float qs[HPG][QD]; 15 threadgroup float s[HPG][QCH]; 16 threadgroup int cid[QCH]; 17 const int chunk = int(tg.x), g = int(tg.y), t = int(tg.z); 18 const uint n_kv = (qsa_rows ? rinfo[8 + 4 * t + 1] : a.pos0 + uint(t)) + 1, width = min(n_kv, uint(QBUDGET)); 19 if (qsa_rows) { const ulong sl = rinfo[8 + 4 * t]; kq += sl * rinfo[0]; vq += sl * rinfo[0]; ksc += sl * rinfo[1]; vsc += sl * rinfo[1]; } 20 const int c0 = chunk * QCH; 21 const int nchunks = (QBUDGET + QCH - 1) / QCH; 22 device float* pp = part + ((uint(t) * nchunks + chunk) * QH) * (QD + 2); 23 if (uint(c0) >= width) { 24 for (int h = 0; h < HPG; ++h) 25 if (tid == 0) { pp[(g * HPG + h) * (QD + 2) + QD] = -INFINITY; pp[(g * HPG + h) * (QD + 2) + QD + 1] = 0.0f; } 26 return; 27 } 28 const int nc = min(QCH, int(width) - c0); 29 for (int h = 0; h < HPG; ++h) qs[h][tid] = qcur[(t * QH + g * HPG + h) * QD + tid]; 30 if (tid < QCH) cid[tid] = tid < nc ? ids[t * QBUDGET + c0 + tid] : 0; 31 threadgroup_barrier(mem_flags::mem_threadgroup); 32 // scores: simdgroup sg takes cells sg, sg + 8, ...; NS of them at a time 33 for (int cb = sg; cb < nc; cb += 8 * NS) { 34 float kv[NS][8], scl[NS]; 35 for (int b = 0; b < NS; ++b) { 36 const int c = cb + 8 * b; 37 if (c < nc) { 38 const uint rowi = uint(cid[c]) * QKV + uint(g); 39 device const char* kr = kq + rowi * QD; 40 scl[b] = float(ksc[rowi * 4 + (lane >> 3)]); 41 for (int i = 0; i < 8; ++i) kv[b][i] = float(kr[lane * 8 + i]); 42 } 43 } 44 for (int b = 0; b < NS; ++b) { 45 const int c = cb + 8 * b; 46 if (c < nc) { 47 for (int i = 0; i < 8; ++i) kv[b][i] = kv[b][i] * scl[b]; 48 for (int h = 0; h < HPG; ++h) { 49 float d = 0.0f; 50 for (int i = 0; i < 8; ++i) d = fma(qs[h][lane * 8 + i], kv[b][i], d); 51 d = simd_sum(d); 52 if (lane == 0) s[h][c] = d * 0.0625f; 53 } 54 } 55 } 56 } 57 threadgroup_barrier(mem_flags::mem_threadgroup); 58 threadgroup float mh[HPG], lh[HPG]; 59 for (int h = sg; h < HPG; h += 8) { 60 float m = -INFINITY; 61 for (int c = lane; c < nc; c += 32) m = max(m, s[h][c]); 62 m = simd_max(m); 63 float l = 0.0f; 64 for (int c = lane; c < nc; c += 32) { 65 const float p = exp(s[h][c] - m); 66 s[h][c] = p; 67 l += p; 68 } 69 l = simd_sum(l); 70 if (lane == 0) { mh[h] = m; lh[h] = l; } 71 } 72 threadgroup_barrier(mem_flags::mem_threadgroup); 73 // acc[h][j] = sum_c p[h][c] v[c][j]: thread = dim j; NB cells' values loaded together, added in cell order 74 float acc[HPG]; 75 for (int h = 0; h < HPG; ++h) acc[h] = 0.0f; 76 for (int cb = 0; cb < nc; cb += NB) { 77 float v[NB]; 78 for (int b = 0; b < NB; ++b) 79 if (cb + b < nc) { 80 const uint rowi = uint(cid[cb + b]) * QKV + uint(g); 81 v[b] = float(vq[rowi * QD + tid]) * float(vsc[rowi * 4 + (tid >> 6)]); 82 } 83 for (int b = 0; b < NB; ++b) 84 if (cb + b < nc) 85 for (int h = 0; h < HPG; ++h) acc[h] = fma(s[h][cb + b], v[b], acc[h]); 86 } 87 for (int h = 0; h < HPG; ++h) { 88 device float* o = pp + (g * HPG + h) * (QD + 2); 89 o[tid] = acc[h]; 90 if (tid == 0) { o[QD] = mh[h]; o[QD + 1] = lh[h]; } 91 } 92}