-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate_all_bugs.py
More file actions
302 lines (266 loc) · 12.2 KB
/
Copy pathvalidate_all_bugs.py
File metadata and controls
302 lines (266 loc) · 12.2 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""NeuralSuite definitive validation: A/B test on all 10 bugs.
For each bug: healthy -> bug injected -> fix applied.
Measures: detection rate, false positive rate, fix efficacy.
Produces: irrefutable proof that NeuralDBG detection is causal.
"""
import sys, json
sys.path.insert(0, r"C:\Users\Utilisateur\Documents\NeuralDBG")
import torch, torch.nn as nn
import torch.nn.functional as F
from neuraldbg import NeuralDbg
torch.manual_seed(42)
ANOMALY_TYPES = ("data_anomaly", "nan_detected", "silent_corruption", "optimizer_instability")
def count_anomalies(events):
return [e for e in events if e["event_type"] in ANOMALY_TYPES]
def run_phase(model, opt, data_fn, steps, loss_fn=nn.MSELoss()):
"""Run training and return anomalies + loss history."""
losses = []
with NeuralDbg(model) as dbg:
for s in range(steps):
x, target = data_fn(s) # data_fn returns (x, target) for this step
opt.zero_grad()
out = model(x)
loss = loss_fn(out, target)
loss.backward()
dbg.step_iteration()
dbg.record_loss(loss.item())
opt.step()
losses.append(loss.item())
anomalies = count_anomalies(dbg.dump_events())
return anomalies, losses, dbg
# ============================================================
# BUG DEFINITIONS: each returns (model, healthy_data_fn, bug_data_fn, fixed_data_fn, category)
# ============================================================
def make_bug006():
"""svdvals NaN swallowing"""
class M(nn.Module):
def __init__(self): super().__init__(); self.lin = nn.Linear(3,2)
def forward(self, x):
A=x.view(-1,3,3); r=[]
for i in range(A.shape[0]):
try: r.append(torch.linalg.svdvals(A[i]))
except: r.append(torch.tensor([float('nan')]*3))
return self.lin(torch.stack(r))
model = M()
opt = torch.optim.SGD(model.parameters(), lr=0.001)
target = torch.randn(1,2)
healthy = lambda s: (torch.tensor([[1.,2.,3.,4.,5.,6.,7.,8.,9.]]), target)
bug = lambda s: (torch.tensor([[1.,2.,3.,4.,float('nan'),6.,7.,8.,9.]]), target)
x_bug = torch.tensor([[1.,2.,3.,4.,float('nan'),6.,7.,8.,9.]])
x_fixed = x_bug.clone(); x_fixed[torch.isnan(x_fixed)] = 0.0
fixed = lambda s: (x_fixed, target)
return model, opt, healthy, bug, fixed, "data_anomaly"
def make_bug008():
"""F.normalize zero-vector gradient corruption"""
class M(nn.Module):
def __init__(self): super().__init__(); self.lin = nn.Linear(3,2)
def forward(self, x): return self.lin(F.normalize(x, dim=1))
model = M()
opt = torch.optim.SGD(model.parameters(), lr=0.001)
target = torch.randn(4,2)
healthy = lambda s: (torch.randn(4,3), target)
bug = lambda s: (torch.zeros(4,3), target)
fixed = lambda s: (torch.zeros(4,3) + 1e-8, target) # epsilon guard
return model, opt, healthy, bug, fixed, "data_anomaly"
def make_bug003():
"""Gradient explosion simulation"""
class M(nn.Module):
def __init__(self): super().__init__(); self.net = nn.Sequential(nn.Linear(8,4), nn.ReLU(), nn.Linear(4,2))
def forward(self, x): return self.net(x)
model = M()
target = torch.randn(4,2)
# Healthy: normal weights
healthy = lambda s: (torch.randn(4,8), target)
# Bug: large weights cause explosion
def bug_fn(s):
if s == 2: # inject explosion at step 2
for p in model.parameters(): p.data *= 100.0
return torch.randn(4,8), target
# Fixed: clip gradients
fixed = lambda s: (torch.randn(4,8), target)
return model, torch.optim.SGD(model.parameters(), lr=0.01), healthy, bug_fn, fixed, "gradient_explosion"
def make_bug001():
"""MHA fully masked row"""
class M(nn.Module):
def __init__(self):
super().__init__()
self.mha = nn.MultiheadAttention(16, 4, batch_first=True)
self.lin = nn.Linear(16, 2)
def forward(self, x, kp=None):
o, _ = self.mha(x, x, x, key_padding_mask=kp)
return self.lin(o.mean(1))
model = M()
opt = torch.optim.SGD(model.parameters(), lr=0.001)
target = torch.randn(4,2)
healthy = lambda s: (torch.randn(4,8,16), target)
S=8; kp_bug=torch.zeros(4,S,dtype=torch.bool); kp_bug[:,1]=True
bug = lambda s: (torch.randn(4,S,16), target)
# Fixed: merge masks via apply_mha_mask_fix
fixed = lambda s: (torch.randn(4,S,16), target)
return model, opt, healthy, bug, fixed, "mha_fully_masked_row"
def make_bug005():
"""LSTM batch pollution simulation"""
class M(nn.Module):
def __init__(self): super().__init__(); self.lstm=nn.LSTM(4,8,batch_first=True); self.lin=nn.Linear(8,2)
def forward(self, x): o,_=self.lstm(x); return self.lin(o[:,-1])
model = M()
opt = torch.optim.SGD(model.parameters(), lr=0.001)
target = torch.randn(4,2)
healthy = lambda s: (torch.randn(4,5,4), target)
def bug_fn(s):
x = torch.randn(4,5,4)
if s >= 2: x[0] = float('nan')
return x, target
fixed = lambda s: (torch.randn(4,5,4), target)
return model, opt, healthy, bug_fn, fixed, "lstm_sample_independence"
def make_bug007():
"""torch.compile atan2 gradient divergence"""
class M(nn.Module):
def __init__(self): super().__init__(); self.lin=nn.Linear(1,2)
def forward(self, x): return self.lin(torch.atan2(x[:,:1], x[:,1:2].abs()+1e-8))
model = M()
opt = torch.optim.SGD(model.parameters(), lr=0.001)
target = torch.randn(4,2)
healthy = lambda s: (torch.randn(4,2), target)
def bug_fn(s):
x = torch.randn(4,2)
if s >= 2: x[:,1] = 0.0
return x, target
fixed = lambda s: (torch.randn(4,2).abs()+1e-6, target)
return model, opt, healthy, bug_fn, fixed, "gradient_explosion"
def make_bug009():
"""SDPA attention with zero input"""
class M(nn.Module):
def __init__(self): super().__init__(); self.lin=nn.Linear(8,2)
def forward(self, x):
B,H,S,D = x.shape[0],2,4,4 # B*H*D = 32
q=x[:,:H*S*D].view(B,H,S,D); k=q; v=q
mask=torch.triu(torch.full((S,S),float('-inf')),diagonal=1)
o=F.scaled_dot_product_attention(q,k,v,attn_mask=mask)
return self.lin(o.mean(dim=(1,2)))
model = M()
opt = torch.optim.SGD(model.parameters(), lr=0.001)
target = torch.randn(4,2)
healthy = lambda s: (torch.randn(4,32), target) # B*H*S*D=32
bug = lambda s: (torch.zeros(4,32), target)
fixed = lambda s: (torch.randn(4,32)*0.01+1e-4, target)
return model, opt, healthy, bug, fixed, "data_anomaly"
def make_bug010():
"""Inductor quantile tied values"""
class M(nn.Module):
def __init__(self): super().__init__(); self.lin=nn.Linear(3,2)
def forward(self, x):
q=torch.quantile(x,torch.tensor([0.25,0.5,0.75]),dim=1)
return self.lin(q.permute(1,0))
model = M()
opt = torch.optim.SGD(model.parameters(), lr=0.001)
target = torch.randn(3,2) # batch=3 to match quantile output
healthy = lambda s: (torch.randn(3,5), target)
bug = lambda s: (torch.ones(3,5)*3.0, target)
fixed = lambda s: (torch.randn(3,5)*0.1+torch.arange(5)*0.1, target)
return model, opt, healthy, bug, fixed, "gradient_explosion"
# ============================================================
# VALIDATION RUNNER
# ============================================================
print("=" * 70)
print("NEURALSUITE DEFINITIVE VALIDATION")
print("=" * 70)
bugs = [
("BUG-001", make_bug001),
("BUG-003", make_bug003),
("BUG-005", make_bug005),
("BUG-006", make_bug006),
("BUG-007", make_bug007),
("BUG-008", make_bug008),
("BUG-009", make_bug009),
("BUG-010", make_bug010),
]
results = {}
for bug_id, make_fn in bugs:
print(f"\n{'='*50}")
print(f" {bug_id}")
print(f"{'='*50}")
try:
model, opt, healthy_fn, bug_fn, fixed_fn, category = make_fn()
# Phase 1: Healthy (5 steps)
model_h = type(model)(); model_h.load_state_dict(model.state_dict())
opt_h = torch.optim.SGD(model_h.parameters(), lr=0.001)
anomalies_h, losses_h, _ = run_phase(model_h, opt_h, healthy_fn, 5)
# Phase 2: Bug injected (5 steps: 2 healthy, 3 bug)
model_b = type(model)(); model_b.load_state_dict(model.state_dict())
opt_b = torch.optim.SGD(model_b.parameters(), lr=0.001)
# Steps 0-1: healthy
a1, l1, _ = run_phase(model_b, opt_b, healthy_fn, 2)
# Steps 2-4: bug
a2, l2, dbg_b = run_phase(model_b, opt_b, bug_fn, 3)
anomalies_bug = a2 # anomalies during bug phase only
# Phase 3: Fixed (5 steps)
model_f = type(model)(); model_f.load_state_dict(model.state_dict())
opt_f = torch.optim.SGD(model_f.parameters(), lr=0.001)
a0, _, _ = run_phase(model_f, opt_f, healthy_fn, 2) # warmup
anomalies_f, losses_f, _ = run_phase(model_f, opt_f, fixed_fn, 3)
healthy_ok = len(anomalies_h) <= 2 # allow minor training noise
bug_detected = len(anomalies_bug) > len(anomalies_h) # must exceed baseline
fix_ok = len(anomalies_f) <= len(anomalies_h) # must return to baseline
results[bug_id] = {
"healthy_anomalies": len(anomalies_h),
"bug_anomalies": len(anomalies_bug),
"fix_anomalies": len(anomalies_f),
"healthy_ok": healthy_ok,
"bug_detected": bug_detected,
"fix_ok": fix_ok,
"category": category,
"anomaly_types_bug": list(set(e["event_type"] for e in anomalies_bug)),
"anomaly_layers_bug": list(set(e.get("layer_name","?") for e in anomalies_bug)),
}
status = "PASS" if (healthy_ok and bug_detected and fix_ok) else "PARTIAL"
print(f" Healthy: {len(anomalies_h)} anomalies [{ 'OK' if healthy_ok else 'NOISY' }]")
print(f" Bug: {len(anomalies_bug)} anomalies [{ 'DETECTED' if bug_detected else 'SAME' }] (baseline={len(anomalies_h)})")
print(f" Fix: {len(anomalies_f)} anomalies [{ 'RESOLVED' if fix_ok else 'ABOVE' }] (baseline={len(anomalies_h)})")
print(f" Status: {status}")
except Exception as e:
print(f" [SKIP] {e}")
results[bug_id] = {"error": str(e)}
# ============================================================
# REPORT
# ============================================================
print(f"\n{'='*70}")
print("VALIDATION REPORT")
print(f"{'='*70}")
total = len([r for r in results.values() if "error" not in r])
detected = sum(1 for r in results.values() if r.get("bug_detected"))
healthy_clean = sum(1 for r in results.values() if r.get("healthy_ok"))
fixed_clean = sum(1 for r in results.values() if r.get("fix_ok"))
print(f"\n Bugs tested: {total}")
print(f" Detected: {detected}/{total} ({100*detected//total if total else 0}%)")
print(f" No false pos: {healthy_clean}/{total}")
print(f" Fix resolves: {fixed_clean}/{total}")
print(f"\n Per-bug breakdown:")
for bug_id, r in results.items():
if "error" in r:
print(f" {bug_id}: SKIP ({r['error'][:60]})")
else:
flags = []
if r["healthy_ok"]: flags.append("NO_FALSE_POS")
if r["bug_detected"]: flags.append("DETECTED")
if r["fix_ok"]: flags.append("RESOLVED")
print(f" {bug_id} ({r['category']}): {', '.join(flags) if flags else 'FAILED'}")
print(f" healthy={r['healthy_anomalies']} bug={r['bug_anomalies']} fix={r['fix_anomalies']}")
print(f" types: {r.get('anomaly_types_bug', [])}")
# Final verdict
print(f"\n{'='*70}")
if total > 0:
detection_rate = 100 * detected // total
false_pos_rate = 100 * (total - healthy_clean) // total
fix_rate = 100 * fixed_clean // total
print(f" Detection rate (above baseline): {detection_rate}%")
print(f" Acceptable baseline noise: {100 - false_pos_rate}%")
print(f" Fix returns to baseline: {fix_rate}%")
if detection_rate >= 75:
print(f"\n VERDICT: NeuralDBG detection is CAUSAL and MEASURABLE.")
print(f" Bugs consistently produce MORE anomalies than healthy baseline.")
else:
print(f"\n VERDICT: More tuning needed.")
else:
print(f" No bugs could be tested.")
print(f"{'='*70}")