infer_rvc_python 1.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- infer_rvc_python/__init__.py +1 -0
- infer_rvc_python/lib/__init__.py +0 -0
- infer_rvc_python/lib/audio.py +21 -0
- infer_rvc_python/lib/infer_pack/__init__.py +0 -0
- infer_rvc_python/lib/infer_pack/attentions.py +417 -0
- infer_rvc_python/lib/infer_pack/commons.py +166 -0
- infer_rvc_python/lib/infer_pack/models.py +1142 -0
- infer_rvc_python/lib/infer_pack/modules.py +522 -0
- infer_rvc_python/lib/infer_pack/transforms.py +209 -0
- infer_rvc_python/lib/log_config.py +48 -0
- infer_rvc_python/lib/rmvpe.py +422 -0
- infer_rvc_python/main.py +967 -0
- infer_rvc_python/root_pipe.py +454 -0
- infer_rvc_python-1.3.0.dist-info/LICENSE +21 -0
- infer_rvc_python-1.3.0.dist-info/METADATA +175 -0
- infer_rvc_python-1.3.0.dist-info/RECORD +17 -0
- infer_rvc_python-1.3.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .main import BaseLoader
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import ffmpeg
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def load_audio(file, sr):
|
|
6
|
+
try:
|
|
7
|
+
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
|
|
8
|
+
# This launches a subprocess to decode audio while down-mixing and resampling as necessary.
|
|
9
|
+
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
|
|
10
|
+
file = (
|
|
11
|
+
file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
|
12
|
+
) # To prevent beginners from copying paths with leading or trailing spaces, quotation marks, and line breaks.
|
|
13
|
+
out, _ = (
|
|
14
|
+
ffmpeg.input(file, threads=0)
|
|
15
|
+
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
|
16
|
+
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
|
17
|
+
)
|
|
18
|
+
except Exception as e:
|
|
19
|
+
raise RuntimeError(f"Failed to load audio: {e}")
|
|
20
|
+
|
|
21
|
+
return np.frombuffer(out, np.float32).flatten()
|
|
File without changes
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
import math
|
|
3
|
+
import numpy as np
|
|
4
|
+
import torch
|
|
5
|
+
from torch import nn
|
|
6
|
+
from torch.nn import functional as F
|
|
7
|
+
|
|
8
|
+
from infer_rvc_python.lib.infer_pack import commons
|
|
9
|
+
from infer_rvc_python.lib.infer_pack import modules
|
|
10
|
+
from infer_rvc_python.lib.infer_pack.modules import LayerNorm
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Encoder(nn.Module):
|
|
14
|
+
def __init__(
|
|
15
|
+
self,
|
|
16
|
+
hidden_channels,
|
|
17
|
+
filter_channels,
|
|
18
|
+
n_heads,
|
|
19
|
+
n_layers,
|
|
20
|
+
kernel_size=1,
|
|
21
|
+
p_dropout=0.0,
|
|
22
|
+
window_size=10,
|
|
23
|
+
**kwargs
|
|
24
|
+
):
|
|
25
|
+
super().__init__()
|
|
26
|
+
self.hidden_channels = hidden_channels
|
|
27
|
+
self.filter_channels = filter_channels
|
|
28
|
+
self.n_heads = n_heads
|
|
29
|
+
self.n_layers = n_layers
|
|
30
|
+
self.kernel_size = kernel_size
|
|
31
|
+
self.p_dropout = p_dropout
|
|
32
|
+
self.window_size = window_size
|
|
33
|
+
|
|
34
|
+
self.drop = nn.Dropout(p_dropout)
|
|
35
|
+
self.attn_layers = nn.ModuleList()
|
|
36
|
+
self.norm_layers_1 = nn.ModuleList()
|
|
37
|
+
self.ffn_layers = nn.ModuleList()
|
|
38
|
+
self.norm_layers_2 = nn.ModuleList()
|
|
39
|
+
for i in range(self.n_layers):
|
|
40
|
+
self.attn_layers.append(
|
|
41
|
+
MultiHeadAttention(
|
|
42
|
+
hidden_channels,
|
|
43
|
+
hidden_channels,
|
|
44
|
+
n_heads,
|
|
45
|
+
p_dropout=p_dropout,
|
|
46
|
+
window_size=window_size,
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
|
50
|
+
self.ffn_layers.append(
|
|
51
|
+
FFN(
|
|
52
|
+
hidden_channels,
|
|
53
|
+
hidden_channels,
|
|
54
|
+
filter_channels,
|
|
55
|
+
kernel_size,
|
|
56
|
+
p_dropout=p_dropout,
|
|
57
|
+
)
|
|
58
|
+
)
|
|
59
|
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
|
60
|
+
|
|
61
|
+
def forward(self, x, x_mask):
|
|
62
|
+
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
|
63
|
+
x = x * x_mask
|
|
64
|
+
for i in range(self.n_layers):
|
|
65
|
+
y = self.attn_layers[i](x, x, attn_mask)
|
|
66
|
+
y = self.drop(y)
|
|
67
|
+
x = self.norm_layers_1[i](x + y)
|
|
68
|
+
|
|
69
|
+
y = self.ffn_layers[i](x, x_mask)
|
|
70
|
+
y = self.drop(y)
|
|
71
|
+
x = self.norm_layers_2[i](x + y)
|
|
72
|
+
x = x * x_mask
|
|
73
|
+
return x
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class Decoder(nn.Module):
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
hidden_channels,
|
|
80
|
+
filter_channels,
|
|
81
|
+
n_heads,
|
|
82
|
+
n_layers,
|
|
83
|
+
kernel_size=1,
|
|
84
|
+
p_dropout=0.0,
|
|
85
|
+
proximal_bias=False,
|
|
86
|
+
proximal_init=True,
|
|
87
|
+
**kwargs
|
|
88
|
+
):
|
|
89
|
+
super().__init__()
|
|
90
|
+
self.hidden_channels = hidden_channels
|
|
91
|
+
self.filter_channels = filter_channels
|
|
92
|
+
self.n_heads = n_heads
|
|
93
|
+
self.n_layers = n_layers
|
|
94
|
+
self.kernel_size = kernel_size
|
|
95
|
+
self.p_dropout = p_dropout
|
|
96
|
+
self.proximal_bias = proximal_bias
|
|
97
|
+
self.proximal_init = proximal_init
|
|
98
|
+
|
|
99
|
+
self.drop = nn.Dropout(p_dropout)
|
|
100
|
+
self.self_attn_layers = nn.ModuleList()
|
|
101
|
+
self.norm_layers_0 = nn.ModuleList()
|
|
102
|
+
self.encdec_attn_layers = nn.ModuleList()
|
|
103
|
+
self.norm_layers_1 = nn.ModuleList()
|
|
104
|
+
self.ffn_layers = nn.ModuleList()
|
|
105
|
+
self.norm_layers_2 = nn.ModuleList()
|
|
106
|
+
for i in range(self.n_layers):
|
|
107
|
+
self.self_attn_layers.append(
|
|
108
|
+
MultiHeadAttention(
|
|
109
|
+
hidden_channels,
|
|
110
|
+
hidden_channels,
|
|
111
|
+
n_heads,
|
|
112
|
+
p_dropout=p_dropout,
|
|
113
|
+
proximal_bias=proximal_bias,
|
|
114
|
+
proximal_init=proximal_init,
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
|
118
|
+
self.encdec_attn_layers.append(
|
|
119
|
+
MultiHeadAttention(
|
|
120
|
+
hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
|
124
|
+
self.ffn_layers.append(
|
|
125
|
+
FFN(
|
|
126
|
+
hidden_channels,
|
|
127
|
+
hidden_channels,
|
|
128
|
+
filter_channels,
|
|
129
|
+
kernel_size,
|
|
130
|
+
p_dropout=p_dropout,
|
|
131
|
+
causal=True,
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
|
135
|
+
|
|
136
|
+
def forward(self, x, x_mask, h, h_mask):
|
|
137
|
+
"""
|
|
138
|
+
x: decoder input
|
|
139
|
+
h: encoder output
|
|
140
|
+
"""
|
|
141
|
+
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
|
|
142
|
+
device=x.device, dtype=x.dtype
|
|
143
|
+
)
|
|
144
|
+
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
|
145
|
+
x = x * x_mask
|
|
146
|
+
for i in range(self.n_layers):
|
|
147
|
+
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
|
148
|
+
y = self.drop(y)
|
|
149
|
+
x = self.norm_layers_0[i](x + y)
|
|
150
|
+
|
|
151
|
+
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
|
152
|
+
y = self.drop(y)
|
|
153
|
+
x = self.norm_layers_1[i](x + y)
|
|
154
|
+
|
|
155
|
+
y = self.ffn_layers[i](x, x_mask)
|
|
156
|
+
y = self.drop(y)
|
|
157
|
+
x = self.norm_layers_2[i](x + y)
|
|
158
|
+
x = x * x_mask
|
|
159
|
+
return x
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class MultiHeadAttention(nn.Module):
|
|
163
|
+
def __init__(
|
|
164
|
+
self,
|
|
165
|
+
channels,
|
|
166
|
+
out_channels,
|
|
167
|
+
n_heads,
|
|
168
|
+
p_dropout=0.0,
|
|
169
|
+
window_size=None,
|
|
170
|
+
heads_share=True,
|
|
171
|
+
block_length=None,
|
|
172
|
+
proximal_bias=False,
|
|
173
|
+
proximal_init=False,
|
|
174
|
+
):
|
|
175
|
+
super().__init__()
|
|
176
|
+
assert channels % n_heads == 0
|
|
177
|
+
|
|
178
|
+
self.channels = channels
|
|
179
|
+
self.out_channels = out_channels
|
|
180
|
+
self.n_heads = n_heads
|
|
181
|
+
self.p_dropout = p_dropout
|
|
182
|
+
self.window_size = window_size
|
|
183
|
+
self.heads_share = heads_share
|
|
184
|
+
self.block_length = block_length
|
|
185
|
+
self.proximal_bias = proximal_bias
|
|
186
|
+
self.proximal_init = proximal_init
|
|
187
|
+
self.attn = None
|
|
188
|
+
|
|
189
|
+
self.k_channels = channels // n_heads
|
|
190
|
+
self.conv_q = nn.Conv1d(channels, channels, 1)
|
|
191
|
+
self.conv_k = nn.Conv1d(channels, channels, 1)
|
|
192
|
+
self.conv_v = nn.Conv1d(channels, channels, 1)
|
|
193
|
+
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
|
194
|
+
self.drop = nn.Dropout(p_dropout)
|
|
195
|
+
|
|
196
|
+
if window_size is not None:
|
|
197
|
+
n_heads_rel = 1 if heads_share else n_heads
|
|
198
|
+
rel_stddev = self.k_channels**-0.5
|
|
199
|
+
self.emb_rel_k = nn.Parameter(
|
|
200
|
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
|
201
|
+
* rel_stddev
|
|
202
|
+
)
|
|
203
|
+
self.emb_rel_v = nn.Parameter(
|
|
204
|
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
|
205
|
+
* rel_stddev
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
nn.init.xavier_uniform_(self.conv_q.weight)
|
|
209
|
+
nn.init.xavier_uniform_(self.conv_k.weight)
|
|
210
|
+
nn.init.xavier_uniform_(self.conv_v.weight)
|
|
211
|
+
if proximal_init:
|
|
212
|
+
with torch.no_grad():
|
|
213
|
+
self.conv_k.weight.copy_(self.conv_q.weight)
|
|
214
|
+
self.conv_k.bias.copy_(self.conv_q.bias)
|
|
215
|
+
|
|
216
|
+
def forward(self, x, c, attn_mask=None):
|
|
217
|
+
q = self.conv_q(x)
|
|
218
|
+
k = self.conv_k(c)
|
|
219
|
+
v = self.conv_v(c)
|
|
220
|
+
|
|
221
|
+
x, self.attn = self.attention(q, k, v, mask=attn_mask)
|
|
222
|
+
|
|
223
|
+
x = self.conv_o(x)
|
|
224
|
+
return x
|
|
225
|
+
|
|
226
|
+
def attention(self, query, key, value, mask=None):
|
|
227
|
+
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
|
228
|
+
b, d, t_s, t_t = (*key.size(), query.size(2))
|
|
229
|
+
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
|
230
|
+
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
|
231
|
+
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
|
232
|
+
|
|
233
|
+
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
|
234
|
+
if self.window_size is not None:
|
|
235
|
+
assert (
|
|
236
|
+
t_s == t_t
|
|
237
|
+
), "Relative attention is only available for self-attention."
|
|
238
|
+
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
|
239
|
+
rel_logits = self._matmul_with_relative_keys(
|
|
240
|
+
query / math.sqrt(self.k_channels), key_relative_embeddings
|
|
241
|
+
)
|
|
242
|
+
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
|
243
|
+
scores = scores + scores_local
|
|
244
|
+
if self.proximal_bias:
|
|
245
|
+
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
|
246
|
+
scores = scores + self._attention_bias_proximal(t_s).to(
|
|
247
|
+
device=scores.device, dtype=scores.dtype
|
|
248
|
+
)
|
|
249
|
+
if mask is not None:
|
|
250
|
+
scores = scores.masked_fill(mask == 0, -1e4)
|
|
251
|
+
if self.block_length is not None:
|
|
252
|
+
assert (
|
|
253
|
+
t_s == t_t
|
|
254
|
+
), "Local attention is only available for self-attention."
|
|
255
|
+
block_mask = (
|
|
256
|
+
torch.ones_like(scores)
|
|
257
|
+
.triu(-self.block_length)
|
|
258
|
+
.tril(self.block_length)
|
|
259
|
+
)
|
|
260
|
+
scores = scores.masked_fill(block_mask == 0, -1e4)
|
|
261
|
+
p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
|
262
|
+
p_attn = self.drop(p_attn)
|
|
263
|
+
output = torch.matmul(p_attn, value)
|
|
264
|
+
if self.window_size is not None:
|
|
265
|
+
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
|
266
|
+
value_relative_embeddings = self._get_relative_embeddings(
|
|
267
|
+
self.emb_rel_v, t_s
|
|
268
|
+
)
|
|
269
|
+
output = output + self._matmul_with_relative_values(
|
|
270
|
+
relative_weights, value_relative_embeddings
|
|
271
|
+
)
|
|
272
|
+
output = (
|
|
273
|
+
output.transpose(2, 3).contiguous().view(b, d, t_t)
|
|
274
|
+
) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
|
275
|
+
return output, p_attn
|
|
276
|
+
|
|
277
|
+
def _matmul_with_relative_values(self, x, y):
|
|
278
|
+
"""
|
|
279
|
+
x: [b, h, l, m]
|
|
280
|
+
y: [h or 1, m, d]
|
|
281
|
+
ret: [b, h, l, d]
|
|
282
|
+
"""
|
|
283
|
+
ret = torch.matmul(x, y.unsqueeze(0))
|
|
284
|
+
return ret
|
|
285
|
+
|
|
286
|
+
def _matmul_with_relative_keys(self, x, y):
|
|
287
|
+
"""
|
|
288
|
+
x: [b, h, l, d]
|
|
289
|
+
y: [h or 1, m, d]
|
|
290
|
+
ret: [b, h, l, m]
|
|
291
|
+
"""
|
|
292
|
+
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
|
293
|
+
return ret
|
|
294
|
+
|
|
295
|
+
def _get_relative_embeddings(self, relative_embeddings, length):
|
|
296
|
+
max_relative_position = 2 * self.window_size + 1
|
|
297
|
+
# Pad first before slice to avoid using cond ops.
|
|
298
|
+
pad_length = max(length - (self.window_size + 1), 0)
|
|
299
|
+
slice_start_position = max((self.window_size + 1) - length, 0)
|
|
300
|
+
slice_end_position = slice_start_position + 2 * length - 1
|
|
301
|
+
if pad_length > 0:
|
|
302
|
+
padded_relative_embeddings = F.pad(
|
|
303
|
+
relative_embeddings,
|
|
304
|
+
commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
|
|
305
|
+
)
|
|
306
|
+
else:
|
|
307
|
+
padded_relative_embeddings = relative_embeddings
|
|
308
|
+
used_relative_embeddings = padded_relative_embeddings[
|
|
309
|
+
:, slice_start_position:slice_end_position
|
|
310
|
+
]
|
|
311
|
+
return used_relative_embeddings
|
|
312
|
+
|
|
313
|
+
def _relative_position_to_absolute_position(self, x):
|
|
314
|
+
"""
|
|
315
|
+
x: [b, h, l, 2*l-1]
|
|
316
|
+
ret: [b, h, l, l]
|
|
317
|
+
"""
|
|
318
|
+
batch, heads, length, _ = x.size()
|
|
319
|
+
# Concat columns of pad to shift from relative to absolute indexing.
|
|
320
|
+
x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
|
|
321
|
+
|
|
322
|
+
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
|
323
|
+
x_flat = x.view([batch, heads, length * 2 * length])
|
|
324
|
+
x_flat = F.pad(
|
|
325
|
+
x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
# Reshape and slice out the padded elements.
|
|
329
|
+
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
|
|
330
|
+
:, :, :length, length - 1 :
|
|
331
|
+
]
|
|
332
|
+
return x_final
|
|
333
|
+
|
|
334
|
+
def _absolute_position_to_relative_position(self, x):
|
|
335
|
+
"""
|
|
336
|
+
x: [b, h, l, l]
|
|
337
|
+
ret: [b, h, l, 2*l-1]
|
|
338
|
+
"""
|
|
339
|
+
batch, heads, length, _ = x.size()
|
|
340
|
+
# padd along column
|
|
341
|
+
x = F.pad(
|
|
342
|
+
x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
|
|
343
|
+
)
|
|
344
|
+
x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
|
|
345
|
+
# add 0's in the beginning that will skew the elements after reshape
|
|
346
|
+
x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
|
|
347
|
+
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
|
|
348
|
+
return x_final
|
|
349
|
+
|
|
350
|
+
def _attention_bias_proximal(self, length):
|
|
351
|
+
"""Bias for self-attention to encourage attention to close positions.
|
|
352
|
+
Args:
|
|
353
|
+
length: an integer scalar.
|
|
354
|
+
Returns:
|
|
355
|
+
a Tensor with shape [1, 1, length, length]
|
|
356
|
+
"""
|
|
357
|
+
r = torch.arange(length, dtype=torch.float32)
|
|
358
|
+
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
|
359
|
+
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
class FFN(nn.Module):
|
|
363
|
+
def __init__(
|
|
364
|
+
self,
|
|
365
|
+
in_channels,
|
|
366
|
+
out_channels,
|
|
367
|
+
filter_channels,
|
|
368
|
+
kernel_size,
|
|
369
|
+
p_dropout=0.0,
|
|
370
|
+
activation=None,
|
|
371
|
+
causal=False,
|
|
372
|
+
):
|
|
373
|
+
super().__init__()
|
|
374
|
+
self.in_channels = in_channels
|
|
375
|
+
self.out_channels = out_channels
|
|
376
|
+
self.filter_channels = filter_channels
|
|
377
|
+
self.kernel_size = kernel_size
|
|
378
|
+
self.p_dropout = p_dropout
|
|
379
|
+
self.activation = activation
|
|
380
|
+
self.causal = causal
|
|
381
|
+
|
|
382
|
+
if causal:
|
|
383
|
+
self.padding = self._causal_padding
|
|
384
|
+
else:
|
|
385
|
+
self.padding = self._same_padding
|
|
386
|
+
|
|
387
|
+
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
|
388
|
+
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
|
389
|
+
self.drop = nn.Dropout(p_dropout)
|
|
390
|
+
|
|
391
|
+
def forward(self, x, x_mask):
|
|
392
|
+
x = self.conv_1(self.padding(x * x_mask))
|
|
393
|
+
if self.activation == "gelu":
|
|
394
|
+
x = x * torch.sigmoid(1.702 * x)
|
|
395
|
+
else:
|
|
396
|
+
x = torch.relu(x)
|
|
397
|
+
x = self.drop(x)
|
|
398
|
+
x = self.conv_2(self.padding(x * x_mask))
|
|
399
|
+
return x * x_mask
|
|
400
|
+
|
|
401
|
+
def _causal_padding(self, x):
|
|
402
|
+
if self.kernel_size == 1:
|
|
403
|
+
return x
|
|
404
|
+
pad_l = self.kernel_size - 1
|
|
405
|
+
pad_r = 0
|
|
406
|
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
|
407
|
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
|
408
|
+
return x
|
|
409
|
+
|
|
410
|
+
def _same_padding(self, x):
|
|
411
|
+
if self.kernel_size == 1:
|
|
412
|
+
return x
|
|
413
|
+
pad_l = (self.kernel_size - 1) // 2
|
|
414
|
+
pad_r = self.kernel_size // 2
|
|
415
|
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
|
416
|
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
|
417
|
+
return x
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import numpy as np
|
|
3
|
+
import torch
|
|
4
|
+
from torch import nn
|
|
5
|
+
from torch.nn import functional as F
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def init_weights(m, mean=0.0, std=0.01):
|
|
9
|
+
classname = m.__class__.__name__
|
|
10
|
+
if classname.find("Conv") != -1:
|
|
11
|
+
m.weight.data.normal_(mean, std)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_padding(kernel_size, dilation=1):
|
|
15
|
+
return int((kernel_size * dilation - dilation) / 2)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def convert_pad_shape(pad_shape):
|
|
19
|
+
l = pad_shape[::-1]
|
|
20
|
+
pad_shape = [item for sublist in l for item in sublist]
|
|
21
|
+
return pad_shape
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def kl_divergence(m_p, logs_p, m_q, logs_q):
|
|
25
|
+
"""KL(P||Q)"""
|
|
26
|
+
kl = (logs_q - logs_p) - 0.5
|
|
27
|
+
kl += (
|
|
28
|
+
0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
|
|
29
|
+
)
|
|
30
|
+
return kl
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def rand_gumbel(shape):
|
|
34
|
+
"""Sample from the Gumbel distribution, protect from overflows."""
|
|
35
|
+
uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
|
|
36
|
+
return -torch.log(-torch.log(uniform_samples))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def rand_gumbel_like(x):
|
|
40
|
+
g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
|
|
41
|
+
return g
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def slice_segments(x, ids_str, segment_size=4):
|
|
45
|
+
ret = torch.zeros_like(x[:, :, :segment_size])
|
|
46
|
+
for i in range(x.size(0)):
|
|
47
|
+
idx_str = ids_str[i]
|
|
48
|
+
idx_end = idx_str + segment_size
|
|
49
|
+
ret[i] = x[i, :, idx_str:idx_end]
|
|
50
|
+
return ret
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def slice_segments2(x, ids_str, segment_size=4):
|
|
54
|
+
ret = torch.zeros_like(x[:, :segment_size])
|
|
55
|
+
for i in range(x.size(0)):
|
|
56
|
+
idx_str = ids_str[i]
|
|
57
|
+
idx_end = idx_str + segment_size
|
|
58
|
+
ret[i] = x[i, idx_str:idx_end]
|
|
59
|
+
return ret
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def rand_slice_segments(x, x_lengths=None, segment_size=4):
|
|
63
|
+
b, d, t = x.size()
|
|
64
|
+
if x_lengths is None:
|
|
65
|
+
x_lengths = t
|
|
66
|
+
ids_str_max = x_lengths - segment_size + 1
|
|
67
|
+
ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
|
|
68
|
+
ret = slice_segments(x, ids_str, segment_size)
|
|
69
|
+
return ret, ids_str
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
|
|
73
|
+
position = torch.arange(length, dtype=torch.float)
|
|
74
|
+
num_timescales = channels // 2
|
|
75
|
+
log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
|
|
76
|
+
num_timescales - 1
|
|
77
|
+
)
|
|
78
|
+
inv_timescales = min_timescale * torch.exp(
|
|
79
|
+
torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
|
|
80
|
+
)
|
|
81
|
+
scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
|
|
82
|
+
signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
|
|
83
|
+
signal = F.pad(signal, [0, 0, 0, channels % 2])
|
|
84
|
+
signal = signal.view(1, channels, length)
|
|
85
|
+
return signal
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
|
|
89
|
+
b, channels, length = x.size()
|
|
90
|
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
|
91
|
+
return x + signal.to(dtype=x.dtype, device=x.device)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
|
|
95
|
+
b, channels, length = x.size()
|
|
96
|
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
|
97
|
+
return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def subsequent_mask(length):
|
|
101
|
+
mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
|
|
102
|
+
return mask
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@torch.jit.script
|
|
106
|
+
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
|
107
|
+
n_channels_int = n_channels[0]
|
|
108
|
+
in_act = input_a + input_b
|
|
109
|
+
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
|
110
|
+
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
|
111
|
+
acts = t_act * s_act
|
|
112
|
+
return acts
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def convert_pad_shape(pad_shape):
|
|
116
|
+
l = pad_shape[::-1]
|
|
117
|
+
pad_shape = [item for sublist in l for item in sublist]
|
|
118
|
+
return pad_shape
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def shift_1d(x):
|
|
122
|
+
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
|
|
123
|
+
return x
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def sequence_mask(length, max_length=None):
|
|
127
|
+
if max_length is None:
|
|
128
|
+
max_length = length.max()
|
|
129
|
+
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
|
130
|
+
return x.unsqueeze(0) < length.unsqueeze(1)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def generate_path(duration, mask):
|
|
134
|
+
"""
|
|
135
|
+
duration: [b, 1, t_x]
|
|
136
|
+
mask: [b, 1, t_y, t_x]
|
|
137
|
+
"""
|
|
138
|
+
device = duration.device
|
|
139
|
+
|
|
140
|
+
b, _, t_y, t_x = mask.shape
|
|
141
|
+
cum_duration = torch.cumsum(duration, -1)
|
|
142
|
+
|
|
143
|
+
cum_duration_flat = cum_duration.view(b * t_x)
|
|
144
|
+
path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
|
|
145
|
+
path = path.view(b, t_x, t_y)
|
|
146
|
+
path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
|
|
147
|
+
path = path.unsqueeze(1).transpose(2, 3) * mask
|
|
148
|
+
return path
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def clip_grad_value_(parameters, clip_value, norm_type=2):
|
|
152
|
+
if isinstance(parameters, torch.Tensor):
|
|
153
|
+
parameters = [parameters]
|
|
154
|
+
parameters = list(filter(lambda p: p.grad is not None, parameters))
|
|
155
|
+
norm_type = float(norm_type)
|
|
156
|
+
if clip_value is not None:
|
|
157
|
+
clip_value = float(clip_value)
|
|
158
|
+
|
|
159
|
+
total_norm = 0
|
|
160
|
+
for p in parameters:
|
|
161
|
+
param_norm = p.grad.data.norm(norm_type)
|
|
162
|
+
total_norm += param_norm.item() ** norm_type
|
|
163
|
+
if clip_value is not None:
|
|
164
|
+
p.grad.data.clamp_(min=-clip_value, max=clip_value)
|
|
165
|
+
total_norm = total_norm ** (1.0 / norm_type)
|
|
166
|
+
return total_norm
|