okami.mom/strata

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

Niko1221Merge f-qwen36 into f-144-rel (geometry: #2179's read_geometry guard kept for qwen4exp, Qwen3.6's family guard on top)4cfeaa0f4

main
9.3 KiB186 linesraw
1// src/kernels/cuda/fused_gdn.cu - see include/strata/kernels/fused_gdn.hpp.
2#include "strata/kernels/fused_gdn.hpp"
3#include "strata/core/geometry.hpp"
4
5#include <cuda_runtime.h>
6
7#include <cstdio>
8#include <cstdlib>
9
10namespace strata::kernels {
11namespace {
12
13constexpr int S = 128;          // state size (rows = cols = 128)
14static_assert(S == strata::core::kCompiledGeometry.ssm_state_size, "fused_gdn is compiled for core/geometry.hpp");
15constexpr int RG = 4;           // row groups
16constexpr int RPG = S / RG;     // 32 rows per thread
17
18// SILU: the output gate is SiLU(z) (Qwen3.6, qwen35moe) instead of sigmoid(z) (Flash-Next); a compile-time flag, so
19// the sigmoid instantiation is the kernel it was.
20template <bool SILU>
21__global__ void __launch_bounds__(S * RG) gdn_step_norm_kernel(float* __restrict__ state, const float* __restrict__ q,
22                                                               const float* __restrict__ k, const float* __restrict__ v,
23                                                               const float* __restrict__ gate,
24                                                               const float* __restrict__ beta,
25                                                               const float* __restrict__ z,
26                                                               const float* __restrict__ gamma, float eps,
27                                                               float* __restrict__ y, int h_k, int h_v) {
28    __shared__ float sk[S], sq[S];
29    __shared__ float red_kv[RG][S];
30    __shared__ float red_o[RG][S];
31    __shared__ float wsum[S * RG / 32];
32    const int head = blockIdx.x;
33    const int col = threadIdx.x;          // 0..127
34    const int rg = threadIdx.y;           // 0..3
35    const int tid = rg * S + col;
36    const int qh = head % h_k;
37    if (tid < S) { sk[tid] = k[qh * S + tid]; sq[tid] = q[qh * S + tid]; }
38    float s[RPG];
39    float* base = state + ((size_t) (rg * RPG) * h_v + head) * S + col;
40    const size_t row_stride = (size_t) h_v * S;
41#pragma unroll
42    for (int r = 0; r < RPG; ++r) s[r] = base[r * row_stride];
43    __syncthreads();
44    const float g = __expf(gate[head]);
45    float kv = 0.0f;
46#pragma unroll
47    for (int r = 0; r < RPG; ++r) kv = fmaf(s[r], sk[rg * RPG + r], kv);
48    red_kv[rg][col] = kv;
49    __syncthreads();
50    const float kv_col = red_kv[0][col] + red_kv[1][col] + red_kv[2][col] + red_kv[3][col];
51    const float delta = (v[head * S + col] - g * kv_col) * beta[head];
52    float o = 0.0f;
53#pragma unroll
54    for (int r = 0; r < RPG; ++r) {
55        s[r] = fmaf(g, s[r], sk[rg * RPG + r] * delta);
56        o = fmaf(s[r], sq[rg * RPG + r], o);
57        base[r * row_stride] = s[r];
58    }
59    red_o[rg][col] = o;
60    __syncthreads();
61    float oc = 0.0f, sq_part = 0.0f;
62    if (rg == 0) {
63        oc = (red_o[0][col] + red_o[1][col] + red_o[2][col] + red_o[3][col]) * rsqrtf((float) S);
64        sq_part = oc * oc;
65    }
66    // RMS over the head's 128 outputs: warps of row group 0 are threads 0..127.
67    for (int o2 = 16; o2 > 0; o2 >>= 1) sq_part += __shfl_xor_sync(0xffffffffu, sq_part, o2);
68    if ((tid & 31) == 0) wsum[tid >> 5] = sq_part;
69    __syncthreads();
70    if (rg == 0) {
71        const float ss = wsum[0] + wsum[1] + wsum[2] + wsum[3];
72        const float scale = rsqrtf(ss / (float) S + eps);
73        const float zz = z[head * S + col];
74        if constexpr (SILU) y[head * S + col] = oc * scale * gamma[col] * (zz / (1.0f + __expf(-zz)));
75        else y[head * S + col] = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));
76    }
77}
78
79__global__ void __launch_bounds__(S) gdn_conv_l2_kernel(float* __restrict__ hist, const float* __restrict__ qkv,
80                                                        const float* __restrict__ w, float* __restrict__ h,
81                                                        int qk_heads, float eps) {
82    __shared__ float part[S / 32];
83    const int c = blockIdx.x * S + threadIdx.x;
84    const float v0 = hist[c * 3], v1 = hist[c * 3 + 1], v2 = hist[c * 3 + 2], x = qkv[c];
85    float sum = v0 * w[c * 4] + v1 * w[c * 4 + 1] + v2 * w[c * 4 + 2] + x * w[c * 4 + 3];
86    hist[c * 3] = v1;
87    hist[c * 3 + 1] = v2;
88    hist[c * 3 + 2] = x;
89    float y = sum / (1.0f + __expf(-sum));
90    if ((int) blockIdx.x < qk_heads) {
91        float sq = y * y;
92        for (int o = 16; o > 0; o >>= 1) sq += __shfl_xor_sync(0xffffffffu, sq, o);
93        if ((threadIdx.x & 31) == 0) part[threadIdx.x >> 5] = sq;
94        __syncthreads();
95        const float ss = part[0] + part[1] + part[2] + part[3];
96        y *= rsqrtf(ss + eps);
97    }
98    h[c] = y;
99}
100
101__global__ void __launch_bounds__(256) gdn_ab_kernel(const float* __restrict__ x, const uint16_t* __restrict__ wa,
102                                                     const uint16_t* __restrict__ wb, const float* __restrict__ dt,
103                                                     const float* __restrict__ ssm_a, float* __restrict__ gate,
104                                                     float* __restrict__ beta, int n, int h_v) {
105    const int row = blockIdx.x * 8 + (threadIdx.x >> 5), lane = threadIdx.x & 31;
106    if (row >= 2 * h_v) return;
107    const bool is_beta = row >= h_v;
108    const int r = is_beta ? row - h_v : row;
109    const uint4* w4 = reinterpret_cast<const uint4*>((is_beta ? wb : wa) + (size_t) r * n);
110    float acc = 0.0f;
111    for (int j = lane; j < n / 8; j += 32) {
112        const uint4 wv = __ldg(w4 + j);
113        const float4 xa = *reinterpret_cast<const float4*>(x + j * 8);
114        const float4 xb = *reinterpret_cast<const float4*>(x + j * 8 + 4);
115        acc = fmaf(__uint_as_float(wv.x << 16), xa.x, acc); acc = fmaf(__uint_as_float(wv.x & 0xffff0000u), xa.y, acc);
116        acc = fmaf(__uint_as_float(wv.y << 16), xa.z, acc); acc = fmaf(__uint_as_float(wv.y & 0xffff0000u), xa.w, acc);
117        acc = fmaf(__uint_as_float(wv.z << 16), xb.x, acc); acc = fmaf(__uint_as_float(wv.z & 0xffff0000u), xb.y, acc);
118        acc = fmaf(__uint_as_float(wv.w << 16), xb.z, acc); acc = fmaf(__uint_as_float(wv.w & 0xffff0000u), xb.w, acc);
119    }
120    for (int o = 16; o > 0; o >>= 1) acc += __shfl_xor_sync(0xffffffffu, acc, o);
121    if (lane != 0) return;
122    if (is_beta) {
123        beta[r] = 1.0f / (1.0f + __expf(-acc));
124    } else {
125        const float v = acc + dt[r];
126        const float sp = v > 20.0f ? v : log1pf(__expf(v));
127        gate[r] = sp * ssm_a[r];
128    }
129}
130
131}  // namespace
132
133void fused_gdn_conv_l2(float* history, const float* qkv, const float* conv_w, float* h, int channels, int qk_heads,
134                       float eps, void* stream) {
135    if (!history || !qkv || !conv_w || !h || channels % S != 0 || qk_heads < 0 || qk_heads > channels / S) {
136        std::fprintf(stderr, "fused_gdn_conv_l2: invalid arguments\n");
137        std::exit(1);
138    }
139    gdn_conv_l2_kernel<<<(unsigned) (channels / S), S, 0, (cudaStream_t) stream>>>(history, qkv, conv_w, h, qk_heads, eps);
140    const cudaError_t e = cudaGetLastError();
141    if (e != cudaSuccess) { std::fprintf(stderr, "fused_gdn_conv_l2: %s\n", cudaGetErrorString(e)); std::exit(1); }
142}
143
144void fused_gdn_ab(const float* x, const uint16_t* w_alpha, const uint16_t* w_beta, const float* dt, const float* ssm_a,
145                  float* gate, float* beta, int n_embd, int h_v, void* stream) {
146    if (!x || !w_alpha || !w_beta || !dt || !ssm_a || !gate || !beta || n_embd % 8 != 0 || h_v <= 0) {
147        std::fprintf(stderr, "fused_gdn_ab: invalid arguments\n");
148        std::exit(1);
149    }
150    gdn_ab_kernel<<<(unsigned) ((2 * h_v + 7) / 8), 256, 0, (cudaStream_t) stream>>>(x, w_alpha, w_beta, dt, ssm_a, gate,
151                                                                                     beta, n_embd, h_v);
152    const cudaError_t e = cudaGetLastError();
153    if (e != cudaSuccess) { std::fprintf(stderr, "fused_gdn_ab: %s\n", cudaGetErrorString(e)); std::exit(1); }
154}
155
156namespace {
157template <bool SILU>
158void step_norm(float* state, const float* q, const float* k, const float* v, const float* gate, const float* beta,
159               const float* z, const float* gamma, float eps, float* y, int h_k, int h_v, void* stream) {
160    if (!state || !q || !k || !v || !gate || !beta || !z || !gamma || !y || h_k <= 0 || h_v <= 0 || h_v % h_k) {
161        std::fprintf(stderr, "fused_gdn_step_norm: invalid arguments\n");
162        std::exit(1);
163    }
164    gdn_step_norm_kernel<SILU><<<(unsigned) h_v, dim3(S, RG), 0, (cudaStream_t) stream>>>(state, q, k, v, gate, beta,
165                                                                                         z, gamma, eps, y, h_k, h_v);
166    const cudaError_t e = cudaGetLastError();
167    if (e != cudaSuccess) {
168        std::fprintf(stderr, "fused_gdn_step_norm: %s\n", cudaGetErrorString(e));
169        std::exit(1);
170    }
171}
172}  // namespace
173
174void fused_gdn_step_norm(float* state, const float* q, const float* k, const float* v, const float* gate,
175                         const float* beta, const float* z, const float* gamma, float eps, float* y, int h_k, int h_v,
176                         void* stream) {
177    step_norm<false>(state, q, k, v, gate, beta, z, gamma, eps, y, h_k, h_v, stream);
178}
179
180void fused_gdn_step_norm_silu(float* state, const float* q, const float* k, const float* v, const float* gate,
181                              const float* beta, const float* z, const float* gamma, float eps, float* y, int h_k,
182                              int h_v, void* stream) {
183    step_norm<true>(state, q, k, v, gate, beta, z, gamma, eps, y, h_k, h_v, stream);
184}
185
186}  // namespace strata::kernels