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
//! Implementation of 2D Pooling layers
//! 2次元プーリングレイヤーの実装

use crate::autograd::Variable;
use crate::nn::Module;
use crate::tensor::Tensor;
use ndarray::Array4;
use num_traits::Float;
use std::fmt::Debug;

/// 2D Max Pooling layer
/// 2次元最大プーリングレイヤー
///
/// Applies a 2D max pooling over an input signal composed of several input planes.
/// 複数の入力プレーンからなる入力信号に対して2次元最大プーリングを適用します。
#[derive(Debug)]
pub struct MaxPool2d {
    /// Kernel size (height, width)
    /// カーネルサイズ (高さ, 幅)
    kernel_size: (usize, usize),

    /// Stride (height, width)
    /// ストライド (高さ, 幅)
    stride: Option<(usize, usize)>,

    /// Padding (height, width)
    /// パディング (高さ, 幅)
    padding: (usize, usize),
}

impl MaxPool2d {
    /// Creates a new MaxPool2d layer
    /// 新しいMaxPool2dレイヤーを作成します
    pub fn new(
        kernel_size: (usize, usize),
        stride: Option<(usize, usize)>,
        padding: Option<(usize, usize)>,
    ) -> Self {
        let stride = stride.unwrap_or(kernel_size); // Default stride = kernel_size
        let padding = padding.unwrap_or((0, 0));

        MaxPool2d {
            kernel_size,
            stride: Some(stride),
            padding,
        }
    }

    /// Computes the output size given input size
    /// 入力サイズから出力サイズを計算します
    pub fn compute_output_size(&self, input_height: usize, input_width: usize) -> (usize, usize) {
        let stride = self.stride.unwrap_or(self.kernel_size);
        let out_height = (input_height + 2 * self.padding.0 - self.kernel_size.0) / stride.0 + 1;
        let out_width = (input_width + 2 * self.padding.1 - self.kernel_size.1) / stride.1 + 1;
        (out_height, out_width)
    }

    /// Applies padding to input tensor
    /// 入力テンソルにパディングを適用します
    fn apply_padding<T: Float + Copy>(&self, input: &Array4<T>) -> Array4<T> {
        if self.padding == (0, 0) {
            return input.clone();
        }

        let (batch_size, channels, height, width) = input.dim();
        let new_height = height + 2 * self.padding.0;
        let new_width = width + 2 * self.padding.1;

        let mut padded = Array4::from_elem(
            (batch_size, channels, new_height, new_width),
            T::neg_infinity(),
        );

        // Copy original data to center of padded tensor
        let mut padded_slice = padded.slice_mut(ndarray::s![
            ..,
            ..,
            self.padding.0..self.padding.0 + height,
            self.padding.1..self.padding.1 + width
        ]);
        padded_slice.assign(input);

        padded
    }

    /// Performs max pooling operation
    /// 最大プーリング演算を実行します
    fn maxpool2d_forward<T: Float + Copy>(&self, input: &Array4<T>) -> Array4<T> {
        let (batch_size, channels, input_height, input_width) = input.dim();
        let stride = self.stride.unwrap_or(self.kernel_size);

        // Apply padding
        let padded_input = self.apply_padding(input);

        // Compute output dimensions
        let (out_height, out_width) = self.compute_output_size(input_height, input_width);

        let mut output = Array4::from_elem(
            (batch_size, channels, out_height, out_width),
            T::neg_infinity(),
        );

        // Perform max pooling
        for b in 0..batch_size {
            for c in 0..channels {
                for oh in 0..out_height {
                    for ow in 0..out_width {
                        let mut max_val = T::neg_infinity();

                        for kh in 0..self.kernel_size.0 {
                            for kw in 0..self.kernel_size.1 {
                                let ih = oh * stride.0 + kh;
                                let iw = ow * stride.1 + kw;

                                max_val = max_val.max(padded_input[[b, c, ih, iw]]);
                            }
                        }

                        output[[b, c, oh, ow]] = max_val;
                    }
                }
            }
        }

        output
    }

