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
//! Normalization layers implementation
//! 正規化層の実装

use crate::autograd::Variable;
use crate::nn::Module;
use crate::tensor::Tensor;
use ndarray::ScalarOperand;
use num_traits::{Float, FromPrimitive, One, ToPrimitive, Zero};
use std::fmt::Debug;
use std::iter::Sum;

/// Layer Normalization
/// レイヤー正規化
///
/// Normalizes across the feature dimension for each sample independently.
/// 各サンプルについて特徴量次元で独立して正規化します。
#[derive(Debug)]
pub struct LayerNorm<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
> {
    /// Learnable scale parameter (gamma)
    /// 学習可能なスケールパラメータ(ガンマ)
    weight: Variable<T>,

    /// Learnable shift parameter (beta)
    /// 学習可能なシフトパラメータ(ベータ)
    bias: Variable<T>,

    /// Normalized shape (dimensions to normalize over)
    /// 正規化形状(正規化する次元)
    normalized_shape: Vec<usize>,

    /// Small value added to variance for numerical stability
    /// 数値安定性のため分散に加える小さな値
    eps: T,

    /// Whether to use learnable affine transformation
    /// 学習可能なアフィン変換を使用するかどうか
    elementwise_affine: bool,
}

impl<T> LayerNorm<T>
where
    T: Float
        + Debug
        + Default
        + FromPrimitive
        + ToPrimitive
        + Zero
        + One
        + 'static
        + Send
        + Sync
        + Copy
        + ndarray::ScalarOperand,
{
    /// Creates a new LayerNorm layer
    /// 新しいLayerNorm層を作成します
    pub fn new(
        normalized_shape: Vec<usize>,
        eps: Option<T>,
        elementwise_affine: Option<bool>,
    ) -> Self {
        assert!(
            !normalized_shape.is_empty(),
            "normalized_shape cannot be empty"
        );

        let eps = eps.unwrap_or_else(|| T::from_f32(1e-5).unwrap());
        let elementwise_affine = elementwise_affine.unwrap_or(true);

        let num_features: usize = normalized_shape.iter().product();

        // Initialize weight (gamma) to ones
        let weight_data = vec![T::one(); num_features];
        let weight = Variable::new(
            Tensor::from_vec(weight_data, normalized_shape.clone()),
            elementwise_affine,
        );

        // Initialize bias (beta) to zeros
        let bias_data = vec![T::zero(); num_features];
        let bias = Variable::new(
            Tensor::from_vec(bias_data, normalized_shape.clone()),
            elementwise_affine,
        );

        LayerNorm {
            weight,
            bias,
            normalized_shape,
            eps,
            elementwise_affine,
        }
    }

    /// Forward pass of LayerNorm
    /// LayerNormの順伝播
    pub fn forward(&self, input: &Variable<T>) -> Variable<T> {
        let input_binding = input.data();
        let input_data = input_binding.read().unwrap();
        let input_shape = input_data.shape();

        // Verify input shape compatibility
        self.verify_input_shape(input_shape);

        // Calculate normalization
        let normalized_data = self.layer_normalize(&input_data);

        let requires_grad = input.requires_grad()
            || (self.elementwise_affine
                && (self.weight.requires_grad() || self.bias.requires_grad()));

        Variable::new(normalized_data, requires_grad)
    }

    /// Verify input shape compatibility
    /// 入力形状の互換性を確認
    fn verify_input_shape(&self, input_shape: &[usize]) {
        let norm_dims = self.normalized_shape.len();
        let input_dims = input_shape.len();

        if input_dims < norm_dims {
            panic!(
                "Input has {} dimensions but normalized_shape has {} dimensions",
                input_dims, norm_dims
            );
        }

        // Check that the last norm_dims dimensions match
        let input_suffix = &input_shape[input_dims - norm_dims..];
        if input_suffix != self.normalized_shape.as_slice() {
            panic!(
                "Input shape suffix {:?} doesn't match normalized_shape {:?}",
                input_suffix, self.normalized_shape
            );
        }
    }

    /// Perform layer normalization
    /// レイヤー正規化を実行
    fn layer_normalize(&self, input: &Tensor<T>) -> Tensor<T> {
        let input_array = input.as_array();
        let input_shape = input.shape();
        let norm_dims = self.normalized_shape.len();
        let input_dims = input_shape.len();

        // Calculate batch dimensions (dimensions before normalized dimensions)
        let batch_dims = input_dims - norm_dims;
        let batch_size: usize = input_shape[..batch_dims].iter().product();
        let feature_size: usize = self.normalized_shape.iter().product();

        let mut output_data = Vec::with_capacity(input_array.len());

        // Process each batch element
        for batch_idx in 0..batch_size {
            // Extract features for this batch element
            let mut features = Vec::with_capacity(feature_size);

            for feat_idx in 0..feature_size {
                let linear_idx = batch_idx * feature_size + feat_idx;
                if let Some(slice) = input_array.as_slice() {
                    features.push(slice[linear_idx]);
                } else {
                    // Fallback for non-contiguous arrays
                    let indices = self.unravel_index(linear_idx, input_shape);
                    features.push(input_array[indices.as_slice()]);
                }
            }

            // Calculate mean and variance for this batch element
            let mean = self.calculate_mean(&features);
            let variance = self.calculate_variance(&features, mean);
            let std = (variance + self.eps).sqrt();

            // Normalize and apply affine transformation
            for (feat_idx, &feature_val) in features.iter().enumerate() {
                let normalized = (feature_val - mean) / std;

                let final_val = if self.elementwise_affine {
                    let weight_binding = self.weight.data();
                    let weight_data = weight_binding.read().unwrap();
                    let bias_binding = self.bias.data();
                    let bias_data = bias_binding.read().unwrap();

                    let weight_indices = self.unravel_index(feat_idx, &self.normalized_shape);
                    let bias_indices = weight_indices.clone();

                    let gamma = weight_data.as_array()[weight_indices.as_slice()];
                    let beta = bias_data.as_array()[bias_indices.as_slice()];

                    gamma * normalized + beta
                } else {
                    normalized
                };

                output_data.push(final_val);
            }
        }

        Tensor::from_vec(output_data, input_shape.to_vec())
    }

    /// Calculate mean of features
    /// 特徴量の平均を計算
    fn calculate_mean(&self, features: &[T]) -> T {
        let sum: T = features.iter().fold(T::zero(), |acc, &x| acc + x);
        sum / T::from_usize(features.len()).unwrap()
    }

    /// Calculate variance of features
    /// 特徴量の分散を計算
    fn calculate_variance(&self, features: &[T], mean: T) -> T {
        let sum_sq_diff: T = features
            .iter()
            .fold(T::zero(), |acc, &x| acc + (x - mean).powi(2));
        sum_sq_diff / T::from_usize(features.len()).unwrap()
    }

    /// Convert linear index to multi-dimensional indices
    /// 線形インデックスを多次元インデックスに変換
    fn unravel_index(&self, mut index: usize, shape: &[usize]) -> Vec<usize> {
        let mut indices = vec![0; shape.len()];

        for i in (0..shape.len()).rev() {
            indices[i] = index % shape[i];
            index /= shape[i];
        }

        indices
    }

    /// Returns the normalized shape
    /// 正規化形状を返します
    pub fn normalized_shape(&self) -> &[usize] {
        &self.normalized_shape
    }

    /// Returns the epsilon value
    /// イプシロン値を返します
    pub fn eps(&self) -> T {
        self.eps
    }

    /// Returns whether elementwise affine is enabled
    /// 要素ごとのアフィン変換が有効かどうかを返します
    pub fn elementwise_affine(&self) -> bool {
        self.elementwise_affine
    }

    /// Returns the parameters of the layer
    /// レイヤーのパラメータを返します
    pub fn parameters(&self) -> Vec<Variable<T>> {
        if self.elementwise_affine {
            vec![self.weight.clone(), self.bias.clone()]
        } else {
            vec![]
        }
    }
}

