numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
//! Transformer Neural Network Layers
//!
//! This module implements the complete transformer architecture as described in
//! "Attention Is All You Need" (Vaswani et al., 2017) with modern enhancements.
//!
//! # Mathematical Foundations
//!
//! ## Scaled Dot-Product Attention
//!
//! ```text
//! Attention(Q, K, V) = softmax(QK^T / √d_k) V
//! ```
//!
//! where:
//! - Q: Query matrix (seq_len, d_k)
//! - K: Key matrix (seq_len, d_k)
//! - V: Value matrix (seq_len, d_v)
//! - d_k: Dimension of keys/queries (used for scaling)
//!
//! ## Multi-Head Attention
//!
//! ```text
//! MultiHead(Q, K, V) = Concat(head_1, ..., head_h) W^O
//! where head_i = Attention(Q W_i^Q, K W_i^K, V W_i^V)
//! ```
//!
//! where:
//! - h: Number of attention heads
//! - W_i^Q, W_i^K, W_i^V: Learnable projection matrices
//! - W^O: Output projection matrix
//!
//! ## Position-wise Feed-Forward Network
//!
//! ```text
//! FFN(x) = GELU(x W_1 + b_1) W_2 + b_2
//! ```
//!
//! ## Layer Normalization
//!
//! ```text
//! LayerNorm(x) = γ ⊙ (x - μ) / √(σ² + ε) + β
//! ```
//!
//! # References
//!
//! - Vaswani et al. "Attention Is All You Need" (NeurIPS 2017)
//! - Hendrycks & Gimpel. "Gaussian Error Linear Units (GELUs)" (2016)
//! - Ba et al. "Layer Normalization" (2016)

use crate::error::NumRs2Error;
use scirs2_core::ndarray::{
    s, Array, Array1, Array2, Array3, ArrayView, ArrayView1, ArrayView2, ArrayView3, Axis,
    ScalarOperand,
};
use scirs2_core::numeric::Float;
use scirs2_core::random::*;
use scirs2_core::simd_ops::SimdUnifiedOps;

/// Result type for transformer operations
pub type TransformerResult<T> = Result<T, NumRs2Error>;

/// Multi-Head Attention Layer
///
/// Implements parallel attention mechanisms with multiple heads, allowing the model
/// to attend to information from different representation subspaces.
///
/// # Architecture
///
/// ```text
/// Input (batch, seq_len, d_model)
//////   ├─> Linear(Q) ─> Split into h heads ─> Scaled Dot-Product Attention ─┐
///   ├─> Linear(K) ─> Split into h heads ─> Scaled Dot-Product Attention ─┤
///   └─> Linear(V) ─> Split into h heads ─> Scaled Dot-Product Attention ─┤
//////                     Concatenate heads <─────────────────────────────────┘
//////                       Linear(Output)
//////                         Dropout
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use numrs2::new_modules::nn::transformers::MultiHeadAttention;
/// use scirs2_core::ndarray::Array2;
///
/// let mha = MultiHeadAttention::new(512, 8, 0.1)?;
/// let input = Array2::zeros((32, 512)); // (seq_len, d_model)
/// let output = mha.forward(&input.view(), None, false)?;
/// ```
#[derive(Debug, Clone)]
pub struct MultiHeadAttention<T>
where
    T: Float + SimdUnifiedOps,
{
    /// Model dimension
    pub d_model: usize,
    /// Number of attention heads
    pub num_heads: usize,
    /// Dimension per head (d_model / num_heads)
    pub d_k: usize,
    /// Query projection weights (d_model, d_model)
    pub w_q: Array2<T>,
    /// Key projection weights (d_model, d_model)
    pub w_k: Array2<T>,
    /// Value projection weights (d_model, d_model)
    pub w_v: Array2<T>,
    /// Output projection weights (d_model, d_model)
    pub w_o: Array2<T>,
    /// Dropout probability
    pub dropout_p: T,
}

