ferrotorch-nn 0.2.1

Neural network modules for ferrotorch — layers, losses, initialization
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
//! Identity and Flatten modules.
//!
//! [`Identity`] passes input through unchanged — useful for model composition,
//! conditional layers, and debugging.
//!
//! [`Flatten`] reshapes input by flattening contiguous dimensions from
//! `start_dim` to `end_dim` into a single dimension. The default
//! (`start_dim=1, end_dim=-1`) flattens everything except the batch dimension.

use ferrotorch_core::grad_fns::shape::reshape;
use ferrotorch_core::{FerrotorchError, FerrotorchResult, Float, Tensor};

use crate::module::Module;
use crate::parameter::Parameter;

// ===========================================================================
// Identity
// ===========================================================================

/// A module that returns its input unchanged.
///
/// Useful as a placeholder in model architectures where a layer is
/// conditionally applied, or for debugging / hook attachment points.
///
/// Has zero learnable parameters.
///
/// # Examples
///
/// ```ignore
/// let id = Identity;
/// let output = id.forward(&input)?; // output == input
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Identity {
    training: bool,
}

impl Identity {
    /// Create a new `Identity` module.
    pub fn new() -> Self {
        Self { training: true }
    }
}

impl Default for Identity {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Float> Module<T> for Identity {
    fn forward(&self, input: &Tensor<T>) -> FerrotorchResult<Tensor<T>> {
        Ok(input.clone())
    }

    fn parameters(&self) -> Vec<&Parameter<T>> {
        vec![]
    }

    fn parameters_mut(&mut self) -> Vec<&mut Parameter<T>> {
        vec![]
    }

    fn named_parameters(&self) -> Vec<(String, &Parameter<T>)> {
        vec![]
    }

    fn train(&mut self) {
        self.training = true;
    }

    fn eval(&mut self) {
        self.training = false;
    }

    fn is_training(&self) -> bool {
        self.training
    }
}

// ===========================================================================
// Flatten
// ===========================================================================

/// Flattens a contiguous range of dimensions in a tensor.
///
/// By default, flattens all dimensions except the batch dimension
/// (`start_dim=1, end_dim=-1`), producing output of shape `[B, *]`.
///
/// Negative `end_dim` values are resolved relative to the input's
/// number of dimensions (`-1` = last dim).
///
/// # Examples
///
/// ```ignore
/// // Input: [2, 3, 4, 5]
/// let flatten = Flatten::new(1, -1);
/// let output = flatten.forward(&input)?;
/// // Output: [2, 60]
///
/// // Flatten specific range
/// let flatten = Flatten::new(2, 3);
/// let output = flatten.forward(&input)?;
/// // Output: [2, 3, 20]
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Flatten {
    /// First dimension to flatten (inclusive).
    pub start_dim: usize,
    /// Last dimension to flatten (inclusive). Negative values count from the end.
    pub end_dim: isize,
    training: bool,
}

impl Flatten {
    /// Create a new `Flatten` module.
    ///
    /// # Arguments
    ///
    /// * `start_dim` - First dimension to flatten (inclusive, 0-indexed).
    /// * `end_dim` - Last dimension to flatten (inclusive). Use `-1` for the
    ///   last dimension, `-2` for second-to-last, etc.
    pub fn new(start_dim: usize, end_dim: isize) -> Self {
        Self {
            start_dim,
            end_dim,
            training: true,
        }
    }

    /// Resolve `end_dim` to a concrete dimension index.
    fn resolve_end_dim(&self, ndim: usize) -> FerrotorchResult<usize> {
        let resolved = if self.end_dim < 0 {
            let d = ndim as isize + self.end_dim;
            if d < 0 {
                return Err(FerrotorchError::InvalidArgument {
                    message: format!(
                        "Flatten: end_dim {} is out of range for input with {} dims",
                        self.end_dim, ndim
                    ),
                });
            }
            d as usize
        } else {
            self.end_dim as usize
        };

        if resolved >= ndim {
            return Err(FerrotorchError::InvalidArgument {
                message: format!(
                    "Flatten: resolved end_dim {} is out of range for input with {} dims",
                    resolved, ndim
                ),
            });
        }

        Ok(resolved)
    }
}

impl Default for Flatten {
    fn default() -> Self {
        Self::new(1, -1)
    }
}

impl<T: Float> Module<T> for Flatten {
    fn forward(&self, input: &Tensor<T>) -> FerrotorchResult<Tensor<T>> {
        let shape = input.shape();
        let ndim = shape.len();

        // 0-D tensor: nothing to flatten.
        if ndim == 0 {
            return Err(FerrotorchError::InvalidArgument {
                message: "Flatten: cannot flatten a 0-D (scalar) tensor".into(),
            });
        }

        // 1-D tensor: already flat.
        if ndim == 1 {
            return Ok(input.clone());
        }

        if self.start_dim >= ndim {
            return Err(FerrotorchError::InvalidArgument {
                message: format!(
                    "Flatten: start_dim {} is out of range for input with {} dims",
                    self.start_dim, ndim
                ),
            });
        }

        let end_dim = self.resolve_end_dim(ndim)?;

        if self.start_dim > end_dim {
            return Err(FerrotorchError::InvalidArgument {
                message: format!(
                    "Flatten: start_dim ({}) must be <= end_dim ({})",
                    self.start_dim, end_dim
                ),
            });
        }

        // If start == end, no flattening needed.
        if self.start_dim == end_dim {
            return Ok(input.clone());
        }

        // Build new shape: [dims before start, flattened, dims after end].
        let mut new_shape: Vec<isize> = Vec::with_capacity(ndim - (end_dim - self.start_dim));

        for &d in &shape[..self.start_dim] {
            new_shape.push(d as isize);
        }

        // Flatten the range [start_dim..=end_dim] into one dim.
        let flattened: usize = shape[self.start_dim..=end_dim].iter().product();
        new_shape.push(flattened as isize);

        for &d in &shape[end_dim + 1..] {
            new_shape.push(d as isize);
        }

        reshape(input, &new_shape)
    }