impl<T> Module<T> for LayerNorm<T>
where
    T: Float
        + Debug
        + Default
        + FromPrimitive
        + ToPrimitive
        + Zero
        + One
        + 'static
        + Send
        + Sync
        + Copy
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        self.forward(input)
    }

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

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

/// Group Normalization
/// グループ正規化
///
/// Normalizes features by dividing channels into groups and normalizing within each group.
/// チャンネルをグループに分割し、各グループ内で正規化します。
#[derive(Debug)]
pub struct GroupNorm<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
> {
    /// Learnable scale parameter (gamma)
    /// 学習可能なスケールパラメータ(ガンマ)
    weight: Variable<T>,

    /// Learnable shift parameter (beta)
    /// 学習可能なシフトパラメータ(ベータ)
    bias: Variable<T>,

    /// Number of groups
    /// グループ数
    num_groups: usize,

    /// Number of channels
    /// チャンネル数
    num_channels: usize,

    /// Small value added to variance for numerical stability
    /// 数値安定性のため分散に加える小さな値
    eps: T,

    /// Whether to use learnable affine transformation
    /// 学習可能なアフィン変換を使用するかどうか
    affine: bool,
}

impl<T> GroupNorm<T>
where
    T: Float
        + Debug
        + Default
        + FromPrimitive
        + ToPrimitive
        + Zero
        + One
        + 'static
        + Send
        + Sync
        + Copy
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive,
{
    /// Creates a new GroupNorm layer
    /// 新しいGroupNorm層を作成します
    pub fn new(
        num_groups: usize,
        num_channels: usize,
        eps: Option<T>,
        affine: Option<bool>,
    ) -> Self {
        assert!(num_groups > 0, "num_groups must be greater than 0");
        assert!(num_channels > 0, "num_channels must be greater than 0");
        assert!(
            num_channels % num_groups == 0,
            "num_channels ({}) must be divisible by num_groups ({})",
            num_channels,
            num_groups
        );

        let eps = eps.unwrap_or_else(|| T::from_f32(1e-5).unwrap());
        let affine = affine.unwrap_or(true);

        // Initialize weight (gamma) to ones
        let weight_data = vec![T::one(); num_channels];
        let weight = Variable::new(Tensor::from_vec(weight_data, vec![num_channels]), affine);

        // Initialize bias (beta) to zeros
        let bias_data = vec![T::zero(); num_channels];
        let bias = Variable::new(Tensor::from_vec(bias_data, vec![num_channels]), affine);

        GroupNorm {
            weight,
            bias,
            num_groups,
            num_channels,
            eps,
            affine,
        }
    }

    /// Forward pass of GroupNorm
    /// GroupNormの順伝播
    pub fn forward(&self, input: &Variable<T>) -> Variable<T> {
        let input_binding = input.data();
        let input_data = input_binding.read().unwrap();
        let input_shape = input_data.shape();

        // Input should be (N, C, H, W) for 4D or (N, C, L) for 3D
        if input_shape.len() < 3 {
            panic!(
                "GroupNorm expects at least 3D input (N, C, ...), got {:?}",
                input_shape
            );
        }

        let _batch_size = input_shape[0];
        let channels = input_shape[1];

        if channels != self.num_channels {
            panic!(
                "Input channels {} doesn't match layer channels {}",
                channels, self.num_channels
            );
        }

        // Calculate normalization
        let normalized_data = self.group_normalize(&input_data);

        let requires_grad = input.requires_grad()
            || (self.affine && (self.weight.requires_grad() || self.bias.requires_grad()));

        Variable::new(normalized_data, requires_grad)
    }

    /// Perform group normalization
    /// グループ正規化を実行
    fn group_normalize(&self, input: &Tensor<T>) -> Tensor<T> {
        let input_array = input.as_array();
        let input_shape = input.shape();
        let batch_size = input_shape[0];
        let channels = input_shape[1];
        let spatial_size: usize = input_shape[2..].iter().product();

        let channels_per_group = channels / self.num_groups;
        let group_size = channels_per_group * spatial_size;

        let mut output_data = Vec::with_capacity(input_array.len());

        // Process each batch element
        for b in 0..batch_size {
            // Process each group
            for g in 0..self.num_groups {
                let group_start_channel = g * channels_per_group;
                let group_end_channel = (g + 1) * channels_per_group;

                // Collect all values in this group
                let mut group_values = Vec::with_capacity(group_size);

                for c in group_start_channel..group_end_channel {
                    for _s in 0..spatial_size {
                        let mut indices = vec![b, c];
                        let spatial_indices = self.unravel_spatial_index(_s, &input_shape[2..]);
                        indices.extend(spatial_indices);

                        group_values.push(input_array[indices.as_slice()]);
                    }
                }

                // Calculate group statistics
                let mean = self.calculate_mean(&group_values);
                let variance = self.calculate_variance(&group_values, mean);
                let std = (variance + self.eps).sqrt();

                // Normalize and apply affine transformation for this group
                let mut value_idx = 0;
                for c in group_start_channel..group_end_channel {
                    for _s in 0..spatial_size {
                        let normalized = (group_values[value_idx] - mean) / std;

                        let final_val = if self.affine {
                            let weight_binding = self.weight.data();
                            let weight_data = weight_binding.read().unwrap();
                            let bias_binding = self.bias.data();
                            let bias_data = bias_binding.read().unwrap();

                            let gamma = weight_data.as_array()[[c]];
                            let beta = bias_data.as_array()[[c]];

                            gamma * normalized + beta
                        } else {
                            normalized
                        };

                        output_data.push(final_val);
                        value_idx += 1;
                    }
                }
            }
        }

        Tensor::from_vec(output_data, input_shape.to_vec())
    }

    /// Convert spatial linear index to multi-dimensional indices
    /// 空間線形インデックスを多次元インデックスに変換
    fn unravel_spatial_index(&self, mut index: usize, spatial_shape: &[usize]) -> Vec<usize> {
        let mut indices = vec![0; spatial_shape.len()];

        for i in (0..spatial_shape.len()).rev() {
            indices[i] = index % spatial_shape[i];
            index /= spatial_shape[i];
        }

        indices
    }

    /// Calculate mean of values
    /// 値の平均を計算
    fn calculate_mean(&self, values: &[T]) -> T {
        let sum: T = values.iter().fold(T::zero(), |acc, &x| acc + x);
        sum / T::from_usize(values.len()).unwrap()
    }

    /// Calculate variance of values
    /// 値の分散を計算
    fn calculate_variance(&self, values: &[T], mean: T) -> T {
        let sum_sq_diff: T = values
            .iter()
            .fold(T::zero(), |acc, &x| acc + (x - mean).powi(2));
        sum_sq_diff / T::from_usize(values.len()).unwrap()
    }

    /// Returns the number of groups
    /// グループ数を返します
    pub fn num_groups(&self) -> usize {
        self.num_groups
    }

    /// Returns the number of channels
    /// チャンネル数を返します
    pub fn num_channels(&self) -> usize {
        self.num_channels
    }

    /// Returns the epsilon value
    /// イプシロン値を返します
    pub fn eps(&self) -> T {
        self.eps
    }

    /// Returns whether affine transformation is enabled
    /// アフィン変換が有効かどうかを返します
    pub fn affine(&self) -> bool {
        self.affine
    }

    /// Returns the parameters of the layer
    /// レイヤーのパラメータを返します
    pub fn parameters(&self) -> Vec<Variable<T>> {
        if self.affine {
            vec![self.weight.clone(), self.bias.clone()]
        } else {
            vec![]
        }
    }
}

