proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
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
//! Neural network model construction and execution.

use super::tensor::Tensor;
use std::io::{Read, Write};

/// A single layer in a neural network.
#[derive(Debug, Clone)]
pub enum Layer {
    Dense(DenseLayer),
    Conv2D(Conv2DLayer),
    MaxPool(MaxPoolLayer),
    BatchNorm(BatchNormLayer),
    Dropout(DropoutLayer),
    Flatten,
    ReLU,
    Sigmoid,
    Tanh,
    Softmax(usize), // axis
    GELU,
    Residual(ResidualBlock),
    Attention(MultiHeadAttention),
}

#[derive(Debug, Clone)]
pub struct DenseLayer {
    pub weights: Tensor, // (in_features, out_features)
    pub bias: Tensor,    // (out_features,)
}

#[derive(Debug, Clone)]
pub struct Conv2DLayer {
    pub filters: Tensor, // (c_out, c_in, kh, kw)
    pub bias: Tensor,    // (c_out,)
    pub stride: usize,
    pub padding: usize,
}

#[derive(Debug, Clone)]
pub struct MaxPoolLayer {
    pub kernel_size: usize,
    pub stride: usize,
}

#[derive(Debug, Clone)]
pub struct BatchNormLayer {
    pub gamma: Tensor,
    pub beta: Tensor,
    pub running_mean: Tensor,
    pub running_var: Tensor,
    pub eps: f32,
}

#[derive(Debug, Clone)]
pub struct DropoutLayer {
    pub p: f32,
    pub training: bool,
}

#[derive(Debug, Clone)]
pub struct ResidualBlock {
    pub layers: Vec<Layer>,
}

#[derive(Debug, Clone)]
pub struct MultiHeadAttention {
    pub heads: usize,
    pub d_model: usize,
    pub d_k: usize,
    pub w_q: Tensor, // (d_model, d_model)
    pub w_k: Tensor,
    pub w_v: Tensor,
    pub w_o: Tensor,
}

impl DenseLayer {
    pub fn new(in_features: usize, out_features: usize) -> Self {
        // Xavier initialization
        let scale = (2.0 / (in_features + out_features) as f32).sqrt();
        let w = Tensor::rand(vec![in_features, out_features], (in_features * out_features) as u64);
        let weights = Tensor {
            shape: w.shape.clone(),
            data: w.data.iter().map(|v| (v - 0.5) * 2.0 * scale).collect(),
        };
        let bias = Tensor::zeros(vec![out_features]);
        Self { weights, bias }
    }

    pub fn forward(&self, input: &Tensor) -> Tensor {
        // input: (batch, in_features) or (in_features,)
        let is_1d = input.shape.len() == 1;
        let input_2d = if is_1d {
            input.reshape(vec![1, input.shape[0]])
        } else {
            input.clone()
        };
        // out = input @ weights + bias
        let mut out = Tensor::matmul(&input_2d, &self.weights);
        // add bias to each row
        let batch = out.shape[0];
        let out_f = out.shape[1];
        for b in 0..batch {
            for j in 0..out_f {
                out.data[b * out_f + j] += self.bias.data[j];
            }
        }
        if is_1d { out.reshape(vec![out_f]) } else { out }
    }

    pub fn parameter_count(&self) -> usize {
        self.weights.data.len() + self.bias.data.len()
    }
}

impl Conv2DLayer {
    pub fn new(in_channels: usize, out_channels: usize, kernel_size: usize) -> Self {
        let n = out_channels * in_channels * kernel_size * kernel_size;
        let scale = (2.0 / (in_channels * kernel_size * kernel_size) as f32).sqrt();
        let r = Tensor::rand(vec![out_channels, in_channels, kernel_size, kernel_size], n as u64);
        let filters = Tensor {
            shape: r.shape.clone(),
            data: r.data.iter().map(|v| (v - 0.5) * 2.0 * scale).collect(),
        };
        let bias = Tensor::zeros(vec![out_channels]);
        Self { filters, bias, stride: 1, padding: 0 }
    }

    pub fn forward(&self, input: &Tensor) -> Tensor {
        let mut out = input.conv2d(&self.filters, self.stride, self.padding);
        // add bias per channel
        let c_out = out.shape[0];
        let spatial: usize = out.shape[1..].iter().product();
        for c in 0..c_out {
            for s in 0..spatial {
                out.data[c * spatial + s] += self.bias.data[c];
            }
        }
        out
    }

