File size: 9,499 Bytes
b80a86d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd0620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b80a86d
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd0620
 
 
 
 
b80a86d
5fd0620
 
b80a86d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd0620
 
 
 
b80a86d
 
 
 
 
 
 
 
5fd0620
b80a86d
 
 
5fd0620
b80a86d
 
5fd0620
b80a86d
 
 
 
 
 
 
 
 
5fd0620
 
b80a86d
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""Hidden states of the target along token sequences, plus the checks the training relies on.

1. Dev references (the kit's target-only outputs): features for the hold-out evaluation,
   teacher-forced agreement of the reference implementation with the engine's greedy choices,
   and the offline simulator replaying the *baseline* head - its acceptance must match what the
   kit measured, otherwise the simulator (and the training objective) is not trustworthy.
2. Generated training sequences (emcdraft.gen_vllm): feature shards for training, under a
   wall-clock budget, domains interleaved so a cut-off keeps the mix.

    python -m emcdraft.extract --target DIR --kit DIR --baseline DIR --gen data/gen --out data --results results
"""
from __future__ import annotations

import argparse
import json
import os
import random
import time
from collections import defaultdict
from pathlib import Path

import torch
import torch.nn.functional as F

from .head import HeadConfig, build_head, load_mtp_state
from .sim import DOMAINS, argmax_logits, save_shard, simulate
from .target import PAD_ID, Target


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


def make_batches(seqs, max_tokens):
    order = sorted(range(len(seqs)), key=lambda i: len(seqs[i]["ids"]))
    out, cur = [], []
    for i in order:
        if cur and (len(cur) + 1) * len(seqs[i]["ids"]) > max_tokens:
            out.append(cur)
            cur = []
        cur.append(i)
    if cur:
        out.append(cur)
    return out


def featurize(tgt, seqs, max_tokens, agree):
    """Teacher-forced hidden states for every sequence (kept on the GPU)."""
    feats = []
    queue = make_batches(seqs, max_tokens)
    while queue:
        b = queue.pop(0)
        L = max(len(seqs[i]["ids"]) for i in b)
        x = torch.full((len(b), L), PAD_ID, dtype=torch.long, device=tgt.device)
        for r, i in enumerate(b):
            x[r, :len(seqs[i]["ids"])] = torch.tensor(seqs[i]["ids"], device=tgt.device)
        try:
            h = tgt.hidden(x)
        except torch.OutOfMemoryError:
            torch.cuda.empty_cache()
            if len(b) == 1:
                raise
            log(f"  out of memory on {len(b)} x {L}; splitting the batch")
            queue[:0] = [b[: len(b) // 2], b[len(b) // 2:]]
            continue
        for r, i in enumerate(b):
            s = seqs[i]
            n, P = len(s["ids"]), s["P"]
            hid = h[r, : n - 1].clone()
            pred = argmax_logits(hid[P - 1:], tgt.W)
            agree[s["domain"]][0] += int((pred == x[r, P:n]).sum())
            agree[s["domain"]][1] += n - P
            feats.append({"tokens": x[r, :n].clone(), "hidden": hid, "P": P, "T": n,
                          "domain": s["domain"], "id": s.get("id", "")})
        del h, x
    return feats


def shard_part(name: str) -> int:
    """Training shards of the first extraction are g0000...; later appended parts p<k>g0000..."""
    return int(name[1:name.index("g")]) if name.startswith("p") else 1


def plan_train(out: Path, gen: Path, append: bool):
    """Which generated files to featurize and how to name the new shards.

    Returns (files, shard prefix, consumed file names, number of kept shards). Without append
    every earlier shard is deleted. With append the shards are kept and only files not listed in
    <out>/consumed_gen.json are featurized (a first extraction without that list consumed the
    plain <domain>.jsonl files)."""
    train = out / "train"
    train.mkdir(parents=True, exist_ok=True)
    kept = sorted(train.glob("*.json"))
    if not append or not kept:
        for p in train.glob("*"):
            p.unlink()
        kept, consumed, prefix = [], set(), "g"
    else:
        listed = out / "consumed_gen.json"
        consumed = (set(json.loads(listed.read_text())) if listed.exists()
                    else {f"{d}.jsonl" for d in DOMAINS})
        prefix = f"p{max(shard_part(p.stem) for p in kept) + 1}g"
    files = [p for p in sorted(Path(gen).glob("*.jsonl"))
             if p.name not in consumed and p.name.split(".")[0].split("-")[0] in DOMAINS]
    return files, prefix, consumed, len(kept)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--target", required=True)
    ap.add_argument("--kit", required=True)
    ap.add_argument("--baseline", required=True)
    ap.add_argument("--gen", required=True)
    ap.add_argument("--out", required=True)
    ap.add_argument("--results", required=True)
    ap.add_argument("--batch-tokens", type=int, default=int(os.environ.get("EXTRACT_BATCH_TOKENS", 16384)))
    ap.add_argument("--minutes", type=float, default=float(os.environ.get("EXTRACT_MINUTES", 22)))
    ap.add_argument("--group", type=int, default=256, help="sequences per training shard")
    ap.add_argument("--seed", type=int, default=0)
    a = ap.parse_args()
    out, res = Path(a.out), Path(a.results)
    (out / "dev").mkdir(parents=True, exist_ok=True)
    for p in (out / "dev").glob("*"):
        p.unlink()
    # EXTRACT_APPEND=1: keep the training shards of earlier extractions, featurize new files only
    files, prefix, consumed, kept = plan_train(out, Path(a.gen), os.environ.get("EXTRACT_APPEND", "0") == "1")
    res.mkdir(parents=True, exist_ok=True)
    report = {"kept_train_shards": kept, "files": [p.name for p in files]}
    log(f"training files to featurize: {report['files']} ({kept} earlier shards kept)")

    t0 = time.time()
    tgt = Target(a.target)
    report["load_seconds"] = round(time.time() - t0, 1)
    report["hidden_state_call"] = tgt.mode
    log(f"target loaded in {report['load_seconds']}s (hidden states via '{tgt.mode}')")

    # 1. dev references
    kit = Path(a.kit) / "data/dev"
    ref = {r["id"]: r["token_ids"] for r in map(json.loads, open(kit / "reference.jsonl")) if r}
    dev = [json.loads(line) for line in open(kit / "prompts.jsonl") if line.strip()]
    seqs = [{"ids": p["prompt_token_ids"] + ref[p["id"]], "P": len(p["prompt_token_ids"]),
             "domain": p["domain"], "id": p["id"]} for p in dev]
    agree = defaultdict(lambda: [0, 0])
    t0 = time.time()
    feats = featurize(tgt, seqs, a.batch_tokens, agree)
    for d in DOMAINS:
        save_shard(str(out / "dev" / d), [f for f in feats if f["domain"] == d])
    report["dev_teacher_forced_agreement"] = {d: round(v[0] / max(1, v[1]), 4) for d, v in agree.items()}
    log(f"dev features: {len(feats)} sequences in {time.time() - t0:.0f}s; teacher-forced agreement with "
        f"the engine's greedy outputs {report['dev_teacher_forced_agreement']}")

    cfg = HeadConfig.from_target(json.loads((Path(a.target) / "config.json").read_text()))
    head = build_head(load_mtp_state(a.baseline), cfg, tgt.device)
    E = tgt.E
    sim = simulate(head, lambda i: F.embedding(i, E), tgt.W, feats)
    base = json.loads((kit / "baseline.json").read_text())
    report["sim_baseline_dev"] = sim
    report["kit_baseline_dev"] = {k: base[k] for k in ("acceptance_length", "acceptance_by_domain")}
    log(f"simulated baseline acceptance {sim['acceptance_length']:.3f} vs kit-measured "
        f"{base['acceptance_length']:.3f}; per domain "
        + ", ".join(f"{d} {sim['acceptance_by_domain'][d]:.3f}/{base['acceptance_by_domain'][d]:.3f}" for d in DOMAINS)
        + f"; step accuracies {[round(x, 3) for x in sim['step_accuracy']]}")
    (res / "extract_checks.json").write_text(json.dumps(report, indent=2))
    gap = abs(sim["acceptance_length"] - base["acceptance_length"])
    if gap > 0.25:
        raise SystemExit(f"the simulator disagrees with the kit by {gap:.3f}: refusing to train on it")
    del head, feats
    torch.cuda.empty_cache()

    # 2. generated training sequences
    rng = random.Random(a.seed)
    gen = []
    for p in files:
        d = p.name.split(".")[0].split("-")[0]
        for line in open(p):
            if line.strip():
                r = json.loads(line)
                if len(r["o"]) >= 8:
                    gen.append({"ids": r["p"] + r["o"], "P": len(r["p"]), "domain": d})
    rng.shuffle(gen)
    log(f"{len(gen)} generated sequences to featurize")
    agree = defaultdict(lambda: [0, 0])
    done = defaultdict(lambda: [0, 0])
    t0 = time.time()
    complete = True
    for g in range(0, len(gen), a.group):
        if time.time() - t0 > a.minutes * 60:
            log("time budget reached")
            complete = False
            break
        feats = featurize(tgt, gen[g:g + a.group], a.batch_tokens, agree)
        save_shard(str(out / "train" / f"{prefix}{g // a.group:04d}"), feats)
        for f in feats:
            done[f["domain"]][0] += 1
            done[f["domain"]][1] += f["T"] - f["P"]
        del feats
        n = sum(v[1] for v in done.values())
        log(f"  group {g // a.group}: {n} generated tokens featurized, {n / (time.time() - t0):.0f} tok/s")
    report["train_sequences"] = {d: {"sequences": v[0], "generated_tokens": v[1]} for d, v in done.items()}
    report["train_teacher_forced_agreement"] = {d: round(v[0] / max(1, v[1]), 4) for d, v in agree.items()}
    report["extract_seconds"] = round(time.time() - t0, 1)
    if complete:      # a file cut off by the time budget is not marked, so it is not skipped later
        (out / "consumed_gen.json").write_text(json.dumps(sorted(consumed | {p.name for p in files})))
    (res / "extract_checks.json").write_text(json.dumps(report, indent=2))
    log(f"done: {report['train_sequences']}")


if __name__ == "__main__":
    main()