okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/kernels/router.metal - the router GEMV and moe_route in ONE dispatch (after quant/units/moe/gemv.metal): the 2// logits rows as gemv's (LPR = 32 lanes a row, KS = 1: bitwise gemv_<F>_32_1_*_1's), 16 rows a threadgroup of 512 3// threads; every threadgroup bumps a device counter after its rows (device-scope fences around it), and the LAST one 4// runs moe_route's body (the softmax / top-10 / weights, then the plan) with its 512 threads (= n_expert) and resets 5// the counter. No threadgroup waits for another. ids / w / plan are bitwise moe_route's. 6// Buffers: W, xp, s16, logits, args (GemvArgs: n_out = n_expert), ids, w, plan, MoeArgs, counter (one uint, zero). 7 8static inline void route_body(device const float* logits, device int* ids, device float* w, device int* plan, 9 constant MoeArgs& a, ushort tid, ushort nth, ushort sg, ushort lane, threadgroup int* s_grp, 10 threadgroup int* s_ent) { 11 if (sg < a.n_tok) { // (route_tok: moe.metal; 512 experts: NL 16, the 256-expert Coder: NL 8) 12 if (a.n_expert == 256) route_tok<8>(logits + sg * 512, ids + sg * ROUTE_K, w + sg * ROUTE_K, lane); 13 else route_tok<16>(logits + sg * 512, ids + sg * ROUTE_K, w + sg * ROUTE_K, lane); 14 } 15 threadgroup_barrier(mem_flags::mem_device); 16 plan_body(ids, w, plan, a, tid, nth, sg, lane, s_grp, s_ent); 17} 18 19template <int F> 20kernel void router_route(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]], 21 device const float* s16 [[buffer(2)]], device float* logits [[buffer(3)]], 22 constant GemvArgs& ga [[buffer(4)]], device int* ids [[buffer(5)]], device float* w [[buffer(6)]], 23 device int* plan [[buffer(7)]], constant MoeArgs& a [[buffer(8)]], 24 device atomic_uint* counter [[buffer(9)]], 25 uint tgx [[threadgroup_position_in_grid]], uint ngx [[threadgroups_per_grid]], 26 ushort tid [[thread_index_in_threadgroup]], ushort nth [[threads_per_threadgroup]], 27 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 28 constexpr int UNIT = fmt<F>::UNIT; 29 threadgroup uint tab[fmt<F>::TAB]; 30 threadgroup int s_grp[32], s_ent[32]; 31 threadgroup uint last; 32 load_tab<F>(tab, tid, nth); 33 threadgroup_barrier(mem_flags::mem_threadgroup); 34 // the GEMV: simdgroup sg -> row tg.x * 16 + sg, 32 lanes over its units 35 const int row = int(tgx) * 16 + sg; 36 const int rr = min(row, int(ga.n_out) - 1); 37 device const uchar* rp = W + (ulong)rr * ga.row_bytes; 38 const int U = int(ga.n_in) / UNIT, s16n = int(ga.n_in) / 16; 39 float acc[8]; 40 for (int t = 0; t < NTOK; ++t) acc[t] = 0.0f; 41 for (int u = lane; u < U; u += 32) { 42 float q[UNIT]; 43 float4 sc; 44 Dec<F>::run(rp, u, q, sc, tab); 45 for (int t = 0; t < NTOK; ++t) { 46 float xs[UNIT], xsum[2]; 47 X<F>::load(xp + t * int(ga.x_stride), s16 + t * s16n, u, xs, xsum); 48 acc[t] += unit_combine<UNIT>(q, sc, xs, xsum); 49 } 50 } 51 for (int t = 0; t < NTOK; ++t) { 52 const float v = lane_sum<32>(acc[t]); 53 if (lane == 0 && row < int(ga.n_out)) logits[t * int(ga.y_stride) + row] = v; 54 } 55 // the last threadgroup to finish routes 56 atomic_thread_fence(mem_flags::mem_device, memory_order_seq_cst, thread_scope_device); 57 threadgroup_barrier(mem_flags::mem_device); 58 if (tid == 0) last = atomic_fetch_add_explicit(counter, 1u, memory_order_relaxed) == ngx - 1 ? 1u : 0u; 59 threadgroup_barrier(mem_flags::mem_threadgroup); 60 if (!last) return; 61 atomic_thread_fence(mem_flags::mem_device, memory_order_seq_cst, thread_scope_device); 62 route_body(logits, ids, w, plan, a, tid, nth, sg, lane, s_grp, s_ent); 63 if (tid == 0) atomic_store_explicit(counter, 0u, memory_order_relaxed); 64} 65 66template [[host_name("router_route_30")]] kernel decltype(router_route<T_BF16>) router_route<T_BF16>; 67template [[host_name("router_route_0")]] kernel decltype(router_route<T_F32>) router_route<T_F32>;