okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/kernels/glue.metal - small elementwise steps between the big kernels (after quant/units/moe.metal). 2// 3// swiglu_prep h = silu(g) * u for the shared expert, written as prepared activations (xprep layout) 4// moe_combine_sh bo[t] = sum_k ye[entry(t, k)] + sigmoid(sg[t]) * ysh[t] (k in order) 5 6// experts per token (Flash-Next 10); the host prelude (engine/geo.h) defines GEO_TOPK for another shape 7#ifndef ROUTE_K 8#ifdef GEO_TOPK 9#define ROUTE_K GEO_TOPK 10#else 11#define ROUTE_K 10 12#endif 13#endif 14 15struct GlueArgs { 16 uint n; // vector width 17 uint n_tok; 18 uint n_embd; 19 uint topk; 20}; 21 22// grid (n / 16, n_tok), 16 threads: one group of 16 values 23kernel void swiglu_prep(device const float* g [[buffer(0)]], device const float* u [[buffer(1)]], 24 device float* xp [[buffer(2)]], constant GlueArgs& a [[buffer(3)]], 25 uint3 tg [[threadgroup_position_in_grid]], ushort j [[thread_index_in_threadgroup]]) { 26 threadgroup float v[16]; 27 const uint t = tg.y, d = tg.x * 16 + j, plane = a.n_tok * a.n; 28 const float gv = g[t * a.n + d]; 29 const float h = gv / (1.0f + exp(-gv)) * u[t * a.n + d]; 30 const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f}; 31 const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f}; 32 device float* raw = xp + t * a.n + d; 33 raw[0] = h; 34 raw[plane] = h * q2s[j & 7]; 35 raw[2 * plane] = h * p4s[j & 3]; 36 raw[3 * plane] = (j & 1) ? h * 0.00390625f : h; 37 if (xh16) put_x16(xp, plane, t * a.n + d, j, h); 38 v[j] = h; 39 threadgroup_barrier(mem_flags::mem_threadgroup); 40 if (j == 0) { 41 float s0 = 0.0f, s1 = 0.0f; 42 for (int k = 0; k < 16; k += 2) { s0 += v[k]; s1 += v[k + 1]; } 43 xp[4 * plane + t * (a.n / 16) + tg.x] = s0 + s1; 44 } 45} 46 47kernel void moe_combine_sh(device const float* ye [[buffer(0)]], device const int* plan [[buffer(1)]], 48 device const float* ysh [[buffer(2)]], device const float* sg [[buffer(3)]], 49 device float* bo [[buffer(4)]], constant GlueArgs& a [[buffer(5)]], 50 uint gid [[thread_position_in_grid]]) { 51 const uint t = gid / a.n_embd, k = gid % a.n_embd; 52 if (t >= a.n_tok) return; 53 float s = 0.0f; 54 for (uint j = 0; j < a.topk; ++j) s += ye[plan[P_TOKENT + t * a.topk + j] * int(a.n_embd) + k]; 55 const float gs = 1.0f / (1.0f + exp(-sg[t])); 56 bo[gid] = fma(gs, ysh[gid], s); 57} 58 59// moe_combine_sh with the ten experts' plan entries and rows loaded together before they are summed (topk 10): the same 60// sum in the same order, one memory latency for the plan and one for the rows instead of ten each. 61kernel void moe_combine_sh2(device const float* ye [[buffer(0)]], device const int* plan [[buffer(1)]], 62 device const float* ysh [[buffer(2)]], device const float* sg [[buffer(3)]], 63 device float* bo [[buffer(4)]], constant GlueArgs& a [[buffer(5)]], 64 uint gid [[thread_position_in_grid]]) { 65 constexpr int TK = ROUTE_K; 66 const uint t = gid / a.n_embd, k = gid % a.n_embd; 67 if (t >= a.n_tok) return; 68 int e[TK]; 69 for (int j = 0; j < TK; ++j) e[j] = plan[P_TOKENT + t * TK + j]; 70 const float gs = 1.0f / (1.0f + exp(-sg[t])); 71 const float sh = ysh[gid]; 72 float y[TK]; 73 for (int j = 0; j < TK; ++j) y[j] = ye[e[j] * int(a.n_embd) + k]; 74 float s = 0.0f; 75 for (int j = 0; j < TK; ++j) s += y[j]; 76 bo[gid] = fma(gs, sh, s); 77} 78 79// slc_touch: reads n16 16-byte words (a weight range the next kernels will need) so they sit in the SLC; the xor is 80// written only if it equals a value it never takes (keeps the loads alive). grid (ceil(n16 / 1024)), 256 threads, 81// 4 words a thread. 82kernel void slc_touch(device const uint4* src [[buffer(0)]], device uint* sink [[buffer(1)]], constant uint& n16 [[buffer(2)]], 83 uint gid [[thread_position_in_grid]]) { 84 uint s = 0; 85 for (uint k = 0; k < 4; ++k) { 86 const uint j = (gid >> 8) * 1024 + k * 256 + (gid & 255); 87 if (j < n16) { 88 const uint4 v = src[j]; 89 s ^= v.x ^ v.y ^ v.z ^ v.w; 90 } 91 } 92 if (s == 0x9e3779b9u) sink[0] = s; 93}