rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//! CNN (畳み込みニューラルネットワーク) モデル実装
//! CNN (Convolutional Neural Network) model implementations

use crate::autograd::Variable;
use crate::models::{Model, ModelBuilder, ModelMode};
use crate::nn::{
    BatchNorm2d, Conv2d, Dropout, Linear, MaxPool2d, Module, ReLU, Sequential, Softmax,
};
use num_traits::Float;
use std::any::Any;
use std::collections::HashMap;
use std::fmt::Debug;

/// 基本的な CNN モデル
/// Basic CNN model
#[derive(Debug)]
pub struct CNN<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    features: Sequential<T>,
    classifier: Sequential<T>,
    mode: ModelMode,
    num_classes: usize,
}

impl<T> CNN<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    /// 新しい CNN モデルを作成
    /// Create a new CNN model
    pub fn new(
        input_channels: usize,
        num_classes: usize,
        hidden_channels: Vec<usize>,
        dropout_rate: f64,
    ) -> Self {
        let mut features = Sequential::new();
        let mut in_channels = input_channels;

        // 畳み込み層の構築
        for (i, &out_channels) in hidden_channels.iter().enumerate() {
            // Conv2d -> BatchNorm -> ReLU -> MaxPool
            features.add_module(Conv2d::new(
                in_channels,
                out_channels,
                (3, 3),
                Some((1, 1)),
                Some((1, 1)),
                Some(true),
            ));
            features.add_module(BatchNorm2d::new(out_channels, None, None, None));
            features.add_module(ReLU::new());

            if i % 2 == 1 {
                // 2層ごとにプーリング
                features.add_module(MaxPool2d::new((2, 2), Some((2, 2)), Some((0, 0))));
            }

            in_channels = out_channels;
        }

        // 分類器の構築
        let mut classifier = Sequential::new();
        let final_feature_size = hidden_channels.last().unwrap_or(&64) * 7 * 7; // 仮定: 224x224 -> 7x7

        classifier.add_module(Dropout::new(
            <T as From<f32>>::from(dropout_rate as f32),
            false,
        ));
        classifier.add_module(Linear::new(final_feature_size, 512));
        classifier.add_module(ReLU::new());
        classifier.add_module(Dropout::new(
            <T as From<f32>>::from(dropout_rate as f32),
            false,
        ));
        classifier.add_module(Linear::new(512, num_classes));
        classifier.add_module(Softmax::new(1));

        CNN {
            features,
            classifier,
            mode: ModelMode::Train,
            num_classes,
        }
    }
}

impl<T> Module<T> for CNN<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        let features = self.features.forward(input);

        // Global Average Pooling または Flatten
        let flattened = self.flatten_features(&features);

        self.classifier.forward(&flattened)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        let mut params = self.features.parameters();
        params.extend(self.classifier.parameters());
        params
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl<T> Model<T> for CNN<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    fn train(&mut self) {
        self.mode = ModelMode::Train;
    }

    fn eval(&mut self) {
        self.mode = ModelMode::Eval;
    }

    fn mode(&self) -> ModelMode {
        self.mode
    }

    fn config(&self) -> HashMap<String, String> {
        let mut config = HashMap::new();
        config.insert("model_type".to_string(), "CNN".to_string());
        config.insert("num_classes".to_string(), self.num_classes.to_string());
        config.insert(
            "feature_layers".to_string(),
            self.features.len().to_string(),
        );
        config.insert(
            "classifier_layers".to_string(),
            self.classifier.len().to_string(),
        );
        config
    }

    fn summary(&self) -> String {
        format!(
            "CNN Model:\n  - Feature layers: {}\n  - Classifier layers: {}\n  - Classes: {}\n  - Mode: {:?}",
            self.features.len(),
            self.classifier.len(),
            self.num_classes,
            self.mode
        )
    }
}

impl<T> CNN<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    /// 特徴量をフラット化
    /// Flatten features
    fn flatten_features(&self, features: &Variable<T>) -> Variable<T> {
        // 実装は簡略化 - 実際にはテンソルの形状変換が必要
        features.clone()
    }
}

/// CNN モデルビルダー
/// CNN model builder
#[derive(Debug, Default)]
pub struct CNNBuilder<T>
where
    T: Float + 'static + Send + Sync + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    input_channels: Option<usize>,
    num_classes: Option<usize>,
    hidden_channels: Vec<usize>,
    dropout_rate: f64,
    _phantom: std::marker::PhantomData<T>,
}