    pub fn parameter_count(&self) -> usize {
        self.filters.data.len() + self.bias.data.len()
    }
}

impl MultiHeadAttention {
    pub fn new(heads: usize, d_model: usize) -> Self {
        let d_k = d_model / heads;
        let init = |seed: u64| {
            let r = Tensor::rand(vec![d_model, d_model], seed);
            let scale = (1.0 / d_model as f32).sqrt();
            Tensor {
                shape: r.shape.clone(),
                data: r.data.iter().map(|v| (v - 0.5) * 2.0 * scale).collect(),
            }
        };
        Self {
            heads,
            d_model,
            d_k,
            w_q: init(1001),
            w_k: init(2002),
            w_v: init(3003),
            w_o: init(4004),
        }
    }

    /// Forward pass. Input shape: (seq_len, d_model). Returns same shape.
    pub fn forward(&self, input: &Tensor) -> Tensor {
        assert_eq!(input.shape.len(), 2);
        let seq_len = input.shape[0];
        let d_model = input.shape[1];
        assert_eq!(d_model, self.d_model);

        let q = Tensor::matmul(input, &self.w_q);
        let k = Tensor::matmul(input, &self.w_k);
        let v = Tensor::matmul(input, &self.w_v);

        let d_k = self.d_k;
        let scale = 1.0 / (d_k as f32).sqrt();

        // accumulate multi-head output
        let mut concat_heads = vec![0.0f32; seq_len * d_model];

        for h in 0..self.heads {
            let offset = h * d_k;
            // extract head slices (seq_len, d_k) for Q, K, V
            let mut qh = vec![0.0f32; seq_len * d_k];
            let mut kh = vec![0.0f32; seq_len * d_k];
            let mut vh = vec![0.0f32; seq_len * d_k];
            for s in 0..seq_len {
                for j in 0..d_k {
                    qh[s * d_k + j] = q.data[s * d_model + offset + j];
                    kh[s * d_k + j] = k.data[s * d_model + offset + j];
                    vh[s * d_k + j] = v.data[s * d_model + offset + j];
                }
            }
            let qh = Tensor::from_vec(qh, vec![seq_len, d_k]);
            let kh_t = Tensor::from_vec(kh, vec![seq_len, d_k]).transpose();
            let vh = Tensor::from_vec(vh, vec![seq_len, d_k]);

            // scores = Q @ K^T / sqrt(d_k)
            let scores = Tensor::matmul(&qh, &kh_t).scale(scale);
            // attention weights via softmax over last axis
            let attn = scores.softmax(1);
            // context = attn @ V
            let context = Tensor::matmul(&attn, &vh);

            // write into concat buffer
            for s in 0..seq_len {
                for j in 0..d_k {
                    concat_heads[s * d_model + offset + j] = context.data[s * d_k + j];
                }
            }
        }

        let concat = Tensor::from_vec(concat_heads, vec![seq_len, d_model]);
        Tensor::matmul(&concat, &self.w_o)
    }

    pub fn parameter_count(&self) -> usize {
        self.w_q.data.len() + self.w_k.data.len() + self.w_v.data.len() + self.w_o.data.len()
    }
}

impl Layer {
    pub fn forward(&self, input: &Tensor) -> Tensor {
        match self {
            Layer::Dense(l) => l.forward(input),
            Layer::Conv2D(l) => l.forward(input),
            Layer::MaxPool(l) => input.max_pool2d(l.kernel_size, l.stride),
            Layer::BatchNorm(l) => {
                input.batch_norm(&l.running_mean, &l.running_var, &l.gamma, &l.beta, l.eps)
            }
            Layer::Dropout(l) => input.dropout(l.p, 12345, l.training),
            Layer::Flatten => input.flatten(),
            Layer::ReLU => input.relu(),
            Layer::Sigmoid => input.sigmoid(),
            Layer::Tanh => input.tanh_act(),
            Layer::Softmax(axis) => input.softmax(*axis),
            Layer::GELU => input.gelu(),
            Layer::Residual(block) => {
                let mut out = input.clone();
                for layer in &block.layers {
                    out = layer.forward(&out);
                }
                input.add(&out)
            }
            Layer::Attention(attn) => attn.forward(input),
        }
    }

    pub fn parameter_count(&self) -> usize {
        match self {
            Layer::Dense(l) => l.parameter_count(),
            Layer::Conv2D(l) => l.parameter_count(),
            Layer::BatchNorm(l) => l.gamma.data.len() + l.beta.data.len(),
            Layer::Attention(a) => a.parameter_count(),
            Layer::Residual(block) => block.layers.iter().map(|l| l.parameter_count()).sum(),
            _ => 0,
        }
    }

