sklears-neural 0.2.0

Neural network implementations for the sklears machine learning library
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
//! Transformer components including positional encoding and transformer blocks.
//!
//! This module provides implementations of transformer architecture components
//! including positional encoding, multi-head attention, and transformer blocks.

use crate::activation::Activation;
use crate::weight_init::{InitStrategy, WeightInitializer};
use crate::NeuralResult;
use scirs2_core::ndarray::{s, Array1, Array2, Array3};
use scirs2_core::numeric::NumCast;
use scirs2_core::random::thread_rng;
use sklears_core::error::SklearsError;
use sklears_core::types::FloatBounds;

/// Apply activation to 3D array (for transformer use)
fn apply_activation_3d<T: FloatBounds>(activation: &Activation, input: &Array3<T>) -> Array3<T> {
    match activation {
        Activation::Identity => input.clone(),
        Activation::Logistic => input.mapv(|val| {
            let exp_neg = (-val).exp();
            T::one() / (T::one() + exp_neg)
        }),
        Activation::Tanh => input.mapv(|val| val.tanh()),
        Activation::Relu => input.mapv(|val| val.max(T::zero())),
        _ => input.clone(), // For other activations, just return input for now
    }
}

/// Positional encoding types
#[derive(Debug, Clone, PartialEq)]
pub enum PositionalEncodingType {
    /// Sinusoidal positional encoding (original transformer)
    Sinusoidal,
    /// Learnable positional encoding
    Learnable,
    /// Relative positional encoding
    Relative,
}