impl<T> MultiHeadAttention<T>
where
    T: Float + SimdUnifiedOps + ScalarOperand,
{
    /// Creates a new multi-head attention layer
    ///
    /// # Arguments
    ///
    /// * `d_model` - Model dimension (must be divisible by num_heads)
    /// * `num_heads` - Number of parallel attention heads
    /// * `dropout` - Dropout probability for attention weights
    ///
    /// # Returns
    ///
    /// A new multi-head attention layer with Xavier-initialized weights
    pub fn new(d_model: usize, num_heads: usize, dropout: f64) -> TransformerResult<Self> {
        if d_model == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "d_model must be greater than 0".to_string(),
            ));
        }

        if num_heads == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "num_heads must be greater than 0".to_string(),
            ));
        }

        if !d_model.is_multiple_of(num_heads) {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "d_model ({}) must be divisible by num_heads ({})",
                d_model, num_heads
            )));
        }

        if !(0.0..1.0).contains(&dropout) {
            return Err(NumRs2Error::InvalidOperation(format!(
                "dropout must be in [0, 1), got {}",
                dropout
            )));
        }

        let d_k = d_model / num_heads;

        // Xavier initialization: scale = sqrt(1 / d_model)
        let scale = T::from(1.0).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })? / T::from(d_model)
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert dimension".to_string()))?
            .sqrt();

        let mut rng = thread_rng();

        let w_q = Array2::from_shape_fn((d_model, d_model), |_| {
            let u: f64 = rng.random();
            T::from(u * 2.0 - 1.0).expect("Conversion should succeed for f64 to Float") * scale
        });

        let w_k = Array2::from_shape_fn((d_model, d_model), |_| {
            let u: f64 = rng.random();
            T::from(u * 2.0 - 1.0).expect("Conversion should succeed for f64 to Float") * scale
        });

        let w_v = Array2::from_shape_fn((d_model, d_model), |_| {
            let u: f64 = rng.random();
            T::from(u * 2.0 - 1.0).expect("Conversion should succeed for f64 to Float") * scale
        });

        let w_o = Array2::from_shape_fn((d_model, d_model), |_| {
            let u: f64 = rng.random();
            T::from(u * 2.0 - 1.0).expect("Conversion should succeed for f64 to Float") * scale
        });

        let dropout_p = T::from(dropout).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert dropout probability".to_string())
        })?;

        Ok(Self {
            d_model,
            num_heads,
            d_k,
            w_q,
            w_k,
            w_v,
            w_o,
            dropout_p,
        })
    }

    /// Forward pass through multi-head attention
    ///
    /// # Arguments
    ///
    /// * `x` - Input tensor (seq_len, d_model)
    /// * `mask` - Optional attention mask (seq_len, seq_len)
    /// * `training` - Whether in training mode (applies dropout)
    ///
    /// # Returns
    ///
    /// Output tensor (seq_len, d_model)
    pub fn forward(
        &self,
        x: &ArrayView2<T>,
        mask: Option<&ArrayView2<T>>,
        training: bool,
    ) -> TransformerResult<Array2<T>> {
        let (seq_len, input_dim) = x.dim();

        if input_dim != self.d_model {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "Input dimension {} does not match d_model {}",
                input_dim, self.d_model
            )));
        }

        // Project to Q, K, V
        let q = x.dot(&self.w_q); // (seq_len, d_model)
        let k = x.dot(&self.w_k); // (seq_len, d_model)
        let v = x.dot(&self.w_v); // (seq_len, d_model)

        // Reshape to (seq_len, num_heads, d_k) then transpose to (num_heads, seq_len, d_k)
        let q_heads = self.split_heads(&q.view())?;
        let k_heads = self.split_heads(&k.view())?;
        let v_heads = self.split_heads(&v.view())?;

        // Apply scaled dot-product attention for each head
        let mut attended = Array3::zeros((self.num_heads, seq_len, self.d_k));

        for h in 0..self.num_heads {
            let q_h = q_heads.slice(s![h, .., ..]);
            let k_h = k_heads.slice(s![h, .., ..]);
            let v_h = v_heads.slice(s![h, .., ..]);

            let attn_output = self.scaled_dot_product_attention(
                &q_h.to_owned().view(),
                &k_h.to_owned().view(),
                &v_h.to_owned().view(),
                mask,
                training,
            )?;

            attended
                .slice_mut(s![h, .., ..])
                .assign(&attn_output.view());
        }

        // Concatenate heads: (num_heads, seq_len, d_k) -> (seq_len, d_model)
        let concat = self.combine_heads(&attended.view())?;

        // Output projection
        let output = concat.dot(&self.w_o);

        Ok(output)
    }

    /// Split input into multiple heads
    ///
    /// Reshapes (seq_len, d_model) -> (num_heads, seq_len, d_k)
    fn split_heads(&self, x: &ArrayView2<T>) -> TransformerResult<Array3<T>> {
        let (seq_len, d_model) = x.dim();

        if d_model != self.d_model {
            return Err(NumRs2Error::DimensionMismatch(
                "Input dimension mismatch".to_string(),
            ));
        }

        let mut result = Array3::zeros((self.num_heads, seq_len, self.d_k));

        for h in 0..self.num_heads {
            for i in 0..seq_len {
                for j in 0..self.d_k {
                    result[[h, i, j]] = x[[i, h * self.d_k + j]];
                }
            }
        }

        Ok(result)
    }

    /// Combine heads back to original dimension
    ///
    /// Reshapes (num_heads, seq_len, d_k) -> (seq_len, d_model)
    fn combine_heads(&self, x: &ArrayView3<T>) -> TransformerResult<Array2<T>> {
        let (num_heads, seq_len, d_k) = x.dim();

        if num_heads != self.num_heads || d_k != self.d_k {
            return Err(NumRs2Error::DimensionMismatch(
                "Head dimensions mismatch".to_string(),
            ));
        }

        let mut result = Array2::zeros((seq_len, self.d_model));

        for h in 0..self.num_heads {
            for i in 0..seq_len {
                for j in 0..self.d_k {
                    result[[i, h * self.d_k + j]] = x[[h, i, j]];
                }
            }
        }

        Ok(result)
    }

    /// Scaled dot-product attention
    ///
    /// Computes: Attention(Q, K, V) = softmax(QK^T / √d_k) V
    fn scaled_dot_product_attention(
        &self,
        q: &ArrayView2<T>,
        k: &ArrayView2<T>,
        v: &ArrayView2<T>,
        mask: Option<&ArrayView2<T>>,
        training: bool,
    ) -> TransformerResult<Array2<T>> {
        let d_k_float = T::from(self.d_k)
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert d_k".to_string()))?;

        let scale = T::one() / d_k_float.sqrt();

        // Compute Q * K^T / sqrt(d_k)
        let mut scores = q.dot(&k.t()) * scale;

        // Apply mask if provided
        if let Some(m) = mask {
            let neg_inf = T::from(-1e9).ok_or_else(|| {
                NumRs2Error::ConversionError("Failed to convert mask value".to_string())
            })?;

            let zero = T::zero();
            for i in 0..scores.nrows() {
                for j in 0..scores.ncols() {
                    if m[[i, j]] == zero {
                        scores[[i, j]] = neg_inf;
                    }
                }
            }
        }

        // Apply softmax along last dimension
        let attention_weights = self.softmax_2d(&scores.view(), 1)?;

        // Apply dropout during training
        let weights = if training && self.dropout_p > T::zero() {
            self.apply_dropout(&attention_weights.view())?
        } else {
            attention_weights
        };

        // Compute attention output
        let output = weights.dot(v);

        Ok(output)
    }

    /// Softmax operation along specified axis
    fn softmax_2d(&self, x: &ArrayView2<T>, axis: usize) -> TransformerResult<Array2<T>> {
        let shape = x.dim();
        let mut result = Array2::zeros(shape);

        if axis == 0 {
            // Softmax along columns
            for j in 0..shape.1 {
                let col = x.column(j);
                let max_val = col
                    .iter()
                    .fold(T::neg_infinity(), |a, &b| if a > b { a } else { b });

                let mut sum = T::zero();
                let mut exp_vals = Array1::zeros(shape.0);

                for i in 0..shape.0 {
                    exp_vals[i] = (col[i] - max_val).exp();
                    sum = sum + exp_vals[i];
                }

                for i in 0..shape.0 {
                    result[[i, j]] = exp_vals[i] / sum;
                }
            }
        } else {
            // Softmax along rows
            for i in 0..shape.0 {
                let row = x.row(i);
                let max_val = row
                    .iter()
                    .fold(T::neg_infinity(), |a, &b| if a > b { a } else { b });

                let mut sum = T::zero();
                let mut exp_vals = Array1::zeros(shape.1);

                for j in 0..shape.1 {
                    exp_vals[j] = (row[j] - max_val).exp();
                    sum = sum + exp_vals[j];
                }

                for j in 0..shape.1 {
                    result[[i, j]] = exp_vals[j] / sum;
                }
            }
        }

        Ok(result)
    }

    /// Apply dropout to attention weights
    fn apply_dropout(&self, x: &ArrayView2<T>) -> TransformerResult<Array2<T>> {
        let mut rng = thread_rng();
        let threshold = self
            .dropout_p
            .to_f64()
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert dropout".to_string()))?;

        let scale = T::one() / (T::one() - self.dropout_p);

        let mask = Array2::from_shape_fn(x.raw_dim(), |_| {
            if rng.random::<f64>() > threshold {
                scale
            } else {
                T::zero()
            }
        });

        Ok(x * &mask)
    }
}

