CoolFace
Apppublic

dthomas84/RVC_RULE1

sourceHugging Facelgpl-3.0updated 3y agoView on Hugging Face
0likes
config.py205 linesDownload Raw Back to root
1import argparse2import sys3import torch4import json5from multiprocessing import cpu_count6 7global usefp168usefp16 = False9 10 11def use_fp32_config():12    usefp16 = False13    device_capability = 014    if torch.cuda.is_available():15        device = torch.device("cuda:0")  # Assuming you have only one GPU (index 0).16        device_capability = torch.cuda.get_device_capability(device)[0]17        if device_capability >= 7:18            usefp16 = True19            for config_file in ["32k.json", "40k.json", "48k.json"]:20                with open(f"configs/{config_file}", "r") as d:21                    data = json.load(d)22 23                if "train" in data and "fp16_run" in data["train"]:24                    data["train"]["fp16_run"] = True25 26                with open(f"configs/{config_file}", "w") as d:27                    json.dump(data, d, indent=4)28 29                print(f"Set fp16_run to true in {config_file}")30 31            with open(32                "trainset_preprocess_pipeline_print.py", "r", encoding="utf-8"33            ) as f:34                strr = f.read()35 36            strr = strr.replace("3.0", "3.7")37 38            with open(39                "trainset_preprocess_pipeline_print.py", "w", encoding="utf-8"40            ) as f:41                f.write(strr)42        else:43            for config_file in ["32k.json", "40k.json", "48k.json"]:44                with open(f"configs/{config_file}", "r") as f:45                    data = json.load(f)46 47                if "train" in data and "fp16_run" in data["train"]:48                    data["train"]["fp16_run"] = False49 50                with open(f"configs/{config_file}", "w") as d:51                    json.dump(data, d, indent=4)52 53                print(f"Set fp16_run to false in {config_file}")54 55            with open(56                "trainset_preprocess_pipeline_print.py", "r", encoding="utf-8"57            ) as f:58                strr = f.read()59 60            strr = strr.replace("3.7", "3.0")61 62            with open(63                "trainset_preprocess_pipeline_print.py", "w", encoding="utf-8"64            ) as f:65                f.write(strr)66    else:67        print(68            "CUDA is not available. Make sure you have an NVIDIA GPU and CUDA installed."69        )70    return (usefp16, device_capability)71 72 73class Config:74    def __init__(self):75        self.device = "cuda:0"76        self.is_half = True77        self.n_cpu = 078        self.gpu_name = None79        self.gpu_mem = None80        (81            self.python_cmd,82            self.listen_port,83            self.iscolab,84            self.noparallel,85            self.noautoopen,86            self.paperspace,87            self.is_cli,88        ) = self.arg_parse()89 90        self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()91 92    @staticmethod93    def arg_parse() -> tuple:94        exe = sys.executable or "python"95        parser = argparse.ArgumentParser()96        parser.add_argument("--port", type=int, default=7865, help="Listen port")97        parser.add_argument("--pycmd", type=str, default=exe, help="Python command")98        parser.add_argument("--colab", action="store_true", help="Launch in colab")99        parser.add_argument(100            "--noparallel", action="store_true", help="Disable parallel processing"101        )102        parser.add_argument(103            "--noautoopen",104            action="store_true",105            help="Do not open in browser automatically",106        )107        parser.add_argument(  # Fork Feature. Paperspace integration for web UI108            "--paperspace",109            action="store_true",110            help="Note that this argument just shares a gradio link for the web UI. Thus can be used on other non-local CLI systems.",111        )112        parser.add_argument(  # Fork Feature. Embed a CLI into the infer-web.py113            "--is_cli",114            action="store_true",115            help="Use the CLI instead of setting up a gradio UI. This flag will launch an RVC text interface where you can execute functions from infer-web.py!",116        )117        cmd_opts = parser.parse_args()118 119        cmd_opts.port = cmd_opts.port if 0 <= cmd_opts.port <= 65535 else 7865120 121        return (122            cmd_opts.pycmd,123            cmd_opts.port,124            cmd_opts.colab,125            cmd_opts.noparallel,126            cmd_opts.noautoopen,127            cmd_opts.paperspace,128            cmd_opts.is_cli,129        )130 131    # has_mps is only available in nightly pytorch (for now) and MasOS 12.3+.132    # check `getattr` and try it for compatibility133    @staticmethod134    def has_mps() -> bool:135        if not torch.backends.mps.is_available():136            return False137        try:138            torch.zeros(1).to(torch.device("mps"))139            return True140        except Exception:141            return False142 143    def device_config(self) -> tuple:144        if torch.cuda.is_available():145            i_device = int(self.device.split(":")[-1])146            self.gpu_name = torch.cuda.get_device_name(i_device)147            if (148                ("16" in self.gpu_name and "V100" not in self.gpu_name.upper())149                or "P40" in self.gpu_name.upper()150                or "1060" in self.gpu_name151                or "1070" in self.gpu_name152                or "1080" in self.gpu_name153            ):154                print("Found GPU", self.gpu_name, ", force to fp32")155                self.is_half = False156            else:157                print("Found GPU", self.gpu_name)158                use_fp32_config()159            self.gpu_mem = int(160                torch.cuda.get_device_properties(i_device).total_memory161                / 1024162                / 1024163                / 1024164                + 0.4165            )166            if self.gpu_mem <= 4:167                with open("trainset_preprocess_pipeline_print.py", "r") as f:168                    strr = f.read().replace("3.7", "3.0")169                with open("trainset_preprocess_pipeline_print.py", "w") as f:170                    f.write(strr)171        elif self.has_mps():172            print("No supported Nvidia GPU found, use MPS instead")173            self.device = "mps"174            self.is_half = False175            use_fp32_config()176        else:177            print("No supported Nvidia GPU found, use CPU instead")178            self.device = "cpu"179            self.is_half = False180            use_fp32_config()181 182        if self.n_cpu == 0:183            self.n_cpu = cpu_count()184 185        if self.is_half:186            # 6G显存配置187            x_pad = 3188            x_query = 10189            x_center = 60190            x_max = 65191        else:192            # 5G显存配置193            x_pad = 1194            x_query = 6195            x_center = 38196            x_max = 41197 198        if self.gpu_mem != None and self.gpu_mem <= 4:199            x_pad = 1200            x_query = 5201            x_center = 30202            x_max = 32203 204        return x_pad, x_query, x_center, x_max205