torsh-nn 0.1.2

Neural network modules for ToRSh with PyTorch-compatible API
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
//! Advanced neural network layers using SciRS2 algorithms

use crate::{Module, Parameter};
use torsh_core::error::TorshError;
use torsh_tensor::{
    creation::{ones, randn, zeros},
    Tensor,
};

/// Multi-Head Attention layer
pub struct MultiHeadAttention {
    pub num_heads: usize,
    pub d_model: usize,
    pub d_k: usize,
    pub d_v: usize,

    // Weight matrices
    pub w_q: Parameter,
    pub w_k: Parameter,
    pub w_v: Parameter,
    pub w_o: Parameter,

    // Optional bias terms
    pub bias_q: Option<Parameter>,
    pub bias_k: Option<Parameter>,
    pub bias_v: Option<Parameter>,
    pub bias_o: Option<Parameter>,

    pub dropout: f64,
    pub scale: f64,
}

impl MultiHeadAttention {
    /// Create a new multi-head attention layer
    pub fn new(
        d_model: usize,
        num_heads: usize,
        dropout: f64,
        bias: bool,
    ) -> Result<Self, TorshError> {
        let d_k = d_model / num_heads;
        let d_v = d_model / num_heads;

        let scale = 1.0 / (d_k as f64).sqrt();

        // Initialize weight matrices with Xavier/Glorot initialization
        let fan_in = d_model as f64;
        let fan_out = d_model as f64;
        let std = (2.0 / (fan_in + fan_out)).sqrt();

        let w_q = Parameter::new(randn(&[d_model, d_model])?.mul_scalar(std as f32)?);
        let w_k = Parameter::new(randn(&[d_model, d_model])?.mul_scalar(std as f32)?);
        let w_v = Parameter::new(randn(&[d_model, d_model])?.mul_scalar(std as f32)?);
        let w_o = Parameter::new(randn(&[d_model, d_model])?.mul_scalar(std as f32)?);

        let bias_q = if bias {
            Some(Parameter::new(zeros(&[d_model])?))
        } else {
            None
        };
        let bias_k = if bias {
            Some(Parameter::new(zeros(&[d_model])?))
        } else {
            None
        };
        let bias_v = if bias {
            Some(Parameter::new(zeros(&[d_model])?))
        } else {
            None
        };
        let bias_o = if bias {
            Some(Parameter::new(zeros(&[d_model])?))
        } else {
            None
        };

        Ok(Self {
            num_heads,
            d_model,
            d_k,
            d_v,
            w_q,
            w_k,
            w_v,
            w_o,
            bias_q,
            bias_k,
            bias_v,
            bias_o,
            dropout,
            scale,
        })
    }

