torsh-functional 0.1.2

Functional programming utilities for ToRSh tensors
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
//! Dropout and regularization functions for neural networks

use crate::random_ops::rand;
use torsh_core::{Result as TorshResult, TorshError};
use torsh_tensor::{creation::rand_like, Tensor};

/// Dropout
///
/// During training, randomly zeroes some elements of the input tensor
/// with probability p using samples from a Bernoulli distribution.
pub fn dropout(input: &Tensor, p: f64, training: bool, inplace: bool) -> TorshResult<Tensor> {
    if !training || p == 0.0 {
        return Ok(input.clone());
    }

    if !(0.0..=1.0).contains(&p) {
        return Err(TorshError::invalid_argument_with_context(
            &format!("Dropout probability must be between 0 and 1, got {}", p),
            "dropout",
        ));
    }

    // Generate random mask with probability (1-p) of keeping values
    let keep_prob = 1.0 - p;
    let random_tensor = rand_like(input)?;

    // Create binary mask where values < p are set to 0, others to 1/keep_prob
    let scale = 1.0 / keep_prob;
    let random_data = random_tensor.data()?;
    let mask_data: Vec<f32> = random_data
        .iter()
        .map(|&x| if x < p as f32 { 0.0 } else { scale as f32 })
        .collect();

    let mask = Tensor::from_data(mask_data, input.shape().dims().to_vec(), input.device())?;

    // Apply mask
    let output = if inplace {
        // TODO: Implement inplace operations when available
        input.clone().mul_op(&mask)?
    } else {
        input.mul_op(&mask)?
    };

    Ok(output)
}

/// Dropout1d
///
/// Randomly zero out entire channels (a channel is a 1D feature map).
/// Usually used after Conv1d modules.
pub fn dropout1d(input: &Tensor, p: f64, training: bool, inplace: bool) -> TorshResult<Tensor> {
    if !training || p == 0.0 {
        return Ok(input.clone());
    }

    let shape = input.shape().dims().to_vec();
    if shape.len() != 3 {
        return Err(TorshError::invalid_argument_with_context(
            &format!("Expected 3D input (N, C, L), got {}D", shape.len()),
            "dropout1d",
        ));
    }

    // Generate mask for channels (N, C, 1)
    let mask_shape = vec![shape[0], shape[1], 1];
    let random_tensor = rand(&mask_shape, Some(0.0), Some(1.0), None)?;

    // Create binary mask for channels
    let keep_prob = 1.0 - p;
    let scale = 1.0 / keep_prob;

    // Generate channel mask data
    let mask_data: Vec<f32> = random_tensor
        .to_vec()?
        .iter()
        .map(|&x| if x < p as f32 { 0.0 } else { scale as f32 })
        .collect();

    // Broadcast mask values to full shape
    let mut broadcast_data = vec![0.0f32; shape[0] * shape[1] * shape[2]];
    for n in 0..shape[0] {
        for c in 0..shape[1] {
            let mask_value = mask_data[n * shape[1] + c];
            for l in 0..shape[2] {
                let idx = (n * shape[1] + c) * shape[2] + l;
                broadcast_data[idx] = mask_value;
            }
        }
    }

    let mask = Tensor::from_data(broadcast_data, shape.clone(), input.device())?;

    // Apply mask
    let output = if inplace {
        input.clone().mul_op(&mask)?
    } else {
        input.mul_op(&mask)?
    };

    Ok(output)
}

/// Dropout2d
///
/// Randomly zero out entire channels (a channel is a 2D feature map).
/// Usually used after Conv2d modules.
pub fn dropout2d(input: &Tensor, p: f64, training: bool, inplace: bool) -> TorshResult<Tensor> {
    if !training || p == 0.0 {
        return Ok(input.clone());
    }

    let shape = input.shape().dims().to_vec();
    if shape.len() != 4 {
        return Err(TorshError::invalid_argument_with_context(
            &format!("Expected 4D input (N, C, H, W), got {}D", shape.len()),
            "dropout2d",
        ));
    }

    // Generate mask for channels (N, C, 1, 1)
    let mask_shape = vec![shape[0], shape[1], 1, 1];
    let random_tensor = rand(&mask_shape, Some(0.0), Some(1.0), None)?;

    // Create binary mask for channels
    let keep_prob = 1.0 - p;
    let scale = 1.0 / keep_prob;

    // Generate channel mask data
    let mask_data: Vec<f32> = random_tensor
        .to_vec()?
        .iter()
        .map(|&x| if x < p as f32 { 0.0 } else { scale as f32 })
        .collect();

    // Broadcast mask values to full shape
    let mut broadcast_data = vec![0.0f32; shape[0] * shape[1] * shape[2] * shape[3]];
    for n in 0..shape[0] {
        for c in 0..shape[1] {
            let mask_value = mask_data[n * shape[1] + c];
            for h in 0..shape[2] {
                for w in 0..shape[3] {
                    let idx = ((n * shape[1] + c) * shape[2] + h) * shape[3] + w;
                    broadcast_data[idx] = mask_value;
                }
            }
        }
    }

    let mask = Tensor::from_data(broadcast_data, shape.clone(), input.device())?;

    // Apply mask
    let output = if inplace {
        input.clone().mul_op(&mask)?
    } else {
        input.mul_op(&mask)?
    };

    Ok(output)
}

