eval in two passes to fit a 15GB T4
Browse files- kaggle/train_voices.py +45 -15
kaggle/train_voices.py
CHANGED
|
@@ -357,8 +357,13 @@ def stage_eval() -> None:
|
|
| 357 |
|
| 358 |
sents = json.loads((BUNDLE / "eval/sentences.json").read_text())["items"]
|
| 359 |
want = EVAL_VOICES or [v["slot"] for v in voices()[:2]]
|
|
|
|
|
|
|
|
|
|
| 360 |
rows = []
|
| 361 |
-
|
|
|
|
|
|
|
| 362 |
latent, spk = tts.prepare_prompt(str(BUNDLE / v["wav"]), use_spk_emb=True)
|
| 363 |
latent = latent.to(device=device, dtype=next(mm.parameters()).dtype)
|
| 364 |
spk = [x.to(device=device, dtype=next(mm.parameters()).dtype) for x in spk]
|
|
@@ -370,28 +375,53 @@ def stage_eval() -> None:
|
|
| 370 |
prompt_text=v["prompt_text"], use_spk_emb=True,
|
| 371 |
raw_text=True, max_decode_steps=max(32, min(400, int(len(text) * 3.5))))
|
| 372 |
tw = tw.cpu().numpy().reshape(-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
frames = max(16, int(round(tw.size / 3528)))
|
| 374 |
torch.manual_seed(SEED)
|
| 375 |
sw = ar_generate(mm, student, STEPS, text, task_prompt, latent,
|
| 376 |
v["prompt_text"], spk, frames)
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
"cer_head": s["cer_prefix"], "hyp": hyp})
|
| 383 |
except Exception:
|
| 384 |
-
log(f"
|
| 385 |
-
|
|
|
|
| 386 |
|
| 387 |
(OUT / "eval_rows.json").write_text(json.dumps(rows, ensure_ascii=False, indent=1))
|
| 388 |
for name in ("teacher", "student"):
|
| 389 |
-
|
| 390 |
-
if
|
| 391 |
-
log(f"{name:<8} n={len(
|
| 392 |
-
f"head={np.mean([r['cer_head'] for r in
|
| 393 |
-
for v in sorted({r["voice"] for r in
|
| 394 |
-
vs = [r["cer"] for r in
|
| 395 |
log(f" {v} n={len(vs)} cer={np.mean(vs):.4f}")
|
| 396 |
|
| 397 |
|
|
|
|
| 357 |
|
| 358 |
sents = json.loads((BUNDLE / "eval/sentences.json").read_text())["items"]
|
| 359 |
want = EVAL_VOICES or [v["slot"] for v in voices()[:2]]
|
| 360 |
+
sel = [x for x in voices() if x["slot"] in want]
|
| 361 |
+
wav_dir = OUT / "eval_wavs"
|
| 362 |
+
wav_dir.mkdir(parents=True, exist_ok=True)
|
| 363 |
rows = []
|
| 364 |
+
|
| 365 |
+
# Pass 1: teacher only. Both DiTs resident does not fit on a 15 GB T4.
|
| 366 |
+
for v in sel:
|
| 367 |
latent, spk = tts.prepare_prompt(str(BUNDLE / v["wav"]), use_spk_emb=True)
|
| 368 |
latent = latent.to(device=device, dtype=next(mm.parameters()).dtype)
|
| 369 |
spk = [x.to(device=device, dtype=next(mm.parameters()).dtype) for x in spk]
|
|
|
|
| 375 |
prompt_text=v["prompt_text"], use_spk_emb=True,
|
| 376 |
raw_text=True, max_decode_steps=max(32, min(400, int(len(text) * 3.5))))
|
| 377 |
tw = tw.cpu().numpy().reshape(-1)
|
| 378 |
+
p = wav_dir / f"{v['slot']}_{item['id']}_teacher.wav"
|
| 379 |
+
torch.save(torch.from_numpy(tw), p.with_suffix(".pt"))
|
| 380 |
+
hyp = asr(tw)
|
| 381 |
+
s = score_row({"text": text, "hyp_text": hyp}, table)
|
| 382 |
+
rows.append({"voice": v["slot"], "artist": v["artist"], "id": item["id"],
|
| 383 |
+
"model": "teacher", "text": text, "cer": s["cer_norm"],
|
| 384 |
+
"cer_head": s["cer_prefix"], "hyp": hyp})
|
| 385 |
+
except Exception:
|
| 386 |
+
log(f"teacher {v['slot']} {item['id']} failed\n{traceback.format_exc()}")
|
| 387 |
+
torch.cuda.empty_cache()
|
| 388 |
+
log(f"teacher {v['slot']} done ({len(rows)} rows)")
|
| 389 |
+
|
| 390 |
+
del teacher
|
| 391 |
+
torch.cuda.empty_cache()
|
| 392 |
+
|
| 393 |
+
# Pass 2: student only, reusing the teacher audio for frame count.
|
| 394 |
+
for v in sel:
|
| 395 |
+
latent, spk = tts.prepare_prompt(str(BUNDLE / v["wav"]), use_spk_emb=True)
|
| 396 |
+
latent = latent.to(device=device, dtype=next(mm.parameters()).dtype)
|
| 397 |
+
spk = [x.to(device=device, dtype=next(mm.parameters()).dtype) for x in spk]
|
| 398 |
+
for item in sents:
|
| 399 |
+
text = item["text"]
|
| 400 |
+
try:
|
| 401 |
+
tp = wav_dir / f"{v['slot']}_{item['id']}_teacher.pt"
|
| 402 |
+
tw = torch.load(tp, map_location="cpu").numpy()
|
| 403 |
frames = max(16, int(round(tw.size / 3528)))
|
| 404 |
torch.manual_seed(SEED)
|
| 405 |
sw = ar_generate(mm, student, STEPS, text, task_prompt, latent,
|
| 406 |
v["prompt_text"], spk, frames)
|
| 407 |
+
hyp = asr(sw)
|
| 408 |
+
s = score_row({"text": text, "hyp_text": hyp}, table)
|
| 409 |
+
rows.append({"voice": v["slot"], "artist": v["artist"], "id": item["id"],
|
| 410 |
+
"model": "student", "text": text, "cer": s["cer_norm"],
|
| 411 |
+
"cer_head": s["cer_prefix"], "hyp": hyp})
|
|
|
|
| 412 |
except Exception:
|
| 413 |
+
log(f"student {v['slot']} {item['id']} failed\n{traceback.format_exc()}")
|
| 414 |
+
torch.cuda.empty_cache()
|
| 415 |
+
log(f"student {v['slot']} done ({len(rows)} rows)")
|
| 416 |
|
| 417 |
(OUT / "eval_rows.json").write_text(json.dumps(rows, ensure_ascii=False, indent=1))
|
| 418 |
for name in ("teacher", "student"):
|
| 419 |
+
sel_rows = [r for r in rows if r["model"] == name]
|
| 420 |
+
if sel_rows:
|
| 421 |
+
log(f"{name:<8} n={len(sel_rows)} cer={np.mean([r['cer'] for r in sel_rows]):.4f} "
|
| 422 |
+
f"head={np.mean([r['cer_head'] for r in sel_rows]):.4f}")
|
| 423 |
+
for v in sorted({r["voice"] for r in sel_rows}):
|
| 424 |
+
vs = [r["cer"] for r in sel_rows if r["voice"] == v]
|
| 425 |
log(f" {v} n={len(vs)} cer={np.mean(vs):.4f}")
|
| 426 |
|
| 427 |
|