()
| 149 | |
| 150 | |
| 151 | def main() -> int: |
| 152 | args = parse_args() |
| 153 | model_dir = resolve_repo_path(args.model) |
| 154 | configure_local_model_loading(model_dir, args.ssl_checkpoint) |
| 155 | |
| 156 | if str(REFERENCE_ROOT) not in sys.path: |
| 157 | sys.path.insert(0, str(REFERENCE_ROOT)) |
| 158 | |
| 159 | import torch |
| 160 | from miocodec import MioCodecModel, load_audio |
| 161 | import miocodec.model as miocodec_model_module |
| 162 | |
| 163 | if args.precision == "fp32": |
| 164 | def fp32_autocast_context(device_type: str): |
| 165 | return nullcontext() |
| 166 | |
| 167 | miocodec_model_module._get_autocast_context = fp32_autocast_context |
| 168 | print(f"reference_precision={args.precision}") |
| 169 | |
| 170 | torch.set_num_threads(max(1, args.threads)) |
| 171 | if args.backend == "cuda": |
| 172 | if not torch.cuda.is_available(): |
| 173 | raise RuntimeError("CUDA backend requested but torch.cuda.is_available() is false") |
| 174 | torch.cuda.set_device(args.device) |
| 175 | device = torch.device(f"cuda:{args.device}") |
| 176 | else: |
| 177 | device = torch.device("cpu") |
| 178 | |
| 179 | model = MioCodecModel.from_pretrained( |
| 180 | config_path=str(model_dir / "config.yaml"), |
| 181 | weights_path=str(model_dir / "model.safetensors"), |
| 182 | ).eval().to(device) |
| 183 | |
| 184 | def load_request_audio(path: Path) -> torch.Tensor: |
| 185 | waveform = load_audio(str(resolve_repo_path(path)), sample_rate=model.config.sample_rate) |
| 186 | if args.max_audio_seconds > 0.0: |
| 187 | max_samples = int(round(args.max_audio_seconds * model.config.sample_rate)) |
| 188 | waveform = waveform[:max_samples] |
| 189 | if waveform.numel() == 0: |
| 190 | raise RuntimeError(f"MioCodec warmbench loaded empty audio: {path}") |
| 191 | return waveform.to(device) |
| 192 | |
| 193 | def run_vc_once(source_path: Path, target_path: Path) -> tuple[np.ndarray, int, float]: |
| 194 | source = load_request_audio(source_path) |
| 195 | target = load_request_audio(target_path) |
| 196 | sync_device(args.backend, args.device) |
| 197 | started = time.perf_counter() |
| 198 | with torch.inference_mode(): |
| 199 | decoded = model.voice_conversion(source, target) |
| 200 | sync_device(args.backend, args.device) |
| 201 | wall_ms = (time.perf_counter() - started) * 1000.0 |
| 202 | return decoded.detach().cpu().float().numpy(), int(model.config.sample_rate), wall_ms |
| 203 | |
| 204 | timing_lines: list[str] = [] |
| 205 | json_requests = load_json_requests(args) |
| 206 | if args.warmup_request_json: |
| 207 | warmup_request = json.loads(args.warmup_request_json) |
| 208 | if not isinstance(warmup_request, dict): |
no test coverage detected