impl<T> CNNBuilder<T>
where
    T: Float + 'static + Send + Sync + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    /// 新しいビルダーを作成
    /// Create a new builder
    pub fn new() -> Self {
        CNNBuilder {
            input_channels: None,
            num_classes: None,
            hidden_channels: vec![64, 128, 256, 512],
            dropout_rate: 0.5,
            _phantom: std::marker::PhantomData,
        }
    }

    /// 入力チャンネル数を設定
    /// Set input channels
    pub fn input_channels(mut self, channels: usize) -> Self {
        self.input_channels = Some(channels);
        self
    }

    /// クラス数を設定
    /// Set number of classes
    pub fn num_classes(mut self, classes: usize) -> Self {
        self.num_classes = Some(classes);
        self
    }

    /// 隠れ層のチャンネル数を設定
    /// Set hidden layer channels
    pub fn hidden_channels(mut self, channels: Vec<usize>) -> Self {
        self.hidden_channels = channels;
        self
    }

    /// ドロップアウト率を設定
    /// Set dropout rate
    pub fn dropout_rate(mut self, rate: f64) -> Self {
        self.dropout_rate = rate;
        self
    }
}

impl<T> ModelBuilder<T> for CNNBuilder<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    type Model = CNN<T>;

    fn build(self) -> Self::Model {
        let input_channels = self
            .input_channels
            .expect("Input channels must be specified");
        let num_classes = self
            .num_classes
            .expect("Number of classes must be specified");

        CNN::new(
            input_channels,
            num_classes,
            self.hidden_channels,
            self.dropout_rate,
        )
    }
}

/// ResNet ブロック
/// ResNet block
#[derive(Debug)]
pub struct ResNetBlock<T>
where
    T: Float + 'static + Send + Sync + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    conv1: Conv2d<T>,
    bn1: BatchNorm2d<T>,
    conv2: Conv2d<T>,
    bn2: BatchNorm2d<T>,
    relu: ReLU<T>,
    downsample: Option<Sequential<T>>,
}

impl<T> ResNetBlock<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    /// 新しい ResNet ブロックを作成
    /// Create a new ResNet block
    pub fn new(
        in_channels: usize,
        out_channels: usize,
        stride: usize,
        downsample: Option<Sequential<T>>,
    ) -> Self {
        ResNetBlock {
            conv1: Conv2d::new(
                in_channels,
                out_channels,
                (3, 3),
                Some((stride, stride)),
                Some((1, 1)),
                Some(true),
            ),
            bn1: BatchNorm2d::new(out_channels, None, None, None),
            conv2: Conv2d::new(
                out_channels,
                out_channels,
                (3, 3),
                Some((1, 1)),
                Some((1, 1)),
                Some(true),
            ),
            bn2: BatchNorm2d::new(out_channels, None, None, None),
            relu: ReLU::new(),
            downsample,
        }
    }
}

impl<T> Module<T> for ResNetBlock<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        let identity = input.clone();

        let mut out = self.conv1.forward(input);
        out = self.bn1.forward(&out);
        out = self.relu.forward(&out);

        out = self.conv2.forward(&out);
        out = self.bn2.forward(&out);

        // ダウンサンプリングが必要な場合
        let _identity = if let Some(ref downsample) = self.downsample {
            downsample.forward(&identity)
        } else {
            identity
        };

        // 残差接続 (簡略化実装)
        // out = out + identity;
        out = self.relu.forward(&out);

        out
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        let mut params = self.conv1.parameters();
        params.extend(self.bn1.parameters());
        params.extend(self.conv2.parameters());
        params.extend(self.bn2.parameters());

        if let Some(ref downsample) = self.downsample {
            params.extend(downsample.parameters());
        }

        params
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// ResNet モデル
/// ResNet model
#[derive(Debug)]
pub struct ResNet<T>
where
    T: Float + 'static + Send + Sync + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    conv1: Conv2d<T>,
    bn1: BatchNorm2d<T>,
    relu: ReLU<T>,
    maxpool: MaxPool2d,
    layers: Vec<Sequential<T>>,
    avgpool: Sequential<T>, // Global Average Pooling の代替
    fc: Linear<T>,
    mode: ModelMode,
    num_classes: usize,
}

