AbstractPhil commited on
Commit
087391f
Β·
verified Β·
1 Parent(s): 486c848

Create eigen_barrage_parallel_refinement_testing.py

Browse files
eigen_barrage_parallel_refinement_testing.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Diagnostic: what exactly breaks in parallel root-finding?
3
+
4
+ Test 1: Pure parallel Laguerre (no Aberth, no clamp, no damp)
5
+ Test 2: Parallel Laguerre + Aberth
6
+ Test 3: Sequential Laguerre + deflation (baseline)
7
+
8
+ Prints per-iteration convergence to identify exactly where it goes wrong.
9
+ """
10
+ import math, torch
11
+
12
+ torch.backends.cuda.matmul.allow_tf32 = False
13
+ torch.set_float32_matmul_precision('highest')
14
+
15
+ dev = torch.device('cuda')
16
+ B = 512; N = 6
17
+ torch.manual_seed(42)
18
+ A = (lambda R: (R+R.mT)/2)(torch.randn(B, N, N, device=dev))
19
+ rv, rV = torch.linalg.eigh(A)
20
+
21
+ # FL Phase 1 β€” get characteristic polynomial
22
+ sc = (torch.linalg.norm(A.reshape(B,-1), dim=-1) / math.sqrt(N)).clamp(min=1e-12)
23
+ As = A / sc[:, None, None]; Ad = As.double()
24
+ I_d = torch.eye(N, device=dev, dtype=torch.float64).unsqueeze(0).expand(B,-1,-1)
25
+ c = torch.zeros(B, N+1, device=dev, dtype=torch.float64); c[:, N] = 1.0
26
+ Mk = torch.zeros(B, N, N, device=dev, dtype=torch.float64)
27
+ for k in range(1, N+1):
28
+ Mk = torch.bmm(Ad, Mk) + c[:, N-k+1, None, None] * I_d
29
+ c[:, N-k] = -(Ad * Mk).sum((-2,-1)) / k
30
+
31
+ # True roots (scaled)
32
+ true_roots = (rv / sc.unsqueeze(-1)).double().sort(dim=-1).values
33
+
34
+ # Init from diagonal
35
+ z_init = Ad.diagonal(dim1=-2, dim2=-1).sort(dim=-1).values
36
+ pert = torch.linspace(-1e-3, 1e-3, N, device=dev, dtype=torch.float64).unsqueeze(0)
37
+ z_init = z_init + pert
38
+
39
+ def horner_pd(c, z):
40
+ """Evaluate p(z), p'(z), p''(z)/2 via Horner. c: [B,n+1], z: [B,n]"""
41
+ B, n_roots = z.shape
42
+ n = c.shape[1] - 1
43
+ pv = c[:, n:n+1].expand(B, n_roots)
44
+ dp = torch.zeros_like(pv)
45
+ d2 = torch.zeros_like(pv)
46
+ for j in range(n-1, -1, -1):
47
+ d2 = d2 * z + dp
48
+ dp = dp * z + pv
49
+ pv = pv * z + c[:, j:j+1]
50
+ return pv, dp, d2
51
+
52
+ def laguerre_step(c, z, n):
53
+ pv, dp, d2 = horner_pd(c, z)
54
+ ok = pv.abs() > 1e-30
55
+ ps = torch.where(ok, pv, torch.ones_like(pv))
56
+ G = torch.where(ok, dp / ps, torch.zeros_like(dp))
57
+ H = G * G - torch.where(ok, 2.0 * d2 / ps, torch.zeros_like(d2))
58
+ disc = ((n-1.0) * (n * H - G * G)).clamp(min=0.0)
59
+ sq = torch.sqrt(disc)
60
+ gp = G + sq; gm = G - sq
61
+ den = torch.where(gp.abs() >= gm.abs(), gp, gm)
62
+ return torch.where(den.abs() > 1e-20, float(n) / den, torch.zeros_like(den))
63
+
64
+ mask_eye = torch.eye(N, device=dev, dtype=torch.bool).unsqueeze(0)
65
+
66
+ def aberth_correction(z):
67
+ diffs = z.unsqueeze(-1) - z.unsqueeze(-2)
68
+ diffs_safe = diffs.masked_fill(mask_eye, 1.0)
69
+ return (1.0 / diffs_safe).masked_fill(mask_eye, 0.0).sum(-1)
70
+
71
+ def report(label, z, iteration):
72
+ err = (z.sort(dim=-1).values - true_roots).abs().max().item()
73
+ # Check for duplicates: min gap between sorted roots
74
+ zs = z.sort(dim=-1).values
75
+ min_gap = (zs[:, 1:] - zs[:, :-1]).min().item()
76
+ # p(z) residual
77
+ pv, _, _ = horner_pd(c, z)
78
+ p_res = pv.abs().max().item()
79
+ print(f" {label:>5} it={iteration:>2} max_err={err:.2e} min_gap={min_gap:.2e} |p(z)|={p_res:.2e}")
80
+
81
+ print("="*78)
82
+ print(" Diagnostic: Parallel Root-Finding")
83
+ print("="*78)
84
+ print(f" B={B} N={N}")
85
+ print(f" True eigenvalue range: [{true_roots.min().item():.3f}, {true_roots.max().item():.3f}]")
86
+ print(f" Diagonal init range: [{z_init.min().item():.3f}, {z_init.max().item():.3f}]")
87
+
88
+ # ═══ Test 1: Pure parallel Laguerre (no Aberth) ═══
89
+ print(f"\n --- Test 1: Pure Laguerre (no Aberth) ---")
90
+ z = z_init.clone()
91
+ for it in range(20):
92
+ step = laguerre_step(c, z, N)
93
+ z = z - step
94
+ if it < 5 or it % 5 == 4:
95
+ report("PurL", z, it)
96
+
97
+ # ═══ Test 2: Laguerre + Aberth (full strength) ═══
98
+ print(f"\n --- Test 2: Laguerre + Aberth (full) ---")
99
+ z = z_init.clone()
100
+ for it in range(20):
101
+ step = laguerre_step(c, z, N)
102
+ corr = aberth_correction(z)
103
+ denom = 1.0 - step * corr
104
+ denom_safe = torch.where(denom.abs() > 1e-20, denom, torch.ones_like(denom))
105
+ full_step = torch.where(denom.abs() > 1e-20, step / denom_safe, step)
106
+ z = z - full_step
107
+ if it < 5 or it % 5 == 4:
108
+ report("LA-F", z, it)
109
+
110
+ # ═══ Test 3: Laguerre + weak Aberth (0.1Γ— correction) ═══
111
+ print(f"\n --- Test 3: Laguerre + weak Aberth (0.1x) ---")
112
+ z = z_init.clone()
113
+ for it in range(20):
114
+ step = laguerre_step(c, z, N)
115
+ corr = aberth_correction(z)
116
+ denom = 1.0 - 0.1 * step * corr
117
+ denom_safe = torch.where(denom.abs() > 1e-20, denom, torch.ones_like(denom))
118
+ full_step = torch.where(denom.abs() > 1e-20, step / denom_safe, step)
119
+ z = z - full_step
120
+ if it < 5 or it % 5 == 4:
121
+ report("LA.1", z, it)
122
+
123
+ # ═══ Test 4: Pure Laguerre + post-sort each iteration ═══
124
+ print(f"\n --- Test 4: Pure Laguerre + re-sort ---")
125
+ z = z_init.clone()
126
+ for it in range(20):
127
+ step = laguerre_step(c, z, N)
128
+ z = z - step
129
+ z = z.sort(dim=-1).values # keep sorted
130
+ if it < 5 or it % 5 == 4:
131
+ report("PL+S", z, it)
132
+
133
+ # ═══ Test 5: Laguerre + Aberth + damped ramp ═══
134
+ print(f"\n --- Test 5: Laguerre + Aberth damped (0.1 β†’ 1.0) ---")
135
+ z = z_init.clone()
136
+ for it in range(20):
137
+ step = laguerre_step(c, z, N)
138
+ corr = aberth_correction(z)
139
+ alpha = min(1.0, 0.1 + 0.1 * it)
140
+ denom = 1.0 - alpha * step * corr
141
+ denom_safe = torch.where(denom.abs() > 1e-20, denom, torch.ones_like(denom))
142
+ full_step = torch.where(denom.abs() > 1e-20, step / denom_safe, step)
143
+ z = z - full_step
144
+ z = z.sort(dim=-1).values
145
+ if it < 5 or it % 5 == 4:
146
+ report("LADa", z, it)
147
+
148
+ # ═══ Test 6: Newton + Aberth (original Aberth-Ehrlich) ═══
149
+ print(f"\n --- Test 6: Newton + Aberth ---")
150
+ z = z_init.clone()
151
+ for it in range(20):
152
+ pv, dp, _ = horner_pd(c, z)
153
+ ok = dp.abs() > 1e-30
154
+ w = torch.where(ok, pv / dp, torch.zeros_like(pv))
155
+ corr = aberth_correction(z)
156
+ denom = 1.0 - w * corr
157
+ denom_safe = torch.where(denom.abs() > 1e-20, denom, torch.ones_like(denom))
158
+ full_step = torch.where(denom.abs() > 1e-20, w / denom_safe, w)
159
+ z = z - full_step
160
+ if it < 5 or it % 5 == 4:
161
+ report("NwAb", z, it)
162
+
163
+ # ═══ Test 7: Pure Newton (no Aberth) ═══
164
+ print(f"\n --- Test 7: Pure Newton ---")
165
+ z = z_init.clone()
166
+ for it in range(20):
167
+ pv, dp, _ = horner_pd(c, z)
168
+ ok = dp.abs() > 1e-30
169
+ w = torch.where(ok, pv / dp, torch.zeros_like(pv))
170
+ z = z - w
171
+ if it < 5 or it % 5 == 4:
172
+ report("PurN", z, it)
173
+
174
+ print("="*78)