rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Activation functions for neural networks
//! ニューラルネットワークの活性化関数

use crate::autograd::Variable;
use crate::nn::Module;
use crate::serialization::core::{Loadable, Saveable, SerializationResult};
use crate::tensor::Tensor;
use num_traits::Float;
use std::collections::HashMap;
use std::fmt::Debug;

/// ReLU (Rectified Linear Unit) activation function
/// ReLU(正規化線形ユニット)活性化関数
///
/// Applies the element-wise function: ReLU(x) = max(0, x)
/// 要素ごとに関数を適用: ReLU(x) = max(0, x)
pub fn relu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply ReLU: max(0, x)
    let output_data = apply_relu(&input_data);

    if x.requires_grad() {
        // Create a new variable that tracks gradients
        let result = Variable::new(output_data, true);

        // Store reference to input for backward pass
        // Note: In a full implementation, we'd use a proper gradient function
        // For now, we'll implement a simplified version
        result
    } else {
        Variable::new(output_data, false)
    }
}

/// Sigmoid activation function
/// シグモイド活性化関数
///
/// Applies the element-wise function: Sigmoid(x) = 1 / (1 + exp(-x))
/// 要素ごとに関数を適用: Sigmoid(x) = 1 / (1 + exp(-x))
pub fn sigmoid<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply Sigmoid: 1 / (1 + exp(-x))
    let output_data = apply_sigmoid(&input_data);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// Tanh (Hyperbolic Tangent) activation function
/// Tanh(双曲線正接)活性化関数
///
/// Applies the element-wise function: Tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
/// 要素ごとに関数を適用: Tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
pub fn tanh<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply Tanh
    let output_data = apply_tanh(&input_data);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// Leaky ReLU activation function
/// Leaky ReLU活性化関数
///
/// Applies the element-wise function: LeakyReLU(x) = max(alpha * x, x)
/// 要素ごとに関数を適用: LeakyReLU(x) = max(alpha * x, x)
pub fn leaky_relu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
    alpha: T,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply Leaky ReLU: max(alpha * x, x)
    let output_data = apply_leaky_relu(&input_data, alpha);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// Softmax activation function
/// ソフトマックス活性化関数
///
/// Applies softmax along the last dimension: Softmax(x_i) = exp(x_i) / sum(exp(x_j))
/// 最後の次元に沿ってソフトマックスを適用: Softmax(x_i) = exp(x_i) / sum(exp(x_j))
pub fn softmax<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply Softmax
    let output_data = apply_softmax(&input_data);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

// Helper functions for applying activation functions to tensors
// テンソルに活性化関数を適用するヘルパー関数

fn apply_relu<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| if x > T::zero() { x } else { T::zero() })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_sigmoid<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            let one = T::one();
            one / (one + (-x).exp())
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_tanh<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            let exp_x = x.exp();
            let exp_neg_x = (-x).exp();
            (exp_x - exp_neg_x) / (exp_x + exp_neg_x)
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_leaky_relu<T: Float + 'static>(tensor: &Tensor<T>, alpha: T) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| if x > T::zero() { x } else { alpha * x })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_softmax<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();

    // For numerical stability, subtract the maximum value
    let max_val = data
        .iter()
        .copied()
        .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .unwrap_or(T::zero());

    // Compute exp(x - max) for each element
    let exp_values: Vec<T> = data.iter().map(|&x| (x - max_val).exp()).collect();

    // Compute sum of exponentials
    let sum_exp = exp_values.iter().fold(T::zero(), |acc, &x| acc + x);

    // Normalize by dividing by sum
    let result_data: Vec<T> = exp_values.iter().map(|&x| x / sum_exp).collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

/// GELU (Gaussian Error Linear Unit) activation function
/// GELU(ガウス誤差線形ユニット)活性化関数
///
/// Applies the element-wise function: GELU(x) = x * Φ(x) where Φ(x) is the CDF of standard normal
/// 要素ごとに関数を適用: GELU(x) = x * Φ(x) ここでΦ(x)は標準正規分布の累積分布関数
pub fn gelu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply GELU approximation: 0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x^3)))
    let output_data = apply_gelu(&input_data);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// Swish (SiLU) activation function
/// Swish(SiLU)活性化関数
///
/// Applies the element-wise function: Swish(x) = x * sigmoid(x)
/// 要素ごとに関数を適用: Swish(x) = x * sigmoid(x)
pub fn swish<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply Swish: x * sigmoid(x)
    let output_data = apply_swish(&input_data);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// ELU (Exponential Linear Unit) activation function