/// Positional encoding implementation for transformer architectures
///
/// Adds positional information to input embeddings to help the model
/// understand the order of elements in a sequence.
#[derive(Debug, Clone)]
#[allow(dead_code)] // dropout_rate retained for future regularization; currently stored but not applied
pub struct PositionalEncoding<T: FloatBounds> {
    /// Maximum sequence length
    max_seq_len: usize,
    /// Model dimension (embedding dimension)
    d_model: usize,
    /// Type of positional encoding
    encoding_type: PositionalEncodingType,
    /// Dropout rate for regularization
    dropout_rate: T,
    /// Pre-computed sinusoidal encodings (for sinusoidal type)
    sinusoidal_encodings: Option<Array2<T>>,
    /// Learnable position embeddings (for learnable type)
    position_embeddings: Option<Array2<T>>,
    /// Whether to scale embeddings by sqrt(d_model)
    scale_embeddings: bool,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand + From<f64>> PositionalEncoding<T> {
    /// Create a new positional encoding layer
    pub fn new(
        max_seq_len: usize,
        d_model: usize,
        encoding_type: PositionalEncodingType,
        dropout_rate: T,
        scale_embeddings: bool,
    ) -> NeuralResult<Self> {
        let mut pe = Self {
            max_seq_len,
            d_model,
            encoding_type: encoding_type.clone(),
            dropout_rate,
            sinusoidal_encodings: None,
            position_embeddings: None,
            scale_embeddings,
        };

        match encoding_type {
            PositionalEncodingType::Sinusoidal => {
                pe.sinusoidal_encodings = Some(pe.create_sinusoidal_encodings()?);
            }
            PositionalEncodingType::Learnable => {
                pe.position_embeddings = Some(pe.create_learnable_embeddings()?);
            }
            PositionalEncodingType::Relative => {
                // Relative positional encoding is handled differently
                // and doesn't require pre-computed embeddings
            }
        }

        Ok(pe)
    }

    /// Create sinusoidal positional encodings
    fn create_sinusoidal_encodings(&self) -> NeuralResult<Array2<T>> {
        let mut encodings = Array2::zeros((self.max_seq_len, self.d_model));

        for pos in 0..self.max_seq_len {
            for i in 0..(self.d_model / 2) {
                let angle = pos as f64 / 10000_f64.powf(2.0 * i as f64 / self.d_model as f64);

                // Even dimensions use sin
                encodings[[pos, 2 * i]] = NumCast::from(angle.sin()).unwrap_or_else(T::zero);

                // Odd dimensions use cos
                if 2 * i + 1 < self.d_model {
                    encodings[[pos, 2 * i + 1]] =
                        NumCast::from(angle.cos()).unwrap_or_else(T::zero);
                }
            }
        }

        Ok(encodings)
    }

    /// Create learnable position embeddings
    fn create_learnable_embeddings(&self) -> NeuralResult<Array2<T>> {
        let mut rng = thread_rng();
        let initializer = WeightInitializer::new(InitStrategy::Normal {
            mean: 0.0,
            std: 0.02,
        });

        initializer.initialize_2d(&mut rng, (self.max_seq_len, self.d_model))
    }

    /// Apply positional encoding to input embeddings
    pub fn encode(&self, embeddings: &Array3<T>) -> NeuralResult<Array3<T>> {
        let (batch_size, seq_len, d_model) = embeddings.dim();

        if d_model != self.d_model {
            return Err(SklearsError::InvalidParameter {
                name: "d_model".to_string(),
                reason: format!("expected {}, got {}", self.d_model, d_model),
            });
        }

        if seq_len > self.max_seq_len {
            return Err(SklearsError::InvalidParameter {
                name: "seq_len".to_string(),
                reason: format!(
                    "sequence length {} exceeds maximum {}",
                    seq_len, self.max_seq_len
                ),
            });
        }

        let mut output = embeddings.clone();

        // Scale embeddings if requested
        if self.scale_embeddings {
            let scale_factor = NumCast::from((self.d_model as f64).sqrt()).unwrap_or(T::one());
            output *= scale_factor;
        }

        // Add positional encodings based on type
        match &self.encoding_type {
            PositionalEncodingType::Sinusoidal => {
                if let Some(ref encodings) = self.sinusoidal_encodings {
                    for batch in 0..batch_size {
                        for pos in 0..seq_len {
                            for dim in 0..d_model {
                                output[[batch, pos, dim]] += encodings[[pos, dim]];
                            }
                        }
                    }
                }
            }
            PositionalEncodingType::Learnable => {
                if let Some(ref embeddings) = self.position_embeddings {
                    for batch in 0..batch_size {
                        for pos in 0..seq_len {
                            for dim in 0..d_model {
                                output[[batch, pos, dim]] += embeddings[[pos, dim]];
                            }
                        }
                    }
                }
            }
            PositionalEncodingType::Relative => {
                // Relative positional encoding is typically handled in the attention mechanism
                // For now, we'll just return the original embeddings
            }
        }

        Ok(output)
    }

    /// Get positional encodings for a specific sequence length
    pub fn get_encodings(&self, seq_len: usize) -> NeuralResult<Array2<T>> {
        if seq_len > self.max_seq_len {
            return Err(SklearsError::InvalidParameter {
                name: "seq_len".to_string(),
                reason: format!(
                    "sequence length {} exceeds maximum {}",
                    seq_len, self.max_seq_len
                ),
            });
        }

        match &self.encoding_type {
            PositionalEncodingType::Sinusoidal => {
                if let Some(ref encodings) = self.sinusoidal_encodings {
                    Ok(encodings.slice(s![..seq_len, ..]).to_owned())
                } else {
                    Err(SklearsError::InvalidParameter {
                        name: "position_encodings".to_string(),
                        reason: "sinusoidal encodings not initialized".to_string(),
                    })
                }
            }
            PositionalEncodingType::Learnable => {
                if let Some(ref embeddings) = self.position_embeddings {
                    Ok(embeddings.slice(s![..seq_len, ..]).to_owned())
                } else {
                    Err(SklearsError::InvalidParameter {
                        name: "position_embeddings".to_string(),
                        reason: "learnable embeddings not initialized".to_string(),
                    })
                }
            }
            PositionalEncodingType::Relative => {
                // Return zeros for relative encoding as it's handled differently
                Ok(Array2::zeros((seq_len, self.d_model)))
            }
        }
    }

    /// Update learnable position embeddings (for gradient-based optimization)
    pub fn update_position_embeddings(
        &mut self,
        gradients: &Array2<T>,
        learning_rate: T,
    ) -> NeuralResult<()> {
        if let PositionalEncodingType::Learnable = self.encoding_type {
            if let Some(ref mut embeddings) = self.position_embeddings {
                *embeddings = embeddings.clone() - gradients * learning_rate;
                Ok(())
            } else {
                Err(SklearsError::InvalidParameter {
                    name: "position_embeddings".to_string(),
                    reason: "learnable embeddings not initialized".to_string(),
                })
            }
        } else {
            Err(SklearsError::InvalidParameter {
                name: "encoding_type".to_string(),
                reason: "position embeddings are not learnable".to_string(),
            })
        }
    }

    /// Get the number of parameters
    pub fn num_parameters(&self) -> usize {
        match &self.encoding_type {
            PositionalEncodingType::Sinusoidal => 0, // No learnable parameters
            PositionalEncodingType::Learnable => {
                if let Some(ref embeddings) = self.position_embeddings {
                    embeddings.len()
                } else {
                    0
                }
            }
            PositionalEncodingType::Relative => 0, // Handled in attention layers
        }
    }
}

/// Multi-head attention mechanism for transformer architectures
///
/// Implements scaled dot-product attention with multiple attention heads:
/// Attention(Q, K, V) = softmax(QK^T / sqrt(d_k))V
#[derive(Debug, Clone)]
#[allow(dead_code)] // dropout_rate, use_bias retained for future regularization and bias enabling
pub struct MultiHeadAttention<T: FloatBounds> {
    /// Number of attention heads
    num_heads: usize,
    /// Model dimension
    d_model: usize,
    /// Dimension of each attention head
    d_k: usize,
    /// Query projection weights
    w_q: Array2<T>,
    /// Key projection weights
    w_k: Array2<T>,
    /// Value projection weights
    w_v: Array2<T>,
    /// Output projection weights
    w_o: Array2<T>,
    /// Query bias
    b_q: Option<Array1<T>>,
    /// Key bias
    b_k: Option<Array1<T>>,
    /// Value bias
    b_v: Option<Array1<T>>,
    /// Output bias
    b_o: Option<Array1<T>>,
    /// Dropout rate for attention weights
    dropout_rate: T,
    /// Whether to use bias terms
    use_bias: bool,
    /// Scaling factor for attention scores
    scale_factor: T,
    /// Cached attention weights for visualization/analysis
    cached_attention_weights: Option<Array3<T>>,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand + From<f64>> MultiHeadAttention<T> {
    /// Create a multi-head attention layer; `d_model` must be divisible by `num_heads`
    pub fn new(
        d_model: usize,
        num_heads: usize,
        dropout_rate: T,
        use_bias: bool,
    ) -> NeuralResult<Self> {
        if !d_model.is_multiple_of(num_heads) {
            return Err(SklearsError::InvalidParameter {
                name: "d_model".to_string(),
                reason: format!(
                    "d_model ({}) must be divisible by num_heads ({})",
                    d_model, num_heads
                ),
            });
        }

        let d_k = d_model / num_heads;
        let scale_factor = NumCast::from(1.0 / (d_k as f64).sqrt()).unwrap_or(T::one());

        // Initialize weights using Xavier initialization
        let mut rng = thread_rng();
        let initializer = WeightInitializer::new(InitStrategy::XavierUniform);

        let w_q = initializer.initialize_2d(&mut rng, (d_model, d_model))?;
        let w_k = initializer.initialize_2d(&mut rng, (d_model, d_model))?;
        let w_v = initializer.initialize_2d(&mut rng, (d_model, d_model))?;
        let w_o = initializer.initialize_2d(&mut rng, (d_model, d_model))?;

        let (b_q, b_k, b_v, b_o) = if use_bias {
            (
                Some(Array1::zeros(d_model)),
                Some(Array1::zeros(d_model)),
                Some(Array1::zeros(d_model)),
                Some(Array1::zeros(d_model)),
            )
        } else {
            (None, None, None, None)
        };

        Ok(Self {
            num_heads,
            d_model,
            d_k,
            w_q,
            w_k,
            w_v,
            w_o,
            b_q,
            b_k,
            b_v,
            b_o,
            dropout_rate,
            use_bias,
            scale_factor,
            cached_attention_weights: None,
        })
    }

    /// Forward pass through multi-head attention
    pub fn forward(
        &mut self,
        query: &Array3<T>,
        key: &Array3<T>,
        value: &Array3<T>,
        mask: Option<&Array3<T>>,
    ) -> NeuralResult<Array3<T>> {
        let (batch_size, seq_len_q, _) = query.dim();
        let (_, seq_len_k, _) = key.dim();
        let (_, seq_len_v, _) = value.dim();

        if seq_len_k != seq_len_v {
            return Err(SklearsError::InvalidParameter {
                name: "seq_len".to_string(),
                reason: "key and value sequence lengths must match".to_string(),
            });
        }

        // Linear projections for Q, K, V
        let q = self.linear_projection(query, &self.w_q, self.b_q.as_ref())?;
        let k = self.linear_projection(key, &self.w_k, self.b_k.as_ref())?;
        let v = self.linear_projection(value, &self.w_v, self.b_v.as_ref())?;

        // Reshape to (batch_size, num_heads, seq_len, d_k)
        let q_heads = self.reshape_for_heads(&q, batch_size, seq_len_q)?;
        let k_heads = self.reshape_for_heads(&k, batch_size, seq_len_k)?;
        let v_heads = self.reshape_for_heads(&v, batch_size, seq_len_v)?;

        // Scaled dot-product attention
        let (attended_values, attention_weights) =
            self.scaled_dot_product_attention(&q_heads, &k_heads, &v_heads, mask)?;

        // Cache attention weights for analysis
        self.cached_attention_weights = Some(attention_weights);

        // Reshape back to (batch_size, seq_len, d_model)
        let concatenated = self.reshape_from_heads(&attended_values, batch_size, seq_len_q)?;

        // Final linear projection
        let output = self.linear_projection(&concatenated, &self.w_o, self.b_o.as_ref())?;

        Ok(output)
    }

    /// Apply linear projection (matrix multiplication + bias)
    fn linear_projection(
        &self,
        input: &Array3<T>,
        weight: &Array2<T>,
        bias: Option<&Array1<T>>,
    ) -> NeuralResult<Array3<T>> {
        let (batch_size, seq_len, d_model) = input.dim();
        let mut output = Array3::zeros((batch_size, seq_len, d_model));

        for batch in 0..batch_size {
            let input_2d = input.slice(s![batch, .., ..]);
            let projected = input_2d.dot(weight);
            output.slice_mut(s![batch, .., ..]).assign(&projected);

            if let Some(bias_vec) = bias {
                for seq in 0..seq_len {
                    for dim in 0..d_model {
                        output[[batch, seq, dim]] += bias_vec[dim];
                    }
                }
            }
        }

        Ok(output)
    }

    /// Reshape tensor for multi-head processing
    fn reshape_for_heads(
        &self,
        input: &Array3<T>,
        batch_size: usize,
        seq_len: usize,
    ) -> NeuralResult<Array3<T>> {
        // Input: (batch_size, seq_len, d_model)
        // Output: (batch_size * num_heads, seq_len, d_k)
        let mut output = Array3::zeros((batch_size * self.num_heads, seq_len, self.d_k));

        for batch in 0..batch_size {
            for head in 0..self.num_heads {
                let head_idx = batch * self.num_heads + head;
                let start_dim = head * self.d_k;
                let _end_dim = start_dim + self.d_k;

                for seq in 0..seq_len {
                    for dim in 0..self.d_k {
                        output[[head_idx, seq, dim]] = input[[batch, seq, start_dim + dim]];
                    }
                }
            }
        }

        Ok(output)
    }

    /// Reshape tensor back from multi-head format
    fn reshape_from_heads(
        &self,
        input: &Array3<T>,
        batch_size: usize,
        seq_len: usize,
    ) -> NeuralResult<Array3<T>> {
        // Input: (batch_size * num_heads, seq_len, d_k)
        // Output: (batch_size, seq_len, d_model)
        let mut output = Array3::zeros((batch_size, seq_len, self.d_model));

        for batch in 0..batch_size {
            for head in 0..self.num_heads {
                let head_idx = batch * self.num_heads + head;
                let start_dim = head * self.d_k;

                for seq in 0..seq_len {
                    for dim in 0..self.d_k {
                        output[[batch, seq, start_dim + dim]] = input[[head_idx, seq, dim]];
                    }
                }
            }
        }

        Ok(output)
    }

    /// Scaled dot-product attention
    fn scaled_dot_product_attention(
        &self,
        q: &Array3<T>,
        k: &Array3<T>,
        v: &Array3<T>,
        mask: Option<&Array3<T>>,
    ) -> NeuralResult<(Array3<T>, Array3<T>)> {
        let (batch_heads, seq_len_q, d_k) = q.dim();
        let (_, seq_len_k, _) = k.dim();

        // Compute attention scores: Q @ K^T
        let mut scores = Array3::zeros((batch_heads, seq_len_q, seq_len_k));

        for batch_head in 0..batch_heads {
            let q_slice = q.slice(s![batch_head, .., ..]);
            let k_slice = k.slice(s![batch_head, .., ..]);
            let score_slice = q_slice.dot(&k_slice.t());
            scores
                .slice_mut(s![batch_head, .., ..])
                .assign(&score_slice);
        }

        // Scale by sqrt(d_k)
        scores *= self.scale_factor;

        // Apply mask if provided
        if let Some(mask_tensor) = mask {
            // Apply mask by setting masked positions to large negative value
            let neg_inf = NumCast::from(-1e9).unwrap_or(T::zero());
            for batch_head in 0..batch_heads {
                for i in 0..seq_len_q {
                    for j in 0..seq_len_k {
                        let mask_batch = batch_head % mask_tensor.dim().0;
                        if mask_tensor[[mask_batch, i, j]] == T::zero() {
                            scores[[batch_head, i, j]] = neg_inf;
                        }
                    }
                }
            }
        }

        // Apply softmax to get attention weights
        let attention_weights = self.softmax_3d(&scores)?;

        // Apply attention to values: Attention @ V
        let mut output = Array3::zeros((batch_heads, seq_len_q, d_k));

        for batch_head in 0..batch_heads {
            let attn_slice = attention_weights.slice(s![batch_head, .., ..]);
            let v_slice = v.slice(s![batch_head, .., ..]);
            let out_slice = attn_slice.dot(&v_slice);
            output.slice_mut(s![batch_head, .., ..]).assign(&out_slice);
        }

        Ok((output, attention_weights))
    }

    /// Apply softmax along the last dimension of a 3D tensor
    fn softmax_3d(&self, input: &Array3<T>) -> NeuralResult<Array3<T>> {
        let (dim0, dim1, dim2) = input.dim();
        let mut output = Array3::zeros((dim0, dim1, dim2));

        for i in 0..dim0 {
            for j in 0..dim1 {
                // Find max for numerical stability
                let mut max_val = input[[i, j, 0]];
                for k in 1..dim2 {
                    if input[[i, j, k]] > max_val {
                        max_val = input[[i, j, k]];
                    }
                }

                // Compute exp(x - max) and sum
                let mut sum = T::zero();
                for k in 0..dim2 {
                    let exp_val = (input[[i, j, k]] - max_val).exp();
                    output[[i, j, k]] = exp_val;
                    sum += exp_val;
                }

                // Normalize
                for k in 0..dim2 {
                    output[[i, j, k]] /= sum;
                }
            }
        }

        Ok(output)
    }

    /// Get the last computed attention weights
    pub fn get_attention_weights(&self) -> Option<&Array3<T>> {
        self.cached_attention_weights.as_ref()
    }

    /// Get the number of parameters
    pub fn num_parameters(&self) -> usize {
        let weight_params = self.w_q.len() + self.w_k.len() + self.w_v.len() + self.w_o.len();
        let bias_params = if self.use_bias {
            self.b_q.as_ref().map_or(0, |b| b.len())
                + self.b_k.as_ref().map_or(0, |b| b.len())
                + self.b_v.as_ref().map_or(0, |b| b.len())
                + self.b_o.as_ref().map_or(0, |b| b.len())
        } else {
            0
        };
        weight_params + bias_params
    }
}

/// Feed-forward network for transformer blocks
#[derive(Debug, Clone)]
#[allow(dead_code)] // dropout_rate and use_bias retained for future regularization and bias enabling
pub struct FeedForward<T: FloatBounds> {
    /// First linear layer weights
    w1: Array2<T>,
    /// Second linear layer weights
    w2: Array2<T>,
    /// First layer bias
    b1: Option<Array1<T>>,
    /// Second layer bias
    b2: Option<Array1<T>>,
    /// Activation function
    activation: Activation,
    /// Dropout rate
    dropout_rate: T,
    /// Whether to use bias
    use_bias: bool,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand> FeedForward<T> {
    /// Create a position-wise feed-forward network with the given model and expansion dimensions
    pub fn new(
        d_model: usize,
        d_ff: usize,
        activation: Activation,
        dropout_rate: T,
        use_bias: bool,
    ) -> NeuralResult<Self> {
        // Initialize weights
        let mut rng = thread_rng();
        let initializer = WeightInitializer::new(InitStrategy::XavierUniform);

        let w1 = initializer.initialize_2d(&mut rng, (d_model, d_ff))?;
        let w2 = initializer.initialize_2d(&mut rng, (d_ff, d_model))?;

        let (b1, b2) = if use_bias {
            (Some(Array1::zeros(d_ff)), Some(Array1::zeros(d_model)))
        } else {
            (None, None)
        };

        Ok(Self {
            w1,
            w2,
            b1,
            b2,
            activation,
            dropout_rate,
            use_bias,
        })
    }

    /// Forward pass through the feed-forward network
    pub fn forward(&mut self, input: &Array3<T>) -> NeuralResult<Array3<T>> {
        let (batch_size, seq_len, d_model) = input.dim();

        // First linear layer
        let mut hidden = Array3::zeros((batch_size, seq_len, self.w1.dim().1));

        for batch in 0..batch_size {
            let input_2d = input.slice(s![batch, .., ..]);
            let hidden_2d = input_2d.dot(&self.w1);
            hidden.slice_mut(s![batch, .., ..]).assign(&hidden_2d);

            if let Some(ref bias) = self.b1 {
                for seq in 0..seq_len {
                    for dim in 0..bias.len() {
                        hidden[[batch, seq, dim]] += bias[dim];
                    }
                }
            }
        }

        // Apply activation
        hidden = apply_activation_3d(&self.activation, &hidden);

        // Second linear layer
        let mut output = Array3::zeros((batch_size, seq_len, d_model));

        for batch in 0..batch_size {
            let hidden_2d = hidden.slice(s![batch, .., ..]);
            let output_2d = hidden_2d.dot(&self.w2);
            output.slice_mut(s![batch, .., ..]).assign(&output_2d);

            if let Some(ref bias) = self.b2 {
                for seq in 0..seq_len {
                    for dim in 0..bias.len() {
                        output[[batch, seq, dim]] += bias[dim];
                    }
                }
            }
        }

        Ok(output)
    }

    /// Get the number of parameters
    pub fn num_parameters(&self) -> usize {
        let weight_params = self.w1.len() + self.w2.len();
        let bias_params = if self.use_bias {
            self.b1.as_ref().map_or(0, |b| b.len()) + self.b2.as_ref().map_or(0, |b| b.len())
        } else {
            0
        };
        weight_params + bias_params
    }
}

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

