singe-kernel 0.1.0-alpha.4

Reusable CPU and GPU kernels.
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! Small convolution and layout-specific convolution helpers.

use crate::{
    error::{Error, Result},
    utility::checked_element_count,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CausalConv1dConfig {
    pub channels_in: usize,
    pub channels_out: usize,
    pub input_length: usize,
    pub kernel_size: usize,
    pub stride: usize,
    pub dilation: usize,
    pub groups: usize,
    pub left_padding: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Conv1dConfig {
    pub channels_in: usize,
    pub channels_out: usize,
    pub input_length: usize,
    pub kernel_size: usize,
    pub stride: usize,
    pub dilation: usize,
    pub groups: usize,
    pub left_padding: usize,
    pub right_padding: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BatchedConv1dConfig {
    pub batch: usize,
    pub channels_in: usize,
    pub channels_out: usize,
    pub input_length: usize,
    pub kernel_size: usize,
    pub stride: usize,
    pub dilation: usize,
    pub groups: usize,
    pub left_padding: usize,
    pub right_padding: usize,
    pub input_batch_stride: usize,
    pub output_batch_stride: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Conv1dIm2colConfig {
    pub batch: usize,
    pub channels_in: usize,
    pub input_length: usize,
    pub kernel_size: usize,
    pub stride: usize,
    pub dilation: usize,
    pub groups: usize,
    pub left_padding: usize,
    pub right_padding: usize,
    pub input_batch_stride: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Conv1dActivation {
    None,
    Gelu,
}

impl CausalConv1dConfig {
    pub fn output_length(self) -> Result<usize> {
        validate_causal_conv1d_config(self)?;
        conv1d_output_length(
            self.input_length,
            self.kernel_size,
            self.stride,
            self.dilation,
            self.left_padding,
            0,
        )
    }
}

impl Conv1dConfig {
    pub fn output_length(self) -> Result<usize> {
        validate_conv1d_config(self)?;
        conv1d_output_length(
            self.input_length,
            self.kernel_size,
            self.stride,
            self.dilation,
            self.left_padding,
            self.right_padding,
        )
    }
}

impl BatchedConv1dConfig {
    pub fn output_length(self) -> Result<usize> {
        validate_batched_conv1d_config(self)?;
        conv1d_output_length(
            self.input_length,
            self.kernel_size,
            self.stride,
            self.dilation,
            self.left_padding,
            self.right_padding,
        )
    }
}

impl Conv1dIm2colConfig {
    pub fn output_length(self) -> Result<usize> {
        validate_conv1d_im2col_config(self)?;
        conv1d_output_length(
            self.input_length,
            self.kernel_size,
            self.stride,
            self.dilation,
            self.left_padding,
            self.right_padding,
        )
    }

    pub fn output_values_per_batch(self) -> Result<usize> {
        let output_length = self.output_length()?;
        checked_element_count(
            checked_element_count(self.channels_in, output_length)?,
            self.kernel_size,
        )
    }
}

pub fn conv1d_causal(input: &[f32], weight: &[f32], config: CausalConv1dConfig) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let channels_in_per_group = config.channels_in / config.groups;
    let channels_out_per_group = config.channels_out / config.groups;
    let mut output = vec![0.0; config.channels_out * output_length];
    for out_channel in 0..config.channels_out {
        let group = out_channel / channels_out_per_group;
        let input_channel_start = group * channels_in_per_group;
        for out_pos in 0..output_length {
            let mut sum = 0.0f32;
            for group_input_channel in 0..channels_in_per_group {
                let input_channel = input_channel_start + group_input_channel;
                for kernel_index in 0..config.kernel_size {
                    let raw_input_pos = out_pos * config.stride + kernel_index * config.dilation;
                    if raw_input_pos < config.left_padding {
                        continue;
                    }
                    let input_pos = raw_input_pos - config.left_padding;
                    if input_pos >= config.input_length {
                        continue;
                    }
                    let input_value = input[input_channel * config.input_length + input_pos];
                    let weight_value = weight[(out_channel * channels_in_per_group
                        + group_input_channel)
                        * config.kernel_size
                        + kernel_index];
                    sum += input_value * weight_value;
                }
            }
            output[out_channel * output_length + out_pos] = sum;
        }
    }
    output
}

pub fn conv1d(input: &[f32], weight: &[f32], config: Conv1dConfig) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let channels_in_per_group = config.channels_in / config.groups;
    let channels_out_per_group = config.channels_out / config.groups;
    let mut output = vec![0.0; config.channels_out * output_length];
    for out_channel in 0..config.channels_out {
        let group = out_channel / channels_out_per_group;
        let input_channel_start = group * channels_in_per_group;
        for out_pos in 0..output_length {
            let mut sum = 0.0f32;
            for group_input_channel in 0..channels_in_per_group {
                let input_channel = input_channel_start + group_input_channel;
                for kernel_index in 0..config.kernel_size {
                    let raw_input_pos = out_pos * config.stride + kernel_index * config.dilation;
                    if raw_input_pos < config.left_padding {
                        continue;
                    }
                    let input_pos = raw_input_pos - config.left_padding;
                    if input_pos >= config.input_length {
                        continue;
                    }
                    let input_value = input[input_channel * config.input_length + input_pos];
                    let weight_value = weight[(out_channel * channels_in_per_group
                        + group_input_channel)
                        * config.kernel_size
                        + kernel_index];
                    sum += input_value * weight_value;
                }
            }
            output[out_channel * output_length + out_pos] = sum;
        }
    }
    output
}

pub fn conv1d_bias_activation(
    input: &[f32],
    weight: &[f32],
    bias: &[f32],
    config: Conv1dConfig,
    activation: Conv1dActivation,
) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let mut output = conv1d(input, weight, config);
    for out_channel in 0..config.channels_out {
        for out_pos in 0..output_length {
            let value = &mut output[out_channel * output_length + out_pos];
            *value += bias[out_channel];
            *value = apply_activation(*value, activation);
        }
    }
    output
}

pub fn conv1d_batched(input: &[f32], weight: &[f32], config: BatchedConv1dConfig) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let output_len = batched_reach(
        config.batch,
        config.output_batch_stride,
        config.channels_out,
        output_length,
    );
    let channels_in_per_group = config.channels_in / config.groups;
    let channels_out_per_group = config.channels_out / config.groups;
    let mut output = vec![0.0; output_len];
    for batch in 0..config.batch {
        let input_batch_base = batch * config.input_batch_stride;
        let output_batch_base = batch * config.output_batch_stride;
        for out_channel in 0..config.channels_out {
            let group = out_channel / channels_out_per_group;
            let input_channel_start = group * channels_in_per_group;
            for out_pos in 0..output_length {
                let mut sum = 0.0f32;
                for group_input_channel in 0..channels_in_per_group {
                    let input_channel = input_channel_start + group_input_channel;
                    for kernel_index in 0..config.kernel_size {
                        let raw_input_pos =
                            out_pos * config.stride + kernel_index * config.dilation;
                        if raw_input_pos < config.left_padding {
                            continue;
                        }
                        let input_pos = raw_input_pos - config.left_padding;
                        if input_pos >= config.input_length {
                            continue;
                        }
                        let input_value = input
                            [input_batch_base + input_channel * config.input_length + input_pos];
                        let weight_value = weight[(out_channel * channels_in_per_group
                            + group_input_channel)
                            * config.kernel_size
                            + kernel_index];
                        sum += input_value * weight_value;
                    }
                }
                output[output_batch_base + out_channel * output_length + out_pos] = sum;
            }
        }
    }
    output
}

pub fn conv1d_batched_im2col(input: &[f32], config: Conv1dIm2colConfig) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let channels_in_per_group = config.channels_in / config.groups;
    let output_values_per_batch = config.output_values_per_batch().expect("output values");
    let mut output = vec![0.0; config.batch * output_values_per_batch];
    for batch in 0..config.batch {
        let input_batch_base = batch * config.input_batch_stride;
        let output_batch_base = batch * output_values_per_batch;
        for group in 0..config.groups {
            for out_pos in 0..output_length {
                for group_input_channel in 0..channels_in_per_group {
                    let input_channel = group * channels_in_per_group + group_input_channel;
                    for kernel_index in 0..config.kernel_size {
                        let output_offset = output_batch_base
                            + (((group * output_length + out_pos) * channels_in_per_group
                                + group_input_channel)
                                * config.kernel_size
                                + kernel_index);
                        let raw_input_pos =
                            out_pos * config.stride + kernel_index * config.dilation;
                        if raw_input_pos < config.left_padding {
                            continue;
                        }
                        let input_pos = raw_input_pos - config.left_padding;
                        if input_pos >= config.input_length {
                            continue;
                        }
                        output[output_offset] = input
                            [input_batch_base + input_channel * config.input_length + input_pos];
                    }
                }
            }
        }
    }
    output
}

