nove_model 0.1.2

An easy-to-use, lightweight deep learning library wrapped around Candle Tensor.
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
use std::{
    collections::HashMap,
    fmt::{Display, Write},
    sync::atomic::{AtomicUsize, Ordering},
};

use nove_tensor::{DType, Device, Tensor};

use crate::{Model, ModelError};

use super::{Conv2d, Conv2dBuilder, Linear, LinearBuilder, MaxPool2d, MaxPool2dBuilder, ReLU};

static ID: AtomicUsize = AtomicUsize::new(0);

/// Layer enum representing different types of CNN layers.
///
/// # Notes
/// * The `CNNLayer` enum wraps different layer types and provides a unified interface
///   through the `Model` trait implementation.
///
/// # Variants
/// * `Conv2d` - 2D convolution layer.
/// * `ReLU` - Rectified Linear Unit activation layer.
/// * `MaxPool2d` - 2D max pooling layer.
/// * `Linear` - Fully connected linear layer.
#[derive(Debug, Clone)]
pub enum CNNLayer {
    Conv2d(Conv2d),
    ReLU(ReLU),
    MaxPool2d(MaxPool2d),
    Linear(Linear),
}

impl Model for CNNLayer {
    type Input = Tensor;
    type Output = Tensor;

    /// Apply the layer to the input tensor.
    ///
    /// # Arguments
    /// * `input: Tensor` - The input tensor.
    ///
    /// # Returns
    /// * `Ok(Tensor)` - The output tensor if successful.
    /// * `Err(ModelError)` - The error when applying the layer to the input tensor.
    fn forward(&mut self, input: Self::Input) -> Result<Self::Output, ModelError> {
        match self {
            CNNLayer::Conv2d(layer) => layer.forward(input),
            CNNLayer::ReLU(layer) => layer.forward(input),
            CNNLayer::MaxPool2d(layer) => layer.forward(input),
            CNNLayer::Linear(layer) => layer.forward(input),
        }
    }

    fn require_grad(&mut self, grad_enabled: bool) -> Result<(), ModelError> {
        match self {
            CNNLayer::Conv2d(layer) => layer.require_grad(grad_enabled),
            CNNLayer::ReLU(layer) => layer.require_grad(grad_enabled),
            CNNLayer::MaxPool2d(layer) => layer.require_grad(grad_enabled),
            CNNLayer::Linear(layer) => layer.require_grad(grad_enabled),
        }
    }

    fn to_device(&mut self, device: &Device) -> Result<(), ModelError> {
        match self {
            CNNLayer::Conv2d(layer) => layer.to_device(device),
            CNNLayer::ReLU(layer) => layer.to_device(device),
            CNNLayer::MaxPool2d(layer) => layer.to_device(device),
            CNNLayer::Linear(layer) => layer.to_device(device),
        }
    }

    fn to_dtype(&mut self, dtype: &DType) -> Result<(), ModelError> {
        match self {
            CNNLayer::Conv2d(layer) => layer.to_dtype(dtype),
            CNNLayer::ReLU(layer) => layer.to_dtype(dtype),
            CNNLayer::MaxPool2d(layer) => layer.to_dtype(dtype),
            CNNLayer::Linear(layer) => layer.to_dtype(dtype),
        }
    }

    fn parameters(&self) -> Result<Vec<Tensor>, ModelError> {
        match self {
            CNNLayer::Conv2d(layer) => layer.parameters(),
            CNNLayer::ReLU(layer) => layer.parameters(),
            CNNLayer::MaxPool2d(layer) => layer.parameters(),
            CNNLayer::Linear(layer) => layer.parameters(),
        }
    }

    fn named_parameters(&self) -> Result<HashMap<String, Tensor>, ModelError> {
        match self {
            CNNLayer::Conv2d(layer) => layer.named_parameters(),
            CNNLayer::ReLU(layer) => layer.named_parameters(),
            CNNLayer::MaxPool2d(layer) => layer.named_parameters(),
            CNNLayer::Linear(layer) => layer.named_parameters(),
        }
    }
}

impl Display for CNNLayer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CNNLayer::Conv2d(layer) => write!(f, "{}", layer),
            CNNLayer::ReLU(layer) => write!(f, "{}", layer),
            CNNLayer::MaxPool2d(layer) => write!(f, "{}", layer),
            CNNLayer::Linear(layer) => write!(f, "{}", layer),
        }
    }
}

