File size: 2,881 Bytes
b177b83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Kaggle notebook body: distil the flow head on the seven mined voices.

Order matters. A smoke pass (5 texts, 5 optimizer steps) validates the
trajectory and training code paths before spending an hour generating 840
trajectories; the earlier failures in this project each cost a full session.

  smoke   traj x5 on one GPU, train x5 steps          ~4 min
  traj    7 voices x 120 texts, sharded over 2 GPUs   ~70 min
  train   4-step student on all shards                ~25 min
  export  ONNX fp32 + int4                            ~5 min
  eval    40 sentences x 2 voices x {teacher, stu}    ~25 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", "a07", "a08"]]
EVAL_VOICES = "a08,a01"


def run(tag: str, env_extra: dict, gpu: int = 0) -> int:
    env = dict(os.environ, WORK_ROOT=str(ROOT), **env_extra)
    if gpu is not None:
        env["CUDA_VISIBLE_DEVICES"] = str(gpu)
    log = open(ROOT / f"train_{tag}.log", "w")
    p = subprocess.Popen([sys.executable, str(BUNDLE / "kaggle/train_voices.py")],
                         env=env, stdout=log, stderr=subprocess.STDOUT, cwd=str(ROOT))
    return p.wait()


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

    print("== smoke: traj x5, train x5 ==", flush=True)
    rc = run("smoke_traj", {"STAGE": "traj", "VOICES": "a08", "SMOKE": "1", "N_TEXTS": "5"})
    print("smoke traj rc", rc)
    rc = run("smoke_train", {"STAGE": "train", "TRAIN_STEPS": "5", "BATCH": "4"})
    print("smoke train rc", rc)
    if rc != 0:
        print((ROOT / "train_smoke_train.log").read_text()[-3000:])
        raise SystemExit("training path failed in smoke")

    print("== full trajectories ==", flush=True)
    procs = []
    for gpu, shard in enumerate(SHARDS):
        env = dict(os.environ, WORK_ROOT=str(ROOT), STAGE="traj",
                   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])

    print("== train + export + eval ==", flush=True)
    rc = run("final", {"STAGE": "all", "TRAIN_STEPS": "1500", "BATCH": "16",
                       "EVAL_VOICES": EVAL_VOICES}, gpu=0)
    print("final rc", rc)


if __name__ == "__main__":
    main()