Skip to content

Commit e9c4c07

Browse files
Politreesgithub-actions[bot]
authored andcommitted
style(ruff): автоматическое форматирование кода
1 parent 8df70ec commit e9c4c07

22 files changed

Lines changed: 553 additions & 458 deletions

download_files.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ def dl_model(link, model_name, dir_name):
2525
# Получаем общий размер файла
2626
total_size = int(r.headers.get("content-length", 0))
2727
# Используем tqdm для отображения прогресса
28-
with open(file_path, "wb") as f, tqdm(
29-
desc=f"Установка {model_name}",
30-
total=total_size,
31-
unit="iB",
32-
unit_scale=True,
33-
unit_divisor=1024,
34-
) as bar:
28+
with (
29+
open(file_path, "wb") as f,
30+
tqdm(
31+
desc=f"Установка {model_name}",
32+
total=total_size,
33+
unit="iB",
34+
unit_scale=True,
35+
unit_divisor=1024,
36+
) as bar,
37+
):
3538
for chunk in r.iter_content(chunk_size=8192):
3639
f.write(chunk)
3740
bar.update(len(chunk))

rvc/lib/algorithm/attentions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77

88
class MultiHeadAttention(torch.nn.Module):
9-
"""
10-
Multi-head attention module with optional relative positional encoding and proximal bias.
9+
"""Multi-head attention module with optional relative positional encoding and proximal bias.
1110
1211
Args:
1312
channels (int): Number of input channels.
@@ -19,6 +18,7 @@ class MultiHeadAttention(torch.nn.Module):
1918
block_length (int, optional): Block length for local attention. Defaults to None.
2019
proximal_bias (bool, optional): Whether to use proximal bias in self-attention. Defaults to False.
2120
proximal_init (bool, optional): Whether to initialize the key projection weights the same as query projection weights. Defaults to False.
21+
2222
"""
2323

2424
def __init__(
@@ -164,8 +164,7 @@ def _attention_bias_proximal(self, length):
164164

165165

166166
class FFN(torch.nn.Module):
167-
"""
168-
Feed-forward network module.
167+
"""Feed-forward network module.
169168
170169
Args:
171170
in_channels (int): Number of input channels.
@@ -175,6 +174,7 @@ class FFN(torch.nn.Module):
175174
p_dropout (float, optional): Dropout probability. Defaults to 0.0.
176175
activation (str, optional): Activation function to use. Defaults to None.
177176
causal (bool, optional): Whether to use causal padding in the convolution layers. Defaults to False.
177+
178178
"""
179179