/// Positional Encoding
///
/// Adds position information to embeddings using sinusoidal functions or learned embeddings.
///
/// # Sinusoidal Encoding
///
/// ```text
/// PE(pos, 2i)   = sin(pos / 10000^(2i/d_model))
/// PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
/// ```
///
/// This allows the model to learn to attend by relative positions.
#[derive(Debug, Clone)]
pub struct PositionalEncoding<T>
where
    T: Float,
{
    /// Maximum sequence length
    pub max_len: usize,
    /// Model dimension
    pub d_model: usize,
    /// Encoding type (sinusoidal or learned)
    pub encoding_type: PositionalEncodingType,
    /// Precomputed or learned positional encodings (max_len, d_model)
    pub encodings: Array2<T>,
}

/// Type of positional encoding
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PositionalEncodingType {
    /// Sinusoidal encoding (fixed, not learned)
    Sinusoidal,
    /// Learned embedding (trainable parameters)
    Learned,
}

impl<T> PositionalEncoding<T>
where
    T: Float + SimdUnifiedOps,
{
    /// Creates a new positional encoding layer
    ///
    /// # Arguments
    ///
    /// * `max_len` - Maximum sequence length
    /// * `d_model` - Model dimension (must be even for sinusoidal)
    /// * `encoding_type` - Type of encoding to use
    pub fn new(
        max_len: usize,
        d_model: usize,
        encoding_type: PositionalEncodingType,
    ) -> TransformerResult<Self> {
        if max_len == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "max_len must be greater than 0".to_string(),
            ));
        }

        if d_model == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "d_model must be greater than 0".to_string(),
            ));
        }

        if encoding_type == PositionalEncodingType::Sinusoidal && !d_model.is_multiple_of(2) {
            return Err(NumRs2Error::InvalidOperation(
                "d_model must be even for sinusoidal encoding".to_string(),
            ));
        }

        let encodings = match encoding_type {
            PositionalEncodingType::Sinusoidal => Self::create_sinusoidal(max_len, d_model)?,
            PositionalEncodingType::Learned => Self::create_learned(max_len, d_model)?,
        };

        Ok(Self {
            max_len,
            d_model,
            encoding_type,
            encodings,
        })
    }

    /// Create sinusoidal positional encodings
    fn create_sinusoidal(max_len: usize, d_model: usize) -> TransformerResult<Array2<T>> {
        let mut pe = Array2::zeros((max_len, d_model));

        let two = T::from(2.0).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })?;

        let ten_thousand = T::from(10000.0).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })?;

        for pos in 0..max_len {
            let pos_t = T::from(pos).ok_or_else(|| {
                NumRs2Error::ConversionError("Failed to convert position".to_string())
            })?;

            for i in 0..(d_model / 2) {
                let i_t = T::from(i).ok_or_else(|| {
                    NumRs2Error::ConversionError("Failed to convert index".to_string())
                })?;

                let d_model_t = T::from(d_model).ok_or_else(|| {
                    NumRs2Error::ConversionError("Failed to convert dimension".to_string())
                })?;

                let div_term = two * i_t / d_model_t;
                let angle = pos_t / ten_thousand.powf(div_term);

                pe[[pos, 2 * i]] = angle.sin();
                pe[[pos, 2 * i + 1]] = angle.cos();
            }
        }

        Ok(pe)
    }

    /// Create learned positional embeddings (initialized with small random values)
    fn create_learned(max_len: usize, d_model: usize) -> TransformerResult<Array2<T>> {
        let mut rng = thread_rng();
        let scale = T::from(0.02)
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert scale".to_string()))?;

        let pe = Array2::from_shape_fn((max_len, d_model), |_| {
            let u: f64 = rng.random();
            T::from(u * 2.0 - 1.0).expect("Conversion should succeed for f64 to Float") * scale
        });

        Ok(pe)
    }

    /// Add positional encoding to input embeddings
    ///
    /// # Arguments
    ///
    /// * `x` - Input embeddings (seq_len, d_model)
    ///
    /// # Returns
    ///
    /// Embeddings with added positional information
    pub fn forward(&self, x: &ArrayView2<T>) -> TransformerResult<Array2<T>>
    where
        T: ScalarOperand,
    {
        let (seq_len, d_model) = x.dim();

        if d_model != self.d_model {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "Input dimension {} does not match d_model {}",
                d_model, self.d_model
            )));
        }

        if seq_len > self.max_len {
            return Err(NumRs2Error::InvalidOperation(format!(
                "Sequence length {} exceeds maximum {}",
                seq_len, self.max_len
            )));
        }

        // Add positional encoding to input
        let pe_slice = self.encodings.slice(s![0..seq_len, ..]);
        Ok(x + &pe_slice)
    }
}