pub fn conv1d_batched_from_im2col(
    columns: &[f32],
    weight: &[f32],
    config: BatchedConv1dConfig,
) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let output_len = batched_reach(
        config.batch,
        config.output_batch_stride,
        config.channels_out,
        output_length,
    );
    let channels_in_per_group = config.channels_in / config.groups;
    let channels_out_per_group = config.channels_out / config.groups;
    let column_values_per_batch = config.channels_in * output_length * config.kernel_size;
    let mut output = vec![0.0; output_len];
    for batch in 0..config.batch {
        let column_batch_base = batch * column_values_per_batch;
        let output_batch_base = batch * config.output_batch_stride;
        for out_channel in 0..config.channels_out {
            let group = out_channel / channels_out_per_group;
            for out_pos in 0..output_length {
                let mut sum = 0.0f32;
                for group_input_channel in 0..channels_in_per_group {
                    for kernel_index in 0..config.kernel_size {
                        let column_offset = column_batch_base
                            + (((group * output_length + out_pos) * channels_in_per_group
                                + group_input_channel)
                                * config.kernel_size
                                + kernel_index);
                        let weight_offset = (out_channel * channels_in_per_group
                            + group_input_channel)
                            * config.kernel_size
                            + kernel_index;
                        sum += columns[column_offset] * weight[weight_offset];
                    }
                }
                output[output_batch_base + out_channel * output_length + out_pos] = sum;
            }
        }
    }
    output
}