180180
def __init__(

rvc/lib/algorithm/commons.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
1-
from typing import Optional
2-
31
import torch
42

53

64
def init_weights(m, mean=0.0, std=0.01):
7-
"""
8-
Initialize the weights of a module.
5+
"""Initialize the weights of a module.
96
107
Args:
118
m: The module to initialize.
129
mean: The mean of the normal distribution.
1310
std: The standard deviation of the normal distribution.
11+
1412
"""
1513
classname = m.__class__.__name__
1614
if classname.find("Conv") != -1:
1715
m.weight.data.normal_(mean, std)
1816

1917

2018
def get_padding(kernel_size, dilation=1):
21-
"""
22-
Calculate the padding needed for a convolution.
19+
"""Calculate the padding needed for a convolution.
2320
2421
Args:
2522
kernel_size: The size of the kernel.
2623
dilation: The dilation of the convolution.
24+
2725
"""
2826
return int((kernel_size * dilation - dilation) / 2)
2927

3028

3129
def convert_pad_shape(pad_shape):
32-
"""
33-
Convert the pad shape to a list of integers.
30+
"""Convert the pad shape to a list of integers.
3431
3532
Args:
3633
pad_shape: The pad shape..
34+
3735
"""
3836
l = pad_shape[::-1]
3937
pad_shape = [item for sublist in l for item in sublist]
4038
return pad_shape
4139

4240

4341
def slice_segments(x: torch.Tensor, ids_str: torch.Tensor, segment_size: int = 4, dim: int = 2):
44-
"""
45-
Slice segments from a tensor, handling tensors with different numbers of dimensions.
42+
"""Slice segments from a tensor, handling tensors with different numbers of dimensions.
4643
4744
Args:
4845
x (torch.Tensor): The tensor to slice.
4946
ids_str (torch.Tensor): The starting indices of the segments.
5047
segment_size (int, optional): The size of each segment. Defaults to 4.
5148
dim (int, optional): The dimension to slice across (2D or 3D tensors). Defaults to 2.
49+
5250
"""
5351
if dim == 2:
5452
ret = torch.zeros_like(x[:, :segment_size])
@@ -67,13 +65,13 @@ def slice_segments(x: torch.Tensor, ids_str: torch.Tensor, segment_size: int = 4
6765

6866

6967
def rand_slice_segments(x, x_lengths=None, segment_size=4):
70-
"""
71-
Randomly slice segments from a tensor.
68+
"""Randomly slice segments from a tensor.
7269
7370
Args:
7471
x: The tensor to slice.
7572
x_lengths: The lengths of the sequences.
7673
segment_size: The size of each segment.
74+
7775
"""
7876
b, _, t = x.size()
7977
if x_lengths is None:
@@ -86,13 +84,13 @@ def rand_slice_segments(x, x_lengths=None, segment_size=4):
8684

8785
@torch.jit.script
8886
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
89-
"""
90-
Fused add tanh sigmoid multiply operation.
87+
"""Fused add tanh sigmoid multiply operation.
9188
9289
Args:
9390
input_a: The first input tensor.
9491
input_b: The second input tensor.
9592
n_channels: The number of channels.
93+
9694
"""
9795
n_channels_int = n_channels[0]
9896
in_act = input_a + input_b
@@ -102,13 +100,13 @@ def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
102100
return acts
103101

104102

105-
def sequence_mask(length: torch.Tensor, max_length: Optional[int] = None):
106-
"""
107-
Generate a sequence mask.
103+
def sequence_mask(length: torch.Tensor, max_length: int | None = None):
104+
"""Generate a sequence mask.
108105
109106
Args:
110107
length: The lengths of the sequences.
111108
max_length: The maximum length of the sequences.
109+
112110
"""
113111
if max_length is None:
114112
max_length = length.max()
@@ -117,12 +115,12 @@ def sequence_mask(length: torch.Tensor, max_length: Optional[int] = None):
117115

118116

119117
def grad_norm(parameters, norm_type: float = 2.0):
120-
"""
121-
Calculates norm of parameter gradients
118+
"""Calculates norm of parameter gradients
122119
123120
Args:
124121
parameters: The list of parameters to clip.
125122
norm_type: The type of norm to use for clipping.
123+
126124
"""
127125
if isinstance(parameters, torch.Tensor):
128126
parameters = [parameters]

rvc/lib/algorithm/discriminators.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88

99
class MultiPeriodDiscriminator(torch.nn.Module):
10-
"""
11-
Multi-period discriminator.
10+
"""Multi-period discriminator.
1211
1312
This class implements a multi-period discriminator, which is used to
1413
discriminate between real and fake audio signals. The discriminator
@@ -20,7 +19,9 @@ class MultiPeriodDiscriminator(torch.nn.Module):
2019
def __init__(self, checkpointing: bool = False):
2120
super().__init__()
2221
self.checkpointing = checkpointing
23-
self.discriminators = torch.nn.ModuleList([DiscriminatorS()] + [DiscriminatorP(period) for period in [2, 3, 5, 7, 11, 17, 23, 37]]) # periods
22+
self.discriminators = torch.nn.ModuleList(
23+
[DiscriminatorS()] + [DiscriminatorP(period) for period in [2, 3, 5, 7, 11, 17, 23, 37]]
24+
) # periods
2425

2526
def forward(self, y, y_hat):
2627
y_d_rs, y_d_gs, fmap_rs, fmap_gs = [], [], [], []
@@ -40,8 +41,7 @@ def forward(self, y, y_hat):
4041

4142

4243
class DiscriminatorS(torch.nn.Module):
43-
"""
44-
Discriminator for the short-term component.
44+
"""Discriminator for the short-term component.
4545
4646
This class implements a discriminator for the short-term component
4747
of the audio signal. The discriminator is composed of a series of
@@ -58,7 +58,7 @@ def __init__(self):
5858
weight_norm(torch.nn.Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
5959
weight_norm(torch.nn.Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
6060
weight_norm(torch.nn.Conv1d(1024, 1024, 5, 1, padding=2)),
61-
]
61+
],
6262
)
6363
self.conv_post = weight_norm(torch.nn.Conv1d(1024, 1, 3, 1, padding=1))
6464
self.lrelu = torch.nn.LeakyReLU(LRELU_SLOPE)
@@ -75,8 +75,7 @@ def forward(self, x):
7575

7676

7777
class DiscriminatorP(torch.nn.Module):
78-
"""
79-
Discriminator for the long-term component.
78+
"""Discriminator for the long-term component.
8079
8180
This class implements a discriminator for the long-term component
8281
of the audio signal. The discriminator is composed of a series of
@@ -86,6 +85,7 @@ class DiscriminatorP(torch.nn.Module):
8685
Args:
8786
period (int): Period of the discriminator.
8887
kernel_size (int): Kernel size of the convolutional layers. Defaults to 5.
88+
8989
"""
9090

9191
def __init__(self, period: int, kernel_size: int = 5):
@@ -100,14 +100,14 @@ def __init__(self, period: int, kernel_size: int = 5):
100100
(kernel_size, 1),
101101
(stride, 1),
102102
padding=(get_padding(kernel_size, 1), 0),
103-
)
103+
),
104104
)
105105
for input_channel, output_channel, stride in zip(
106106
[1, 32, 128, 512, 1024], # input_channels
107107
[32, 128, 512, 1024, 1024], # output_channels
108108
[3, 3, 3, 3, 1], # strides
109109
)
110-
]
110+
],
111111
)
112112

113113
self.conv_post = weight_norm(torch.nn.Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))

rvc/lib/algorithm/encoders.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import math
2-
from typing import Optional
32

43
import torch
54

@@ -10,8 +9,7 @@
109

1110

1211
class Encoder(torch.nn.Module):
13-
"""
14-
Encoder module for the Transformer model.
12+
"""Encoder module for the Transformer model.
1513
1614
Args:
1715
hidden_channels (int): Number of hidden channels in the encoder.
@@ -21,6 +19,7 @@ class Encoder(torch.nn.Module):
2119
kernel_size (int, optional): Kernel size of the convolution layers in the feed-forward network. Defaults to 1.
2220
p_dropout (float, optional): Dropout probability. Defaults to 0.0.
2321
window_size (int, optional): Window size for relative positional encoding. Defaults to 10.
22+
2423
"""
2524

2625
def __init__(
@@ -49,7 +48,7 @@ def __init__(
4948
window_size=window_size,
5049
)
5150
for _ in range(n_layers)
52-
]
51+
],
5352
)
5453
self.norm_layers_1 = torch.nn.ModuleList([LayerNorm(hidden_channels) for _ in range(n_layers)])
5554
self.ffn_layers = torch.nn.ModuleList(
@@ -62,7 +61,7 @@ def __init__(
6261
p_dropout=p_dropout,
6362
)
6463
for _ in range(n_layers)
65-
]
64+
],
6665
)
6766
self.norm_layers_2 = torch.nn.ModuleList([LayerNorm(hidden_channels) for _ in range(n_layers)])
6867