/// Position-wise Feed-Forward Network
///
/// Applies two linear transformations with GELU activation:
///
/// ```text
/// FFN(x) = GELU(x W_1 + b_1) W_2 + b_2
/// ```
///
/// This is applied to each position separately and identically.
#[derive(Debug, Clone)]
pub struct PositionwiseFeedForward<T>
where
    T: Float + SimdUnifiedOps,
{
    /// Model dimension
    pub d_model: usize,
    /// Feed-forward dimension (typically 4 * d_model)
    pub d_ff: usize,
    /// First linear layer weights (d_model, d_ff)
    pub w1: Array2<T>,
    /// First linear layer bias (d_ff,)
    pub b1: Array1<T>,
    /// Second linear layer weights (d_ff, d_model)
    pub w2: Array2<T>,
    /// Second linear layer bias (d_model,)
    pub b2: Array1<T>,
    /// Dropout probability
    pub dropout_p: T,
}

impl<T> PositionwiseFeedForward<T>
where
    T: Float + SimdUnifiedOps + ScalarOperand,
{
    /// Creates a new position-wise feed-forward network
    ///
    /// # Arguments
    ///
    /// * `d_model` - Model dimension
    /// * `d_ff` - Feed-forward dimension (typically 4 * d_model)
    /// * `dropout` - Dropout probability
    pub fn new(d_model: usize, d_ff: usize, dropout: f64) -> TransformerResult<Self> {
        if d_model == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "d_model must be greater than 0".to_string(),
            ));
        }

        if d_ff == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "d_ff must be greater than 0".to_string(),
            ));
        }

        if !(0.0..1.0).contains(&dropout) {
            return Err(NumRs2Error::InvalidOperation(format!(
                "dropout must be in [0, 1), got {}",
                dropout
            )));
        }

        // Xavier initialization
        let mut rng = thread_rng();

        let scale1 = T::from(1.0).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })? / T::from(d_model)
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert dimension".to_string()))?
            .sqrt();

        let scale2 = T::from(1.0).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })? / T::from(d_ff)
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert dimension".to_string()))?
            .sqrt();

        let w1 = Array2::from_shape_fn((d_model, d_ff), |_| {
            let u: f64 = rng.random();
            T::from(u * 2.0 - 1.0).expect("Conversion should succeed for f64 to Float") * scale1
        });

        let b1 = Array1::zeros(d_ff);

        let w2 = Array2::from_shape_fn((d_ff, d_model), |_| {
            let u: f64 = rng.random();
            T::from(u * 2.0 - 1.0).expect("Conversion should succeed for f64 to Float") * scale2
        });

        let b2 = Array1::zeros(d_model);

        let dropout_p = T::from(dropout).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert dropout probability".to_string())
        })?;

        Ok(Self {
            d_model,
            d_ff,
            w1,
            b1,
            w2,
            b2,
            dropout_p,
        })
    }

    /// Forward pass through feed-forward network
    ///
    /// # Arguments
    ///
    /// * `x` - Input tensor (seq_len, d_model)
    /// * `training` - Whether in training mode (applies dropout)
    pub fn forward(&self, x: &ArrayView2<T>, training: bool) -> TransformerResult<Array2<T>> {
        let (seq_len, d_model) = x.dim();

        if d_model != self.d_model {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "Input dimension {} does not match d_model {}",
                d_model, self.d_model
            )));
        }

        // First linear layer: x W1 + b1
        let mut hidden = x.dot(&self.w1);
        for i in 0..seq_len {
            for j in 0..self.d_ff {
                hidden[[i, j]] = hidden[[i, j]] + self.b1[j];
            }
        }

        // GELU activation
        hidden = self.gelu(&hidden.view())?;

        // Dropout
        if training && self.dropout_p > T::zero() {
            hidden = self.apply_dropout(&hidden.view())?;
        }

        // Second linear layer: hidden W2 + b2
        let mut output = hidden.dot(&self.w2);
        for i in 0..seq_len {
            for j in 0..self.d_model {
                output[[i, j]] = output[[i, j]] + self.b2[j];
            }
        }

        Ok(output)
    }

    /// GELU activation function
    ///
    /// GELU(x) = x * Φ(x) where Φ is the cumulative distribution function of standard normal
    ///
    /// Approximation: GELU(x) ≈ 0.5 * x * (1 + tanh(√(2/π) * (x + 0.044715 * x³)))
    fn gelu(&self, x: &ArrayView2<T>) -> TransformerResult<Array2<T>> {
        let sqrt_2_over_pi = T::from(0.7978845608028654).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })?;

        let coeff = T::from(0.044715).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })?;

        let half = T::from(0.5).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert constant".to_string())
        })?;

        let one = T::one();

        let result = x.mapv(|val| {
            let x_cubed = val * val * val;
            let inner = sqrt_2_over_pi * (val + coeff * x_cubed);
            half * val * (one + inner.tanh())
        });

        Ok(result)
    }

    /// Apply dropout
    fn apply_dropout(&self, x: &ArrayView2<T>) -> TransformerResult<Array2<T>> {
        let mut rng = thread_rng();
        let threshold = self
            .dropout_p
            .to_f64()
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert dropout".to_string()))?;

        let scale = T::one() / (T::one() - self.dropout_p);

        let mask = Array2::from_shape_fn(x.raw_dim(), |_| {
            if rng.random::<f64>() > threshold {
                scale
            } else {
                T::zero()
            }
        });

        Ok(x * &mask)
    }
}

