""" Normalization layers and wrappers

Norm layer definitions that support fast norm and consistent channel arg order (always first arg).

Hacked together by / Copyright 2022 Ross Wightman
"""

import torch
import torch.nn as nn
import torch.nn.functional as F


class LayerNorm2d(nn.LayerNorm):
    """LayerNorm for channels of '2D' spatial NCHW tensors"""

    def __init__(self, num_channels, eps=1e-6, affine=True):
        super().__init__(num_channels, eps=eps, elementwise_affine=affine)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = x.permute(0, 2, 3, 1)
        if self.weight is not None:
            x = F.layer_norm(
                x,
                (x.shape[-1],),
                self.weight,
                self.bias,
                self.eps,
            )
        else:
            x = F.layer_norm(x, (x.shape[-1],), eps=self.eps)
        x = x.permute(0, 3, 1, 2)
        return x


class GroupNorm2d(nn.GroupNorm):
    """GroupNorm for channels of '2D' spatial NCHW tensors"""

    def __init__(self, num_groups, num_channels, eps=1e-6, affine=True):
        super().__init__(num_groups, num_channels, eps=eps, affine=affine)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        if self.weight is not None:
            x = F.group_norm(
                x,
                self.num_groups,
                self.weight,
                self.bias,
                self.eps,
            )
        else:
            x = F.group_norm(x, self.num_groups, eps=self.eps)
        return x


class BatchNorm2d(nn.BatchNorm2d):
    def __init__(
        self,
        num_channels: int,
        eps: float = 0.00001,
        momentum: float = 0.1,
        affine: bool = True,
        track_running_stats: bool = True,
        device=None,
        dtype=None,
    ) -> None:
        super().__init__(
            num_channels, eps, momentum, affine, track_running_stats, device, dtype
        )

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        self._check_input_dim(input)

        # exponential_average_factor is set to self.momentum
        # (when it is available) only so that it gets updated
        # in ONNX graph when this node is exported to ONNX.
        if self.momentum is None:
            exponential_average_factor = 0.0
        else:
            exponential_average_factor = self.momentum

        if self.training and self.track_running_stats:
            # TODO: if statement only here to tell the jit to skip emitting this when it is None
            if self.num_batches_tracked is not None:  # type: ignore[has-type]
                self.num_batches_tracked.add_(1)  # type: ignore[has-type]
                if self.momentum is None:  # use cumulative moving average
                    exponential_average_factor = 1.0 / float(self.num_batches_tracked)
                else:  # use exponential moving average
                    exponential_average_factor = self.momentum

        r"""
        Decide whether the mini-batch stats should be used for normalization rather than the buffers.
        Mini-batch stats are used in training mode, and in eval mode when buffers are None.
        """
        if self.training:
            bn_training = True
        else:
            bn_training = (self.running_mean is None) and (self.running_var is None)

        r"""
        Buffers are only updated if they are to be tracked and we are in training mode. Thus they only need to be
        passed when the update should occur (i.e. in training mode when they are tracked), or when buffer stats are
        used for normalization (i.e. in eval mode when buffers are not None).
        """
        return F.batch_norm(
            input,
            # If buffers are not to be tracked, ensure that they won't be updated
            (
                self.running_mean[: input.shape[1]]
                if not self.training or self.track_running_stats
                else None
            ),
            (
                self.running_var[: input.shape[1]]
                if not self.training or self.track_running_stats
                else None
            ),
            self.weight,
            self.bias,
            bn_training,
            exponential_average_factor,
            self.eps,
        )