@@ -83,8 +82,7 @@ def forward(self, x, x_mask):
8382

8483

8584
class TextEncoder(torch.nn.Module):
86-
"""
87-
Text Encoder with configurable embedding dimension.
85+
"""Text Encoder with configurable embedding dimension.
8886
8987
Args:
9088
out_channels (int): Output channels of the encoder.
@@ -95,6 +93,7 @@ class TextEncoder(torch.nn.Module):
9593
kernel_size (int): Kernel size of the convolutional layers.
9694
p_dropout (float): Dropout probability.
9795
embedding_dim (int): Embedding dimension for phone embeddings (v1 = 256, v2 = 768).
96+
9897
"""
9998

10099
def __init__(
@@ -118,7 +117,7 @@ def __init__(
118117
self.encoder = Encoder(hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout)
119118
self.proj = torch.nn.Conv1d(hidden_channels, out_channels * 2, 1)
120119

121-
def forward(self, phone: torch.Tensor, pitch: Optional[torch.Tensor], lengths: torch.Tensor):
120+
def forward(self, phone: torch.Tensor, pitch: torch.Tensor | None, lengths: torch.Tensor):
122121
x = self.emb_phone(phone)
123122
if pitch is not None and self.emb_pitch:
124123
x += self.emb_pitch(pitch)
@@ -136,8 +135,7 @@ def forward(self, phone: torch.Tensor, pitch: Optional[torch.Tensor], lengths: t
136135

137136

138137
class PosteriorEncoder(torch.nn.Module):
139-
"""
140-
Posterior Encoder for inferring latent representation.
138+
"""Posterior Encoder for inferring latent representation.
141139
142140
Args:
143141
in_channels (int): Number of channels in the input.
@@ -147,6 +145,7 @@ class PosteriorEncoder(torch.nn.Module):
147145
dilation_rate (int): Dilation rate of the convolutional layers.
148146
n_layers (int): Number of layers in the encoder.
149147
gin_channels (int, optional): Number of channels for the global conditioning input. Defaults to 0.
148+
150149
"""
151150

152151
def __init__(
@@ -171,7 +170,7 @@ def __init__(
171170
)
172171
self.proj = torch.nn.Conv1d(hidden_channels, out_channels * 2, 1)
173172

174-
def forward(self, x: torch.Tensor, x_lengths: torch.Tensor, g: Optional[torch.Tensor] = None):
173+
def forward(self, x: torch.Tensor, x_lengths: torch.Tensor, g: torch.Tensor | None = None):
175174
x_mask = sequence_mask(x_lengths, x.size(2)).unsqueeze(1).to(x.dtype)
176175

177176
x = self.pre(x) * x_mask

0 commit comments

Comments
 (0)