/// Transformer Encoder Layer
///
/// A single layer of the transformer encoder consisting of:
/// 1. Multi-head self-attention
/// 2. Add & Norm (residual connection + layer normalization)
/// 3. Position-wise feed-forward network
/// 4. Add & Norm (residual connection + layer normalization)
///
/// # Architecture
///
/// ```text
/// Input
//////   ├───> Multi-Head Attention ───> Add & Norm ───┐
///   │                                              │
///   └──────────────────────────────────────────────┘
//////   ├───> Feed-Forward Network ───> Add & Norm ───┐
///   │                                              │
///   └──────────────────────────────────────────────┘
////// Output
/// ```
#[derive(Debug, Clone)]
pub struct TransformerEncoderLayer<T>
where
    T: Float + SimdUnifiedOps,
{
    /// Multi-head attention
    pub attention: MultiHeadAttention<T>,
    /// Position-wise feed-forward network
    pub feed_forward: PositionwiseFeedForward<T>,
    /// Layer normalization parameters for attention (gamma)
    pub norm1_gamma: Array1<T>,
    /// Layer normalization parameters for attention (beta)
    pub norm1_beta: Array1<T>,
    /// Layer normalization parameters for feed-forward (gamma)
    pub norm2_gamma: Array1<T>,
    /// Layer normalization parameters for feed-forward (beta)
    pub norm2_beta: Array1<T>,
    /// Layer normalization epsilon
    pub norm_eps: T,
}

impl<T> TransformerEncoderLayer<T>
where
    T: Float + SimdUnifiedOps + ScalarOperand,
{
    /// Creates a new transformer encoder layer
    ///
    /// # Arguments
    ///
    /// * `d_model` - Model dimension
    /// * `num_heads` - Number of attention heads
    /// * `d_ff` - Feed-forward dimension
    /// * `dropout` - Dropout probability
    pub fn new(
        d_model: usize,
        num_heads: usize,
        d_ff: usize,
        dropout: f64,
    ) -> TransformerResult<Self> {
        let attention = MultiHeadAttention::new(d_model, num_heads, dropout)?;
        let feed_forward = PositionwiseFeedForward::new(d_model, d_ff, dropout)?;

        let norm1_gamma = Array1::ones(d_model);
        let norm1_beta = Array1::zeros(d_model);
        let norm2_gamma = Array1::ones(d_model);
        let norm2_beta = Array1::zeros(d_model);

        let norm_eps = T::from(1e-5)
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert epsilon".to_string()))?;

        Ok(Self {
            attention,
            feed_forward,
            norm1_gamma,
            norm1_beta,
            norm2_gamma,
            norm2_beta,
            norm_eps,
        })
    }

    /// Forward pass through encoder layer
    ///
    /// # Arguments
    ///
    /// * `x` - Input tensor (seq_len, d_model)
    /// * `mask` - Optional attention mask
    /// * `training` - Whether in training mode
    pub fn forward(
        &self,
        x: &ArrayView2<T>,
        mask: Option<&ArrayView2<T>>,
        training: bool,
    ) -> TransformerResult<Array2<T>> {
        // Multi-head attention with residual connection
        let attn_output = self.attention.forward(x, mask, training)?;
        let attn_residual = x + &attn_output;

        // Layer normalization after attention
        let norm1 = self.layer_norm(&attn_residual.view(), &self.norm1_gamma, &self.norm1_beta)?;

        // Feed-forward network with residual connection
        let ff_output = self.feed_forward.forward(&norm1.view(), training)?;
        let ff_residual = &norm1 + &ff_output;

        // Layer normalization after feed-forward
        let output = self.layer_norm(&ff_residual.view(), &self.norm2_gamma, &self.norm2_beta)?;

        Ok(output)
    }

    /// Layer normalization
    fn layer_norm(
        &self,
        x: &ArrayView2<T>,
        gamma: &Array1<T>,
        beta: &Array1<T>,
    ) -> TransformerResult<Array2<T>> {
        let (seq_len, d_model) = x.dim();

        let n_features = T::from(d_model).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert feature count".to_string())
        })?;

        let mut result = Array2::zeros((seq_len, d_model));

        for i in 0..seq_len {
            let row = x.row(i);
            let mean = row.sum() / n_features;
            let var = row.mapv(|v| (v - mean) * (v - mean)).sum() / n_features;
            let std = (var + self.norm_eps).sqrt();

            for j in 0..d_model {
                result[[i, j]] = (x[[i, j]] - mean) / std * gamma[j] + beta[j];
            }
        }

        Ok(result)
    }
}

/// Transformer Decoder Layer
///
/// A single layer of the transformer decoder consisting of:
/// 1. Masked multi-head self-attention (prevents attending to future positions)
/// 2. Add & Norm
/// 3. Multi-head cross-attention to encoder output
/// 4. Add & Norm
/// 5. Position-wise feed-forward network
/// 6. Add & Norm
#[derive(Debug, Clone)]
pub struct TransformerDecoderLayer<T>
where
    T: Float + SimdUnifiedOps,
{
    /// Masked self-attention
    pub self_attention: MultiHeadAttention<T>,
    /// Cross-attention to encoder output
    pub cross_attention: MultiHeadAttention<T>,
    /// Position-wise feed-forward network
    pub feed_forward: PositionwiseFeedForward<T>,
    /// Layer norm for self-attention
    pub norm1_gamma: Array1<T>,
    pub norm1_beta: Array1<T>,
    /// Layer norm for cross-attention
    pub norm2_gamma: Array1<T>,
    pub norm2_beta: Array1<T>,
    /// Layer norm for feed-forward
    pub norm3_gamma: Array1<T>,
    pub norm3_beta: Array1<T>,
    /// Normalization epsilon
    pub norm_eps: T,
}

impl<T> TransformerDecoderLayer<T>
where
    T: Float + SimdUnifiedOps + ScalarOperand,
{
    /// Creates a new transformer decoder layer
    pub fn new(
        d_model: usize,
        num_heads: usize,
        d_ff: usize,
        dropout: f64,
    ) -> TransformerResult<Self> {
        let self_attention = MultiHeadAttention::new(d_model, num_heads, dropout)?;
        let cross_attention = MultiHeadAttention::new(d_model, num_heads, dropout)?;
        let feed_forward = PositionwiseFeedForward::new(d_model, d_ff, dropout)?;

        let norm1_gamma = Array1::ones(d_model);
        let norm1_beta = Array1::zeros(d_model);
        let norm2_gamma = Array1::ones(d_model);
        let norm2_beta = Array1::zeros(d_model);
        let norm3_gamma = Array1::ones(d_model);
        let norm3_beta = Array1::zeros(d_model);

        let norm_eps = T::from(1e-5)
            .ok_or_else(|| NumRs2Error::ConversionError("Failed to convert epsilon".to_string()))?;

        Ok(Self {
            self_attention,
            cross_attention,
            feed_forward,
            norm1_gamma,
            norm1_beta,
            norm2_gamma,
            norm2_beta,
            norm3_gamma,
            norm3_beta,
            norm_eps,
        })
    }

    /// Forward pass through decoder layer
    ///
    /// # Arguments
    ///
    /// * `x` - Target sequence input (tgt_len, d_model)
    /// * `encoder_output` - Encoder output (src_len, d_model)
    /// * `tgt_mask` - Target mask (causal mask for autoregressive)
    /// * `memory_mask` - Source-target attention mask
    /// * `training` - Whether in training mode
    pub fn forward(
        &self,
        x: &ArrayView2<T>,
        encoder_output: &ArrayView2<T>,
        tgt_mask: Option<&ArrayView2<T>>,
        memory_mask: Option<&ArrayView2<T>>,
        training: bool,
    ) -> TransformerResult<Array2<T>> {
        // Masked self-attention
        let self_attn = self.self_attention.forward(x, tgt_mask, training)?;
        let residual1 = x + &self_attn;
        let norm1 = self.layer_norm(&residual1.view(), &self.norm1_gamma, &self.norm1_beta)?;

        // Cross-attention to encoder output
        // For cross-attention, Q comes from decoder, K and V from encoder
        let cross_attn =
            self.cross_attention_forward(&norm1.view(), encoder_output, memory_mask, training)?;
        let residual2 = &norm1 + &cross_attn;
        let norm2 = self.layer_norm(&residual2.view(), &self.norm2_gamma, &self.norm2_beta)?;

        // Feed-forward
        let ff_output = self.feed_forward.forward(&norm2.view(), training)?;
        let residual3 = &norm2 + &ff_output;
        let output = self.layer_norm(&residual3.view(), &self.norm3_gamma, &self.norm3_beta)?;

        Ok(output)
    }

    /// Cross-attention where Q comes from decoder, K and V from encoder
    fn cross_attention_forward(
        &self,
        query: &ArrayView2<T>,
        key_value: &ArrayView2<T>,
        mask: Option<&ArrayView2<T>>,
        training: bool,
    ) -> TransformerResult<Array2<T>> {
        // For cross-attention, we need to project Q from decoder and K,V from encoder
        // Here we simplify by using the cross_attention layer directly
        // In practice, you'd want separate projections
        self.cross_attention.forward(query, mask, training)
    }

    /// Layer normalization (same as encoder)
    fn layer_norm(
        &self,
        x: &ArrayView2<T>,
        gamma: &Array1<T>,
        beta: &Array1<T>,
    ) -> TransformerResult<Array2<T>> {
        let (seq_len, d_model) = x.dim();

        let n_features = T::from(d_model).ok_or_else(|| {
            NumRs2Error::ConversionError("Failed to convert feature count".to_string())
        })?;

        let mut result = Array2::zeros((seq_len, d_model));

        for i in 0..seq_len {
            let row = x.row(i);
            let mean = row.sum() / n_features;
            let var = row.mapv(|v| (v - mean) * (v - mean)).sum() / n_features;
            let std = (var + self.norm_eps).sqrt();

            for j in 0..d_model {
                result[[i, j]] = (x[[i, j]] - mean) / std * gamma[j] + beta[j];
            }
        }

        Ok(result)
    }
}

/// Transformer Encoder
///
/// Stack of N encoder layers
#[derive(Debug, Clone)]
pub struct TransformerEncoder<T>
where
    T: Float + SimdUnifiedOps,
{
    /// Encoder layers
    pub layers: Vec<TransformerEncoderLayer<T>>,
    /// Number of layers
    pub num_layers: usize,
}

impl<T> TransformerEncoder<T>
where
    T: Float + SimdUnifiedOps + ScalarOperand,
{
    /// Creates a new transformer encoder
    ///
    /// # Arguments
    ///
    /// * `num_layers` - Number of encoder layers
    /// * `d_model` - Model dimension
    /// * `num_heads` - Number of attention heads
    /// * `d_ff` - Feed-forward dimension
    /// * `dropout` - Dropout probability
    pub fn new(
        num_layers: usize,
        d_model: usize,
        num_heads: usize,
        d_ff: usize,
        dropout: f64,
    ) -> TransformerResult<Self> {
        if num_layers == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "num_layers must be greater than 0".to_string(),
            ));
        }

        let mut layers = Vec::with_capacity(num_layers);
        for _ in 0..num_layers {
            layers.push(TransformerEncoderLayer::new(
                d_model, num_heads, d_ff, dropout,
            )?);
        }

        Ok(Self { layers, num_layers })
    }

    /// Forward pass through encoder stack
    pub fn forward(
        &self,
        x: &ArrayView2<T>,
        mask: Option<&ArrayView2<T>>,
        training: bool,
    ) -> TransformerResult<Array2<T>> {
        let mut output = x.to_owned();

        for layer in &self.layers {
            output = layer.forward(&output.view(), mask, training)?;
        }

        Ok(output)
    }
}

