Spaces:
Build error
Build error
File size: 13,721 Bytes
67d041f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
import torch
class Conv1d(torch.nn.Conv1d):
def __init__(self, w_init_gain= 'linear', *args, **kwargs):
self.w_init_gain = w_init_gain
super().__init__(*args, **kwargs)
def reset_parameters(self):
if self.w_init_gain in ['zero']:
torch.nn.init.zeros_(self.weight)
elif self.w_init_gain is None:
pass
elif self.w_init_gain in ['relu', 'leaky_relu']:
torch.nn.init.kaiming_uniform_(self.weight, nonlinearity= self.w_init_gain)
elif self.w_init_gain == 'glu':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.kaiming_uniform_(self.weight[:self.out_channels // 2], nonlinearity= 'linear')
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
elif self.w_init_gain == 'gate':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.xavier_uniform_(self.weight[:self.out_channels // 2], gain= torch.nn.init.calculate_gain('tanh'))
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
else:
torch.nn.init.xavier_uniform_(self.weight, gain= torch.nn.init.calculate_gain(self.w_init_gain))
if not self.bias is None:
torch.nn.init.zeros_(self.bias)
class ConvTranspose1d(torch.nn.ConvTranspose1d):
def __init__(self, w_init_gain= 'linear', *args, **kwargs):
self.w_init_gain = w_init_gain
super().__init__(*args, **kwargs)
def reset_parameters(self):
if self.w_init_gain in ['zero']:
torch.nn.init.zeros_(self.weight)
elif self.w_init_gain in ['relu', 'leaky_relu']:
torch.nn.init.kaiming_uniform_(self.weight, nonlinearity= self.w_init_gain)
elif self.w_init_gain == 'glu':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.kaiming_uniform_(self.weight[:self.out_channels // 2], nonlinearity= 'linear')
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
elif self.w_init_gain == 'gate':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.xavier_uniform_(self.weight[:self.out_channels // 2], gain= torch.nn.init.calculate_gain('tanh'))
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
else:
torch.nn.init.xavier_uniform_(self.weight, gain= torch.nn.init.calculate_gain(self.w_init_gain))
if not self.bias is None:
torch.nn.init.zeros_(self.bias)
class Conv2d(torch.nn.Conv2d):
def __init__(self, w_init_gain= 'linear', *args, **kwargs):
self.w_init_gain = w_init_gain
super().__init__(*args, **kwargs)
def reset_parameters(self):
if self.w_init_gain in ['zero']:
torch.nn.init.zeros_(self.weight)
elif self.w_init_gain in ['relu', 'leaky_relu']:
torch.nn.init.kaiming_uniform_(self.weight, nonlinearity= self.w_init_gain)
elif self.w_init_gain == 'glu':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.kaiming_uniform_(self.weight[:self.out_channels // 2], nonlinearity= 'linear')
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
elif self.w_init_gain == 'gate':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.xavier_uniform_(self.weight[:self.out_channels // 2], gain= torch.nn.init.calculate_gain('tanh'))
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
else:
torch.nn.init.xavier_uniform_(self.weight, gain= torch.nn.init.calculate_gain(self.w_init_gain))
if not self.bias is None:
torch.nn.init.zeros_(self.bias)
class ConvTranspose2d(torch.nn.ConvTranspose2d):
def __init__(self, w_init_gain= 'linear', *args, **kwargs):
self.w_init_gain = w_init_gain
super().__init__(*args, **kwargs)
def reset_parameters(self):
if self.w_init_gain in ['zero']:
torch.nn.init.zeros_(self.weight)
elif self.w_init_gain in ['relu', 'leaky_relu']:
torch.nn.init.kaiming_uniform_(self.weight, nonlinearity= self.w_init_gain)
elif self.w_init_gain == 'glu':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.kaiming_uniform_(self.weight[:self.out_channels // 2], nonlinearity= 'linear')
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
elif self.w_init_gain == 'gate':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.xavier_uniform_(self.weight[:self.out_channels // 2], gain= torch.nn.init.calculate_gain('tanh'))
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
else:
torch.nn.init.xavier_uniform_(self.weight, gain= torch.nn.init.calculate_gain(self.w_init_gain))
if not self.bias is None:
torch.nn.init.zeros_(self.bias)
class Linear(torch.nn.Linear):
def __init__(self, w_init_gain= 'linear', *args, **kwargs):
self.w_init_gain = w_init_gain
super().__init__(*args, **kwargs)
def reset_parameters(self):
if self.w_init_gain in ['zero']:
torch.nn.init.zeros_(self.weight)
elif self.w_init_gain in ['relu', 'leaky_relu']:
torch.nn.init.kaiming_uniform_(self.weight, nonlinearity= self.w_init_gain)
elif self.w_init_gain == 'glu':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.kaiming_uniform_(self.weight[:self.out_channels // 2], nonlinearity= 'linear')
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
else:
torch.nn.init.xavier_uniform_(self.weight, gain= torch.nn.init.calculate_gain(self.w_init_gain))
if not self.bias is None:
torch.nn.init.zeros_(self.bias)
class Lambda(torch.nn.Module):
def __init__(self, lambd):
super().__init__()
self.lambd = lambd
def forward(self, x):
return self.lambd(x)
class Residual(torch.nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, *args, **kwargs):
return self.module(*args, **kwargs)
class LayerNorm(torch.nn.Module):
def __init__(self, num_features: int, eps: float= 1e-5):
super().__init__()
self.eps = eps
self.gamma = torch.nn.Parameter(torch.ones(num_features))
self.beta = torch.nn.Parameter(torch.zeros(num_features))
def forward(self, inputs: torch.Tensor):
means = inputs.mean(dim= 1, keepdim= True)
variances = (inputs - means).pow(2.0).mean(dim= 1, keepdim= True)
x = (inputs - means) * (variances + self.eps).rsqrt()
shape = [1, -1] + [1] * (x.ndim - 2)
return x * self.gamma.view(*shape) + self.beta.view(*shape)
class LightweightConv1d(torch.nn.Module):
'''
Args:
input_size: # of channels of the input and output
kernel_size: convolution channels
padding: padding
num_heads: number of heads used. The weight is of shape
`(num_heads, 1, kernel_size)`
weight_softmax: normalize the weight with softmax before the convolution
Shape:
Input: BxCxT, i.e. (batch_size, input_size, timesteps)
Output: BxCxT, i.e. (batch_size, input_size, timesteps)
Attributes:
weight: the learnable weights of the module of shape
`(num_heads, 1, kernel_size)`
bias: the learnable bias of the module of shape `(input_size)`
'''
def __init__(
self,
input_size,
kernel_size=1,
padding=0,
num_heads=1,
weight_softmax=False,
bias=False,
weight_dropout=0.0,
w_init_gain= 'linear'
):
super().__init__()
self.input_size = input_size
self.kernel_size = kernel_size
self.num_heads = num_heads
self.padding = padding
self.weight_softmax = weight_softmax
self.weight = torch.nn.Parameter(torch.Tensor(num_heads, 1, kernel_size))
self.w_init_gain = w_init_gain
if bias:
self.bias = torch.nn.Parameter(torch.Tensor(input_size))
else:
self.bias = None
self.weight_dropout_module = FairseqDropout(
weight_dropout, module_name=self.__class__.__name__
)
self.reset_parameters()
def reset_parameters(self):
if self.w_init_gain in ['relu', 'leaky_relu']:
torch.nn.init.kaiming_uniform_(self.weight, nonlinearity= self.w_init_gain)
elif self.w_init_gain == 'glu':
assert self.out_channels % 2 == 0, 'The out_channels of GLU requires even number.'
torch.nn.init.kaiming_uniform_(self.weight[:self.out_channels // 2], nonlinearity= 'linear')
torch.nn.init.xavier_uniform_(self.weight[self.out_channels // 2:], gain= torch.nn.init.calculate_gain('sigmoid'))
else:
torch.nn.init.xavier_uniform_(self.weight, gain= torch.nn.init.calculate_gain(self.w_init_gain))
if not self.bias is None:
torch.nn.init.zeros_(self.bias)
def forward(self, input):
"""
input size: B x C x T
output size: B x C x T
"""
B, C, T = input.size()
H = self.num_heads
weight = self.weight
if self.weight_softmax:
weight = weight.softmax(dim=-1)
weight = self.weight_dropout_module(weight)
# Merge every C/H entries into the batch dimension (C = self.input_size)
# B x C x T -> (B * C/H) x H x T
# One can also expand the weight to C x 1 x K by a factor of C/H
# and do not reshape the input instead, which is slow though
input = input.view(-1, H, T)
output = torch.nn.functional.conv1d(input, weight, padding=self.padding, groups=self.num_heads)
output = output.view(B, C, T)
if self.bias is not None:
output = output + self.bias.view(1, -1, 1)
return output
class FairseqDropout(torch.nn.Module):
def __init__(self, p, module_name=None):
super().__init__()
self.p = p
self.module_name = module_name
self.apply_during_inference = False
def forward(self, x, inplace: bool = False):
if self.training or self.apply_during_inference:
return torch.nn.functional.dropout(x, p=self.p, training=True, inplace=inplace)
else:
return x
class LinearAttention(torch.nn.Module):
def __init__(
self,
channels: int,
calc_channels: int,
num_heads: int,
dropout_rate: float= 0.1,
use_scale: bool= True,
use_residual: bool= True,
use_norm: bool= True
):
super().__init__()
assert calc_channels % num_heads == 0
self.calc_channels = calc_channels
self.num_heads = num_heads
self.use_scale = use_scale
self.use_residual = use_residual
self.use_norm = use_norm
self.prenet = Conv1d(
in_channels= channels,
out_channels= calc_channels * 3,
kernel_size= 1,
bias=False,
w_init_gain= 'linear'
)
self.projection = Conv1d(
in_channels= calc_channels,
out_channels= channels,
kernel_size= 1,
w_init_gain= 'linear'
)
self.dropout = torch.nn.Dropout(p= dropout_rate)
if use_scale:
self.scale = torch.nn.Parameter(torch.zeros(1))
if use_norm:
self.norm = LayerNorm(num_features= channels)
def forward(self, x: torch.Tensor, *args, **kwargs):
'''
x: [Batch, Enc_d, Enc_t]
'''
residuals = x
x = self.prenet(x) # [Batch, Calc_d * 3, Enc_t]
x = x.view(x.size(0), self.num_heads, x.size(1) // self.num_heads, x.size(2)) # [Batch, Head, Calc_d // Head * 3, Enc_t]
queries, keys, values = x.chunk(chunks= 3, dim= 2) # [Batch, Head, Calc_d // Head, Enc_t] * 3
keys = (keys + 1e-5).softmax(dim= 3)
contexts = keys @ values.permute(0, 1, 3, 2) # [Batch, Head, Calc_d // Head, Calc_d // Head]
contexts = contexts.permute(0, 1, 3, 2) @ queries # [Batch, Head, Calc_d // Head, Enc_t]
contexts = contexts.view(contexts.size(0), contexts.size(1) * contexts.size(2), contexts.size(3)) # [Batch, Calc_d, Enc_t]
contexts = self.projection(contexts) # [Batch, Enc_d, Enc_t]
if self.use_scale:
contexts = self.scale * contexts
contexts = self.dropout(contexts)
if self.use_residual:
contexts = contexts + residuals
if self.use_norm:
contexts = self.norm(contexts)
return contexts
|