sklears-neural 0.1.1

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
//! Encoder-Decoder Transformer Architectures
//!
//! This module provides complete transformer architectures including encoder-decoder
//! models for sequence-to-sequence tasks, encoder-only models for classification,
//! and decoder-only models for language generation.

use crate::layers::attention::MultiHeadAttention;
use crate::layers::transformer::{PositionalEncoding, PositionalEncodingType};
use crate::weight_init::{InitStrategy, WeightInitializer};
use crate::NeuralResult;
use scirs2_core::ndarray::{s, Array1, Array2, Array3};
use scirs2_core::random::ChaCha8Rng;
use scirs2_core::random::SeedableRng;
use sklears_core::types::FloatBounds;

/// Configuration for transformer models
#[derive(Debug, Clone)]
pub struct TransformerConfig<T: FloatBounds> {
    /// Model dimension (d_model)
    pub d_model: usize,
    /// Number of attention heads
    pub num_heads: usize,
    /// Dimension of feed-forward network
    pub d_ff: usize,
    /// Number of encoder layers
    pub num_encoder_layers: usize,
    /// Number of decoder layers
    pub num_decoder_layers: usize,
    /// Maximum sequence length
    pub max_seq_len: usize,
    /// Vocabulary size
    pub vocab_size: usize,
    /// Dropout rate
    pub dropout_rate: T,
    /// Label smoothing for training
    pub label_smoothing: T,
    /// Whether to use pre-normalization
    pub pre_norm: bool,
    /// Activation function for FFN
    pub activation: String,
    /// Whether to share embeddings between encoder and decoder
    pub share_embeddings: bool,
}

impl<T: FloatBounds> Default for TransformerConfig<T> {
    fn default() -> Self {
        Self {
            d_model: 512,
            num_heads: 8,
            d_ff: 2048,
            num_encoder_layers: 6,
            num_decoder_layers: 6,
            max_seq_len: 512,
            vocab_size: 30000,
            dropout_rate: T::from(0.1).unwrap_or_else(|| T::zero()),
            label_smoothing: T::from(0.1).unwrap_or_else(|| T::zero()),
            pre_norm: false,
            activation: "relu".to_string(),
            share_embeddings: false,
        }
    }
}

/// Feed-forward network used in transformer blocks
#[derive(Debug, Clone)]
pub struct FeedForwardNetwork<T: FloatBounds> {
    /// First linear transformation
    linear1: Array2<T>,
    /// First bias
    bias1: Array1<T>,
    /// Second linear transformation
    linear2: Array2<T>,
    /// Second bias
    bias2: Array1<T>,
    /// Activation function
    activation: String,
    /// Dropout rate
    dropout_rate: T,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand> FeedForwardNetwork<T> {
    /// Create a new feed-forward network
    pub fn new(
        d_model: usize,
        d_ff: usize,
        dropout_rate: T,
        activation: String,
    ) -> NeuralResult<Self> {
        let mut rng = ChaCha8Rng::seed_from_u64(42);
        let initializer = WeightInitializer::new(InitStrategy::XavierUniform);

        let linear1 = initializer.initialize_2d(&mut rng, (d_model, d_ff))?;
        let bias1 = Array1::zeros(d_ff);
        let linear2 = initializer.initialize_2d(&mut rng, (d_ff, d_model))?;
        let bias2 = Array1::zeros(d_model);

        Ok(Self {
            linear1,
            bias1,
            linear2,
            bias2,
            activation,
            dropout_rate,
        })
    }

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

        for b in 0..batch_size {
            for s in 0..seq_len {
                let x = input.slice(s![b, s, ..]);

                // First linear transformation
                let hidden = x.dot(&self.linear1) + &self.bias1;

                // Apply activation
                let activated = match self.activation.as_str() {
                    "relu" => hidden.mapv(|x| x.max(T::zero())),
                    "gelu" => hidden.mapv(|x| {
                        let x_f64 = x.to_f64().unwrap_or(0.0);
                        let gelu_val = 0.5 * x_f64 * (1.0 + (x_f64 * 0.7978845608028654).tanh());
                        T::from(gelu_val).unwrap_or(T::zero())
                    }),
                    _ => hidden, // Default to identity
                };

                // Apply dropout if training
                let dropout_applied = if training && self.dropout_rate > T::zero() {
                    // Simple dropout implementation
                    let mut rng = scirs2_core::random::thread_rng();
                    activated.mapv(|x| {
                        if rng.random::<f64>() < self.dropout_rate.to_f64().unwrap_or(0.0) {
                            T::zero()
                        } else {
                            x / (T::one() - self.dropout_rate)
                        }
                    })
                } else {
                    activated
                };

                // Second linear transformation
                let final_output = dropout_applied.dot(&self.linear2) + &self.bias2;
                output.slice_mut(s![b, s, ..]).assign(&final_output);
            }
        }

        Ok(output)
    }
}