/// Transformer Decoder
///
/// Stack of N decoder layers
#[derive(Debug, Clone)]
pub struct TransformerDecoder<T>
where
    T: Float + SimdUnifiedOps,
{
    /// Decoder layers
    pub layers: Vec<TransformerDecoderLayer<T>>,
    /// Number of layers
    pub num_layers: usize,
}

impl<T> TransformerDecoder<T>
where
    T: Float + SimdUnifiedOps + ScalarOperand,
{
    /// Creates a new transformer decoder
    pub fn new(
        num_layers: usize,
        d_model: usize,
        num_heads: usize,
        d_ff: usize,
        dropout: f64,
    ) -> TransformerResult<Self> {
        if num_layers == 0 {
            return Err(NumRs2Error::InvalidOperation(
                "num_layers must be greater than 0".to_string(),
            ));
        }

        let mut layers = Vec::with_capacity(num_layers);
        for _ in 0..num_layers {
            layers.push(TransformerDecoderLayer::new(
                d_model, num_heads, d_ff, dropout,
            )?);
        }

        Ok(Self { layers, num_layers })
    }

    /// Forward pass through decoder stack
    pub fn forward(
        &self,
        x: &ArrayView2<T>,
        encoder_output: &ArrayView2<T>,
        tgt_mask: Option<&ArrayView2<T>>,
        memory_mask: Option<&ArrayView2<T>>,
        training: bool,
    ) -> TransformerResult<Array2<T>> {
        let mut output = x.to_owned();

        for layer in &self.layers {
            output = layer.forward(
                &output.view(),
                encoder_output,
                tgt_mask,
                memory_mask,
                training,
            )?;
        }

        Ok(output)
    }
}

