mlx_rs/nn/
normalization.rs

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
use std::borrow::Cow;

use crate::{
    array,
    error::Exception,
    module::{Module, Param},
    ops::{ones, rsqrt, zeros},
    Array,
};
use mlx_internal_macros::{Buildable, Builder};
use mlx_macros::ModuleParameters;

fn instance_norm(x: &Array, axes: &[i32], eps: &Array) -> Result<Array, Exception> {
    // Compute stats
    let mean = x.mean(axes, true)?;
    let variance = x.variance(axes, true, None)?;

    // Normalize
    let x = x.subtract(&mean)?.multiply(rsqrt(&variance.add(eps)?)?)?;

    Ok(x)
}

/// Builder for [`InstanceNorm`].
#[derive(Debug, Clone, Builder)]
#[builder(
    root = crate,
    build_with = build_instance_norm,
    err = Exception,
)]
pub struct InstanceNormBuilder {
    /// Number of features in the input
    pub dimensions: i32,

    /// Value added to the denominator for numerical stability. Default to
    /// [`InstanceNorm::DEFAULT_EPS`].
    #[builder(optional, default = InstanceNorm::DEFAULT_EPS)]
    pub eps: f32,

    /// If `true`, addes a trainable `weight` and `bias`. Default to
    /// [`InstanceNorm::DEFAULT_AFFINE`].
    #[builder(optional, default = InstanceNorm::DEFAULT_AFFINE)]
    pub affine: bool,
}

fn build_instance_norm(builder: InstanceNormBuilder) -> Result<InstanceNorm, Exception> {
    let eps = builder.eps;
    let affine = builder.affine;

    let (weight, bias) = if affine {
        (
            Some(ones::<f32>(&[builder.dimensions])?),
            Some(zeros::<f32>(&[builder.dimensions])?),
        )
    } else {
        (None, None)
    };

    Ok(InstanceNorm {
        dimensions: builder.dimensions,
        eps: array!(eps),
        weight: Param::new(weight),
        bias: Param::new(bias),
    })
}

/// Applies instance normalization [1] on the inputs.
///
/// ### References
///
/// 1. [https://arxiv.org/abs/1607.08022](https://arxiv.org/abs/1607.08022)
#[derive(Debug, Clone, ModuleParameters, Buildable)]
#[module(root = crate)]
#[buildable(root = crate)]
pub struct InstanceNorm {
    /// Number of features in the input
    pub dimensions: i32,

    /// Value added to the denominator for numerical stability.
    pub eps: Array,

    /// An optional trainable weight
    pub weight: Param<Option<Array>>,

    /// An optional trainable bias
    pub bias: Param<Option<Array>>,
}

impl InstanceNorm {
    /// Default value for `eps`.
    pub const DEFAULT_EPS: f32 = 1e-5;

    /// Disable trainable `weight` and `bias` by default.
    pub const DEFAULT_AFFINE: bool = false;
}

impl Module<&Array> for InstanceNorm {
    type Error = Exception;
    type Output = Array;

    fn forward(&mut self, x: &Array) -> Result<Array, Self::Error> {
        let reduction_axes = (1..x.ndim() as i32 - 1).collect::<Vec<_>>();

        let x = instance_norm(x, &reduction_axes, &self.eps)?;

        if let (Some(weight), Some(bias)) = (self.weight.as_ref(), self.bias.as_ref()) {
            weight.multiply(x)?.add(bias)
        } else {
            Ok(x)
        }
    }

    fn training_mode(&mut self, _mode: bool) {}
}

/// Builder for [`LayerNorm`].
#[derive(Debug, Clone, Builder)]
#[builder(
    root = crate,
    build_with = build_layer_norm,
    err = Exception,
)]
pub struct LayerNormBuilder {
    /// Number of features in the input
    pub dimensions: i32,

    /// Value added to the denominator for numerical stability. Default to
    /// [`LayerNorm::DEFAULT_EPS`].
    #[builder(optional, default = LayerNorm::DEFAULT_EPS)]
    pub eps: f32,

    /// If `true`, addes a trainable `weight` and `bias`. Default to
    /// [`LayerNorm::DEFAULT_AFFINE`].
    #[builder(optional, default = LayerNorm::DEFAULT_AFFINE)]
    pub affine: bool,
}