    pub fn name(&self) -> &str {
        match self {
            Layer::Dense(_) => "Dense",
            Layer::Conv2D(_) => "Conv2D",
            Layer::MaxPool(_) => "MaxPool",
            Layer::BatchNorm(_) => "BatchNorm",
            Layer::Dropout(_) => "Dropout",
            Layer::Flatten => "Flatten",
            Layer::ReLU => "ReLU",
            Layer::Sigmoid => "Sigmoid",
            Layer::Tanh => "Tanh",
            Layer::Softmax(_) => "Softmax",
            Layer::GELU => "GELU",
            Layer::Residual(_) => "Residual",
            Layer::Attention(_) => "Attention",
        }
    }
}

/// A sequential neural network model.
#[derive(Debug, Clone)]
pub struct Model {
    pub layers: Vec<Layer>,
    pub name: String,
}

impl Model {
    pub fn new(name: &str) -> Self {
        Self { layers: Vec::new(), name: name.to_string() }
    }

    pub fn forward(&self, input: &Tensor) -> Tensor {
        let mut x = input.clone();
        for layer in &self.layers {
            x = layer.forward(&x);
        }
        x
    }

    pub fn parameter_count(&self) -> usize {
        self.layers.iter().map(|l| l.parameter_count()).sum()
    }

    /// Collect all weight tensors from the model in order.
    fn collect_weights(&self) -> Vec<&Tensor> {
        let mut weights = Vec::new();
        for layer in &self.layers {
            match layer {
                Layer::Dense(l) => { weights.push(&l.weights); weights.push(&l.bias); }
                Layer::Conv2D(l) => { weights.push(&l.filters); weights.push(&l.bias); }
                Layer::BatchNorm(l) => {
                    weights.push(&l.gamma); weights.push(&l.beta);
                    weights.push(&l.running_mean); weights.push(&l.running_var);
                }
                Layer::Attention(a) => {
                    weights.push(&a.w_q); weights.push(&a.w_k);
                    weights.push(&a.w_v); weights.push(&a.w_o);
                }
                Layer::Residual(block) => {
                    // For simplicity, build a temp Model to reuse logic
                    let m = Model { layers: block.layers.clone(), name: String::new() };
                    // We can't easily return refs into block here without
                    // restructuring, so we skip residual sub-weights in save/load.
                    let _ = m;
                }
                _ => {}
            }
        }
        weights
    }

    /// Save weights to a simple binary format:
    /// For each tensor: [ndim: u32] [shape[0]: u32] ... [shape[n-1]: u32] [data as f32 LE bytes]
    pub fn save_weights(&self, path: &str) -> Result<(), String> {
        let mut file = std::fs::File::create(path).map_err(|e| e.to_string())?;
        let weights = self.collect_weights();
        let count = weights.len() as u32;
        file.write_all(&count.to_le_bytes()).map_err(|e| e.to_string())?;
        for w in weights {
            let ndim = w.shape.len() as u32;
            file.write_all(&ndim.to_le_bytes()).map_err(|e| e.to_string())?;
            for &d in &w.shape {
                file.write_all(&(d as u32).to_le_bytes()).map_err(|e| e.to_string())?;
            }
            for &v in &w.data {
                file.write_all(&v.to_le_bytes()).map_err(|e| e.to_string())?;
            }
        }
        Ok(())
    }