impl<T> ResNet<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    /// 新しい ResNet モデルを作成
    /// Create a new ResNet model
    pub fn new(
        layers: Vec<usize>, // 各レイヤーのブロック数
        num_classes: usize,
    ) -> Self {
        let conv1 = Conv2d::new(3, 64, (7, 7), Some((2, 2)), Some((3, 3)), Some(true));
        let bn1 = BatchNorm2d::new(64, None, None, None);
        let relu = ReLU::new();
        let maxpool = MaxPool2d::new((3, 3), Some((2, 2)), Some((0, 0)));

        // ResNet レイヤーの構築
        let mut resnet_layers = Vec::new();
        let channels = [64, 128, 256, 512];
        let mut in_channels = 64;

        for (i, &num_blocks) in layers.iter().enumerate() {
            let out_channels = channels[i];
            let stride = if i == 0 { 1 } else { 2 };

            let mut layer = Sequential::new();

            // 最初のブロック(ストライドあり)
            let downsample = if stride != 1 || in_channels != out_channels {
                let mut ds = Sequential::new();
                ds.add_module(Conv2d::new(
                    in_channels,
                    out_channels,
                    (1, 1),
                    Some((stride, stride)),
                    Some((0, 0)),
                    Some(true),
                ));
                ds.add_module(BatchNorm2d::new(out_channels, None, None, None));
                Some(ds)
            } else {
                None
            };

            layer.add_module(ResNetBlock::new(
                in_channels,
                out_channels,
                stride,
                downsample,
            ));

            // 残りのブロック
            for _ in 1..num_blocks {
                layer.add_module(ResNetBlock::new(out_channels, out_channels, 1, None));
            }

            resnet_layers.push(layer);
            in_channels = out_channels;
        }

        let avgpool = Sequential::new(); // Global Average Pooling の実装が必要
        let fc = Linear::new(512, num_classes);

        ResNet {
            conv1,
            bn1,
            relu,
            maxpool,
            layers: resnet_layers,
            avgpool,
            fc,
            mode: ModelMode::Train,
            num_classes,
        }
    }

    /// ResNet-18 を作成
    /// Create ResNet-18
    pub fn resnet18(num_classes: usize) -> Self {
        Self::new(vec![2, 2, 2, 2], num_classes)
    }

    /// ResNet-34 を作成
    /// Create ResNet-34
    pub fn resnet34(num_classes: usize) -> Self {
        Self::new(vec![3, 4, 6, 3], num_classes)
    }

    /// ResNet-50 を作成
    /// Create ResNet-50
    pub fn resnet50(num_classes: usize) -> Self {
        Self::new(vec![3, 4, 6, 3], num_classes)
    }
}

impl<T> Module<T> for ResNet<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        let mut x = self.conv1.forward(input);
        x = self.bn1.forward(&x);
        x = self.relu.forward(&x);
        x = self.maxpool.forward(&x);

        // ResNet レイヤーを順次適用
        for layer in &self.layers {
            x = layer.forward(&x);
        }

        x = self.avgpool.forward(&x);
        x = self.fc.forward(&x);

        x
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        let mut params = self.conv1.parameters();
        params.extend(self.bn1.parameters());

        for layer in &self.layers {
            params.extend(layer.parameters());
        }

        params.extend(self.avgpool.parameters());
        params.extend(self.fc.parameters());

        params
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl<T> Model<T> for ResNet<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    fn train(&mut self) {
        self.mode = ModelMode::Train;
    }

    fn eval(&mut self) {
        self.mode = ModelMode::Eval;
    }

    fn mode(&self) -> ModelMode {
        self.mode
    }

    fn config(&self) -> HashMap<String, String> {
        let mut config = HashMap::new();
        config.insert("model_type".to_string(), "ResNet".to_string());
        config.insert("num_classes".to_string(), self.num_classes.to_string());
        config.insert("num_layers".to_string(), self.layers.len().to_string());
        config
    }

    fn summary(&self) -> String {
        format!(
            "ResNet Model:\n  - Layers: {}\n  - Classes: {}\n  - Mode: {:?}",
            self.layers.len(),
            self.num_classes,
            self.mode
        )
    }
}

/// ResNet ビルダー
/// ResNet builder
#[derive(Debug, Default)]
pub struct ResNetBuilder<T>
where
    T: Float + 'static + Send + Sync + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    layers: Vec<usize>,
    num_classes: Option<usize>,
    _phantom: std::marker::PhantomData<T>,
}

impl<T> ResNetBuilder<T>
where
    T: Float + 'static + Send + Sync + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    /// 新しいビルダーを作成
    /// Create a new builder
    pub fn new() -> Self {
        ResNetBuilder {
            layers: vec![2, 2, 2, 2], // ResNet-18 デフォルト
            num_classes: None,
            _phantom: std::marker::PhantomData,
        }
    }

    /// レイヤー構成を設定
    /// Set layer configuration
    pub fn layers(mut self, layers: Vec<usize>) -> Self {
        self.layers = layers;
        self
    }

    /// クラス数を設定
    /// Set number of classes
    pub fn num_classes(mut self, classes: usize) -> Self {
        self.num_classes = Some(classes);
        self
    }

    /// ResNet-18 設定
    /// ResNet-18 configuration
    pub fn resnet18(mut self) -> Self {
        self.layers = vec![2, 2, 2, 2];
        self
    }

    /// ResNet-34 設定
    /// ResNet-34 configuration
    pub fn resnet34(mut self) -> Self {
        self.layers = vec![3, 4, 6, 3];
        self
    }

    /// ResNet-50 設定
    /// ResNet-50 configuration
    pub fn resnet50(mut self) -> Self {
        self.layers = vec![3, 4, 6, 3];
        self
    }
}

impl<T> ModelBuilder<T> for ResNetBuilder<T>
where
    T: Float
        + 'static
        + Send
        + Sync
        + Debug
        + Default
        + Copy
        + From<f32>
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive
        + num_traits::ToPrimitive
        + num_traits::Zero
        + num_traits::One
        + std::iter::Sum
        + std::fmt::Display,
{
    type Model = ResNet<T>;

    fn build(self) -> Self::Model {
        let num_classes = self
            .num_classes
            .expect("Number of classes must be specified");
        ResNet::new(self.layers, num_classes)
    }
}