File size: 4,537 Bytes
a64b4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ec358a
 
a64b4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ec358a
 
a64b4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
"""Short training runs that compare training recipes before the full run.

Every run starts from the same weights, sees the same data in the same order for the same
number of steps, and is scored by the offline simulator on the held-out validation set
(through FP8 rounding when --qat is given). The recipe with the highest validation
acceptance length is written to <state>/train_args for the full training run.

Recipes (each adds one published idea to the one before it):
  control         the recipe of the previous run (fixed step weights 1 / 0.7 / 0.5,
                  full-distribution distillation)
  accw_topk       acceptance-aware step weights - each step weighted by the derivative of
                  the acceptance length with respect to its accuracy, which favours early
                  steps (cf. Gumiho; FastMTP's decaying step weights) - plus distillation of
                  the target's top-10 tokens only (HASS, harmonized objective distillation)
  accw_topk_mask  + later steps trained almost only where the engine reaches them
                  (token-alignable training, GRIFFIN): floor 0.3 -> 0.1
  accw_topk_lr2   accw_topk with twice the learning rate
  accw_topk_lora  accw_topk, but only rank-128 updates of every matrix are learned (LoRA, Hu et
                  al. 2021) and merged before export - a low-rank constraint on what changes

    python -m emcdraft.ablate --target DIR --init DIR --data DIR --out runs/ablate --results results --state state
"""
from __future__ import annotations

import argparse
import json
import subprocess
import sys
import time
from pathlib import Path

RECIPES = [
    ("control", "previous recipe", ["--step-w", "1", "0.7", "0.5", "--topk", "0", "--floor", "0.3", "--rel-lr", "1e-3"]),
    ("accw_topk", "acceptance-aware step weights + top-10 distillation",
     ["--step-w", "auto", "--topk", "10", "--floor", "0.3", "--rel-lr", "1e-3"]),
    ("accw_topk_mask", "accw_topk + stricter masking of unreachable later steps",
     ["--step-w", "auto", "--topk", "10", "--floor", "0.1", "--rel-lr", "1e-3"]),
    ("accw_topk_lr2", "accw_topk + twice the learning rate",
     ["--step-w", "auto", "--topk", "10", "--floor", "0.3", "--rel-lr", "2e-3"]),
    ("accw_topk_lora", "accw_topk with rank-128 LoRA adapters instead of full fine-tuning",
     ["--step-w", "auto", "--topk", "10", "--floor", "0.3", "--rel-lr", "1e-3", "--lora", "128"]),
]


def log(msg):
    print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)


def main():
    ap = argparse.ArgumentParser()
    for k in ("--target", "--init", "--data", "--out", "--results", "--state"):
        ap.add_argument(k, required=True)
    ap.add_argument("--dev", default=None)
    ap.add_argument("--qat", action="store_true")
    ap.add_argument("--steps", type=int, default=300)
    a = ap.parse_args()
    out = Path(a.out)
    report = {"steps": a.steps, "recipes": {}}
    for name, desc, flags in RECIPES:
        run = out / name
        summary = run / "train_summary.json"
        if not summary.exists():          # finished runs are reused after an interruption
            cmd = [sys.executable, "-m", "emcdraft.train", "--target", a.target, "--init", a.init,
                   "--data", a.data, "--out", str(run), "--max-steps", str(a.steps), "--eval-every", "100",
                   "--no-export", "--quick", "--max-minutes", "600", *flags]
            if a.dev:
                cmd += ["--dev", a.dev]
            if a.qat:
                cmd.append("--qat")
            log(f"recipe {name}: {desc}")
            subprocess.run(cmd, check=True)
        s = json.loads(summary.read_text())
        start = s["sim"]["fp8_untrained" if a.qat else "init"]["val"]["acceptance_length"]
        report["recipes"][name] = {"description": desc, "flags": flags, "val_tau_start": start,
                                   "val_tau_best": s["best_val_tau"],
                                   "history": s["history"], "seconds": s.get("total_seconds")}
        log(f"recipe {name}: validation tau {start:.4f} -> {s['best_val_tau']:.4f}")
    best = max(report["recipes"], key=lambda n: report["recipes"][n]["val_tau_best"])
    report["chosen"] = best
    Path(a.results).mkdir(parents=True, exist_ok=True)
    (Path(a.results) / "ablation.json").write_text(json.dumps(report, indent=2))
    (Path(a.state) / "train_args").write_text(" ".join(report["recipes"][best]["flags"]) + "\n")
    log(f"chosen recipe: {best} ({report['recipes'][best]['description']})")


if __name__ == "__main__":
    main()