Eval harness v2: clean rewrite — no walrus helpers, ref embedding computed before arm loop, trackio imported
Browse files- eval_2x2.py +38 -51
eval_2x2.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
"""eval_2x2.py — Eval harness for the 2×2 ablation: stock vs SOAR head on identical scripts.
|
| 2 |
|
| 3 |
Generates the same 9 two-speaker dialogue scripts (short/medium/long × 3) with BOTH the
|
| 4 |
-
stock head and the SOAR-trained head
|
| 5 |
-
- WER
|
| 6 |
-
- SIM-O
|
| 7 |
- Drift: cosine(first 30s, last 30s) within each long sample
|
| 8 |
-
- RTF per sample
|
| 9 |
|
| 10 |
-
Env: ARMS (comma list, default "stock,soar")
|
| 11 |
-
|
| 12 |
-
Prints EVAL DONE at the end.
|
| 13 |
"""
|
| 14 |
import io, json, os, sys, time
|
| 15 |
sys.path.insert(0, "/VibeVoice-classic")
|
|
@@ -17,7 +16,10 @@ sys.path.insert(0, "/VibeVoice-classic")
|
|
| 17 |
import numpy as np
|
| 18 |
import soundfile as sf
|
| 19 |
import torch
|
|
|
|
|
|
|
| 20 |
from huggingface_hub import hf_hub_download, HfApi
|
|
|
|
| 21 |
|
| 22 |
from vibevoice.modular.modeling_vibevoice import VibeVoiceForConditionalGeneration
|
| 23 |
from vibevoice.processor.vibevoice_processor import VibeVoiceProcessor
|
|
@@ -27,10 +29,8 @@ SOAR_REPO = "ACloudCenter/vibevoice-soar-head-1.5B"
|
|
| 27 |
OUT_REPO = "ACloudCenter/vibevoice-eval-2x2"
|
| 28 |
SR = 24000
|
| 29 |
device = "cuda"
|
| 30 |
-
ARMS = os.environ.get("ARMS", "stock,soar").split(",")
|
| 31 |
-
REPO_TYPE = "model"
|
| 32 |
|
| 33 |
-
# ---------------- fixed eval scripts (2 speakers: Speaker 1 / Speaker 2) ----------------
|
| 34 |
SHORTS = [
|
| 35 |
"Speaker 1: Did you see the meteor shower last night?\nSpeaker 2: I missed it! Was it worth staying up for?\nSpeaker 1: Absolutely. The sky lit up green for a full minute.\nSpeaker 2: Okay, now I'm actually upset I slept through it.",
|
| 36 |
"Speaker 1: The bread machine finally arrived.\nSpeaker 2: So you're a baker now?\nSpeaker 1: I'm a disaster with a bread machine. The first loaf came out like a brick.\nSpeaker 2: Send me a photo. Immediately.",
|
|
@@ -47,6 +47,7 @@ LONGS = [
|
|
| 47 |
"Speaker 1: I finally fixed the shed door.\nSpeaker 2: The one that's been hanging crooked since spring?\nSpeaker 1: Since spring. The hinges were stripped, not the frame, which saved me a full rebuild.\nSpeaker 2: How long did that take?\nSpeaker 1: Four hours, two of which were finding screws that actually fit.\nSpeaker 2: The hardware store run is always the real project.\nSpeaker 1: Two trips. One for the wrong screws, one for the right ones.\nSpeaker 2: Classic. Did you paint it?\nSpeaker 1: Not yet. The wood needs to dry out first, apparently.\nSpeaker 2: So the project continues.\nSpeaker 1: The project continues. But the door closes now, which is more than March could say.",
|
| 48 |
]
|
| 49 |
|
|
|
|
| 50 |
def load_model_with_head(load_soar):
|
| 51 |
model = VibeVoiceForConditionalGeneration.from_pretrained(
|
| 52 |
MODEL, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True,
|
|
@@ -54,10 +55,15 @@ def load_model_with_head(load_soar):
|
|
| 54 |
if load_soar:
|
| 55 |
sd_path = hf_hub_download(SOAR_REPO, "soar_head_state.pt")
|
| 56 |
sd = torch.load(sd_path, map_location="cpu", weights_only=True)
|
| 57 |
-
|
| 58 |
print(f"loaded SOAR head: {len(sd)} tensors", flush=True)
|
| 59 |
return model
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def main():
|
| 62 |
api = HfApi()
|
| 63 |
scripts = ([("short", i, t) for i, t in enumerate(SHORTS)]
|
|
@@ -70,28 +76,19 @@ def main():
|
|
| 70 |
refs[spk] = wav.astype(np.float32)
|
| 71 |
|
| 72 |
proc = VibeVoiceProcessor.from_pretrained(MODEL)
|
| 73 |
-
asr = torch.device(device)
|
| 74 |
from transformers import pipeline
|
| 75 |
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3",
|
| 76 |
-
device=
|
| 77 |
-
|
| 78 |
-
enc = VoiceEncoder(device)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
def sim_to_ref(wav, ref_key):
|
| 84 |
-
emb = enc.embed_utterance(preprocess_wav(wav, source_sr=SR))
|
| 85 |
-
ref = enc.embed_utterance(preprocess_wav(refs[ref_key_for(ref_key_for_i(ref_key=ref_key_for_i:=None))])) if False else None
|
| 86 |
-
return emb
|
| 87 |
|
| 88 |
results = []
|
| 89 |
os.makedirs("/out", exist_ok=True)
|
| 90 |
for arm in ARMS:
|
| 91 |
-
arm = arm.strip()
|
| 92 |
print(f"=== ARM {arm} ===", flush=True)
|
| 93 |
model = load_model_with_head(load_soar=(arm == "soar"))
|
| 94 |
-
model.model.noise_scheduler = model.model.noise_scheduler # default ddpm
|
| 95 |
model.set_ddpm_inference_steps(num_steps=10)
|
| 96 |
for tier, idx, script in scripts:
|
| 97 |
t0 = time.time()
|
|
@@ -102,43 +99,30 @@ def main():
|
|
| 102 |
cfg_scale=1.3,
|
| 103 |
generation_config={"do_sample": False},
|
| 104 |
)
|
| 105 |
-
audio = out.speech_outputs[0]
|
| 106 |
-
if torch.is_tensor(audio := audio or None) or True:
|
| 107 |
-
pass
|
| 108 |
gen_s = time.time() - t0
|
|
|
|
| 109 |
wav = audio if isinstance(audio, np.ndarray) else audio.float().cpu().numpy()
|
|
|
|
| 110 |
audio_s = len(wav) / SR
|
| 111 |
rtf = gen_s / max(audio_s, 1e-6)
|
| 112 |
fn = f"{arm}_{tier}_{idx}.wav"
|
| 113 |
sf.write(f"/out/{fn}", wav, SR)
|
| 114 |
hyp = transcriber(f"/out/{fn}", generate_kwargs={"language": "english"})["text"].strip()
|
| 115 |
-
gt =
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
emb_gen = enc.embed_utterance(preprocess_wav(f"/out/{fn}"))
|
| 120 |
-
ref_key = tier
|
| 121 |
-
ref_idx = {"short": 0, "medium": 0, "long": 0}[tier]
|
| 122 |
-
ref_name = {"Speaker 1": "en-Carter_man.wav", "Speaker 2": "en-Maya_woman.wav"}
|
| 123 |
-
sims = []
|
| 124 |
-
for spk_line in script.strip().splitlines():
|
| 125 |
-
spk = spk_line.split(":")[0] if (spk_line := spk_line) else None
|
| 126 |
-
# simple: SIM vs combined ref (concat refs)
|
| 127 |
-
ref_wav = np.concatenate([refs[1], np.zeros(SR // 4, dtype=np.float32), refs[2]])
|
| 128 |
-
emb_ref = enc.embed_utterance(preprocess_wav(ref_wav.astype(np.float32), SR))
|
| 129 |
-
sim = float(np.dot(emb_gen, ref_emb) / (np.linalg.norm(emb_gen) * np.linalg.norm(ref_emb)))
|
| 130 |
drift = None
|
| 131 |
if tier == "long" and audio_s > 60:
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
e2 = enc.embed_utterance(preprocess_wav(last, SR))
|
| 135 |
drift = float(np.dot(e1, e2) / (np.linalg.norm(e1) * np.linalg.norm(e2)))
|
| 136 |
results.append({"arm": arm, "tier": tier, "idx": idx, "audio_s": audio_s,
|
| 137 |
-
"gen_s": gen_s, "rtf": rtf, "wer":
|
| 138 |
"drift": drift, "hyp": hyp})
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
trackio.log({"rtf": rtf, "wer":
|
| 142 |
api.upload_file(path_or_fileobj=f"/out/{fn}", path_in_repo=f"{arm}/{fn}",
|
| 143 |
repo_id=OUT_REPO, repo_type="model")
|
| 144 |
torch.cuda.empty_cache()
|
|
@@ -147,7 +131,6 @@ def main():
|
|
| 147 |
|
| 148 |
with open("/out/results.json", "w") as f:
|
| 149 |
json.dump(results, f, indent=2)
|
| 150 |
-
import statistics as st
|
| 151 |
summary = {}
|
| 152 |
for arm in ARMS:
|
| 153 |
rows = [r for r in results if r["arm"] == arm]
|
|
@@ -164,4 +147,8 @@ def main():
|
|
| 164 |
repo_id=OUT_REPO, repo_type="model")
|
| 165 |
print("EVAL DONE", flush=True)
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""eval_2x2.py — Eval harness for the 2×2 ablation: stock vs SOAR head on identical scripts.
|
| 2 |
|
| 3 |
Generates the same 9 two-speaker dialogue scripts (short/medium/long × 3) with BOTH the
|
| 4 |
+
stock head and the SOAR-trained head, then computes per arm:
|
| 5 |
+
- WER via openai/whisper-large-v3 vs the known ground-truth scripts
|
| 6 |
+
- SIM-O: resemblyzer speaker-embedding cosine (generated sample vs combined voice refs)
|
| 7 |
- Drift: cosine(first 30s, last 30s) within each long sample
|
| 8 |
+
- RTF per sample
|
| 9 |
|
| 10 |
+
Env: ARMS (comma list, default "stock,soar"). Pushes wavs + results to
|
| 11 |
+
ACloudCenter/vibevoice-eval-2x2 under {arm}/. Prints EVAL DONE at the end.
|
|
|
|
| 12 |
"""
|
| 13 |
import io, json, os, sys, time
|
| 14 |
sys.path.insert(0, "/VibeVoice-classic")
|
|
|
|
| 16 |
import numpy as np
|
| 17 |
import soundfile as sf
|
| 18 |
import torch
|
| 19 |
+
import jiwer
|
| 20 |
+
import statistics as st
|
| 21 |
from huggingface_hub import hf_hub_download, HfApi
|
| 22 |
+
from resemblyzer import VoiceEncoder, preprocess_wav
|
| 23 |
|
| 24 |
from vibevoice.modular.modeling_vibevoice import VibeVoiceForConditionalGeneration
|
| 25 |
from vibevoice.processor.vibevoice_processor import VibeVoiceProcessor
|
|
|
|
| 29 |
OUT_REPO = "ACloudCenter/vibevoice-eval-2x2"
|
| 30 |
SR = 24000
|
| 31 |
device = "cuda"
|
| 32 |
+
ARMS = [a.strip() for a in os.environ.get("ARMS", "stock,soar").split(",")]
|
|
|
|
| 33 |
|
|
|
|
| 34 |
SHORTS = [
|
| 35 |
"Speaker 1: Did you see the meteor shower last night?\nSpeaker 2: I missed it! Was it worth staying up for?\nSpeaker 1: Absolutely. The sky lit up green for a full minute.\nSpeaker 2: Okay, now I'm actually upset I slept through it.",
|
| 36 |
"Speaker 1: The bread machine finally arrived.\nSpeaker 2: So you're a baker now?\nSpeaker 1: I'm a disaster with a bread machine. The first loaf came out like a brick.\nSpeaker 2: Send me a photo. Immediately.",
|
|
|
|
| 47 |
"Speaker 1: I finally fixed the shed door.\nSpeaker 2: The one that's been hanging crooked since spring?\nSpeaker 1: Since spring. The hinges were stripped, not the frame, which saved me a full rebuild.\nSpeaker 2: How long did that take?\nSpeaker 1: Four hours, two of which were finding screws that actually fit.\nSpeaker 2: The hardware store run is always the real project.\nSpeaker 1: Two trips. One for the wrong screws, one for the right ones.\nSpeaker 2: Classic. Did you paint it?\nSpeaker 1: Not yet. The wood needs to dry out first, apparently.\nSpeaker 2: So the project continues.\nSpeaker 1: The project continues. But the door closes now, which is more than March could say.",
|
| 48 |
]
|
| 49 |
|
| 50 |
+
|
| 51 |
def load_model_with_head(load_soar):
|
| 52 |
model = VibeVoiceForConditionalGeneration.from_pretrained(
|
| 53 |
MODEL, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True,
|
|
|
|
| 55 |
if load_soar:
|
| 56 |
sd_path = hf_hub_download(SOAR_REPO, "soar_head_state.pt")
|
| 57 |
sd = torch.load(sd_path, map_location="cpu", weights_only=True)
|
| 58 |
+
model.model.prediction_head.load_state_dict(sd, strict=True)
|
| 59 |
print(f"loaded SOAR head: {len(sd)} tensors", flush=True)
|
| 60 |
return model
|
| 61 |
|
| 62 |
+
|
| 63 |
+
def gt_text(script):
|
| 64 |
+
return " ".join(line.split(": ", 1)[1].strip() for line in script.strip().splitlines())
|
| 65 |
+
|
| 66 |
+
|
| 67 |
def main():
|
| 68 |
api = HfApi()
|
| 69 |
scripts = ([("short", i, t) for i, t in enumerate(SHORTS)]
|
|
|
|
| 76 |
refs[spk] = wav.astype(np.float32)
|
| 77 |
|
| 78 |
proc = VibeVoiceProcessor.from_pretrained(MODEL)
|
|
|
|
| 79 |
from transformers import pipeline
|
| 80 |
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3",
|
| 81 |
+
device=device, torch_dtype=torch.float16)
|
| 82 |
+
encoder = VoiceEncoder(device)
|
|
|
|
| 83 |
|
| 84 |
+
ref_wav = np.concatenate([refs[1], np.zeros(SR // 4, dtype=np.float32), refs[2]])
|
| 85 |
+
emb_ref = encoder.embed_utterance(preprocess_wav(ref_wav, SR))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
results = []
|
| 88 |
os.makedirs("/out", exist_ok=True)
|
| 89 |
for arm in ARMS:
|
|
|
|
| 90 |
print(f"=== ARM {arm} ===", flush=True)
|
| 91 |
model = load_model_with_head(load_soar=(arm == "soar"))
|
|
|
|
| 92 |
model.set_ddpm_inference_steps(num_steps=10)
|
| 93 |
for tier, idx, script in scripts:
|
| 94 |
t0 = time.time()
|
|
|
|
| 99 |
cfg_scale=1.3,
|
| 100 |
generation_config={"do_sample": False},
|
| 101 |
)
|
|
|
|
|
|
|
|
|
|
| 102 |
gen_s = time.time() - t0
|
| 103 |
+
audio = out.speech_outputs[0]
|
| 104 |
wav = audio if isinstance(audio, np.ndarray) else audio.float().cpu().numpy()
|
| 105 |
+
wav = np.asarray(wav, dtype=np.float32)
|
| 106 |
audio_s = len(wav) / SR
|
| 107 |
rtf = gen_s / max(audio_s, 1e-6)
|
| 108 |
fn = f"{arm}_{tier}_{idx}.wav"
|
| 109 |
sf.write(f"/out/{fn}", wav, SR)
|
| 110 |
hyp = transcriber(f"/out/{fn}", generate_kwargs={"language": "english"})["text"].strip()
|
| 111 |
+
gt = gt_text(script)
|
| 112 |
+
wer = float(jiwer.wer(gt, hyp))
|
| 113 |
+
emb_gen = encoder.embed_utterance(preprocess_wav(f"/out/{fn}"))
|
| 114 |
+
sim = float(np.dot(emb_gen, emb_ref) / (np.linalg.norm(emb_gen) * np.linalg.norm(emb_ref)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
drift = None
|
| 116 |
if tier == "long" and audio_s > 60:
|
| 117 |
+
e1 = encoder.embed_utterance(preprocess_wav(wav[: 30 * SR], SR))
|
| 118 |
+
e2 = encoder.embed_utterance(preprocess_wav(wav[-30 * SR:], SR))
|
|
|
|
| 119 |
drift = float(np.dot(e1, e2) / (np.linalg.norm(e1) * np.linalg.norm(e2)))
|
| 120 |
results.append({"arm": arm, "tier": tier, "idx": idx, "audio_s": audio_s,
|
| 121 |
+
"gen_s": gen_s, "rtf": rtf, "wer": wer, "sim_ref": sim,
|
| 122 |
"drift": drift, "hyp": hyp})
|
| 123 |
+
d = "n/a" if drift is None else f"{drift:.3f}"
|
| 124 |
+
print(f"{arm} {tier}{idx}: rtf {rtf:.3f} wer {wer:.3f} sim {sim:.3f} drift {d}", flush=True)
|
| 125 |
+
trackio.log({"rtf": rtf, "wer": wer, "sim": sim}, step=len(results))
|
| 126 |
api.upload_file(path_or_fileobj=f"/out/{fn}", path_in_repo=f"{arm}/{fn}",
|
| 127 |
repo_id=OUT_REPO, repo_type="model")
|
| 128 |
torch.cuda.empty_cache()
|
|
|
|
| 131 |
|
| 132 |
with open("/out/results.json", "w") as f:
|
| 133 |
json.dump(results, f, indent=2)
|
|
|
|
| 134 |
summary = {}
|
| 135 |
for arm in ARMS:
|
| 136 |
rows = [r for r in results if r["arm"] == arm]
|
|
|
|
| 147 |
repo_id=OUT_REPO, repo_type="model")
|
| 148 |
print("EVAL DONE", flush=True)
|
| 149 |
|
| 150 |
+
|
| 151 |
+
import trackio
|
| 152 |
+
trackio.init(project="vibevoice-soar-head", space_id="ACloudCenter/vibevoice-soar-head-trackio")
|
| 153 |
+
main()
|
| 154 |
+
trackio.finish()
|