okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/kernels/gr.metal - the hyper-connection (gated residual) read for a window of tokens, with the previous 2// sublayer's write folded in. The math is fused_gr.hpp's (native contract: FP32 activations, BF16 weights): 3// 4// gr_norm R'[c] = R[c] + bo * 2 sigmoid(inj_prev[c] / hc) (when apply; written back in place) 5// xn[c] = R'[c] * w_norm[c] * rsqrt(mean(R'[c]^2) + eps) 6// gr_down lo[k] = silu((w_down[k] . xn) / hc) k < hc_lr; inject[c] = w_inject[c] . xn 7// gr_up mixed[d] = mean_c xn[c][d] * sigmoid(w_up[c n + d] . lo), written as prepared activations (xprep's 8// planes and per-16 sums) for the GEMVs that read it 9// 10// Shapes: n_embd 2560, hc 4, hc_lr 320. Per token t: R[t] (hc * n), bo[t] (n), inj_prev[t] (hc), xn[t] (hc * n), 11// lo[t] (hc_lr), inject[t] (hc), XP planes of n_tok rows. 12 13constant int GR_NTOK [[function_constant(1)]]; 14// GEO_NJ: n_embd / 256, the elements per thread of one stream's row in gr_norm2 (Flash-Next 10); GEO_GRU_NU: the units of one up row a lane of 15// the prefetching gr_up holds, ceil(hc_lr / 32 / 4) (Flash-Next 3). The host prelude (engine/geo.h) defines them for another shape. 16#ifdef GEO_NJ 17#define GR_NJ GEO_NJ 18#else 19#define GR_NJ 10 20#endif 21#ifdef GEO_GRU_NU 22#define GR_NU GEO_GRU_NU 23#else 24#define GR_NU 3 25#endif 26 27struct GrArgs { 28 uint n_embd; 29 uint hc; 30 uint hc_lr; 31 uint n_tok; 32 uint apply; 33 float eps; 34}; 35 36// one threadgroup per (stream c, token t), 256 threads 37kernel void gr_norm(device float* R [[buffer(0)]], device const float* bo [[buffer(1)]], 38 device const float* inj [[buffer(2)]], device const float* w_norm [[buffer(3)]], 39 device float* xn [[buffer(4)]], constant GrArgs& a [[buffer(5)]], 40 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 41 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 42 threadgroup float part[8]; 43 const uint c = tg.x, t = tg.y, n = a.n_embd; 44 device float* r = R + (t * a.hc + c) * n; 45 const float g = a.apply ? 2.0f / (1.0f + exp(-inj[t * a.hc + c] / float(a.hc))) : 0.0f; 46 float ss = 0.0f; 47 for (uint d = tid; d < n; d += 256) { 48 float v = r[d]; 49 if (a.apply) { 50 v = fma(bo[t * n + d], g, v); 51 r[d] = v; 52 } 53 ss = fma(v, v, ss); 54 } 55 ss = simd_sum(ss); 56 if (lane == 0) part[sg] = ss; 57 threadgroup_barrier(mem_flags::mem_threadgroup); 58 float tot = 0.0f; 59 for (int i = 0; i < 8; ++i) tot += part[i]; 60 const float rs = rsqrt(tot / float(n) + a.eps); 61 device float* o = xn + (t * a.hc + c) * n; 62 device const float* w = w_norm + c * n; 63 for (uint d = tid; d < n; d += 256) o[d] = r[d] * w[d] * rs; 64} 65 66// gr_norm with its loads batched: the same arithmetic, in the same order, as gr_norm (n_embd 2560: ten elements a thread). gr_norm's 67// loop stores R in place between the loads of the next elements, which the compiler cannot move past each other, so its ten 68// iterations were ten dependent memory latencies (plus ten more in the second loop); here R, bo and w_norm are all loaded 69// first, then summed, reduced and stored. 70kernel void gr_norm2(device float* R [[buffer(0)]], device const float* bo [[buffer(1)]], 71 device const float* inj [[buffer(2)]], device const float* w_norm [[buffer(3)]], 72 device float* xn [[buffer(4)]], constant GrArgs& a [[buffer(5)]], 73 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 74 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 75 constexpr int NJ = GR_NJ; 76 threadgroup float part[8]; 77 const uint c = tg.x, t = tg.y, n = a.n_embd; 78 device float* r = R + (t * a.hc + c) * n; 79 device const float* b = bo + t * n; 80 device const float* w = w_norm + c * n; 81 float rv[NJ], wv[NJ], bv[NJ]; 82 for (int j = 0; j < NJ; ++j) rv[j] = r[tid + 256 * j]; 83 if (a.apply) 84 for (int j = 0; j < NJ; ++j) bv[j] = b[tid + 256 * j]; 85 for (int j = 0; j < NJ; ++j) wv[j] = w[tid + 256 * j]; 86 const float g = a.apply ? 2.0f / (1.0f + exp(-inj[t * a.hc + c] / float(a.hc))) : 0.0f; 87 float ss = 0.0f; 88 for (int j = 0; j < NJ; ++j) { 89 float v = rv[j]; 90 if (a.apply) { 91 v = fma(bv[j], g, v); 92 rv[j] = v; 93 } 94 ss = fma(v, v, ss); 95 } 96 if (a.apply) 97 for (int j = 0; j < NJ; ++j) r[tid + 256 * j] = rv[j]; 98 ss = simd_sum(ss); 99 if (lane == 0) part[sg] = ss; 100 threadgroup_barrier(mem_flags::mem_threadgroup); 101 float tot = 0.0f; 102 for (int i = 0; i < 8; ++i) tot += part[i]; 103 const float rs = rsqrt(tot / float(n) + a.eps); 104 device float* o = xn + (t * a.hc + c) * n; 105 for (int j = 0; j < NJ; ++j) o[tid + 256 * j] = rv[j] * wv[j] * rs; 106} 107 108// rows: 0 .. hc_lr - 1 = w_down, hc_lr .. hc_lr + hc - 1 = w_inject (both bf16 rows of hc * n_embd). 109// SG simdgroups split the row's units KS ways; LPR = 32 (a simdgroup per row); RS = SG / KS rows per threadgroup. 110// one row's K-split partial over xn (units of format F) 111template <int F> 112static inline void grd_acc(device const uchar* w, int nin, int ks, int KS, ushort lane, device const float* xn, 113 thread float* acc, threadgroup const uint* notab) { 114 constexpr int UNIT = fmt<F>::UNIT; 115 const int U = nin / UNIT, u0 = ks * U / KS, u1 = (ks + 1) * U / KS; 116 for (int u = u0 + lane; u < u1; u += 32) { 117 float q[UNIT]; 118 float4 sc; 119 Dec<F>::run(w, u, q, sc, notab); 120 for (int t = 0; t < GR_NTOK; ++t) { 121 float xs[UNIT], xsum[2] = {0.0f, 0.0f}; 122 for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * u + k, xs + k); 123 acc[t] += unit_combine<UNIT>(q, sc, xs, xsum); 124 } 125 } 126} 127 128// the same sums with the loads of two units in flight (units u and u + 32 decoded before either is combined): the lane order and the 129// per-token addition order are grd_acc's (A then B), so every bit is the same; only the memory latencies overlap. A lane has 1-2 units here. 130template <int F> 131static inline void grd_acc_p(device const uchar* w, int nin, int ks, int KS, ushort lane, device const float* xn, 132 thread float* acc, threadgroup const uint* notab) { 133 constexpr int UNIT = fmt<F>::UNIT; 134 const int U = nin / UNIT, u0 = ks * U / KS, u1 = (ks + 1) * U / KS; 135 int u = u0 + lane; 136 for (; u + 32 < u1; u += 64) { 137 float qa[UNIT], qb[UNIT]; 138 float4 sa, sb; 139 Dec<F>::run(w, u, qa, sa, notab); 140 Dec<F>::run(w, u + 32, qb, sb, notab); 141 for (int t = 0; t < GR_NTOK; ++t) { 142 float xa[UNIT], xb[UNIT], xsum[2] = {0.0f, 0.0f}; 143 for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * u + k, xa + k); 144 for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * (u + 32) + k, xb + k); 145 acc[t] += unit_combine<UNIT>(qa, sa, xa, xsum); 146 acc[t] += unit_combine<UNIT>(qb, sb, xb, xsum); 147 } 148 } 149 for (; u < u1; u += 32) { 150 float q[UNIT]; 151 float4 sc; 152 Dec<F>::run(w, u, q, sc, notab); 153 for (int t = 0; t < GR_NTOK; ++t) { 154 float xs[UNIT], xsum[2] = {0.0f, 0.0f}; 155 for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * u + k, xs + k); 156 acc[t] += unit_combine<UNIT>(q, sc, xs, xsum); 157 } 158 } 159} 160 161// FI: the inject rows' format (the Unsloth files: Q8_0 down, F32 inject); FI = F is the original kernel 162template <int SG, int KS, int F, int FI, int PF = 0> 163kernel void gr_down_f(device const uchar* w_down [[buffer(0)]], device const uchar* w_inject [[buffer(1)]], 164 device const float* xn [[buffer(2)]], device float* lo [[buffer(3)]], 165 device float* inject [[buffer(4)]], constant GrArgs& a [[buffer(5)]], 166 uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], 167 ushort lane [[thread_index_in_simdgroup]]) { 168 constexpr int RS = SG / KS; 169 threadgroup float red[KS * RS * 8]; 170 threadgroup uint notab[1]; 171 const int ks = sg / RS, rs = sg % RS; 172 const int row = int(tg.x) * RS + rs; 173 const int nrows = int(a.hc_lr + a.hc); 174 const int rr = min(row, nrows - 1); 175 const int nin = int(a.hc * a.n_embd); 176 const uint rb = uint(nin / fmt<F>::QK * fmt<F>::BB), rbi = uint(nin / fmt<FI>::QK * fmt<FI>::BB); 177 float acc[8]; 178 for (int t = 0; t < 8; ++t) acc[t] = 0.0f; 179 if (PF) { 180 if (rr < int(a.hc_lr)) grd_acc_p<F>(w_down + ulong(rr) * rb, nin, ks, KS, lane, xn, acc, notab); 181 else grd_acc_p<FI>(w_inject + ulong(rr - int(a.hc_lr)) * rbi, nin, ks, KS, lane, xn, acc, notab); 182 } else { 183 if (rr < int(a.hc_lr)) grd_acc<F>(w_down + ulong(rr) * rb, nin, ks, KS, lane, xn, acc, notab); 184 else grd_acc<FI>(w_inject + ulong(rr - int(a.hc_lr)) * rbi, nin, ks, KS, lane, xn, acc, notab); 185 } 186 for (int t = 0; t < GR_NTOK; ++t) { 187 const float v = simd_sum(acc[t]); 188 if (lane == 0) red[(ks * RS + rs) * 8 + t] = v; 189 } 190 threadgroup_barrier(mem_flags::mem_threadgroup); 191 if (ks == 0 && lane < GR_NTOK && row < nrows) { 192 const int t = lane; 193 float v = 0.0f; 194 for (int k = 0; k < KS; ++k) v += red[(k * RS + rs) * 8 + t]; 195 if (row < int(a.hc_lr)) { 196 const float z = v / float(a.hc); 197 lo[t * int(a.hc_lr) + row] = z / (1.0f + exp(-z)); 198 } else { 199 inject[t * int(a.hc) + row - int(a.hc_lr)] = v; 200 } 201 } 202} 203 204// A threadgroup owns 16 consecutive d for all hc = 4 streams: 8 simdgroups, simdgroup s -> stream s / 2 and 205// d0 + 8 (s % 2) .. + 8; LPR = 4 lanes per row (320 = 20 units of 16, 5 per lane). 206// XP: [4 kinds][n_tok][n] then S16 [n_tok][n / 16] (units.metal's xprep layout). 207template <int F, int PF = 0> 208kernel void gr_up_f(device const uchar* w_up [[buffer(0)]], device const float* lo [[buffer(1)]], 209 device const float* xn [[buffer(2)]], device float* xp [[buffer(3)]], 210 constant GrArgs& a [[buffer(4)]], 211 uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], 212 ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) { 213 threadgroup float gate[8][4][16]; 214 threadgroup float msum[8][16]; 215 threadgroup uint notab[1]; 216 const int n = int(a.n_embd), c = sg >> 1, dl = 8 * (sg & 1) + lane / 4, li = lane & 3; 217 const int d = int(tg.x) * 16 + dl; 218 constexpr int UNIT = fmt<F>::UNIT; 219 device const uchar* w = w_up + ulong(c * n + d) * (a.hc_lr / fmt<F>::QK * fmt<F>::BB); 220 const int U = int(a.hc_lr) / UNIT; 221 float acc[8]; 222 for (int t = 0; t < 8; ++t) acc[t] = 0.0f; 223 if (PF) { 224 // up to three units (U = 10 over 4 lanes) decoded before the first is combined; the per-token order is u = li, li + 4, li + 8 225 constexpr int NU = GR_NU; 226 float q[NU][UNIT]; 227 float4 sc[NU]; 228 for (int j = 0; j < NU; ++j) 229 if (li + 4 * j < U) Dec<F>::run(w, li + 4 * j, q[j], sc[j], notab); 230 for (int t = 0; t < GR_NTOK; ++t) 231 for (int j = 0; j < NU; ++j) 232 if (li + 4 * j < U) { 233 float xs[UNIT], xsum[2] = {0.0f, 0.0f}; 234 for (int k = 0; k < UNIT; k += 16) load16(lo + t * int(a.hc_lr) + UNIT * (li + 4 * j) + k, xs + k); 235 acc[t] += unit_combine<UNIT>(q[j], sc[j], xs, xsum); 236 } 237 } else 238 for (int u = li; u < U; u += 4) { 239 float q[UNIT]; 240 float4 sc; 241 Dec<F>::run(w, u, q, sc, notab); 242 for (int t = 0; t < GR_NTOK; ++t) { 243 float xs[UNIT], xsum[2] = {0.0f, 0.0f}; 244 for (int k = 0; k < UNIT; k += 16) load16(lo + t * int(a.hc_lr) + UNIT * u + k, xs + k); 245 acc[t] += unit_combine<UNIT>(q, sc, xs, xsum); 246 } 247 } 248 for (int t = 0; t < GR_NTOK; ++t) { 249 const float v = lane_sum<4>(acc[t]); 250 if (li == 0) gate[t][c][dl] = 1.0f / (1.0f + exp(-v)); 251 } 252 threadgroup_barrier(mem_flags::mem_threadgroup); 253 // 16 threads per token: thread j -> d0 + j 254 const int t = tid / 16, j = tid & 15; 255 if (t < GR_NTOK) { 256 const int dd = int(tg.x) * 16 + j; 257 const int hc = int(a.hc); 258 float m = 0.0f; 259 for (int cc = 0; cc < hc; ++cc) m = fma(xn[(t * hc + cc) * n + dd], gate[t][cc][j], m); 260 m = m / float(hc); 261 const uint plane = a.n_tok * uint(n); 262 device float* raw = xp + t * n + dd; 263 const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 264 0.00006103515625f}; 265 const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f}; 266 raw[0] = m; 267 raw[plane] = m * q2s[j & 7]; 268 raw[2 * plane] = m * p4s[j & 3]; 269 raw[3 * plane] = (j & 1) ? m * 0.00390625f : m; 270 if (xh16) put_x16(xp, plane, t * n + dd, j, m); 271 msum[t][j] = m; 272 } 273 threadgroup_barrier(mem_flags::mem_threadgroup); 274 if (t < GR_NTOK && j == 0) { 275 // the per-16 sum in xprep's order (even and odd partial sums, then added) 276 float s0 = 0.0f, s1 = 0.0f; 277 for (int k = 0; k < 16; k += 2) { s0 += msum[t][k]; s1 += msum[t][k + 1]; } 278 xp[4 * a.n_tok * uint(n) + t * (n / 16) + tg.x] = s0 + s1; 279 } 280} 281 282#define GRD(S, K) template [[host_name("gr_down_" #S "_" #K)]] kernel decltype(gr_down_f<S, K, T_BF16, T_BF16>) gr_down_f<S, K, T_BF16, T_BF16>; \ 283 template [[host_name("gr_down_q8_" #S "_" #K)]] kernel decltype(gr_down_f<S, K, T_Q8_0, T_Q8_0>) gr_down_f<S, K, T_Q8_0, T_Q8_0>; \ 284 template [[host_name("gr_down_q8f_" #S "_" #K)]] kernel decltype(gr_down_f<S, K, T_Q8_0, T_F32>) gr_down_f<S, K, T_Q8_0, T_F32>; 285GRD(8, 8) GRD(8, 4) GRD(4, 4) GRD(4, 2) GRD(16, 16) GRD(16, 8) 286template [[host_name("gr_up")]] kernel decltype(gr_up_f<T_BF16>) gr_up_f<T_BF16>; 287template [[host_name("gr_up_q8")]] kernel decltype(gr_up_f<T_Q8_0>) gr_up_f<T_Q8_0>; 288// the prefetching variants (the engine's default; STRATA_METAL_HCLD=0 keeps the originals above) 289template [[host_name("gr_downp_q8_8_8")]] kernel decltype(gr_down_f<8, 8, T_Q8_0, T_Q8_0, 1>) gr_down_f<8, 8, T_Q8_0, T_Q8_0, 1>; 290template [[host_name("gr_downp_q8f_8_8")]] kernel decltype(gr_down_f<8, 8, T_Q8_0, T_F32, 1>) gr_down_f<8, 8, T_Q8_0, T_F32, 1>; 291template [[host_name("gr_upp_q8")]] kernel decltype(gr_up_f<T_Q8_0, 1>) gr_up_f<T_Q8_0, 1>;