Repository: voicepaw/so-vits-svc-fork

Issue:
Realtime voice conversion not working when "Use GPU" is ticked
**Describe the bug**
Whenever I start the live voice changer with "Use GPU" enabled, it doesn't load, and hangs with this in the console:
`2023-03-22 02:21:56 | INFO | fairseq.tasks.hubert_pretraining | HubertPretrainingTask Config {'_name': 'hubert_pretraining', 'data': 'metadata', 'fine_tuning': False, 'labels': ['km'], 'label_dir': 'label', 'label_rate': 50.0, 'sample_rate': 16000, 'normalize': False, 'enable_padding': False, 'max_keep_size': None, 'max_sample_size': 250000, 'min_sample_size': 32000, 'single_target': False, 'random_crop': True, 'pad_audio': False}
2023-03-22 02:21:56 | INFO | fairseq.models.hubert.hubert | HubertModel Config: {'_name': 'hubert', 'label_rate': 50.0, 'extractor_mode': default, 'encoder_layers': 12, 'encoder_embed_dim': 768, 'encoder_ffn_embed_dim': 3072, 'encoder_attention_heads': 12, 'activation_fn': gelu, 'layer_type': transformer, 'dropout': 0.1, 'attention_dropout': 0.1, 'activation_dropout': 0.0, 'encoder_layerdrop': 0.05, 'dropout_input': 0.1, 'dropout_features': 0.1, 'final_dim': 256, 'untie_final_proj': True, 'layer_norm_first': False, 'conv_feature_layers': '[(512,10,5)] + [(512,3,2)] * 4 + [(512,2,2)] * 2', 'conv_bias': False, 'logit_temp': 0.1, 'target_glu': False, 'feature_grad_mult': 0.1, 'mask_length': 10, 'mask_prob': 0.8, 'mask_selection': static, 'mask_other': 0.0, 'no_mask_overlap': False, 'mask_min_space': 1, 'mask_channel_length': 10, 'mask_channel_prob': 0.0, 'mask_channel_selection': static, 'mask_channel_other': 0.0, 'no_mask_channel_overlap': False, 'mask_channel_min_space': 1, 'conv_pos': 128, 'conv_pos_groups': 16, 'latent_temp': [2.0, 0.5, 0.999995], 'skip_masked': False, 'skip_nomask': False, 'checkpoint_activations': False, 'required_seq_len_multiple': 2, 'depthwise_conv_kernel_size': 31, 'attn_type': '', 'pos_enc_type': 'abs', 'fp16': False}`

**To Reproduce**
Steps to reproduce the behavior: Load a model path and config path, tick "Use GPU", press "(Re)Start Voice Changer"

**Additional context**
I'm running the program on Windows 10 through anaconda3, and I have a GTX 1070 GPU. I've tested different models with the same results. Using CPU works. There's never any usage shown on my GPU, nor does the memory used rise when attempting to load using GPU.


Discussion:
Currently there is no way to view the error that occurred with the voice changer (big problem). The likely cause is that the GPU version of pytorch has not been properly installed.
Does GPU inference work for files?

Resolving patch:
diff --git a/src/so_vits_svc_fork/gui.py b/src/so_vits_svc_fork/gui.py
--- a/src/so_vits_svc_fork/gui.py
+++ b/src/so_vits_svc_fork/gui.py
@@ -8,7 +8,7 @@
 import PySimpleGUI as sg
 import sounddevice as sd
 import torch
-from pebble import ProcessPool
+from pebble import ProcessFuture, ProcessPool
 
 from .__main__ import init_logger
 
@@ -360,7 +360,22 @@ def main():
             frames["Presets"],
             [
                 sg.Checkbox(
-                    key="use_gpu", default=torch.cuda.is_available(), text="Use GPU"
+                    key="use_gpu",
+                    default=(
+                        torch.cuda.is_available() or torch.backends.mps.is_available()
+                    ),
+                    text="Use GPU"
+                    + (
+                        " (not available; if your device has GPU, make sure you installed PyTorch with CUDA support)"
+                        if not (
+                            torch.cuda.is_available()
+                            or torch.backends.mps.is_available()
+                        )
+                        else ""
+                    ),
+                    disabled=not (
+                        torch.cuda.is_available() or torch.backends.mps.is_available()
+                    ),
                 )
             ],
             [
@@ -408,12 +423,11 @@ def apply_preset(name: str) -> None:
     window["presets"].update(default_name)
     del default_name
     with ProcessPool(max_workers=1) as pool:
-        future = None
+        future: None | ProcessFuture = None
         while True:
-            event, values = window.read()
+            event, values = window.read(100)
             if event == sg.WIN_CLOSED:
                 break
-
             if not event == sg.EVENT_TIMEOUT:
                 LOG.info(f"Event {event}, values {values}")
             if event.endswith("_path"):
@@ -428,8 +442,7 @@ def apply_preset(name: str) -> None:
                             browser.update()
                         else:
                             LOG.warning(f"Browser {browser} is not a FileBrowse")
-
-            if event == "add_preset":
+            elif event == "add_preset":
                 presets = add_preset(
                     values["preset_name"], {key: values[key] for key in PRESET_KEYS}
                 )
@@ -469,7 +482,15 @@ def apply_preset(name: str) -> None:
                     pad_seconds=values["pad_seconds"],
                     absolute_thresh=values["absolute_thresh"],
                     chunk_seconds=values["chunk_seconds"],
-                    device="cuda" if values["use_gpu"] else "cpu",
+                    device="cpu"
+                    if not values["use_gpu"]
+                    else (
+                        "cuda"
+                        if torch.cuda.is_available()
+                        else "mps"
+                        if torch.backends.mps.is_available()
+                        else "cpu"
+                    ),
                 )
                 if values["auto_play"]:
                     pool.schedule(play_audio, args=[output_path])
@@ -518,6 +539,9 @@ def apply_preset(name: str) -> None:
                 if future:
                     future.cancel()
                     future = None
+            if future is not None and future.done():
+                LOG.error(f"Error in realtime: {future.exception()}")
+                future = None
         if future:
             future.cancel()
     window.close()


Test patch: