Skip to content

Commit

Permalink
Автоматическое форматирование кода (#21)
Browse files Browse the repository at this point in the history
Автоматическое форматирование кода с помощью black
  • Loading branch information
Bebra777228 authored Sep 5, 2024
2 parents b07ecf2 + c627f80 commit 2e99f88
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 126 deletions.
32 changes: 8 additions & 24 deletions rvc/lib/algorithm/attentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def attention(self, query, key, value, mask=None):

scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
if self.window_size is not None:
assert (
t_s == t_t
), "Relative attention is only available for self-attention."
assert t_s == t_t, "Relative attention is only available for self-attention."
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
rel_logits = self._matmul_with_relative_keys(
query / math.sqrt(self.k_channels), key_relative_embeddings
Expand All @@ -95,9 +93,7 @@ def attention(self, query, key, value, mask=None):
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e4)
if self.block_length is not None:
assert (
t_s == t_t
), "Local attention is only available for self-attention."
assert t_s == t_t, "Local attention is only available for self-attention."
block_mask = (
torch.ones_like(scores)
.triu(-self.block_length)
Expand All @@ -109,15 +105,11 @@ def attention(self, query, key, value, mask=None):
output = torch.matmul(p_attn, value)
if self.window_size is not None:
relative_weights = self._absolute_position_to_relative_position(p_attn)
value_relative_embeddings = self._get_relative_embeddings(
self.emb_rel_v, t_s
)
value_relative_embeddings = self._get_relative_embeddings(self.emb_rel_v, t_s)
output = output + self._matmul_with_relative_values(
relative_weights, value_relative_embeddings
)
output = (
output.transpose(2, 3).contiguous().view(b, d, t_t)
)
output = output.transpose(2, 3).contiguous().view(b, d, t_t)
return output, p_attn

def _matmul_with_relative_values(self, x, y):
Expand Down Expand Up @@ -147,14 +139,10 @@ def _get_relative_embeddings(self, relative_embeddings, length):
def _relative_position_to_absolute_position(self, x):
batch, heads, length, _ = x.size()

x = F.pad(
x, convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]])
)
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))

x_flat = x.view([batch, heads, length * 2 * length])
x_flat = F.pad(
x_flat, convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
)
x_flat = F.pad(x_flat, convert_pad_shape([[0, 0], [0, 0], [0, length - 1]]))

x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
:, :, :length, length - 1 :
Expand All @@ -163,13 +151,9 @@ def _relative_position_to_absolute_position(self, x):

