Skip to main content

teeny_core/nn/
groupnorm.rs

1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use core::marker::PhantomData;
18
19use crate::{
20    dtype::{Dtype, EagerTensor, Tensor},
21    nn::Layer,
22};
23
24/// Group normalisation over `num_groups` groups of channels.
25///
26/// Input shape: `[N, C, *]` — normalises over `(C/num_groups, *)` per sample.
27/// Output shape: same as input.
28///
29/// Parameters:
30/// - `num_groups`   — number of groups to divide C into (must divide C evenly)
31/// - `num_channels` — number of channels C
32/// - `eps`          — numerical stability constant (default 1e-5)
33/// - `affine`       — if true, learns per-channel γ and β (default true)
34pub struct GroupNorm<D: Dtype, IT, OT, const RANK: usize> {
35    /// Number of groups to divide the channels into.
36    pub num_groups: usize,
37    /// Number of channels.
38    pub num_channels: usize,
39    /// Numerical stability constant added to the variance.
40    pub eps: f64,
41    /// Whether to learn per-channel scale (gamma) and shift (beta) parameters.
42    pub affine: bool,
43    _pd: PhantomData<(D, IT, OT)>,
44}
45
46impl<D: Dtype, IT, OT, const RANK: usize> GroupNorm<D, IT, OT, RANK> {
47    /// Creates a new layer with default `eps`, affine on.
48    pub fn new(num_groups: usize, num_channels: usize) -> Self {
49        Self {
50            num_groups,
51            num_channels,
52            eps: 1e-5,
53            affine: true,
54            _pd: PhantomData,
55        }
56    }
57
58    /// Sets the numerical stability constant.
59    pub fn with_eps(mut self, eps: f64) -> Self {
60        self.eps = eps;
61        self
62    }
63
64    /// Sets whether to learn per-channel scale/shift parameters.
65    pub fn with_affine(mut self, affine: bool) -> Self {
66        self.affine = affine;
67        self
68    }
69}
70
71impl<D: Dtype, IT: Tensor<D, RANK> + EagerTensor, OT: Tensor<D, RANK>, const RANK: usize> Layer<IT>
72    for GroupNorm<D, IT, OT, RANK>
73{
74    type Output = OT;
75
76    fn call(&self, _input: IT) -> Self::Output {
77        todo!()
78    }
79}