add use_device() funcion

can switch to a specific device
This commit is contained in:
Ftps
2024-03-07 22:39:08 +09:00
parent bf31c5afe8
commit cf00b3fb9e
2 changed files with 71 additions and 54 deletions

1
rvc/configs/__init__.py Normal file
View File

@@ -0,0 +1 @@
from rvc.configs.config import Config

View File

@@ -105,18 +105,27 @@ class Config:
def has_xpu() -> bool: def has_xpu() -> bool:
return hasattr(torch, "xpu") and torch.xpu.is_available() return hasattr(torch, "xpu") and torch.xpu.is_available()
def use_fp32_config(self) -> None: def params_config(self) -> tuple:
for config_file, data in self.json_config.items(): if self.gpu_mem is not None and self.gpu_mem <= 4:
try: x_pad = 1
data["train"]["fp16_run"] = False x_query = 5
with open(config_file, "w") as json_file: x_center = 30
json.dump(data, json_file, indent=4) x_max = 32
except Exception as e: elif self.is_half:
logger.info(f"Error updating {config_file}: {str(e)}") # 6G PU_RAM conf
logger.info("overwrite configs.json") x_pad = 3
x_query = 10
x_center = 60
x_max = 65
else:
# 5G GPU_RAM conf
x_pad = 1
x_query = 6
x_center = 38
x_max = 41
return x_pad, x_query, x_center, x_max
def device_config(self) -> tuple: def use_cuda(self) -> None:
if torch.cuda.is_available():
if self.has_xpu(): if self.has_xpu():
self.device = self.instead = "xpu:0" self.device = self.instead = "xpu:0"
self.is_half = True self.is_half = True
@@ -136,46 +145,53 @@ class Config:
else: else:
logger.info(f"Found GPU {self.gpu_name}") logger.info(f"Found GPU {self.gpu_name}")
self.gpu_mem = int( self.gpu_mem = int(
torch.cuda.get_device_properties(i_device).total_memory torch.cuda.get_device_properties(i_device).total_memory / 1024 / 1024 / 1024
/ 1024
/ 1024
/ 1024
+ 0.4 + 0.4
) )
elif self.has_mps():
logger.info("No supported Nvidia GPU found") def use_mps(self) -> None:
self.device = self.instead = "mps" self.device = self.instead = "mps"
self.is_half = False self.is_half = False
self.use_fp32_config() self.use_fp32_config()
elif self.dml: self.params_config()
def use_dml(self) -> None:
import torch_directml import torch_directml
self.device = torch_directml.device(torch_directml.default_device()) self.device = torch_directml.device(torch_directml.default_device())
self.is_half = False self.is_half = False
self.params_config()
def use_cpu(self) -> None:
self.device = self.instead = "cpu"
self.is_half = False
self.use_fp32_config()
self.params_config()
def use_fp32_config(self) -> None:
for config_file, data in self.json_config.items():
try:
data["train"]["fp16_run"] = False
with open(config_file, "w") as json_file:
json.dump(data, json_file, indent=4)
except Exception as e:
logger.info(f"Error updating {config_file}: {str(e)}")
logger.info("overwrite configs.json")
def device_config(self) -> tuple:
if torch.cuda.is_available():
self.use_cuda()
elif self.has_mps():
logger.info("No supported Nvidia GPU found")
self.use_mps()
elif self.dml:
self.use_dml()
else: else:
logger.info("No supported Nvidia GPU found") logger.info("No supported Nvidia GPU found")
self.device = self.instead = "cpu" self.device = self.instead = "cpu"
self.is_half = False self.is_half = False
self.use_fp32_config() self.use_fp32_config()
if self.gpu_mem is not None and self.gpu_mem <= 4:
x_pad = 1
x_query = 5
x_center = 30
x_max = 32
elif self.is_half:
# 6G PU_RAM conf
x_pad = 3
x_query = 10
x_center = 60
x_max = 65
else:
# 5G GPU_RAM conf
x_pad = 1
x_query = 6
x_center = 38
x_max = 41
logger.info(f"Use {self.dml or self.instead} instead") logger.info(f"Use {self.dml or self.instead} instead")
logger.info(f"is_half:{self.is_half}, device:{self.device}") logger.info(f"is_half:{self.is_half}, device:{self.device}")
return x_pad, x_query, x_center, x_max return self.params_config()