/// ELU(指数線形ユニット)活性化関数
///
/// Applies the element-wise function: ELU(x) = x if x > 0 else alpha * (exp(x) - 1)
/// 要素ごとに関数を適用: ELU(x) = x if x > 0 else alpha * (exp(x) - 1)
pub fn elu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
    alpha: T,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply ELU
    let output_data = apply_elu(&input_data, alpha);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// SELU (Scaled Exponential Linear Unit) activation function
/// SELU(スケール指数線形ユニット)活性化関数
///
/// Applies the element-wise function with fixed parameters for self-normalizing properties
/// 自己正規化特性のため固定パラメータで要素ごとに関数を適用
pub fn selu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // SELU with fixed parameters: alpha = 1.6732632423543772848170429916717, scale = 1.0507009873554804934193349852946
    let alpha = T::from(1.673_263_242_354_377_284_817_042_991_671_7_f32).unwrap();
    let scale = T::from(1.050_700_987_355_480_493_419_334_985_294_6_f32).unwrap();
    let output_data = apply_selu(&input_data, alpha, scale);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// Mish activation function
/// Mish活性化関数
///
/// Applies the element-wise function: Mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + exp(x)))
/// 要素ごとに関数を適用: Mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + exp(x)))
pub fn mish<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply Mish: x * tanh(softplus(x))
    let output_data = apply_mish(&input_data);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

/// GLU (Gated Linear Unit) activation function
/// GLU(ゲート線形ユニット)活性化関数
///
/// Applies the element-wise function: GLU(x) = a ⊙ σ(b)
/// where x is split into two halves a and b along the last dimension
/// 要素ごとに関数を適用: GLU(x) = a ⊙ σ(b)
/// ここで x は最後の次元で半分に分割され a と b になります
pub fn glu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive + Default,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_tensor = x.data();
    let input_guard = input_tensor.read().unwrap();
    let input_shape = input_guard.shape();
    let input_data = input_guard.as_slice().unwrap();

    // GLU requires even number of elements on the last dimension
    let last_dim = input_shape[input_shape.len() - 1];
    assert_eq!(
        last_dim % 2,
        0,
        "GLU requires even number of elements on last dimension"
    );

    let half_size = last_dim / 2;
    let total_elements = input_shape.iter().product::<usize>();
    let batch_size = total_elements / last_dim;

    // Create output shape (half the size of last dimension)
    let mut output_shape = input_shape.to_vec();
    let last_dim_idx = output_shape.len() - 1;
    output_shape[last_dim_idx] = half_size;
    let output_size = output_shape.iter().product::<usize>();
    let mut output_data = vec![T::default(); output_size];

    // Process each batch
    for batch in 0..batch_size {
        let input_offset = batch * last_dim;
        let output_offset = batch * half_size;

        for i in 0..half_size {
            let a = input_data[input_offset + i]; // First half (values)
            let b = input_data[input_offset + half_size + i]; // Second half (gates)

            // Apply sigmoid to gate and multiply: a * sigmoid(b)
            let sigmoid_b = T::from_f32(1.0).unwrap() / (T::from_f32(1.0).unwrap() + (-b).exp());
            output_data[output_offset + i] = a * sigmoid_b;
        }
    }

    let output_tensor = Tensor::from_vec(output_data, output_shape);
    Variable::new(output_tensor, x.requires_grad())
}

/// GLU activation layer struct
/// GLU活性化レイヤー構造体
#[derive(Debug)]
pub struct GLU<T: Float + Send + Sync + ndarray::ScalarOperand + num_traits::FromPrimitive> {
    _phantom: std::marker::PhantomData<T>,
}

