Skip to content

Commit 1963eb0

Browse files
committed
support cpu training, use cpu training on mac
1 parent 9317817 commit 1963eb0

File tree

8 files changed

+41
-43
lines changed

8 files changed

+41
-43
lines changed

GPT_SoVITS/inference_webui.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
is_share = eval(is_share)
5050
if "_CUDA_VISIBLE_DEVICES" in os.environ:
5151
os.environ["CUDA_VISIBLE_DEVICES"] = os.environ["_CUDA_VISIBLE_DEVICES"]
52-
is_half = eval(os.environ.get("is_half", "True")) and not torch.backends.mps.is_available()
52+
is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
5353
import gradio as gr
5454
from transformers import AutoModelForMaskedLM, AutoTokenizer
5555
import numpy as np
@@ -69,7 +69,7 @@
6969

7070
i18n = I18nAuto()
7171

72-
os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1' # 确保直接启动推理UI时也能够设置。
72+
# os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1' # 确保直接启动推理UI时也能够设置。
7373

7474
if torch.cuda.is_available():
7575
device = "cuda"

GPT_SoVITS/prepare_datasets/1-get-text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def my_save(fea,path):#####fix issue: torch.save doesn't support chinese path
4949
os.makedirs(bert_dir, exist_ok=True)
5050
if torch.cuda.is_available():
5151
device = "cuda:0"
52-
elif torch.backends.mps.is_available():
53-
device = "mps"
52+
# elif torch.backends.mps.is_available():
53+
# device = "mps"
5454
else:
5555
device = "cpu"
5656
tokenizer = AutoTokenizer.from_pretrained(bert_pretrained_dir)

GPT_SoVITS/prepare_datasets/2-get-hubert-wav32k.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def my_save(fea,path):#####fix issue: torch.save doesn't support chinese path
5050
alpha=0.5
5151
if torch.cuda.is_available():
5252
device = "cuda:0"
53-
elif torch.backends.mps.is_available():
54-
device = "mps"
53+
# elif torch.backends.mps.is_available():
54+
# device = "mps"
5555
else:
5656
device = "cpu"
5757
model=cnhubert.get_model()

GPT_SoVITS/prepare_datasets/3-get-semantic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
if torch.cuda.is_available():
4242
device = "cuda"
43-
elif torch.backends.mps.is_available():
44-
device = "mps"
43+
# elif torch.backends.mps.is_available():
44+
# device = "mps"
4545
else:
4646
device = "cpu"
4747
hps = utils.get_hparams_from_file(s2config_path)

GPT_SoVITS/s1_train.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ def main(args):
118118
os.environ["MASTER_ADDR"]="localhost"
119119
trainer: Trainer = Trainer(
120120
max_epochs=config["train"]["epochs"],
121-
accelerator="gpu",
121+
accelerator="gpu" if torch.cuda.is_available() else "cpu",
122122
# val_check_interval=9999999999999999999999,###不要验证
123123
# check_val_every_n_epoch=None,
124124
limit_val_batches=0,
125-
devices=-1,
125+
devices=-1 if torch.cuda.is_available() else 1,
126126
benchmark=False,
127127
fast_dev_run=False,
128-
strategy = "auto" if torch.backends.mps.is_available() else DDPStrategy(
128+
strategy = DDPStrategy(
129129
process_group_backend="nccl" if platform.system() != "Windows" else "gloo"
130-
), # mps 不支持多节点训练
130+
) if torch.cuda.is_available() else "auto",
131131
precision=config["train"]["precision"],
132132
logger=logger,
133133
num_sanity_val_steps=0,

GPT_SoVITS/s2_train.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
# from config import pretrained_s2G,pretrained_s2D
4242
global_step = 0
4343

44+
device = "cpu" # cuda以外的设备,等mps优化后加入
45+
4446

4547
def main():
46-
"""Assume Single Node Multi GPUs Training Only"""
47-
assert torch.cuda.is_available() or torch.backends.mps.is_available(), "Only GPU training is allowed."
4848

49-
if torch.backends.mps.is_available():
50-
n_gpus = 1
51-
else:
49+
if torch.cuda.is_available():
5250
n_gpus = torch.cuda.device_count()
51+
else:
52+
n_gpus = 1
5353
os.environ["MASTER_ADDR"] = "localhost"
5454
os.environ["MASTER_PORT"] = str(randint(20000, 55555))
5555

@@ -73,7 +73,7 @@ def run(rank, n_gpus, hps):
7373
writer_eval = SummaryWriter(log_dir=os.path.join(hps.s2_ckpt_dir, "eval"))
7474

7575
dist.init_process_group(
76-
backend = "gloo" if os.name == "nt" or torch.backends.mps.is_available() else "nccl",
76+
backend = "gloo" if os.name == "nt" or not torch.cuda.is_available() else "nccl",
7777
init_method="env://",
7878
world_size=n_gpus,
7979
rank=rank,
@@ -137,9 +137,9 @@ def run(rank, n_gpus, hps):
137137
hps.train.segment_size // hps.data.hop_length,
138138
n_speakers=hps.data.n_speakers,
139139
**hps.model,
140-
).to("mps")
140+
).to(device)
141141

