|
import torch |
|
from torch import nn |
|
from torch.nn import functional as F |
|
|
|
|
|
class FusedLeakyReLU(nn.Module): |
|
def __init__(self, channel, bias=True, negative_slope=0.2, scale=2**0.5): |
|
super().__init__() |
|
|
|
if bias: |
|
self.bias = nn.Parameter(torch.zeros(channel)) |
|
|
|
else: |
|
self.bias = 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 fused_leaky_relu(input, bias=None, negative_slope=0.2, scale=2**0.5): |
|
if input.dtype == torch.float16: |
|
bias = bias.half() |
|
|
|
if bias is not None: |
|
rest_dim = [1] * (input.ndim - bias.ndim - 1) |
|
return F.leaky_relu( |
|
input + bias.view(1, bias.shape[0], *rest_dim), negative_slope=0.2 |
|
) * scale |
|
|
|
else: |
|
return F.leaky_relu(input, negative_slope=0.2) * scale |
|
|
|
|
|
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)): |
|
up_x, up_y = up, up |
|
down_x, down_y = down, down |
|
pad_x0, pad_x1, pad_y0, pad_y1 = pad[0], pad[1], pad[0], pad[1] |
|
|
|
_, channel, in_h, in_w = input.shape |
|
input = input.reshape(-1, in_h, in_w, 1) |
|
|
|
_, in_h, in_w, minor = input.shape |
|
kernel_h, kernel_w = kernel.shape |
|
|
|
out = input.view(-1, in_h, 1, in_w, 1, minor) |
|
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, minor) |
|
|
|
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), :, ] |
|
|
|
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) |
|
out = F.conv2d(out, w) |
|
out = out.reshape( |
|
-1, |
|
minor, |
|
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) |
|
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(-1, channel, out_h, out_w) |
|
|