brain2qwerty 0.0.1

Brain2Qwerty V1/V2 MEG neural decoding inference in Rust (parity-tested vs Python)
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! RLX graph for Conformer tail (input z `[B,T,D]` → z_final, c_out).

use rlx::ir::shape;
use rlx::ir::GraphExt;
use rlx::ops::MaskKind;
use rlx::prelude::*;

#[derive(Clone, Copy, Debug)]
pub struct EncoderSpec {
    pub b: usize,
    pub t: usize,
    pub d: usize,
    pub n_classes: usize,
    pub num_layers: usize,
    pub num_heads: usize,
    pub ffn_dim: usize,
    pub dw_kernel: usize,
    pub aux: bool,
}

fn s3(a: usize, b: usize, c: usize) -> Shape {
    Shape::new(&[a, b, c], DType::F32)
}

fn linear(g: &mut Graph, x: NodeId, w: NodeId, b: NodeId) -> NodeId {
    let y = g.mm(x, w);
    let out_d = g.shape(y).dims()[2].unwrap_static();
    let b3 = g.reshape_(b, vec![1, 1, out_d as i64]);
    g.add(y, b3)
}

pub fn build_conformer_tail_graph(spec: &EncoderSpec) -> Graph {
    let mut g = Graph::new("brain2qwerty_conformer_tail");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let mut x = z_in;

    for layer in 0..spec.num_layers {
        x = conformer_layer(&mut g, x, layer, spec);
    }
    let z_final = x;

    let mut c_in = z_final;
    if spec.aux {
        let ln_w = g.param(
            "shared_layer_norm.weight",
            Shape::new(&[spec.d], DType::F32),
        );
        let ln_b = g.param("shared_layer_norm.bias", Shape::new(&[spec.d], DType::F32));
        c_in = g.ln(c_in, ln_w, ln_b, 1e-5);
    }
    let out_w = g.param(
        "output_layer.weight",
        Shape::new(&[spec.d, spec.n_classes], DType::F32),
    );
    let out_b = g.param(
        "output_layer.bias",
        Shape::new(&[spec.n_classes], DType::F32),
    );
    let c_out = linear(&mut g, c_in, out_w, out_b);

    g.set_outputs(vec![z_final, c_out]);
    g
}

/// Self-attention block only (post layer-norm input expected externally).
pub fn build_self_attn_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_self_attn");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}");
    let sa_ln_w = g.param(
        format!("{p}.self_attn_layer_norm.weight"),
        Shape::new(&[spec.d], DType::F32),
    );
    let sa_ln_b = g.param(
        format!("{p}.self_attn_layer_norm.bias"),
        Shape::new(&[spec.d], DType::F32),
    );
    let xn = g.ln(z_in, sa_ln_w, sa_ln_b, 1e-5);
    let out = self_attn(&mut g, xn, layer, spec);
    g.set_outputs(vec![out]);
    g
}

/// Pointwise + GLU front of conv module (for bisection).
pub fn build_conv_p1_glu_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_conv_p1_glu");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}.conv_module");
    let d = spec.d;
    let b = spec.b;
    let t = spec.t;
    let ln_w = g.param(
        format!("{p}.layer_norm.weight"),
        Shape::new(&[d], DType::F32),
    );
    let ln_b = g.param(format!("{p}.layer_norm.bias"), Shape::new(&[d], DType::F32));
    let x = g.ln(z_in, ln_w, ln_b, 1e-5);
    let x4 = bct_to_nchw(&mut g, x, b, d, t);
    let p1_w = g.param(
        format!("{p}.sequential.0.weight"),
        Shape::new(&[2 * d, d, 1, 1], DType::F32),
    );
    let p1_b = g.param(
        format!("{p}.sequential.0.bias"),
        Shape::new(&[2 * d], DType::F32),
    );
    let y = g.conv2d(x4, p1_w, [1, 1], [1, 1], [0, 0], [1, 1], 1);
    let y = add_bias_nchw(&mut g, y, p1_b, 2 * d);
    let y0 = g.narrow_(y, 1, 0, d);
    let y1 = g.narrow_(y, 1, d, d);
    let gate = sigmoid(&mut g, y1);
    let glu = g.mul(y0, gate);
    let out = nchw_to_btd(&mut g, glu, b, d, t);
    g.set_outputs(vec![out]);
    g
}

