File size: 8,518 Bytes
53a1159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""Freeze and execute the single-world depth/time development comparison."""

from __future__ import annotations

import argparse
import json
import os
import queue
import shutil
import subprocess
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path

from llm_memory_editability.grok_depth import utc, write_json
from llm_memory_editability.storage_composition import file_hash

CONFIG = Path("configs/depth-step-development-v1.json")
ARTIFACTS = Path("docs/development-artifacts/depth-step-v1")
RESULTS = Path("results/depth-step-v1")
DESIGN = ARTIFACTS / "design.md"
SOURCES = [
    "src/llm_memory_editability/depth_step.py",
    "src/llm_memory_editability/bios_model.py",
    "src/llm_memory_editability/grok_depth.py",
    "src/llm_memory_editability/grok_depth_data.py",
    "src/llm_memory_editability/grok_multihop_data.py",
    "src/llm_memory_editability/grok_loop_data.py",
    "src/llm_memory_editability/grok_loop_model.py",
    "src/llm_memory_editability/storage_composition.py",
    "src/llm_memory_editability/storage_frontier.py",
    "src/llm_memory_editability/latent_scaling.py",
    "src/llm_memory_editability/text_pretrain.py",
    "scripts/run_depth_step.py",
    "scripts/execute_depth_step.py",
    "scripts/report_depth_step.py",
    "tests/test_depth_step.py",
    "tests/test_depth_step_report.py",
]


def specifications():
    base = {
        "world_seed": 751011,
        "entities": 64,
        "relations": 4,
        "degree": 4,
        "phi": 4.0,
        "id_fraction": 0.75,
        "id_test_fraction": 0.2,
        "evaluation_size": 512,
        "width": 128,
        "heads": 4,
        "dropout": 0.0,
        "initialization": 752011,
        "stream_seed": 753011,
        "model_initialization": "scaled_effective",
        "batch_size": 128,
        "steps": 64000,
        "nodes": [0, 256, 512, 1000, 2000, 4000, 8000, 16000, 32000, 64000],
        "checkpoint_nodes": [0, 8000, 32000, 64000],
        "lr": 0.001,
        "weight_decay": 0.01,
        "warmup": 200,
        "schedule": "cosine",
        "min_lr_ratio": 0.1,
        "clip": 1.0,
    }
    architectures = [(layers, 1) for layers in (1, 2, 3, 4, 6)]
    architectures += [(1, repeats) for repeats in (2, 3, 4, 6)]
    return [{**base, "layers": layers, "repeats": repeats} for layers, repeats in architectures]


def prepare():
    from llm_memory_editability.depth_step import build_world, construct

    if CONFIG.exists():
        raise FileExistsError(CONFIG)
    specs = specifications()
    world = build_world(specs[0])
    for spec in specs:
        spec["frozen_data_sha256"] = world["metadata"]["dataset_sha256"]
    data = {key: len(value) for key, value in world.items() if key != "metadata"}
    source = {path: file_hash(path) for path in SOURCES}
    initial_models = []
    for spec in specs:
        model = construct(spec, "cpu")
        from llm_memory_editability.latent_scaling import model_digest

        initial_models.append(
            {
                "layers": spec["layers"],
                "repeats": spec["repeats"],
                "parameters": sum(p.numel() for p in model.parameters()),
                "initial_model_sha256": model_digest(model),
            }
        )
    config = {
        "created_utc": utc(),
        "phase": "single-world development; no new-world confirmation",
        "specs": specs,
        "source": source,
        "design_sha256": file_hash(DESIGN),
        "data_sizes": data,
        "metadata": world["metadata"],
        "initial_models": initial_models,
        "git_commit": subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip(),
        "primary": "Fixed-endpoint familiar/strict accuracies for k=2/3/4 and all trajectories",
        "analysis_unit": (
            "One development world and one initialization; "
            "layers/steps/queries are repeated measurements"
        ),
        "budget": {
            "runs": len(specs),
            "updates": sum(s["steps"] for s in specs),
            "learning_nodes": sum(len(s["nodes"]) for s in specs),
        },
    }
    write_json(CONFIG, config)
    for path in SOURCES:
        dest = ARTIFACTS / "source" / path
        dest.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy2(path, dest)
    shutil.copy2(CONFIG, ARTIFACTS / "frozen-config.json")
    print(json.dumps({"data_sizes": data, "budget": config["budget"]}), flush=True)


def check_config():
    config = json.loads(CONFIG.read_text())
    for path, digest in config["source"].items():
        if file_hash(path) != digest:
            raise RuntimeError(f"Frozen source changed: {path}")
    if file_hash(DESIGN) != config["design_sha256"]:
        raise RuntimeError("Frozen design changed")
    return config


def run_one(spec, source, folder, gpu):
    folder.mkdir(parents=True, exist_ok=True)
    if (folder / "complete.json").exists():
        raise FileExistsError(folder)
    write_json(folder / "input-spec.json", {"spec": spec, "source": source})
    env = dict(os.environ, OMP_NUM_THREADS="1", MKL_NUM_THREADS="1")
    statuses = []
    for stage in ("train", "audit"):
        command = [
            sys.executable,
            "scripts/run_depth_step.py",
            stage,
            "--out",
            str(folder),
            "--device",
            f"cuda:{gpu}",
        ]
        if stage == "train":
            command.extend(["--spec-file", str(folder / "input-spec.json")])
        started = time.perf_counter()
        with (folder / f"{stage}-process.log").open("w") as log:
            result = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT, env=env)
        status = {
            "stage": stage,
            "gpu": gpu,
            "returncode": result.returncode,
            "seconds": time.perf_counter() - started,
        }
        statuses.append(status)
        write_json(folder / f"{stage}-process-status.json", status)
        if result.returncode:
            raise RuntimeError(f"{stage} failed: {folder}")
    return {"run": folder.name, "stages": statuses}


def preflight(gpu):
    config = check_config()
    records = []
    for layers, repeats in ((2, 1), (1, 2)):
        spec = next(
            s for s in config["specs"] if (s["layers"], s["repeats"]) == (layers, repeats)
        ).copy()
        spec.update(steps=8, nodes=[0, 8], checkpoint_nodes=[0, 8])
        records.append(
            run_one(spec, config["source"], RESULTS / f"engineering-l{layers}-r{repeats}", gpu)
        )
    write_json(
        ARTIFACTS / "preflight.json",
        {
            "scope": "8-step engineering, no performance selection",
            "passed": True,
            "records": records,
        },
    )


def execute(gpus):
    from llm_memory_editability.depth_step import run_name

    config = check_config()
    if not json.loads((ARTIFACTS / "preflight.json").read_text())["passed"]:
        raise RuntimeError("Engineering preflight required")
    if not gpus or len(set(gpus)) != len(gpus):
        raise ValueError("GPU slots must be nonempty and distinct")
    slots = queue.Queue()
    for gpu in gpus:
        slots.put(gpu)
    records = []
    started = time.perf_counter()
    write_json(
        ARTIFACTS / "launch.json",
        {
            "started_utc": utc(),
            "gpus": gpus,
            "config_sha256": file_hash(CONFIG),
            "runs": len(config["specs"]),
        },
    )

    def launch(spec):
        gpu = slots.get()
        try:
            record = run_one(spec, config["source"], RESULTS / run_name(spec), gpu)
            print(json.dumps(record), flush=True)
            return record
        finally:
            slots.put(gpu)

    with ThreadPoolExecutor(max_workers=len(gpus)) as pool:
        for record in pool.map(launch, config["specs"]):
            records.append(record)
    write_json(
        ARTIFACTS / "execution.json",
        {"finished_utc": utc(), "wall_seconds": time.perf_counter() - started, "records": records},
    )


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("stage", choices=["prepare", "preflight", "execute"])
    parser.add_argument("--gpus", default="0,1,2,3,4,5,6,7")
    args = parser.parse_args()
    gpus = [int(s) for s in args.gpus.split(",")]
    if args.stage == "prepare":
        prepare()
    elif args.stage == "preflight":
        preflight(gpus[0])
    else:
        execute(gpus)


if __name__ == "__main__":
    main()