    /// Apply attention mechanism
    ///
    /// Implements scaled dot-product attention: Attention(Q,K,V) = softmax(QK^T / sqrt(d_k))V
    ///
    /// # Arguments
    /// * `q` - Query tensor [batch_size, num_heads, seq_len, d_k]
    /// * `k` - Key tensor [batch_size, num_heads, seq_len, d_k]
    /// * `v` - Value tensor [batch_size, num_heads, seq_len, d_v]
    /// * `mask` - Optional attention mask
    ///
    /// # Returns
    /// Attention output [batch_size, num_heads, seq_len, d_v]
    pub fn attention(
        &self,
        q: &Tensor,
        k: &Tensor,
        v: &Tensor,
        mask: Option<&Tensor>,
    ) -> Result<Tensor, TorshError> {
        // Input shape: [batch_size, num_heads, seq_len, d_k]
        let q_shape_binding = q.shape();
        let q_shape = q_shape_binding.dims();
        let batch_size = q_shape[0];
        let num_heads = q_shape[1];
        let seq_len_q = q_shape[2];
        let d_k = q_shape[3];

        let k_shape_binding = k.shape();
        let k_shape = k_shape_binding.dims();
        let seq_len_k = k_shape[2];

        // Step 1: Compute attention scores: Q @ K^T
        // We need to transpose the last two dimensions of K
        // K: [batch, heads, seq_k, d_k] -> K^T: [batch, heads, d_k, seq_k]

        // Flatten batch and heads dimensions for easier processing
        // q: [batch*heads, seq_q, d_k]
        // k: [batch*heads, seq_k, d_k]
        // v: [batch*heads, seq_k, d_v]
        let batch_heads = batch_size * num_heads;

        let q_flat = q.view(&[batch_heads as i32, seq_len_q as i32, d_k as i32])?;
        let k_flat = k.view(&[batch_heads as i32, seq_len_k as i32, d_k as i32])?;
        let v_flat = v.view(&[batch_heads as i32, seq_len_k as i32, d_k as i32])?;

        // Compute Q @ K^T for each batch*head
        // We need to do this manually since we need per-batch matmul
        let q_data = q_flat.to_vec()?;
        let k_data = k_flat.to_vec()?;
        let v_data = v_flat.to_vec()?;

        let mut scores_data = vec![0.0f32; batch_heads * seq_len_q * seq_len_k];

        // For each batch*head
        for bh in 0..batch_heads {
            let q_offset = bh * seq_len_q * d_k;
            let k_offset = bh * seq_len_k * d_k;
            let scores_offset = bh * seq_len_q * seq_len_k;

            // Compute Q @ K^T for this batch*head
            for i in 0..seq_len_q {
                for j in 0..seq_len_k {
                    let mut dot_product = 0.0f32;
                    for d in 0..d_k {
                        let q_val = q_data[q_offset + i * d_k + d];
                        let k_val = k_data[k_offset + j * d_k + d];
                        dot_product += q_val * k_val;
                    }
                    // Scale by sqrt(d_k) for numerical stability
                    scores_data[scores_offset + i * seq_len_k + j] =
                        dot_product / (d_k as f32).sqrt();
                }
            }
        }

        // Step 2: Apply mask if provided (add large negative value to masked positions)
        if let Some(mask_tensor) = mask {
            let mask_data = mask_tensor.to_vec()?;
            for i in 0..scores_data.len() {
                if mask_data[i] == 0.0 {
                    scores_data[i] = -1e9; // Large negative value for masked positions
                }
            }
        }

        // Step 3: Apply softmax over the last dimension (seq_len_k)
        // Softmax is applied row-wise (for each query position)
        for bh in 0..batch_heads {
            for i in 0..seq_len_q {
                let row_offset = bh * seq_len_q * seq_len_k + i * seq_len_k;

                // Find max for numerical stability
                let max_val = scores_data[row_offset..row_offset + seq_len_k]
                    .iter()
                    .fold(f32::NEG_INFINITY, |a, &b| a.max(b));

                // Compute exp(x - max) and sum
                let mut exp_sum = 0.0f32;
                for j in 0..seq_len_k {
                    let idx = row_offset + j;
                    scores_data[idx] = (scores_data[idx] - max_val).exp();
                    exp_sum += scores_data[idx];
                }

                // Normalize
                for j in 0..seq_len_k {
                    let idx = row_offset + j;
                    scores_data[idx] /= exp_sum + 1e-9; // Add epsilon for numerical stability
                }
            }
        }

        // Step 4: Apply attention to values: attention_weights @ V
        // scores: [batch*heads, seq_q, seq_k]
        // v: [batch*heads, seq_k, d_k]
        // output: [batch*heads, seq_q, d_k]
        let mut output_data = vec![0.0f32; batch_heads * seq_len_q * d_k];

        for bh in 0..batch_heads {
            let scores_offset = bh * seq_len_q * seq_len_k;
            let v_offset = bh * seq_len_k * d_k;
            let output_offset = bh * seq_len_q * d_k;

            for i in 0..seq_len_q {
                for d in 0..d_k {
                    let mut weighted_sum = 0.0f32;
                    for j in 0..seq_len_k {
                        let attention_weight = scores_data[scores_offset + i * seq_len_k + j];
                        let v_val = v_data[v_offset + j * d_k + d];
                        weighted_sum += attention_weight * v_val;
                    }
                    output_data[output_offset + i * d_k + d] = weighted_sum;
                }
            }
        }

        // Reshape back to [batch_size, num_heads, seq_len_q, d_k]
        let output_flat = Tensor::from_vec(output_data, &[batch_heads, seq_len_q, d_k])?;
        output_flat.view(&[
            batch_size as i32,
            num_heads as i32,
            seq_len_q as i32,
            d_k as i32,
        ])
    }
}

