Upload shaders/ssu.wgsl with huggingface_hub
Browse files- shaders/ssu.wgsl +92 -0
shaders/ssu.wgsl
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ssu.wgsl — selective_state_update (SSM scan, single decode step, batch=1).
|
| 2 |
+
// Ported from gfx1151_inference/shaders/ssu.comp (Vulkan GLSL → WebGPU WGSL).
|
| 3 |
+
//
|
| 4 |
+
// Math (per (h, s)):
|
| 5 |
+
// delta_h = softplus(dt[h] + dt_bias[h])
|
| 6 |
+
// state[h, s] = state[h, s] * exp(delta_h * A[h, s]) + (delta_h * B[s]) * x[h]
|
| 7 |
+
// Then per h:
|
| 8 |
+
// y[h] = sum_s( state[h, s] * C[s] ) + D[h] * x[h]
|
| 9 |
+
//
|
| 10 |
+
// Falcon-Mamba 7B: hidden_dim=4096, ssm_state_size=16.
|
| 11 |
+
// Dispatch: one workgroup per h. Workgroup size = ssm_state_size = 16 threads.
|
| 12 |
+
//
|
| 13 |
+
// State buffer is updated in-place (read+write).
|
| 14 |
+
|
| 15 |
+
const WG_SIZE: u32 = 16u;
|
| 16 |
+
|
| 17 |
+
struct Params {
|
| 18 |
+
H: u32, // hidden_dim
|
| 19 |
+
S: u32, // ssm_state_size
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
@group(0) @binding(0) var<storage, read_write> state_buf: array<f32>; // [H, S]
|
| 23 |
+
@group(0) @binding(1) var<storage, read> x_buf: array<f32>; // [H]
|
| 24 |
+
@group(0) @binding(2) var<storage, read> dt_buf: array<f32>; // [H]
|
| 25 |
+
@group(0) @binding(3) var<storage, read> A_buf: array<f32>; // [H, S]
|
| 26 |
+
@group(0) @binding(4) var<storage, read> B_buf: array<f32>; // [S]
|
| 27 |
+
@group(0) @binding(5) var<storage, read> C_buf: array<f32>; // [S]
|
| 28 |
+
@group(0) @binding(6) var<storage, read> D_buf: array<f32>; // [H]
|
| 29 |
+
@group(0) @binding(7) var<storage, read> dt_bias_buf: array<f32>; // [H]
|
| 30 |
+
@group(0) @binding(8) var<storage, read_write> y_buf: array<f32>; // [H]
|
| 31 |
+
|
| 32 |
+
@group(1) @binding(0) var<uniform> params: Params;
|
| 33 |
+
|
| 34 |
+
var<workgroup> partial_y: array<f32, 16>; // WG_SIZE = 16
|
| 35 |
+
|
| 36 |
+
fn stable_softplus(x: f32) -> f32 {
|
| 37 |
+
// softplus(x) = log(1 + exp(x)), numerically stable
|
| 38 |
+
return max(x, 0.0) + log(1.0 + exp(-abs(x)));
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
@compute @workgroup_size(WG_SIZE)
|
| 42 |
+
fn main(
|
| 43 |
+
@builtin(workgroup_id) wg_id: vec3<u32>,
|
| 44 |
+
@builtin(local_invocation_id) lid: vec3<u32>
|
| 45 |
+
) {
|
| 46 |
+
let h = wg_id.x;
|
| 47 |
+
let s = lid.x;
|
| 48 |
+
|
| 49 |
+
// All threads participate in barriers — guard computation, not control flow
|
| 50 |
+
var my_partial: f32 = 0.0;
|
| 51 |
+
var D_h: f32 = 0.0;
|
| 52 |
+
var x_h: f32 = 0.0;
|
| 53 |
+
|
| 54 |
+
if (h < params.H && s < params.S) {
|
| 55 |
+
let dt_h = dt_buf[h];
|
| 56 |
+
let bias_h = dt_bias_buf[h];
|
| 57 |
+
let delta_h = stable_softplus(dt_h + bias_h);
|
| 58 |
+
x_h = x_buf[h];
|
| 59 |
+
D_h = D_buf[h];
|
| 60 |
+
|
| 61 |
+
let state_idx = h * params.S + s;
|
| 62 |
+
let A_hs = -exp(A_buf[state_idx]);
|
| 63 |
+
let B_s = B_buf[s];
|
| 64 |
+
let C_s = C_buf[s];
|
| 65 |
+
|
| 66 |
+
let delta_A = exp(delta_h * A_hs);
|
| 67 |
+
let delta_B = delta_h * B_s;
|
| 68 |
+
|
| 69 |
+
let new_state = state_buf[state_idx] * delta_A + delta_B * x_h;
|
| 70 |
+
state_buf[state_idx] = new_state;
|
| 71 |
+
|
| 72 |
+
my_partial = new_state * C_s;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
partial_y[s] = my_partial;
|
| 76 |
+
workgroupBarrier();
|
| 77 |
+
|
| 78 |
+
// Tree reduction — all threads participate uniformly
|
| 79 |
+
var off: u32 = WG_SIZE / 2u;
|
| 80 |
+
loop {
|
| 81 |
+
if (off == 0u) { break; }
|
| 82 |
+
if (s < off) {
|
| 83 |
+
partial_y[s] = partial_y[s] + partial_y[s + off];
|
| 84 |
+
}
|
| 85 |
+
workgroupBarrier();
|
| 86 |
+
off = off >> 1u;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
if (s == 0u && h < params.H) {
|
| 90 |
+
y_buf[h] = partial_y[0u] + D_h * x_h;
|
| 91 |
+
}
|
| 92 |
+
}
|