File size: 7,084 Bytes
2a5c21d
 
 
 
 
 
 
 
 
 
 
 
 
3ad584c
 
2a5c21d
 
 
 
3ad584c
 
 
 
 
 
 
 
 
 
 
 
2a5c21d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a6ee3c
2a5c21d
 
 
 
 
 
 
93c0c81
 
3568e2c
f772d40
93c0c81
2a5c21d
 
 
 
 
 
 
 
 
 
 
 
 
 
ab233ec
2a5c21d
 
 
 
 
 
 
4a6ee3c
 
93c0c81
 
 
 
 
3568e2c
 
 
 
f772d40
 
 
 
93c0c81
2a5c21d
 
 
 
 
 
 
 
 
 
 
ab233ec
 
 
 
 
2a5c21d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""User-facing commands for one arm, the full research pipeline, and a live local explorer."""

import argparse
import json
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path

from .records import HORIZONS, METHODS, storage_root


def dashboard(project, port):
    """Serve the same Pages application locally with a live view of atomically recorded events."""
    from .report_records import read_json, study_snapshot
    partial_calibrations=storage_root(project)/"calibration"

    class Handler(SimpleHTTPRequestHandler):
        def do_GET(self):
            if self.path.split("?",1)[0] == "/live.json":
                study = study_snapshot(project)
                for run in study["runs"]:
                    if run.get("status")=="initializing":
                        kinds={"combined":("trace","third"),"sham":("third",)}.get(run.get("method"),(run.get("method"),))
                        for kind in kinds:
                            suffix="-fp64" if run["stage"]=="abstracts" else ""
                            file=partial_calibrations/f"{run['stage']}-{kind}{suffix}-draws.json"
                            draws=read_json(file).get("draws",[])
                            if draws:
                                run["status"]=f"calibrating ({len(draws)}/16)"
                                run["calibration_progress"]={"draws_completed":len(draws),"last_draw":draws[-1]}
                data = json.dumps(study,ensure_ascii=False,allow_nan=False).encode()
                self.send_response(200)
                self.send_header("Content-Type","application/json; charset=utf-8")
                self.send_header("Cache-Control","no-store")
                self.send_header("Content-Length",str(len(data)))
                self.end_headers()
                self.wfile.write(data)
            else:
                super().do_GET()
    server = ThreadingHTTPServer(("127.0.0.1",port),partial(Handler,directory=str(project/"docs")))
    print(f"Live study dashboard http://127.0.0.1:{port}/",flush=True)
    server.serve_forever()


def parser():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("--project",type=Path,default=Path.cwd())
    parsers = p.add_subparsers(dest="action",required=True)
    for action in ("prepare","train","study","freeze","evaluate","report","dashboard","publish","progress","verify"):
        sub = parsers.add_parser(action)
        if action in {"prepare","study"}:
            sub.add_argument("--stage",choices=(*HORIZONS,"both"),default="both")
        if action in {"train","freeze"}:
            sub.add_argument("--stage",choices=HORIZONS,required=True)
        if action in {"train","study","evaluate"}:
            sub.add_argument("--device",default="cuda")
        if action=="verify":
            sub.add_argument("--pretrained",action="store_true",help="Also compare a full joint derivative on the prepared adapter training corpus")
            sub.add_argument("--reference",action="store_true",help="Compute the FP64 pretrained reference using the original CUDA signs")
            sub.add_argument("--scoring-batches",action="store_true",help="Benchmark Pythia inference grouping on training windows")
            sub.add_argument("--device",default="cuda",help="Device for the optional full pretrained cross-check")
        if action=="train":
            sub.add_argument("--method",choices=(*METHODS,"unigram","initial"),required=True)
            sub.add_argument("--phase",choices=("pilot","control","confirm","smoke"),default="pilot")
            sub.add_argument("--seed",type=int,default=0)
            sub.add_argument("--ratio",type=float,default=0.03)
            sub.add_argument("--microbatch",type=int)
        if action in {"train","study","evaluate"}:
            sub.add_argument("--publish",action="store_true",help="Publish results to the configured project accounts")
        if action=="evaluate":
            sub.add_argument("run_id")
        if action=="dashboard":
            sub.add_argument("--port",type=int,default=8765)
        if action=="publish":
            sub.add_argument("--message",default="Update third-derivative study records")
            sub.add_argument("--checkpoints",action="store_true",help="Republish the fitted checkpoints and restart states in the collection")
    return p


def main(argv=None):
    args = parser().parse_args(argv)
    project = args.project.resolve()
    storage = storage_root(project)
    if args.action=="verify":
        from .verification import verify
        verify(project)
        if args.pretrained:
            from .probe_check import verify_pretrained
            from .records import setup_device
            verify_pretrained(project,setup_device(args.device))
        if args.reference:
            from .precision import reference_gradient
            from .records import setup_device
            reference_gradient(project,setup_device(args.device))
        if args.scoring_batches:
            from .probe_check import benchmark_scoring
            from .records import setup_device
            benchmark_scoring(project,setup_device(args.device))
        return
    if args.action=="dashboard":
        return dashboard(project,args.port)
    if args.action in {"report","progress","publish"}:
        from .progress import progress
        if args.action == "progress":
            print(progress(project))
            return
        from .reporting import build_report
        build_report(project)
        progress(project)
        if args.action == "publish":
            from .release import publish_snapshot, publish_weights
            if args.checkpoints:
                from .records import read_json
                for path in sorted((project/"results").glob("*/summary.json")):
                    publish_weights(project,storage,read_json(path))
            print(publish_snapshot(project,args.message))
        return
    from .study import evaluate_one,freeze,milestone,prepare_stage,run_study
    if args.action=="freeze":
        print(freeze(project,storage,args.stage)["freeze_id"])
        return
    stages = list(HORIZONS) if getattr(args,"stage",None)=="both" else [getattr(args,"stage",None)]
    if args.action=="prepare":
        for stage in stages:
            prepare_stage(project,storage,stage)
        return
    from .records import setup_device
    device = setup_device(args.device)
    if args.action=="study":
        run_study(project,storage,stages,device,args.publish)
        return
    if args.action=="evaluate":
        summary = evaluate_one(project,storage,args.run_id,device)
    else:
        from .train import run_one
        summary = run_one(project,storage,args.stage,args.method,args.phase,args.seed,args.ratio,device,args.microbatch)
    milestone(project,storage,summary,args.publish)


if __name__=="__main__":
    main()