| |
| """Batched GPU audit of Claim 2 on the paper's lambda_B ring setup.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import math |
| import time |
|
|
| import torch |
|
|
|
|
| WEIGHTS = [0.4, 2.2, 1.2, 0.5, 1.0, 0.6, 1.5, 0.5, |
| 1.0, 0.7, 1.3, 0.9, 1.4, 0.6, 1.2, 1.0] |
|
|
|
|
| def mh_ring(weights: torch.Tensor, epsilon: float = 0.3) -> torch.Tensor: |
| n = len(weights) |
| matrix = torch.zeros((n, n), dtype=weights.dtype, device=weights.device) |
| degree = 2.0 |
| for i in range(n): |
| for j in ((i - 1) % n, (i + 1) % n): |
| acceptance = min(1.0, float(weights[j] / weights[i])) |
| matrix[i, j] = (1.0 - epsilon) * acceptance / degree |
| matrix[i, i] = 1.0 - matrix[i].sum() |
| return matrix |
|
|
|
|
| def centered_radius(matrix: torch.Tensor, weights: torch.Tensor) -> float: |
| n = len(weights) |
| projection = torch.ones((n, 1), dtype=matrix.dtype, device=matrix.device) @ (weights[None, :] / n) |
| return float(torch.linalg.eigvals(matrix - projection).abs().max().cpu()) |
|
|
|
|
| def paired_stats(values: torch.Tensor) -> dict: |
| values = values.double() |
| mean = float(values.mean().cpu()) |
| se = float((values.std(unbiased=True) / math.sqrt(values.numel())).cpu()) |
| return { |
| "mean": mean, |
| "ci95": [mean - 1.96 * se, mean + 1.96 * se], |
| "positive_fraction": float((values > 0).double().mean().cpu()), |
| } |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--trials", type=int, default=4096) |
| parser.add_argument("--iterations", type=int, default=300) |
| parser.add_argument("--alpha", type=float, default=0.02) |
| parser.add_argument("--noise-std", type=float, default=1.0) |
| parser.add_argument("--device", default="cuda") |
| args = parser.parse_args() |
|
|
| if args.device == "cuda" and not torch.cuda.is_available(): |
| raise RuntimeError("CUDA requested but unavailable") |
| device = torch.device(args.device) |
| dtype = torch.float64 |
| generator = torch.Generator(device=device).manual_seed(20260722) |
| n, dimension = 16, 10 |
| weights = torch.tensor(WEIGHTS, device=device, dtype=dtype) |
| weights = weights * n / weights.sum() |
| row = mh_ring(weights) |
| doubly = mh_ring(torch.ones_like(weights)) |
| rho_lambda = centered_radius(row, weights) |
| rho_j = centered_radius(doubly, torch.ones_like(weights)) |
|
|
| zeta = torch.empty((args.trials, n), device=device, dtype=dtype).uniform_(5.5, 12.5, generator=generator) |
| curvature = zeta + 0.01 |
| base = torch.randn((args.trials, 1, dimension), device=device, dtype=dtype, generator=generator) |
| directions = torch.randn((args.trials, n, dimension), device=device, dtype=dtype, generator=generator) |
| directions = directions / torch.linalg.vector_norm(directions, dim=2, keepdim=True) |
| centers = base + 3.0 * directions |
| theta0 = torch.randn((args.trials, n, dimension), device=device, dtype=dtype, generator=generator) |
| noise0 = torch.randn((args.trials, n, dimension), device=device, dtype=dtype, generator=generator) |
|
|
| theta_ds = theta0.clone() |
| theta_row = theta0.clone() |
| true_ds = curvature[:, :, None] * theta_ds - zeta[:, :, None] * centers |
| true_row = true_ds.clone() |
| stochastic_ds = true_ds + args.noise_std * noise0 |
| stochastic_row = true_row + args.noise_std * noise0 |
| tracker_ds = weights[None, :, None] * stochastic_ds |
| tracker_row = stochastic_row.clone() |
| auc_ds = torch.zeros(args.trials, device=device, dtype=dtype) |
| auc_row = torch.zeros_like(auc_ds) |
| tail_ds = torch.zeros_like(auc_ds) |
| tail_row = torch.zeros_like(auc_ds) |
|
|
| if device.type == "cuda": |
| torch.cuda.synchronize() |
| started = time.perf_counter() |
| for step in range(args.iterations): |
| norm_ds = torch.linalg.vector_norm((weights[None, :, None] * true_ds).mean(dim=1), dim=1) |
| norm_row = torch.linalg.vector_norm((weights[None, :, None] * true_row).mean(dim=1), dim=1) |
| auc_ds += norm_ds |
| auc_row += norm_row |
| if step >= args.iterations - 30: |
| tail_ds += norm_ds |
| tail_row += norm_row |
|
|
| next_theta_ds = torch.einsum("ij,sjd->sid", doubly, theta_ds - args.alpha * tracker_ds) |
| next_theta_row = torch.einsum("ij,sjd->sid", row, theta_row - args.alpha * tracker_row) |
| next_true_ds = curvature[:, :, None] * next_theta_ds - zeta[:, :, None] * centers |
| next_true_row = curvature[:, :, None] * next_theta_row - zeta[:, :, None] * centers |
| noise = torch.randn((args.trials, n, dimension), device=device, dtype=dtype, generator=generator) |
| next_stochastic_ds = next_true_ds + args.noise_std * noise |
| next_stochastic_row = next_true_row + args.noise_std * noise |
| next_tracker_ds = torch.einsum("ij,sjd->sid", doubly, tracker_ds) + weights[None, :, None] * (next_stochastic_ds - stochastic_ds) |
| next_tracker_row = torch.einsum("ij,sjd->sid", row, tracker_row) + (next_stochastic_row - stochastic_row) |
| theta_ds, theta_row = next_theta_ds, next_theta_row |
| true_ds, true_row = next_true_ds, next_true_row |
| stochastic_ds, stochastic_row = next_stochastic_ds, next_stochastic_row |
| tracker_ds, tracker_row = next_tracker_ds, next_tracker_row |
|
|
| if device.type == "cuda": |
| torch.cuda.synchronize() |
| elapsed = time.perf_counter() - started |
| auc_ds /= args.iterations |
| auc_row /= args.iterations |
| tail_ds /= 30.0 |
| tail_row /= 30.0 |
| result = { |
| "device": str(device), |
| "gpu_name": torch.cuda.get_device_name(0) if device.type == "cuda" else None, |
| "torch_version": torch.__version__, |
| "dtype": str(dtype), |
| "trials": args.trials, |
| "iterations": args.iterations, |
| "alpha": args.alpha, |
| "noise_std": args.noise_std, |
| "rho_j": rho_j, |
| "rho_lambda": rho_lambda, |
| "gap_j": 1.0 - rho_j, |
| "gap_lambda": 1.0 - rho_lambda, |
| "row_gap_is_smaller": (1.0 - rho_lambda) < (1.0 - rho_j), |
| "ds_tail_mean": float(tail_ds.mean().cpu()), |
| "row_tail_mean": float(tail_row.mean().cpu()), |
| "tail_improvement_percent": float((100.0 * (1.0 - tail_row.mean() / tail_ds.mean())).cpu()), |
| "paired_tail_ds_minus_row": paired_stats(tail_ds - tail_row), |
| "ds_auc_mean": float(auc_ds.mean().cpu()), |
| "row_auc_mean": float(auc_row.mean().cpu()), |
| "auc_improvement_percent": float((100.0 * (1.0 - auc_row.mean() / auc_ds.mean())).cpu()), |
| "paired_auc_ds_minus_row": paired_stats(auc_ds - auc_row), |
| "runtime_seconds": elapsed, |
| } |
| print("RESULT_JSON_START") |
| print(json.dumps(result, indent=2)) |
| print("RESULT_JSON_END") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|