fn build_layer_norm(builder: LayerNormBuilder) -> Result<LayerNorm, Exception> {
    let eps = builder.eps;
    let affine = builder.affine;

    let (weight, bias) = if affine {
        (
            Some(ones::<f32>(&[builder.dimensions])?),
            Some(zeros::<f32>(&[builder.dimensions])?),
        )
    } else {
        (None, None)
    };

    Ok(LayerNorm {
        dimensions: builder.dimensions,
        eps,
        weight: Param::new(weight),
        bias: Param::new(bias),
    })
}

/// Applies layer normalization [1] on the inputs.
///
/// ### References
///
/// 1. [https://arxiv.org/abs/1607.06450](https://arxiv.org/abs/1607.06450)
#[derive(Debug, Clone, ModuleParameters, Buildable)]
#[module(root = crate)]
#[buildable(root = crate)]
pub struct LayerNorm {
    /// Number of features in the input
    pub dimensions: i32,

    /// Value added to the denominator for numerical stability.
    pub eps: f32,

    /// An optional trainable weight
    #[param]
    pub weight: Param<Option<Array>>,

    /// An optional trainable bias
    #[param]
    pub bias: Param<Option<Array>>,
}

impl LayerNorm {
    /// Default value for `eps`.
    pub const DEFAULT_EPS: f32 = 1e-5;

    /// Enable trainable `weight` and `bias` by default.
    pub const DEFAULT_AFFINE: bool = true;
}

impl Module<&Array> for LayerNorm {
    type Error = Exception;
    type Output = Array;

    fn forward(&mut self, x: &Array) -> Result<Array, Self::Error> {
        let weight = self.weight.as_ref();
        let bias = self.bias.as_ref();
        let eps = self.eps;
        crate::fast::layer_norm(x, weight, bias, eps)
    }

    fn training_mode(&mut self, _mode: bool) {}
}

/// Builder for [`RmsNorm`].
#[derive(Debug, Clone, Builder)]
#[builder(
    root = crate,
    build_with = build_rms_norm,
    err = Exception,
)]
pub struct RmsNormBuilder {
    /// Number of features in the input
    pub dimensions: i32,

    /// Value added to the denominator for numerical stability. Default to
    /// [`RmsNorm::DEFAULT_EPS`].
    #[builder(optional, default = RmsNorm::DEFAULT_EPS)]
    pub eps: f32,
}

fn build_rms_norm(builder: RmsNormBuilder) -> Result<RmsNorm, Exception> {
    let weight = ones::<f32>(&[builder.dimensions])?;
    let eps = builder.eps;
    Ok(RmsNorm {
        weight: Param::new(weight),
        eps,
    })
}

/// Applies Root Mean Square normalization [1] to the inputs.
///
/// Concretely:
///
/// ```swift
/// weight * x * MLX.rsqrt(x.square().mean() + eps)
/// ```
///
/// where `weight` is initialized with ones and `eps` is a small float to
/// ensure the numerical stability of inverse square root.
///
/// ### References
///
/// 1. [https://arxiv.org/abs/1910.07467](https://arxiv.org/abs/1910.07467)
#[derive(Debug, Clone, ModuleParameters, Buildable)]
#[module(root = crate)]
#[buildable(root = crate)]
pub struct RmsNorm {
    /// Weight
    #[param]
    pub weight: Param<Array>,

    /// A small float to ensure the numerical stability
    pub eps: f32,
}

impl RmsNorm {
    /// Default value for `eps`.
    pub const DEFAULT_EPS: f32 = 1e-5;
}

impl Module<&Array> for RmsNorm {
    type Error = Exception;
    type Output = Array;

    fn forward(&mut self, x: &Array) -> Result<Array, Self::Error> {
        let weight = self.weight.as_ref();
        let eps = self.eps;
        crate::fast::rms_norm(x, weight, eps)
    }

    fn training_mode(&mut self, _mode: bool) {}
}