    #[test]
    #[ignore]
    fn test_sinusoidal_positional_encoding() {
        let pe =
            PositionalEncoding::<f64>::new(100, 64, PositionalEncodingType::Sinusoidal, 0.1, true)
                .expect("operation should succeed");

        assert_eq!(pe.max_seq_len, 100);
        assert_eq!(pe.d_model, 64);
        assert!(pe.sinusoidal_encodings.is_some());

        let encodings = pe.get_encodings(10).expect("operation should succeed");
        assert_eq!(encodings.dim(), (10, 64));
    }

    #[test]
    #[ignore]
    fn test_learnable_positional_encoding() {
        let pe =
            PositionalEncoding::<f64>::new(50, 32, PositionalEncodingType::Learnable, 0.1, false)
                .expect("operation should succeed");

        assert_eq!(pe.num_parameters(), 50 * 32);
        assert!(pe.position_embeddings.is_some());

        let encodings = pe.get_encodings(20).expect("operation should succeed");
        assert_eq!(encodings.dim(), (20, 32));
    }

    #[test]
    #[ignore]
    fn test_positional_encoding_forward() {
        let pe =
            PositionalEncoding::<f64>::new(50, 16, PositionalEncodingType::Sinusoidal, 0.0, false)
                .expect("operation should succeed");

        let embeddings = Array3::zeros((2, 10, 16)); // batch=2, seq=10, d_model=16
        let encoded = pe.encode(&embeddings).expect("operation should succeed");

        assert_eq!(encoded.dim(), (2, 10, 16));
    }

