mirror of
https://github.com/fumiama/Retrieval-based-Voice-Conversion-WebUI.git
synced 2026-06-05 01:10:22 +08:00
optimize(infer): move jit into rvc
This commit is contained in:
@@ -1 +0,0 @@
|
||||
from .utils import load, rmvpe_jit_export, synthesizer_jit_export
|
||||
@@ -1,12 +0,0 @@
|
||||
import torch
|
||||
|
||||
|
||||
def get_rmvpe(model_path="assets/rmvpe/rmvpe.pt", device=torch.device("cpu")):
|
||||
from rvc.f0.e2e import E2E
|
||||
|
||||
model = E2E(4, 1, (2, 2))
|
||||
ckpt = torch.load(model_path, map_location=device)
|
||||
model.load_state_dict(ckpt)
|
||||
model.eval()
|
||||
model = model.to(device)
|
||||
return model
|
||||
@@ -1,163 +0,0 @@
|
||||
from io import BytesIO
|
||||
import pickle
|
||||
import time
|
||||
import torch
|
||||
from tqdm import tqdm
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
def load_inputs(path, device, is_half=False):
|
||||
parm = torch.load(path, map_location=torch.device("cpu"))
|
||||
for key in parm.keys():
|
||||
parm[key] = parm[key].to(device)
|
||||
if is_half and parm[key].dtype == torch.float32:
|
||||
parm[key] = parm[key].half()
|
||||
elif not is_half and parm[key].dtype == torch.float16:
|
||||
parm[key] = parm[key].float()
|
||||
return parm
|
||||
|
||||
|
||||
def benchmark(
|
||||
model, inputs_path, device=torch.device("cpu"), epoch=1000, is_half=False
|
||||
):
|
||||
parm = load_inputs(inputs_path, device, is_half)
|
||||
total_ts = 0.0
|
||||
bar = tqdm(range(epoch))
|
||||
for i in bar:
|
||||
start_time = time.perf_counter()
|
||||
o = model(**parm)
|
||||
total_ts += time.perf_counter() - start_time
|
||||
print(f"num_epoch: {epoch} | avg time(ms): {(total_ts*1000)/epoch}")
|
||||
|
||||
|
||||
def jit_warm_up(model, inputs_path, device=torch.device("cpu"), epoch=5, is_half=False):
|
||||
benchmark(model, inputs_path, device, epoch=epoch, is_half=is_half)
|
||||
|
||||
|
||||
def to_jit_model(
|
||||
model_path,
|
||||
model_type: str,
|
||||
mode: str = "trace",
|
||||
inputs_path: str = None,
|
||||
device=torch.device("cpu"),
|
||||
is_half=False,
|
||||
):
|
||||
model = None
|
||||
if model_type.lower() == "synthesizer":
|
||||
from rvc.synthesizer import load_synthesizer
|
||||
|
||||
model, _ = load_synthesizer(model_path, device)
|
||||
model.forward = model.infer
|
||||
elif model_type.lower() == "rmvpe":
|
||||
from .rmvpe import get_rmvpe
|
||||
|
||||
model = get_rmvpe(model_path, device)
|
||||
elif model_type.lower() == "hubert":
|
||||
from rvc.hubert import get_hubert
|
||||
|
||||
model = get_hubert(model_path, device)
|
||||
model.forward = model.infer
|
||||
else:
|
||||
raise ValueError(f"No model type named {model_type}")
|
||||
model = model.eval()
|
||||
model = model.half() if is_half else model.float()
|
||||
if mode == "trace":
|
||||
assert not inputs_path
|
||||
inputs = load_inputs(inputs_path, device, is_half)
|
||||
model_jit = torch.jit.trace(model, example_kwarg_inputs=inputs)
|
||||
elif mode == "script":
|
||||
model_jit = torch.jit.script(model)
|
||||
model_jit.to(device)
|
||||
model_jit = model_jit.half() if is_half else model_jit.float()
|
||||
# model = model.half() if is_half else model.float()
|
||||
return (model, model_jit)
|
||||
|
||||
|
||||
def export(
|
||||
model: torch.nn.Module,
|
||||
mode: str = "trace",
|
||||
inputs: dict = None,
|
||||
device=torch.device("cpu"),
|
||||
is_half: bool = False,
|
||||
) -> dict:
|
||||
model = model.half() if is_half else model.float()
|
||||
model.eval()
|
||||
if mode == "trace":
|
||||
assert inputs is not None
|
||||
model_jit = torch.jit.trace(model, example_kwarg_inputs=inputs)
|
||||
elif mode == "script":
|
||||
model_jit = torch.jit.script(model)
|
||||
model_jit.to(device)
|
||||
model_jit = model_jit.half() if is_half else model_jit.float()
|
||||
buffer = BytesIO()
|
||||
# model_jit=model_jit.cpu()
|
||||
torch.jit.save(model_jit, buffer)
|
||||
del model_jit
|
||||
cpt = OrderedDict()
|
||||
cpt["model"] = buffer.getvalue()
|
||||
cpt["is_half"] = is_half
|
||||
return cpt
|
||||
|
||||
|
||||
def load(path: str):
|
||||
with open(path, "rb") as f:
|
||||
return pickle.load(f)
|
||||
|
||||
|
||||
def save(ckpt: dict, save_path: str):
|
||||
with open(save_path, "wb") as f:
|
||||
pickle.dump(ckpt, f)
|
||||
|
||||
|
||||
def rmvpe_jit_export(
|
||||
model_path: str,
|
||||
mode: str = "script",
|
||||
inputs_path: str = None,
|
||||
save_path: str = None,
|
||||
device=torch.device("cpu"),
|
||||
is_half=False,
|
||||
):
|
||||
if not save_path:
|
||||
save_path = model_path.rstrip(".pth")
|
||||
save_path += ".half.jit" if is_half else ".jit"
|
||||
if "cuda" in str(device) and ":" not in str(device):
|
||||
device = torch.device("cuda:0")
|
||||
from .rmvpe import get_rmvpe
|
||||
|
||||
model = get_rmvpe(model_path, device)
|
||||
inputs = None
|
||||
if mode == "trace":
|
||||
inputs = load_inputs(inputs_path, device, is_half)
|
||||
ckpt = export(model, mode, inputs, device, is_half)
|
||||
ckpt["device"] = str(device)
|
||||
save(ckpt, save_path)
|
||||
return ckpt
|
||||
|
||||
|
||||
def synthesizer_jit_export(
|
||||
model_path: str,
|
||||
mode: str = "script",
|
||||
inputs_path: str = None,
|
||||
save_path: str = None,
|
||||
device=torch.device("cpu"),
|
||||
is_half=False,
|
||||
):
|
||||
if not save_path:
|
||||
save_path = model_path.rstrip(".pth")
|
||||
save_path += ".half.jit" if is_half else ".jit"
|
||||
if "cuda" in str(device) and ":" not in str(device):
|
||||
device = torch.device("cuda:0")
|
||||
from rvc.synthesizer import load_synthesizer
|
||||
|
||||
model, cpt = load_synthesizer(model_path, device)
|
||||
assert isinstance(cpt, dict)
|
||||
model.forward = model.infer
|
||||
inputs = None
|
||||
if mode == "trace":
|
||||
inputs = load_inputs(inputs_path, device, is_half)
|
||||
ckpt = export(model, mode, inputs, device, is_half)
|
||||
cpt.pop("weight")
|
||||
cpt["model"] = ckpt["model"]
|
||||
cpt["device"] = device
|
||||
save(cpt, save_path)
|
||||
return cpt
|
||||
@@ -125,27 +125,10 @@ class RVC:
|
||||
self.net_g = self.net_g.float()
|
||||
|
||||
def set_jit_model():
|
||||
jit_pth_path = self.pth_path.rstrip(".pth")
|
||||
jit_pth_path += ".half.jit" if self.is_half else ".jit"
|
||||
reload = False
|
||||
if str(self.device) == "cuda":
|
||||
self.device = torch.device("cuda:0")
|
||||
if os.path.exists(jit_pth_path):
|
||||
cpt = jit.load(jit_pth_path)
|
||||
model_device = cpt["device"]
|
||||
if model_device != str(self.device):
|
||||
reload = True
|
||||
else:
|
||||
reload = True
|
||||
from rvc.jit import get_jit_model
|
||||
from rvc.synthesizer import synthesizer_jit_export
|
||||
|
||||
if reload:
|
||||
cpt = jit.synthesizer_jit_export(
|
||||
self.pth_path,
|
||||
"script",
|
||||
None,
|
||||
device=self.device,
|
||||
is_half=self.is_half,
|
||||
)
|
||||
cpt = get_jit_model(self.pth_path, self.is_half, synthesizer_jit_export)
|
||||
|
||||
self.tgt_sr = cpt["config"][-1]
|
||||
self.if_f0 = cpt.get("f0", 1)
|
||||
|
||||
Reference in New Issue
Block a user