    fn parameters(&self) -> Vec<&Parameter<T>> {
        vec![]
    }

    fn parameters_mut(&mut self) -> Vec<&mut Parameter<T>> {
        vec![]
    }

    fn named_parameters(&self) -> Vec<(String, &Parameter<T>)> {
        vec![]
    }

    fn train(&mut self) {
        self.training = true;
    }

    fn eval(&mut self) {
        self.training = false;
    }

    fn is_training(&self) -> bool {
        self.training
    }
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use ferrotorch_core::autograd::graph::backward;
    use ferrotorch_core::storage::TensorStorage;

    /// Helper: create a leaf tensor with given data, shape, and requires_grad.
    fn leaf(data: &[f64], shape: &[usize], requires_grad: bool) -> Tensor<f64> {
        Tensor::from_storage(
            TensorStorage::cpu(data.to_vec()),
            shape.to_vec(),
            requires_grad,
        )
        .unwrap()
    }

    // -----------------------------------------------------------------------
    // Identity tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_identity_forward() {
        let id = Identity::new();
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[2, 2], false);
        let output: Tensor<f64> = id.forward(&input).unwrap();
        assert_eq!(output.shape(), input.shape());
        assert_eq!(output.data_vec().unwrap(), input.data_vec().unwrap());
    }

    #[test]
    fn test_identity_no_parameters() {
        let id = Identity::new();
        assert!(Module::<f64>::parameters(&id).is_empty());
        assert!(Module::<f64>::named_parameters(&id).is_empty());
    }

    #[test]
    fn test_identity_preserves_grad() {
        let id = Identity::new();
        let input = leaf(&[1.0, 2.0, 3.0], &[3], true);
        let output: Tensor<f64> = id.forward(&input).unwrap();
        assert!(output.requires_grad());
    }

    #[test]
    fn test_identity_train_eval() {
        let mut id = Identity::new();
        assert!(Module::<f64>::is_training(&id));
        Module::<f64>::eval(&mut id);
        assert!(!Module::<f64>::is_training(&id));
        Module::<f64>::train(&mut id);
        assert!(Module::<f64>::is_training(&id));
    }

    #[test]
    fn test_identity_empty_tensor() {
        let id = Identity::new();
        let input = leaf(&[], &[0], false);
        let output: Tensor<f64> = id.forward(&input).unwrap();
        assert_eq!(output.shape(), &[0]);
        assert_eq!(output.numel(), 0);
    }

    #[test]
    fn test_identity_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Identity>();
    }

    // -----------------------------------------------------------------------
    // Flatten tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_flatten_default() {
        // Default: start_dim=1, end_dim=-1 => flatten all but batch.
        let flatten = Flatten::default();
        let input = leaf(
            &(0..120).map(|i| i as f64).collect::<Vec<_>>(),
            &[2, 3, 4, 5],
            false,
        );
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.shape(), &[2, 60]);
    }

    #[test]
    fn test_flatten_specific_range() {
        // Flatten dims 2..3 of [2, 3, 4, 5] => [2, 3, 20].
        let flatten = Flatten::new(2, 3);
        let input = leaf(
            &(0..120).map(|i| i as f64).collect::<Vec<_>>(),
            &[2, 3, 4, 5],
            false,
        );
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.shape(), &[2, 3, 20]);
    }

    #[test]
    fn test_flatten_all_dims() {
        // start_dim=0, end_dim=-1 => flatten everything.
        let flatten = Flatten::new(0, -1);
        let input = leaf(
            &(0..24).map(|i| i as f64).collect::<Vec<_>>(),
            &[2, 3, 4],
            false,
        );
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.shape(), &[24]);
    }

    #[test]
    fn test_flatten_noop_single_dim() {
        // start_dim == end_dim => no-op.
        let flatten = Flatten::new(1, 1);
        let input = leaf(
            &(0..12).map(|i| i as f64).collect::<Vec<_>>(),
            &[3, 4],
            false,
        );
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.shape(), &[3, 4]);
    }

    #[test]
    fn test_flatten_1d_input() {
        // 1-D input: already flat, should return as-is.
        let flatten = Flatten::new(0, -1);
        let input = leaf(&[1.0, 2.0, 3.0], &[3], false);
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.shape(), &[3]);
    }

    #[test]
    fn test_flatten_0d_error() {
        // 0-D tensor should error.
        let flatten = Flatten::new(0, -1);
        let input = leaf(&[42.0], &[], false);
        assert!(Module::<f64>::forward(&flatten, &input).is_err());
    }

    #[test]
    fn test_flatten_start_dim_out_of_range() {
        let flatten = Flatten::new(5, -1);
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[2, 2], false);
        assert!(Module::<f64>::forward(&flatten, &input).is_err());
    }

    #[test]
    fn test_flatten_end_dim_out_of_range() {
        let flatten = Flatten::new(0, 10);
        let input = leaf(&[1.0, 2.0, 3.0, 4.0], &[2, 2], false);
        assert!(Module::<f64>::forward(&flatten, &input).is_err());
    }

    #[test]
    fn test_flatten_start_gt_end_error() {
        let flatten = Flatten::new(2, 1);
        let input = leaf(
            &(0..24).map(|i| i as f64).collect::<Vec<_>>(),
            &[2, 3, 4],
            false,
        );
        assert!(Module::<f64>::forward(&flatten, &input).is_err());
    }

    #[test]
    fn test_flatten_preserves_data() {
        let flatten = Flatten::default();
        let data: Vec<f64> = (0..24).map(|i| i as f64).collect();
        let input = leaf(&data, &[2, 3, 4], false);
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.data_vec().unwrap(), data);
    }

    #[test]
    fn test_flatten_backward() {
        use ferrotorch_core::tensor::GradFn;
        use std::sync::Arc;

        /// Sum backward helper that propagates gradients.
        #[derive(Debug)]
        struct SumBackwardHelper {
            input: Tensor<f64>,
        }

        impl GradFn<f64> for SumBackwardHelper {
            fn backward(
                &self,
                _grad_output: &Tensor<f64>,
            ) -> FerrotorchResult<Vec<Option<Tensor<f64>>>> {
                let ones_data = vec![1.0f64; self.input.numel()];
                let ones = Tensor::from_storage(
                    TensorStorage::cpu(ones_data),
                    self.input.shape().to_vec(),
                    false,
                )?;
                Ok(vec![Some(ones)])
            }

            fn inputs(&self) -> Vec<&Tensor<f64>> {
                vec![&self.input]
            }

            fn name(&self) -> &'static str {
                "SumBackwardHelper"
            }
        }

        let flatten = Flatten::default();
        let input = leaf(
            &(0..24).map(|i| i as f64).collect::<Vec<_>>(),
            &[2, 3, 4],
            true,
        );
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.shape(), &[2, 12]);
        assert!(output.requires_grad());

        // Trigger backward through a differentiable sum.
        let out_data = output.data().unwrap();
        let total: f64 = out_data.iter().sum();
        let sum_gf = Arc::new(SumBackwardHelper {
            input: output.clone(),
        });
        let loss = Tensor::from_operation(TensorStorage::cpu(vec![total]), vec![], sum_gf).unwrap();
        backward(&loss).unwrap();

        let grad = input.grad().unwrap().unwrap();
        assert_eq!(grad.shape(), &[2, 3, 4]);
        // Gradient of sum is all ones.
        for &v in grad.data().unwrap().iter() {
            assert!((v - 1.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_flatten_no_parameters() {
        let flatten = Flatten::default();
        assert!(Module::<f64>::parameters(&flatten).is_empty());
        assert!(Module::<f64>::named_parameters(&flatten).is_empty());
    }

    #[test]
    fn test_flatten_zero_size_dim() {
        // Tensor with a zero-size dimension should still work.
        let flatten = Flatten::default();
        let input = leaf(&[], &[2, 0, 4], false);
        let output: Tensor<f64> = flatten.forward(&input).unwrap();
        assert_eq!(output.shape(), &[2, 0]);
    }

    #[test]
    fn test_flatten_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Flatten>();
    }
}