/// Depthwise + group norm (post-GLU, for bisection).
pub fn build_conv_dw_gn_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_conv_dw_gn");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}.conv_module");
    let d = spec.d;
    let b = spec.b;
    let t = spec.t;
    let k = spec.dw_kernel;
    let pad = k / 2;

    let glu = bct_to_nchw(&mut g, z_in, b, d, t);
    let dw_w = g.param(
        format!("{p}.sequential.2.weight"),
        Shape::new(&[d, 1, 1, k], DType::F32),
    );
    let dw_b = g.param(
        format!("{p}.sequential.2.bias"),
        Shape::new(&[d], DType::F32),
    );
    let mut dw = g.conv2d(glu, dw_w, [1, k], [1, 1], [0, pad], [1, 1], d);
    dw = add_bias_nchw(&mut g, dw, dw_b, d);

    let gn_w = g.param(
        format!("{p}.sequential.3.weight"),
        Shape::new(&[d], DType::F32),
    );
    let gn_b = g.param(
        format!("{p}.sequential.3.bias"),
        Shape::new(&[d], DType::F32),
    );
    let dw_btd = nchw_to_btd(&mut g, dw, b, d, t);
    let out = group_norm_1group_btd(&mut g, dw_btd, gn_w, gn_b, d, 1e-5);
    g.set_outputs(vec![out]);
    g
}

/// Depthwise conv only (post-GLU, for bisection).
pub fn build_conv_dw_only_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_conv_dw_only");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}.conv_module");
    let d = spec.d;
    let b = spec.b;
    let t = spec.t;
    let k = spec.dw_kernel;
    let pad = k / 2;

    let glu = bct_to_nchw(&mut g, z_in, b, d, t);
    let dw_w = g.param(
        format!("{p}.sequential.2.weight"),
        Shape::new(&[d, 1, 1, k], DType::F32),
    );
    let dw_b = g.param(
        format!("{p}.sequential.2.bias"),
        Shape::new(&[d], DType::F32),
    );
    let mut dw = g.conv2d(glu, dw_w, [1, k], [1, 1], [0, pad], [1, 1], d);
    dw = add_bias_nchw(&mut g, dw, dw_b, d);
    let out = nchw_to_btd(&mut g, dw, b, d, t);
    g.set_outputs(vec![out]);
    g
}

/// Depthwise conv + norm + p2 (post-GLU conv-module tail, for bisection).
pub fn build_conv_dw_tail_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_conv_dw_tail");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}.conv_module");
    let d = spec.d;
    let b = spec.b;
    let t = spec.t;
    let k = spec.dw_kernel;
    let pad = k / 2;

    let glu = bct_to_nchw(&mut g, z_in, b, d, t);

    let dw_w = g.param(
        format!("{p}.sequential.2.weight"),
        Shape::new(&[d, 1, 1, k], DType::F32),
    );
    let dw_b = g.param(
        format!("{p}.sequential.2.bias"),
        Shape::new(&[d], DType::F32),
    );
    let mut dw = g.conv2d(glu, dw_w, [1, k], [1, 1], [0, pad], [1, 1], d);
    dw = add_bias_nchw(&mut g, dw, dw_b, d);

    let gn_w = g.param(
        format!("{p}.sequential.3.weight"),
        Shape::new(&[d], DType::F32),
    );
    let gn_b = g.param(
        format!("{p}.sequential.3.bias"),
        Shape::new(&[d], DType::F32),
    );
    let dw_btd = nchw_to_btd(&mut g, dw, b, d, t);
    let dw_btd = group_norm_1group_btd(&mut g, dw_btd, gn_w, gn_b, d, 1e-5);
    let dw_btd = g.silu(dw_btd);
    let dw4 = bct_to_nchw(&mut g, dw_btd, b, d, t);

    let p2_w = g.param(
        format!("{p}.sequential.5.weight"),
        Shape::new(&[d, d, 1, 1], DType::F32),
    );
    let p2_b = g.param(
        format!("{p}.sequential.5.bias"),
        Shape::new(&[d], DType::F32),
    );
    let out = g.conv2d(dw4, p2_w, [1, 1], [1, 1], [0, 0], [1, 1], 1);
    let out = add_bias_nchw(&mut g, out, p2_b, d);
    let out = nchw_to_btd(&mut g, out, b, d, t);
    g.set_outputs(vec![out]);
    g
}