pub fn conv1d_causal_bias(
    input: &[f32],
    weight: &[f32],
    bias: &[f32],
    config: CausalConv1dConfig,
) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let mut output = conv1d_causal(input, weight, config);
    for out_channel in 0..config.channels_out {
        for out_pos in 0..output_length {
            output[out_channel * output_length + out_pos] += bias[out_channel];
        }
    }
    output
}

pub fn conv1d_causal_bias_gelu(
    input: &[f32],
    weight: &[f32],
    bias: &[f32],
    config: CausalConv1dConfig,
) -> Vec<f32> {
    let mut output = conv1d_causal_bias(input, weight, bias, config);
    for value in &mut output {
        *value = gelu(*value);
    }
    output
}

pub fn conv1d_causal_bias_activation(
    input: &[f32],
    weight: &[f32],
    bias: &[f32],
    config: CausalConv1dConfig,
    activation: Conv1dActivation,
) -> Vec<f32> {
    let output_length = config.output_length().expect("output length");
    let mut output = conv1d_causal_bias(input, weight, bias, config);
    for out_channel in 0..config.channels_out {
        for out_pos in 0..output_length {
            let value = &mut output[out_channel * output_length + out_pos];
            *value = apply_activation(*value, activation);
        }
    }
    output
}

fn batched_reach(batch: usize, batch_stride: usize, channels: usize, length: usize) -> usize {
    (batch - 1) * batch_stride + channels * length
}

fn gelu(value: f32) -> f32 {
    0.5 * value * (1.0 + (0.7978846 * (value + 0.044715 * value * value * value)).tanh())
}

fn apply_activation(value: f32, activation: Conv1dActivation) -> f32 {
    match activation {
        Conv1dActivation::None => value,
        Conv1dActivation::Gelu => gelu(value),
    }
}

fn validate_conv1d_config(config: Conv1dConfig) -> Result<()> {
    validate_conv1d_shape(
        config.channels_in,
        config.channels_out,
        config.input_length,
        config.kernel_size,
        config.stride,
        config.dilation,
        config.groups,
    )
}

fn validate_batched_conv1d_config(config: BatchedConv1dConfig) -> Result<()> {
    if config.batch == 0 || config.input_batch_stride == 0 || config.output_batch_stride == 0 {
        return Err(Error::InvalidLength);
    }
    validate_conv1d_shape(
        config.channels_in,
        config.channels_out,
        config.input_length,
        config.kernel_size,
        config.stride,
        config.dilation,
        config.groups,
    )?;
    let input_item_len = checked_element_count(config.channels_in, config.input_length)?;
    let output_length = conv1d_output_length(
        config.input_length,
        config.kernel_size,
        config.stride,
        config.dilation,
        config.left_padding,
        config.right_padding,
    )?;
    let output_item_len = checked_element_count(config.channels_out, output_length)?;
    if config.input_batch_stride < input_item_len || config.output_batch_stride < output_item_len {
        return Err(Error::InvalidLength);
    }
    Ok(())
}

