diff --git a/server/voice_changer/DDSP_SVC/deviceManager/DeviceManager.py b/server/voice_changer/DDSP_SVC/deviceManager/DeviceManager.py index 9075e99a..5852e4c3 100644 --- a/server/voice_changer/DDSP_SVC/deviceManager/DeviceManager.py +++ b/server/voice_changer/DDSP_SVC/deviceManager/DeviceManager.py @@ -18,10 +18,11 @@ class DeviceManager(object): ) def getDevice(self, id: int): - if id < 0 or (self.gpu_num == 0 and self.mps_enabled is False): - dev = torch.device("cpu") - elif self.mps_enabled: - dev = torch.device("mps") + if id < 0 or self.gpu_num == 0: + if self.mps_enabled is False: + dev = torch.device("cpu") + else: + dev = torch.device("mps") else: dev = torch.device("cuda", index=id) return dev @@ -51,6 +52,6 @@ class DeviceManager(object): try: return torch.cuda.get_device_properties(id).total_memory # except Exception as e: - except: + except: # NOQA # print(e) return 0 diff --git a/server/voice_changer/DiffusionSVC/pitchExtractor/CrepePitchExtractor.py b/server/voice_changer/DiffusionSVC/pitchExtractor/CrepePitchExtractor.py index 17452bf0..c86251f5 100644 --- a/server/voice_changer/DiffusionSVC/pitchExtractor/CrepePitchExtractor.py +++ b/server/voice_changer/DiffusionSVC/pitchExtractor/CrepePitchExtractor.py @@ -3,21 +3,19 @@ import torch import numpy as np from const import PitchExtractorType from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor +from voice_changer.RVC.deviceManager.DeviceManager import DeviceManager from voice_changer.utils.VoiceChangerModel import AudioInOut class CrepePitchExtractor(PitchExtractor): - def __init__(self): + def __init__(self, gpu: int): super().__init__() self.pitchExtractorType: PitchExtractorType = "crepe" self.f0_min = 50 self.f0_max = 1100 self.uv_interp = True - if torch.cuda.is_available(): - self.device = torch.device("cuda:" + str(torch.cuda.current_device())) - else: - self.device = torch.device("cpu") + self.device = DeviceManager.get_instance().getDevice(gpu) def extract(self, audio: AudioInOut, sr: int, block_size: int, model_sr: int, pitch, f0_up_key, silence_front=0): hop_size = block_size * sr / model_sr diff --git a/server/voice_changer/DiffusionSVC/pitchExtractor/PitchExtractorManager.py b/server/voice_changer/DiffusionSVC/pitchExtractor/PitchExtractorManager.py index 561ad2e5..793e28a6 100644 --- a/server/voice_changer/DiffusionSVC/pitchExtractor/PitchExtractorManager.py +++ b/server/voice_changer/DiffusionSVC/pitchExtractor/PitchExtractorManager.py @@ -33,7 +33,7 @@ class PitchExtractorManager(Protocol): elif pitchExtractorType == "dio": return DioPitchExtractor() elif pitchExtractorType == "crepe": - return CrepePitchExtractor() + return CrepePitchExtractor(gpu) elif pitchExtractorType == "crepe_tiny": return CrepeOnnxPitchExtractor(pitchExtractorType, cls.params.crepe_onnx_tiny, gpu) elif pitchExtractorType == "crepe_full": diff --git a/server/voice_changer/DiffusionSVC/pitchExtractor/RMVPEPitchExtractor.py b/server/voice_changer/DiffusionSVC/pitchExtractor/RMVPEPitchExtractor.py index 0110948b..66fb6a5a 100644 --- a/server/voice_changer/DiffusionSVC/pitchExtractor/RMVPEPitchExtractor.py +++ b/server/voice_changer/DiffusionSVC/pitchExtractor/RMVPEPitchExtractor.py @@ -5,6 +5,7 @@ from const import PitchExtractorType from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor from voice_changer.DiffusionSVC.pitchExtractor.rmvpe.rmvpe import RMVPE from scipy.ndimage import zoom +from voice_changer.RVC.deviceManager.DeviceManager import DeviceManager from voice_changer.utils.VoiceChangerModel import AudioInOut @@ -18,10 +19,7 @@ class RMVPEPitchExtractor(PitchExtractor): self.f0_max = 1100 self.uv_interp = True self.input_sr = -1 - if torch.cuda.is_available() and gpu >= 0: - self.device = torch.device("cuda:" + str(torch.cuda.current_device())) - else: - self.device = torch.device("cpu") + self.device = DeviceManager.get_instance().getDevice(gpu) self.rmvpe = RMVPE(model_path=file, is_half=False, device=self.device) diff --git a/server/voice_changer/RVC/deviceManager/DeviceManager.py b/server/voice_changer/RVC/deviceManager/DeviceManager.py index c627e1b9..2053a753 100644 --- a/server/voice_changer/RVC/deviceManager/DeviceManager.py +++ b/server/voice_changer/RVC/deviceManager/DeviceManager.py @@ -20,10 +20,11 @@ class DeviceManager(object): ) def getDevice(self, id: int): - if id < 0 or (self.gpu_num == 0 and self.mps_enabled is False): - dev = torch.device("cpu") - elif self.mps_enabled: - dev = torch.device("mps") + if id < 0 or self.gpu_num == 0: + if self.mps_enabled is False: + dev = torch.device("cpu") + else: + dev = torch.device("mps") else: if id < self.gpu_num: dev = torch.device("cuda", index=id) @@ -46,7 +47,7 @@ class DeviceManager(object): "execution_mode": onnxruntime.ExecutionMode.ORT_PARALLEL, "inter_op_num_threads": 8, } - ] + ] elif gpu >= 0 and "DmlExecutionProvider" in availableProviders: return ["DmlExecutionProvider"], [{"device_id": gpu}] else: diff --git a/server/voice_changer/RVC/pitchExtractor/CrepePitchExtractor.py b/server/voice_changer/RVC/pitchExtractor/CrepePitchExtractor.py index 55528a0d..e8d2d8b0 100644 --- a/server/voice_changer/RVC/pitchExtractor/CrepePitchExtractor.py +++ b/server/voice_changer/RVC/pitchExtractor/CrepePitchExtractor.py @@ -1,23 +1,19 @@ import torchcrepe -import torch import numpy as np from const import PitchExtractorType +from voice_changer.RVC.deviceManager.DeviceManager import DeviceManager from voice_changer.RVC.pitchExtractor.PitchExtractor import PitchExtractor class CrepePitchExtractor(PitchExtractor): - def __init__(self): + def __init__(self, gpu: int): super().__init__() self.pitchExtractorType: PitchExtractorType = "crepe" - if torch.cuda.is_available(): - self.device = torch.device("cuda:" + str(torch.cuda.current_device())) - else: - self.device = torch.device("cpu") + self.device = DeviceManager.get_instance().getDevice(gpu) def extract(self, audio, pitchf, f0_up_key, sr, window, silence_front=0): - n_frames = int(len(audio) // window) + 1 start_frame = int(silence_front * sr / window) real_silence_front = start_frame * window / sr diff --git a/server/voice_changer/RVC/pitchExtractor/PitchExtractorManager.py b/server/voice_changer/RVC/pitchExtractor/PitchExtractorManager.py index bc7c1c9c..ea538958 100644 --- a/server/voice_changer/RVC/pitchExtractor/PitchExtractorManager.py +++ b/server/voice_changer/RVC/pitchExtractor/PitchExtractorManager.py @@ -33,7 +33,7 @@ class PitchExtractorManager(Protocol): elif pitchExtractorType == "dio": return DioPitchExtractor() elif pitchExtractorType == "crepe": - return CrepePitchExtractor() + return CrepePitchExtractor(gpu) elif pitchExtractorType == "crepe_tiny": return CrepeOnnxPitchExtractor(pitchExtractorType, cls.params.crepe_onnx_tiny, gpu) elif pitchExtractorType == "crepe_full": diff --git a/server/voice_changer/RVC/pitchExtractor/RMVPEPitchExtractor.py b/server/voice_changer/RVC/pitchExtractor/RMVPEPitchExtractor.py index 8e41905b..5b5bc98f 100644 --- a/server/voice_changer/RVC/pitchExtractor/RMVPEPitchExtractor.py +++ b/server/voice_changer/RVC/pitchExtractor/RMVPEPitchExtractor.py @@ -1,8 +1,8 @@ -import torch import numpy as np from const import PitchExtractorType from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor from voice_changer.DiffusionSVC.pitchExtractor.rmvpe.rmvpe import RMVPE +from voice_changer.RVC.deviceManager.DeviceManager import DeviceManager class RMVPEPitchExtractor(PitchExtractor): @@ -17,11 +17,7 @@ class RMVPEPitchExtractor(PitchExtractor): self.uv_interp = True self.input_sr = -1 - if torch.cuda.is_available() and gpu >= 0: - self.device = torch.device("cuda:" + str(torch.cuda.current_device())) - else: - self.device = torch.device("cpu") - + self.device = DeviceManager.get_instance().getDevice(gpu) self.rmvpe = RMVPE(model_path=file, is_half=False, device=self.device) def extract(self, audio, pitchf, f0_up_key, sr, window, silence_front=0):