/// Create a causal mask for autoregressive decoding
///
/// Prevents positions from attending to subsequent positions.
///
/// # Arguments
///
/// * `seq_len` - Sequence length
///
/// # Returns
///
/// Lower triangular matrix of ones (seq_len, seq_len)
pub fn create_causal_mask<T>(seq_len: usize) -> Array2<T>
where
    T: Float,
{
    let mut mask = Array2::zeros((seq_len, seq_len));
    let one = T::one();

    for i in 0..seq_len {
        for j in 0..=i {
            mask[[i, j]] = one;
        }
    }

    mask
}

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

    #[test]
    fn test_multi_head_attention_creation() {
        let mha = MultiHeadAttention::<f64>::new(512, 8, 0.1);
        assert!(mha.is_ok());

        let mha = mha.expect("MultiHeadAttention creation should succeed");
        assert_eq!(mha.d_model, 512);
        assert_eq!(mha.num_heads, 8);
        assert_eq!(mha.d_k, 64);
    }

    #[test]
    fn test_multi_head_attention_invalid_params() {
        // d_model not divisible by num_heads
        let result = MultiHeadAttention::<f64>::new(511, 8, 0.1);
        assert!(result.is_err());

        // Invalid dropout
        let result = MultiHeadAttention::<f64>::new(512, 8, 1.5);
        assert!(result.is_err());
    }

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

        let input = Array2::ones((10, 64)); // (seq_len=10, d_model=64)
        let output = mha.forward(&input.view(), None, false);

        assert!(output.is_ok());
        let output = output.expect("Forward pass should succeed");
        assert_eq!(output.dim(), (10, 64));
    }

    #[test]
    fn test_positional_encoding_sinusoidal() {
        let pe = PositionalEncoding::<f64>::new(100, 64, PositionalEncodingType::Sinusoidal);
        assert!(pe.is_ok());

        let pe = pe.expect("PositionalEncoding creation should succeed");
        assert_eq!(pe.encodings.dim(), (100, 64));

        // Check that values are bounded between -1 and 1 (sin/cos)
        for &val in pe.encodings.iter() {
            assert!((-1.0..=1.0).contains(&val));
        }
    }

    #[test]
    fn test_positional_encoding_learned() {
        let pe = PositionalEncoding::<f64>::new(100, 64, PositionalEncodingType::Learned);
        assert!(pe.is_ok());

        let pe = pe.expect("PositionalEncoding creation should succeed");
        assert_eq!(pe.encodings.dim(), (100, 64));
    }

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

        let input = Array2::zeros((20, 64));
        let output = pe.forward(&input.view());

        assert!(output.is_ok());
        let output = output.expect("Forward pass should succeed");
        assert_eq!(output.dim(), (20, 64));

        // Output should equal positional encoding when input is zero
        for i in 0..20 {
            for j in 0..64 {
                assert_abs_diff_eq!(output[[i, j]], pe.encodings[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_feedforward_network() -> Result<(), Box<dyn std::error::Error>> {
        // Use minimal dimensions to avoid OOM in parallel testing
        let d_model = 8;
        let d_ff = 16;
        let seq_len = 4;

        let ffn = PositionwiseFeedForward::<f64>::new(d_model, d_ff, 0.0)?;
        assert_eq!(ffn.d_model, d_model);
        assert_eq!(ffn.d_ff, d_ff);

        let input = Array2::ones((seq_len, d_model));
        let output = ffn.forward(&input.view(), false)?;

        assert_eq!(output.dim(), (seq_len, d_model));

        // Verify output contains finite values
        for &val in output.iter() {
            assert!(val.is_finite(), "Output should contain finite values");
        }

        Ok(())
    }

    #[test]
    fn test_gelu_activation() {
        let ffn =
            PositionwiseFeedForward::<f64>::new(4, 16, 0.0).expect("FFN creation should succeed");

        let input = Array2::from_shape_vec((2, 4), vec![0.0, 1.0, -1.0, 2.0, -2.0, 0.5, -0.5, 1.5])
            .expect("Array creation should succeed");

        let output = ffn.gelu(&input.view());
        assert!(output.is_ok());

        let output = output.expect("GELU should succeed");
        // GELU(0) ≈ 0
        assert_abs_diff_eq!(output[[0, 0]], 0.0, epsilon = 0.01);
        // GELU is monotonic increasing
        assert!(output[[0, 1]] > output[[0, 0]]);
    }

    #[test]
    fn test_transformer_encoder_layer() {
        // Use smaller dimensions to avoid OOM in parallel testing
        let layer = TransformerEncoderLayer::<f64>::new(16, 4, 64, 0.0);
        assert!(layer.is_ok());

        let layer = layer.expect("EncoderLayer creation should succeed");
        let input = Array2::ones((5, 16));
        let output = layer.forward(&input.view(), None, false);

        assert!(output.is_ok());
        let output = output.expect("Forward pass should succeed");
        assert_eq!(output.dim(), (5, 16));
    }

    #[test]
    fn test_transformer_decoder_layer() {
        // Use smaller dimensions to avoid OOM in parallel testing
        let layer = TransformerDecoderLayer::<f64>::new(16, 4, 64, 0.0);
        assert!(layer.is_ok());

        let layer = layer.expect("DecoderLayer creation should succeed");
        let tgt_input = Array2::ones((5, 16));
        let src_input = Array2::ones((8, 16));

        let output = layer.forward(&tgt_input.view(), &src_input.view(), None, None, false);

        assert!(output.is_ok());
        let output = output.expect("Forward pass should succeed");
        assert_eq!(output.dim(), (5, 16));
    }

    #[test]
    fn test_transformer_encoder() {
        // Use smaller dimensions to avoid OOM in parallel testing
        let encoder = TransformerEncoder::<f64>::new(3, 16, 4, 64, 0.0);
        assert!(encoder.is_ok());

        let encoder = encoder.expect("Encoder creation should succeed");
        assert_eq!(encoder.num_layers, 3);

        let input = Array2::ones((5, 16));
        let output = encoder.forward(&input.view(), None, false);

        assert!(output.is_ok());
        let output = output.expect("Forward pass should succeed");
        assert_eq!(output.dim(), (5, 16));
    }

    #[test]
    fn test_transformer_decoder() {
        // Use smaller dimensions to avoid OOM in parallel testing
        let decoder = TransformerDecoder::<f64>::new(3, 16, 4, 64, 0.0);
        assert!(decoder.is_ok());

        let decoder = decoder.expect("Decoder creation should succeed");
        assert_eq!(decoder.num_layers, 3);

        let tgt = Array2::ones((5, 16));
        let memory = Array2::ones((8, 16));
        let output = decoder.forward(&tgt.view(), &memory.view(), None, None, false);

        assert!(output.is_ok());
        let output = output.expect("Forward pass should succeed");
        assert_eq!(output.dim(), (5, 16));
    }

    #[test]
    fn test_causal_mask() {
        let mask = create_causal_mask::<f64>(5);
        assert_eq!(mask.dim(), (5, 5));

        // Check lower triangular structure
        for i in 0..5 {
            for j in 0..5 {
                if j <= i {
                    assert_eq!(mask[[i, j]], 1.0);
                } else {
                    assert_eq!(mask[[i, j]], 0.0);
                }
            }
        }
    }

    #[test]
    fn test_attention_weights_sum_to_one() {
        let mha = MultiHeadAttention::<f64>::new(64, 4, 0.0)
            .expect("MultiHeadAttention creation should succeed");

        let q = Array2::ones((5, 64));
        let k = Array2::ones((5, 64));
        let v = Array2::ones((5, 64));

        let output = mha.scaled_dot_product_attention(&q.view(), &k.view(), &v.view(), None, false);
        assert!(output.is_ok());

        // With uniform inputs, attention should distribute equally
        let output = output.expect("Attention should succeed");
        assert_eq!(output.dim(), (5, 64));
    }

    #[test]
    fn test_layer_normalization_properties() {
        let layer = TransformerEncoderLayer::<f64>::new(64, 4, 256, 0.0)
            .expect("EncoderLayer creation should succeed");

        let input = Array2::from_shape_fn((10, 64), |(i, j)| (i * 64 + j) as f64);
        let gamma = Array1::ones(64);
        let beta = Array1::zeros(64);

        let normalized = layer.layer_norm(&input.view(), &gamma, &beta);
        assert!(normalized.is_ok());

        let normalized = normalized.expect("Layer norm should succeed");

        // Each row should have approximately zero mean and unit variance
        for i in 0..10 {
            let row = normalized.row(i);
            let mean = row.sum() / 64.0;
            let var = row.mapv(|v| (v - mean) * (v - mean)).sum() / 64.0;

            assert_abs_diff_eq!(mean, 0.0, epsilon = 1e-5);
            assert_abs_diff_eq!(var, 1.0, epsilon = 1e-4);
        }
    }

    #[test]
    fn test_masked_attention() {
        let mha = MultiHeadAttention::<f64>::new(64, 4, 0.0)
            .expect("MultiHeadAttention creation should succeed");

        let input = Array2::ones((5, 64));
        let mask = create_causal_mask::<f64>(5);

        let output = mha.forward(&input.view(), Some(&mask.view()), false);
        assert!(output.is_ok());

        let output = output.expect("Masked attention should succeed");
        assert_eq!(output.dim(), (5, 64));
    }
}