/// Builder for [`GroupNorm`].
#[derive(Debug, Clone, Builder)]
#[builder(
    root = crate,
    build_with = build_group_norm,
    err = Exception,
)]
pub struct GroupNormBuilder {
    /// Number of groups to separate the features into
    pub group_count: i32,

    /// Number of features in the input
    pub dimensions: i32,

    /// Value added to the denominator for numerical stability. Default to
    /// [`GroupNorm::DEFAULT_EPS`].
    #[builder(optional, default = GroupNorm::DEFAULT_EPS)]
    pub eps: f32,

    /// If `true`, add a trainable `weight` and `bias`. Default to
    /// [`GroupNorm::DEFAULT_AFFINE`].
    #[builder(optional, default = GroupNorm::DEFAULT_AFFINE)]
    pub affine: bool,

    /// If `true`, perform the group normalization in the same order/grouping as PyTorch.
    /// Default to [`GroupNorm::DEFAULT_PYTORCH_COMPATIBLE`].
    #[builder(optional, default = GroupNorm::DEFAULT_PYTORCH_COMPATIBLE)]
    pub pytorch_compatible: bool,
}

fn build_group_norm(builder: GroupNormBuilder) -> Result<GroupNorm, Exception> {
    let eps = builder.eps;
    let affine = builder.affine;
    let pytorch_compatible = builder.pytorch_compatible;

    let (weight, bias) = if affine {
        (
            Some(ones::<f32>(&[builder.dimensions])?),
            Some(zeros::<f32>(&[builder.dimensions])?),
        )
    } else {
        (None, None)
    };

    Ok(GroupNorm {
        group_count: builder.group_count,
        dimensions: builder.dimensions,
        eps: array!(eps),
        pytorch_compatible,
        weight: Param::new(weight),
        bias: Param::new(bias),
    })
}

/// Applies Group Normalization [1] on the inputs.
///
/// ### References
///
/// 1. [https://arxiv.org/abs/1803.08494](https://arxiv.org/abs/1803.08494)
#[derive(Debug, Clone, ModuleParameters, Buildable)]
#[module(root = crate)]
#[buildable(root = crate)]
pub struct GroupNorm {
    /// Number of groups to separate the features into
    pub group_count: i32,

    /// Number of features in the input
    pub dimensions: i32,

    /// Value added to the denominator for numerical stability.
    pub eps: Array,

    /// If `true`, perform the group normalization in the same order/grouping as PyTorch.
    pub pytorch_compatible: bool,

    /// An optional trainable weight
    #[param]
    pub weight: Param<Option<Array>>,

    /// An optional trainable bias
    #[param]
    pub bias: Param<Option<Array>>,
}

impl GroupNorm {
    /// Default value for `eps`.
    pub const DEFAULT_EPS: f32 = 1e-5;

    /// Enable trainable `weight` and `bias` by default.
    pub const DEFAULT_AFFINE: bool = true;

    /// Default value for `pytorch_compatible`.
    pub const DEFAULT_PYTORCH_COMPATIBLE: bool = false;

    fn pytorch_group_norm(&self, x: &Array) -> Result<Array, Exception> {
        let batch = x.dim(0);
        let dims = x.dim(-1);
        let rest = &x.shape()[1..x.ndim() - 1];
        let group_size = dims / self.group_count;

        // Split into groups
        let x = x.reshape(&[batch, -1, self.group_count, group_size])?;
        let x = x
            .transpose(&[0, 2, 1, 3])?
            .reshape(&[batch, self.group_count, -1])?;

        // Normalize
        let x = crate::fast::layer_norm(x, None, None, self.eps.item::<f32>())?;

        let x = x.reshape(&[batch, self.group_count, -1, group_size])?;

        let new_shape: Vec<_> = [batch]
            .into_iter()
            .chain(rest.iter().copied())
            .chain([dims])
            .collect();
        x.transpose(&[0, 2, 1, 3])?.reshape(&new_shape[..])
    }

    fn group_norm(&self, x: &Array) -> Result<Array, Exception> {
        let batch = x.dim(0);
        let dims = x.dim(-1);
        let rest = &x.shape()[1..x.ndim() - 1];

        // Split into groups
        let x = x.reshape(&[batch, -1, self.group_count])?;

        // Normalize
        let x = instance_norm(&x, &[1], &self.eps)?;

        let new_shape: Vec<_> = [batch]
            .into_iter()
            .chain(rest.iter().copied())
            .chain([dims])
            .collect();
        x.reshape(&new_shape[..])
    }
}