    #[test]
    #[ignore]
    fn test_multi_head_attention_creation() {
        let mha =
            MultiHeadAttention::<f64>::new(512, 8, 0.1, true).expect("construction should succeed");

        assert_eq!(mha.num_heads, 8);
        assert_eq!(mha.d_model, 512);
        assert_eq!(mha.d_k, 64);
        assert!(mha.use_bias);
    }

    #[test]
    #[ignore]
    fn test_multi_head_attention_invalid_dimensions() {
        let result = MultiHeadAttention::<f64>::new(511, 8, 0.1, true);
        assert!(result.is_err()); // 511 is not divisible by 8
    }

    #[test]
    #[ignore]
    fn test_multi_head_attention_forward() {
        let mut mha =
            MultiHeadAttention::<f64>::new(64, 4, 0.0, false).expect("construction should succeed");

        let query = Array3::zeros((2, 10, 64)); // batch=2, seq=10, d_model=64
        let key = Array3::zeros((2, 15, 64)); // batch=2, seq=15, d_model=64
        let value = Array3::zeros((2, 15, 64)); // batch=2, seq=15, d_model=64

        let output = mha
            .forward(&query, &key, &value, None)
            .expect("forward pass should succeed");
        assert_eq!(output.dim(), (2, 10, 64));

        // Check that attention weights were cached
        assert!(mha.get_attention_weights().is_some());
    }