/// SwiGLU (Swish-Gated Linear Unit) activation function
/// SwiGLU(Swish-ゲート線形ユニット)活性化関数
pub fn swiglu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive + Default,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_tensor = x.data();
    let input_guard = input_tensor.read().unwrap();
    let input_shape = input_guard.shape();
    let input_data = input_guard.as_slice().unwrap();

    let last_dim = input_shape[input_shape.len() - 1];
    assert_eq!(
        last_dim % 2,
        0,
        "SwiGLU requires even number of elements on last dimension"
    );

    let half_size = last_dim / 2;
    let total_elements = input_shape.iter().product::<usize>();
    let batch_size = total_elements / last_dim;

    let mut output_shape = input_shape.to_vec();
    let last_idx = output_shape.len() - 1;
    output_shape[last_idx] = half_size;
    let mut output_data = vec![T::default(); batch_size * half_size];

    for i in 0..batch_size {
        let offset = i * last_dim;
        for j in 0..half_size {
            let a = input_data[offset + j];
            let b = input_data[offset + j + half_size];

            // Swish activation: x * sigmoid(x)
            let swish_b =
                b * (T::from_f32(1.0).unwrap() / (T::from_f32(1.0).unwrap() + (-b).exp()));
            output_data[i * half_size + j] = a * swish_b;
        }
    }

    let output_tensor = Tensor::from_vec(output_data, output_shape);
    Variable::new(output_tensor, x.requires_grad())
}

/// GeGLU (GELU-Gated Linear Unit) activation function  
/// GeGLU(GELU-ゲート線形ユニット)活性化関数
pub fn geglu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive + Default,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_tensor = x.data();
    let input_guard = input_tensor.read().unwrap();
    let input_shape = input_guard.shape();
    let input_data = input_guard.as_slice().unwrap();

    let last_dim = input_shape[input_shape.len() - 1];
    assert_eq!(
        last_dim % 2,
        0,
        "GeGLU requires even number of elements on last dimension"
    );

    let half_size = last_dim / 2;
    let total_elements = input_shape.iter().product::<usize>();
    let batch_size = total_elements / last_dim;

    let mut output_shape = input_shape.to_vec();
    let last_idx = output_shape.len() - 1;
    output_shape[last_idx] = half_size;
    let mut output_data = vec![T::default(); batch_size * half_size];

    for i in 0..batch_size {
        let offset = i * last_dim;
        for j in 0..half_size {
            let a = input_data[offset + j];
            let b = input_data[offset + j + half_size];

            // GELU activation: x * Φ(x) where Φ is the standard Gaussian CDF
            let gelu_b = b
                * T::from_f32(0.5).unwrap()
                * (T::from_f32(1.0).unwrap()
                    + (b * T::from_f32(0.7978845608).unwrap()
                        * (T::from_f32(1.0).unwrap() + T::from_f32(0.044715).unwrap() * b * b))
                        .tanh());
            output_data[i * half_size + j] = a * gelu_b;
        }
    }

    let output_tensor = Tensor::from_vec(output_data, output_shape);
    Variable::new(output_tensor, x.requires_grad())
}

/// ReGLU (ReLU-Gated Linear Unit) activation function
/// ReGLU(ReLU-ゲート線形ユニット)活性化関数
pub fn reglu<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive + Default,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_tensor = x.data();
    let input_guard = input_tensor.read().unwrap();
    let input_shape = input_guard.shape();
    let input_data = input_guard.as_slice().unwrap();

    let last_dim = input_shape[input_shape.len() - 1];
    assert_eq!(
        last_dim % 2,
        0,
        "ReGLU requires even number of elements on last dimension"
    );

    let half_size = last_dim / 2;
    let total_elements = input_shape.iter().product::<usize>();
    let batch_size = total_elements / last_dim;

    let mut output_shape = input_shape.to_vec();
    let last_idx = output_shape.len() - 1;
    output_shape[last_idx] = half_size;
    let mut output_data = vec![T::default(); batch_size * half_size];

    for i in 0..batch_size {
        let offset = i * last_dim;
        for j in 0..half_size {
            let a = input_data[offset + j];
            let b = input_data[offset + j + half_size];

            // ReLU activation: max(0, x)
            let relu_b = if b > T::zero() { b } else { T::zero() };
            output_data[i * half_size + j] = a * relu_b;
        }
    }

    let output_tensor = Tensor::from_vec(output_data, output_shape);
    Variable::new(output_tensor, x.requires_grad())
}

