// metal/kernels/gr.metal - the hyper-connection (gated residual) read for a window of tokens, with the previous // sublayer's write folded in. The math is fused_gr.hpp's (native contract: FP32 activations, BF16 weights): // // gr_norm R'[c] = R[c] + bo * 2 sigmoid(inj_prev[c] / hc) (when apply; written back in place) // xn[c] = R'[c] * w_norm[c] * rsqrt(mean(R'[c]^2) + eps) // gr_down lo[k] = silu((w_down[k] . xn) / hc) k < hc_lr; inject[c] = w_inject[c] . xn // gr_up mixed[d] = mean_c xn[c][d] * sigmoid(w_up[c n + d] . lo), written as prepared activations (xprep's // planes and per-16 sums) for the GEMVs that read it // // 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), // lo[t] (hc_lr), inject[t] (hc), XP planes of n_tok rows. constant int GR_NTOK [[function_constant(1)]]; // 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 // the prefetching gr_up holds, ceil(hc_lr / 32 / 4) (Flash-Next 3). The host prelude (engine/geo.h) defines them for another shape. #ifdef GEO_NJ #define GR_NJ GEO_NJ #else #define GR_NJ 10 #endif #ifdef GEO_GRU_NU #define GR_NU GEO_GRU_NU #else #define GR_NU 3 #endif struct GrArgs { uint n_embd; uint hc; uint hc_lr; uint n_tok; uint apply; float eps; }; // one threadgroup per (stream c, token t), 256 threads kernel void gr_norm(device float* R [[buffer(0)]], device const float* bo [[buffer(1)]], device const float* inj [[buffer(2)]], device const float* w_norm [[buffer(3)]], device float* xn [[buffer(4)]], constant GrArgs& a [[buffer(5)]], uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { threadgroup float part[8]; const uint c = tg.x, t = tg.y, n = a.n_embd; device float* r = R + (t * a.hc + c) * n; const float g = a.apply ? 2.0f / (1.0f + exp(-inj[t * a.hc + c] / float(a.hc))) : 0.0f; float ss = 0.0f; for (uint d = tid; d < n; d += 256) { float v = r[d]; if (a.apply) { v = fma(bo[t * n + d], g, v); r[d] = v; } ss = fma(v, v, ss); } ss = simd_sum(ss); if (lane == 0) part[sg] = ss; threadgroup_barrier(mem_flags::mem_threadgroup); float tot = 0.0f; for (int i = 0; i < 8; ++i) tot += part[i]; const float rs = rsqrt(tot / float(n) + a.eps); device float* o = xn + (t * a.hc + c) * n; device const float* w = w_norm + c * n; for (uint d = tid; d < n; d += 256) o[d] = r[d] * w[d] * rs; } // 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 // loop stores R in place between the loads of the next elements, which the compiler cannot move past each other, so its ten // iterations were ten dependent memory latencies (plus ten more in the second loop); here R, bo and w_norm are all loaded // first, then summed, reduced and stored. kernel void gr_norm2(device float* R [[buffer(0)]], device const float* bo [[buffer(1)]], device const float* inj [[buffer(2)]], device const float* w_norm [[buffer(3)]], device float* xn [[buffer(4)]], constant GrArgs& a [[buffer(5)]], uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { constexpr int NJ = GR_NJ; threadgroup float part[8]; const uint c = tg.x, t = tg.y, n = a.n_embd; device float* r = R + (t * a.hc + c) * n; device const float* b = bo + t * n; device const float* w = w_norm + c * n; float rv[NJ], wv[NJ], bv[NJ]; for (int j = 0; j < NJ; ++j) rv[j] = r[tid + 256 * j]; if (a.apply) for (int j = 0; j < NJ; ++j) bv[j] = b[tid + 256 * j]; for (int j = 0; j < NJ; ++j) wv[j] = w[tid + 256 * j]; const float g = a.apply ? 2.0f / (1.0f + exp(-inj[t * a.hc + c] / float(a.hc))) : 0.0f; float ss = 0.0f; for (int j = 0; j < NJ; ++j) { float v = rv[j]; if (a.apply) { v = fma(bv[j], g, v); rv[j] = v; } ss = fma(v, v, ss); } if (a.apply) for (int j = 0; j < NJ; ++j) r[tid + 256 * j] = rv[j]; ss = simd_sum(ss); if (lane == 0) part[sg] = ss; threadgroup_barrier(mem_flags::mem_threadgroup); float tot = 0.0f; for (int i = 0; i < 8; ++i) tot += part[i]; const float rs = rsqrt(tot / float(n) + a.eps); device float* o = xn + (t * a.hc + c) * n; for (int j = 0; j < NJ; ++j) o[tid + 256 * j] = rv[j] * wv[j] * rs; } // rows: 0 .. hc_lr - 1 = w_down, hc_lr .. hc_lr + hc - 1 = w_inject (both bf16 rows of hc * n_embd). // SG simdgroups split the row's units KS ways; LPR = 32 (a simdgroup per row); RS = SG / KS rows per threadgroup. // one row's K-split partial over xn (units of format F) template static inline void grd_acc(device const uchar* w, int nin, int ks, int KS, ushort lane, device const float* xn, thread float* acc, threadgroup const uint* notab) { constexpr int UNIT = fmt::UNIT; const int U = nin / UNIT, u0 = ks * U / KS, u1 = (ks + 1) * U / KS; for (int u = u0 + lane; u < u1; u += 32) { float q[UNIT]; float4 sc; Dec::run(w, u, q, sc, notab); for (int t = 0; t < GR_NTOK; ++t) { float xs[UNIT], xsum[2] = {0.0f, 0.0f}; for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * u + k, xs + k); acc[t] += unit_combine(q, sc, xs, xsum); } } } // 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 // 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. template static inline void grd_acc_p(device const uchar* w, int nin, int ks, int KS, ushort lane, device const float* xn, thread float* acc, threadgroup const uint* notab) { constexpr int UNIT = fmt::UNIT; const int U = nin / UNIT, u0 = ks * U / KS, u1 = (ks + 1) * U / KS; int u = u0 + lane; for (; u + 32 < u1; u += 64) { float qa[UNIT], qb[UNIT]; float4 sa, sb; Dec::run(w, u, qa, sa, notab); Dec::run(w, u + 32, qb, sb, notab); for (int t = 0; t < GR_NTOK; ++t) { float xa[UNIT], xb[UNIT], xsum[2] = {0.0f, 0.0f}; for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * u + k, xa + k); for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * (u + 32) + k, xb + k); acc[t] += unit_combine(qa, sa, xa, xsum); acc[t] += unit_combine(qb, sb, xb, xsum); } } for (; u < u1; u += 32) { float q[UNIT]; float4 sc; Dec::run(w, u, q, sc, notab); for (int t = 0; t < GR_NTOK; ++t) { float xs[UNIT], xsum[2] = {0.0f, 0.0f}; for (int k = 0; k < UNIT; k += 16) load16(xn + t * nin + UNIT * u + k, xs + k); acc[t] += unit_combine(q, sc, xs, xsum); } } } // FI: the inject rows' format (the Unsloth files: Q8_0 down, F32 inject); FI = F is the original kernel template kernel void gr_down_f(device const uchar* w_down [[buffer(0)]], device const uchar* w_inject [[buffer(1)]], device const float* xn [[buffer(2)]], device float* lo [[buffer(3)]], device float* inject [[buffer(4)]], constant GrArgs& a [[buffer(5)]], uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { constexpr int RS = SG / KS; threadgroup float red[KS * RS * 8]; threadgroup uint notab[1]; const int ks = sg / RS, rs = sg % RS; const int row = int(tg.x) * RS + rs; const int nrows = int(a.hc_lr + a.hc); const int rr = min(row, nrows - 1); const int nin = int(a.hc * a.n_embd); const uint rb = uint(nin / fmt::QK * fmt::BB), rbi = uint(nin / fmt::QK * fmt::BB); float acc[8]; for (int t = 0; t < 8; ++t) acc[t] = 0.0f; if (PF) { if (rr < int(a.hc_lr)) grd_acc_p(w_down + ulong(rr) * rb, nin, ks, KS, lane, xn, acc, notab); else grd_acc_p(w_inject + ulong(rr - int(a.hc_lr)) * rbi, nin, ks, KS, lane, xn, acc, notab); } else { if (rr < int(a.hc_lr)) grd_acc(w_down + ulong(rr) * rb, nin, ks, KS, lane, xn, acc, notab); else grd_acc(w_inject + ulong(rr - int(a.hc_lr)) * rbi, nin, ks, KS, lane, xn, acc, notab); } for (int t = 0; t < GR_NTOK; ++t) { const float v = simd_sum(acc[t]); if (lane == 0) red[(ks * RS + rs) * 8 + t] = v; } threadgroup_barrier(mem_flags::mem_threadgroup); if (ks == 0 && lane < GR_NTOK && row < nrows) { const int t = lane; float v = 0.0f; for (int k = 0; k < KS; ++k) v += red[(k * RS + rs) * 8 + t]; if (row < int(a.hc_lr)) { const float z = v / float(a.hc); lo[t * int(a.hc_lr) + row] = z / (1.0f + exp(-z)); } else { inject[t * int(a.hc) + row - int(a.hc_lr)] = v; } } } // A threadgroup owns 16 consecutive d for all hc = 4 streams: 8 simdgroups, simdgroup s -> stream s / 2 and // d0 + 8 (s % 2) .. + 8; LPR = 4 lanes per row (320 = 20 units of 16, 5 per lane). // XP: [4 kinds][n_tok][n] then S16 [n_tok][n / 16] (units.metal's xprep layout). template kernel void gr_up_f(device const uchar* w_up [[buffer(0)]], device const float* lo [[buffer(1)]], device const float* xn [[buffer(2)]], device float* xp [[buffer(3)]], constant GrArgs& a [[buffer(4)]], uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) { threadgroup float gate[8][4][16]; threadgroup float msum[8][16]; threadgroup uint notab[1]; const int n = int(a.n_embd), c = sg >> 1, dl = 8 * (sg & 1) + lane / 4, li = lane & 3; const int d = int(tg.x) * 16 + dl; constexpr int UNIT = fmt::UNIT; device const uchar* w = w_up + ulong(c * n + d) * (a.hc_lr / fmt::QK * fmt::BB); const int U = int(a.hc_lr) / UNIT; float acc[8]; for (int t = 0; t < 8; ++t) acc[t] = 0.0f; if (PF) { // 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 constexpr int NU = GR_NU; float q[NU][UNIT]; float4 sc[NU]; for (int j = 0; j < NU; ++j) if (li + 4 * j < U) Dec::run(w, li + 4 * j, q[j], sc[j], notab); for (int t = 0; t < GR_NTOK; ++t) for (int j = 0; j < NU; ++j) if (li + 4 * j < U) { float xs[UNIT], xsum[2] = {0.0f, 0.0f}; for (int k = 0; k < UNIT; k += 16) load16(lo + t * int(a.hc_lr) + UNIT * (li + 4 * j) + k, xs + k); acc[t] += unit_combine(q[j], sc[j], xs, xsum); } } else for (int u = li; u < U; u += 4) { float q[UNIT]; float4 sc; Dec::run(w, u, q, sc, notab); for (int t = 0; t < GR_NTOK; ++t) { float xs[UNIT], xsum[2] = {0.0f, 0.0f}; for (int k = 0; k < UNIT; k += 16) load16(lo + t * int(a.hc_lr) + UNIT * u + k, xs + k); acc[t] += unit_combine(q, sc, xs, xsum); } } for (int t = 0; t < GR_NTOK; ++t) { const float v = lane_sum<4>(acc[t]); if (li == 0) gate[t][c][dl] = 1.0f / (1.0f + exp(-v)); } threadgroup_barrier(mem_flags::mem_threadgroup); // 16 threads per token: thread j -> d0 + j const int t = tid / 16, j = tid & 15; if (t < GR_NTOK) { const int dd = int(tg.x) * 16 + j; const int hc = int(a.hc); float m = 0.0f; for (int cc = 0; cc < hc; ++cc) m = fma(xn[(t * hc + cc) * n + dd], gate[t][cc][j], m); m = m / float(hc); const uint plane = a.n_tok * uint(n); device float* raw = xp + t * n + dd; const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f}; const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f}; raw[0] = m; raw[plane] = m * q2s[j & 7]; raw[2 * plane] = m * p4s[j & 3]; raw[3 * plane] = (j & 1) ? m * 0.00390625f : m; if (xh16) put_x16(xp, plane, t * n + dd, j, m); msum[t][j] = m; } threadgroup_barrier(mem_flags::mem_threadgroup); if (t < GR_NTOK && j == 0) { // the per-16 sum in xprep's order (even and odd partial sums, then added) float s0 = 0.0f, s1 = 0.0f; for (int k = 0; k < 16; k += 2) { s0 += msum[t][k]; s1 += msum[t][k + 1]; } xp[4 * a.n_tok * uint(n) + t * (n / 16) + tg.x] = s0 + s1; } } #define GRD(S, K) template [[host_name("gr_down_" #S "_" #K)]] kernel decltype(gr_down_f) gr_down_f; \ template [[host_name("gr_down_q8_" #S "_" #K)]] kernel decltype(gr_down_f) gr_down_f; \ template [[host_name("gr_down_q8f_" #S "_" #K)]] kernel decltype(gr_down_f) gr_down_f; GRD(8, 8) GRD(8, 4) GRD(4, 4) GRD(4, 2) GRD(16, 16) GRD(16, 8) template [[host_name("gr_up")]] kernel decltype(gr_up_f) gr_up_f; template [[host_name("gr_up_q8")]] kernel decltype(gr_up_f) gr_up_f; // the prefetching variants (the engine's default; STRATA_METAL_HCLD=0 keeps the originals above) template [[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>; template [[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>; template [[host_name("gr_upp_q8")]] kernel decltype(gr_up_f) gr_up_f;