okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
4.9 KiB94 linesraw
1// metal/src/engine/msl/mtp.metal - the small steps of the MTP draft layer's input branches (include/strata/core/mtp.hpp)
2// around mac-kern's kernels (GEMVs, the hyper-connection read, QSA, MoE, the head):
3//
4//   mtp_rms      y[r] = x[r] * rsqrt(mean(x[r]^2) + eps) * w        rows of width n (w: (1 + w) folded by the host)
5//   mtp_xprep    xprep (units.metal) of rows read with a stride: the prepared activation of one stream of rows
6//   mtp_combine  R[t][c][d] = h[c][t][d] + e[t][d]                  the streams of fc_hidden plus fc_embedding
7//
8// Prepared activations: [4 kinds][n_tok][n] then S16 [n_tok][n / 16] (units.metal's xprep layout).
9
10#include <metal_stdlib>
11using namespace metal;
12
13struct RmsArgs { uint n; float eps; };
14
15// grid (rows), 256 threads
16kernel void mtp_rms(device const float* x [[buffer(0)]], device const float* w [[buffer(1)]], device float* y [[buffer(2)]],
17                    constant RmsArgs& a [[buffer(3)]], uint3 tg [[threadgroup_position_in_grid]],
18                    ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]],
19                    ushort lane [[thread_index_in_simdgroup]]) {
20    threadgroup float part[8];
21    device const float* xr = x + tg.x * a.n;
22    float ss = 0.0f;
23    for (uint d = tid; d < a.n; d += 256) ss = fma(xr[d], xr[d], ss);
24    ss = simd_sum(ss);
25    if (lane == 0) part[sg] = ss;
26    threadgroup_barrier(mem_flags::mem_threadgroup);
27    float tot = 0.0f;
28    for (int i = 0; i < 8; ++i) tot += part[i];
29    const float rs = rsqrt(tot / float(a.n) + a.eps);
30    device float* yr = y + tg.x * a.n;
31    for (uint d = tid; d < a.n; d += 256) yr[d] = xr[d] * rs * w[d];
32}
33
34// mtp_rms with the row's NJ = n / 256 elements a thread loaded up front (the original's second loop stores y between the loads of the
35// next elements, so each iteration waited a memory latency): the same sum and the same products, in the same order.
36template <int NJ>
37kernel void mtp_rms_b(device const float* x [[buffer(0)]], device const float* w [[buffer(1)]], device float* y [[buffer(2)]],
38                      constant RmsArgs& a [[buffer(3)]], uint3 tg [[threadgroup_position_in_grid]],
39                      ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]],
40                      ushort lane [[thread_index_in_simdgroup]]) {
41    threadgroup float part[8];
42    device const float* xr = x + tg.x * a.n;
43    float xv[NJ], wv[NJ];
44    for (int j = 0; j < NJ; ++j) xv[j] = xr[tid + 256 * j];
45    for (int j = 0; j < NJ; ++j) wv[j] = w[tid + 256 * j];
46    float ss = 0.0f;
47    for (int j = 0; j < NJ; ++j) ss = fma(xv[j], xv[j], ss);
48    ss = simd_sum(ss);
49    if (lane == 0) part[sg] = ss;
50    threadgroup_barrier(mem_flags::mem_threadgroup);
51    float tot = 0.0f;
52    for (int i = 0; i < 8; ++i) tot += part[i];
53    const float rs = rsqrt(tot / float(a.n) + a.eps);
54    device float* yr = y + tg.x * a.n;
55    for (int j = 0; j < NJ; ++j) yr[tid + 256 * j] = xv[j] * rs * wv[j];
56}
57template [[host_name("mtp_rms_b10")]] kernel decltype(mtp_rms_b<10>) mtp_rms_b<10>;
58template [[host_name("mtp_rms_b40")]] kernel decltype(mtp_rms_b<40>) mtp_rms_b<40>;
59
60struct XpArgs { uint n; uint n_tok; uint plane_tok; uint stride; };
61
62// one thread per 16 values of one row: row t at x + t * stride; out planes of plane_tok rows
63kernel void mtp_xprep(device const float* x [[buffer(0)]], device float* xp [[buffer(1)]], constant XpArgs& a [[buffer(2)]],
64                      uint gid [[thread_position_in_grid]]) {
65    const uint per = a.n / 16;
66    if (gid >= per * a.n_tok) return;
67    const uint t = gid / per, i = gid % per;
68    device const float* src = x + t * a.stride + 16 * i;
69    const uint plane = a.plane_tok * a.n;
70    device float* raw = xp + t * a.n + 16 * i;
71    float v[16];
72    for (int j = 0; j < 16; ++j) v[j] = src[j];
73    float s0 = 0, s1 = 0;
74    for (int j = 0; j < 16; j += 2) { s0 += v[j]; s1 += v[j + 1]; }
75    const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f};
76    const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f};
77    for (int j = 0; j < 16; ++j) {
78        raw[j] = v[j];
79        raw[plane + j] = v[j] * q2s[j & 7];
80        raw[2 * plane + j] = v[j] * p4s[j & 3];
81        raw[3 * plane + j] = (j & 1) ? v[j] * 0.00390625f : v[j];
82    }
83    xp[4 * plane + t * per + i] = s0 + s1;
84}
85
86struct CombArgs { uint n; uint hc; uint n_tok; uint h_stride; };   // h: stream c's rows at h + c * h_stride
87
88kernel void mtp_combine(device const float* h [[buffer(0)]], device const float* e [[buffer(1)]], device float* R [[buffer(2)]],
89                        constant CombArgs& a [[buffer(3)]], uint gid [[thread_position_in_grid]]) {
90    const uint per = a.hc * a.n;
91    if (gid >= a.n_tok * per) return;
92    const uint t = gid / per, c = (gid % per) / a.n, d = gid % a.n;
93    R[gid] = h[c * a.h_stride + t * a.n + d] + e[t * a.n + d];
94}