# test_depth_checkpoint.py: Frame-by-frame decoding with the depth module (per-level sampling, cached state, # plan schedule) and the SilenceGuard. import argparse import json import os import sys from pathlib import Path import torch _REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(_REPO_ROOT)) sys.path.insert(0, str(_REPO_ROOT / "scripts")) NUM_LEVELS = int(os.environ.get("MVC_NUM_CODEC_LEVELS", "8")) CODEBOOK_SIZE = 2048 DEFAULT_PROMPTS = [ "The quick brown fox jumps over the lazy dog.", "It was the best of times, it was the worst of times.", "Good morning. I hope you slept well last night.", ] @torch.no_grad() def trim_trailing_silence(wav, sr, thresh_db=-40.0, keep_ms=120): import numpy as np w = np.asarray(wav, dtype=np.float32) mono = w.mean(axis=1) if w.ndim > 1 else w peak = float(np.max(np.abs(mono))) + 1e-9 thr = peak * (10.0 ** (thresh_db / 20.0)) above = np.abs(mono) > thr if not above.any(): return wav last = int(np.nonzero(above)[0][-1]) keep = min(len(mono), last + int(sr * keep_ms / 1000)) return w[:keep] @torch.no_grad() def think_generate(model, prefix, registry, tokenizer, max_think_tokens=200, temp=0.7, speaker_id=None, device="cuda"): backbone = model.backbone.backbone emb = backbone.embedding inject = None speaker_emb = None if speaker_id is not None and getattr(model, "use_speaker_conditioning", False): sid = torch.tensor([speaker_id], dtype=torch.long, device=device) speaker_emb = model.speaker_encoder(sid) if getattr(model, "use_speaker_input", False): inject = model.speaker_input_proj(speaker_emb) ids = prefix.clone().to(device) think_ids = [] ban_lo, ban_hi = registry.speech_token_id_min, registry.speech_token_id_max banned_ctl = [registry.audio_start_id, registry.audio_end_id, registry.eos_id, registry.think_start_id] for _ in range(max_think_tokens): if inject is not None: emb.set_speaker(inject) emb.set_codes(torch.full((1, ids.shape[1], NUM_LEVELS), -1, dtype=torch.long, device=device)) h = backbone(ids) if speaker_emb is not None: h = model.adaln(h, speaker_emb) logits = model.backbone.lm_head(h[:, -1, :])[0].float() logits[ban_lo:ban_hi + 1] = float("-inf") for t in banned_ctl: logits[t] = float("-inf") if temp > 0.0: nxt = int(torch.multinomial( torch.softmax(logits / temp, dim=-1), 1).item()) else: nxt = int(logits.argmax().item()) if nxt == registry.think_end_id: break think_ids.append(nxt) ids = torch.cat([ids, torch.tensor([[nxt]], device=device)], dim=1) tail = torch.tensor([[registry.think_end_id, registry.audio_start_id]], device=device) ids = torch.cat([ids, tail], dim=1) reasoning = tokenizer.decode(think_ids) if think_ids else "" return ids, reasoning def _half_up(v) -> int: return int(float(v) + 0.5) def build_decode_plan_schedule(durations, prefix_len, max_frames, *, mode="stretch", target_frames=None, device=None): from delay_dataset import NO_WORD, build_plan_schedule as _frame_map from model import build_plan_schedule as _position_schedule if prefix_len < 1: raise ValueError(f"prefix_len must be >= 1 (the