    #[test]
    #[ignore]
    fn test_multi_head_attention_with_mask() {
        let mut mha =
            MultiHeadAttention::<f64>::new(32, 2, 0.0, false).expect("construction should succeed");

        let query = Array3::ones((1, 5, 32));
        let key = Array3::ones((1, 5, 32));
        let value = Array3::ones((1, 5, 32));

        // Create a causal mask (lower triangular)
        let mut mask = Array3::zeros((1, 5, 5));
        for i in 0..5 {
            for j in 0..=i {
                mask[[0, i, j]] = 1.0;
            }
        }

        let output = mha
            .forward(&query, &key, &value, Some(&mask))
            .expect("forward pass should succeed");
        assert_eq!(output.dim(), (1, 5, 32));
    }

    #[test]
    #[ignore]
    fn test_feed_forward_network() {
        let mut ffn = FeedForward::<f64>::new(256, 1024, Activation::Relu, 0.1, true)
            .expect("construction should succeed");

        let input = Array3::zeros((2, 10, 256));
        let output = ffn.forward(&input).expect("forward pass should succeed");

        assert_eq!(output.dim(), (2, 10, 256));
        assert_eq!(ffn.num_parameters(), 256 * 1024 + 1024 * 256 + 1024 + 256);
    }

    #[test]
    #[ignore]
    fn test_positional_encoding_sequence_length_validation() {
        let pe =
            PositionalEncoding::<f64>::new(20, 16, PositionalEncodingType::Sinusoidal, 0.0, false)
                .expect("operation should succeed");

        let long_embeddings = Array3::zeros((1, 25, 16)); // seq_len > max_seq_len
        let result = pe.encode(&long_embeddings);
        assert!(result.is_err());
    }