/// Convolutional Neural Network (CNN) model.
///
/// # Notes
/// * The `CNN` is a sequential model that chains multiple layers together.
/// * The `CNN` is now only created by the `CNNBuilder`.
///
/// # Fields
/// * `layers` - The sequence of layers in the network.
/// * `id` - The unique ID of the CNN model.
///
/// # Examples
/// ```
/// use nove::model::layer::{CNNBuilder, CNNConvBlock, CNNLinearBlock};
///
/// let mut cnn = CNNBuilder::default()
///     .conv_block(CNNConvBlock::new(1, 16).use_pool(true))
///     .conv_block(CNNConvBlock::new(16, 32).use_pool(true))
///     .linear_block(CNNLinearBlock::new(800, 10))
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct CNN {
    layers: Vec<CNNLayer>,
    id: usize,
}

impl CNN {
    /// Get the layers of the CNN model.
    ///
    /// # Returns
    /// * `&Vec<CNNLayer>` - The layers of the CNN model.
    pub fn layers(&self) -> &Vec<CNNLayer> {
        &self.layers
    }

    /// Get a mutable reference to the layers of the CNN model.
    ///
    /// # Returns
    /// * `&mut Vec<CNNLayer>` - A mutable reference to the layers of the CNN model.
    pub fn layers_mut(&mut self) -> &mut Vec<CNNLayer> {
        &mut self.layers
    }
}

impl Model for CNN {
    type Input = (Tensor, bool);
    type Output = Tensor;

    /// Apply the CNN model to the input tensor.
    ///
    /// # Arguments
    /// * `input` - A tuple containing the input tensor and a boolean flag.
    ///   - `Tensor` - The input tensor.
    ///   - `bool` - Whether the model is in training mode (ignored by CNN).
    ///
    /// # Returns
    /// * `Ok(Tensor)` - The output tensor if successful.
    /// * `Err(ModelError)` - The error when applying the CNN model to the input tensor.
    ///
    /// # Notes
    /// * The `bool` flag indicating training mode is ignored by CNN.
    /// * CNN produces identical outputs in both training and non-training modes,
    ///   as it does not contain layers with different behaviors (e.g., Dropout, BatchNorm).
    fn forward(&mut self, input: Self::Input) -> Result<Self::Output, ModelError> {
        let (mut input, _training) = input;
        for layer in &mut self.layers {
            // Flatten the input tensor before Linear layer
            if let CNNLayer::Linear(_) = layer {
                let shape = input.shape()?;
                if shape.dims().len() == 4 {
                    let batch_size = shape.dims()[0];
                    let flattened_size = shape.dims()[1] * shape.dims()[2] * shape.dims()[3];
                    input =
                        input.reshape(&nove_tensor::Shape::from(&[batch_size, flattened_size]))?;
                }
            }
            input = layer.forward(input)?;
        }
        Ok(input)
    }

    fn require_grad(&mut self, grad_enabled: bool) -> Result<(), ModelError> {
        for layer in &mut self.layers {
            layer.require_grad(grad_enabled)?;
        }
        Ok(())
    }

    fn to_device(&mut self, device: &Device) -> Result<(), ModelError> {
        for layer in &mut self.layers {
            layer.to_device(device)?;
        }
        Ok(())
    }

    fn to_dtype(&mut self, dtype: &DType) -> Result<(), ModelError> {
        for layer in &mut self.layers {
            layer.to_dtype(dtype)?;
        }
        Ok(())
    }

    fn parameters(&self) -> Result<Vec<Tensor>, ModelError> {
        let mut params = Vec::new();
        for layer in &self.layers {
            params.extend(layer.parameters()?);
        }
        Ok(params)
    }

    fn named_parameters(&self) -> Result<HashMap<String, Tensor>, ModelError> {
        let mut params = HashMap::new();
        for (i, layer) in self.layers.iter().enumerate() {
            let prefix = format!("cnn{}.", i);
            for (name, tensor) in layer.named_parameters()? {
                params.insert(format!("{}{}", prefix, name), tensor);
            }
        }
        Ok(params)
    }
}

impl Display for CNN {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = String::new();
        writeln!(s, "cnn.{}(", self.id)?;
        for layer in &self.layers {
            writeln!(s, "  {},", layer)?;
        }
        write!(s, ")")?;
        write!(f, "{}", s)
    }
}