/// Conv module block only.
pub fn build_conv_module_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_conv_module");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let out = conv_module(&mut g, z_in, layer, spec);
    g.set_outputs(vec![out]);
    g
}

/// FFN1 macaron + self-attention macaron (pre conv-module residual path).
pub fn build_pre_conv_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_pre_conv");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}");
    let d = spec.d;
    let half = scalar(&mut g, 0.5);

    let residual = z_in;
    let ffn1 = ffn_block(&mut g, z_in, &format!("{p}.ffn1"), spec.ffn_dim);
    let scaled = g.mul(ffn1, half);
    let x = g.add(scaled, residual);

    let residual = x;
    let sa_ln_w = g.param(
        format!("{p}.self_attn_layer_norm.weight"),
        Shape::new(&[d], DType::F32),
    );
    let sa_ln_b = g.param(
        format!("{p}.self_attn_layer_norm.bias"),
        Shape::new(&[d], DType::F32),
    );
    let xn = g.ln(x, sa_ln_w, sa_ln_b, 1e-5);
    let attn = self_attn(&mut g, xn, layer, spec);
    let out = g.add(attn, residual);
    g.set_outputs(vec![out]);
    g
}

/// FFN2 macaron + final layer norm (post conv-module residual path).
pub fn build_post_conv_graph(spec: &EncoderSpec, layer: usize) -> Graph {
    let mut g = Graph::new("brain2qwerty_post_conv");
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}");
    let d = spec.d;
    let half = scalar(&mut g, 0.5);

    let residual = z_in;
    let ffn2 = ffn_block(&mut g, z_in, &format!("{p}.ffn2"), spec.ffn_dim);
    let scaled = g.mul(ffn2, half);
    let x = g.add(scaled, residual);

    let fln_w = g.param(
        format!("{p}.final_layer_norm.weight"),
        Shape::new(&[d], DType::F32),
    );
    let fln_b = g.param(
        format!("{p}.final_layer_norm.bias"),
        Shape::new(&[d], DType::F32),
    );
    let out = g.ln(x, fln_w, fln_b, 1e-5);
    g.set_outputs(vec![out]);
    g
}

/// Single FFN macaron block (for parity bisection).
pub fn build_ffn_block_graph(spec: &EncoderSpec, layer: usize, which: &str) -> Graph {
    let mut g = Graph::new(format!("brain2qwerty_ffn_{which}"));
    let z_in = g.input("z", s3(spec.b, spec.t, spec.d));
    let p = format!("transformer.conformer_layers.{layer}.{which}");
    let out = ffn_block(&mut g, z_in, &p, spec.ffn_dim);
    g.set_outputs(vec![out]);
    g
}

fn conformer_layer(g: &mut Graph, x: NodeId, layer: usize, spec: &EncoderSpec) -> NodeId {
    let p = format!("transformer.conformer_layers.{layer}");
    let d = spec.d;
    let half = scalar(g, 0.5);

    let residual = x;
    let ffn1 = ffn_block(g, x, &format!("{p}.ffn1"), spec.ffn_dim);
    let scaled = g.mul(ffn1, half);
    let x = g.add(scaled, residual);

    let residual = x;
    let sa_ln_w = g.param(
        format!("{p}.self_attn_layer_norm.weight"),
        Shape::new(&[d], DType::F32),
    );
    let sa_ln_b = g.param(
        format!("{p}.self_attn_layer_norm.bias"),
        Shape::new(&[d], DType::F32),
    );
    let xn = g.ln(x, sa_ln_w, sa_ln_b, 1e-5);
    let attn = self_attn(g, xn, layer, spec);
    let x = g.add(attn, residual);

    let residual = x;
    let conv = conv_module(g, x, layer, spec);
    let x = g.add(conv, residual);

    let residual = x;
    let ffn2 = ffn_block(g, x, &format!("{p}.ffn2"), spec.ffn_dim);
    let scaled2 = g.mul(ffn2, half);
    let x = g.add(scaled2, residual);

    let fln_w = g.param(
        format!("{p}.final_layer_norm.weight"),
        Shape::new(&[d], DType::F32),
    );
    let fln_b = g.param(
        format!("{p}.final_layer_norm.bias"),
        Shape::new(&[d], DType::F32),
    );
    g.ln(x, fln_w, fln_b, 1e-5)
}