/// Dropout3d
///
/// Randomly zero out entire channels (a channel is a 3D feature map).
/// Usually used after Conv3d modules.
pub fn dropout3d(input: &Tensor, p: f64, training: bool, inplace: bool) -> TorshResult<Tensor> {
    if !training || p == 0.0 {
        return Ok(input.clone());
    }

    let shape = input.shape().dims().to_vec();
    if shape.len() != 5 {
        return Err(TorshError::invalid_argument_with_context(
            &format!("Expected 5D input (N, C, D, H, W), got {}D", shape.len()),
            "dropout3d",
        ));
    }

    // Generate mask for channels (N, C, 1, 1, 1)
    let mask_shape = vec![shape[0], shape[1], 1, 1, 1];
    let random_tensor = rand(&mask_shape, Some(0.0), Some(1.0), None)?;

    // Create binary mask for channels
    let keep_prob = 1.0 - p;
    let scale = 1.0 / keep_prob;

    // Generate channel mask data
    let mask_data: Vec<f32> = random_tensor
        .to_vec()?
        .iter()
        .map(|&x| if x < p as f32 { 0.0 } else { scale as f32 })
        .collect();

    // Broadcast mask values to full shape
    let mut broadcast_data = vec![0.0f32; shape[0] * shape[1] * shape[2] * shape[3] * shape[4]];
    for n in 0..shape[0] {
        for c in 0..shape[1] {
            let mask_value = mask_data[n * shape[1] + c];
            for d in 0..shape[2] {
                for h in 0..shape[3] {
                    for w in 0..shape[4] {
                        let idx =
                            (((n * shape[1] + c) * shape[2] + d) * shape[3] + h) * shape[4] + w;
                        broadcast_data[idx] = mask_value;
                    }
                }
            }
        }
    }

    let mask = Tensor::from_data(broadcast_data, shape.clone(), input.device())?;

    // Apply mask
    let output = if inplace {
        input.clone().mul_op(&mask)?
    } else {
        input.mul_op(&mask)?
    };

    Ok(output)
}

/// Alpha dropout
///
/// Applies alpha dropout to the input. Alpha Dropout is a type of Dropout
/// that maintains the self-normalizing property.
pub fn alpha_dropout(input: &Tensor, p: f64, training: bool, inplace: bool) -> TorshResult<Tensor> {
    if !training || p == 0.0 {
        return Ok(input.clone());
    }

    if !(0.0..=1.0).contains(&p) {
        return Err(TorshError::invalid_argument_with_context(
            &format!(
                "Alpha dropout probability must be between 0 and 1, got {}",
                p
            ),
            "alpha_dropout",
        ));
    }

    // Alpha dropout parameters for SELU activation
    let alpha = 1.673_263_2_f32;
    let scale = 1.050_701_f32;
    let alpha_p = -alpha * scale;

    // Calculate affine transformation parameters
    let a = ((1.0f32 - p as f32) * (1.0f32 + p as f32 * alpha_p.powi(2))).sqrt();
    let b = -a * alpha_p * p as f32;

    // Generate mask (1.0 where random > p, 0.0 otherwise)
    let random_tensor = rand_like(input)?;
    let random_data = random_tensor.data()?;
    let mask_data: Vec<f32> = random_data
        .iter()
        .map(|&x| if x > p as f32 { 1.0 } else { 0.0 })
        .collect();

    let mask = Tensor::from_data(mask_data, input.shape().dims().to_vec(), input.device())?;

    // Apply alpha dropout

    if inplace {
        // x = x * mask + alpha_p * (1 - mask)
        // x = a * x + b
        let not_mask = mask.neg()?.add_scalar(1.0)?;
        let alpha_term = not_mask.mul_scalar(alpha_p)?;
        let x = input.clone().mul_op(&mask)?.add_op(&alpha_term)?;
        x.mul_scalar(a)?.add_scalar(b)
    } else {
        let not_mask = mask.neg()?.add_scalar(1.0)?;
        let alpha_term = not_mask.mul_scalar(alpha_p)?;
        let x = input.mul_op(&mask)?.add_op(&alpha_term)?;
        x.mul_scalar(a)?.add_scalar(b)
    }
}