/// Convolutional block configuration for building CNN models.
///
/// # Notes
/// * The `CNNConvBlock` is used to configure a convolutional layer with optional
///   ReLU activation and max pooling.
/// * The default configuration uses a 3x3 kernel with stride 1 and padding 1,
///   with ReLU activation enabled and pooling disabled.
///
/// # Fields
/// * `in_channels` - The number of input channels.
/// * `out_channels` - The number of output channels.
/// * `kernel_size` - The size of the convolution kernel (height, width).
/// * `stride` - The stride of the convolution.
/// * `padding` - The padding size.
/// * `use_relu` - Whether to use ReLU activation after convolution.
/// * `use_pool` - Whether to use max pooling after ReLU.
/// * `pool_kernel_size` - The size of the pooling kernel (height, width).
/// * `pool_stride` - The stride of the pooling operation (height, width).
///
/// # Examples
/// ```
/// use nove::model::layer::CNNConvBlock;
///
/// let block = CNNConvBlock::new(1, 16)
///     .kernel_size((3, 3))
///     .stride(1)
///     .padding(1)
///     .use_relu(true)
///     .use_pool(true)
///     .pool_kernel_size((2, 2))
///     .pool_stride((2, 2));
/// ```
#[derive(Debug, Clone)]
pub struct CNNConvBlock {
    in_channels: usize,
    out_channels: usize,
    kernel_size: (usize, usize),
    stride: usize,
    padding: usize,
    use_relu: bool,
    use_pool: bool,
    pool_kernel_size: (usize, usize),
    pool_stride: (usize, usize),
}

impl CNNConvBlock {
    /// Create a new convolutional block with default settings.
    ///
    /// # Arguments
    /// * `in_channels` - The number of input channels.
    /// * `out_channels` - The number of output channels.
    ///
    /// # Returns
    /// * `CNNConvBlock` - The new convolutional block.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16);
    /// ```
    pub fn new(in_channels: usize, out_channels: usize) -> Self {
        Self {
            in_channels,
            out_channels,
            kernel_size: (3, 3),
            stride: 1,
            padding: 1,
            use_relu: true,
            use_pool: false,
            pool_kernel_size: (2, 2),
            pool_stride: (2, 2),
        }
    }

    /// Configure the size of the convolution kernel.
    ///
    /// # Arguments
    /// * `kernel_size` - The size of the convolution kernel (height, width).
    ///
    /// # Returns
    /// * `Self` - The convolutional block with the configured kernel size.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16).kernel_size((3, 3));
    /// ```
    pub fn kernel_size(mut self, kernel_size: (usize, usize)) -> Self {
        self.kernel_size = kernel_size;
        self
    }

    /// Configure the stride of the convolution.
    ///
    /// # Arguments
    /// * `stride` - The stride of the convolution.
    ///
    /// # Returns
    /// * `Self` - The convolutional block with the configured stride.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16).stride(1);
    /// ```
    pub fn stride(mut self, stride: usize) -> Self {
        self.stride = stride;
        self
    }

    /// Configure the padding size.
    ///
    /// # Arguments
    /// * `padding` - The padding size.
    ///
    /// # Returns
    /// * `Self` - The convolutional block with the configured padding.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16).padding(1);
    /// ```
    pub fn padding(mut self, padding: usize) -> Self {
        self.padding = padding;
        self
    }

    /// Configure whether to use ReLU activation after convolution.
    ///
    /// # Arguments
    /// * `use_relu` - Whether to use ReLU activation.
    ///
    /// # Returns
    /// * `Self` - The convolutional block with the configured ReLU activation.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16).use_relu(true);
    /// ```
    pub fn use_relu(mut self, use_relu: bool) -> Self {
        self.use_relu = use_relu;
        self
    }

    /// Configure whether to use max pooling after ReLU.
    ///
    /// # Arguments
    /// * `use_pool` - Whether to use max pooling.
    ///
    /// # Returns
    /// * `Self` - The convolutional block with the configured pooling.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16).use_pool(true);
    /// ```
    pub fn use_pool(mut self, use_pool: bool) -> Self {
        self.use_pool = use_pool;
        self
    }

    /// Configure the size of the pooling kernel.
    ///
    /// # Arguments
    /// * `pool_kernel_size` - The size of the pooling kernel (height, width).
    ///
    /// # Returns
    /// * `Self` - The convolutional block with the configured pooling kernel size.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16).pool_kernel_size((2, 2));
    /// ```
    pub fn pool_kernel_size(mut self, pool_kernel_size: (usize, usize)) -> Self {
        self.pool_kernel_size = pool_kernel_size;
        self
    }

    /// Configure the stride of the pooling operation.
    ///
    /// # Arguments
    /// * `pool_stride` - The stride of the pooling operation (height, width).
    ///
    /// # Returns
    /// * `Self` - The convolutional block with the configured pooling stride.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNConvBlock;
    /// let block = CNNConvBlock::new(1, 16).pool_stride((2, 2));
    /// ```
    pub fn pool_stride(mut self, pool_stride: (usize, usize)) -> Self {
        self.pool_stride = pool_stride;
        self
    }
}