fn validate_conv1d_im2col_config(config: Conv1dIm2colConfig) -> Result<()> {
    if config.batch == 0 || config.input_batch_stride == 0 {
        return Err(Error::InvalidLength);
    }
    validate_conv1d_shape(
        config.channels_in,
        config.channels_in,
        config.input_length,
        config.kernel_size,
        config.stride,
        config.dilation,
        config.groups,
    )?;
    let input_item_len = checked_element_count(config.channels_in, config.input_length)?;
    if config.input_batch_stride < input_item_len {
        return Err(Error::InvalidLength);
    }
    Ok(())
}

fn validate_causal_conv1d_config(config: CausalConv1dConfig) -> Result<()> {
    validate_conv1d_shape(
        config.channels_in,
        config.channels_out,
        config.input_length,
        config.kernel_size,
        config.stride,
        config.dilation,
        config.groups,
    )
}

fn validate_conv1d_shape(
    channels_in: usize,
    channels_out: usize,
    input_length: usize,
    kernel_size: usize,
    stride: usize,
    dilation: usize,
    groups: usize,
) -> Result<()> {
    if channels_in == 0
        || channels_out == 0
        || input_length == 0
        || kernel_size == 0
        || stride == 0
        || dilation == 0
        || groups == 0
    {
        return Err(Error::InvalidLength);
    }
    if !channels_in.is_multiple_of(groups) || !channels_out.is_multiple_of(groups) {
        return Err(Error::InvalidLength);
    }
    effective_kernel_size(kernel_size, dilation)?;
    Ok(())
}

fn conv1d_output_length(
    input_length: usize,
    kernel_size: usize,
    stride: usize,
    dilation: usize,
    left_padding: usize,
    right_padding: usize,
) -> Result<usize> {
    let padded = input_length
        .checked_add(left_padding)
        .and_then(|padded| padded.checked_add(right_padding))
        .ok_or(Error::SizeOverflow)?;
    let effective_kernel_size = effective_kernel_size(kernel_size, dilation)?;
    if padded < effective_kernel_size {
        return Ok(0);
    }
    Ok((padded - effective_kernel_size) / stride + 1)
}

fn effective_kernel_size(kernel_size: usize, dilation: usize) -> Result<usize> {
    kernel_size
        .checked_sub(1)
        .ok_or(Error::SizeOverflow)?
        .checked_mul(dilation)
        .and_then(|size| size.checked_add(1))
        .ok_or(Error::SizeOverflow)
}

/// Depthwise causal conv1d decode step with in-place rolling state and SiLU.
///
/// `input` and `out` are `[batch, channels]`. `conv_state` is
/// `[batch, channels, kernel_size]` and is shifted left by one slot per
/// channel before inserting the new input at `kernel_size - 1`. `weight` is
/// `[channels, kernel_size]`, `bias` is `[channels]`, and the returned output
/// is `silu(dot(updated_state, weight) + bias)`.
pub fn causal_conv1d_update_silu(
    conv_state: &[f32],
    input: &[f32],
    weight: &[f32],
    bias: &[f32],
    batch: usize,
    channels: usize,
    kernel_size: usize,
) -> (Vec<f32>, Vec<f32>) {
    let mut out = vec![0.0f32; batch * channels];
    let mut next_state = conv_state.to_vec();
    for batch_index in 0..batch {
        for (channel, bias_value) in bias.iter().copied().enumerate().take(channels) {
            let token_offset = batch_index * channels + channel;
            let state_offset = (batch_index * channels + channel) * kernel_size;
            let weight_offset = channel * kernel_size;
            let mut dot = bias_value;
            for kernel in 0..kernel_size {
                let value = if kernel + 1 == kernel_size {
                    input[token_offset]
                } else {
                    conv_state[state_offset + kernel + 1]
                };
                dot += value * weight[weight_offset + kernel];
            }
            out[token_offset] = dot / (1.0 + (-dot).exp());
            for kernel in 0..kernel_size - 1 {
                next_state[state_offset + kernel] = conv_state[state_offset + kernel + 1];
            }
            next_state[state_offset + kernel_size - 1] = input[token_offset];
        }
    }
    (out, next_state)
}