/// Layer normalization for transformers
#[derive(Debug, Clone)]
pub struct LayerNorm<T: FloatBounds> {
    /// Normalization weights
    weight: Array1<T>,
    /// Normalization bias
    bias: Array1<T>,
    /// Epsilon for numerical stability
    eps: T,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand> LayerNorm<T> {
    /// Create a new layer normalization layer
    pub fn new(d_model: usize) -> Self {
        Self {
            weight: Array1::ones(d_model),
            bias: Array1::zeros(d_model),
            eps: T::from(1e-5).unwrap_or_else(|| T::zero()),
        }
    }

    /// Apply layer normalization
    pub fn forward(&self, input: &Array3<T>) -> Array3<T> {
        let (batch_size, seq_len, d_model) = input.dim();
        let mut output = Array3::zeros((batch_size, seq_len, d_model));

        for b in 0..batch_size {
            for s in 0..seq_len {
                let x = input.slice(s![b, s, ..]);
                let mean = x.mean().unwrap_or(T::zero());
                let variance = x
                    .mapv(|v| (v - mean) * (v - mean))
                    .mean()
                    .unwrap_or(T::zero());
                let std = (variance + self.eps).sqrt();

                let normalized = x.mapv(|v| (v - mean) / std);
                let scaled = &normalized * &self.weight + &self.bias;

                output.slice_mut(s![b, s, ..]).assign(&scaled);
            }
        }

        output
    }
}

/// Transformer encoder layer
#[derive(Debug, Clone)]
#[allow(dead_code)] // dropout_rate retained for future dropout application during forward pass
pub struct TransformerEncoderLayer<T: FloatBounds> {
    /// Multi-head self-attention
    self_attention: MultiHeadAttention<T>,
    /// Feed-forward network
    ffn: FeedForwardNetwork<T>,
    /// Layer normalization for attention
    norm1: LayerNorm<T>,
    /// Layer normalization for FFN
    norm2: LayerNorm<T>,
    /// Dropout rate
    dropout_rate: T,
    /// Whether to use pre-normalization
    pre_norm: bool,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand> TransformerEncoderLayer<T> {
    /// Create a new encoder layer from the given transformer configuration
    pub fn new(config: &TransformerConfig<T>) -> NeuralResult<Self> {
        let self_attention =
            MultiHeadAttention::new(config.num_heads, config.d_model, Some(config.dropout_rate))?;

        let ffn = FeedForwardNetwork::new(
            config.d_model,
            config.d_ff,
            config.dropout_rate,
            config.activation.clone(),
        )?;

        let norm1 = LayerNorm::new(config.d_model);
        let norm2 = LayerNorm::new(config.d_model);

        Ok(Self {
            self_attention,
            ffn,
            norm1,
            norm2,
            dropout_rate: config.dropout_rate,
            pre_norm: config.pre_norm,
        })
    }

    /// Forward pass through encoder layer
    pub fn forward(
        &mut self,
        input: &Array3<T>,
        mask: Option<&Array2<bool>>,
        training: bool,
    ) -> NeuralResult<Array3<T>> {
        if self.pre_norm {
            // Pre-normalization: LayerNorm -> Attention -> Residual
            let norm_input = self.norm1.forward(input);
            let attn_output =
                self.self_attention
                    .apply(&norm_input, &norm_input, &norm_input, mask, training)?;
            let residual1 = input + &attn_output;

            let norm_residual1 = self.norm2.forward(&residual1);
            let ffn_output = self.ffn.forward(&norm_residual1, training)?;
            let residual2 = &residual1 + &ffn_output;

            Ok(residual2)
        } else {
            // Post-normalization: Attention -> Residual -> LayerNorm
            let attn_output = self
                .self_attention
                .apply(input, input, input, mask, training)?;
            let residual1 = input + &attn_output;
            let norm1_output = self.norm1.forward(&residual1);

            let ffn_output = self.ffn.forward(&norm1_output, training)?;
            let residual2 = &norm1_output + &ffn_output;
            let norm2_output = self.norm2.forward(&residual2);

            Ok(norm2_output)
        }
    }
}

/// Transformer decoder layer
#[derive(Debug, Clone)]
#[allow(dead_code)] // dropout_rate retained for future dropout application during forward pass
pub struct TransformerDecoderLayer<T: FloatBounds> {
    /// Masked self-attention
    self_attention: MultiHeadAttention<T>,
    /// Cross-attention with encoder
    cross_attention: MultiHeadAttention<T>,
    /// Feed-forward network
    ffn: FeedForwardNetwork<T>,
    /// Layer normalization layers
    norm1: LayerNorm<T>,
    norm2: LayerNorm<T>,
    norm3: LayerNorm<T>,
    /// Dropout rate
    dropout_rate: T,
    /// Whether to use pre-normalization
    pre_norm: bool,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand> TransformerDecoderLayer<T> {
    /// Create a new decoder layer from the given transformer configuration
    pub fn new(config: &TransformerConfig<T>) -> NeuralResult<Self> {
        let self_attention =
            MultiHeadAttention::new(config.num_heads, config.d_model, Some(config.dropout_rate))?;

        let cross_attention =
            MultiHeadAttention::new(config.num_heads, config.d_model, Some(config.dropout_rate))?;

        let ffn = FeedForwardNetwork::new(
            config.d_model,
            config.d_ff,
            config.dropout_rate,
            config.activation.clone(),
        )?;

        let norm1 = LayerNorm::new(config.d_model);
        let norm2 = LayerNorm::new(config.d_model);
        let norm3 = LayerNorm::new(config.d_model);

        Ok(Self {
            self_attention,
            cross_attention,
            ffn,
            norm1,
            norm2,
            norm3,
            dropout_rate: config.dropout_rate,
            pre_norm: config.pre_norm,
        })
    }

    /// Forward pass through decoder layer
    pub fn forward(
        &mut self,
        input: &Array3<T>,
        encoder_output: &Array3<T>,
        self_mask: Option<&Array2<bool>>,
        cross_mask: Option<&Array2<bool>>,
        training: bool,
    ) -> NeuralResult<Array3<T>> {
        if self.pre_norm {
            // Pre-normalization
            let norm_input = self.norm1.forward(input);
            let self_attn_output = self.self_attention.apply(
                &norm_input,
                &norm_input,
                &norm_input,
                self_mask,
                training,
            )?;
            let residual1 = input + &self_attn_output;

            let norm_residual1 = self.norm2.forward(&residual1);
            let cross_attn_output = self.cross_attention.apply(
                &norm_residual1,
                encoder_output,
                encoder_output,
                cross_mask,
                training,
            )?;
            let residual2 = &residual1 + &cross_attn_output;

            let norm_residual2 = self.norm3.forward(&residual2);
            let ffn_output = self.ffn.forward(&norm_residual2, training)?;
            let residual3 = &residual2 + &ffn_output;

            Ok(residual3)
        } else {
            // Post-normalization
            let self_attn_output = self
                .self_attention
                .apply(input, input, input, self_mask, training)?;
            let residual1 = input + &self_attn_output;
            let norm1_output = self.norm1.forward(&residual1);

            let cross_attn_output = self.cross_attention.apply(
                &norm1_output,
                encoder_output,
                encoder_output,
                cross_mask,
                training,
            )?;
            let residual2 = &norm1_output + &cross_attn_output;
            let norm2_output = self.norm2.forward(&residual2);

            let ffn_output = self.ffn.forward(&norm2_output, training)?;
            let residual3 = &norm2_output + &ffn_output;
            let norm3_output = self.norm3.forward(&residual3);

            Ok(norm3_output)
        }
    }
}

/// Complete Transformer encoder
#[derive(Debug, Clone)]
pub struct TransformerEncoder<T: FloatBounds> {
    /// Stack of encoder layers
    layers: Vec<TransformerEncoderLayer<T>>,
    /// Input embeddings
    embeddings: Array2<T>,
    /// Positional encoding
    positional_encoding: PositionalEncoding<T>,
    /// Final layer normalization
    final_norm: Option<LayerNorm<T>>,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand + From<f64>> TransformerEncoder<T> {
    /// Create a new transformer encoder
    pub fn new(config: &TransformerConfig<T>) -> NeuralResult<Self> {
        let mut rng = ChaCha8Rng::seed_from_u64(42);
        let initializer = WeightInitializer::new(InitStrategy::XavierUniform);

        // Create encoder layers
        let mut layers = Vec::new();
        for _ in 0..config.num_encoder_layers {
            layers.push(TransformerEncoderLayer::new(config)?);
        }

        // Initialize embeddings
        let embeddings =
            initializer.initialize_2d(&mut rng, (config.vocab_size, config.d_model))?;

        // Create positional encoding
        let positional_encoding = PositionalEncoding::new(
            config.max_seq_len,
            config.d_model,
            PositionalEncodingType::Sinusoidal,
            config.dropout_rate,
            true,
        )?;

        // Final layer normalization for pre-norm architectures
        let final_norm = if config.pre_norm {
            Some(LayerNorm::new(config.d_model))
        } else {
            None
        };

        Ok(Self {
            layers,
            embeddings,
            positional_encoding,
            final_norm,
        })
    }

    /// Forward pass through encoder
    pub fn forward(
        &mut self,
        input_ids: &Array2<usize>,
        mask: Option<&Array2<bool>>,
        training: bool,
    ) -> NeuralResult<Array3<T>> {
        let (batch_size, seq_len) = input_ids.dim();
        let d_model = self.embeddings.ncols();

        // Embedding lookup
        let mut embedded = Array3::zeros((batch_size, seq_len, d_model));
        for b in 0..batch_size {
            for s in 0..seq_len {
                let token_id = input_ids[[b, s]];
                if token_id < self.embeddings.nrows() {
                    embedded
                        .slice_mut(s![b, s, ..])
                        .assign(&self.embeddings.row(token_id));
                }
            }
        }

        // Add positional encoding
        let mut x = self.positional_encoding.encode(&embedded)?;

        // Pass through encoder layers
        for layer in &mut self.layers {
            x = layer.forward(&x, mask, training)?;
        }

        // Apply final normalization if using pre-norm
        if let Some(ref final_norm) = self.final_norm {
            x = final_norm.forward(&x);
        }

        Ok(x)
    }
}

/// Complete Transformer decoder
#[derive(Debug, Clone)]
pub struct TransformerDecoder<T: FloatBounds> {
    /// Stack of decoder layers
    layers: Vec<TransformerDecoderLayer<T>>,
    /// Output embeddings
    embeddings: Array2<T>,
    /// Positional encoding
    positional_encoding: PositionalEncoding<T>,
    /// Final layer normalization
    final_norm: Option<LayerNorm<T>>,
    /// Output projection to vocabulary
    output_projection: Array2<T>,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand + From<f64>> TransformerDecoder<T> {
    /// Create a new transformer decoder
    pub fn new(config: &TransformerConfig<T>) -> NeuralResult<Self> {
        let mut rng = ChaCha8Rng::seed_from_u64(42);
        let initializer = WeightInitializer::new(InitStrategy::XavierUniform);

        // Create decoder layers
        let mut layers = Vec::new();
        for _ in 0..config.num_decoder_layers {
            layers.push(TransformerDecoderLayer::new(config)?);
        }

        // Initialize embeddings
        let embeddings =
            initializer.initialize_2d(&mut rng, (config.vocab_size, config.d_model))?;

        // Create positional encoding
        let positional_encoding = PositionalEncoding::new(
            config.max_seq_len,
            config.d_model,
            PositionalEncodingType::Sinusoidal,
            config.dropout_rate,
            true,
        )?;

        // Final layer normalization
        let final_norm = if config.pre_norm {
            Some(LayerNorm::new(config.d_model))
        } else {
            None
        };

        // Output projection
        let output_projection =
            initializer.initialize_2d(&mut rng, (config.d_model, config.vocab_size))?;

        Ok(Self {
            layers,
            embeddings,
            positional_encoding,
            final_norm,
            output_projection,
        })
    }

    /// Forward pass through decoder
    pub fn forward(
        &mut self,
        input_ids: &Array2<usize>,
        encoder_output: &Array3<T>,
        self_mask: Option<&Array2<bool>>,
        cross_mask: Option<&Array2<bool>>,
        training: bool,
    ) -> NeuralResult<Array3<T>> {
        let (batch_size, seq_len) = input_ids.dim();
        let d_model = self.embeddings.ncols();

        // Embedding lookup
        let mut embedded = Array3::zeros((batch_size, seq_len, d_model));
        for b in 0..batch_size {
            for s in 0..seq_len {
                let token_id = input_ids[[b, s]];
                if token_id < self.embeddings.nrows() {
                    embedded
                        .slice_mut(s![b, s, ..])
                        .assign(&self.embeddings.row(token_id));
                }
            }
        }

        // Add positional encoding
        let mut x = self.positional_encoding.encode(&embedded)?;

        // Pass through decoder layers
        for layer in &mut self.layers {
            x = layer.forward(&x, encoder_output, self_mask, cross_mask, training)?;
        }

        // Apply final normalization if using pre-norm
        if let Some(ref final_norm) = self.final_norm {
            x = final_norm.forward(&x);
        }

        // Project to vocabulary
        let (batch_size, seq_len, _d_model) = x.dim();
        let mut logits = Array3::zeros((batch_size, seq_len, self.output_projection.ncols()));

        for b in 0..batch_size {
            for s in 0..seq_len {
                let hidden = x.slice(s![b, s, ..]);
                let output = hidden.dot(&self.output_projection);
                logits.slice_mut(s![b, s, ..]).assign(&output);
            }
        }

        Ok(logits)
    }
}

/// Complete Encoder-Decoder Transformer
#[derive(Debug, Clone)]
pub struct EncoderDecoderTransformer<T: FloatBounds> {
    /// Transformer encoder
    encoder: TransformerEncoder<T>,
    /// Transformer decoder
    decoder: TransformerDecoder<T>,
    /// Configuration
    config: TransformerConfig<T>,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand + From<f64>>
    EncoderDecoderTransformer<T>
{
    /// Create a new encoder-decoder transformer
    pub fn new(config: TransformerConfig<T>) -> NeuralResult<Self> {
        let encoder = TransformerEncoder::new(&config)?;
        let decoder = TransformerDecoder::new(&config)?;

        Ok(Self {
            encoder,
            decoder,
            config,
        })
    }

    /// Forward pass for training
    pub fn forward(
        &mut self,
        src_ids: &Array2<usize>,
        tgt_ids: &Array2<usize>,
        src_mask: Option<&Array2<bool>>,
        tgt_mask: Option<&Array2<bool>>,
        cross_mask: Option<&Array2<bool>>,
        training: bool,
    ) -> NeuralResult<Array3<T>> {
        // Encode source sequence
        let encoder_output = self.encoder.forward(src_ids, src_mask, training)?;

        // Decode target sequence
        let decoder_output =
            self.decoder
                .forward(tgt_ids, &encoder_output, tgt_mask, cross_mask, training)?;

        Ok(decoder_output)
    }

    /// Generate sequences auto-regressively
    pub fn generate(
        &mut self,
        src_ids: &Array2<usize>,
        max_length: usize,
        start_token: usize,
        end_token: usize,
        src_mask: Option<&Array2<bool>>,
    ) -> NeuralResult<Array2<usize>> {
        let batch_size = src_ids.nrows();

        // Encode source
        let encoder_output = self.encoder.forward(src_ids, src_mask, false)?;

        // Initialize decoder input with start token
        let mut generated = Array2::from_elem((batch_size, 1), start_token);

        for _ in 1..max_length {
            // Create causal mask for decoder
            let seq_len = generated.ncols();
            let mut tgt_mask = Array2::from_elem((seq_len, seq_len), true);
            for i in 0..seq_len {
                for j in i + 1..seq_len {
                    tgt_mask[[i, j]] = false;
                }
            }

            // Forward pass through decoder
            let logits =
                self.decoder
                    .forward(&generated, &encoder_output, Some(&tgt_mask), None, false)?;

            // Get next token predictions (greedy decoding)
            let last_logits = logits.slice(s![.., -1, ..]);
            let mut next_tokens = Array1::zeros(batch_size);

            for b in 0..batch_size {
                let logits_row = last_logits.slice(s![b, ..]);
                let mut max_idx = 0;
                let mut max_val = *logits_row.iter().next().expect("empty iterator");

                for (idx, &val) in logits_row.iter().enumerate() {
                    if val > max_val {
                        max_val = val;
                        max_idx = idx;
                    }
                }
                next_tokens[b] = max_idx;
            }

            // Append next tokens to generated sequence
            let mut new_generated = Array2::zeros((batch_size, generated.ncols() + 1));
            new_generated
                .slice_mut(s![.., ..generated.ncols()])
                .assign(&generated);
            for b in 0..batch_size {
                new_generated[[b, generated.ncols()]] = next_tokens[b];
            }
            generated = new_generated;

            // Check for end tokens (early stopping)
            let mut all_ended = true;
            for b in 0..batch_size {
                if next_tokens[b] != end_token {
                    all_ended = false;
                    break;
                }
            }
            if all_ended {
                break;
            }
        }

        Ok(generated)
    }

    /// Get configuration
    pub fn config(&self) -> &TransformerConfig<T> {
        &self.config
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::essentials::Normal;
    use scirs2_core::ndarray::Array3;
    use scirs2_core::random::thread_rng;

    #[test]
    #[ignore]
    fn test_transformer_config() {
        let config: TransformerConfig<f64> = TransformerConfig::default();
        assert_eq!(config.d_model, 512);
        assert_eq!(config.num_heads, 8);
        assert_eq!(config.d_ff, 2048);
    }

    #[test]
    #[ignore]
    fn test_feed_forward_network() {
        let ffn = FeedForwardNetwork::new(512, 2048, 0.1, "relu".to_string())
            .expect("construction should succeed");
        let input = Array3::from_shape_fn((2, 10, 512), |_| {
            let mut rng = thread_rng();
            rng.sample(Normal::new(0.0, 1.0).expect("construction should succeed"))
        });
        let output = ffn
            .forward(&input, false)
            .expect("forward pass should succeed");
        assert_eq!(output.dim(), (2, 10, 512));
    }

    #[test]
    #[ignore]
    fn test_layer_norm() {
        let layer_norm = LayerNorm::<f64>::new(512);
        let input = Array3::from_shape_fn((2, 10, 512), |_| {
            let mut rng = thread_rng();
            rng.sample(Normal::new(0.0, 1.0).expect("construction should succeed"))
        });
        let output = layer_norm.forward(&input);
        assert_eq!(output.dim(), (2, 10, 512));
    }

    #[test]
    #[ignore]
    fn test_transformer_encoder_layer() {
        let config: TransformerConfig<f64> = TransformerConfig::default();
        let mut encoder_layer =
            TransformerEncoderLayer::new(&config).expect("construction should succeed");

        let input = Array3::from_shape_fn((2, 10, 512), |_| {
            let mut rng = thread_rng();
            rng.sample(Normal::new(0.0, 1.0).expect("construction should succeed"))
        });
        let output = encoder_layer
            .forward(&input, None, false)
            .expect("forward pass should succeed");
        assert_eq!(output.dim(), (2, 10, 512));
    }

    #[test]
    #[ignore]
    fn test_transformer_decoder_layer() {
        let config: TransformerConfig<f64> = TransformerConfig::default();
        let mut decoder_layer =
            TransformerDecoderLayer::new(&config).expect("construction should succeed");

        let input = Array3::from_shape_fn((2, 10, 512), |_| {
            let mut rng = thread_rng();
            rng.sample(Normal::new(0.0, 1.0).expect("construction should succeed"))
        });
        let encoder_output = Array3::from_shape_fn((2, 15, 512), |_| {
            let mut rng = thread_rng();
            rng.sample(Normal::new(0.0, 1.0).expect("construction should succeed"))
        });
        let output = decoder_layer
            .forward(&input, &encoder_output, None, None, false)
            .expect("operation should succeed");
        assert_eq!(output.dim(), (2, 10, 512));
    }

    #[test]
    #[ignore]
    fn test_encoder_decoder_transformer() {
        let config: TransformerConfig<f64> = TransformerConfig {
            vocab_size: 1000,
            max_seq_len: 20,
            d_model: 128,
            num_heads: 4,
            d_ff: 256,
            num_encoder_layers: 2,
            num_decoder_layers: 2,
            ..Default::default()
        };

        let mut transformer =
            EncoderDecoderTransformer::new(config).expect("construction should succeed");

        let src_ids = Array2::from_elem((2, 10), 1); // Simple input
        let tgt_ids = Array2::from_elem((2, 8), 2); // Simple target

        let output = transformer
            .forward(&src_ids, &tgt_ids, None, None, None, false)
            .expect("operation should succeed");
        assert_eq!(output.dim(), (2, 8, 1000)); // batch, seq, vocab
    }

    #[test]
    #[ignore]
    fn test_transformer_generation() {
        let config: TransformerConfig<f64> = TransformerConfig {
            vocab_size: 100,
            max_seq_len: 20,
            d_model: 64,
            num_heads: 2,
            d_ff: 128,
            num_encoder_layers: 1,
            num_decoder_layers: 1,
            ..Default::default()
        };

        let mut transformer =
            EncoderDecoderTransformer::new(config).expect("construction should succeed");

        let src_ids = Array2::from_elem((1, 5), 1);
        let generated = transformer
            .generate(&src_ids, 10, 2, 3, None)
            .expect("operation should succeed");

        assert_eq!(generated.nrows(), 1);
        assert!(generated.ncols() <= 10);
        assert_eq!(generated[[0, 0]], 2); // Should start with start token
    }
}