forked from NightlyTwo58/QuantAdv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuantAdv.py
More file actions
807 lines (699 loc) · 30.3 KB
/
Copy pathQuantAdv.py
File metadata and controls
807 lines (699 loc) · 30.3 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#!/usr/bin/env python
# coding: utf-8
import importlib.util
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as T
import numpy as np
import pandas as pd
import copy
import os
import json
import traceback
import sys
import matplotlib.pyplot as plt
import seaborn as sns
import torchattacks
from autoattack import AutoAttack
device = "cuda" if torch.cuda.is_available() else "cpu"
print("device:", device)
DATA_DIR = "data"
os.makedirs(DATA_DIR, exist_ok=True)
RESULTS_CSV = os.path.join(DATA_DIR, "results.csv")
SWEEP_CSV = os.path.join(DATA_DIR, "results_sweep.csv")
PLOT_PNG = os.path.join(DATA_DIR, "accuracy_plot.png")
CIFAR10_ROOT = os.environ.get("CIFAR10_ROOT", "./")
SEEDS = [0, 1, 2]
"""
Archived non-threaded single Python invocation analysis of metrics of attacks per model and eplison
"""
def ablation_csv_path(model_name):
return os.path.join(DATA_DIR, f"ablation_{model_name}.csv")
def layerwise_csv_path(model_name):
return os.path.join(DATA_DIR, f"layerwise_{model_name}.csv")
def trajectory_json_path(model_name):
return os.path.join(DATA_DIR, f"trajectory_{model_name}.json")
# weight-only vs activation-only vs both quantization ablation
def component_ablation_csv_path(model_name):
return os.path.join(DATA_DIR, f"component_ablation_{model_name}.csv")
missing = [pkg for pkg in ("torchattacks", "autoattack") if importlib.util.find_spec(pkg) is None]
if missing:
raise ImportError(f"Missing packages: {missing}.\nInstall via: pip install -r requirements.txt")
print("All required packages are available.")
expected = os.path.join(CIFAR10_ROOT, "cifar-10-batches-py")
if not os.path.isdir(expected):
raise FileNotFoundError(f"Expected extracted CIFAR-10 at {expected!r}")
def get_dataloaders(batch_size=100, eval_n=500, finetune_n=4000):
transform_train = T.Compose([
T.RandomCrop(32, padding=4),
T.RandomHorizontalFlip(),
T.ToTensor(),
T.Normalize(mean=(0.4914, 0.4822, 0.4465), std=(0.2023, 0.1994, 0.2010))
])
transform_test = T.Compose([
T.ToTensor(),
T.Normalize(mean=(0.4914, 0.4822, 0.4465), std=(0.2023, 0.1994, 0.2010))
])
train_full = torchvision.datasets.CIFAR10(root=CIFAR10_ROOT, train=True, download=False, transform=transform_train)
test_full = torchvision.datasets.CIFAR10(root=CIFAR10_ROOT, train=False, download=False, transform=transform_test)
finetune_subset = torch.utils.data.Subset(train_full, list(range(finetune_n)))
eval_subset = torch.utils.data.Subset(test_full, list(range(eval_n)))
workers = min(4, os.cpu_count() or 1)
finetune_loader = torch.utils.data.DataLoader(
finetune_subset, batch_size=batch_size, shuffle=True, num_workers=workers, pin_memory=True
)
eval_loader = torch.utils.data.DataLoader(
eval_subset, batch_size=100, shuffle=False, num_workers=workers, pin_memory=True
)
return finetune_loader, eval_loader
PRETRAINED_NAMES = {
"ResNet20": "cifar10_resnet20",
"ResNet56": "cifar10_resnet56",
"MobileNetV2": "cifar10_mobilenetv2_x1_0",
"VGG16_BN": "cifar10_vgg16_bn",
"ShuffleNetV2": "cifar10_shufflenetv2_x1_0",
"RepVGG_A0": "cifar10_repvgg_a0"
}
def load_pretrained(arch_key):
hub_name = PRETRAINED_NAMES[arch_key]
model = torch.hub.load("chenyaofo/pytorch-cifar-models", hub_name, pretrained=True)
return model.to(device).eval()
def sanity_check_accuracy(model, loader):
model.eval()
correct, total = 0, 0
with torch.no_grad():
for x, y in loader:
x, y = x.to(device), y.to(device)
pred = model(x).argmax(dim=1)
correct += (pred == y).sum().item()
total += y.size(0)
return correct / total
class FakeQuantSTE(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return torch.round(x)
@staticmethod
def backward(ctx, grad_output):
return grad_output
def quantize_tensor(t, bits, use_ste):
if bits is None:
return t
qmax = 2 ** (bits - 1) - 1
scale = torch.clamp(t.detach().abs().max() / qmax, min=1e-8)
t_scaled = t / scale
t_round = FakeQuantSTE.apply(t_scaled) if use_ste else torch.round(t_scaled)
t_round = torch.clamp(t_round, -qmax - 1, qmax)
return t_round * scale
class QuantConv2d(nn.Conv2d):
def forward(self, x):
bits = getattr(self, 'bits', None)
use_ste = getattr(self, 'use_ste', False)
quant_weight = getattr(self, 'quant_weight', True)
quant_act = getattr(self, 'quant_act', True)
w = quantize_tensor(self.weight, bits, use_ste) if quant_weight else self.weight
out = self._conv_forward(x, w, self.bias)
if quant_act:
out = quantize_tensor(out, bits, use_ste)
return out
class QuantLinear(nn.Linear):
def forward(self, x):
bits = getattr(self, 'bits', None)
use_ste = getattr(self, 'use_ste', False)
quant_weight = getattr(self, 'quant_weight', True)
quant_act = getattr(self, 'quant_act', True)
w = quantize_tensor(self.weight, bits, use_ste) if quant_weight else self.weight
out = F.linear(x, w, self.bias)
if quant_act:
out = quantize_tensor(out, bits, use_ste)
return out
def _to_quant_module(mod, bits, quant_weight=True, quant_act=True):
if isinstance(mod, nn.Conv2d):
new = QuantConv2d(mod.in_channels, mod.out_channels, mod.kernel_size,
mod.stride, mod.padding, mod.dilation, mod.groups,
mod.bias is not None, mod.padding_mode)
new.weight = mod.weight
if mod.bias is not None:
new.bias = mod.bias
new.bits, new.use_ste, new.quant_weight, new.quant_act = bits, False, quant_weight, quant_act
return new
if isinstance(mod, nn.Linear):
new = QuantLinear(mod.in_features, mod.out_features, bias=mod.bias is not None)
new.weight = mod.weight
if mod.bias is not None:
new.bias = mod.bias
new.bits, new.use_ste, new.quant_weight, new.quant_act = bits, False, quant_weight, quant_act
return new
return None
def _replace_recursive(module, bits, quant_weight=True, quant_act=True):
for name, child in list(module.named_children()):
nc = _to_quant_module(child, bits, quant_weight, quant_act)
if nc is not None:
setattr(module, name, nc)
else:
_replace_recursive(child, bits, quant_weight, quant_act)
def convert_to_quant(model, bits, quant_weight=True, quant_act=True):
m = copy.deepcopy(model)
_replace_recursive(m, bits, quant_weight, quant_act)
return m
def set_ste_mode(model, flag):
for mod in model.modules():
if isinstance(mod, (QuantConv2d, QuantLinear)):
mod.use_ste = flag
def count_quant_layers(model):
return sum(1 for m in model.modules() if isinstance(m, (QuantConv2d, QuantLinear)))
# flip weight/activation quantization on an already-built quantized model
# without rebuilding it, so one model object can be reused for all three
# ablation configs instead of re-running convert_to_quant/QAT.
def set_quant_components(model, quant_weight, quant_act):
for mod in model.modules():
if isinstance(mod, (QuantConv2d, QuantLinear)):
mod.quant_weight = quant_weight
mod.quant_act = quant_act
def prepare_qat(fp32_model, bits, finetune_loader, epochs=3, lr=1e-3):
m = convert_to_quant(fp32_model, bits, quant_weight=True, quant_act=True)
if torch.cuda.device_count() > 1:
m = nn.DataParallel(m)
set_ste_mode(m, True)
m.train()
opt = torch.optim.SGD(m.parameters(), lr=lr, momentum=0.9, weight_decay=5e-4)
for epoch in range(epochs):
running = 0.0
for x, y in finetune_loader:
x, y = x.to(device), y.to(device)
opt.zero_grad()
loss = F.cross_entropy(m(x), y)
loss.backward()
opt.step()
running += loss.item()
print(f" QAT epoch {epoch+1}/{epochs} avg loss {running/len(finetune_loader):.4f}")
set_ste_mode(m, False)
return m.eval()
CIFAR_MEAN = torch.tensor([0.4914, 0.4822, 0.4465]).view(1, 3, 1, 1)
CIFAR_STD = torch.tensor([0.2023, 0.1994, 0.2010]).view(1, 3, 1, 1)
CLIP_MIN = ((0.0 - CIFAR_MEAN) / CIFAR_STD)
CLIP_MAX = ((1.0 - CIFAR_MEAN) / CIFAR_STD)
def run_fgsm_pgd(model, loader, eps=8/255, seeds=SEEDS):
model.eval()
fgsm = torchattacks.FGSM(model, eps=eps)
out = {}
correct, total = 0, 0
for x, y in loader:
x, y = x.to(device), y.to(device)
x_adv = fgsm(x, y)
with torch.no_grad():
pred = model(x_adv).argmax(dim=1)
correct += (pred == y).sum().item()
total += y.size(0)
out["FGSM"] = correct / total
pgd_accs = []
for seed in seeds:
torch.manual_seed(seed)
pgd = torchattacks.PGD(model, eps=eps, alpha=2/255, steps=20, random_start=True)
correct, total = 0, 0
for x, y in loader:
x, y = x.to(device), y.to(device)
x_adv = pgd(x, y)
with torch.no_grad():
pred = model(x_adv).argmax(dim=1)
correct += (pred == y).sum().item()
total += y.size(0)
pgd_accs.append(correct / total)
out["PGD"] = float(np.mean(pgd_accs))
out["PGD_mean"] = float(np.mean(pgd_accs))
out["PGD_std"] = float(np.std(pgd_accs))
return out
def run_autoattack(model, loader, eps=8/255):
model.eval()
adversary = AutoAttack(model, norm="Linf", eps=eps, version="custom", device=device, verbose=False)
adversary.attacks_to_run = ["apgd-ce", "apgd-t"]
correct, total = 0, 0
for x, y in loader:
x, y = x.to(device, non_blocking=True), y.to(device, non_blocking=True)
x_adv = adversary.run_standard_evaluation(x, y, bs=x.size(0))
with torch.no_grad():
pred = model(x_adv).argmax(1)
correct += (pred == y).sum().item()
total += y.size(0)
return correct / total
def transfer_attack(source_model, target_model, loader, eps=8/255):
pgd = torchattacks.PGD(source_model, eps=eps, alpha=2/255, steps=20, random_start=True)
correct, total = 0, 0
for x, y in loader:
x, y = x.to(device), y.to(device)
x_adv = pgd(x, y)
with torch.no_grad():
pred = target_model(x_adv).argmax(dim=1)
correct += (pred == y).sum().item()
total += y.size(0)
return correct / total
def bpda_pgd_attack(model, x, y, eps=8/255, alpha=2/255, steps=20):
set_ste_mode(model, True)
clip_min = CLIP_MIN.to(device)
clip_max = CLIP_MAX.to(device)
x_adv = x.clone().detach() + torch.empty_like(x).uniform_(-eps, eps)
x_adv = torch.max(torch.min(x_adv, clip_max), clip_min).detach()
for _ in range(steps):
x_adv.requires_grad_(True)
loss = F.cross_entropy(model(x_adv), y)
grad = torch.autograd.grad(loss, x_adv)[0]
x_adv = x_adv.detach() + alpha * grad.sign()
x_adv = torch.min(torch.max(x_adv, x - eps), x + eps)
x_adv = torch.max(torch.min(x_adv, clip_max), clip_min).detach()
set_ste_mode(model, False)
return x_adv.detach()
def _run_bpda_once(model, loader, eps, n_restarts):
correct_masks = []
for x, y in loader:
x, y = x.to(device), y.to(device)
worst_correct = torch.ones(y.size(0), dtype=torch.bool, device=device)
for _ in range(n_restarts):
x_adv = bpda_pgd_attack(model, x, y, eps=eps)
with torch.no_grad():
pred = model(x_adv).argmax(dim=1)
worst_correct &= (pred == y)
correct_masks.append(worst_correct)
all_correct = torch.cat(correct_masks)
return all_correct.float().mean().item()
def run_bpda(model, loader, eps=8/255, n_restarts=1, seeds=SEEDS):
"""
Runs the whole worst-case-over-n_restarts procedure once per seed and
reports mean/std across seeds, in addition to the original scalar
(mean of seeds) for backward-compat.
"""
accs = []
for seed in seeds:
torch.manual_seed(seed)
accs.append(_run_bpda_once(model, loader, eps, n_restarts))
return {
"BPDA_PGD": float(np.mean(accs)),
"BPDA_PGD_mean": float(np.mean(accs)),
"BPDA_PGD_std": float(np.std(accs)),
}
def gradient_diagnostics(model, loader, fp32_ref=None, max_batches=5):
set_ste_mode(model, False)
frac_zero_hard, norm_hard = [], []
frac_zero_ste, norm_ste = [], []
cos_sims = []
for bi, (x, y) in enumerate(loader):
if bi >= max_batches:
break
x, y = x.to(device), y.to(device)
set_ste_mode(model, False)
x_in = x.clone().requires_grad_(True)
loss = F.cross_entropy(model(x_in), y)
g_hard = torch.autograd.grad(loss, x_in)[0].flatten()
frac_zero_hard.append((g_hard.abs() < 1e-8).float().mean().item())
norm_hard.append(g_hard.norm().item())
set_ste_mode(model, True)
x_in2 = x.clone().requires_grad_(True)
loss2 = F.cross_entropy(model(x_in2), y)
g_ste = torch.autograd.grad(loss2, x_in2)[0].flatten()
set_ste_mode(model, False)
frac_zero_ste.append((g_ste.abs() < 1e-8).float().mean().item())
norm_ste.append(g_ste.norm().item())
if fp32_ref is not None:
fp32_ref.eval()
x_ref = x.clone().requires_grad_(True)
loss_ref = F.cross_entropy(fp32_ref(x_ref), y)
g_ref = torch.autograd.grad(loss_ref, x_ref)[0].flatten()
cos_sims.append(F.cosine_similarity(g_ste.unsqueeze(0), g_ref.unsqueeze(0)).item())
diagnostics = {
"frac_zero_grad_hard": float(np.mean(frac_zero_hard)),
"frac_zero_grad_ste": float(np.mean(frac_zero_ste)),
"grad_norm_hard": float(np.mean(norm_hard)),
"grad_norm_ste": float(np.mean(norm_ste)),
}
if cos_sims:
diagnostics["grad_cosine_sim_with_FP32"] = float(np.mean(cos_sims))
return diagnostics
def random_noise_attack(model, loader, eps=8/255, n_restarts=1, seed=None):
if seed is not None:
torch.manual_seed(seed)
model.eval()
clip_min, clip_max = CLIP_MIN.to(device), CLIP_MAX.to(device)
correct, total = 0, 0
with torch.no_grad():
for x, y in loader:
x, y = x.to(device), y.to(device)
worst_correct = torch.ones(y.size(0), dtype=torch.bool, device=device)
for _ in range(n_restarts):
noise = torch.empty_like(x).uniform_(-eps, eps)
x_adv = torch.max(torch.min(x + noise, clip_max), clip_min)
pred = model(x_adv).argmax(dim=1)
worst_correct &= (pred == y)
correct += worst_correct.sum().item()
total += y.size(0)
return correct / total
def run_random_noise_seeded(model, loader, eps=8/255, seeds=SEEDS):
"""Seed-averaged wrapper around random_noise_attack."""
accs = [random_noise_attack(model, loader, eps=eps, seed=s) for s in seeds]
return {
"Random_Noise": float(np.mean(accs)),
"Random_Noise_mean": float(np.mean(accs)),
"Random_Noise_std": float(np.std(accs)),
}
def pgd_steps_ablation(model, loader, eps=8/255, step_list=(0, 1, 2, 5, 10, 20, 50)):
model.eval()
out = {}
for steps in step_list:
if steps == 0:
acc = random_noise_attack(model, loader, eps=eps, seed=0)
else:
pgd = torchattacks.PGD(model, eps=eps, alpha=2/255, steps=steps, random_start=True)
correct, total = 0, 0
for x, y in loader:
x, y = x.to(device), y.to(device)
x_adv = pgd(x, y)
with torch.no_grad():
pred = model(x_adv).argmax(dim=1)
correct += (pred == y).sum().item()
total += y.size(0)
acc = correct / total
out[steps] = acc
return out
def pgd_trajectory_diagnostics(model, loader, eps=8/255, alpha=2/255, steps=20, max_batches=5):
model.eval()
clip_min, clip_max = CLIP_MIN.to(device), CLIP_MAX.to(device)
step_grad_norms = [0.0] * steps
step_movement = [0.0] * steps
n_batches = 0
for bi, (x, y) in enumerate(loader):
if bi >= max_batches:
break
x, y = x.to(device), y.to(device)
noise = torch.empty_like(x).uniform_(-eps, eps)
x_start = torch.max(torch.min(x + noise, clip_max), clip_min).detach()
x_adv = x_start.clone()
for s in range(steps):
x_adv.requires_grad_(True)
loss = F.cross_entropy(model(x_adv), y)
grad = torch.autograd.grad(loss, x_adv)[0]
step_grad_norms[s] += grad.flatten(1).norm(dim=1).mean().item()
x_adv = x_adv.detach() + alpha * grad.sign()
x_adv = torch.min(torch.max(x_adv, x - eps), x + eps)
x_adv = torch.max(torch.min(x_adv, clip_max), clip_min).detach()
step_movement[s] += (x_adv - x_start).flatten(1).abs().max(dim=1).values.mean().item()
n_batches += 1
return {
"grad_norm_per_step": [g / n_batches for g in step_grad_norms],
"movement_from_random_start_per_step": [m / n_batches for m in step_movement],
}
def layerwise_grad_profile(model, loader, use_ste, max_batches=3):
quant_layers = [(n, m) for n, m in model.named_modules() if isinstance(m, (QuantConv2d, QuantLinear))]
norms = {n: [] for n, _ in quant_layers}
handles = []
def make_hook(name):
def hook(module, grad_input, grad_output):
gi = grad_input[0]
if gi is not None:
norms[name].append(gi.flatten(1).norm(dim=1).mean().item())
return hook
for n, m in quant_layers:
handles.append(m.register_full_backward_hook(make_hook(n)))
set_ste_mode(model, use_ste)
model.eval()
for bi, (x, y) in enumerate(loader):
if bi >= max_batches:
break
x, y = x.to(device), y.to(device)
x = x.clone().requires_grad_(True)
loss = F.cross_entropy(model(x), y)
model.zero_grad(set_to_none=True)
loss.backward()
for h in handles:
h.remove()
set_ste_mode(model, False)
ordered_names = [n for n, _ in quant_layers]
return {n: (float(np.mean(norms[n])) if len(norms[n]) else None) for n in ordered_names}
def staircase_diagnostic(model, loader, radius=1/255, n_points=40):
model.eval()
x, y = next(iter(loader))
x = x.to(device)
direction = torch.randn_like(x)
flat_norm = direction.flatten(1).norm(dim=1).view(-1, *([1] * (x.dim() - 1)))
direction = direction / flat_norm
clip_min, clip_max = CLIP_MIN.to(device), CLIP_MAX.to(device)
with torch.no_grad():
prev_logits = model(x)
plateau_hits = 0.0
for i in range(1, n_points + 1):
step = x + direction * (radius * i / n_points)
step = torch.max(torch.min(step, clip_max), clip_min)
logits = model(step)
plateau_hits += (logits == prev_logits).all(dim=1).float().mean().item()
prev_logits = logits
return {"plateau_fraction": plateau_hits / n_points}
# weight-only vs activation-only vs both quantization ablation.
# Cheap by design -- reuses the already-built quantized model, just flips
# quant_weight/quant_act flags in place, and only computes clean_acc + a
# single-seed 20-step PGD + frac_zero_grad_hard per config (not the full
# AutoAttack/BPDA/trajectory suite). Restores the model to (True, True)
# (its original state) before returning.
def run_quant_component_ablation(model, loader, name, eps=8/255):
configs = [
("weight_only", True, False),
("act_only", False, True),
("both", True, True),
]
rows = []
for label, qw, qa in configs:
set_quant_components(model, qw, qa)
clean_acc = sanity_check_accuracy(model, loader)
torch.manual_seed(0)
pgd = torchattacks.PGD(model, eps=eps, alpha=2/255, steps=20, random_start=True)
correct, total = 0, 0
for x, y in loader:
x, y = x.to(device), y.to(device)
x_adv = pgd(x, y)
with torch.no_grad():
pred = model(x_adv).argmax(dim=1)
correct += (pred == y).sum().item()
total += y.size(0)
pgd_acc = correct / total
x, y = next(iter(loader))
x, y = x.to(device), y.to(device)
x_in = x.clone().requires_grad_(True)
loss = F.cross_entropy(model(x_in), y)
g_hard = torch.autograd.grad(loss, x_in)[0].flatten()
frac_zero = (g_hard.abs() < 1e-8).float().mean().item()
rows.append({
"model": name, "config": label,
"quant_weight": qw, "quant_act": qa,
"clean_acc": clean_acc, "PGD_acc": pgd_acc,
"frac_zero_grad_hard": frac_zero,
})
# restore original (both quantized) state
set_quant_components(model, True, True)
return rows
def run_suite(model, loader, name, fp32_ref=None, eps=8/255):
model.eval()
results = {"model": name}
try:
results["clean_acc"] = sanity_check_accuracy(model, loader)
except Exception as e:
print(f" [WARN] clean_acc failed for {name}: {e}")
results["clean_acc"] = None
try:
results.update(run_fgsm_pgd(model, loader, eps=eps))
except Exception as e:
print(f" [WARN] FGSM/PGD failed for {name}: {e}")
results["FGSM"] = results.get("FGSM", None)
results["PGD"] = results.get("PGD", None)
try:
results["AutoAttack"] = run_autoattack(model, loader, eps=eps)
except Exception as e:
print(f" [WARN] AutoAttack failed for {name}: {e}")
results["AutoAttack"] = None
if fp32_ref is not None:
try:
results["Transfer_from_FP32"] = transfer_attack(fp32_ref, model, loader, eps=eps)
except Exception as e:
print(f" [WARN] transfer_attack failed for {name}: {e}")
results["Transfer_from_FP32"] = None
try:
results.update(run_random_noise_seeded(model, loader, eps=eps))
except Exception as e:
print(f" [WARN] random_noise_attack failed for {name}: {e}")
results["Random_Noise"] = None
if count_quant_layers(model) > 0:
try:
results.update(run_bpda(model, loader, eps=eps, n_restarts=5))
except Exception as e:
print(f" [WARN] BPDA failed for {name}: {e}")
results["BPDA_PGD"] = None
try:
results.update(gradient_diagnostics(model, loader, fp32_ref=fp32_ref, max_batches=5))
except Exception as e:
print(f" [WARN] gradient_diagnostics failed for {name}: {e}")
try:
results.update(staircase_diagnostic(model, loader))
except Exception as e:
print(f" [WARN] staircase_diagnostic failed for {name}: {e}")
try:
ablation = pgd_steps_ablation(model, loader, eps=eps)
pd.DataFrame([{"model": name, "steps": k, "acc": v} for k, v in ablation.items()]) \
.to_csv(ablation_csv_path(name), index=False)
except Exception as e:
print(f" [WARN] pgd_steps_ablation failed for {name}: {e}")
try:
traj = pgd_trajectory_diagnostics(model, loader, eps=eps, max_batches=5)
with open(trajectory_json_path(name), "w") as f:
json.dump(traj, f, indent=2)
except Exception as e:
print(f" [WARN] pgd_trajectory_diagnostics failed for {name}: {e}")
try:
prof_hard = layerwise_grad_profile(model, loader, use_ste=False)
prof_ste = layerwise_grad_profile(model, loader, use_ste=True)
rows = [{"model": name, "layer": n, "grad_norm_hard": prof_hard.get(n),
"grad_norm_ste": prof_ste.get(n)} for n in prof_hard]
pd.DataFrame(rows).to_csv(layerwise_csv_path(name), index=False)
except Exception as e:
print(f" [WARN] layerwise_grad_profile failed for {name}: {e}")
# weight-only / activation-only / both ablation
try:
rows = run_quant_component_ablation(model, loader, name, eps=eps)
pd.DataFrame(rows).to_csv(component_ablation_csv_path(name), index=False)
except Exception as e:
print(f" [WARN] run_quant_component_ablation failed for {name}: {e}")
return results
def run_epsilon_sweep_for_model(model, loader, name, epsilons):
rows = []
is_quant = count_quant_layers(model) > 0
for eps in epsilons:
row = {"model": name, "epsilon": eps}
try:
pgd = torchattacks.PGD(model, eps=eps, alpha=2/255, steps=20, random_start=True)
correct, total = 0, 0
for x, y in loader:
x, y = x.to(device), y.to(device)
x_adv = pgd(x, y)
with torch.no_grad():
pred = model(x_adv).argmax(dim=1)
correct += (pred == y).sum().item()
total += y.size(0)
row["PGD_acc"] = correct / total
except Exception as e:
print(f" [WARN] PGD sweep failed for {name} eps={eps:.4f}: {e}")
row["PGD_acc"] = None
try:
row["Random_Noise_acc"] = random_noise_attack(model, loader, eps=eps)
except Exception as e:
print(f" [WARN] random_noise sweep failed for {name} eps={eps:.4f}: {e}")
row["Random_Noise_acc"] = None
if is_quant:
try:
row["BPDA_acc"] = _run_bpda_once(model, loader, eps=eps, n_restarts=3)
except Exception as e:
print(f" [WARN] BPDA sweep failed for {name} eps={eps:.4f}: {e}")
row["BPDA_acc"] = None
rows.append(row)
return rows
def parallelize(model):
if torch.cuda.device_count() > 1 and not isinstance(model, nn.DataParallel):
return nn.DataParallel(model)
return model
def main():
finetune_loader, eval_loader = get_dataloaders()
model_registry = {}
for arch_key in PRETRAINED_NAMES:
print(f"\n>>> {arch_key} <<<")
try:
fp32 = load_pretrained(arch_key)
acc = sanity_check_accuracy(fp32, eval_loader)
print(f" loaded pretrained {arch_key}, clean acc: {acc:.3f}")
model_registry[f"{arch_key}_FP32"] = (fp32, None)
except Exception as e:
print(f" [FAIL] could not load {arch_key}: {e}")
traceback.print_exc()
continue
try:
int8_ptq = convert_to_quant(fp32, bits=8, quant_weight=True, quant_act=True)
model_registry[f"{arch_key}_int8_PTQ"] = (int8_ptq, fp32)
except Exception as e:
print(f" [FAIL] int8 PTQ for {arch_key}: {e}")
try:
int4_ptq = convert_to_quant(fp32, bits=4, quant_weight=True, quant_act=True)
model_registry[f"{arch_key}_int4_PTQ"] = (int4_ptq, fp32)
except Exception as e:
print(f" [FAIL] int4 PTQ for {arch_key}: {e}")
try:
int8_qat = prepare_qat(fp32, bits=8, finetune_loader=finetune_loader, epochs=3)
model_registry[f"{arch_key}_int8_QAT"] = (int8_qat, fp32)
except Exception as e:
print(f" [FAIL] int8 QAT for {arch_key}: {e}")
traceback.print_exc()
try:
int4_qat = prepare_qat(fp32, bits=4, finetune_loader=finetune_loader, epochs=3)
model_registry[f"{arch_key}_int4_QAT"] = (int4_qat, fp32)
except Exception as e:
print(f" [FAIL] int4 QAT for {arch_key}: {e}")
traceback.print_exc()
print("\nRegistry built:", list(model_registry.keys()))
for k in model_registry:
m, r = model_registry[k]
model_registry[k] = (parallelize(m), parallelize(r) if r else None)
if os.path.exists(RESULTS_CSV):
df_results = pd.read_csv(RESULTS_CSV)
done = set(df_results["model"].astype(str))
else:
df_results = pd.DataFrame(columns=["model"])
done = set()
for name, (model, ref) in list(model_registry.items()):
if name in done:
print(f"Skipping {name} (already in {RESULTS_CSV})")
continue
print(f"\nEvaluating {name} ...")
try:
res = run_suite(model, eval_loader, name, fp32_ref=ref)
except Exception as e:
print(f" [FAIL] run_suite failed for {name}: {e}")
traceback.print_exc()
res = {"model": name}
new_row = pd.DataFrame([res])
df_results = pd.concat([df_results, new_row], ignore_index=True)
df_results.to_csv(RESULTS_CSV, index=False)
print("Result:")
print(new_row.to_string(index=False))
print("-" * 100)
print("\nFinal results:")
print(df_results)
acc_cols = [c for c in ["clean_acc", "FGSM", "PGD", "AutoAttack", "Transfer_from_FP32", "Random_Noise", "BPDA_PGD"] if c in df_results.columns]
if len(acc_cols) > 0:
df_plot = df_results.melt(id_vars="model", value_vars=acc_cols, var_name="Attack", value_name="Accuracy")
plt.figure(figsize=(14, 6))
sns.barplot(data=df_plot, x="model", y="Accuracy", hue="Attack")
plt.xticks(rotation=45, ha="right")
plt.title("Model Accuracy under Various Adversarial Attacks")
plt.ylim(0, 1.0)
plt.grid(axis="y", linestyle="--", alpha=0.7)
plt.tight_layout()
plt.savefig(PLOT_PNG, dpi=300, bbox_inches="tight")
plt.show()
SWEEP_EPSILONS = [1/255, 2/255, 4/255, 8/255, 16/255]
if os.path.exists(SWEEP_CSV):
df_sweep = pd.read_csv(SWEEP_CSV)
sweep_done = set(zip(df_sweep["model"].astype(str), df_sweep["epsilon"].round(6)))
else:
df_sweep = pd.DataFrame()
sweep_done = set()
for name, (model, ref) in model_registry.items():
print(f"\nSweeping {name} ...")
pending_eps = [eps for eps in SWEEP_EPSILONS if (name, round(eps, 6)) not in sweep_done]
if not pending_eps:
print(f" Skipping {name} (already done)")
continue
try:
rows = run_epsilon_sweep_for_model(model, eval_loader, name, pending_eps)
if rows:
new_sweep = pd.DataFrame(rows)
df_sweep = pd.concat([df_sweep, new_sweep], ignore_index=True)
df_sweep.to_csv(SWEEP_CSV, index=False)
except Exception as e:
print(f" [FAIL] epsilon sweep failed for {name}: {e}")
traceback.print_exc()
print("\nEpsilon sweep completed. Results saved to", SWEEP_CSV)
print("All done.")
if __name__ == '__main__':
main()