Files
Robert KarlandClaude Fable 5.1 5c6eb91ca8 GDN output gate selectable per model: SiLU(z) for qwen35moe, sigmoid(z) kept for qwen4exp
Qwen3.6 closes the GDN layer with rms_norm(o) * ssm_norm * SiLU(z) where Flash-Next uses
sigmoid(z) (study 3.2, inventory row 39; the oracle's negative control puts the two 1.07
relative L1 apart).  `kernels::GdnGate` names the choice and every decode output kernel
takes it as an argument: `gdn_out_norm` (legacy), `fused_gdn_step_norm` (the default native
decode), `native_gdn_out_norm` (pinned unfused), and the verify window's
`gdn_step_norm_multi`, which must stay bitwise the fused kernel.  The sigmoid arithmetic is
unchanged; SiLU is ggml's op_silu form z / (1 + exp(-z)).  No global: the layer passes
`ModelGeometry::gdn_gate()` (next commit).

gdn_parity now runs the recurrence at both geometries, 16/48 (VPK 3, sigmoid) and 16/32
(VPK 2, SiLU), through all four implementations, checks the verify kernel bitwise against
the fused one (verify, commit and split-commit paths), and makes the wrong gate a negative
case through each kernel itself (86-125% apart), beside the existing four traps.  The
harness uses a blocking stream: its first version raced its own pageable memcpy resets on
a non-blocking stream and reported the native kernels 7.5x off at one geometry.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017tPVMg2wGSNR1KANEiUtwr
2026-10-11 12:18:08 -07:00

41 lines
2.6 KiB
C++

// include/strata/kernels/fused_gdn.hpp - plan v0.3 P3: the gated delta-net step and its output norm in ONE kernel.
//
// The native step (after llama.cpp's gated_delta_net.cu) gives each warp one (head, column) and strides its 32
// lanes over the 128 ROWS of the state, which is stored `[row][head][col]`: every lane's load is 24.5 KB from its
// neighbour's, so each 32 B sector delivers 4 useful bytes - 47.6 us per layer for 6.3 MB of state traffic. Here a
// block owns one whole head, its 512 threads are (row group of 32, column), and a row's 128 columns are one
// contiguous 512 B load. With the head in one block, the per-head RMS norm, the norm weight and the sigmoid(z)
// gate of `gdn_out_norm` run in the same kernel.
//
// S <- g S + k (beta (v - g S^T k))^T (g = exp(gate[head]), per value head; q/k shared by h_v / h_k heads)
// o = (S^T q) / sqrt(128)
// y = rmsnorm(o) * gamma * act(z) (act = sigmoid for Flash-Next, SiLU for Qwen3.6: `GdnGate`, gdn.hpp)
#pragma once
#include "strata/kernels/gdn.hpp"
#include <cstdint>
namespace strata::kernels {
/// The 4-tap causal conv + SiLU over all `channels` (history is `[channel][3]`, updated), then the L2 norm of
/// each of the first `qk_heads` 128-channel heads (q then k), eps as the native `l2_norm` (x / sqrt(sum + eps)).
/// One block per 128-channel head; replaces conv_silu + two l2_norm launches.
void fused_gdn_conv_l2(float* history, const float* qkv, const float* conv_w, float* h, int channels, int qk_heads,
float eps, void* stream);
/// alpha and beta, both `(h_v, n_embd)` BF16 against the FP32 activation, with their epilogues:
/// gate = softplus(alpha + dt) * ssm_a, beta = sigmoid(beta). One warp per row; replaces two MMVF launches and
/// two elementwise kernels.
void fused_gdn_ab(const float* x, const uint16_t* w_alpha, const uint16_t* w_beta, const float* dt, const float* ssm_a,
float* gate, float* beta, int n_embd, int h_v, void* stream);
/// The step and the output norm in one kernel: state (S, h_v, S) in place, q/k (h_k, 128) L2-normalised and NOT
/// pre-scaled (the 1/sqrt(S) goes on the readout), v/z/y (h_v, 128), gate/beta (h_v), gamma (128). `act` is the
/// model's output gate (`GdnGate`); `h_v % h_k == 0`, any h_v (Flash-Next 48, Qwen3.6 32).
void fused_gdn_step_norm(float* state, const float* q, const float* k, const float* v, const float* gate,
const float* beta, const float* z, const float* gamma, float eps, GdnGate act, float* y,
int h_k, int h_v, void* stream);
} // namespace strata::kernels