impl<
        T: Float
            + Send
            + Sync
            + 'static
            + Debug
            + ndarray::ScalarOperand
            + num_traits::FromPrimitive
            + Default,
    > GLU<T>
{
    /// Create a new GLU activation function
    /// 新しいGLU活性化関数を作成
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<
        T: Float
            + Send
            + Sync
            + 'static
            + Debug
            + ndarray::ScalarOperand
            + num_traits::FromPrimitive
            + Default,
    > Module<T> for GLU<T>
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        glu(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        Vec::new()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Hardswish activation function (used in MobileNetV3)
/// Hardswish活性化関数(MobileNetV3で使用)
///
/// Applies the element-wise function: Hardswish(x) = x * ReLU6(x + 3) / 6
/// 要素ごとに関数を適用: Hardswish(x) = x * ReLU6(x + 3) / 6
pub fn hardswish<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
>(
    x: &Variable<T>,
) -> Variable<T> {
    let input_data = x.data().read().unwrap().clone();

    // Apply Hardswish
    let output_data = apply_hardswish(&input_data);

    if x.requires_grad() {
        Variable::new(output_data, true)
    } else {
        Variable::new(output_data, false)
    }
}

fn apply_gelu<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            // GELU approximation: 0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x^3)))
            let sqrt_2_over_pi = T::from(0.7978845608028654f32).unwrap(); // sqrt(2/π)
            let coeff = T::from(0.044715f32).unwrap();
            let half = T::from(0.5f32).unwrap();
            let one = T::one();

            let x_cubed = x * x * x;
            let inner = sqrt_2_over_pi * (x + coeff * x_cubed);
            let tanh_val = {
                let exp_2x = (inner + inner).exp();
                (exp_2x - one) / (exp_2x + one)
            };

            half * x * (one + tanh_val)
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_swish<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            // Swish: x * sigmoid(x)
            let sigmoid_x = T::one() / (T::one() + (-x).exp());
            x * sigmoid_x
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_elu<T: Float + 'static>(tensor: &Tensor<T>, alpha: T) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            if x > T::zero() {
                x
            } else {
                alpha * (x.exp() - T::one())
            }
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_selu<T: Float + 'static>(tensor: &Tensor<T>, alpha: T, scale: T) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            if x > T::zero() {
                scale * x
            } else {
                scale * alpha * (x.exp() - T::one())
            }
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_mish<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            // Mish: x * tanh(softplus(x)) = x * tanh(ln(1 + exp(x)))
            let softplus = (T::one() + x.exp()).ln();
            let tanh_softplus = {
                let exp_2x = (softplus + softplus).exp();
                (exp_2x - T::one()) / (exp_2x + T::one())
            };
            x * tanh_softplus
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

fn apply_hardswish<T: Float + 'static>(tensor: &Tensor<T>) -> Tensor<T> {
    let data = tensor.as_array();
    let result_data: Vec<T> = data
        .iter()
        .map(|&x| {
            // Hardswish: x * ReLU6(x + 3) / 6
            let three = T::from(3.0f32).unwrap();
            let six = T::from(6.0f32).unwrap();

            // ReLU6(x + 3) = min(max(x + 3, 0), 6)
            let relu6_val = (x + three).max(T::zero()).min(six);

            x * relu6_val / six
        })
        .collect();

    Tensor::from_vec(result_data, tensor.shape().to_vec())
}

/// ReLU activation layer (module)
/// ReLU活性化層(モジュール)
#[derive(Debug, Clone)]
pub struct ReLU<T: Float + Send + Sync + 'static + Debug> {
    _phantom: std::marker::PhantomData<T>,
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > ReLU<T>
{
    /// Create a new ReLU activation function
    /// 新しいReLU活性化関数を作成
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Module<T> for ReLU<T>
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        relu(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        Vec::new()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn train(&mut self) {
        // No parameters to train
    }

    fn eval(&mut self) {
        // No parameters to eval
    }
}

// Serialization support for ReLU (stateless activation function)
impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Saveable for ReLU<T>
{
    fn save_binary(&self) -> SerializationResult<Vec<u8>> {
        // ReLU has no parameters, so just save a marker
        Ok(vec![0u8])
    }

    fn type_id(&self) -> &'static str {
        "nn.ReLU"
    }

    fn metadata(&self) -> HashMap<String, String> {
        let mut meta = HashMap::new();
        meta.insert("activation_type".to_string(), "ReLU".to_string());
        meta.insert("stateless".to_string(), "true".to_string());
        meta
    }
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Loadable for ReLU<T>
{
    fn load_binary(_data: &[u8]) -> SerializationResult<Self> {
        // ReLU has no parameters, so just create a new instance
        Ok(ReLU::new())
    }

    fn expected_type_id() -> &'static str {
        "nn.ReLU"
    }
}