impl Module<&Array> for GroupNorm {
    type Error = Exception;
    type Output = Array;

    fn forward(&mut self, x: &Array) -> Result<Array, Self::Error> {
        let x = if self.pytorch_compatible {
            self.pytorch_group_norm(x)?
        } else {
            self.group_norm(x)?
        };

        if let (Some(weight), Some(bias)) = (self.weight.as_ref(), self.bias.as_ref()) {
            weight.multiply(&x)?.add(bias)
        } else {
            Ok(x)
        }
    }

    fn training_mode(&mut self, _mode: bool) {}
}

/// Builder for [`BatchNorm`].
#[derive(Debug, Clone, Builder)]
#[builder(
    root = crate,
    build_with = build_batch_norm,
    err = Exception,
)]
pub struct BatchNormBuilder {
    /// Number of features in the input
    pub feature_count: i32,

    /// Value added to the denominator for numerical stability. Default to
    /// [`BatchNorm::DEFAULT_EPS`].
    #[builder(optional, default = BatchNorm::DEFAULT_EPS)]
    pub eps: f32,

    /// Momentum for updating the running mean and variance. Default to
    /// [`BatchNorm::DEFAULT_MOMENTUM`].
    #[builder(optional, default = BatchNorm::DEFAULT_MOMENTUM)]
    pub momentum: f32,

    /// If `true`, addes a trainable `weight` and `bias`. Default to
    /// [`BatchNorm::DEFAULT_AFFINE`].
    #[builder(optional, default = BatchNorm::DEFAULT_AFFINE)]
    pub affine: bool,

    /// If `true`, track the running mean and variance. Default to
    /// [`BatchNorm::DEFAULT_TRACK_RUNNING_STATS`].
    #[builder(optional, default = BatchNorm::DEFAULT_TRACK_RUNNING_STATS)]
    pub track_running_stats: bool,
}

fn build_batch_norm(builder: BatchNormBuilder) -> Result<BatchNorm, Exception> {
    let eps = builder.eps;
    let momentum = builder.momentum;
    let affine = builder.affine;
    let track_running_stats = builder.track_running_stats;

    let (weight, bias) = if affine {
        (
            Some(ones::<f32>(&[builder.feature_count])?),
            Some(zeros::<f32>(&[builder.feature_count])?),
        )
    } else {
        (None, None)
    };

    let (running_mean, running_var) = if track_running_stats {
        (
            Some(zeros::<f32>(&[builder.feature_count])?),
            Some(ones::<f32>(&[builder.feature_count])?),
        )
    } else {
        (None, None)
    };

    Ok(BatchNorm {
        feature_count: builder.feature_count,
        eps: array!(eps),
        momentum: array!(momentum),
        weight: Param::new(weight),
        bias: Param::new(bias),
        running_mean: Param::new(running_mean),
        running_var: Param::new(running_var),
        training: BatchNorm::DEFAULT_TRAINING,
    })
}

/// Applies batch normalization [1] on the inputs.
///
/// ### References
///
/// 1. [https://arxiv.org/abs/1502.03167](https://arxiv.org/abs/1502.03167)
#[derive(Debug, Clone, ModuleParameters, Buildable)]
#[module(root = crate)]
#[buildable(root = crate)]
pub struct BatchNorm {
    /// Number of features in the input
    pub feature_count: i32,

    /// Value added to the denominator for numerical stability.
    pub eps: Array,

    /// Momentum for updating the running mean and variance.
    pub momentum: Array,

    /// An optional trainable weight
    #[param]
    pub weight: Param<Option<Array>>,

    /// An optional trainable bias
    #[param]
    pub bias: Param<Option<Array>>,

    /// Tracked running mean
    #[param]
    pub running_mean: Param<Option<Array>>,

    /// Tracked running variance
    #[param]
    pub running_var: Param<Option<Array>>,

    /// If `true`, the module is in training mode.
    pub training: bool,
}

