File size: 8,955 Bytes
9d4a7db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""H4: train the per-layer dial hypernetwork.

Teacher: router v2's choice for each pool question, CROSS-FITTED (5 folds, so no question is labelled by a router that
saw it), lambda 0.01. Target text: the response from the chosen setting if it is correct and not capped; otherwise
the shortest correct response from any setting; questions with none are skipped. Targets over 4,096 tokens are
skipped (memory).
Hypernetwork: question vector (Instruct layer 18, pooled) -> standardise -> PCA-64 -> MLP 64-128-37 -> sigmoid = beta
(36 layers + embedding/head). Starts near beta 0.12 everywhere (close to D).
Loss: next-token loss of the target under the composed model with beta(x), + MU * mean(beta) (deliberation costs).
Only the hypernetwork trains; both 4B models stay frozen. Writes hyper/h4/hypernet.pt, betas.pt (beta for every pool
and test question), train_log.jsonl.
Usage: h4_train.py [--epochs 3] [--mu 0.02] [--limit N]
"""
import json
import math
import os
import random
import sys
import time

import torch
import torch.nn.functional as F
from torch.utils.checkpoint import checkpoint
from transformers import AutoTokenizer

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import h3_train_eval as v1  # noqa: E402
import h3b_value_router as v2  # noqa: E402
from h4_compose import Composed, T_DIR  # noqa: E402

ROOT = v1.ROOT
# H4_OUT and H4_POOL_TEXT redirect output and teacher data (the 32k-budget retrain, "H5", sets both); unset = H4.
OUT = os.environ.get("H4_OUT", f"{ROOT}/hyper/h4")
POOL_TEXT = os.environ.get("H4_POOL_TEXT", f"{ROOT}/hyper/pool_text")
SETS = ["dial0", "dial75", "dial100"]


def arg(n, d):
    return type(d)(sys.argv[sys.argv.index(n) + 1]) if n in sys.argv else d


def teacher_choices(emb, pids, pool):
    idx = {i: n for n, i in enumerate(emb["ids"])}
    X = emb["x"][[idx[i] for i in pids]]
    choice = {}
    perm = torch.randperm(len(pids), generator=torch.Generator().manual_seed(1))
    for f in range(5):
        test = perm[f::5]
        tset = set(test.tolist())
        tr = torch.tensor([j for j in range(len(pids)) if j not in tset])
        mu, sd = X[tr].mean(0), X[tr].std(0).clamp(min=1e-6)
        Z = (X - mu) / sd
        _, _, V = torch.pca_lowrank(Z[tr], q=64, center=False)
        Zp = Z @ V[:, :64]
        tok = torch.tensor([sum((pool[pids[j]][k]["tokens"] or 0) for j in tr.tolist()) / len(tr) for k in range(3)])
        P = []
        for k in range(3):
            y = torch.tensor([float(pool[pids[j]][k]["ok"]) for j in tr.tolist()])
            w, b = v2.fit_logistic(Zp[tr], y, 0.01)
            P.append(torch.sigmoid(Zp[test] @ w + b))
        util = torch.stack(P, 1) - 0.01 * tok / 1000
        for j, k in zip(test.tolist(), util.argmax(1).tolist()):
            choice[pids[j]] = k
    return choice


class HyperNet(torch.nn.Module):
    def __init__(self, mu, sd, proj, n_out=37):
        super().__init__()
        self.register_buffer("mu", mu)
        self.register_buffer("sd", sd)
        self.register_buffer("proj", proj)
        self.net = torch.nn.Sequential(torch.nn.Linear(proj.shape[1], 128), torch.nn.GELU(), torch.nn.Linear(128, n_out))
        torch.nn.init.zeros_(self.net[2].weight)
        torch.nn.init.constant_(self.net[2].bias, -2.0)  # beta ~ 0.12 at the start

    def forward(self, x):
        return torch.sigmoid(self.net(((x - self.mu) / self.sd) @ self.proj))


def main():
    epochs, mu_cost, limit = arg("--epochs", 3), arg("--mu", 0.02), arg("--limit", 0)
    maxlen = arg("--maxlen", 4096)  # H5: 24,576 so long Thinking answers (maths at 32k) can be targets
    os.makedirs(OUT, exist_ok=True)
    dev = torch.device("cuda:0")
    emb = torch.load(f"{ROOT}/hyper/embed.pt", weights_only=True)
    idx = {i: n for n, i in enumerate(emb["ids"])}
    texts = {}
    for k, s in enumerate(SETS):
        for r in map(json.loads, open(f"{POOL_TEXT}/{s}.jsonl")):
            texts.setdefault(r["id"], {})[k] = r
    pool = {i: v for i, v in texts.items() if len(v) == 3}
    pids = sorted(pool)
    choice = teacher_choices(emb, pids, pool)
    tok = AutoTokenizer.from_pretrained(T_DIR)
    data = []
    for i in pids:
        c = pool[i][choice[i]]
        if not (c["ok"] and not c["truncated"]):
            good = [pool[i][k] for k in range(3) if pool[i][k]["ok"] and not pool[i][k]["truncated"]]
            if not good:
                continue
            c = min(good, key=lambda r: r["tokens"] or 0)
        p_ids = tok(c["prompt"], add_special_tokens=False)["input_ids"]
        t_ids = tok(c["text"] + "<|im_end|>", add_special_tokens=False)["input_ids"]
        if len(p_ids) + len(t_ids) > maxlen:
            continue
        data.append(dict(id=i, x=emb["x"][idx[i]], ids=torch.tensor(p_ids + t_ids), start=len(p_ids),
                         teacher=choice[i], src=SETS.index(next(s for s in SETS if pool[i][SETS.index(s)] is c))))
    random.Random(3).shuffle(data)
    if limit:
        data = data[:limit]
    n_val = max(4, len(data) // 10)
    val, train = data[:n_val], data[n_val:]
    print(f"targets: {len(data)} (train {len(train)}, val {len(val)}); teacher choices "
          f"{ {s: sum(1 for d in data if d['teacher'] == k) for k, s in enumerate(SETS)} }; target sources "
          f"{ {s: sum(1 for d in data if d['src'] == k) for k, s in enumerate(SETS)} }", flush=True)

    Xp = torch.stack([d["x"] for d in train])
    mu, sd = Xp.mean(0), Xp.std(0).clamp(min=1e-6)
    _, _, V = torch.pca_lowrank((Xp - mu) / sd, q=64, center=False)
    hyper = HyperNet(mu, sd, V[:, :64]).to(dev)
    opt = torch.optim.AdamW(hyper.parameters(), lr=1e-3, weight_decay=1e-2)
    comp = Composed(dev)
    print(comp.summary, flush=True)
    lm_head = comp.model.lm_head

    def chunk_loss(h, y):
        return F.cross_entropy(lm_head(h).float(), y, reduction="sum")

    def sample_loss(d):
        beta = hyper(d["x"].to(dev).unsqueeze(0))[0]
        comp.set_beta(beta)
        ids = d["ids"].to(dev).unsqueeze(0)
        h = comp.model.model(input_ids=ids).last_hidden_state[0]
        y, hs = ids[0, d["start"]:], h[d["start"] - 1:-1]
        tot = 0.0
        for s in range(0, y.numel(), 512):
            tot = tot + checkpoint(chunk_loss, hs[s:s + 512], y[s:s + 512], use_reentrant=False)
        return tot / y.numel(), beta

    log = open(f"{OUT}/train_log.jsonl", "a")
    t0, step = time.time(), 0
    for ep in range(epochs):
        random.Random(ep).shuffle(train)
        opt.zero_grad()
        run_ce, run_b = 0.0, 0.0
        skipped = 0
        for n, d in enumerate(train, 1):
            try:
                ce, beta = sample_loss(d)
                loss = (ce + mu_cost * beta.mean()) / 8
                loss.backward()
                run_ce += float(ce)
                run_b += float(beta.mean())
            except torch.cuda.OutOfMemoryError:  # H5: one target too long for memory; drop this accumulation group
                skipped += 1
                ce = beta = loss = None
                opt.zero_grad()
                torch.cuda.empty_cache()
                print(f"ep {ep} {n}: out of memory on {d['id']} ({d['ids'].numel()} tokens), skipped", flush=True)
            if n % 8 == 0 or n == len(train):
                opt.step()
                opt.zero_grad()
                step += 1
            if n % 40 == 0 or n == len(train):
                print(f"ep {ep} {n}/{len(train)} ce {run_ce / n:.4f} mean beta {run_b / n:.3f} "
                      f"({time.time() - t0:.0f} s, peak {torch.cuda.max_memory_allocated(dev) / 2**30:.1f} GiB)", flush=True)
        with torch.no_grad():
            vals = []
            for d in val:
                try:
                    vals.append(float(sample_loss(d)[0]))
                except torch.cuda.OutOfMemoryError:
                    torch.cuda.empty_cache()
            vce = sum(vals) / max(1, len(vals))
        rec = dict(epoch=ep, train_ce=run_ce / len(train), val_ce=vce, mean_beta=run_b / len(train),
                   seconds=round(time.time() - t0), skipped_oom=skipped)
        log.write(json.dumps(rec) + "\n")
        log.flush()
        print("EPOCH", json.dumps(rec), flush=True)
        torch.save(hyper.state_dict(), f"{OUT}/hypernet.pt")
    with torch.no_grad():
        B = hyper(emb["x"].to(dev)).cpu()
    torch.save({"ids": emb["ids"], "tasks": emb["tasks"], "beta": B}, f"{OUT}/betas.pt")
    by = {}
    for i, t, b in zip(emb["ids"], emb["tasks"], B):
        key = ("pool " if i.startswith("pool-") else "test ") + (t if t != "bbh" else "bbh-" + i.split("bbh-")[1].rsplit("-", 1)[0])
        by.setdefault(key, []).append(b)
    for key in sorted(by):
        m = torch.stack(by[key]).mean(0)
        print(f"{key:48s} mean beta {float(m[:36].mean()):.2f} | early {float(m[:12].mean()):.2f} mid "
              f"{float(m[12:24].mean()):.2f} late {float(m[24:36].mean()):.2f} | emb {float(m[36]):.2f}", flush=True)


if __name__ == "__main__":
    main()