/// RMS Normalization (Root Mean Square Normalization)
/// RMS正規化(二乗平均平方根正規化)
///
/// A simplified normalization that only uses RMS, without centering.
/// 中心化せずにRMSのみを使用する簡略化された正規化。
#[derive(Debug)]
pub struct RMSNorm<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
> {
    /// Learnable scale parameter
    /// 学習可能なスケールパラメータ
    weight: Variable<T>,

    /// Normalized shape (dimensions to normalize over)
    /// 正規化形状(正規化する次元)
    normalized_shape: Vec<usize>,

    /// Small value added to variance for numerical stability
    /// 数値安定性のため分散に加える小さな値
    eps: T,
}

impl<T> RMSNorm<T>
where
    T: Float
        + Debug
        + Default
        + FromPrimitive
        + ToPrimitive
        + Zero
        + One
        + 'static
        + Send
        + Sync
        + Copy
        + ScalarOperand
        + Sum
        + std::fmt::Display,
{
    /// Creates a new RMSNorm layer
    /// 新しいRMSNorm層を作成します
    pub fn new(normalized_shape: Vec<usize>, eps: Option<T>) -> Self {
        assert!(
            !normalized_shape.is_empty(),
            "normalized_shape cannot be empty"
        );

        let eps = eps.unwrap_or_else(|| T::from(1e-8).unwrap());
        let num_features: usize = normalized_shape.iter().product();

        // Initialize weight to ones
        let weight_data = vec![T::one(); num_features];
        let weight = Variable::new(
            Tensor::from_vec(weight_data, normalized_shape.clone()),
            true,
        );

        RMSNorm {
            weight,
            normalized_shape,
            eps,
        }
    }

    /// Forward pass of RMSNorm
    /// RMSNormの順伝播
    pub fn forward(&self, input: &Variable<T>) -> Variable<T> {
        let input_binding = input.data();
        let input_data = input_binding.read().unwrap();
        let input_shape = input_data.shape();

        // Verify input shape compatibility
        self.verify_input_shape(input_shape);

        // Calculate RMS normalization
        let normalized_data = self.rms_normalize(&input_data);

        let requires_grad = input.requires_grad() || self.weight.requires_grad();
        Variable::new(normalized_data, requires_grad)
    }

    /// Verify input shape compatibility
    /// 入力形状の互換性を確認
    fn verify_input_shape(&self, input_shape: &[usize]) {
        let norm_dims = self.normalized_shape.len();
        let input_dims = input_shape.len();

        if input_dims < norm_dims {
            panic!(
                "Input has {} dimensions but normalized_shape has {} dimensions",
                input_dims, norm_dims
            );
        }

        // Check that the last norm_dims dimensions match
        let input_suffix = &input_shape[input_dims - norm_dims..];
        if input_suffix != self.normalized_shape.as_slice() {
            panic!(
                "Input shape suffix {:?} doesn't match normalized_shape {:?}",
                input_suffix, self.normalized_shape
            );
        }
    }

    /// Perform RMS normalization
    /// RMS正規化を実行
    fn rms_normalize(&self, input: &Tensor<T>) -> Tensor<T> {
        let input_array = input.as_array();
        let input_shape = input.shape();
        let norm_dims = self.normalized_shape.len();
        let input_dims = input_shape.len();

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

        let mut output_data = Vec::with_capacity(input_array.len());

        // Process each batch element
        for batch_idx in 0..batch_size {
            // Extract features for this batch element
            let mut features = Vec::with_capacity(feature_size);

            for feat_idx in 0..feature_size {
                let linear_idx = batch_idx * feature_size + feat_idx;
                if let Some(slice) = input_array.as_slice() {
                    features.push(slice[linear_idx]);
                } else {
                    // Fallback for non-contiguous arrays
                    let indices = self.unravel_index(linear_idx, input_shape);
                    features.push(input_array[indices.as_slice()]);
                }
            }

            // Calculate RMS for this batch element
            let mean_square: T = features.iter().fold(T::zero(), |acc, &x| acc + x.powi(2))
                / T::from_usize(features.len()).unwrap();
            let rms = (mean_square + self.eps).sqrt();

            // Normalize and apply scale
            for (feat_idx, &feature_val) in features.iter().enumerate() {
                let normalized = feature_val / rms;

                let weight_binding = self.weight.data();
                let weight_data = weight_binding.read().unwrap();
                let weight_indices = self.unravel_index(feat_idx, &self.normalized_shape);
                let gamma = weight_data.as_array()[weight_indices.as_slice()];

                let final_val = gamma * normalized;
                output_data.push(final_val);
            }
        }

        Tensor::from_vec(output_data, input_shape.to_vec())
    }

    /// Convert linear index to multi-dimensional indices
    /// 線形インデックスを多次元インデックスに変換
    fn unravel_index(&self, mut index: usize, shape: &[usize]) -> Vec<usize> {
        let mut indices = vec![0; shape.len()];

        for i in (0..shape.len()).rev() {
            indices[i] = index % shape[i];
            index /= shape[i];
        }

        indices
    }

    /// Returns the epsilon value
    /// イプシロン値を返します
    pub fn eps(&self) -> T {
        self.eps
    }

    /// Returns the parameters of the layer
    /// レイヤーのパラメータを返します
    pub fn parameters(&self) -> Vec<Variable<T>> {
        vec![self.weight.clone()]
    }
}

