okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/kernels/prompt.metal - the elementwise steps of a prompt chunk around the GEMMs (after quant/units.metal): 2// 3// route_topk per token: softmax over the 512 logits, top 10 (ties: the lower expert), weights renormalised 4// over the selected with a 2^-14 floor - moe_route's rule; one simdgroup a token 5// gr_epi_down after the HC down GEMM (V = xn . [w_down; w_inject], T x (hc_lr + hc)): lo_h = half(silu(v / hc)), 6// inject = v 7// gr_epi_up after the HC up GEMM (G = lo . w_up, T x hc*n): mixed = mean_c xn[c] * sigmoid(G[c]) -> float and 8// half (the next GEMMs' operand) 9// swiglu_h h = half(silu(g) * u) 10// moe_out bo = y_routed + sigmoid(sg) * y_shared 11 12// one token by one simdgroup over NL * 32 logits (512 experts: NL 16; the 256-expert Coder: NL 8) - moe.metal's route_tok 13// experts per token (Flash-Next 10); the host prelude (engine/geo.h) defines GEO_TOPK for another shape 14#ifndef ROUTE_K 15#ifdef GEO_TOPK 16#define ROUTE_K GEO_TOPK 17#else 18#define ROUTE_K 10 19#endif 20#endif 21 22template <int NL> 23static inline void route_tok_p(device const float* l, device int* ids, device float* w, ushort lane) { 24 float v[NL]; 25 for (int i = 0; i < NL; ++i) v[i] = l[lane + 32 * i]; 26 float mx = -INFINITY; 27 for (int i = 0; i < NL; ++i) mx = max(mx, v[i]); 28 for (ushort m = 16; m > 0; m >>= 1) mx = max(mx, simd_shuffle_xor(mx, m)); 29 float sum = 0.0f; 30 for (int i = 0; i < NL; ++i) { 31 v[i] = exp(v[i] - mx); 32 sum += v[i]; 33 } 34 for (ushort m = 16; m > 0; m >>= 1) sum += simd_shuffle_xor(sum, m); 35 const float rcp = 1.0f / sum; 36 for (int i = 0; i < NL; ++i) { 37 v[i] *= rcp; 38 if (isnan(v[i])) v[i] = -FLT_MAX; 39 } 40 float selected = 0.0f, ssum = 0.0f; 41 for (int rank = 0; rank < ROUTE_K; ++rank) { 42 float best = v[0]; 43 int ex = lane; 44 for (int i = 1; i < NL; ++i) 45 if (v[i] > best) { best = v[i]; ex = lane + 32 * i; } 46 for (ushort m = 16; m > 0; m >>= 1) { 47 const float ob = simd_shuffle_xor(best, m); 48 const int oe = simd_shuffle_xor(ex, m); 49 if (ob > best || (ob == best && oe < ex)) { best = ob; ex = oe; } 50 } 51 if ((ex & 31) == lane) { 52 v[ex / 32] = -INFINITY; 53 ids[rank] = ex; 54 ssum += best; 55 } 56 if (rank == lane) selected = best; 57 } 58 for (ushort m = 16; m > 0; m >>= 1) ssum += simd_shuffle_xor(ssum, m); 59 ssum = max(ssum, 6.103515625e-5f); 60 if (lane < ROUTE_K) w[lane] = selected * (1.0f / ssum); 61} 62 63kernel void route_topk(device const float* logits [[buffer(0)]], device int* ids [[buffer(1)]], 64 device float* w [[buffer(2)]], constant uint& n_tok [[buffer(3)]], constant uint& n_exp [[buffer(4)]], 65 uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], 66 ushort lane [[thread_index_in_simdgroup]]) { 67 const uint t = tg.x * 8 + sg; 68 if (t >= n_tok) return; 69 device const float* l = logits + ulong(t) * n_exp; 70 if (n_exp == 256) route_tok_p<8>(l, ids + t * ROUTE_K, w + t * ROUTE_K, lane); 71 else route_tok_p<16>(l, ids + t * ROUTE_K, w + t * ROUTE_K, lane); 72} 73 74struct EpiArgs { 75 uint n_tok; 76 uint n_embd; // 2560 77 uint hc; // 4 78 uint hc_lr; // 320 79}; 80 81// grid (T * (hc_lr + hc) / 256) 82kernel void gr_epi_down(device const float* V [[buffer(0)]], device half* lo_h [[buffer(1)]], 83 device float* inject [[buffer(2)]], constant EpiArgs& a [[buffer(3)]], 84 uint gid [[thread_position_in_grid]]) { 85 const uint nr = a.hc_lr + a.hc, t = gid / nr, r = gid % nr; 86 if (t >= a.n_tok) return; 87 const float v = V[gid]; 88 if (r < a.hc_lr) { 89 const float z = v / float(a.hc); 90 lo_h[t * a.hc_lr + r] = half(z / (1.0f + exp(-z))); 91 } else { 92 inject[t * a.hc + r - a.hc_lr] = v; 93 } 94} 95 96// grid (T * n_embd / 256) 97kernel void gr_epi_up(device const float* G [[buffer(0)]], device const float* xn [[buffer(1)]], 98 device float* mixed [[buffer(2)]], device half* mixed_h [[buffer(3)]], 99 constant EpiArgs& a [[buffer(4)]], uint gid [[thread_position_in_grid]]) { 100 const uint n = a.n_embd, t = gid / n, d = gid % n; 101 if (t >= a.n_tok) return; 102 float m = 0.0f; 103 for (uint c = 0; c < a.hc; ++c) { 104 const float g = G[ulong(t) * a.hc * n + c * n + d]; 105 m = fma(xn[ulong(t) * a.hc * n + c * n + d], 1.0f / (1.0f + exp(-g)), m); 106 } 107 m = m / float(a.hc); 108 mixed[gid] = m; 109 mixed_h[gid] = half(m); 110} 111 112kernel void swiglu_h(device const float* g [[buffer(0)]], device const float* u [[buffer(1)]], device half* h [[buffer(2)]], 113 constant uint& n [[buffer(3)]], uint gid [[thread_position_in_grid]]) { 114 if (gid >= n) return; 115 const float gv = g[gid]; 116 h[gid] = half(gv / (1.0f + exp(-gv)) * u[gid]); 117} 118 119// grid (T * n_embd / 256); sg: T values 120kernel void moe_out(device const float* yr [[buffer(0)]], device const float* ys [[buffer(1)]], 121 device const float* sg [[buffer(2)]], device float* bo [[buffer(3)]], constant uint2& a [[buffer(4)]], 122 uint gid [[thread_position_in_grid]]) { 123 const uint t = gid / a.y; 124 if (t >= a.x) return; 125 bo[gid] = fma(1.0f / (1.0f + exp(-sg[t])), ys[gid], yr[gid]); 126} 127 128// ---- the HC read with fewer passes (after dense_mm.metal: GemmArgs, MetalPerformancePrimitives) --------------------- 129// gr_norm_h: gr_norm with xn written as fp16 only (the GEMM operand; gemm_hc_up's epilogue reads it too). 130// one threadgroup per (stream c, token t), 256 threads; args as gr_norm's (GrArgs, gr.metal) 131kernel void gr_norm_h(device float* R [[buffer(0)]], device const float* bo [[buffer(1)]], 132 device const float* inj [[buffer(2)]], device const float* w_norm [[buffer(3)]], 133 device half* xnh [[buffer(4)]], constant GrArgs& a [[buffer(5)]], 134 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 135 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 136 threadgroup float part[8]; 137 const uint c = tg.x, t = tg.y, n = a.n_embd; 138 device float* r = R + (t * a.hc + c) * n; 139 const float g = a.apply ? 2.0f / (1.0f + exp(-inj[t * a.hc + c] / float(a.hc))) : 0.0f; 140 float ss = 0.0f; 141 for (uint d = tid; d < n; d += 256) { 142 float v = r[d]; 143 if (a.apply) { 144 v = fma(bo[t * n + d], g, v); 145 r[d] = v; 146 } 147 ss = fma(v, v, ss); 148 } 149 ss = simd_sum(ss); 150 if (lane == 0) part[sg] = ss; 151 threadgroup_barrier(mem_flags::mem_threadgroup); 152 float tot = 0.0f; 153 for (int i = 0; i < 8; ++i) tot += part[i]; 154 const float rs = rsqrt(tot / float(n) + a.eps); 155 device half* o = xnh + (t * a.hc + c) * n; 156 device const float* w = w_norm + c * n; 157 for (uint d = tid; d < n; d += 256) o[d] = half(r[d] * w[d] * rs); 158} 159 160// gemm_hc_up: G = lo . w_up^T with w_up's rows interleaved by dq_f16 (perm = hc: row d * hc + c), so a TN-column 161// tile holds TN / hc outputs d with all their streams; the epilogue mixed[t][d] = mean_c xn[t][c][d] * 162// sigmoid(G[t][d * hc + c]) -> fp32 and fp16. G is never written. grid (hc * n / TN, ceil(T / TM)), 32 * SG threads. 163template <int TM, int TN, int SG> 164kernel void gemm_hc_up(device half* A [[buffer(0)]], device half* B [[buffer(1)]], device const half* xnh [[buffer(2)]], 165 device float* mixed [[buffer(3)]], device half* mixh [[buffer(4)]], constant GemmArgs& g [[buffer(5)]], 166 constant EpiArgs& e [[buffer(6)]], uint3 tg [[threadgroup_position_in_grid]], 167 ushort tid [[thread_index_in_threadgroup]]) { 168 threadgroup float ot[TM * TN]; 169 tensor<device half, dextents<int32_t, 2>, tensor_inline> tA(A, dextents<int32_t, 2>(g.K, g.M)); 170 tensor<device half, dextents<int32_t, 2>, tensor_inline> tB(B, dextents<int32_t, 2>(g.K, g.N)); 171 tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline> tO(ot, dextents<int32_t, 2>(TN, TM)); 172 constexpr auto desc = matmul2d_descriptor(TM, TN, dynamic_length_v<int>, false, true, false); 173 matmul2d<desc, execution_simdgroups<SG>> op; 174 auto mA = tA.slice(0, int(tg.y) * TM); 175 auto mB = tB.slice(0, int(tg.x) * TN); 176 op.run(mA, mB, tO); 177 threadgroup_barrier(mem_flags::mem_threadgroup); 178 const int hc = int(e.hc), n = int(e.n_embd), DPT = TN / hc; 179 const int t0 = int(tg.y) * TM, d0 = int(tg.x) * DPT; 180 for (int idx = tid; idx < TM * DPT; idx += SG * 32) { 181 const int m = idx / DPT, dd = idx - m * DPT, t = t0 + m; 182 if (t >= int(e.n_tok)) continue; 183 float s = 0.0f; 184 for (int c = 0; c < hc; ++c) 185 s = fma(float(xnh[ulong(t) * hc * n + c * n + d0 + dd]), 1.0f / (1.0f + exp(-ot[m * TN + dd * hc + c])), s); 186 s = s / float(hc); 187 mixed[ulong(t) * n + d0 + dd] = s; 188 mixh[ulong(t) * n + d0 + dd] = half(s); 189 } 190} 191template [[host_name("gemm_hc_up_64_64_4")]] kernel decltype(gemm_hc_up<64, 64, 4>) gemm_hc_up<64, 64, 4>; 192template [[host_name("gemm_hc_up_64_32_4")]] kernel decltype(gemm_hc_up<64, 32, 4>) gemm_hc_up<64, 32, 4>; 193template [[host_name("gemm_hc_up_32_64_4")]] kernel decltype(gemm_hc_up<32, 64, 4>) gemm_hc_up<32, 64, 4>;