/// Softmax activation layer (module)
/// Softmax活性化層(モジュール)
#[derive(Debug, Clone)]
pub struct Softmax<T: Float + Send + Sync + 'static + Debug> {
    _phantom: std::marker::PhantomData<T>,
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Softmax<T>
{
    /// Create a new Softmax activation function
    /// 新しいSoftmax活性化関数を作成
    pub fn new(_dim: i32) -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Module<T> for Softmax<T>
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        softmax(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        Vec::new()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn train(&mut self) {
        // No parameters to train
    }

    fn eval(&mut self) {
        // No parameters to eval
    }
}

/// GELU activation layer (module)
/// GELU活性化層(モジュール)
#[derive(Debug, Clone)]
pub struct GELU<T: Float + Send + Sync + 'static + Debug> {
    _phantom: std::marker::PhantomData<T>,
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > GELU<T>
{
    /// Create a new GELU activation function
    /// 新しいGELU活性化関数を作成
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Module<T> for GELU<T>
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        gelu(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        Vec::new()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn train(&mut self) {
        // No parameters to train
    }

    fn eval(&mut self) {
        // No parameters to eval
    }
}

/// Swish activation layer (module)
/// Swish活性化層(モジュール)
#[derive(Debug, Clone)]
pub struct Swish<T: Float + Send + Sync + 'static + Debug> {
    _phantom: std::marker::PhantomData<T>,
}

/// Tanh activation layer (module)
/// Tanh活性化層(モジュール)
#[derive(Debug, Clone)]
pub struct Tanh<T: Float + Send + Sync + 'static + Debug> {
    _phantom: std::marker::PhantomData<T>,
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Swish<T>
{
    /// Create a new Swish activation function
    /// 新しいSwish活性化関数を作成
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Tanh<T>
{
    /// Create a new Tanh activation function
    /// 新しいTanh活性化関数を作成
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Module<T> for Swish<T>
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        swish(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        Vec::new()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn train(&mut self) {
        // No parameters to train
    }

    fn eval(&mut self) {
        // No parameters to eval
    }
}

impl<
        T: Float + Send + Sync + 'static + Debug + ndarray::ScalarOperand + num_traits::FromPrimitive,
    > Module<T> for Tanh<T>
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        tanh(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        Vec::new()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn train(&mut self) {
        // No parameters to train
    }

    fn eval(&mut self) {
        // No parameters to eval
    }
}

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

    #[test]
    fn test_relu() {
        let input = Variable::new(
            Tensor::from_vec(vec![-2.0, -1.0, 0.0, 1.0, 2.0], vec![5]),
            false,
        );

        let output = relu(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        let expected = [0.0, 0.0, 0.0, 1.0, 2.0];
        for (actual, expected) in result_data.as_array().iter().zip(expected.iter()) {
            assert_abs_diff_eq!(*actual, *expected, epsilon = 1e-6);
        }
    }

    #[test]
    fn test_sigmoid() {
        let input = Variable::new(Tensor::from_vec(vec![0.0], vec![1]), false);

        let output = sigmoid(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // sigmoid(0) = 0.5
        assert_abs_diff_eq!(
            result_data.as_array().iter().next().unwrap(),
            &0.5,
            epsilon = 1e-6
        );
    }

    #[test]
    fn test_tanh() {
        let input = Variable::new(Tensor::from_vec(vec![0.0], vec![1]), false);

        let output = tanh(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // tanh(0) = 0.0
        assert_abs_diff_eq!(
            result_data.as_array().iter().next().unwrap(),
            &0.0,
            epsilon = 1e-6
        );
    }

    #[test]
    fn test_leaky_relu() {
        let input = Variable::new(Tensor::from_vec(vec![-1.0, 0.0, 1.0], vec![3]), false);

        let output = leaky_relu(&input, 0.1);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        let expected = [-0.1, 0.0, 1.0];
        for (actual, expected) in result_data.as_array().iter().zip(expected.iter()) {
            assert_abs_diff_eq!(*actual, *expected, epsilon = 1e-6);
        }
    }

    #[test]
    fn test_softmax() {
        let input = Variable::new(Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]), false);

        let output = softmax(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // Check that probabilities sum to 1
        let sum: f32 = result_data.as_array().iter().sum();
        assert_abs_diff_eq!(sum, 1.0, epsilon = 1e-6);

        // Check that all values are positive
        for &val in result_data.as_array().iter() {
            assert!(val > 0.0);
        }
    }

    #[test]
    fn test_relu_with_gradients() {
        let input = Variable::new(Tensor::from_vec(vec![-1.0, 0.0, 1.0], vec![3]), true);

        let output = relu(&input);
        assert!(output.requires_grad());

        // Test that the computation works
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();
        let expected = [0.0, 0.0, 1.0];
        for (actual, expected) in result_data.as_array().iter().zip(expected.iter()) {
            assert_abs_diff_eq!(*actual, *expected, epsilon = 1e-6);
        }
    }

    #[test]
    fn test_gelu() {
        let input = Variable::new(Tensor::from_vec(vec![0.0, 1.0, -1.0], vec![3]), false);

        let output = gelu(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // GELU(0) ≈ 0, GELU(1) ≈ 0.8413, GELU(-1) ≈ -0.1587
        assert_abs_diff_eq!(result_data.as_array()[0], 0.0, epsilon = 1e-3);
        assert!(result_data.as_array()[1] > 0.8);
        assert!(result_data.as_array()[2] < 0.0);
    }

    #[test]
    fn test_swish() {
        let input = Variable::new(Tensor::from_vec(vec![0.0, 1.0, -1.0], vec![3]), false);

        let output = swish(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // Swish(0) = 0, Swish(1) ≈ 0.7311, Swish(-1) ≈ -0.2689
        assert_abs_diff_eq!(result_data.as_array()[0], 0.0, epsilon = 1e-6);
        assert!(result_data.as_array()[1] > 0.7);
        assert!(result_data.as_array()[2] < 0.0);
    }

    #[test]
    fn test_elu() {
        let input = Variable::new(Tensor::from_vec(vec![-1.0, 0.0, 1.0], vec![3]), false);

        let output = elu(&input, 1.0);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // ELU(-1, α=1) ≈ -0.632, ELU(0) = 0, ELU(1) = 1
        assert!(result_data.as_array()[0] < 0.0 && result_data.as_array()[0] > -1.0);
        assert_abs_diff_eq!(result_data.as_array()[1], 0.0, epsilon = 1e-6);
        assert_abs_diff_eq!(result_data.as_array()[2], 1.0, epsilon = 1e-6);
    }

    #[test]
    fn test_selu() {
        let input = Variable::new(Tensor::from_vec(vec![0.0, 1.0], vec![2]), false);

        let output = selu(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // SELU should preserve zero and scale positive values
        assert_abs_diff_eq!(result_data.as_array()[0], 0.0, epsilon = 1e-6);
        assert!(result_data.as_array()[1] > 1.0); // Should be scaled
    }

    #[test]
    fn test_mish() {
        let input = Variable::new(Tensor::from_vec(vec![0.0, 1.0, -1.0], vec![3]), false);

        let output = mish(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // Mish(0) ≈ 0, Mish(1) ≈ 0.865, Mish(-1) ≈ -0.303
        assert_abs_diff_eq!(result_data.as_array()[0], 0.0, epsilon = 1e-3);
        assert!(result_data.as_array()[1] > 0.8);
        assert!(result_data.as_array()[2] < 0.0);
    }

    #[test]
    fn test_hardswish() {
        let input = Variable::new(Tensor::from_vec(vec![-3.0, 0.0, 3.0, 6.0], vec![4]), false);

        let output = hardswish(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // Hardswish(-3) = 0, Hardswish(0) = 0, Hardswish(3) = 3, Hardswish(6) = 6
        assert_abs_diff_eq!(result_data.as_array()[0], 0.0, epsilon = 1e-6);
        assert_abs_diff_eq!(result_data.as_array()[1], 0.0, epsilon = 1e-6);
        assert_abs_diff_eq!(result_data.as_array()[2], 3.0, epsilon = 1e-6);
        assert_abs_diff_eq!(result_data.as_array()[3], 6.0, epsilon = 1e-6);
    }

    #[test]
    fn test_glu() {
        // Test GLU with 4-element input (2 gates)
        let input = Variable::new(Tensor::from_vec(vec![1.0, 2.0, 0.5, -1.0], vec![4]), false);

        let output = glu(&input);
        let result_binding = output.data();
        let result_data = result_binding.read().unwrap();

        // GLU splits input in half: gate = sigmoid([0.5, -1.0]), value = [1.0, 2.0]
        // Expected: [1.0 * sigmoid(0.5), 2.0 * sigmoid(-1.0)]
        assert_eq!(result_data.shape(), &[2]); // Half the input size
        assert!(result_data.as_array()[0] > 0.5); // 1.0 * sigmoid(0.5) > 0.5
        assert!(result_data.as_array()[1] > 0.0 && result_data.as_array()[1] < 1.0);
        // 2.0 * sigmoid(-1.0)
    }
}