// include/strata/kernels/gdn.hpp - the gated delta-net (GDN) layer's non-projection parts, P2.S2. // // Transcribed from `ref/gdn.py`, which is itself transcribed from `build_layer_attn_linear` and // `build_delta_net_autoregressive`. The projections (`wqkv`, `wqkv_gate`, `ssm_out`) are the existing // `s_gemv`/`s2_gemv` kernels; what lives here is everything BETWEEN them. // // Geometry of the real artifact (48 layers are GDN, 12 are QSA): // // S = state_size = 128 head_k_dim == head_v_dim // h_k = 16 k/q heads h_v = 48 v heads (idx[h] = h % h_k) // C = 2*128*16 + 128*48 = 10240 channels d_conv = 4 // // STATE LAYOUT. This is the one place the kernels deliberately differ from the reference, and the reason is // coalescing. `ref/gdn.py` carries the state as (S, S, H_v) = (i, j, h); here it is **(S, H_v, S)** = // (i, h, j), so that for a fixed i a warp walking consecutive j reads CONTIGUOUS memory. The state is a // runtime buffer, not a weight, so this costs nothing to choose - but it must be said, because the two // layouts index the same three numbers and a mismatch is silent. // // WHAT MAKES THE RECURRENCE PARALLEL, and it is worth stating because it is not obvious: // // dec = exp(gate) (h_v,) // st = state * dec // sk[h,j] = sum_i st[i,j,h] * k[idx[h]][i] // d[h,j] = (v[h,j] - sk[h,j]) * beta[h] // st[i,j,h] += k[idx[h]][i] * d[h,j] // o[h,j] = sum_i st[i,j,h] * q[idx[h]][i] // // Every line touches ONLY the (j, h) column. Nothing couples one column to another, so one thread can own a // column for the whole step and no barrier is needed anywhere in the recurrence. (The decay must still be // applied BEFORE the rank-1 update - `ref/gdn.py` PROPERTY 5 pins that, and the test re-pins it here.) #pragma once #include namespace strata::kernels { /// Geometry of one GDN layer. Flash-Next: S = 128, h_k = 16, h_v = 48 (VPK 3); Qwen3.6-35B-A3B: S = 128, /// h_k = 16, h_v = 32 (VPK 2). Every kernel here takes these at runtime; `h_v % h_k == 0` is the one rule. struct GdnShapes { int64_t S = 0; ///< head dim, and the state's contracted axis int64_t h_k = 0; ///< key/query heads (q and k are (h_k, S)) int64_t h_v = 0; ///< value heads (v, z, o are (h_v, S)); h_v must be a multiple of h_k }; /// The output gate's activation on `z` in `y = rms_norm(o) * ssm_norm * act(z)`. Flash-Next (`qwen4exp`) multiplies /// by sigmoid(z); Qwen3.6 (`qwen35moe`) by SiLU(z) = z * sigmoid(z) - llama.cpp `qwen35moe.cpp:273` is `ggml_silu`, /// and `qwen4exp.cpp:481` calls it "the one numerical difference". The oracle's negative control puts the two /// readings 1.07 relative L1 apart at `final_output-0` (docs/qwen36-port/oracle-crosscheck.md), so this is a /// per-model contract, not a tuning knob: the layer picks it from `ModelGeometry::arch` /// (`ModelGeometry::gdn_gate()`), never from a global, so one process can serve both models. Every GDN output /// kernel takes it as an argument (`gdn_out_norm`, `fused_gdn_step_norm`, `native_gdn_out_norm`, /// `gdn_step_norm_multi`); the sigmoid arithmetic is bitwise what it was before the parameter existed. enum class GdnGate : int { Sigmoid = 0, Silu = 1 }; inline const char* gdn_gate_name(GdnGate g) { return g == GdnGate::Silu ? "silu" : "sigmoid"; } /// One step of the delta-rule recurrence, IN PLACE on `state`. /// /// state (S, h_v, S) f32, j fastest, updated in place /// q, k (h_k, S) already L2-normalised, and q ALREADY SCALED by 1/sqrt(S) - the two llama.cpp paths /// apply that scale in different places, so the caller owns it /// v (h_v, S) /// gate (h_v,) the PRE-exp value; `dec = exp(gate)` is formed here /// beta (h_v,) /// o (h_v, S) /// /// The head pairing is `h % h_k` (MODULO), not `h / (h_v/h_k)` (INTERLEAVE). `ref/gdn.py` PROPERTY 8 and 11 /// pin this and the test re-pins it, because both conventions produce well-formed output. void gdn_step(float* state, const float* q, const float* k, const float* v, const float* gate, const float* beta, float* o, const GdnShapes& s, void* stream); /// `ggml_ssm_conv` with the carried state prepended. Updates `conv_state` in place. /// /// conv_state (d_conv-1, channels) the previous d_conv-1 input columns, OLDEST FIRST /// x (channels,) the new column /// kW (channels, d_conv) the weight in GGML-NATIVE layout: `kW[c*d_conv + i]` is tap i of /// channel c. The manifest gives `ssm_conv1d.weight` ne = [4, 10240], /// i.e. ne0 = d_conv is the FAST axis, which is this and NOT the /// reference's row-major (d_conv, channels). /// out (channels,) /// /// out[c] = sum_{i