mr2.nn.GroupNorm

class mr2.nn.GroupNorm[source]

Bases: GroupNorm

A 32-bit GroupNorm with (optional) automatic group size selection.

Casts to float32 before calling the parent class to avoid instabilities in mixed precision training.

If n_groups is not provided, the number of groups is selected automatically as follows:

  • start from 1 group,

  • try powers of two (2, 4, 8, ...),

  • keep the largest candidate that divides n_channels,

  • enforce at most 32 groups and at least 4 channels per group.

This yields a stable default that stays close to common GroupNorm choices while adapting to small channel counts.

__init__(n_channels: int, n_groups: int | None = None, affine: bool = False, features_last: bool = False)[source]

Initialize GroupNorm.

Parameters:
  • n_channels (int) – The number of channels in the input tensor.

  • n_groups (int | None, default: None) – The number of groups to use. If None, the number of groups is determined automatically as the largest power of 2 that divides n_channels, is less than or equal to 32, and leaves at least 4 channels per group.

  • affine (bool, default: False) – Whether to use learnable affine parameters.

  • features_last (bool, default: False) – Whether the features are last in the input tensor, as common in transformer models, or in the second dimension, as common in CNNs.

__call__(x: Tensor) → Tensor[source]

Apply GroupNorm32.

Parameters:

x (Tensor) – The input tensor.

Returns:

The output tensor.

forward(x: Tensor) → Tensor[source]

Apply GroupNorm.

extra_repr() → str

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.