okami.mom/strata

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

Niko1221Merge f-029-par-metal (766b54afb)c0e245c8b

main
5.5 KiB103 linesraw
1// metal/src/engine/msl/ple.metal - the PLE block (include/strata/kernels/ple.hpp) after its two projections, for a
2// window of T rows, on the GPU (the CUDA engine's ple.cu math; its block sums are double there, float here):
3//
4//   key   = grouped_norm(pkey, norm_key)          per stream c of 4: x * rsqrt(mean(x^2) + eps) * w
5//   query = grouped_norm(R, norm_query)
6//   gate[c] = sigmoid(sign(s) sqrt(max(|s|, 1e-6))),  s = key[c] . query[c] / sqrt(n_embd)
7//   gated = value[d] * gate[c];   normalized = grouped_norm(gated, norm_conv)
8//   conv[c] = silu(sum_k kW[k + 4c] * row(t - (3 - k) * 3)[c])   rows: the history, then the window's own
9//   R += gated + conv
10//
11// History: hist[row + 9 * channel] (row-fastest, oldest first, the committed rows); the window's normalized rows
12// are written to norm_w[t][channel] (the host appends the kept ones at commit).
13
14#include <metal_stdlib>
15using namespace metal;
16
17struct PleArgs {
18    uint n_embd;   // 2560
19    uint hc;       // 4
20    float eps;
21};
22
23static inline float tg_sum(float v, threadgroup float* part, ushort sg, ushort lane) {
24    v = simd_sum(v);
25    if (lane == 0) part[sg] = v;
26    threadgroup_barrier(mem_flags::mem_threadgroup);
27    float t = 0.0f;
28    for (int i = 0; i < 8; ++i) t += part[i];
29    threadgroup_barrier(mem_flags::mem_threadgroup);
30    return t;
31}
32
33// grid (hc, T), 256 threads: y[t][c] = grouped_norm(x[t][c]) with w[c]
34kernel void ple_gnorm(device const float* x [[buffer(0)]], device const float* w [[buffer(1)]], device float* y [[buffer(2)]],
35                      constant PleArgs& a [[buffer(3)]], uint3 tg [[threadgroup_position_in_grid]],
36                      ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]],
37                      ushort lane [[thread_index_in_simdgroup]]) {
38    threadgroup float part[8];
39    const uint c = tg.x, t = tg.y, n = a.n_embd;
40    device const float* xc = x + (t * a.hc + c) * n;
41    float ss = 0.0f;
42    for (uint d = tid; d < n; d += 256) ss += xc[d] * xc[d];
43    const float mean = tg_sum(ss, part, sg, lane) / float(n);
44    const float scale = 1.0f / sqrt(mean + a.eps);
45    device float* yc = y + (t * a.hc + c) * n;
46    for (uint d = tid; d < n; d += 256) yc[d] = xc[d] * scale * w[c * n + d];
47}
48
49// grid (hc, T), 256 threads: the gate, then gated = value * gate and normalized = grouped_norm(gated, norm_conv)
50kernel void ple_gate_norm(device const float* key [[buffer(0)]], device const float* query [[buffer(1)]],
51                          device const float* value [[buffer(2)]], device const float* w_conv [[buffer(3)]],
52                          device float* gated [[buffer(4)]], device float* norm_w [[buffer(5)]],
53                          constant PleArgs& a [[buffer(6)]], uint3 tg [[threadgroup_position_in_grid]],
54                          ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]],
55                          ushort lane [[thread_index_in_simdgroup]]) {
56    threadgroup float part[8];
57    const uint c = tg.x, t = tg.y, n = a.n_embd, row = (t * a.hc + c) * n;
58    float acc = 0.0f;
59    for (uint d = tid; d < n; d += 256) acc += key[row + d] * query[row + d];
60    const float s = tg_sum(acc, part, sg, lane) * (1.0f / sqrt(float(n)));
61    const float mag = sqrt(max(fabs(s), 1e-6f));
62    const float sgn = s > 0.0f ? 1.0f : (s < 0.0f ? -1.0f : 0.0f);
63    const float gate = 1.0f / (1.0f + exp(-(sgn * mag)));
64    float ss = 0.0f;
65    for (uint d = tid; d < n; d += 256) {
66        const float g = value[t * n + d] * gate;
67        gated[row + d] = g;
68        ss += g * g;
69    }
70    const float mean = tg_sum(ss, part, sg, lane) / float(n);
71    const float scale = 1.0f / sqrt(mean + a.eps);
72    for (uint d = tid; d < n; d += 256) norm_w[row + d] = gated[row + d] * scale * w_conv[c * n + d];
73}
74
75// grid (hc * n_embd / 256, T), 256 threads: the conv over [history | the window's rows], SiLU, and the residual
76// PLE_ROWS (function constant 32): the rows are different sequences (batch slots): row t convolves its slot's history only (slot
77// stride rinfo[7] floats, row t at rinfo[8 + 4 t] = {slot, ...}); the three taps before the row itself always read the history.
78constant bool PLE_ROWS_C [[function_constant(32)]];
79constant bool ple_rows = is_function_constant_defined(PLE_ROWS_C) && PLE_ROWS_C;
80
81kernel void ple_conv_add(device const float* hist [[buffer(0)]], device const float* norm_w [[buffer(1)]],
82                         device const half* kw [[buffer(2)]], device const float* gated [[buffer(3)]],
83                         device float* R [[buffer(4)]], constant PleArgs& a [[buffer(5)]],
84                         device const uint* rinfo [[buffer(6), function_constant(ple_rows)]],
85                         uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]]) {
86    const uint ch = tg.x * 256 + tid, t = tg.y, hcd = a.hc * a.n_embd;
87    if (ch >= hcd) return;
88    if (ple_rows) hist += ulong(rinfo[8 + 4 * t]) * rinfo[7];
89    float acc = 0.0f;
90    for (int k = 0; k < 4; ++k) {
91        float v;
92        if (ple_rows) {
93            v = k == 3 ? norm_w[t * hcd + ch] : hist[uint(9 - (3 - k) * 3) + 9 * ch];
94        } else {
95            const int p = int(t) - (3 - k) * 3;   // the row this tap reads: < 0 = the committed history
96            v = p >= 0 ? norm_w[uint(p) * hcd + ch] : hist[uint(9 + p) + 9 * ch];
97        }
98        acc += float(kw[k + 4 * ch]) * v;
99    }
100    const float conv = acc / (1.0f + exp(-acc));
101    device float* r = R + t * hcd;
102    r[ch] = r[ch] + gated[t * hcd + ch] + conv;
103}