    /// Load weights from the binary format written by `save_weights`.
    pub fn load_weights(&mut self, path: &str) -> Result<(), String> {
        let mut file = std::fs::File::open(path).map_err(|e| e.to_string())?;
        let mut buf4 = [0u8; 4];

        file.read_exact(&mut buf4).map_err(|e| e.to_string())?;
        let count = u32::from_le_bytes(buf4) as usize;

        let mut tensors = Vec::with_capacity(count);
        for _ in 0..count {
            file.read_exact(&mut buf4).map_err(|e| e.to_string())?;
            let ndim = u32::from_le_bytes(buf4) as usize;
            let mut shape = Vec::with_capacity(ndim);
            for _ in 0..ndim {
                file.read_exact(&mut buf4).map_err(|e| e.to_string())?;
                shape.push(u32::from_le_bytes(buf4) as usize);
            }
            let n: usize = shape.iter().product();
            let mut data = Vec::with_capacity(n);
            for _ in 0..n {
                file.read_exact(&mut buf4).map_err(|e| e.to_string())?;
                data.push(f32::from_le_bytes(buf4));
            }
            tensors.push(Tensor { shape, data });
        }

        // Assign weights back to layers
        let mut idx = 0;
        for layer in &mut self.layers {
            match layer {
                Layer::Dense(l) => {
                    if idx + 1 < tensors.len() {
                        l.weights = tensors[idx].clone();
                        l.bias = tensors[idx + 1].clone();
                        idx += 2;
                    }
                }
                Layer::Conv2D(l) => {
                    if idx + 1 < tensors.len() {
                        l.filters = tensors[idx].clone();
                        l.bias = tensors[idx + 1].clone();
                        idx += 2;
                    }
                }
                Layer::BatchNorm(l) => {
                    if idx + 3 < tensors.len() {
                        l.gamma = tensors[idx].clone();
                        l.beta = tensors[idx + 1].clone();
                        l.running_mean = tensors[idx + 2].clone();
                        l.running_var = tensors[idx + 3].clone();
                        idx += 4;
                    }
                }
                Layer::Attention(a) => {
                    if idx + 3 < tensors.len() {
                        a.w_q = tensors[idx].clone();
                        a.w_k = tensors[idx + 1].clone();
                        a.w_v = tensors[idx + 2].clone();
                        a.w_o = tensors[idx + 3].clone();
                        idx += 4;
                    }
                }
                _ => {}
            }
        }
        Ok(())
    }
}

/// Human-readable model summary.
pub struct ModelSummary;

impl ModelSummary {
    pub fn print(model: &Model) -> String {
        let mut lines = Vec::new();
        lines.push(format!("Model: {}", model.name));
        lines.push(format!("{:-<60}", ""));
        lines.push(format!("{:<20} {:>20} {:>15}", "Layer", "Output Shape", "Params"));
        lines.push(format!("{:-<60}", ""));
        for (i, layer) in model.layers.iter().enumerate() {
            let params = layer.parameter_count();
            lines.push(format!("{:<20} {:>20} {:>15}", format!("{}_{}", layer.name(), i), "dynamic", params));
        }
        lines.push(format!("{:-<60}", ""));
        lines.push(format!("Total parameters: {}", model.parameter_count()));
        lines.join("\n")
    }
}

/// Builder for constructing models layer by layer.
pub struct Sequential {
    layers: Vec<Layer>,
    name: String,
}

impl Sequential {
    pub fn new(name: &str) -> Self {
        Self { layers: Vec::new(), name: name.to_string() }
    }

    pub fn dense(mut self, in_features: usize, out_features: usize) -> Self {
        self.layers.push(Layer::Dense(DenseLayer::new(in_features, out_features)));
        self
    }

    pub fn conv2d(mut self, in_channels: usize, out_channels: usize, kernel_size: usize) -> Self {
        self.layers.push(Layer::Conv2D(Conv2DLayer::new(in_channels, out_channels, kernel_size)));
        self
    }

    pub fn max_pool(mut self, kernel_size: usize, stride: usize) -> Self {
        self.layers.push(Layer::MaxPool(MaxPoolLayer { kernel_size, stride }));
        self
    }

    pub fn batch_norm(mut self, num_features: usize) -> Self {
        self.layers.push(Layer::BatchNorm(BatchNormLayer {
            gamma: Tensor::ones(vec![num_features]),
            beta: Tensor::zeros(vec![num_features]),
            running_mean: Tensor::zeros(vec![num_features]),
            running_var: Tensor::ones(vec![num_features]),
            eps: 1e-5,
        }));
        self
    }

    pub fn dropout(mut self, p: f32) -> Self {
        self.layers.push(Layer::Dropout(DropoutLayer { p, training: true }));
        self
    }

    pub fn flatten(mut self) -> Self {
        self.layers.push(Layer::Flatten);
        self
    }

    pub fn relu(mut self) -> Self {
        self.layers.push(Layer::ReLU);
        self
    }

    pub fn sigmoid(mut self) -> Self {
        self.layers.push(Layer::Sigmoid);
        self
    }

    pub fn tanh_act(mut self) -> Self {
        self.layers.push(Layer::Tanh);
        self
    }

    pub fn softmax(mut self) -> Self {
        // default: softmax over the last axis, represented as axis=0 for 1-D
        self.layers.push(Layer::Softmax(0));
        self
    }

    pub fn softmax_axis(mut self, axis: usize) -> Self {
        self.layers.push(Layer::Softmax(axis));
        self
    }