impl Module for MultiHeadAttention {
    fn forward(&self, input: &Tensor) -> Result<Tensor, TorshError> {
        let batch_size = input.shape().dims()[0];
        let seq_len = input.shape().dims()[1];
        let d_model = input.shape().dims()[2];

        // Reshape input to 2D for linear transformations: [batch_size * seq_len, d_model]
        let input_2d = input.view(&[(batch_size * seq_len) as i32, d_model as i32])?;

        // Linear transformations for Q, K, V
        let q_2d = input_2d.matmul(&self.w_q.clone_data())?;
        let k_2d = input_2d.matmul(&self.w_k.clone_data())?;
        let v_2d = input_2d.matmul(&self.w_v.clone_data())?;

        // Reshape back to 3D: [batch_size, seq_len, d_model]
        let q = q_2d.view(&[batch_size as i32, seq_len as i32, d_model as i32])?;
        let k = k_2d.view(&[batch_size as i32, seq_len as i32, d_model as i32])?;
        let v = v_2d.view(&[batch_size as i32, seq_len as i32, d_model as i32])?;

        // Add bias if present
        let q = if let Some(ref bias) = self.bias_q {
            q.add(&bias.clone_data())?
        } else {
            q
        };

        let k = if let Some(ref bias) = self.bias_k {
            k.add(&bias.clone_data())?
        } else {
            k
        };

        let v = if let Some(ref bias) = self.bias_v {
            v.add(&bias.clone_data())?
        } else {
            v
        };

        // Reshape for multi-head attention
        // [batch_size, seq_len, d_model] -> [batch_size, num_heads, seq_len, d_k]
        let q = q
            .view(&[
                batch_size as i32,
                seq_len as i32,
                self.num_heads as i32,
                self.d_k as i32,
            ])?
            .transpose(1, 2)?;
        let k = k
            .view(&[
                batch_size as i32,
                seq_len as i32,
                self.num_heads as i32,
                self.d_k as i32,
            ])?
            .transpose(1, 2)?;
        let v = v
            .view(&[
                batch_size as i32,
                seq_len as i32,
                self.num_heads as i32,
                self.d_v as i32,
            ])?
            .transpose(1, 2)?;

        // Apply attention
        let attention_output = self.attention(&q, &k, &v, None)?;

        // Reshape back to original dimensions
        let attention_output = attention_output.transpose(1, 2)?.contiguous()?.view(&[
            batch_size as i32,
            seq_len as i32,
            self.d_model as i32,
        ])?;

        // Final linear transformation - reshape to 2D for matmul
        let output_2d =
            attention_output.view(&[(batch_size * seq_len) as i32, self.d_model as i32])?;
        let output_transformed = output_2d.matmul(&self.w_o.clone_data())?;
        let output =
            output_transformed.view(&[batch_size as i32, seq_len as i32, self.d_model as i32])?;

        if let Some(ref bias) = self.bias_o {
            Ok(output.add(&bias.clone_data())?)
        } else {
            Ok(output)
        }
    }

    fn parameters(&self) -> std::collections::HashMap<String, Parameter> {
        let mut params = std::collections::HashMap::new();
        params.insert("w_q".to_string(), self.w_q.clone());
        params.insert("w_k".to_string(), self.w_k.clone());
        params.insert("w_v".to_string(), self.w_v.clone());
        params.insert("w_o".to_string(), self.w_o.clone());

        if let Some(ref bias) = self.bias_q {
            params.insert("bias_q".to_string(), bias.clone());
        }
        if let Some(ref bias) = self.bias_k {
            params.insert("bias_k".to_string(), bias.clone());
        }
        if let Some(ref bias) = self.bias_v {
            params.insert("bias_v".to_string(), bias.clone());
        }
        if let Some(ref bias) = self.bias_o {
            params.insert("bias_o".to_string(), bias.clone());
        }

        params
    }