fn scalar(g: &mut Graph, v: f32) -> NodeId {
    g.append_node(
        Op::Constant {
            data: v.to_le_bytes().to_vec(),
        },
        vec![],
        Shape::new(&[1], DType::F32),
        None,
    )
}

/// GroupNorm with `num_groups=1` on `[B, T, D]`, matching the Rust reference.
fn group_norm_1group_btd(
    g: &mut Graph,
    x: NodeId,
    weight: NodeId,
    bias: NodeId,
    d: usize,
    eps: f32,
) -> NodeId {
    let eps_n = scalar(g, eps);
    let one = scalar(g, 1.0);
    let mean = g.mean(x, vec![1, 2], true);
    let centered = g.sub(x, mean);
    let sq = g.mul(centered, centered);
    let var = g.mean(sq, vec![1, 2], true);
    let var_eps = g.add(var, eps_n);
    let inv_denom = g.sqrt(var_eps);
    let inv = g.div(one, inv_denom);
    let normed = g.mul(centered, inv);
    let w = g.reshape_(weight, vec![1, 1, d as i64]);
    let b = g.reshape_(bias, vec![1, 1, d as i64]);
    let scaled = g.mul(normed, w);
    g.add(scaled, b)
}

fn ffn_block(g: &mut Graph, x: NodeId, prefix: &str, ffn_dim: usize) -> NodeId {
    let dim = g.shape(x).dims()[2].unwrap_static();
    let ln_w = g.param(
        format!("{prefix}.sequential.0.weight"),
        Shape::new(&[dim], DType::F32),
    );
    let ln_b = g.param(
        format!("{prefix}.sequential.0.bias"),
        Shape::new(&[dim], DType::F32),
    );
    let x = g.ln(x, ln_w, ln_b, 1e-5);
    let w1 = g.param(
        format!("{prefix}.sequential.1.weight"),
        Shape::new(&[dim, ffn_dim], DType::F32),
    );
    let b1 = g.param(
        format!("{prefix}.sequential.1.bias"),
        Shape::new(&[ffn_dim], DType::F32),
    );
    let mm1 = linear(g, x, w1, b1);
    let h = g.silu(mm1);
    let w2 = g.param(
        format!("{prefix}.sequential.4.weight"),
        Shape::new(&[ffn_dim, dim], DType::F32),
    );
    let b2 = g.param(
        format!("{prefix}.sequential.4.bias"),
        Shape::new(&[dim], DType::F32),
    );
    linear(g, h, w2, b2)
}

fn self_attn(g: &mut Graph, x: NodeId, layer: usize, spec: &EncoderSpec) -> NodeId {
    let p = format!("transformer.conformer_layers.{layer}.self_attn");
    let d = spec.d;
    let nh = spec.num_heads;
    let dh = d / nh;

    let in_w = g.param(
        format!("{p}.in_proj.weight"),
        Shape::new(&[d, 3 * d], DType::F32),
    );
    let in_b = g.param(
        format!("{p}.in_proj.bias"),
        Shape::new(&[3 * d], DType::F32),
    );
    let wo = g.param(
        format!("{p}.out_proj.weight"),
        Shape::new(&[d, d], DType::F32),
    );
    let bo = g.param(format!("{p}.out_proj.bias"), Shape::new(&[d], DType::F32));

    let qkv = linear(g, x, in_w, in_b);
    let q = g.narrow_(qkv, 2, 0, d);
    let k = g.narrow_(qkv, 2, d, d);
    let v = g.narrow_(qkv, 2, 2 * d, d);

    let b = spec.b;
    let s = spec.t;
    let q4 = g.reshape_(q, vec![b as i64, s as i64, nh as i64, dh as i64]);
    let k4 = g.reshape_(k, vec![b as i64, s as i64, nh as i64, dh as i64]);
    let v4 = g.reshape_(v, vec![b as i64, s as i64, nh as i64, dh as i64]);
    let q_bhs = g.transpose_(q4, vec![0, 2, 1, 3]);
    let k_bhs = g.transpose_(k4, vec![0, 2, 1, 3]);
    let v_bhs = g.transpose_(v4, vec![0, 2, 1, 3]);

    let attn_shape = shape::attention_shape(g.shape(q_bhs));
    let attn = g.attention_kind(q_bhs, k_bhs, v_bhs, nh, dh, MaskKind::None, attn_shape);
    let attn_bsh = g.transpose_(attn, vec![0, 2, 1, 3]);
    let attn_3 = g.reshape_(attn_bsh, vec![b as i64, s as i64, (nh * dh) as i64]);
    linear(g, attn_3, wo, bo)
}

