Skip to content

Commit d1f9297

Browse files
authored
New Mehod
1 parent 19c9ac3 commit d1f9297

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+25614
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import torch
2+
import json
3+
import os
4+
5+
6+
version_config_paths = [
7+
os.path.join("v1", "32000.json"),
8+
os.path.join("v1", "40000.json"),
9+
os.path.join("v1", "48000.json"),
10+
os.path.join("v2", "48000.json"),
11+
os.path.join("v2", "40000.json"),
12+
os.path.join("v2", "32000.json"),
13+
]
14+
15+
16+
def singleton(cls):
17+
instances = {}
18+
19+
def get_instance(*args, **kwargs):
20+
if cls not in instances:
21+
instances[cls] = cls(*args, **kwargs)
22+
return instances[cls]
23+
24+
return get_instance
25+
26+
27+
@singleton
28+
class Config:
29+
def __init__(self):
30+
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
31+
self.is_half = self.device != "cpu"
32+
self.gpu_name = (
33+
torch.cuda.get_device_name(int(self.device.split(":")[-1]))
34+
if self.device.startswith("cuda")
35+
else None
36+
)
37+
self.json_config = self.load_config_json()
38+
self.gpu_mem = None
39+
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
40+
41+
def load_config_json(self) -> dict:
42+
configs = {}
43+
for config_file in version_config_paths:
44+
config_path = os.path.join(
45+
"programs", "applio_code", "rvc", "configs", config_file
46+
)
47+
with open(config_path, "r") as f:
48+
configs[config_file] = json.load(f)
49+
return configs
50+
51+
def has_mps(self) -> bool:
52+
# Check if Metal Performance Shaders are available - for macOS 12.3+.
53+
return torch.backends.mps.is_available()
54+
55+
def has_xpu(self) -> bool:
56+
# Check if XPU is available.
57+
return hasattr(torch, "xpu") and torch.xpu.is_available()
58+
59+
def set_precision(self, precision):
60+
if precision not in ["fp32", "fp16"]:
61+
raise ValueError("Invalid precision type. Must be 'fp32' or 'fp16'.")
62+
63+
fp16_run_value = precision == "fp16"
64+
preprocess_target_version = "3.7" if precision == "fp16" else "3.0"
65+
preprocess_path = os.path.join(
66+
os.path.dirname(__file__),
67+
os.pardir,
68+
"rvc",
69+
"train",
70+
"preprocess",
71+
"preprocess.py",
72+
)
73+
74+
for config_path in version_config_paths:
75+
full_config_path = os.path.join(
76+
"programs", "applio_code", "rvc", "configs", config_path
77+
)
78+
try:
79+
with open(full_config_path, "r") as f:
80+
config = json.load(f)
81+
config["train"]["fp16_run"] = fp16_run_value
82+
with open(full_config_path, "w") as f:
83+
json.dump(config, f, indent=4)
84+
except FileNotFoundError:
85+
print(f"File not found: {full_config_path}")
86+
87+
if os.path.exists(preprocess_path):
88+
with open(preprocess_path, "r") as f:
89+
preprocess_content = f.read()
90+
preprocess_content = preprocess_content.replace(
91+
"3.0" if precision == "fp16" else "3.7", preprocess_target_version
92+
)
93+
with open(preprocess_path, "w") as f:
94+
f.write(preprocess_content)
95+
96+
return f"Overwritten preprocess and config.json to use {precision}."
97+
98+
def get_precision(self):
99+
if not version_config_paths:
100+
raise FileNotFoundError("No configuration paths provided.")
101+
102+
full_config_path = os.path.join(
103+
"programs", "applio_code", "rvc", "configs", version_config_paths[0]
104+
)
105+
try:
106+
with open(full_config_path, "r") as f:
107+
config = json.load(f)
108+
fp16_run_value = config["train"].get("fp16_run", False)
109+
precision = "fp16" if fp16_run_value else "fp32"
110+
return precision
111+
except FileNotFoundError:
112+
print(f"File not found: {full_config_path}")
113+
return None
114+
115+
def device_config(self) -> tuple:
116+
if self.device.startswith("cuda"):
117+
self.set_cuda_config()
118+
elif self.has_mps():
119+
self.device = "mps"
120+
self.is_half = False
121+
self.set_precision("fp32")
122+
else:
123+
self.device = "cpu"
124+
self.is_half = False
125+
self.set_precision("fp32")
126+
127+
# Configuration for 6GB GPU memory
128+
x_pad, x_query, x_center, x_max = (
129+
(3, 10, 60, 65) if self.is_half else (1, 6, 38, 41)
130+
)
131+
if self.gpu_mem is not None and self.gpu_mem <= 4:
132+
# Configuration for 5GB GPU memory
133+
x_pad, x_query, x_center, x_max = (1, 5, 30, 32)
134+
135+
return x_pad, x_query, x_center, x_max
136+
137+
def set_cuda_config(self):
138+
i_device = int(self.device.split(":")[-1])
139+
self.gpu_name = torch.cuda.get_device_name(i_device)
140+
# Zluda
141+
if self.gpu_name.endswith("[ZLUDA]"):
142+
print("Zluda compatibility enabled, experimental feature.")
143+
torch.backends.cudnn.enabled = False
144+
torch.backends.cuda.enable_flash_sdp(False)
145+
torch.backends.cuda.enable_math_sdp(True)
146+
torch.backends.cuda.enable_mem_efficient_sdp(False)
147+
low_end_gpus = ["16", "P40", "P10", "1060", "1070", "1080"]
148+
if (
149+
any(gpu in self.gpu_name for gpu in low_end_gpus)
150+
and "V100" not in self.gpu_name.upper()
151+
):
152+
self.is_half = False
153+
self.set_precision("fp32")
154+
155+
self.gpu_mem = torch.cuda.get_device_properties(i_device).total_memory // (
156+
1024**3
157+
)
158+
159+
160+
def max_vram_gpu(gpu):
161+
if torch.cuda.is_available():
162+
gpu_properties = torch.cuda.get_device_properties(gpu)
163+
total_memory_gb = round(gpu_properties.total_memory / 1024 / 1024 / 1024)
164+
return total_memory_gb
165+
else:
166+
return "0"
167+
168+
169+
def get_gpu_info():
170+
ngpu = torch.cuda.device_count()
171+
gpu_infos = []
172+
if torch.cuda.is_available() or ngpu != 0:
173+
for i in range(ngpu):
174+
gpu_name = torch.cuda.get_device_name(i)
175+
mem = int(
176+
torch.cuda.get_device_properties(i).total_memory / 1024 / 1024 / 1024
177+
+ 0.4
178+
)
179+
gpu_infos.append(f"{i}: {gpu_name} ({mem} GB)")
180+
if len(gpu_infos) > 0:
181+
gpu_info = "\n".join(gpu_infos)
182+
else:
183+
gpu_info = "Unfortunately, there is no compatible GPU available to support your training."
184+
return gpu_info
185+
186+
187+
def get_number_of_gpus():
188+
if torch.cuda.is_available():
189+
num_gpus = torch.cuda.device_count()
190+
return "-".join(map(str, range(num_gpus)))
191+
else:
192+
return "-"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"train": {
3+
"log_interval": 200,
4+
"seed": 1234,
5+
"epochs": 20000,
6+
"learning_rate": 1e-4,
7+
"betas": [0.8, 0.99],
8+
"eps": 1e-9,
9+
"batch_size": 4,
10+
"fp16_run": true,
11+
"lr_decay": 0.999875,
12+
"segment_size": 12800,
13+
"init_lr_ratio": 1,
14+
"warmup_epochs": 0,
15+
"c_mel": 45,
16+
"c_kl": 1.0
17+
},
18+
"data": {
19+
"max_wav_value": 32768.0,
20+
"sample_rate": 32000,
21+
"filter_length": 1024,
22+
"hop_length": 320,
23+
"win_length": 1024,
24+
"n_mel_channels": 80,
25+
"mel_fmin": 0.0,
26+
"mel_fmax": null
27+
},
28+
"model": {
29+
"inter_channels": 192,
30+
"hidden_channels": 192,
31+
"filter_channels": 768,
32+
"text_enc_hidden_dim": 256,
33+
"n_heads": 2,
34+
"n_layers": 6,
35+
"kernel_size": 3,
36+
"p_dropout": 0,
37+
"resblock": "1",
38+
"resblock_kernel_sizes": [3,7,11],
39+
"resblock_dilation_sizes": [[1,3,5], [1,3,5], [1,3,5]],
40+
"upsample_rates": [10,4,2,2,2],
41+
"upsample_initial_channel": 512,
42+
"upsample_kernel_sizes": [16,16,4,4,4],
43+
"use_spectral_norm": false,
44+
"gin_channels": 256,
45+
"spk_embed_dim": 109
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"train": {
3+
"log_interval": 200,
4+
"seed": 1234,
5+
"epochs": 20000,
6+
"learning_rate": 1e-4,
7+
"betas": [0.8, 0.99],
8+
"eps": 1e-9,
9+
"batch_size": 4,
10+
"fp16_run": true,
11+
"lr_decay": 0.999875,
12+
"segment_size": 12800,
13+
"init_lr_ratio": 1,
14+
"warmup_epochs": 0,
15+
"c_mel": 45,
16+
"c_kl": 1.0
17+
},
18+
"data": {
19+
"max_wav_value": 32768.0,
20+
"sample_rate": 40000,
21+
"filter_length": 2048,
22+
"hop_length": 400,
23+
"win_length": 2048,
24+
"n_mel_channels": 125,
25+
"mel_fmin": 0.0,
26+
"mel_fmax": null
27+
},
28+
"model": {
29+
"inter_channels": 192,
30+
"hidden_channels": 192,
31+
"filter_channels": 768,
32+
"text_enc_hidden_dim": 256,
33+
"n_heads": 2,
34+
"n_layers": 6,
35+
"kernel_size": 3,
36+
"p_dropout": 0,
37+
"resblock": "1",
38+
"resblock_kernel_sizes": [3,7,11],
39+
"resblock_dilation_sizes": [[1,3,5], [1,3,5], [1,3,5]],
40+
"upsample_rates": [10,10,2,2],
41+
"upsample_initial_channel": 512,
42+
"upsample_kernel_sizes": [16,16,4,4],
43+
"use_spectral_norm": false,
44+
"gin_channels": 256,
45+
"spk_embed_dim": 109
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"train": {
3+
"log_interval": 200,
4+
"seed": 1234,
5+
"epochs": 20000,
6+
"learning_rate": 1e-4,
7+
"betas": [0.8, 0.99],
8+
"eps": 1e-9,
9+
"batch_size": 4,
10+
"fp16_run": true,
11+
"lr_decay": 0.999875,
12+
"segment_size": 11520,
13+
"init_lr_ratio": 1,
14+
"warmup_epochs": 0,
15+
"c_mel": 45,
16+
"c_kl": 1.0
17+
},
18+
"data": {
19+
"max_wav_value": 32768.0,
20+
"sample_rate": 48000,
21+
"filter_length": 2048,
22+
"hop_length": 480,
23+
"win_length": 2048,
24+
"n_mel_channels": 128,
25+
"mel_fmin": 0.0,
26+
"mel_fmax": null
27+
},
28+
"model": {
29+
"inter_channels": 192,
30+
"hidden_channels": 192,
31+
"filter_channels": 768,
32+
"text_enc_hidden_dim": 256,
33+
"n_heads": 2,
34+
"n_layers": 6,
35+
"kernel_size": 3,
36+
"p_dropout": 0,
37+
"resblock": "1",
38+
"resblock_kernel_sizes": [3,7,11],
39+
"resblock_dilation_sizes": [[1,3,5], [1,3,5], [1,3,5]],
40+
"upsample_rates": [10,6,2,2,2],
41+
"upsample_initial_channel": 512,
42+
"upsample_kernel_sizes": [16,16,4,4,4],
43+
"use_spectral_norm": false,
44+
"gin_channels": 256,
45+
"spk_embed_dim": 109
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"train": {
3+
"log_interval": 200,
4+
"seed": 1234,
5+
"learning_rate": 1e-4,
6+
"betas": [0.8, 0.99],
7+
"eps": 1e-9,
8+
"fp16_run": true,
9+
"lr_decay": 0.999875,
10+
"segment_size": 12800,
11+
"c_mel": 45,
12+
"c_kl": 1.0
13+
},
14+
"data": {
15+
"max_wav_value": 32768.0,
16+
"sample_rate": 32000,
17+
"filter_length": 1024,
18+
"hop_length": 320,
19+
"win_length": 1024,
20+
"n_mel_channels": 80,
21+
"mel_fmin": 0.0,
22+
"mel_fmax": null
23+
},
24+
"model": {
25+
"inter_channels": 192,
26+
"hidden_channels": 192,
27+
"filter_channels": 768,
28+
"text_enc_hidden_dim": 768,
29+
"n_heads": 2,
30+
"n_layers": 6,
31+
"kernel_size": 3,
32+
"p_dropout": 0,
33+
"resblock": "1",
34+
"resblock_kernel_sizes": [3,7,11],
35+
"resblock_dilation_sizes": [[1,3,5], [1,3,5], [1,3,5]],
36+
"upsample_rates": [10,8,2,2],
37+
"upsample_initial_channel": 512,
38+
"upsample_kernel_sizes": [20,16,4,4],
39+
"use_spectral_norm": false,
40+
"gin_channels": 256,
41+
"spk_embed_dim": 109
42+
}
43+
}

0 commit comments

Comments
 (0)