"""Plain PyTorch versions of the custom CUDA operations in rosinality's StyleGAN2.

e4e, GAN-Control, SemanticStyleGAN and MobileStyleGAN all carry a copy of
rosinality's `op` package, which compiles CUDA kernels when it's imported.
That can't work on a Mac, and the ONNX exporter couldn't follow the kernels
anyway. These do the same sums with ordinary tensor operations, so the
networks run on the CPU and export to ONNX.

`install(name)` puts this module in place of the package called `name`
(for example "models.stylegan2.op") before the network's code imports it.
"""

import sys
import types

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


def fused_leaky_relu(input, bias=None, negative_slope=0.2, scale=2 ** 0.5):
    if bias is not None:
        shape = [1, bias.shape[0]] + [1] * (input.dim() - 2)
        input = input + bias.view(*shape)
    return F.leaky_relu(input, negative_slope=negative_slope) * scale


class FusedLeakyReLU(nn.Module):
    def __init__(self, channel, bias=True, negative_slope=0.2, scale=2 ** 0.5):
        super().__init__()
        self.bias = nn.Parameter(torch.zeros(channel)) if bias else None
        self.negative_slope = negative_slope
        self.scale = scale

    def forward(self, input):
        return fused_leaky_relu(input, self.bias, self.negative_slope, self.scale)


def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
    """Upsample by zero insertion, pad, filter with `kernel`, then downsample."""
    if len(pad) == 2:
        pad = (pad[0], pad[1], pad[0], pad[1])
    if isinstance(up, int):
        up = (up, up)
    if isinstance(down, int):
        down = (down, down)
    up_x, up_y = up
    down_x, down_y = down
    pad_x0, pad_x1, pad_y0, pad_y1 = pad

    batch, channel, in_h, in_w = input.shape
    kernel_h, kernel_w = kernel.shape
    out = input.reshape(-1, in_h, in_w, 1)

    #  Zero insertion
    out = out.view(-1, in_h, 1, in_w, 1, 1)
    out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
    out = out.view(-1, in_h * up_y, in_w * up_x, 1)

    #  Padding, or cropping when the padding is negative
    out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0), max(pad_y1, 0)])
    out = out[
        :,
        max(-pad_y0, 0): out.shape[1] - max(-pad_y1, 0),
        max(-pad_x0, 0): out.shape[2] - max(-pad_x1, 0),
        :,
    ]

    #  The filter, as a convolution with the flipped kernel
    out = out.permute(0, 3, 1, 2)
    out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x + pad_x0 + pad_x1])
    w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w).to(out.dtype)
    out = F.conv2d(out, w)
    out = out.reshape(
        -1,
        1,
        in_h * up_y + pad_y0 + pad_y1 - kernel_h + 1,
        in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1,
    )
    out = out.permute(0, 2, 3, 1)

    #  Downsampling
    out = out[:, ::down_y, ::down_x, :]
    out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
    out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
    return out.view(batch, channel, out_h, out_w)


def unfused_modulated_conv2d(self, input, style):
    """rosinality's ModulatedConv2d.forward, without folding the style into the weights.

    The original multiplies the weights by each sample's style and reshapes them
    into a grouped convolution, so the exporter sees a kernel of unknown shape.
    Scaling the input by the style instead, convolving with the plain weights,
    then scaling each output channel by the demodulation gives the same result.
    Replace the class's forward with this before exporting.
    """
    in_channel, out_channel = self.in_channel, self.out_channel
    s = self.modulation(style).view(-1, in_channel, 1, 1)
    w = self.scale * self.weight[0]
    x = input * s
    if self.upsample:
        if getattr(self, "conv_transpose", True):
            out = F.conv_transpose2d(x, w.transpose(0, 1), padding=0, stride=2)
            out = self.blur(out)
        else:
            out = F.interpolate(x, scale_factor=2, mode="nearest")
            out = nn.ReflectionPad2d(1)(out)
            out = F.conv2d(out, w, padding=0, stride=1)
    elif self.downsample:
        out = F.conv2d(self.blur(x), w, padding=0, stride=2)
    else:
        out = F.conv2d(x, w, padding=self.padding)
    if self.demodulate:
        energy = (w ** 2).sum([2, 3])
        demod = torch.rsqrt((s.view(-1, 1, in_channel) ** 2 * energy[None]).sum(2) + 1e-8)
        out = out * demod.view(-1, out_channel, 1, 1)
    return out


class _Conv2dGradfix(types.ModuleType):
    """rosinality's conv2d_gradfix only changes the gradients, so plain convolutions do."""

    def __init__(self):
        super().__init__("conv2d_gradfix")
        self.conv2d = F.conv2d
        self.conv_transpose2d = F.conv_transpose2d
        self.enabled = False


def install(name):
    """Make `import <name>` (and `from <name> import ...`) find these operations."""
    module = types.ModuleType(name)
    module.FusedLeakyReLU = FusedLeakyReLU
    module.fused_leaky_relu = fused_leaky_relu
    module.upfirdn2d = upfirdn2d
    gradfix = _Conv2dGradfix()
    module.conv2d_gradfix = gradfix
    module.__path__ = []
    sys.modules[name] = module
    for sub, value in (
        ("fused_act", module),
        ("upfirdn2d", module),
        ("conv2d_gradfix", gradfix),
    ):
        sys.modules[f"{name}.{sub}"] = value
    return module