    /// Forward pass of the MaxPool2d layer
    /// MaxPool2dレイヤーの順伝播
    pub fn forward<T>(&self, input: &Variable<T>) -> Variable<T>
    where
        T: Float
            + Copy
            + Send
            + Sync
            + 'static
            + ndarray::ScalarOperand
            + num_traits::FromPrimitive,
    {
        let input_binding = input.data();
        let input_data = input_binding.read().unwrap();

        // Ensure input is 4D: (batch_size, channels, height, width)
        let input_shape = input_data.shape();
        if input_shape.len() != 4 {
            panic!(
                "MaxPool2d expects 4D input (batch_size, channels, height, width), got shape {:?}",
                input_shape
            );
        }

        // Reshape tensor to Array4 for pooling
        let input_array = input_data
            .as_array()
            .view()
            .into_dimensionality::<ndarray::Ix4>()
            .unwrap();

        // Perform max pooling
        let output_array = self.maxpool2d_forward(&input_array.to_owned());
        let output_tensor = Tensor::new(output_array.into_dyn());

        // MaxPool doesn't have learnable parameters, so gradient requirement is inherited
        Variable::new(output_tensor, input.requires_grad())
    }

    /// Returns kernel size
    /// カーネルサイズを返します
    pub fn kernel_size(&self) -> (usize, usize) {
        self.kernel_size
    }

    /// Returns stride
    /// ストライドを返します
    pub fn stride(&self) -> (usize, usize) {
        self.stride.unwrap_or(self.kernel_size)
    }

    /// Returns padding
    /// パディングを返します
    pub fn padding(&self) -> (usize, usize) {
        self.padding
    }
}

impl<T> Module<T> for MaxPool2d
where
    T: Float + Copy + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        self.forward(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        vec![] // MaxPool has no parameters
    }

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

/// 2D Average Pooling layer
/// 2次元平均プーリングレイヤー
#[derive(Debug)]
pub struct AvgPool2d {
    /// Kernel size (height, width)
    /// カーネルサイズ (高さ, 幅)
    kernel_size: (usize, usize),

    /// Stride (height, width)
    /// ストライド (高さ, 幅)
    stride: Option<(usize, usize)>,

    /// Padding (height, width)
    /// パディング (高さ, 幅)
    padding: (usize, usize),
}

impl AvgPool2d {
    /// Creates a new AvgPool2d layer
    /// 新しいAvgPool2dレイヤーを作成します
    pub fn new(
        kernel_size: (usize, usize),
        stride: Option<(usize, usize)>,
        padding: Option<(usize, usize)>,
    ) -> Self {
        let stride = stride.unwrap_or(kernel_size);
        let padding = padding.unwrap_or((0, 0));

        AvgPool2d {
            kernel_size,
            stride: Some(stride),
            padding,
        }
    }

    /// Computes the output size given input size
    /// 入力サイズから出力サイズを計算します
    pub fn compute_output_size(&self, input_height: usize, input_width: usize) -> (usize, usize) {
        let stride = self.stride.unwrap_or(self.kernel_size);
        let out_height = (input_height + 2 * self.padding.0 - self.kernel_size.0) / stride.0 + 1;
        let out_width = (input_width + 2 * self.padding.1 - self.kernel_size.1) / stride.1 + 1;
        (out_height, out_width)
    }

    /// Applies padding to input tensor
    /// 入力テンソルにパディングを適用します
    fn apply_padding<T: Float + Copy>(&self, input: &Array4<T>) -> Array4<T> {
        if self.padding == (0, 0) {
            return input.clone();
        }

        let (batch_size, channels, height, width) = input.dim();
        let new_height = height + 2 * self.padding.0;
        let new_width = width + 2 * self.padding.1;

        let mut padded = Array4::zeros((batch_size, channels, new_height, new_width));

        // Copy original data to center of padded tensor
        let mut padded_slice = padded.slice_mut(ndarray::s![
            ..,
            ..,
            self.padding.0..self.padding.0 + height,
            self.padding.1..self.padding.1 + width
        ]);
        padded_slice.assign(input);

        padded
    }

    /// Performs average pooling operation
    /// 平均プーリング演算を実行します
    fn avgpool2d_forward<T: Float + Copy>(&self, input: &Array4<T>) -> Array4<T> {
        let (batch_size, channels, input_height, input_width) = input.dim();
        let stride = self.stride.unwrap_or(self.kernel_size);

        // Apply padding
        let padded_input = self.apply_padding(input);

        // Compute output dimensions
        let (out_height, out_width) = self.compute_output_size(input_height, input_width);

        let mut output = Array4::zeros((batch_size, channels, out_height, out_width));
        let pool_size = T::from(self.kernel_size.0 * self.kernel_size.1).unwrap();

        // Perform average pooling
        for b in 0..batch_size {
            for c in 0..channels {
                for oh in 0..out_height {
                    for ow in 0..out_width {
                        let mut sum = T::zero();

                        for kh in 0..self.kernel_size.0 {
                            for kw in 0..self.kernel_size.1 {
                                let ih = oh * stride.0 + kh;
                                let iw = ow * stride.1 + kw;

                                sum = sum + padded_input[[b, c, ih, iw]];
                            }
                        }

                        output[[b, c, oh, ow]] = sum / pool_size;
                    }
                }
            }
        }

        output
    }

