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
// Copyright (C) 2025 zk4x
// SPDX-License-Identifier: LGPL-3.0-only
use zyx::{DType, Tensor, ZyxError};
use zyx_derive::Module;
/// Group normalization
#[derive(Debug, Module)]
#[cfg_attr(feature = "py", pyo3::pyclass)]
pub struct GroupNorm {
/// number of groups
pub num_groups: u64,
/// epsilon
pub eps: f32,
/// shape: [C]
pub weight: Option<Tensor>,
/// shape: [C]
pub bias: Option<Tensor>,
}
impl GroupNorm {
/// Creates a new GroupNorm module.
///
/// Group Normalization divides the channels into groups and normalizes
/// the activations within each group, making it independent of batch size.
///
/// # Arguments
/// - `num_groups`: Number of groups to divide channels into.
/// - `num_channels`: Total number of input channels (must be divisible by `num_groups`).
/// - `affine`: If `true`, includes learnable scale (`weight`) and bias (`bias`) parameters.
///
/// # Returns
/// A `GroupNorm` module with optional learnable parameters.
///
/// # Example
/// ```rust ignore
/// let gn = GroupNorm::new(32, 64, true, DType::F32)?;
/// let out = gn.forward(x)?;
/// ```
pub fn new(
num_groups: u64,
num_channels: u64,
affine: bool,
dtype: DType,
) -> Result<Self, ZyxError> {
if !num_channels.is_multiple_of(num_groups) {
return Err(ZyxError::ShapeError(
format!(
"num_channels ({}) must be divisible by num_groups ({})",
num_channels, num_groups
)
.into(),
));
}
let (weight, bias) = if affine {
(
Some(Tensor::ones([num_channels], dtype)),
Some(Tensor::zeros([num_channels], dtype)),
)
} else {
(None, None)
};
Ok(Self {
num_groups,
eps: 1e-5,
weight,
bias,
})
}
/// Applies group normalization to the input tensor.
///
/// The input is expected to have shape `[N, C, ...]` where:
/// - `N` is the batch size
/// - `C` is the number of channels
/// - Remaining dimensions are treated as spatial or temporal axes
///
/// Normalization is applied per sample, per group:
/// - Input is reshaped to `[N, num_groups, C / num_groups, ...]`
/// - Mean and variance are computed across group channels and spatial dims
/// - Output is normalized and optionally scaled and shifted by `weight` and `bias`
///
/// # Arguments
/// - `x`: Input tensor of shape `[N, C, *]`
///
/// # Returns
/// A normalized tensor of the same shape as input.
///
/// # Errors
/// Returns an error if the input shape is invalid or incompatible with `num_groups`.
///
/// # Example
/// ```rust ignore
/// let gn = GroupNorm::new(8, 64, true, DType::F32)?;
/// let out = gn.forward(x)?;
/// ```
pub fn forward(&self, x: impl Into<Tensor>) -> Result<Tensor, ZyxError> {
let x = x.into();
let shape = x.shape();
if shape.len() < 2 {
return Err(ZyxError::ShapeError(
format!("GroupNorm requires at least 2D input, got {:?}", shape).into(),
));
}
let n = shape[0];
let c = shape[1];
let rest = &shape[2..];
if c % self.num_groups != 0 {
return Err(ZyxError::ShapeError(
format!(
"num_channels ({}) must be divisible by num_groups ({})",
c, self.num_groups
)
.into(),
));
}
let group_size = c / self.num_groups;
// Reshape: [N, C, ...] → [N, G, C//G, ...]
let mut new_shape = vec![n, self.num_groups, group_size];
new_shape.extend_from_slice(rest);
let x = x.reshape(new_shape.clone())?;
// Axes to normalize over: [2, 3, 4, ...]
let axes = 2..(new_shape.len() as i32);
let eps = Tensor::from(self.eps).cast(x.dtype());
let mean = x.mean_keepdim(axes.clone())?;
let var = x.var_keepdim(axes.clone())?;
let x = (x - mean) / (var + eps).sqrt();
// Reshape back to original shape
let mut x = x.reshape(shape)?;
if let Some(weight) = &self.weight {
x = x * weight;
}
if let Some(bias) = &self.bias {
x = x + bias;
}
Ok(x)
}
}