/// Linear (fully connected) block configuration for building CNN models.
///
/// # Notes
/// * The `CNNLinearBlock` is used to configure a linear layer with optional
///   ReLU activation.
/// * The default configuration has bias enabled and ReLU activation disabled.
///
/// # Fields
/// * `in_features` - The number of input features.
/// * `out_features` - The number of output features.
/// * `use_relu` - Whether to use ReLU activation after the linear layer.
/// * `bias_enabled` - Whether to enable the bias term.
///
/// # Examples
/// ```
/// use nove::model::layer::CNNLinearBlock;
///
/// let block = CNNLinearBlock::new(800, 10)
///     .use_relu(false)
///     .bias_enabled(true);
/// ```
#[derive(Debug, Clone)]
pub struct CNNLinearBlock {
    in_features: usize,
    out_features: usize,
    use_relu: bool,
    bias_enabled: bool,
}

impl CNNLinearBlock {
    /// Create a new linear block with default settings.
    ///
    /// # Arguments
    /// * `in_features` - The number of input features.
    /// * `out_features` - The number of output features.
    ///
    /// # Returns
    /// * `CNNLinearBlock` - The new linear block.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNLinearBlock;
    /// let block = CNNLinearBlock::new(800, 10);
    /// ```
    pub fn new(in_features: usize, out_features: usize) -> Self {
        Self {
            in_features,
            out_features,
            use_relu: false,
            bias_enabled: true,
        }
    }

    /// Configure whether to use ReLU activation after the linear layer.
    ///
    /// # Arguments
    /// * `use_relu` - Whether to use ReLU activation.
    ///
    /// # Returns
    /// * `Self` - The linear block with the configured ReLU activation.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNLinearBlock;
    /// let block = CNNLinearBlock::new(800, 10).use_relu(true);
    /// ```
    pub fn use_relu(mut self, use_relu: bool) -> Self {
        self.use_relu = use_relu;
        self
    }

    /// Configure whether to enable the bias term.
    ///
    /// # Arguments
    /// * `bias_enabled` - Whether to enable the bias term.
    ///
    /// # Returns
    /// * `Self` - The linear block with the configured bias term.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNLinearBlock;
    /// let block = CNNLinearBlock::new(800, 10).bias_enabled(true);
    /// ```
    pub fn bias_enabled(mut self, bias_enabled: bool) -> Self {
        self.bias_enabled = bias_enabled;
        self
    }
}

/// The builder for the CNN model.
///
/// # Notes
/// * The `CNNBuilder` implements the `Default` trait, so you can
///   use `CNNBuilder::default()` to create a builder with default values.
///
/// # Required Arguments
/// * At least one `CNNConvBlock` or `CNNLinearBlock` must be added.
///
/// # Optional Arguments
/// * `device` - The device to use for the model. Default is `Device::cpu()`.
/// * `dtype` - The data type to use for the model. Default is `DType::F32`.
/// * `grad_enabled` - Whether to enable the gradient computation. Default is `true`.
///
/// # Fields
/// * `conv_blocks` - The convolutional blocks to add to the model.
/// * `linear_blocks` - The linear blocks to add to the model.
/// * `device` - The device to use for the model.
/// * `dtype` - The data type to use for the model.
/// * `grad_enabled` - Whether to enable the gradient computation.
///
/// # Examples
/// ```
/// use nove::model::layer::{CNNBuilder, CNNConvBlock, CNNLinearBlock};
/// use nove::tensor::{Device, DType};
///
/// let cnn = CNNBuilder::default()
///     .conv_block(CNNConvBlock::new(1, 16).use_pool(true))
///     .conv_block(CNNConvBlock::new(16, 32).use_pool(true))
///     .linear_block(CNNLinearBlock::new(800, 10))
///     .device(Device::cpu())
///     .dtype(DType::F32)
///     .grad_enabled(true)
///     .build();
/// ```
pub struct CNNBuilder {
    conv_blocks: Vec<CNNConvBlock>,
    linear_blocks: Vec<CNNLinearBlock>,
    device: Device,
    dtype: DType,
    grad_enabled: bool,
}

impl Default for CNNBuilder {
    fn default() -> Self {
        Self {
            conv_blocks: Vec::new(),
            linear_blocks: Vec::new(),
            device: Device::cpu(),
            dtype: DType::F32,
            grad_enabled: true,
        }
    }
}

