Spaces:
Running
Running
import torch | |
from torch import nn | |
from modules import WN | |
DEFAULT_MIN_BIN_WIDTH = 1e-3 | |
DEFAULT_MIN_BIN_HEIGHT = 1e-3 | |
DEFAULT_MIN_DERIVATIVE = 1e-3 | |
class ResidualCouplingLayer(nn.Module): | |
def __init__( | |
self, | |
channels, | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
p_dropout=0, | |
gin_channels=0, | |
mean_only=False, | |
): | |
assert channels % 2 == 0, "channels should be divisible by 2" | |
super().__init__() | |
self.channels = channels | |
self.hidden_channels = hidden_channels | |
self.kernel_size = kernel_size | |
self.dilation_rate = dilation_rate | |
self.n_layers = n_layers | |
self.half_channels = channels // 2 | |
self.mean_only = mean_only | |
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1) | |
self.enc = WN( | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
p_dropout=p_dropout, | |
gin_channels=gin_channels, | |
) | |
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1) | |
self.post.weight.data.zero_() | |
self.post.bias.data.zero_() | |
def forward(self, x, x_mask, g=None, reverse=False): | |
x0, x1 = torch.split(x, [self.half_channels] * 2, 1) | |
h = self.pre(x0) * x_mask | |
h = self.enc(h, x_mask, g=g) | |
stats = self.post(h) * x_mask | |
if not self.mean_only: | |
m, logs = torch.split(stats, [self.half_channels] * 2, 1) | |
else: | |
m = stats | |
logs = torch.zeros_like(m) | |
if not reverse: | |
x1 = m + x1 * torch.exp(logs) * x_mask | |
x = torch.cat([x0, x1], 1) | |
logdet = torch.sum(logs, [1, 2]) | |
return x, logdet | |
else: | |
x1 = (x1 - m) * torch.exp(-logs) * x_mask | |
x = torch.cat([x0, x1], 1) | |
return x | |
class Flip(nn.Module): | |
def forward(self, x, *args, reverse=False, **kwargs): | |
x = torch.flip(x, [1]) | |
if not reverse: | |
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device) | |
return x, logdet | |
else: | |
return x | |
class ResidualCouplingBlock(nn.Module): | |
def __init__( | |
self, | |
channels, | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
n_flows=4, | |
gin_channels=0, | |
): | |
super().__init__() | |
self.channels = channels | |
self.hidden_channels = hidden_channels | |
self.kernel_size = kernel_size | |
self.dilation_rate = dilation_rate | |
self.n_layers = n_layers | |
self.n_flows = n_flows | |
self.gin_channels = gin_channels | |
self.flows = nn.ModuleList() | |
for i in range(n_flows): | |
self.flows.append( | |
ResidualCouplingLayer( | |
channels, | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
gin_channels=gin_channels, | |
mean_only=True, | |
) | |
) | |
self.flows.append(Flip()) | |
def forward(self, x, x_mask, g=None, reverse=False): | |
if not reverse: | |
for flow in self.flows: | |
x, _ = flow(x, x_mask, g=g, reverse=reverse) | |
else: | |
for flow in reversed(self.flows): | |
x = flow(x, x_mask, g=g, reverse=reverse) | |
return x | |