File size: 6,283 Bytes
6cd33cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
"""Run the locked RAID-only pairwise-CE neologism training recipe."""

from __future__ import annotations

import argparse
import os
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path


@dataclass(frozen=True)
class ModelProfile:
    model: str
    ai_token: str
    human_token: str
    pilot_lr: float
    continuation_steps: int
    continuation_lr: float | None = None
    continuation_pairs: int | None = None
    schedule_total_steps: int | None = None


PROFILES = {
    "gemma": ModelProfile(
        model="google/gemma-4-E4B-it",
        ai_token="<ai>",
        human_token="<human>",
        pilot_lr=1e-3,
        continuation_steps=75,
        continuation_lr=1e-4,
        continuation_pairs=5_000,
        schedule_total_steps=625,
    ),
    "llama": ModelProfile(
        model="meta-llama/Llama-3.1-8B-Instruct",
        ai_token="<AIGEN>",
        human_token="<REAL>",
        pilot_lr=1e-4,
        continuation_steps=0,
    ),
}


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--model-family", choices=PROFILES, required=True)
    parser.add_argument("--dataset-disk", type=Path, required=True)
    parser.add_argument("--ai-init", type=Path, required=True)
    parser.add_argument("--human-init", type=Path, required=True)
    parser.add_argument("--output-dir", type=Path, required=True)
    parser.add_argument("--seed", type=int, default=42)
    parser.add_argument("--model", help="Override the Hugging Face model ID/path.")
    parser.add_argument("--runner", type=Path)
    parser.add_argument("--beemo-pairs", type=int, default=2_163)
    parser.add_argument("--bootstrap-resamples", type=int, default=1_000)
    parser.add_argument("--dry-run", action="store_true")
    return parser.parse_args()


def run(command: list[str], *, repo_root: Path, dry_run: bool) -> None:
    printable = " ".join(subprocess.list2cmdline([arg]) for arg in command)
    print(f"+ {printable}", flush=True)
    if dry_run:
        return
    env = os.environ.copy()
    env["PYTHONPATH"] = os.pathsep.join(
        part
        for part in (str(repo_root), env.get("PYTHONPATH", ""))
        if part
    )
    subprocess.run(command, cwd=repo_root, env=env, check=True)


def common_command(
    *,
    python: str,
    runner: Path,
    args: argparse.Namespace,
    profile: ModelProfile,
    output_dir: Path,
    ai_init: Path,
    human_init: Path,
) -> list[str]:
    return [
        python,
        "-u",
        str(runner),
        "--dataset-disk",
        str(args.dataset_disk),
        "--train-split",
        "standard_train_expanded6",
        "--test-split",
        "standard_test",
        "--output-dir",
        str(output_dir),
        "--model",
        args.model or profile.model,
        "--ai-token",
        profile.ai_token,
        "--human-token",
        profile.human_token,
        "--prompt-template",
        "Write {token} text.",
        "--ai-init",
        str(ai_init),
        "--human-init",
        str(human_init),
        "--objective",
        "ai_pairwise",
        "--max-length",
        "512",
        "--batch-size",
        "8",
        "--eval-batch-size",
        "8",
        "--min-lr",
        "1e-5",
        "--warmup-steps",
        "20",
        "--beta",
        "1",
        "--bootstrap-resamples",
        str(args.bootstrap_resamples),
        "--seed",
        str(args.seed),
    ]


def main() -> None:
    args = parse_args()
    profile = PROFILES[args.model_family]
    repo_root = Path(__file__).resolve().parents[1]
    runner = args.runner or repo_root / "scripts/run_gemma_expanded_pairwise_ce.py"
    output_dir = args.output_dir.resolve()
    train_dir = output_dir / "train"
    eval_dir = output_dir / "eval"

    if output_dir.exists() and any(output_dir.iterdir()):
        raise FileExistsError(f"Refusing to overwrite non-empty {output_dir}")
    train_dir.mkdir(parents=True, exist_ok=True)

    train = common_command(
        python=sys.executable,
        runner=runner,
        args=args,
        profile=profile,
        output_dir=train_dir,
        ai_init=args.ai_init,
        human_init=args.human_init,
    )
    train.extend(
        [
            "--pilot-pairs",
            "500",
            "--pilot-epochs",
            "2",
            "--pilot-lrs",
            str(profile.pilot_lr),
            "--pilot-beemo-pairs",
            "250",
        ]
    )

    if args.model_family == "gemma":
        assert profile.continuation_lr is not None
        assert profile.continuation_pairs is not None
        assert profile.schedule_total_steps is not None
        train.extend(
            [
                "--full-pairs",
                str(profile.continuation_pairs),
                "--full-epochs",
                "1",
                "--full-lr",
                str(profile.continuation_lr),
                "--stop-after-steps",
                str(profile.continuation_steps),
                "--schedule-total-steps",
                str(profile.schedule_total_steps),
                "--eval-every",
                str(profile.continuation_steps),
                "--beemo-pairs",
                str(args.beemo_pairs),
            ]
        )
        run(train, repo_root=repo_root, dry_run=args.dry_run)
        return

    train.append("--pilot-only")
    run(train, repo_root=repo_root, dry_run=args.dry_run)

    # Llama continuation consistently regressed the fixed monitor, so evaluate
    # the 125-update pilot directly on both authoritative test populations.
    selected_ai = train_dir / "pilot/lr_0.0001/final/tokens/ai_token.pt"
    eval_dir.mkdir(parents=True, exist_ok=True)
    evaluate = common_command(
        python=sys.executable,
        runner=runner,
        args=args,
        profile=profile,
        output_dir=eval_dir,
        ai_init=selected_ai,
        human_init=args.human_init,
    )
    evaluate.extend(
        [
            "--eval-only",
            "--eval-targets",
            "beemo,raid",
            "--beemo-pairs",
            str(args.beemo_pairs),
        ]
    )
    run(evaluate, repo_root=repo_root, dry_run=args.dry_run)


if __name__ == "__main__":
    main()