rullama 0.5.0

Browser-resident Gemma 4 inference: pure Rust → WebAssembly + WebGPU. Loads Ollama's on-disk GGUF blobs and runs the forward pass on the local GPU via hand-written WGSL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! StyleTTS2 / Kokoro vocoder DSP dispatchers: 1-D convs (+ f16), transpose
//! convs, the activations they feed (leaky_relu, gelu_exact, snake, adain),
//! 2-D transpose / nearest-upsample, and the iSTFT vocoder (spec_phase +
//! istft). Their param structs live here since nothing else uses them; the
//! `cached_dispatch` / `wg_grid` helpers come from the parent module.

use bytemuck::{Pod, Zeroable};

use super::{cached_dispatch, wg_grid};
use crate::backend::WgpuCtx;
use crate::backend::pipelines::Pipelines;

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct Conv1dParams {
    cin: u32,
    tin: u32,
    cout: u32,
    tout: u32,
    k: u32,
    stride: u32,
    pad: u32,
    dilation: u32,
    groups: u32,
    has_bias: u32,
    _p0: u32,
    _p1: u32,
}

/// General channel-major 1-D conv. Returns `tout`. Buffers: x, w, bias, y.
/// Caller must size `y` to `cout * tout` where tout = (tin+2pad-dil*(k-1)-1)/stride+1.
#[allow(clippy::too_many_arguments)]
pub fn conv1d_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    w: &wgpu::Buffer,
    bias: Option<&wgpu::Buffer>,
    dummy: &wgpu::Buffer,
    y: &wgpu::Buffer,
    cin: usize,
    tin: usize,
    cout: usize,
    k: usize,
    stride: usize,
    pad: usize,
    dilation: usize,
    groups: usize,
) -> usize {
    let tout = (tin + 2 * pad - dilation * (k - 1) - 1) / stride + 1;
    let params = Conv1dParams {
        cin: cin as u32,
        tin: tin as u32,
        cout: cout as u32,
        tout: tout as u32,
        k: k as u32,
        stride: stride as u32,
        pad: pad as u32,
        dilation: dilation as u32,
        groups: groups as u32,
        has_bias: bias.is_some() as u32,
        _p0: 0,
        _p1: 0,
    };
    let b = bias.unwrap_or(dummy);
    let total = (cout * tout) as u32;
    cached_dispatch(
        ctx,
        enc,
        &p.conv1d,
        "conv1d",
        &[x, w, b, y],
        &params,
        wg_grid(total as usize),
    );
    tout
}

/// f16-weight variant of [`conv1d_chained`]. Identical args, except `w` is an
/// f16-packed weight buffer (2 halves per u32, `write_storage_f16` layout). x /
/// bias / y stay f32. Halves the resident weight footprint of the StyleTTS2
/// convolutions for the memory-tight f16 clone.
#[allow(clippy::too_many_arguments)]
pub fn conv1d_f16_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    w: &wgpu::Buffer,
    bias: Option<&wgpu::Buffer>,
    dummy: &wgpu::Buffer,
    y: &wgpu::Buffer,
    cin: usize,
    tin: usize,
    cout: usize,
    k: usize,
    stride: usize,
    pad: usize,
    dilation: usize,
    groups: usize,
) -> usize {
    let tout = (tin + 2 * pad - dilation * (k - 1) - 1) / stride + 1;
    let params = Conv1dParams {
        cin: cin as u32,
        tin: tin as u32,
        cout: cout as u32,
        tout: tout as u32,
        k: k as u32,
        stride: stride as u32,
        pad: pad as u32,
        dilation: dilation as u32,
        groups: groups as u32,
        has_bias: bias.is_some() as u32,
        _p0: 0,
        _p1: 0,
    };
    let b = bias.unwrap_or(dummy);
    let total = (cout * tout) as u32;
    cached_dispatch(
        ctx,
        enc,
        &p.conv1d_f16,
        "conv1d_f16",
        &[x, w, b, y],
        &params,
        wg_grid(total as usize),
    );
    tout
}

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct ConvT1dParams {
    cin: u32,
    tin: u32,
    cout: u32,
    tout: u32,
    k: u32,
    stride: u32,
    pad: u32,
    groups: u32,
    has_bias: u32,
    _p0: u32,
    _p1: u32,
    _p2: u32,
}

