diff --git a/engine/src/ling32/metal/ling32_decode.metal b/engine/src/ling32/metal/ling32_decode.metal index 356d525623..12a6b64272 100644 --- a/engine/src/ling32/metal/ling32_decode.metal +++ b/engine/src/ling32/metal/ling32_decode.metal @@ -332,6 +332,82 @@ kernel void alflb_route2(device const float* logits [[buffer(0)]], device const } } +// The same router with 1024 threads per token (decode / verify rows): the group selection by rank (8 threads instead of one serial loop: the same groups in the same order, ties to the lower index), and the candidate ranks counted by four +// threads each over a quarter of the candidates (integer counts: any order is exact). Requires NCAND = 256. Same results as alflb_route2. +kernel void alflb_route2w(device const float* logits [[buffer(0)]], device const float* sel_bias [[buffer(1)]], device int* out_idx [[buffer(2)]], device float* out_w [[buffer(3)]], + uint t [[threadgroup_position_in_grid]], uint tid [[thread_index_in_threadgroup]]) { + constexpr int E = L32_E, per = L32_E / L32_NGROUP, top_n = L32_TOPK, NCAND = L32_TOPKG * (L32_E / L32_NGROUP); + static_assert(NCAND == 256, "alflb_route2w needs 256 candidates"); + threadgroup float sig[E], sc[E]; + threadgroup float4 scc4[NCAND / 4]; + threadgroup int4 eid4[NCAND / 4]; + threadgroup float* scc = (threadgroup float*)scc4; + threadgroup int* eid = (threadgroup int*)eid4; + threadgroup int chosen[L32_TOPK], glist[L32_NGROUP], rpart[4 * NCAND]; + threadgroup float gscore[L32_NGROUP]; + device const float* lg = logits + (ulong)t * E; + const uint lane = tid & 31, warp = tid >> 5; + for (int e = int(tid); e < E; e += 1024) { + sig[e] = sigmoidf_(lg[e]); + sc[e] = sig[e] + sel_bias[e]; + } + threadgroup_barrier(mem_flags::mem_threadgroup); + for (int g = int(warp); g < L32_NGROUP; g += 32) { + float m1 = -FLT_MAX, m2 = -FLT_MAX; + for (int i = int(lane); i < per; i += 32) { + const float vv = sc[g * per + i]; + if (vv > m1) { m2 = m1; m1 = vv; } else if (vv > m2) m2 = vv; + } + for (ushort o = 16; o >= 1; o >>= 1) { + const float b1 = simd_shuffle_xor(m1, o), b2 = simd_shuffle_xor(m2, o); + const float n1 = max(m1, b1); + m2 = max(min(m1, b1), max(m2, b2)); + m1 = n1; + } + if (lane == 0) gscore[g] = m1 + m2; + } + threadgroup_barrier(mem_flags::mem_threadgroup); + if (tid < L32_NGROUP) { // the k-th kept group is the one with k groups ahead of it (a higher score, or the same score and a lower index) + int r = 0; + for (int g2 = 0; g2 < L32_NGROUP; ++g2) r += (gscore[g2] > gscore[tid] || (gscore[g2] == gscore[tid] && g2 < int(tid))) ? 1 : 0; + if (r < L32_TOPKG) glist[r] = int(tid); + } + threadgroup_barrier(mem_flags::mem_threadgroup); + if (tid < NCAND) { + const int e = glist[tid / per] * per + int(tid) % per; + eid[tid] = e; + scc[tid] = sc[e]; + } + threadgroup_barrier(mem_flags::mem_threadgroup); + { + const int c = int(tid) & (NCAND - 1), q = int(tid) / NCAND; + const float vv = scc[c]; + const int e = eid[c]; + int r = 0; + for (int f = q * (NCAND / 16); f < (q + 1) * (NCAND / 16); ++f) { + const float4 sv = scc4[f]; + const int4 ev = eid4[f]; + r += (sv.x > vv || (sv.x == vv && ev.x < e)) ? 1 : 0; + r += (sv.y > vv || (sv.y == vv && ev.y < e)) ? 1 : 0; + r += (sv.z > vv || (sv.z == vv && ev.z < e)) ? 1 : 0; + r += (sv.w > vv || (sv.w == vv && ev.w < e)) ? 1 : 0; + } + rpart[q * NCAND + c] = r; + } + threadgroup_barrier(mem_flags::mem_threadgroup); + if (tid < NCAND) { + const int r = rpart[tid] + rpart[NCAND + tid] + rpart[2 * NCAND + tid] + rpart[3 * NCAND + tid]; + if (r < top_n) chosen[r] = eid[tid]; + } + threadgroup_barrier(mem_flags::mem_threadgroup); + if (int(tid) < top_n) { + float tot = 0.0f; + for (int i = 0; i < top_n; ++i) tot += sig[chosen[i]]; + out_idx[(ulong)t * top_n + tid] = chosen[tid]; + out_w[(ulong)t * top_n + tid] = sig[chosen[tid]] * (L32_SCALE / (tot + 1e-20f)); + } +} + // split-K attention for FOUR heads per threadgroup: every cache row is read once for the four queries (the rows are the same for all heads). grid (N, H / 4, S), 256 threads, kc <= 256 kernel void mla_attn_part4(device const float* qabs [[buffer(0)]], device const float* q [[buffer(1)]], device const float* cache [[buffer(2)]], device const int* d_pos0 [[buffer(3)]], device float* part [[buffer(4)]], constant float& scale [[buffer(5)]], constant int& S [[buffer(6)]], constant int& kc [[buffer(7)]], constant int& reg_lo [[buffer(8)]], constant int& reg_hi [[buffer(9)]], diff --git a/engine/src/ling32/metal/ling32_metal_main.cpp b/engine/src/ling32/metal/ling32_metal_main.cpp index 462ad0fcef..69c3840769 100644 --- a/engine/src/ling32/metal/ling32_metal_main.cpp +++ b/engine/src/ling32/metal/ling32_metal_main.cpp @@ -669,7 +669,11 @@ struct Engine { NB(t, N); gemv(t, L.su, R(cur), N, R(u), c.I_shared, false); if (N > 16 && fast_f32 && route_tol > 0.0f) t.add("alflb_route2x", {B(R(rlog)), B(ar(L.gate_bias)), B(R(ridx)), B(R(rw)), B(ar(L.gate_w)), B(R(cur)), Fl(route_tol)}, N, 1, 1, 256); // near ties: exact logits - else t.add("alflb_route2", {B(R(rlog)), B(ar(L.gate_bias)), B(R(ridx)), B(R(rw))}, N, 1, 1, 256); + else { + static const bool route_w = !std::getenv("L32_ROUTE_W") || std::atoi(std::getenv("L32_ROUTE_W")) != 0; // decode / verify rows (<= 4): 1024 threads per token, same selection; L32_ROUTE_W=0: the plain kernel + if (route_w && N <= 4) t.add("alflb_route2w", {B(R(rlog)), B(ar(L.gate_bias)), B(R(ridx)), B(R(rw))}, N, 1, 1, 1024); + else t.add("alflb_route2", {B(R(rlog)), B(ar(L.gate_bias)), B(R(ridx)), B(R(rw))}, N, 1, 1, 256); + } if (trace_row >= 0) { const size_t base_w = size_t(il) * trace_stride() + D + c.n_exp; t.add("copy_u32", {B(R(ridx, size_t(trace_row) * c.top_k * 4)), B(R(trace, base_w * 4)), I(c.top_k)}, 1, 1, 1, 32);