impl<T> Module<T> for GroupNorm<T>
where
    T: Float
        + Debug
        + Default
        + FromPrimitive
        + ToPrimitive
        + Zero
        + One
        + 'static
        + Send
        + Sync
        + Copy
        + ScalarOperand
        + Sum
        + std::fmt::Display
        + num_traits::FromPrimitive,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        self.forward(input)
    }

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

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

impl<T> Module<T> for RMSNorm<T>
where
    T: Float
        + Debug
        + Default
        + FromPrimitive
        + ToPrimitive
        + Zero
        + One
        + 'static
        + Send
        + Sync
        + Copy
        + ScalarOperand
        + Sum
        + std::fmt::Display,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        self.forward(input)
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        vec![self.weight.clone()]
    }

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

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

    #[test]
    fn test_layer_norm_creation() {
        let layer_norm = LayerNorm::<f32>::new(vec![128], None, None);

        assert_eq!(layer_norm.normalized_shape(), &[128]);
        assert!(layer_norm.elementwise_affine());

        let params = layer_norm.parameters();
        assert_eq!(params.len(), 2); // weight and bias
    }

    #[test]
    fn test_layer_norm_forward() {
        let layer_norm = LayerNorm::<f32>::new(vec![4], None, None);

        // Create input: batch_size=2, features=4
        let input = Variable::new(
            Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], vec![2, 4]),
            false,
        );

        let output = layer_norm.forward(&input);
        let output_binding = output.data();
        let output_data = output_binding.read().unwrap();

        assert_eq!(output_data.shape(), &[2, 4]);
    }

    #[test]
    fn test_group_norm_creation() {
        let group_norm = GroupNorm::<f32>::new(2, 8, None, None);

        assert_eq!(group_norm.num_groups, 2);
        assert_eq!(group_norm.num_channels, 8);
        assert!(group_norm.affine);

        let params = group_norm.parameters();
        assert_eq!(params.len(), 2); // weight and bias
    }

    #[test]
    fn test_rms_norm_creation() {
        let rms_norm = RMSNorm::<f32>::new(vec![64], None);

        assert_eq!(rms_norm.normalized_shape, vec![64]);

        let params = rms_norm.parameters();
        assert_eq!(params.len(), 1); // only weight, no bias
    }
}