def _absolute_position_to_relative_position(self, x):
batch, heads, length, _ = x.size()
x = F.pad(
x, convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
)
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]]))
x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
x_flat = F.pad(
x_flat, convert_pad_shape([[0, 0], [0, 0], [length, 0]])
)
x_flat = F.pad(x_flat, convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
return x_final

Expand Down
4 changes: 1 addition & 3 deletions rvc/lib/algorithm/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def convert_pad_shape(pad_shape):

def kl_divergence(m_p, logs_p, m_q, logs_q):
kl = (logs_q - logs_p) - 0.5
kl += (
0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
)
kl += 0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
return kl


Expand Down
8 changes: 2 additions & 6 deletions rvc/lib/algorithm/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,10 @@ def forward(self, f0: torch.Tensor, upp: int):
f0_buf[:, :, 0] = f0[:, :, 0]
f0_buf[:, :, 1:] = (
f0_buf[:, :, 0:1]
* torch.arange(2, self.harmonic_num + 2, device=f0.device)[
None, None, :
]
* torch.arange(2, self.harmonic_num + 2, device=f0.device)[None, None, :]
)
rad_values = (f0_buf / float(self.sample_rate)) % 1
rand_ini = torch.rand(
f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device
)
rand_ini = torch.rand(f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device)
rand_ini[:, 0] = 0
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
tmp_over_one = torch.cumsum(rad_values, 1)
Expand Down
16 changes: 4 additions & 12 deletions rvc/lib/algorithm/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ def __init__(
self.drop = nn.Dropout(p_dropout)

if gin_channels != 0:
cond_layer = nn.Conv1d(
gin_channels, 2 * hidden_channels * n_layers, 1
)
self.cond_layer = weight_norm(
cond_layer, name="weight"
)
cond_layer = nn.Conv1d(gin_channels, 2 * hidden_channels * n_layers, 1)
self.cond_layer = weight_norm(cond_layer, name="weight")

dilations = [dilation_rate**i for i in range(n_layers)]
paddings = [(kernel_size * d - d) // 2 for d in dilations]
Expand All @@ -48,19 +44,15 @@ def __init__(
dilation=dilations[i],
padding=paddings[i],
)
in_layer = weight_norm(
in_layer, name="weight"
)
in_layer = weight_norm(in_layer, name="weight")
self.in_layers.append(in_layer)

res_skip_channels = (
hidden_channels if i == n_layers - 1 else 2 * hidden_channels
)

res_skip_layer = nn.Conv1d(hidden_channels, res_skip_channels, 1)
res_skip_layer = weight_norm(
res_skip_layer, name="weight"
)
res_skip_layer = weight_norm(res_skip_layer, name="weight")
self.res_skip_layers.append(res_skip_layer)

def forward(self, x, x_mask, g=None, **kwargs):
Expand Down
4 changes: 1 addition & 3 deletions rvc/lib/algorithm/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ def __init__(self, channels, eps=1e-5):

def forward(self, x):
x = x.transpose(1, -1)
x = F.layer_norm(
x, (x.size(-1),), self.gamma, self.beta, self.eps
)
x = F.layer_norm(x, (x.size(-1),), self.gamma, self.beta, self.eps)
return x.transpose(1, -1)
7 changes: 2 additions & 5 deletions rvc/lib/algorithm/nsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def __init__(
self.num_kernels = len(resblock_kernel_sizes)
self.num_upsamples = len(upsample_rates)
self.f0_upsamp = nn.Upsample(scale_factor=math.prod(upsample_rates))
self.m_source = SourceModuleHnNSF(
sample_rate=sr, harmonic_num=0, is_half=is_half
)
self.m_source = SourceModuleHnNSF(sample_rate=sr, harmonic_num=0, is_half=is_half)

self.conv_pre = nn.Conv1d(
initial_channel, upsample_initial_channel, 7, 1, padding=3
Expand All @@ -72,8 +70,7 @@ def __init__(
self.noise_convs = nn.ModuleList()

channels = [
upsample_initial_channel // (2 ** (i + 1))
for i in range(len(upsample_rates))
upsample_initial_channel // (2 ** (i + 1)) for i in range(len(upsample_rates))
]
stride_f0s = [
math.prod(upsample_rates[i + 1 :]) if i + 1 < len(upsample_rates) else 1
Expand Down
4 changes: 1 addition & 3 deletions rvc/lib/algorithm/residuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ def __init__(
p_dropout=p_dropout,
gin_channels=gin_channels,
)
self.post = nn.Conv1d(
hidden_channels, self.half_channels * (2 - mean_only), 1
)
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
self.post.weight.data.zero_()
self.post.bias.data.zero_()

Expand Down
4 changes: 1 addition & 3 deletions rvc/lib/my_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

def load_audio(file, sr):
try:
file = (
file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
)
file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
out, _ = (
ffmpeg.input(file, threads=0)
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
Expand Down
32 changes: 8 additions & 24 deletions rvc/lib/predictors/FCPE.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def load_wav_to_torch(full_path, target_sr=None, return_empty_on_exception=False
return [], sample_rate or target_sr or 48000
if target_sr is not None and sample_rate != target_sr:
data = torch.from_numpy(
librosa.core.resample(
data.numpy(), orig_sr=sample_rate, target_sr=target_sr
)
librosa.core.resample(data.numpy(), orig_sr=sample_rate, target_sr=target_sr)
)
sample_rate = target_sr

Expand Down Expand Up @@ -189,9 +187,7 @@ def softmax_kernel(
if is_query:
data_dash = ratio * (
torch.exp(
data_dash
- diag_data
- torch.max(data_dash, dim=-1, keepdim=True).values
data_dash - diag_data - torch.max(data_dash, dim=-1, keepdim=True).values
)
+ eps
)
Expand Down Expand Up @@ -359,16 +355,12 @@ def gaussian_orthogonal_random_matrix(
block_list = []

for _ in range(nb_full_blocks):
q = orthogonal_matrix_chunk(
nb_columns, qr_uniform_q=qr_uniform_q, device=device
)
q = orthogonal_matrix_chunk(nb_columns, qr_uniform_q=qr_uniform_q, device=device)
block_list.append(q)

remaining_rows = nb_rows - nb_full_blocks * nb_columns
if remaining_rows > 0:
q = orthogonal_matrix_chunk(
nb_columns, qr_uniform_q=qr_uniform_q, device=device
)
q = orthogonal_matrix_chunk(nb_columns, qr_uniform_q=qr_uniform_q, device=device)
block_list.append(q[:remaining_rows])

final_matrix = torch.cat(block_list)
Expand Down Expand Up @@ -669,9 +661,7 @@ def forward(
def cents_decoder(self, y, mask=True):
B, N, _ = y.size()
ci = self.cent_table[None, None, :].expand(B, N, -1)
rtn = torch.sum(ci * y, dim=-1, keepdim=True) / torch.sum(
y, dim=-1, keepdim=True
)
rtn = torch.sum(ci * y, dim=-1, keepdim=True) / torch.sum(y, dim=-1, keepdim=True)
if mask:
confident = torch.max(y, dim=-1, keepdim=True)[0]
confident_mask = torch.ones_like(confident)
Expand Down Expand Up @@ -786,13 +776,9 @@ def extract_mel(self, audio, sample_rate, keyshift=0, train=False):
)
audio_res = self.resample_kernel[key_str](audio)

mel = self.extract_nvstft(
audio_res, keyshift=keyshift, train=train
)
mel = self.extract_nvstft(audio_res, keyshift=keyshift, train=train)
n_frames = int(audio.shape[1] // self.hop_size) + 1
mel = (
torch.cat((mel, mel[:, -1:, :]), 1) if n_frames > int(mel.shape[1]) else mel
)
mel = torch.cat((mel, mel[:, -1:, :]), 1) if n_frames > int(mel.shape[1]) else mel
mel = mel[:, :n_frames, :] if n_frames < int(mel.shape[1]) else mel
return mel

Expand Down Expand Up @@ -847,9 +833,7 @@ def repeat_expand(
):
ndim = content.ndim
content = (
content[None, None]
if ndim == 1
else content[None] if ndim == 2 else content
content[None, None] if ndim == 1 else content[None] if ndim == 2 else content
)
assert content.ndim == 3
is_np = isinstance(content, np.ndarray)
Expand Down
Loading

0 comments on commit 2e99f88

Please sign in to comment.