    fn train(&mut self) {
        // Set to training mode
    }

    fn eval(&mut self) {
        // Set to evaluation mode
    }
}

/// Advanced Layer Normalization with learnable parameters for transformers
pub struct AdvancedLayerNorm {
    pub normalized_shape: Vec<usize>,
    pub weight: Parameter,
    pub bias: Option<Parameter>,
    pub eps: f64,
}

impl AdvancedLayerNorm {
    /// Create a new layer normalization layer
    pub fn new(normalized_shape: Vec<usize>, bias: bool, eps: f64) -> Result<Self, TorshError> {
        let num_features = normalized_shape.iter().product();

        let weight = Parameter::new(ones(&[num_features])?);
        let bias = if bias {
            Some(Parameter::new(zeros(&[num_features])?))
        } else {
            None
        };

        Ok(Self {
            normalized_shape,
            weight,
            bias,
            eps,
        })
    }
}

impl Module for AdvancedLayerNorm {
    fn forward(&self, input: &Tensor) -> Result<Tensor, TorshError> {
        // Implement proper layer normalization
        // Layer norm: (x - mean) / sqrt(var + eps) * weight + bias

        let input_shape_binding = input.shape();
        let input_shape = input_shape_binding.dims();
        let num_features = self.normalized_shape.iter().product::<usize>();

        // Verify that the normalized shape matches the last dimensions of input
        let input_suffix = &input_shape[input_shape.len() - self.normalized_shape.len()..];
        if input_suffix != self.normalized_shape.as_slice() {
            return Err(TorshError::InvalidArgument(format!(
                "Normalized shape {:?} doesn't match input shape suffix {:?}",
                self.normalized_shape, input_suffix
            )));
        }

        // Calculate batch dimensions
        let batch_size: usize = input_shape[..input_shape.len() - self.normalized_shape.len()]
            .iter()
            .product();

        // Get input data
        let input_data = input.to_vec()?;
        let weight_data = self.weight.clone_data().to_vec()?;
        let bias_data = if let Some(ref bias) = self.bias {
            Some(bias.clone_data().to_vec()?)
        } else {
            None
        };

        let mut output_data = vec![0.0f32; input_data.len()];

        // Process each instance (normalize over the last dimensions)
        for b in 0..batch_size {
            let instance_offset = b * num_features;
            let instance = &input_data[instance_offset..instance_offset + num_features];

            // Compute mean
            let mean: f32 = instance.iter().sum::<f32>() / num_features as f32;

            // Compute variance
            let variance: f32 =
                instance.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / num_features as f32;

            // Normalize and apply affine transformation
            let inv_std = 1.0 / (variance + self.eps as f32).sqrt();

            for i in 0..num_features {
                let normalized = (instance[i] - mean) * inv_std;
                let scaled = normalized * weight_data[i];
                let shifted = if let Some(ref bias) = bias_data {
                    scaled + bias[i]
                } else {
                    scaled
                };
                output_data[instance_offset + i] = shifted;
            }
        }

        Tensor::from_vec(output_data, input_shape)
    }

    fn parameters(&self) -> std::collections::HashMap<String, Parameter> {
        let mut params = std::collections::HashMap::new();
        params.insert("weight".to_string(), self.weight.clone());
        if let Some(ref bias) = self.bias {
            params.insert("bias".to_string(), bias.clone());
        }
        params
    }

    fn train(&mut self) {}
    fn eval(&mut self) {}
}

/// Positional Encoding for Transformer models
pub struct PositionalEncoding {
    pub encoding: Tensor,
    pub dropout: f64,
}