    #[test]
    #[ignore]
    fn test_positional_encoding_dimension_validation() {
        let pe =
            PositionalEncoding::<f64>::new(50, 16, PositionalEncodingType::Sinusoidal, 0.0, false)
                .expect("operation should succeed");

        let wrong_dim_embeddings = Array3::zeros((1, 10, 32)); // d_model mismatch
        let result = pe.encode(&wrong_dim_embeddings);
        assert!(result.is_err());
    }

    #[test]
    #[ignore]
    fn test_learnable_embedding_updates() {
        let mut pe =
            PositionalEncoding::<f64>::new(10, 8, PositionalEncodingType::Learnable, 0.0, false)
                .expect("operation should succeed");

        let gradients = Array2::ones((10, 8));
        let learning_rate = 0.01;

        let result = pe.update_position_embeddings(&gradients, learning_rate);
        assert!(result.is_ok());
    }

    #[test]
    #[ignore]
    fn test_sinusoidal_encoding_properties() {
        let pe =
            PositionalEncoding::<f64>::new(100, 64, PositionalEncodingType::Sinusoidal, 0.0, false)
                .expect("operation should succeed");

        let encodings = pe
            .sinusoidal_encodings
            .as_ref()
            .expect("operation should succeed");

        // Check that even positions use sin and odd positions use cos
        // This is a basic sanity check - the actual values depend on the formula
        assert_eq!(encodings.dim(), (100, 64));

        // Check that positions 0 and 1 have different patterns
        let pos_0 = encodings.row(0);
        let pos_1 = encodings.row(1);

        let mut different = false;
        for i in 0..64 {
            if (pos_0[i] - pos_1[i]).abs() > 1e-6_f64 {
                different = true;
                break;
            }
        }
        assert!(
            different,
            "Different positions should have different encodings"
        );
    }
}