impl BatchNorm {
    /// Default value for `eps`.
    pub const DEFAULT_EPS: f32 = 1e-5;

    /// Default value for `momentum`.
    pub const DEFAULT_MOMENTUM: f32 = 0.1;

    /// Enable trainable `weight` and `bias` by default.
    pub const DEFAULT_AFFINE: bool = true;

    /// Enable tracking of running mean and variance by default.
    pub const DEFAULT_TRACK_RUNNING_STATS: bool = true;

    /// Enable training mode by default.
    pub const DEFAULT_TRAINING: bool = true;

    fn stats(x: &Array) -> Result<(Array, Array), Exception> {
        let reduction_axes = (0..x.ndim() as i32 - 1).collect::<Vec<_>>();

        let mean = x.mean(&reduction_axes, None)?;
        let variance = x.variance(&reduction_axes, None, None)?;

        Ok((mean, variance))
    }
}

impl Module<&Array> for BatchNorm {
    type Error = Exception;
    type Output = Array;

    fn forward(&mut self, x: &Array) -> Result<Array, Self::Error> {
        let ndim = x.ndim();
        if !(2..=4).contains(&ndim) {
            return Err(Exception::custom(
                "Input tensor must be at least 2 dimensions and at most 4 dimensions",
            ));
        }

        let (mean, variance) = Self::stats(x)?;
        let mut mean = Cow::Owned(mean);
        let mut variance = Cow::Owned(variance);

        if let (Some(running_mean), Some(running_var)) =
            (self.running_mean.as_mut(), self.running_var.as_mut())
        {
            if self.training {
                let mu = &self.momentum;
                // SAFETY: momentum is a single element array
                let one_minus_mu = array!(1.0) - mu;

                *running_mean = one_minus_mu
                    .multiply(&running_mean)?
                    .add(mu.multiply(&mean)?)?;
                *running_var = one_minus_mu
                    .multiply(&running_var)?
                    .add(mu.multiply(&variance)?)?;
            } else {
                mean = Cow::Borrowed(&*running_mean);
                variance = Cow::Borrowed(&*running_var);
            }
        }

        let x = x
            .subtract(&mean)?
            .multiply(rsqrt(&variance.add(&self.eps)?)?)?;

        if let (Some(weight), Some(bias)) = (self.weight.as_ref(), self.bias.as_ref()) {
            weight.multiply(&x)?.add(bias)
        } else {
            Ok(x)
        }
    }

    fn training_mode(&mut self, mode: bool) {
        self.training = mode;
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        ops::indexing::{Ellipsis, IndexOp},
        Dtype,
    };
    use float_eq::assert_float_eq;

    use super::*;

