okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3e0210663
main
1// metal/kernels/moeu.metal - moe.metal's decode gate/up and down kernels with the unit loop unrolled UF times: the weights of UF 2// units are decoded (their loads issued) before the first is applied. Each lane adds its units in the original order (u, u + LPR, 3// ...), so the sums are bitwise moe.metal's; a lane waits one memory latency for UF units instead of one for each. After moe.metal. 4// Names: moe_guu_<type>_<LPR>_<NR>_<SG>_<UF>, moe_dnu_<type>_<LPR>_<NR>_<SG>_<UF>. 5 6template <int F, int LPR, int NR, int NE, int UF> 7static inline void gu_body_u(device const uchar* g0, device const uchar* u0, device const float* x, device const float* xs16, 8 device float* h, device const int* etok, int c, int row0, int li, constant MoeArgs& a, 9 threadgroup const uint* tab) { 10 constexpr int UNIT = fmt<F>::UNIT; 11 float ag[NR][NE], au[NR][NE]; 12 for (int r = 0; r < NR; ++r) 13 for (int t = 0; t < NE; ++t) ag[r][t] = au[r][t] = 0.0f; 14 const int U = int(a.n_embd) / UNIT; 15 for (int u = li; u < U; u += LPR * UF) { 16 float qg[UF][NR][UNIT], qu[UF][NR][UNIT]; 17 float4 cg[UF][NR], cu[UF][NR]; 18 for (int k = 0; k < UF; ++k) 19 if (u + k * LPR < U) 20 for (int r = 0; r < NR; ++r) { 21 Dec<F>::run(g0 + r * a.gu_row, u + k * LPR, qg[k][r], cg[k][r], tab); 22 Dec<F>::run(u0 + r * a.gu_row, u + k * LPR, qu[k][r], cu[k][r], tab); 23 } 24 for (int k = 0; k < UF; ++k) 25 if (u + k * LPR < U) 26 for (int t = 0; t < NE; ++t) { 27 const int tok = etok[t]; 28 float xs[UNIT], xsum[2]; 29 X<F>::load(x + tok * int(a.n_embd), xs16 + tok * int(a.n_embd / 16), u + k * LPR, xs, xsum); 30 for (int r = 0; r < NR; ++r) { 31 ag[r][t] += unit_combine<UNIT>(qg[k][r], cg[k][r], xs, xsum); 32 au[r][t] += unit_combine<UNIT>(qu[k][r], cu[k][r], xs, xsum); 33 } 34 } 35 } 36 for (int r = 0; r < NR; ++r) 37 for (int t = 0; t < NE; ++t) { 38 const float gv = lane_sum<LPR>(ag[r][t]), uv = lane_sum<LPR>(au[r][t]); 39 if (li == 0) h[(c + t) * int(a.n_ff) + row0 + r] = gv / (1.0f + exp(-gv)) * uv; 40 } 41} 42 43template <int F, int LPR, int NR, int SG, int UF> 44kernel void moe_gu_u(device const uchar* W [[buffer(0)]], device const float* x [[buffer(1)]], 45 device float* h [[buffer(2)]], device const int* plan [[buffer(3)]], 46 constant MoeArgs& a [[buffer(4)]], device const float* xs16 [[buffer(5)]], 47 device const uint* smap [[buffer(6)]], device const uchar* W1 [[buffer(7)]], 48 device const uchar* W2 [[buffer(8)]], device const uchar* W3 [[buffer(9)]], 49 uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], 50 ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) { 51 constexpr int RG = 32 / LPR; 52 threadgroup uint tab[fmt<F>::TAB]; 53 if (moe_probe != 1) { 54 load_tab<F>(tab, tid, SG * 32); 55 threadgroup_barrier(mem_flags::mem_threadgroup); 56 } 57 const int g = int(tg.y); 58 if (g >= plan[P_NG]) return; 59 const int li = lane % LPR; 60 const int row0 = ((int(tg.x) * SG + sg) * RG + lane / LPR) * NR; 61 const ulong ex = (ulong)plan[P_GEXP + g]; 62 const int c = plan[P_GSTART + g], ne = plan[P_GSTART + g + 1] - c; 63 device const int* etok = plan + P_ETOK + c; 64 device const uchar* E = kArena ? arena_slot(smap[ex], W, W1, W2, W3) : W + ex * a.gu_stride; 65 device const uchar* g0 = E + a.gate_off + (ulong)row0 * a.gu_row; 66 device const uchar* u0 = E + a.up_off + (ulong)row0 * a.gu_row; 67 switch (ne) { 68 case 1: gu_body_u<F, LPR, NR, 1, UF>(g0, u0, x, xs16, h, etok, c, row0, li, a, tab); break; 69 case 2: gu_body_u<F, LPR, NR, 2, UF>(g0, u0, x, xs16, h, etok, c, row0, li, a, tab); break; 70 case 3: gu_body_u<F, LPR, NR, 3, UF>(g0, u0, x, xs16, h, etok, c, row0, li, a, tab); break; 71 case 4: gu_body_u<F, LPR, NR, 4, UF>(g0, u0, x, xs16, h, etok, c, row0, li, a, tab); break; 72 default: 73 for (int k = 0; k < ne; k += 4) { 74 const int m = min(4, ne - k); 75 if (m == 4) gu_body_u<F, LPR, NR, 4, UF>(g0, u0, x, xs16, h, etok + k, c + k, row0, li, a, tab); 76 else if (m == 3) gu_body_u<F, LPR, NR, 3, UF>(g0, u0, x, xs16, h, etok + k, c + k, row0, li, a, tab); 77 else if (m == 2) gu_body_u<F, LPR, NR, 2, UF>(g0, u0, x, xs16, h, etok + k, c + k, row0, li, a, tab); 78 else gu_body_u<F, LPR, NR, 1, UF>(g0, u0, x, xs16, h, etok + k, c + k, row0, li, a, tab); 79 } 80 } 81} 82 83template <int F, int LPR, int NR, int NE, int UF> 84static inline void dn_body_u(device const uchar* d0, device const float* h, device const float* hs16, device float* ye, 85 device const int* plan, int c, int row0, int li, constant MoeArgs& a, threadgroup const uint* tab) { 86 constexpr int UNIT = fmt<F>::UNIT; 87 float ac[NR][NE]; 88 for (int r = 0; r < NR; ++r) 89 for (int t = 0; t < NE; ++t) ac[r][t] = 0.0f; 90 const int U = int(a.n_ff) / UNIT; 91 for (int u = li; u < U; u += LPR * UF) { 92 float q[UF][NR][UNIT]; 93 float4 sc[UF][NR]; 94 for (int k = 0; k < UF; ++k) 95 if (u + k * LPR < U) 96 for (int r = 0; r < NR; ++r) Dec<F>::run(d0 + r * a.d_row, u + k * LPR, q[k][r], sc[k][r], tab); 97 for (int k = 0; k < UF; ++k) 98 if (u + k * LPR < U) 99 for (int t = 0; t < NE; ++t) { 100 float xs[UNIT], xsum[2]; 101 X<F>::load(h + (c + t) * int(a.n_ff), hs16 + (c + t) * int(a.n_ff / 16), u + k * LPR, xs, xsum); 102 for (int r = 0; r < NR; ++r) ac[r][t] += unit_combine<UNIT>(q[k][r], sc[k][r], xs, xsum); 103 } 104 } 105 for (int r = 0; r < NR; ++r) 106 for (int t = 0; t < NE; ++t) { 107 const float v = lane_sum<LPR>(ac[r][t]); 108 if (li == 0) ye[(c + t) * int(a.n_embd) + row0 + r] = as_type<float>(plan[P_EW + c + t]) * v; 109 } 110} 111 112template <int F, int LPR, int NR, int SG, int UF> 113kernel void moe_down_u(device const uchar* W [[buffer(0)]], device const float* h [[buffer(1)]], 114 device float* ye [[buffer(2)]], device const int* plan [[buffer(3)]], 115 constant MoeArgs& a [[buffer(4)]], device const float* hs16 [[buffer(5)]], 116 device const uint* smap [[buffer(6)]], device const uchar* W1 [[buffer(7)]], 117 device const uchar* W2 [[buffer(8)]], device const uchar* W3 [[buffer(9)]], 118 uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], 119 ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) { 120 constexpr int RG = 32 / LPR; 121 threadgroup uint tab[fmt<F>::TAB]; 122 load_tab<F>(tab, tid, SG * 32); 123 threadgroup_barrier(mem_flags::mem_threadgroup); 124 const int g = int(tg.y); 125 if (g >= plan[P_NG]) return; 126 const int li = lane % LPR; 127 const int row0 = ((int(tg.x) * SG + sg) * RG + lane / LPR) * NR; 128 const ulong ex = (ulong)plan[P_GEXP + g]; 129 const int c = plan[P_GSTART + g], ne = plan[P_GSTART + g + 1] - c; 130 device const uchar* d0 = (kArena ? arena_slot(smap[ex], W, W1, W2, W3) : W + ex * a.d_stride) + a.down_off + (ulong)row0 * a.d_row; 131 switch (ne) { 132 case 1: dn_body_u<F, LPR, NR, 1, UF>(d0, h, hs16, ye, plan, c, row0, li, a, tab); break; 133 case 2: dn_body_u<F, LPR, NR, 2, UF>(d0, h, hs16, ye, plan, c, row0, li, a, tab); break; 134 case 3: dn_body_u<F, LPR, NR, 3, UF>(d0, h, hs16, ye, plan, c, row0, li, a, tab); break; 135 case 4: dn_body_u<F, LPR, NR, 4, UF>(d0, h, hs16, ye, plan, c, row0, li, a, tab); break; 136 default: 137 for (int k = 0; k < ne; k += 4) { 138 const int m = min(4, ne - k); 139 if (m == 4) dn_body_u<F, LPR, NR, 4, UF>(d0, h, hs16, ye, plan, c + k, row0, li, a, tab); 140 else if (m == 3) dn_body_u<F, LPR, NR, 3, UF>(d0, h, hs16, ye, plan, c + k, row0, li, a, tab); 141 else if (m == 2) dn_body_u<F, LPR, NR, 2, UF>(d0, h, hs16, ye, plan, c + k, row0, li, a, tab); 142 else dn_body_u<F, LPR, NR, 1, UF>(d0, h, hs16, ye, plan, c + k, row0, li, a, tab); 143 } 144 } 145} 146 147#define GUU_(F, L, R, S, U) template [[host_name("moe_guu_" #F "_" #L "_" #R "_" #S "_" #U)]] kernel decltype(moe_gu_u<F, L, R, S, U>) moe_gu_u<F, L, R, S, U>; 148#define GUU(F, L, R, S, U) GUU_(F, L, R, S, U) 149#define DNU_(F, L, R, S, U) template [[host_name("moe_dnu_" #F "_" #L "_" #R "_" #S "_" #U)]] kernel decltype(moe_down_u<F, L, R, S, U>) moe_down_u<F, L, R, S, U>; 150#define DNU(F, L, R, S, U) DNU_(F, L, R, S, U) 151// the tiles the Q2_0 and IQ3_S packs use (window.cpp / token_chain.cpp pick 32,1,4 for gate/up; down 8,1,4 for Q2_0 and 4,2,4 otherwise) 152#define GUU_ALL(U) GUU(T_Q2_0, 32, 1, 4, U) GUU(T_IQ3_XXS, 32, 1, 4, U) GUU(T_IQ2_S, 32, 1, 4, U) GUU(T_IQ3_S, 32, 1, 4, U) \ 153 GUU(T_IQ4_XS, 32, 1, 4, U) 154#define DNU_ALL(U) DNU(T_Q2_0, 8, 1, 4, U) DNU(T_Q2_0R, 8, 1, 4, U) DNU(T_IQ4_NL, 4, 2, 4, U) DNU(T_Q8_0, 4, 2, 4, U) DNU(T_Q5_1, 4, 2, 4, U) DNU(T_Q5_0, 4, 2, 4, U) 155GUU_ALL(2) DNU_ALL(2)