File size: 2,229 Bytes
cb7d6f0
 
 
 
 
 
 
 
 
 
 
 
 
 
2003020
cb7d6f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2003020
 
 
 
 
 
 
 
 
cb7d6f0
 
 
 
 
 
 
 
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
"""Kaggle notebook body: build the director's training set, fine-tune, re-score.

Three subprocesses, same upgraded environment:

1. collect - sample the base model, keep every script that already satisfies the
   TTS contract (acceptance is about 60% with the line format, so 160 scripts
   costs roughly 35 minutes).
2. train   - fine-tune the last six blocks and the head on those 160.
3. eval    - score the tuned model on the same topic list as the baseline.

Everything runs out-of-process because transformers has to be upgraded to 5.x
and this notebook already imported huggingface_hub to fetch the bundle.
"""
import os
import shutil
import subprocess
import sys

BUNDLE = "/kaggle/working/webfm-eval"
ENV = dict(os.environ, WORK_ROOT="/kaggle/working")


def sh(cmd: str) -> None:
    print(f"$ {cmd}", flush=True)
    if subprocess.run(cmd, shell=True).returncode != 0:
        raise SystemExit(f"failed: {cmd}")


def run(script: str, **env) -> int:
    target = os.path.join(BUNDLE, "kaggle", script)
    print(f"\n===== {script} {env} =====", flush=True)
    rc = subprocess.run([sys.executable, target], env=dict(ENV, **env)).returncode
    print(f"----- {script} rc={rc} -----", flush=True)
    return rc


def main() -> None:
    sh("pip install -q --upgrade 'transformers>=5' 'huggingface_hub>=1.31,<2' accelerate")

    if not os.path.exists(os.path.join(BUNDLE, "kaggle", "director_probe.py")):
        raise SystemExit("director_probe.py missing")

    # 160 scripts collected on the first attempt (68% acceptance); reuse them
    # instead of paying 25 minutes of sampling again.
    seed = os.path.join(BUNDLE, "director", "sft.jsonl")
    if os.path.exists(seed):
        os.makedirs("/kaggle/working/director", exist_ok=True)
        shutil.copyfile(seed, "/kaggle/working/director/sft.jsonl")
        print("reusing collected scripts", flush=True)
    else:
        run("director_probe.py", MODE="collect", TARGET="160", MAX_TRIES="700")
    if run("director_sft.py", EPOCHS="4", TRAIN_LAYERS="6") != 0:
        raise SystemExit("training failed")
    run("director_probe.py", MODE="eval", N_SAMPLES="12",
        DIRECTOR_MODEL="/kaggle/working/director/model")


if __name__ == "__main__":
    main()