Download scripts/pipeline/stage5_synth.py from mistmlm/rvc-models: direct link, hf CLI and curl.
- Browser
- Download file 5.61 kB
-
https://huggingface.co/mistmlm/rvc-models/resolve/main/scripts/pipeline/stage5_synth.py
- Command line
-
hf download hf://mistmlm/rvc-models/scripts/pipeline/stage5_synth.py
-
curl -L -o stage5_synth.py https://huggingface.co/mistmlm/rvc-models/resolve/main/scripts/pipeline/stage5_synth.py
5.61 kB
| """Stage 5: qai-hub compile Synthesizer parts → Samsung Galaxy S23 (Family) .bin. | |
| Three traced torchscripts (T_ph = 15 = 320ms at 50Hz phone rate): | |
| encflow_T15.pt2 : (phone 1,15,768) + (pitch 1,15) + (sid 0,) | |
| -> (z, z_p, logs, g_emb) each (1, 192, 15) | |
| flowreverse_T15 : (z_p 1,192,15) + (mask 1,1,15) + (g 1,192,15) | |
| -> z (1, 192, 15) | |
| decnsf_T15 : (z 1,192,15) + (f0 1,15) + (sid 0,) | |
| -> audio (1, 1, 7200) = 0.15s @ 48k | |
| The split lets us put each chunk on NPU independently. RVC inference | |
| fans these out at 320ms wallclock cadence; the bottleneck for end-to-end | |
| real-time is decnsf (GeneratorNSF is heavy). | |
| Forward-compat: compiled for Hexagon v73 (S23) → runs on v79 (8 Elite). | |
| """ | |
| import os | |
| import sys | |
| import shutil | |
| import qai_hub | |
| OUT_BIN_DIR = "models/synthesizer" | |
| os.makedirs(OUT_BIN_DIR, exist_ok=True) | |
| # Each part: (name, pt2 path, onnx path, bin path, input_specs, output_names, input_names) | |
| T_PH = 15 | |
| PARTS = [ | |
| { | |
| "name": "encflow", | |
| "pt2": "models/synthesizer/encflow_T15.pt2", | |
| "onnx": "models/synthesizer/encflow_T15.onnx", | |
| "bin": "models/synthesizer/encflow_s23.bin", | |
| "input_names": ["phone", "pitch", "sid"], | |
| "input_specs": { | |
| "phone": ((1, T_PH, 768), "float32"), | |
| "pitch": ((1, T_PH), "int64"), | |
| "sid": ((1,), "int64"), | |
| }, | |
| "outputs": ["m_p", "logs_p", "mask", "g"], | |
| }, | |
| { | |
| "name": "flowreverse", | |
| "pt2": "models/synthesizer/flowreverse_T15.pt2", | |
| "onnx": "models/synthesizer/flowreverse_T15.onnx", | |
| "bin": "models/synthesizer/flowreverse_s23.bin", | |
| "input_names": ["z_p", "mask", "g"], | |
| "input_specs": { | |
| "z_p": ((1, 192, T_PH), "float32"), | |
| "mask": ((1, 1, T_PH), "float32"), | |
| "g": ((1, 256, 1), "float32"), # speaker emb from encflow | |
| }, | |
| }, | |
| { | |
| "name": "decnsf", | |
| "pt2": "models/synthesizer/decnsf_T15.pt2", | |
| "onnx": "models/synthesizer/decnsf_T15.onnx", | |
| "bin": "models/synthesizer/decnsf_s23.bin", | |
| "input_names": ["z", "f0", "sid"], | |
| "input_specs": { | |
| "z": ((1, 192, T_PH), "float32"), | |
| "f0": ((1, T_PH), "float32"), | |
| "sid": ((1,), "int64"), | |
| }, | |
| }, | |
| ] | |
| def export_onnx(part): | |
| if os.path.exists(part["onnx"]) and os.path.getsize(part["onnx"]) > 1_000_000: | |
| print(f" ONNX exists ({os.path.getsize(part['onnx'])/1e6:.1f} MB), skipping export") | |
| return | |
| import torch | |
| m = torch.jit.load(part["pt2"]) | |
| m.eval() | |
| # Build example inputs from spec | |
| if part["name"] == "encflow": | |
| # pitch is used as nn.Embedding index -> must be int64 | |
| phone = torch.randn(1, T_PH, 768) | |
| pitch = torch.zeros(1, T_PH, dtype=torch.long) | |
| sid = torch.tensor([0], dtype=torch.long) | |
| args = (phone, pitch, sid) | |
| elif part["name"] == "flowreverse": | |
| z_p = torch.randn(1, 192, T_PH) | |
| mask = torch.ones(1, 1, T_PH) | |
| g = torch.randn(1, 256, 1) # speaker embedding from encflow | |
| args = (z_p, mask, g) | |
| elif part["name"] == "decnsf": | |
| z = torch.randn(1, 192, T_PH) | |
| f0 = torch.zeros(1, T_PH) # phone-rate f0, continuous Hz | |
| sid = torch.tensor([0], dtype=torch.long) | |
| args = (z, f0, sid) | |
| with torch.no_grad(): | |
| y_ref = m(*args) | |
| if isinstance(y_ref, tuple): | |
| out_shapes = [tuple(t.shape) for t in y_ref] | |
| else: | |
| out_shapes = [tuple(y_ref.shape)] | |
| print(f" eager out: {out_shapes}") | |
| # output_names from part["outputs"] if set, else generic | |
| if "outputs" in part and len(part["outputs"]) == len(out_shapes): | |
| out_names = part["outputs"] | |
| else: | |
| out_names = [f"out_{i}" for i in range(len(out_shapes))] | |
| print(f" exporting to {part['onnx']} ...") | |
| torch.onnx.export( | |
| m, | |
| args, | |
| part["onnx"], | |
| input_names=part["input_names"], | |
| output_names=out_names, | |
| opset_version=14, | |
| dynamo=False, | |
| do_constant_folding=True, | |
| ) | |
| import onnx | |
| onnx.checker.check_model(onnx.load(part["onnx"])) | |
| print(f" onnx.checker: OK") | |
| def compile_part(part, s23): | |
| import onnx | |
| onnx_model = onnx.load(part["onnx"]) | |
| print(f"\n--- compile {part['name']} ---") | |
| job = qai_hub.submit_compile_job( | |
| model=onnx_model, | |
| device=s23, | |
| input_specs=part["input_specs"], | |
| options="--target_runtime qnn_context_binary --truncate_64bit_io", | |
| ) | |
| print(f" job: {job}") | |
| job.wait() | |
| status = job.get_status() | |
| print(f" status: {status}") | |
| if str(status.state) != "State.SUCCESS": | |
| print(f" COMPILE FAILED: {status.state}") | |
| try: | |
| print(f" log: {job.download_artifacts_for_type(OUT_BIN_DIR, qai_hub.JobArtifactType.HUB_LOG)}") | |
| except Exception as e: | |
| print(f" (no log: {e})") | |
| return None | |
| got = job.download_target_model(part["bin"]) | |
| if got and os.path.isfile(got) and os.path.abspath(got) != os.path.abspath(part["bin"]): | |
| shutil.move(got, part["bin"]) | |
| print(f" saved {part['bin']} ({os.path.getsize(part['bin'])/1e6:.1f} MB)") | |
| return job.job_id | |
| if __name__ == "__main__": | |
| for part in PARTS: | |
| print(f"\n=== {part['name']} ===") | |
| export_onnx(part) | |
| devs = qai_hub.get_devices() | |
| s23 = next(d for d in devs if d.name == "Samsung Galaxy S23 (Family)") | |
| print(f"\ntarget: {s23.name}") | |
| for part in PARTS: | |
| compile_part(part, s23) | |