171 lines
5.3 KiB
Python
171 lines
5.3 KiB
Python
import torch
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
version_config_list = [
|
|
"v1/32000.json",
|
|
"v1/40000.json",
|
|
"v1/48000.json",
|
|
"v2/48000.json",
|
|
"v2/32000.json",
|
|
]
|
|
|
|
|
|
def singleton_variable(func):
|
|
def wrapper(*args, **kwargs):
|
|
if not wrapper.instance:
|
|
wrapper.instance = func(*args, **kwargs)
|
|
return wrapper.instance
|
|
|
|
wrapper.instance = None
|
|
return wrapper
|
|
|
|
|
|
@singleton_variable
|
|
class Config:
|
|
def __init__(self):
|
|
self.device = "cuda:0"
|
|
self.is_half = True
|
|
self.use_jit = False
|
|
self.n_cpu = 0
|
|
self.gpu_name = None
|
|
self.json_config = self.load_config_json()
|
|
self.gpu_mem = None
|
|
self.instead = ""
|
|
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
|
|
|
|
@staticmethod
|
|
def load_config_json() -> dict:
|
|
d = {}
|
|
base_path = Path(__file__).resolve().parent
|
|
for config_file in version_config_list:
|
|
config_path = base_path / config_file
|
|
with open(config_path, "r") as f:
|
|
d[config_file] = json.load(f)
|
|
return d
|
|
|
|
@staticmethod
|
|
def has_mps() -> bool:
|
|
if not torch.backends.mps.is_available():
|
|
return False
|
|
try:
|
|
torch.zeros(1).to(torch.device("mps"))
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
@staticmethod
|
|
def has_xpu() -> bool:
|
|
if hasattr(torch, "xpu") and torch.xpu.is_available():
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def use_fp32_config(self):
|
|
print(
|
|
f"Using FP32 config instead of FP16 due to GPU compatibility ({self.gpu_name})"
|
|
)
|
|
for config_file in version_config_list:
|
|
config_path = os.path.join(os.getcwd(), "system", "tts_engines", "rvc", "configs", config_file)
|
|
with open(config_path, "r") as f:
|
|
config_data = f.read().replace("true", "false")
|
|
with open(config_path, "w") as f:
|
|
f.write(config_data)
|
|
|
|
preprocess_path = os.path.join(os.getcwd(), "system", "tts_engines", "rvc", "train", "preprocess", "preprocess.py")
|
|
with open(preprocess_path, "r") as f:
|
|
preprocess_data = f.read().replace("3.7", "3.0")
|
|
with open(preprocess_path, "w") as f:
|
|
f.write(preprocess_data)
|
|
|
|
def device_config(self) -> tuple:
|
|
if torch.cuda.is_available():
|
|
if self.has_xpu():
|
|
self.device = self.instead = "xpu:0"
|
|
self.is_half = True
|
|
i_device = int(self.device.split(":")[-1])
|
|
self.gpu_name = torch.cuda.get_device_name(i_device)
|
|
if (
|
|
("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
|
|
or "P40" in self.gpu_name.upper()
|
|
or "P10" in self.gpu_name.upper()
|
|
or "1060" in self.gpu_name
|
|
or "1070" in self.gpu_name
|
|
or "1080" in self.gpu_name
|
|
):
|
|
self.is_half = False
|
|
self.use_fp32_config()
|
|
self.gpu_mem = int(
|
|
torch.cuda.get_device_properties(i_device).total_memory
|
|
/ 1024
|
|
/ 1024
|
|
/ 1024
|
|
+ 0.4
|
|
)
|
|
if self.gpu_mem <= 4:
|
|
preprocess_path = os.path.join(os.getcwd(), "system", "tts_engines", "rvc", "train", "preprocess", "preprocess.py")
|
|
with open(preprocess_path, "r") as f:
|
|
strr = f.read().replace("3.7", "3.0")
|
|
with open(preprocess_path, "w") as f:
|
|
f.write(strr)
|
|
elif self.has_mps():
|
|
print("No supported Nvidia GPU found")
|
|
self.device = self.instead = "mps"
|
|
self.is_half = False
|
|
self.use_fp32_config()
|
|
else:
|
|
print("No supported Nvidia GPU found")
|
|
self.device = self.instead = "cpu"
|
|
self.is_half = False
|
|
self.use_fp32_config()
|
|
|
|
if self.n_cpu == 0:
|
|
self.n_cpu = os.cpu_count()
|
|
|
|
if self.is_half:
|
|
x_pad = 3
|
|
x_query = 10
|
|
x_center = 60
|
|
x_max = 65
|
|
else:
|
|
x_pad = 1
|
|
x_query = 6
|
|
x_center = 38
|
|
x_max = 41
|
|
|
|
if self.gpu_mem is not None and self.gpu_mem <= 4:
|
|
x_pad = 1
|
|
x_query = 5
|
|
x_center = 30
|
|
x_max = 32
|
|
|
|
return x_pad, x_query, x_center, x_max
|
|
|
|
|
|
def max_vram_gpu(gpu):
|
|
if torch.cuda.is_available():
|
|
gpu_properties = torch.cuda.get_device_properties(gpu)
|
|
total_memory_gb = round(gpu_properties.total_memory / 1024 / 1024 / 1024)
|
|
return total_memory_gb
|
|
else:
|
|
return "0"
|
|
|
|
|
|
def get_gpu_info():
|
|
ngpu = torch.cuda.device_count()
|
|
gpu_infos = []
|
|
if torch.cuda.is_available() or ngpu != 0:
|
|
for i in range(ngpu):
|
|
gpu_name = torch.cuda.get_device_name(i)
|
|
mem = int(
|
|
torch.cuda.get_device_properties(i).total_memory / 1024 / 1024 / 1024
|
|
+ 0.4
|
|
)
|
|
gpu_infos.append("%s: %s %s GB" % (i, gpu_name, mem))
|
|
if len(gpu_infos) > 0:
|
|
gpu_info = "\n".join(gpu_infos)
|
|
else:
|
|
gpu_info = "Unfortunately, there is no compatible GPU available to support your training."
|
|
return gpu_info
|