    #[test]
    fn test_instance_norm() {
        crate::random::seed(435).unwrap();
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], None).unwrap();
        assert_eq!(a.shape(), &[2, 8, 16]);
        assert_eq!(a.dtype(), Dtype::Float32);
        assert_float_eq!(
            a.mean(None, None).unwrap().item::<f32>(),
            0.500_064_6,
            abs <= 0.010_001_292
        );
        assert_float_eq!(
            a.sum(None, None).unwrap().item::<f32>(),
            128.016_54,
            abs <= 2.560_330_9
        );

        let result = InstanceNorm::new(8)
            .unwrap()
            .forward(&a)
            .unwrap()
            .index((0, 0));
        assert_eq!(result.shape(), &[16]);
        assert_eq!(result.dtype(), Dtype::Float32);
        assert_float_eq!(
            result.mean(None, None).unwrap().item::<f32>(),
            0.106_454_11,
            abs <= 0.002_129_082_3
        );
        assert_float_eq!(
            result.sum(None, None).unwrap().item::<f32>(),
            1.703_265_8,
            abs <= 0.034_065_317
        );
    }

    #[test]
    fn test_layer_norm() {
        crate::random::seed(635).unwrap();
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], None).unwrap();
        assert_eq!(a.shape(), &[2, 8, 16]);
        assert_eq!(a.dtype(), Dtype::Float32);
        assert_float_eq!(
            a.mean(None, None).unwrap().item::<f32>(),
            0.492_690_32,
            abs <= 0.009_853_806
        );
        assert_float_eq!(
            a.sum(None, None).unwrap().item::<f32>(),
            126.128_72,
            abs <= 2.522_574_4
        );

        let result = LayerNorm::new(16)
            .unwrap()
            .forward(&a)
            .unwrap()
            .index((Ellipsis, 0));
        assert_eq!(result.shape(), &[2, 8]);
        assert_eq!(result.dtype(), Dtype::Float32);
        assert_float_eq!(
            result.mean(None, None).unwrap().item::<f32>(),
            0.290_990_38,
            abs <= 0.005_819_807_8
        );
        assert_float_eq!(
            result.sum(None, None).unwrap().item::<f32>(),
            4.655_846,
            abs <= 0.093_116_924
        );
    }

    #[test]
    fn test_rms_norm() {
        crate::random::seed(103).unwrap();
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], None).unwrap();
        assert_eq!(a.shape(), &[2, 8, 16]);
        assert_eq!(a.dtype(), Dtype::Float32);
        assert_float_eq!(
            a.mean(None, None).unwrap().item::<f32>(),
            0.505_476_36,
            abs <= 0.010_109_527
        );
        assert_float_eq!(
            a.sum(None, None).unwrap().item::<f32>(),
            129.401_95,
            abs <= 2.588_039
        );

        let result = RmsNorm::new(16).unwrap().forward(&a).unwrap();
        assert_eq!(result.shape(), &[2, 8, 16]);
        assert_eq!(result.dtype(), Dtype::Float32);
        assert_float_eq!(
            result.mean(None, None).unwrap().item::<f32>(),
            0.872_938_75,
            abs <= 0.017_458_774
        );
        assert_float_eq!(
            result.sum(None, None).unwrap().item::<f32>(),
            223.472_32,
            abs <= 4.469_446
        );
    }

    #[test]
    fn test_group_norm() {
        crate::random::seed(855).unwrap();
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], None).unwrap();
        assert_eq!(a.shape(), &[2, 8, 16]);
        assert_eq!(a.dtype(), Dtype::Float32);
        assert_float_eq!(
            a.mean(None, None).unwrap().item::<f32>(),
            0.486_665_87,
            abs <= 0.009_733_317
        );
        assert_float_eq!(
            a.sum(None, None).unwrap().item::<f32>(),
            124.586_464,
            abs <= 2.491_729_3
        );

        let result = GroupNorm::new(4, 16)
            .unwrap()
            .forward(&a)
            .unwrap()
            .index((0, 0));
        assert_eq!(result.shape(), &[16]);
        assert_eq!(result.dtype(), Dtype::Float32);
        assert_float_eq!(
            result.mean(None, None).unwrap().item::<f32>(),
            -0.054_606_52,
            abs <= 0.001_092_130_4
        );
        assert_float_eq!(
            result.sum(None, None).unwrap().item::<f32>(),
            -0.873_704_3,
            abs <= 0.017_474_087
        );
    }

    #[test]
    fn test_batch_norm() {
        crate::random::seed(266).unwrap();
        let a = crate::random::uniform::<_, f32>(0.0, 1.0, &[2, 8, 16], None).unwrap();
        assert_eq!(a.shape(), &[2, 8, 16]);
        assert_eq!(a.dtype(), Dtype::Float32);
        assert_float_eq!(
            a.mean(None, None).unwrap().item::<f32>(),
            0.505_814_7,
            abs <= 0.010_116_293
        );
        assert_float_eq!(
            a.sum(None, None).unwrap().item::<f32>(),
            129.488_56,
            abs <= 2.589_771
        );

        let result = BatchNorm::new(16)
            .unwrap()
            .forward(&a)
            .unwrap()
            .index((0, 0));
        assert_eq!(result.shape(), &[16]);
        assert_eq!(result.dtype(), Dtype::Float32);
        assert_float_eq!(
            result.mean(None, None).unwrap().item::<f32>(),
            0.439_785_24,
            abs <= 0.008_795_705
        );
        assert_float_eq!(
            result.sum(None, None).unwrap().item::<f32>(),
            7.036_564,
            abs <= 0.140_731_28
        );
    }
}