/// Depthwise causal conv1d sequence prefill from an implicit zero state.
///
/// `input` and `out` use `[batch, channels, time]` layout. For each output
/// position, values before the beginning of the sequence are zero-padded. The
/// returned state is the final `[batch, channels, kernel_size]` rolling window,
/// also left-padded with zeros when the input is shorter than the kernel.
pub fn causal_conv1d_prefill_silu(
    input: &[f32],
    weight: &[f32],
    bias: &[f32],
    batch: usize,
    channels: usize,
    kernel_size: usize,
    input_length: usize,
    output_length: usize,
) -> (Vec<f32>, Vec<f32>) {
    let mut out = vec![0.0f32; batch * channels * output_length];
    let mut final_state = vec![0.0f32; batch * channels * kernel_size];
    for batch_index in 0..batch {
        for (channel, bias_value) in bias.iter().copied().enumerate().take(channels) {
            let input_offset = (batch_index * channels + channel) * input_length;
            let output_offset = (batch_index * channels + channel) * output_length;
            let weight_offset = channel * kernel_size;
            let state_offset = (batch_index * channels + channel) * kernel_size;

            for time in 0..output_length {
                let mut dot = bias_value;
                for kernel in 0..kernel_size {
                    let input_time = time as isize + kernel as isize + 1 - kernel_size as isize;
                    let value = if input_time >= 0 && (input_time as usize) < input_length {
                        input[input_offset + input_time as usize]
                    } else {
                        0.0
                    };
                    dot += value * weight[weight_offset + kernel];
                }
                out[output_offset + time] = dot / (1.0 + (-dot).exp());
            }

            for kernel in 0..kernel_size {
                let input_time = input_length as isize + kernel as isize - kernel_size as isize;
                final_state[state_offset + kernel] =
                    if input_time >= 0 && (input_time as usize) < input_length {
                        input[input_offset + input_time as usize]
                    } else {
                        0.0
                    };
            }
        }
    }
    (out, final_state)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn causal_conv1d_prefill_matches_repeated_update_from_zero_state() {
        let batch = 2usize;
        let channels = 3usize;
        let kernel_size = 5usize;
        let input_length = 4usize;
        let input = (0..batch * channels * input_length)
            .map(|index| (index as f32 % 11.0) * 0.125 - 0.5)
            .collect::<Vec<_>>();
        let weight = (0..channels * kernel_size)
            .map(|index| (index as f32 % 7.0) * 0.25 - 0.75)
            .collect::<Vec<_>>();
        let bias = (0..channels)
            .map(|index| (index as f32 % 5.0) * 0.125 - 0.25)
            .collect::<Vec<_>>();

        let (actual_out, actual_state) = causal_conv1d_prefill_silu(
            &input,
            &weight,
            &bias,
            batch,
            channels,
            kernel_size,
            input_length,
            input_length,
        );
        let mut expected_out = vec![0.0f32; batch * channels * input_length];
        let mut state = vec![0.0f32; batch * channels * kernel_size];
        for time in 0..input_length {
            let token = (0..batch * channels)
                .map(|index| {
                    let batch_index = index / channels;
                    let channel = index % channels;
                    input[(batch_index * channels + channel) * input_length + time]
                })
                .collect::<Vec<_>>();
            let (step_out, next_state) = causal_conv1d_update_silu(
                &state,
                &token,
                &weight,
                &bias,
                batch,
                channels,
                kernel_size,
            );
            for batch_index in 0..batch {
                for channel in 0..channels {
                    expected_out[(batch_index * channels + channel) * input_length + time] =
                        step_out[batch_index * channels + channel];
                }
            }
            state = next_state;
        }

        assert_eq!(actual_out, expected_out);
        assert_eq!(actual_state, state);
    }

    #[test]
    fn causal_conv1d_prefill_final_state_zero_pads_short_input() {
        let input = [1.0f32, 2.0];
        let weight = [1.0f32, 1.0, 1.0, 1.0];
        let bias = [0.0f32];
        let (_, state) = causal_conv1d_prefill_silu(&input, &weight, &bias, 1, 1, 4, 2, 2);
        assert_eq!(state, vec![0.0, 0.0, 1.0, 2.0]);
    }
}