impl CNNBuilder {
    /// Add a convolutional block to the CNN model.
    ///
    /// # Arguments
    /// * `block` - The convolutional block to add.
    ///
    /// # Returns
    /// * `&mut Self` - The builder with the added convolutional block.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::{CNNBuilder, CNNConvBlock};
    /// let mut builder = CNNBuilder::default();
    /// builder.conv_block(CNNConvBlock::new(1, 16).use_pool(true));
    /// ```
    pub fn conv_block(&mut self, block: CNNConvBlock) -> &mut Self {
        self.conv_blocks.push(block);
        self
    }

    /// Add a linear block to the CNN model.
    ///
    /// # Arguments
    /// * `block` - The linear block to add.
    ///
    /// # Returns
    /// * `&mut Self` - The builder with the added linear block.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::{CNNBuilder, CNNLinearBlock};
    /// let mut builder = CNNBuilder::default();
    /// builder.linear_block(CNNLinearBlock::new(800, 10));
    /// ```
    pub fn linear_block(&mut self, block: CNNLinearBlock) -> &mut Self {
        self.linear_blocks.push(block);
        self
    }

    /// Configure the device to use for the model.
    ///
    /// # Arguments
    /// * `device` - The device to use for the model.
    ///
    /// # Returns
    /// * `&mut Self` - The builder with the configured device.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNBuilder;
    /// use nove::tensor::Device;
    /// let mut builder = CNNBuilder::default();
    /// builder.device(Device::cpu());
    /// ```
    pub fn device(&mut self, device: Device) -> &mut Self {
        self.device = device;
        self
    }

    /// Configure the data type to use for the model.
    ///
    /// # Arguments
    /// * `dtype` - The data type to use for the model.
    ///
    /// # Returns
    /// * `&mut Self` - The builder with the configured data type.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNBuilder;
    /// use nove::tensor::DType;
    /// let mut builder = CNNBuilder::default();
    /// builder.dtype(DType::F32);
    /// ```
    pub fn dtype(&mut self, dtype: DType) -> &mut Self {
        self.dtype = dtype;
        self
    }

    /// Configure whether to enable the gradient computation.
    ///
    /// # Arguments
    /// * `grad_enabled` - Whether to enable the gradient computation.
    ///
    /// # Returns
    /// * `&mut Self` - The builder with the configured gradient computation.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::CNNBuilder;
    /// let mut builder = CNNBuilder::default();
    /// builder.grad_enabled(true);
    /// ```
    pub fn grad_enabled(&mut self, grad_enabled: bool) -> &mut Self {
        self.grad_enabled = grad_enabled;
        self
    }

    /// Build the CNN model.
    ///
    /// # Returns
    /// * `Ok(CNN)` - The built CNN model.
    /// * `Err(ModelError)` - The error when building the CNN model.
    ///
    /// # Examples
    /// ```
    /// use nove::model::layer::{CNNBuilder, CNNConvBlock, CNNLinearBlock};
    /// let mut builder = CNNBuilder::default();
    /// builder.conv_block(CNNConvBlock::new(1, 16).use_pool(true));
    /// builder.linear_block(CNNLinearBlock::new(800, 10));
    /// let cnn = builder.build().unwrap();
    /// ```
    pub fn build(&self) -> Result<CNN, ModelError> {
        let mut layers = Vec::new();

        for block in &self.conv_blocks {
            let conv = Conv2dBuilder::default()
                .in_channels(block.in_channels)
                .out_channels(block.out_channels)
                .kernel_size(block.kernel_size)
                .stride(block.stride)
                .padding(block.padding)
                .device(self.device.clone())
                .dtype(self.dtype.clone())
                .grad_enabled(self.grad_enabled)
                .build()?;
            layers.push(CNNLayer::Conv2d(conv));

            if block.use_relu {
                layers.push(CNNLayer::ReLU(ReLU::new()));
            }

            if block.use_pool {
                let pool = MaxPool2dBuilder::default()
                    .kernel_size(block.pool_kernel_size)
                    .stride(block.pool_stride)
                    .build()?;
                layers.push(CNNLayer::MaxPool2d(pool));
            }
        }

        for block in &self.linear_blocks {
            let linear = LinearBuilder::default()
                .in_features(block.in_features)
                .out_features(block.out_features)
                .bias_enabled(block.bias_enabled)
                .device(self.device.clone())
                .dtype(self.dtype.clone())
                .grad_enabled(self.grad_enabled)
                .build()?;
            layers.push(CNNLayer::Linear(linear));

            if block.use_relu {
                layers.push(CNNLayer::ReLU(ReLU::new()));
            }
        }

        let id = ID.fetch_add(1, Ordering::Relaxed);

        Ok(CNN { layers, id })
    }
}