/// General channel-major ConvTranspose1d (groups=1 for ISTFTNet ups, groups=C for
/// the depthwise pool). Returns tout. Buffers: x, w, bias, y.
#[allow(clippy::too_many_arguments)]
pub fn conv_transpose1d_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    w: &wgpu::Buffer,
    bias: Option<&wgpu::Buffer>,
    dummy: &wgpu::Buffer,
    y: &wgpu::Buffer,
    cin: usize,
    tin: usize,
    cout: usize,
    k: usize,
    stride: usize,
    pad: usize,
    output_padding: usize,
    groups: usize,
) -> usize {
    let tout = (tin - 1) * stride + (k - 1) + output_padding + 1 - 2 * pad;
    let params = ConvT1dParams {
        cin: cin as u32,
        tin: tin as u32,
        cout: cout as u32,
        tout: tout as u32,
        k: k as u32,
        stride: stride as u32,
        pad: pad as u32,
        groups: groups as u32,
        has_bias: bias.is_some() as u32,
        _p0: 0,
        _p1: 0,
        _p2: 0,
    };
    let b = bias.unwrap_or(dummy);
    let total = (cout * tout) as u32;
    cached_dispatch(
        ctx,
        enc,
        &p.conv_transpose1d,
        "convT1d",
        &[x, w, b, y],
        &params,
        wg_grid(total as usize),
    );
    tout
}

/// f16-weight variant of [`conv_transpose1d_chained`]. `w` is an f16-packed
/// weight buffer (2 halves per u32, `write_storage_f16` layout); x/bias/y f32.
#[allow(clippy::too_many_arguments)]
pub fn conv_transpose1d_f16_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    w: &wgpu::Buffer,
    bias: Option<&wgpu::Buffer>,
    dummy: &wgpu::Buffer,
    y: &wgpu::Buffer,
    cin: usize,
    tin: usize,
    cout: usize,
    k: usize,
    stride: usize,
    pad: usize,
    output_padding: usize,
    groups: usize,
) -> usize {
    let tout = (tin - 1) * stride + (k - 1) + output_padding + 1 - 2 * pad;
    let params = ConvT1dParams {
        cin: cin as u32,
        tin: tin as u32,
        cout: cout as u32,
        tout: tout as u32,
        k: k as u32,
        stride: stride as u32,
        pad: pad as u32,
        groups: groups as u32,
        has_bias: bias.is_some() as u32,
        _p0: 0,
        _p1: 0,
        _p2: 0,
    };
    let b = bias.unwrap_or(dummy);
    let total = (cout * tout) as u32;
    cached_dispatch(
        ctx,
        enc,
        &p.conv_transpose1d_f16,
        "convT1d_f16",
        &[x, w, b, y],
        &params,
        wg_grid(total as usize),
    );
    tout
}

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct LeakyReluParams {
    n: u32,
    slope: f32,
    _p0: u32,
    _p1: u32,
}

/// Elementwise LeakyReLU in-place on `y`.
pub fn leaky_relu_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    y: &wgpu::Buffer,
    n: usize,
    slope: f32,
) {
    let params = LeakyReluParams {
        n: n as u32,
        slope,
        _p0: 0,
        _p1: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.leaky_relu,
        "leaky_relu",
        &[y],
        &params,
        wg_grid(n),
    );
}

/// Exact (erf) GELU, in-place — the StyleTTS2 diffusion denoiser activation.
pub fn gelu_exact_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    y: &wgpu::Buffer,
    n: usize,
) {
    let params = LeakyReluParams {
        n: n as u32,
        slope: 0.0,
        _p0: 0,
        _p1: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.gelu_exact,
        "gelu_exact",
        &[y],
        &params,
        wg_grid(n),
    );
}

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct SnakeParams {
    c: u32,
    t: u32,
    _p0: u32,
    _p1: u32,
}