    /// Forward pass of the AvgPool2d layer
    /// AvgPool2dレイヤーの順伝播
    pub fn forward<T>(&self, input: &Variable<T>) -> Variable<T>
    where
        T: Float
            + Copy
            + Send
            + Sync
            + 'static
            + ndarray::ScalarOperand
            + num_traits::FromPrimitive,
    {
        let input_binding = input.data();
        let input_data = input_binding.read().unwrap();

        // Ensure input is 4D: (batch_size, channels, height, width)
        let input_shape = input_data.shape();
        if input_shape.len() != 4 {
            panic!(
                "AvgPool2d expects 4D input (batch_size, channels, height, width), got shape {:?}",
                input_shape
            );
        }

        // Reshape tensor to Array4 for pooling
        let input_array = input_data
            .as_array()
            .view()
            .into_dimensionality::<ndarray::Ix4>()
            .unwrap();

        // Perform average pooling
        let output_array = self.avgpool2d_forward(&input_array.to_owned());
        let output_tensor = Tensor::new(output_array.into_dyn());

        Variable::new(output_tensor, input.requires_grad())
    }

    /// Returns kernel size
    /// カーネルサイズを返します
    pub fn kernel_size(&self) -> (usize, usize) {
        self.kernel_size
    }

    /// Returns stride
    /// ストライドを返します
    pub fn stride(&self) -> (usize, usize) {
        self.stride.unwrap_or(self.kernel_size)
    }

    /// Returns padding
    /// パディングを返します
    pub fn padding(&self) -> (usize, usize) {
        self.padding
    }
}

impl<T> Module<T> for AvgPool2d
where
    T: Float + Copy + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        self.forward(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        vec![] // AvgPool has no parameters
    }

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

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

    #[test]
    fn test_maxpool2d_output_size() {
        let pool = MaxPool2d::new((2, 2), Some((2, 2)), Some((0, 0)));

        let (out_h, out_w) = pool.compute_output_size(4, 4);
        assert_eq!((out_h, out_w), (2, 2));

        let (out_h, out_w) = pool.compute_output_size(8, 8);
        assert_eq!((out_h, out_w), (4, 4));
    }

    #[test]
    fn test_maxpool2d_forward() {
        let pool = MaxPool2d::new((2, 2), Some((2, 2)), Some((0, 0)));

        // Create input: batch=1, channels=1, height=4, width=4
        let input_data = vec![
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
        ];
        let input = Variable::new(Tensor::from_vec(input_data, vec![1, 1, 4, 4]), false);

        let output = pool.forward(&input);
        let output_binding = output.data();
        let output_data = output_binding.read().unwrap();
        let output_shape = output_data.shape();

        // Expected output shape: (1, 1, 2, 2)
        assert_eq!(output_shape, &[1, 1, 2, 2]);

        // Check values - max pooling should take max of each 2x2 region
        let output_values: Vec<f32> = output_data.as_array().iter().cloned().collect();
        assert_eq!(output_values, vec![6.0, 8.0, 14.0, 16.0]);
    }

    #[test]
    fn test_avgpool2d_forward() {
        let pool = AvgPool2d::new((2, 2), Some((2, 2)), Some((0, 0)));

        // Create input: batch=1, channels=1, height=2, width=2
        let input_data = vec![1.0, 2.0, 3.0, 4.0];
        let input = Variable::new(Tensor::from_vec(input_data, vec![1, 1, 2, 2]), false);

        let output = pool.forward(&input);
        let output_binding = output.data();
        let output_data = output_binding.read().unwrap();
        let output_shape = output_data.shape();

        // Expected output shape: (1, 1, 1, 1)
        assert_eq!(output_shape, &[1, 1, 1, 1]);

        // Check value - average of [1, 2, 3, 4] should be 2.5
        let output_values: Vec<f32> = output_data.as_array().iter().cloned().collect();
        assert_eq!(output_values, vec![2.5]);
    }
}