    pub fn gelu(mut self) -> Self {
        self.layers.push(Layer::GELU);
        self
    }

    pub fn residual(mut self, layers: Vec<Layer>) -> Self {
        self.layers.push(Layer::Residual(ResidualBlock { layers }));
        self
    }

    pub fn attention(mut self, heads: usize, d_model: usize) -> Self {
        self.layers.push(Layer::Attention(MultiHeadAttention::new(heads, d_model)));
        self
    }

    pub fn layer(mut self, layer: Layer) -> Self {
        self.layers.push(layer);
        self
    }

    pub fn build(self) -> Model {
        Model { layers: self.layers, name: self.name }
    }
}

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

    #[test]
    fn test_dense_forward_shape() {
        let layer = DenseLayer::new(4, 3);
        let input = Tensor::ones(vec![2, 4]);
        let out = layer.forward(&input);
        assert_eq!(out.shape, vec![2, 3]);
    }

    #[test]
    fn test_dense_forward_1d() {
        let layer = DenseLayer::new(3, 2);
        let input = Tensor::ones(vec![3]);
        let out = layer.forward(&input);
        assert_eq!(out.shape, vec![2]);
    }

    #[test]
    fn test_sequential_build() {
        let model = Sequential::new("test")
            .dense(10, 5)
            .relu()
            .dense(5, 2)
            .softmax()
            .build();
        assert_eq!(model.layers.len(), 4);
        assert_eq!(model.name, "test");
    }

    #[test]
    fn test_model_forward_shape() {
        let model = Sequential::new("mlp")
            .dense(4, 8)
            .relu()
            .dense(8, 3)
            .build();
        let input = Tensor::ones(vec![2, 4]);
        let out = model.forward(&input);
        assert_eq!(out.shape, vec![2, 3]);
    }

    #[test]
    fn test_parameter_count() {
        let model = Sequential::new("mlp")
            .dense(10, 5) // 10*5 + 5 = 55
            .dense(5, 2)  // 5*2 + 2 = 12
            .build();
        assert_eq!(model.parameter_count(), 55 + 12);
    }

    #[test]
    fn test_residual_connection() {
        // residual block: dense(4,4) + relu, then added to input
        let block_layers = vec![
            Layer::Dense(DenseLayer {
                weights: Tensor::zeros(vec![4, 4]),
                bias: Tensor::zeros(vec![4]),
            }),
            Layer::ReLU,
        ];
        let model = Sequential::new("res")
            .residual(block_layers)
            .build();
        let input = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], vec![1, 4]);
        let out = model.forward(&input);
        // With zero weights, dense outputs zeros, relu(zeros)=zeros, residual = input + 0 = input
        assert_eq!(out.shape, vec![1, 4]);
        assert_eq!(out.data, vec![1.0, 2.0, 3.0, 4.0]);
    }

    #[test]
    fn test_attention_forward_shape() {
        let attn = MultiHeadAttention::new(2, 4);
        let input = Tensor::rand(vec![3, 4], 42); // seq_len=3, d_model=4
        let out = attn.forward(&input);
        assert_eq!(out.shape, vec![3, 4]);
    }

    #[test]
    fn test_model_summary() {
        let model = Sequential::new("demo")
            .dense(10, 5)
            .relu()
            .build();
        let summary = ModelSummary::print(&model);
        assert!(summary.contains("demo"));
        assert!(summary.contains("Dense"));
        assert!(summary.contains("ReLU"));
    }

    #[test]
    fn test_save_load_weights() {
        let model = Sequential::new("test")
            .dense(3, 2)
            .build();
        let path = std::env::temp_dir().join("proof_engine_test_weights.bin");
        let path_str = path.to_str().unwrap();
        model.save_weights(path_str).unwrap();

        let mut model2 = Sequential::new("test")
            .dense(3, 2)
            .build();
        model2.load_weights(path_str).unwrap();

        // weights should match
        if let (Layer::Dense(l1), Layer::Dense(l2)) = (&model.layers[0], &model2.layers[0]) {
            assert_eq!(l1.weights.data, l2.weights.data);
            assert_eq!(l1.bias.data, l2.bias.data);
        }
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn test_conv2d_layer_forward() {
        let layer = Conv2DLayer::new(1, 2, 3);
        let input = Tensor::ones(vec![1, 5, 5]);
        let out = layer.forward(&input);
        assert_eq!(out.shape, vec![2, 3, 3]);
    }
}