File size: 2,861 Bytes
1f48ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""One-pass (online) spherical SGD baseline on the correlation loss.

Ben Arous et al. (2021), Thm 1.4: for information exponent 2 activations, one-pass
SGD with the largest stable step size eta ~ 1/d needs n >~ d log d samples for weak
recovery.  This is the baseline that Claims 2/5 of arXiv:2602.02431 separate from.

Each replica sees every sample exactly once, so a single run of length n_max also
gives the overlap for every smaller n -> the whole delta-curve comes from one pass.
"""

from __future__ import annotations

import argparse
import csv
import math
import os
import sys
import time

import torch

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import sim


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--act", default="trunc", choices=["quad", "trunc", "smooth"])
    p.add_argument("--dims", default="64,128,256,512,1024,2048,4096,8192")
    p.add_argument("--seeds", type=int, default=32)
    p.add_argument("--M", type=float, default=8.0)
    p.add_argument("--eta-cs", default="0.025,0.05,0.1,0.2",
                   help="grid of step sizes eta = c/d (the c values)")
    p.add_argument("--delta-max-mult", type=float, default=6.0,
                   help="delta_max = mult * log(d)")
    p.add_argument("--n-checkpoints", type=int, default=60)
    p.add_argument("--out", required=True)
    args = p.parse_args()

    dev = "cuda" if torch.cuda.is_available() else "cpu"
    dims = [int(v) for v in args.dims.split(",")]
    rows = []
    t_start = time.time()
    for d in dims:
        dmax = args.delta_max_mult * math.log(d)
        deltas = [round(dmax * (i + 1) / args.n_checkpoints, 4) for i in range(args.n_checkpoints)]
        cps = sorted({max(1, int(round(dl * d))) for dl in deltas})
        n_max = cps[-1]
        chunk = max(128, min(2048, (1 << 23) // (d * args.seeds)))
        for c in [float(v) for v in args.eta_cs.split(",")]:
            eta = c / d
            t0 = time.time()
            out = sim.online_sgd(
                d, n_max, args.seeds, args.act, args.M, eta, 4242 + d, dev,
                torch.float32, cps, chunk=chunk,
            )
            for n_used, ov in out.items():
                rows.append(dict(act=args.act, d=d, n=n_used, delta=round(n_used / d, 4),
                                 eta_c=c, eta=eta, seeds=args.seeds,
                                 sq_overlap=round(ov, 6)))
            print(f"[{time.time()-t_start:7.1f}s] d={d:5d} n_max={n_max} eta={eta:.3g} "
                  f"(c={c}) final ov2={out[cps[-1]]:.4f} ({time.time()-t0:.1f}s)", flush=True)

    with open(args.out, "w", newline="") as f:
        w = csv.DictWriter(f, fieldnames=list(rows[0].keys()))
        w.writeheader()
        w.writerows(rows)
    print(f"wrote {args.out} ({len(rows)} rows, {time.time()-t_start:.1f}s)")


if __name__ == "__main__":
    main()