impl PositionalEncoding {
    /// Create positional encoding
    ///
    /// Creates sinusoidal positional encodings as described in "Attention Is All You Need"
    /// PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
    /// PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
    pub fn new(d_model: usize, max_len: usize, dropout: f64) -> Result<Self, TorshError> {
        // Create sinusoidal positional encoding
        let mut encoding_data = vec![0.0f32; max_len * d_model];

        for pos in 0..max_len {
            for i in (0..d_model).step_by(2) {
                let angle = pos as f32 / 10000.0_f32.powf(i as f32 / d_model as f32);

                // Apply sin to even indices
                encoding_data[pos * d_model + i] = angle.sin();

                // Apply cos to odd indices
                if i + 1 < d_model {
                    encoding_data[pos * d_model + i + 1] = angle.cos();
                }
            }
        }

        let encoding = Tensor::from_vec(encoding_data, &[max_len, d_model])?;

        Ok(Self { encoding, dropout })
    }
}

impl Module for PositionalEncoding {
    fn forward(&self, input: &Tensor) -> Result<Tensor, TorshError> {
        // Input shape: [batch_size, seq_len, d_model]
        let input_shape_binding = input.shape();
        let input_shape = input_shape_binding.dims();
        let seq_len = input_shape[1];
        let d_model = input_shape[2];

        // Get the positional encoding for the sequence length
        // encoding shape: [max_len, d_model]
        // We need: [seq_len, d_model]
        let encoding_shape_binding = self.encoding.shape();
        let encoding_shape = encoding_shape_binding.dims();
        let max_len = encoding_shape[0];

        if seq_len > max_len {
            return Err(TorshError::InvalidArgument(format!(
                "Sequence length {} exceeds maximum positional encoding length {}",
                seq_len, max_len
            )));
        }

        // Slice the encoding to match sequence length
        let encoding_data = self.encoding.to_vec()?;
        let seq_encoding_data: Vec<f32> = encoding_data[..seq_len * d_model].to_vec();

        // Create tensor with shape [seq_len, d_model]
        let seq_encoding = Tensor::from_vec(seq_encoding_data, &[seq_len, d_model])?;

        // Add positional encoding to input
        // input: [batch_size, seq_len, d_model]
        // seq_encoding: [seq_len, d_model]
        // Broadcasting: seq_encoding will be added to each batch

        let input_data = input.to_vec()?;
        let encoding_slice = seq_encoding.to_vec()?;

        let batch_size = input_shape[0];
        let mut output_data = vec![0.0f32; input_data.len()];

        for b in 0..batch_size {
            for s in 0..seq_len {
                for d in 0..d_model {
                    let input_idx = b * seq_len * d_model + s * d_model + d;
                    let encoding_idx = s * d_model + d;
                    output_data[input_idx] = input_data[input_idx] + encoding_slice[encoding_idx];
                }
            }
        }

        let output = Tensor::from_vec(output_data, input_shape)?;

        // Note: Dropout would be applied here in training mode
        // For now, dropout is not implemented as it requires training mode tracking
        Ok(output)
    }

    fn parameters(&self) -> std::collections::HashMap<String, Parameter> {
        // Positional encoding is not learnable
        std::collections::HashMap::new()
    }

    fn train(&mut self) {}
    fn eval(&mut self) {}
}

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

    #[test]
    fn test_multi_head_attention() {
        let mha = MultiHeadAttention::new(512, 8, 0.1, true)
            .expect("Multi Head Attention should succeed");
        let input = randn(&[2, 10, 512]).expect("randn should succeed"); // batch_size=2, seq_len=10, d_model=512

        let output = mha.forward(&input).expect("forward pass should succeed");
        assert_eq!(output.shape().dims(), &[2, 10, 512]);
    }

    #[test]
    fn test_advanced_layer_norm() {
        let ln = AdvancedLayerNorm::new(vec![512], true, 1e-5)
            .expect("Advanced Layer Norm should succeed");
        let input = randn(&[2, 10, 512]).expect("randn should succeed");

        let output = ln.forward(&input).expect("forward pass should succeed");
        assert_eq!(output.shape().dims(), &[2, 10, 512]);
    }
}