/// Snake1D activation (channel-major), per-channel alpha. Buffers: x, alpha, y.
#[allow(clippy::too_many_arguments)]
pub fn snake_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    alpha: &wgpu::Buffer,
    y: &wgpu::Buffer,
    c: usize,
    t: usize,
) {
    let params = SnakeParams {
        c: c as u32,
        t: t as u32,
        _p0: 0,
        _p1: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.snake,
        "snake",
        &[x, alpha, y],
        &params,
        wg_grid(c * t),
    );
}

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct AdainParams {
    c: u32,
    t: u32,
    eps: f32,
    _p0: u32,
}

/// AdaIN1d (channel-major): per-channel InstanceNorm + (1+gamma)*·+beta. gamma/beta
/// are precomputed = chunk(fc(style)). One workgroup per channel. Buffers: x, gamma, beta, y.
#[allow(clippy::too_many_arguments)]
pub fn adain_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    gamma: &wgpu::Buffer,
    beta: &wgpu::Buffer,
    y: &wgpu::Buffer,
    c: usize,
    t: usize,
    eps: f32,
) {
    let params = AdainParams {
        c: c as u32,
        t: t as u32,
        eps,
        _p0: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.adain,
        "adain",
        &[x, gamma, beta, y],
        &params,
        (c as u32, 1, 1),
    );
}

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct Transpose2dParams {
    rows: u32,
    cols: u32,
    _p0: u32,
    _p1: u32,
}

/// 2-D transpose: x[rows, cols] row-major → y[cols, rows]. Buffers: x, y.
pub fn transpose2d_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
) {
    let params = Transpose2dParams {
        rows: rows as u32,
        cols: cols as u32,
        _p0: 0,
        _p1: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.transpose2d,
        "transpose2d",
        &[x, y],
        &params,
        (((rows * cols) as u32).div_ceil(64), 1, 1),
    );
}

/// Nearest ×2 upsample (channel-major): x[c, t] → y[c, 2t]. Buffers: x, y.
pub fn nearest_upsample2x_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    y: &wgpu::Buffer,
    c: usize,
    t: usize,
) {
    let params = Transpose2dParams {
        rows: c as u32,
        cols: t as u32,
        _p0: 0,
        _p1: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.nearest_upsample2x,
        "nearest2x",
        &[x, y],
        &params,
        wg_grid(c * 2 * t),
    );
}

/// ISTFTNet spectral split: post[2*nbins, t] → spec=exp(post[:nbins]), phase=sin(post[nbins:]).
/// Buffers: post, spec, phase.
pub fn spec_phase_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    post: &wgpu::Buffer,
    spec: &wgpu::Buffer,
    phase: &wgpu::Buffer,
    nbins: usize,
    t: usize,
) {
    let params = Transpose2dParams {
        rows: nbins as u32,
        cols: t as u32,
        _p0: 0,
        _p1: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.spec_phase,
        "spec_phase",
        &[post, spec, phase],
        &params,
        (((nbins * t) as u32).div_ceil(64), 1, 1),
    );
}

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable, Debug)]
struct IstftParams {
    nbins: u32,
    frames: u32,
    nfft: u32,
    hop: u32,
    pad: u32,
    out_len: u32,
    _p0: u32,
    _p1: u32,
}

/// Exact iSTFT (gather form). spec/phase are `[nbins, frames]` channel-major;
/// writes `y[out_len]`. Returns out_len. Buffers: spec, phase, y.
#[allow(clippy::too_many_arguments)]
pub fn istft_chained(
    ctx: &WgpuCtx,
    p: &Pipelines,
    enc: &mut wgpu::CommandEncoder,
    spec: &wgpu::Buffer,
    phase: &wgpu::Buffer,
    y: &wgpu::Buffer,
    nbins: usize,
    frames: usize,
    nfft: usize,
    hop: usize,
) -> usize {
    let pad = nfft / 2;
    let out_len = (frames - 1) * hop + nfft - 2 * pad;
    let params = IstftParams {
        nbins: nbins as u32,
        frames: frames as u32,
        nfft: nfft as u32,
        hop: hop as u32,
        pad: pad as u32,
        out_len: out_len as u32,
        _p0: 0,
        _p1: 0,
    };
    cached_dispatch(
        ctx,
        enc,
        &p.istft,
        "istft",
        &[spec, phase, y],
        &params,
        ((out_len as u32).div_ceil(64), 1, 1),
    );
    out_len
}