From 2cf3fd181f197caa84f4c3b19907dd1bb106957d Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 30 Oct 2026 01:22:28 +0100 Subject: [PATCH] Metal: gdn_scan_f (scan + gdn_post fused, bit-identical, slower: kept as a measured negative), bench --- metal/kernels/gdn_prompt.metal | 101 ++++++++++++++++++++++++++++++++ metal/tests/gdn_chunk_bench.cpp | 39 ++++++++++++ 2 files changed, 140 insertions(+) diff --git a/metal/kernels/gdn_prompt.metal b/metal/kernels/gdn_prompt.metal index 31bb0ce4..fc906180 100644 --- a/metal/kernels/gdn_prompt.metal +++ b/metal/kernels/gdn_prompt.metal @@ -328,6 +328,107 @@ template [[host_name("gdn_scanr_8_4")]] kernel decltype(gdn_scan_r<8, 4>) gdn_sc template [[host_name("gdn_scanr_2_8")]] kernel decltype(gdn_scan_r<2, 8>) gdn_scan_r<2, 8>; template [[host_name("gdn_scanr_2_4")]] kernel decltype(gdn_scan_r<2, 4>) gdn_scan_r<2, 4>; +// ---- gdn_scanf: gdn_scan_r<4, TB> with gdn_post folded in. One threadgroup a value head (grid h_v), 16 simdgroups = +// 4 column blocks x 4 row blocks, 512 threads. The scan is gdn_scan_r's, token for token (the same operations in the +// same order, so o is the same bits); a column block's row-block-0 simdgroup holds o for its 32 columns, writes them and +// their simd_sum of squares to threadgroup memory, and once every TB tokens the whole threadgroup runs gdn_post's +// arithmetic on those tokens (the sum of the four partials in gdn_post's order: the same bits as gdn_post). Outputs: +// y (fp16) and, with `f32`, fp32 y; o itself never reaches device memory. +template +kernel void gdn_scan_f(device float* state [[buffer(0)]], device const float4* qn [[buffer(1)]], + device const float4* kn [[buffer(2)]], device const float* vc [[buffer(3)]], + device const float2* gb [[buffer(4)]], device const float* kq [[buffer(5)]], + device const float* z [[buffer(6)]], device const float* gamma [[buffer(7)]], + device half* yh [[buffer(8)]], device float* y32 [[buffer(9)]], constant GdnPArgs& a [[buffer(10)]], + uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], + ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { + constexpr int S = 128, IB = 4, R = S / IB, R4 = R / 4; + threadgroup float4 qs[TB][32], ks[TB][32]; + threadgroup float2 red[2][16][32]; + threadgroup float vs[TB][128]; + threadgroup float4 gk[TB]; + threadgroup float ot[TB][128]; + threadgroup float osq[TB][4]; + const int h = int(tg.x), hk = h % int(a.h_k), hv = int(a.h_v), hkn = int(a.h_k); + const int cb = sg >> 2, ib = sg & 3; + const int j = cb * 32 + lane, i0 = ib * R; + const int T = int(a.n_tok); + const float scale = 1.0f / sqrt(float(S)); + float st[R]; +#pragma clang loop unroll(full) + for (int i = 0; i < R; ++i) st[i] = state[((i0 + i) * hv + h) * S + j]; + for (int t0 = 0; t0 < T; t0 += TB) { + const int nb = min(TB, T - t0); + threadgroup_barrier(mem_flags::mem_threadgroup); + for (int idx = tid; idx < nb * 32; idx += 512) { + const int tt = idx >> 5, w = idx & 31; + qs[tt][w] = qn[((t0 + tt) * hkn + hk) * 32 + w]; + ks[tt][w] = kn[((t0 + tt) * hkn + hk) * 32 + w]; + } + for (int idx = tid; idx < nb * 128; idx += 512) { + const int tt = idx >> 7, w = idx & 127; + vs[tt][w] = vc[((t0 + tt) * hv + h) * S + w]; + } + for (int tt = tid; tt < nb; tt += 512) { + const float2 g_b = gb[(t0 + tt) * hv + h]; + gk[tt] = float4(g_b.x, g_b.y, kq[(t0 + tt) * hkn + hk], 0.0f); + } + threadgroup_barrier(mem_flags::mem_threadgroup); + for (int tt = 0; tt < nb; ++tt) { + const int t = t0 + tt, slot = t & 1; + float ka = 0.0f, qa = 0.0f; +#pragma clang loop unroll(full) + for (int w = 0; w < R4; ++w) { + const float4 k4 = ks[tt][i0 / 4 + w], q4 = qs[tt][i0 / 4 + w]; + ka = fma(st[4 * w], k4.x, ka); ka = fma(st[4 * w + 1], k4.y, ka); + ka = fma(st[4 * w + 2], k4.z, ka); ka = fma(st[4 * w + 3], k4.w, ka); + qa = fma(st[4 * w], q4.x, qa); qa = fma(st[4 * w + 1], q4.y, qa); + qa = fma(st[4 * w + 2], q4.z, qa); qa = fma(st[4 * w + 3], q4.w, qa); + } + const float4 g_b = gk[tt]; + const float v = vs[tt][j]; + red[slot][sg][lane] = float2(ka, qa); + threadgroup_barrier(mem_flags::mem_threadgroup); + float2 s = red[slot][cb * 4][lane]; +#pragma clang loop unroll(full) + for (int b = 1; b < IB; ++b) s += red[slot][cb * 4 + b][lane]; + const float g = g_b.x; + const float delta = (v - g * s.x) * g_b.y; + if (ib == 0) { + const float ov = fma(g, s.y, delta * g_b.z) * scale; + ot[tt][j] = ov; + const float ss = simd_sum(ov * ov); + if (lane == 0) osq[tt][cb] = ss; + } +#pragma clang loop unroll(full) + for (int w = 0; w < R4; ++w) { + const float4 k4 = ks[tt][i0 / 4 + w]; + st[4 * w] = fma(g, st[4 * w], k4.x * delta); + st[4 * w + 1] = fma(g, st[4 * w + 1], k4.y * delta); + st[4 * w + 2] = fma(g, st[4 * w + 2], k4.z * delta); + st[4 * w + 3] = fma(g, st[4 * w + 3], k4.w * delta); + } + } + threadgroup_barrier(mem_flags::mem_threadgroup); + // gdn_post for these tokens + for (int idx = tid; idx < nb * 128; idx += 512) { + const int tt = idx >> 7, c = idx & 127; + const float tot = (osq[tt][0] + osq[tt][1]) + (osq[tt][2] + osq[tt][3]); + const float rs = rsqrt(tot / 128.0f + a.eps); + const ulong i = ulong((t0 + tt) * hv + h) * 128 + c; + const float ov = ot[tt][c]; + const float yv = ((rs * ov) * gamma[c]) * (1.0f / (1.0f + exp(-z[i]))); + yh[i] = half(yv); + if (a.f32) y32[i] = yv; + } + } +#pragma clang loop unroll(full) + for (int i = 0; i < R; ++i) state[((i0 + i) * hv + h) * S + j] = st[i]; +} +template [[host_name("gdn_scanf_8")]] kernel decltype(gdn_scan_f<8>) gdn_scan_f<8>; +template [[host_name("gdn_scanf_4")]] kernel decltype(gdn_scan_f<4>) gdn_scan_f<4>; +template [[host_name("gdn_scanf_2")]] kernel decltype(gdn_scan_f<2>) gdn_scan_f<2>; + // ---- the chunked form (chunks of GC = 32 tokens), the WY / UT form of the gated delta rule. With G_t the product // of the chunk's gates up to t (log space: lg_t) and S0 the state entering the chunk: // (I + A) D = B, A_ts = beta_t (k_t . k_s) G_t / G_s (s < t), B = [beta v | beta G k] -> D = [U | W] diff --git a/metal/tests/gdn_chunk_bench.cpp b/metal/tests/gdn_chunk_bench.cpp index 870baa9c..2ec86f45 100644 --- a/metal/tests/gdn_chunk_bench.cpp +++ b/metal/tests/gdn_chunk_bench.cpp @@ -135,6 +135,20 @@ int main(int argc, char** argv) { d.grid[0] = HV; d.grid[1] = n; d.tg[0] = 128; ds.push_back(d); }; + mtl::Pipeline pf[3] = {cx.pipeline(lib, "gdn_scanf_4"), cx.pipeline(lib, "gdn_scanf_8"), cx.pipeline(lib, "gdn_scanf_2")}; + std::printf("fused scan+post pipelines (max threads / static tg mem; 512 asked): TB4 %d/%d TB8 %d/%d TB2 %d/%d\n", pf[0].max_threads, + pf[0].static_tg_mem, pf[1].max_threads, pf[1].static_tg_mem, pf[2].max_threads, pf[2].static_tg_mem); + auto fused_d = [&](int which, int t0, int n, const mtl::Buffer& st, std::vector& ds) { + GdnPArgs a{(uint32_t)n, HK, HV, CH, 1e-6f, 1u}; + mtl::Dispatch d; + d.p = &pf[which]; + d.args = {mtl::Arg::B(st), mtl::Arg::B(qn, (size_t)t0 * HK * S * 4), mtl::Arg::B(kn, (size_t)t0 * HK * S * 4), + mtl::Arg::B(vc, (size_t)t0 * HV * S * 4), mtl::Arg::B(gb, (size_t)t0 * HV * 8), mtl::Arg::B(kq, (size_t)t0 * HK * 4), + mtl::Arg::B(z, (size_t)t0 * HV * S * 4), mtl::Arg::B(gam), mtl::Arg::B(yh, (size_t)t0 * HV * S * 2), + mtl::Arg::B(yB, (size_t)t0 * HV * S * 4), mtl::Arg::V(a)}; + d.grid[0] = HV; d.tg[0] = 512; + ds.push_back(d); + }; auto timeit = [&](const std::vector& ds) { cx.warm(ds, 0.3); std::vector t; @@ -249,6 +263,25 @@ int main(int argc, char** argv) { } } } + std::printf("== fused scan+post vs pre/scanr_4/post: y rel L2, fp16 y mismatches, state rel L2\n"); + for (int T : Ts) { + if (T > 8192) continue; + copy_st0(stX); + std::vector d0; scan_d(1, 0, T, stX, o, d0); post_d(0, T, o, d0); cx.run(d0); + std::vector yref(yB.as(), yB.as() + (size_t)T * HV * S), sref(stX.as(), stX.as() + (size_t)S * HV * S); + std::vector href(yh.as(), yh.as() + (size_t)T * HV * S); + for (int w = 0; w < 3; ++w) { + copy_st0(stX); + std::memset(yB.ptr, 0, (size_t)T * HV * S * 4); + std::vector d1; fused_d(w, 0, T, stX, d1); cx.run(d1); + double se = 0, sr = 0, ss = 0, ssr = 0; size_t hm = 0; + for (size_t i = 0; i < (size_t)T * HV * S; ++i) { const double e = yB.as()[i] - yref[i]; se += e * e; sr += (double)yref[i] * yref[i]; hm += yh.as()[i] != href[i]; } + for (size_t i = 0; i < (size_t)S * HV * S; ++i) { const double e = stX.as()[i] - sref[i]; ss += e * e; ssr += (double)sref[i] * sref[i]; } + const bool ok = std::sqrt(se / sr) < 1e-5 && std::sqrt(ss / ssr) < 1e-5; + fails += !ok; + std::printf("T %5d fused TB%-2d: y rel %.2e, fp16 mismatches %zu, state rel %.2e %s\n", T, w == 0 ? 4 : w == 1 ? 8 : 2, std::sqrt(se / sr), hm, std::sqrt(ss / ssr), ok ? "PASS" : "FAIL"); + } + } std::printf("%s\n", fails ? "SOME FAILED" : "ALL PASS"); } // timing: gdn_pre, the scan variants, chunked parts, gdn_post; the stage = pre + scan + post @@ -276,6 +309,12 @@ int main(int argc, char** argv) { const double tc = timeit(dc), tc1 = timeit({dc[0]}), tc2 = timeit({dc[1]}), tc3 = timeit({dc[2]}); const double base = tp + ts1 + tpo, ch = tp + tc + tpo; std::printf(" | %8.3f (%7.3f %7.3f %7.3f) | %8.3f %8.3f %5.2f | best %s stage %.3f = x%.3f (scan x%.3f)\n", tc, tc1, tc2, tc3, base, ch, base / ch, svs[wbest].nm, tp + tbest + tpo, base / (tp + tbest + tpo), ts1 / tbest); + { + double tf[3]; + for (int w = 0; w < 3; ++w) { std::vector d1; fused_d(w, 0, T, stX, d1); tf[w] = timeit(d1); } + const double fb = tp + std::min(tf[0], std::min(tf[1], tf[2])); + std::printf(" fused scan+post TB4 %.3f TB8 %.3f TB2 %.3f ms; stage pre+fused best %.3f = x%.3f vs scanr_4 stage\n", tf[0], tf[1], tf[2], fb, base / fb); + } std::fflush(stdout); } return fails ? 1 : 0;