File size: 3,305 Bytes
b42b672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Kaggle notebook body: hold out one voice, then measure generalisation.

The previous student fitted the seven voices it was trained on to within 1.19 pp
of the teacher. That is in-distribution. The case that matters for user
uploads is a voice the student has never heard, so this run trains on six and
evaluates the seventh.

a07 (伊芙琳) is held out. a08 stays in training as the in-distribution control,
so the two columns separate "fitted" from "generalised".

Also measures the int4 quantisation of the same student, since that decides
which bundle ships.

  traj       6 voices x 120 texts, 2 GPUs     ~55 min
  train      4-step student                   ~25 min
  export     ONNX fp32 + int4                 ~5 min
  eval       a07 + a08, teacher vs student    ~15 min
  eval_int4  same cells through the int4 graph ~12 min
"""
import os
import subprocess
import sys
import time

from notebook_eval import BUNDLE, MODEL, ROOT, WEIGHTS, pull, stage_deps, stage_repo

BUNDLE_REPO = "idefinity/webfm-eval"
SHARDS = [["a01", "a03", "a04"], ["a05", "a06", "a08"]]
HOLDOUT = "a07"
EVAL_VOICES = "a07,a08"


def run(tag: str, extra: dict, gpu: int = 0) -> int:
    env = dict(os.environ, WORK_ROOT=str(ROOT), HOLDOUT=HOLDOUT, **extra)
    env["CUDA_VISIBLE_DEVICES"] = str(gpu)
    env["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
    log = open(ROOT / f"holdout_{tag}.log", "w")
    p = subprocess.Popen([sys.executable, str(BUNDLE / "kaggle/train_voices.py")],
                         env=env, stdout=log, stderr=subprocess.STDOUT, cwd=str(ROOT))
    rc = p.wait()
    print(f"[{tag}] rc={rc}", flush=True)
    if rc != 0:
        print((ROOT / f"holdout_{tag}.log").read_text()[-2500:])
    return rc


def main() -> None:
    stage_repo()
    stage_deps()
    pull(WEIGHTS, MODEL)
    pull(BUNDLE_REPO, BUNDLE, repo_type="dataset")

    # Guard: confirm the holdout filter works before spending an hour.
    rc = run("smoke", {"STAGE": "traj", "VOICES": "a08", "SMOKE": "1", "N_TEXTS": "2"})
    if rc != 0:
        raise SystemExit("traj path broken")
    if (ROOT / "train/traj/a07").exists() and any((ROOT / "train/traj/a07").iterdir()):
        raise SystemExit("holdout voice leaked into trajectories")

    print("== trajectories (6 voices) ==", flush=True)
    procs = []
    for gpu, shard in enumerate(SHARDS):
        env = dict(os.environ, WORK_ROOT=str(ROOT), STAGE="traj", HOLDOUT=HOLDOUT,
                   VOICES=",".join(shard), CUDA_VISIBLE_DEVICES=str(gpu))
        log = open(ROOT / f"traj_gpu{gpu}.log", "w")
        procs.append(subprocess.Popen([sys.executable, str(BUNDLE / "kaggle/train_voices.py")],
                                      env=env, stdout=log, stderr=subprocess.STDOUT, cwd=str(ROOT)))
    while any(p.poll() is None for p in procs):
        time.sleep(60)
        n = len(list((ROOT / "train/traj").glob("*/*.pt")))
        print(f"[{time.strftime('%H:%M')}] trajectories={n}", flush=True)
    print("traj exits:", [p.returncode for p in procs])

    if run("final", {"STAGE": "all", "TRAIN_STEPS": "1500", "BATCH": "16",
                     "EVAL_VOICES": EVAL_VOICES}) != 0:
        raise SystemExit("train/export/eval failed")

    run("int4", {"STAGE": "eval_int4", "EVAL_VOICES": EVAL_VOICES})


if __name__ == "__main__":
    main()