142-
net_d = MultiPeriodDiscriminator(hps.model.use_spectral_norm).cuda(rank) if torch.cuda.is_available() else MultiPeriodDiscriminator(hps.model.use_spectral_norm).to("mps")
142+
net_d = MultiPeriodDiscriminator(hps.model.use_spectral_norm).cuda(rank) if torch.cuda.is_available() else MultiPeriodDiscriminator(hps.model.use_spectral_norm).to(device)
143143
for name, param in net_g.named_parameters():
144144
if not param.requires_grad:
145145
print(name, "not requires_grad")
@@ -187,8 +187,8 @@ def run(rank, n_gpus, hps):
187187
net_g = DDP(net_g, device_ids=[rank], find_unused_parameters=True)
188188
net_d = DDP(net_d, device_ids=[rank], find_unused_parameters=True)
189189
else:
190-
net_g = net_g.to("mps")
191-
net_d = net_d.to("mps")
190+
net_g = net_g.to(device)
191+
net_d = net_d.to(device)
192192

193193
try: # 如果能加载自动resume
194194
_, _, _, epoch_str = utils.load_checkpoint(
@@ -320,12 +320,12 @@ def train_and_evaluate(
320320
rank, non_blocking=True
321321
)
322322
else:
323-
spec, spec_lengths = spec.to("mps"), spec_lengths.to("mps")
324-
y, y_lengths = y.to("mps"), y_lengths.to("mps")
325-
ssl = ssl.to("mps")
323+
spec, spec_lengths = spec.to(device), spec_lengths.to(device)
324+
y, y_lengths = y.to(device), y_lengths.to(device)
325+
ssl = ssl.to(device)
326326
ssl.requires_grad = False
327327
# ssl_lengths = ssl_lengths.cuda(rank, non_blocking=True)
328-
text, text_lengths = text.to("mps"), text_lengths.to("mps")
328+
text, text_lengths = text.to(device), text_lengths.to(device)
329329

330330
with autocast(enabled=hps.train.fp16_run):
331331
(
@@ -532,10 +532,10 @@ def evaluate(hps, generator, eval_loader, writer_eval):
532532
ssl = ssl.cuda()
533533
text, text_lengths = text.cuda(), text_lengths.cuda()
534534
else:
535-
spec, spec_lengths = spec.to("mps"), spec_lengths.to("mps")
536-
y, y_lengths = y.to("mps"), y_lengths.to("mps")
537-
ssl = ssl.to("mps")
538-
text, text_lengths = text.to("mps"), text_lengths.to("mps")
535+
spec, spec_lengths = spec.to(device), spec_lengths.to(device)
536+
y, y_lengths = y.to(device), y_lengths.to(device)
537+
ssl = ssl.to(device)
538+
text, text_lengths = text.to(device), text_lengths.to(device)
539539
for test in [0, 1]:
540540
y_hat, mask, *_ = generator.module.infer(
541541
ssl, spec, spec_lengths, text, text_lengths, test=test

api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
`-dt` - `默认参考音频文本`
1414
`-dl` - `默认参考音频语种, "中文","英文","日文","zh","en","ja"`
1515
16-
`-d` - `推理设备, "cuda","cpu","mps"`
16+
`-d` - `推理设备, "cuda","cpu"`
1717
`-a` - `绑定地址, 默认"127.0.0.1"`
1818
`-p` - `绑定端口, 默认9880, 可在 config.py 中指定`
1919
`-fp` - `覆盖 config.py 使用全精度`
@@ -143,7 +143,7 @@
143143
parser.add_argument("-dt", "--default_refer_text", type=str, default="", help="默认参考音频文本")
144144
parser.add_argument("-dl", "--default_refer_language", type=str, default="", help="默认参考音频语种")
145145

146-
parser.add_argument("-d", "--device", type=str, default=g_config.infer_device, help="cuda / cpu / mps")
146+
parser.add_argument("-d", "--device", type=str, default=g_config.infer_device, help="cuda / cpu")
147147
parser.add_argument("-a", "--bind_addr", type=str, default="0.0.0.0", help="default: 0.0.0.0")
148148
parser.add_argument("-p", "--port", type=int, default=g_config.api_port, help="default: 9880")
149149
parser.add_argument("-fp", "--full_precision", action="store_true", default=False, help="覆盖config.is_half为False, 使用全精度")
@@ -482,9 +482,6 @@ def handle(refer_wav_path, prompt_text, prompt_language, text, text_language):
482482
wav.seek(0)
483483

484484
torch.cuda.empty_cache()
485-
if device == "mps":
486-
print('executed torch.mps.empty_cache()')
487-
torch.mps.empty_cache()
488485
return StreamingResponse(wav, media_type="audio/wav")
489486

490487

webui.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from tools.my_utils import load_audio
5656
from multiprocessing import cpu_count
5757

58-
os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1' # 当遇到mps不支持的步骤时使用cpu
58+
# os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1' # 当遇到mps不支持的步骤时使用cpu
5959

6060
n_cpu=cpu_count()
6161

@@ -73,18 +73,19 @@
7373
if_gpu_ok = True # 至少有一张能用的N卡
7474
gpu_infos.append("%s\t%s" % (i, gpu_name))
7575
mem.append(int(torch.cuda.get_device_properties(i).total_memory/ 1024/ 1024/ 1024+ 0.4))
76-
# 判断是否支持mps加速
77-
if torch.backends.mps.is_available():
78-
if_gpu_ok = True
79-
gpu_infos.append("%s\t%s" % ("0", "Apple GPU"))
80-
mem.append(psutil.virtual_memory().total/ 1024 / 1024 / 1024) # 实测使用系统内存作为显存不会爆显存
76+
# # 判断是否支持mps加速
77+
# if torch.backends.mps.is_available():
78+
# if_gpu_ok = True
79+
# gpu_infos.append("%s\t%s" % ("0", "Apple GPU"))
80+
# mem.append(psutil.virtual_memory().total/ 1024 / 1024 / 1024) # 实测使用系统内存作为显存不会爆显存
8181

8282
if if_gpu_ok and len(gpu_infos) > 0:
8383
gpu_info = "\n".join(gpu_infos)
8484
default_batch_size = min(mem) // 2
8585
else:
86-
gpu_info = i18n("很遗憾您这没有能用的显卡来支持您训练")
87-
default_batch_size = 1
86+
gpu_info = ("%s\t%s" % ("0", "CPU"))
87+
gpu_infos.append("%s\t%s" % ("0", "CPU"))
88+
default_batch_size = psutil.virtual_memory().total/ 1024 / 1024 / 1024 / 2
8889
gpus = "-".join([i[0] for i in gpu_infos])
8990

9091
pretrained_sovits_name="GPT_SoVITS/pretrained_models/s2G488k.pth"

0 commit comments

Comments
 (0)