/// Feature alpha dropout
///
/// Applies alpha dropout to entire channels.
pub fn feature_alpha_dropout(
    input: &Tensor,
    p: f64,
    training: bool,
    inplace: bool,
) -> TorshResult<Tensor> {
    if !training || p == 0.0 {
        return Ok(input.clone());
    }

    let shape = input.shape().dims().to_vec();
    if shape.len() < 2 {
        return Err(TorshError::invalid_argument_with_context(
            "Feature alpha dropout requires at least 2D input",
            "feature_alpha_dropout",
        ));
    }

    // Alpha dropout parameters
    let alpha = 1.673_263_2_f32;
    let scale = 1.050_701_f32;
    let alpha_p = -alpha * scale;

    // Calculate affine transformation parameters
    let a = ((1.0f32 - p as f32) * (1.0f32 + p as f32 * alpha_p.powi(2))).sqrt();
    let b = -a * alpha_p * p as f32;

    // Generate mask for channels
    let mut mask_shape = vec![shape[0], shape[1]];
    mask_shape.extend(vec![1; shape.len() - 2]);
    let random_tensor = rand(&mask_shape, Some(0.0), Some(1.0), None)?;

    // Create mask (1.0 where random > p, 0.0 otherwise)
    let mask_data: Vec<f32> = random_tensor
        .to_vec()?
        .iter()
        .map(|&x| if x > p as f32 { 1.0 } else { 0.0 })
        .collect();

    // Broadcast mask to input shape
    let total_size: usize = shape.iter().product();
    let mut broadcast_data = vec![0.0f32; total_size];

    // Calculate strides for broadcasting
    let mut strides = vec![1; shape.len()];
    for i in (0..shape.len() - 1).rev() {
        strides[i] = strides[i + 1] * shape[i + 1];
    }

    // Broadcast the mask
    for i in 0..total_size {
        let mut idx = i;
        let n = idx / strides[0];
        idx %= strides[0];
        let c = idx / strides[1];

        let mask_idx = n * shape[1] + c;
        broadcast_data[i] = mask_data[mask_idx];
    }

    let mask = Tensor::from_data(broadcast_data, shape.clone(), input.device())?;

    // Apply feature alpha dropout

    if inplace {
        let not_mask = mask.neg()?.add_scalar(1.0)?;
        let alpha_term = not_mask.mul_scalar(alpha_p)?;
        let x = input.clone().mul_op(&mask)?.add_op(&alpha_term)?;
        x.mul_scalar(a)?.add_scalar(b)
    } else {
        let not_mask = mask.neg()?.add_scalar(1.0)?;
        let alpha_term = not_mask.mul_scalar(alpha_p)?;
        let x = input.mul_op(&mask)?.add_op(&alpha_term)?;
        x.mul_scalar(a)?.add_scalar(b)
    }
}

/// Fractional max pooling 2d with dropout
///
/// Applies fractional max pooling with random sampling for regularization
pub fn fractional_max_pool2d_with_indices(
    input: &Tensor,
    _kernel_size: (usize, usize),
    _output_size: Option<(usize, usize)>,
    _output_ratio: Option<(f64, f64)>,
    _return_indices: bool,
) -> TorshResult<(Tensor, Option<Tensor>)> {
    // TODO: Implement fractional max pooling
    // For now, return the input unchanged as a placeholder
    Ok((input.clone(), None))
}

/// Gaussian dropout
///
/// Multiplicative noise where each element is multiplied by a sample from
/// a Gaussian distribution with mean 1 and standard deviation sqrt(p/(1-p))
pub fn gaussian_dropout(
    input: &Tensor,
    p: f64,
    training: bool,
    inplace: bool,
) -> TorshResult<Tensor> {
    if !training || p == 0.0 {
        return Ok(input.clone());
    }

    if !(0.0..1.0).contains(&p) {
        return Err(TorshError::invalid_argument_with_context(
            &format!("Gaussian dropout probability must be in [0, 1), got {}", p),
            "gaussian_dropout",
        ));
    }

    // Standard deviation of the multiplicative noise
    let std = (p / (1.0 - p)).sqrt();

    // Generate Gaussian noise with mean 0 and std 1, then scale and shift to mean 1
    let randn = torsh_tensor::creation::randn_like(input);
    let noise = randn?.mul_scalar(std as f32)?.add_scalar(1.0)?;

    // Apply multiplicative noise

    if inplace {
        input.clone().mul_op(&noise)
    } else {
        input.mul_op(&noise)
    }
}

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

    #[test]
    fn test_dropout_probability_validation() -> TorshResult<()> {
        // Test that invalid probabilities are rejected
        let input = ones::<f32>(&[2, 3])?;

        assert!(dropout(&input, -0.1, true, false).is_err());
        assert!(dropout(&input, 1.1, true, false).is_err());

        // Test that p=0 returns input unchanged (doesn't execute dropout logic)
        assert!(dropout(&input, 0.0, true, false).is_ok());

        // Test that training=false returns input unchanged (doesn't execute dropout logic)
        assert!(dropout(&input, 0.5, false, false).is_ok());
        Ok(())
    }
}