fn sigmoid(g: &mut Graph, x: NodeId) -> NodeId {
    let neg_one = scalar(g, -1.0);
    let neg = g.mul(x, neg_one);
    let e = g.exp(neg);
    let one = scalar(g, 1.0);
    let denom = g.add(one, e);
    g.div(one, denom)
}

fn bct_to_nchw(g: &mut Graph, x: NodeId, b: usize, d: usize, t: usize) -> NodeId {
    let x_bdt = g.transpose_(x, vec![0, 2, 1]);
    g.reshape_(x_bdt, vec![b as i64, d as i64, 1, t as i64])
}

fn nchw_to_btd(g: &mut Graph, x: NodeId, b: usize, d: usize, t: usize) -> NodeId {
    let x3 = g.reshape_(x, vec![b as i64, d as i64, t as i64]);
    g.transpose_(x3, vec![0, 2, 1])
}

fn add_bias_nchw(g: &mut Graph, x: NodeId, bias: NodeId, channels: usize) -> NodeId {
    let b = g.reshape_(bias, vec![1, channels as i64, 1, 1]);
    g.add(x, b)
}

fn conv_module(g: &mut Graph, x: NodeId, layer: usize, spec: &EncoderSpec) -> NodeId {
    let _ = layer;
    let p = format!("transformer.conformer_layers.{layer}.conv_module");
    let d = spec.d;
    let b = spec.b;
    let t = spec.t;
    let k = spec.dw_kernel;
    let pad = k / 2;

    let ln_w = g.param(
        format!("{p}.layer_norm.weight"),
        Shape::new(&[d], DType::F32),
    );
    let ln_b = g.param(format!("{p}.layer_norm.bias"), Shape::new(&[d], DType::F32));
    let x = g.ln(x, ln_w, ln_b, 1e-5);
    let x4 = bct_to_nchw(g, x, b, d, t);

    let p1_w = g.param(
        format!("{p}.sequential.0.weight"),
        Shape::new(&[2 * d, d, 1, 1], DType::F32),
    );
    let p1_b = g.param(
        format!("{p}.sequential.0.bias"),
        Shape::new(&[2 * d], DType::F32),
    );
    let y = g.conv2d(x4, p1_w, [1, 1], [1, 1], [0, 0], [1, 1], 1);
    let y = add_bias_nchw(g, y, p1_b, 2 * d);

    let y0 = g.narrow_(y, 1, 0, d);
    let y1 = g.narrow_(y, 1, d, d);
    let gate = sigmoid(g, y1);
    let glu = g.mul(y0, gate);

    let dw_w = g.param(
        format!("{p}.sequential.2.weight"),
        Shape::new(&[d, 1, 1, k], DType::F32),
    );
    let dw_b = g.param(
        format!("{p}.sequential.2.bias"),
        Shape::new(&[d], DType::F32),
    );
    let mut dw = g.conv2d(glu, dw_w, [1, k], [1, 1], [0, pad], [1, 1], d);
    dw = add_bias_nchw(g, dw, dw_b, d);

    let gn_w = g.param(
        format!("{p}.sequential.3.weight"),
        Shape::new(&[d], DType::F32),
    );
    let gn_b = g.param(
        format!("{p}.sequential.3.bias"),
        Shape::new(&[d], DType::F32),
    );
    let dw_btd = nchw_to_btd(g, dw, b, d, t);
    let dw_btd = group_norm_1group_btd(g, dw_btd, gn_w, gn_b, d, 1e-5);
    let dw_btd = g.silu(dw_btd);
    let dw = bct_to_nchw(g, dw_btd, b, d, t);

    let p2_w = g.param(
        format!("{p}.sequential.5.weight"),
        Shape::new(&[d, d, 1, 1], DType::F32),
    );
    let p2_b = g.param(
        format!("{p}.sequential.5.bias"),
        Shape::new(&[d], DType::F32),
    );
    let out = g.conv2d(dw, p2_w, [1, 1], [1, 1], [0, 0], [1, 1], 1);
    let out = add_bias_nchw(g, out, p2_b, d);
    nchw_to_btd(g, out, b, d, t)
}