"""Smoke test + timing benchmark for sim.py.""" import math import sys import time import torch sys.path.insert(0, __file__.rsplit("/", 1)[0]) import sim dev = "cuda" if torch.cuda.is_available() else "cpu" print("device:", dev, torch.cuda.get_device_name(0) if dev == "cuda" else "") # --- activation sanity ------------------------------------------------------ z = torch.linspace(-6, 6, 13, device=dev, dtype=torch.float64) for act in ("quad", "trunc", "smooth"): s = sim.sigma(z, act, 8.0) print(f"{act:7s} sigma:", [round(float(v), 3) for v in s]) # numeric derivative check eps = 1e-6 for act in ("quad", "trunc", "smooth"): zz = torch.tensor([0.5, 1.5, 2.5, 3.5], device=dev, dtype=torch.float64) num = (sim.sigma(zz + eps, act, 8.0) - sim.sigma(zz - eps, act, 8.0)) / (2 * eps) ana = sim.sigma_prime(zz, act, 8.0) print(f"{act:7s} d/dz max err:", float((num - ana).abs().max())) # --- E[2 y x x^T] spectrum sanity (population lambda1=6, lambda2=2 for quad) -- for act in ("quad", "trunc", "smooth"): d, n = 64, 64 * 400 data = sim.make_data(d, n, 0, act, 8.0, dev, torch.float64) A = sim.a_star(data) l1, l2, v1 = sim.top2_eig(A) ov = float((v1 @ data.theta_star) ** 2) print(f"{act:7s} n/d=400: lam1={l1:.3f} lam2={l2:.3f} ov^2={ov:.4f}") # --- flow smoke ------------------------------------------------------------- for act in ("quad", "trunc"): d, n = 256, 256 * 8 data = sim.make_data(d, n, 1, act, 8.0, dev, torch.float32) th0 = sim.rand_sphere(d, 1234, dev, torch.float32) t0 = time.time() th, steps, _ = sim.spherical_flow(data, th0, act, 8.0, eta=0.1, T=20000) ov = float((th @ data.theta_star) ** 2) l1, l2, v1 = sim.top2_eig(sim.a_star(data)) print( f"{act:7s} flow d={d} delta=8: ov^2={ov:.4f} steps={steps} " f"({time.time()-t0:.1f}s) v1(A*) ov^2={float((v1@data.theta_star)**2):.4f}" ) # --- squared-loss GD smoke -------------------------------------------------- d, n = 256, 2560 data = sim.make_data(d, n, 2, "trunc", 8.0, dev, torch.float64) th0 = sim.rand_sphere(d, 7, dev, torch.float64) * d ** -2.0 t0 = time.time() rec = sim.squared_gd(data, th0, "trunc", 8.0, eta=0.1 / 64, T=4000, record_every=20) print( f"squared GD d={d} delta=10: final ov^2={rec['sq_overlap'][-1]:.5f} " f"norm={rec['norm'][-1]:.4f} dist2={rec['dist2'][-1]:.3e} ({time.time()-t0:.1f}s)" ) # --- timing benchmark ------------------------------------------------------- for d in (1024, 4096): n = 11 * d t0 = time.time() data = sim.make_data(d, n, 3, "trunc", 8.0, dev, torch.float32) torch.cuda.synchronize() if dev == "cuda" else None t_gen = time.time() - t0 th0 = sim.rand_sphere(d, 5, dev, torch.float32) t0 = time.time() sim.spherical_flow(data, th0, "trunc", 8.0, T=200, check_every=10 ** 9) torch.cuda.synchronize() if dev == "cuda" else None t_flow = time.time() - t0 t0 = time.time() A = sim.a_star(data) torch.cuda.synchronize() if dev == "cuda" else None t_A = time.time() - t0 t0 = time.time() sim.spherical_flow(data, th0, "quad", 8.0, T=2000, check_every=10 ** 9) torch.cuda.synchronize() if dev == "cuda" else None t_mat = time.time() - t0 print( f"d={d} n={n}: gen={t_gen:.2f}s trunc-flow 200 steps={t_flow:.2f}s " f"form A*={t_A:.2f}s matrix-flow 2000 steps={t_mat:.2f}s" ) del data torch.cuda.empty_cache() if dev == "cuda" else None print("T=1000 log^2 d:", {d: sim.log2_steps(d) for d in (64, 1024, 4096, 8192)})