Skip to content

VDC

VDC

Bases: Attack

VDC (Virtual Dense Connection) attack for ViTs.

From the paper: Improving the Adversarial Transferability of Vision Transformers with Virtual Dense Connection.

Parameters:

Name Type Description Default
model Module | AttackModel

The model to attack.

required
normalize Callable[[Tensor], Tensor] | None

A transform to normalize images.

None
device device | None

Device to use for tensors. Defaults to cuda if available.

None
hook_cfg str

Config used for applying hooks to the model. Supported values: vit_base_patch16_224, deit_base_distilled_patch16_224, pit_b_224, visformer_small.

''
eps float

The maximum perturbation. Defaults to 8/255.

8 / 255
steps int

Number of steps. Defaults to 10.

10
alpha float | None

Step size, eps / steps if None. Defaults to None.

None
decay float

Momentum decay factor. Defaults to 1.0.

1.0
sample_num_batches int

Number of batches to sample. Defaults to 130.

130
clip_min float

Minimum value for clipping. Defaults to 0.0.

0.0
clip_max float

Maximum value for clipping. Defaults to 1.0.

1.0
targeted bool

Targeted attack if True. Defaults to False.

False
Source code in torchattack/vdc.py
 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
@register_attack(category='GRADIENT_VIT')
class VDC(Attack):
    """VDC (Virtual Dense Connection) attack for ViTs.

    > From the paper: [Improving the Adversarial Transferability of Vision Transformers
    with Virtual Dense Connection](https://ojs.aaai.org/index.php/AAAI/article/view/28541).

    Args:
        model: The model to attack.
        normalize: A transform to normalize images.
        device: Device to use for tensors. Defaults to cuda if available.
        hook_cfg: Config used for applying hooks to the model. Supported values:
            `vit_base_patch16_224`, `deit_base_distilled_patch16_224`, `pit_b_224`,
            `visformer_small`.
        eps: The maximum perturbation. Defaults to 8/255.
        steps: Number of steps. Defaults to 10.
        alpha: Step size, `eps / steps` if None. Defaults to None.
        decay: Momentum decay factor. Defaults to 1.0.
        sample_num_batches: Number of batches to sample. Defaults to 130.
        clip_min: Minimum value for clipping. Defaults to 0.0.
        clip_max: Maximum value for clipping. Defaults to 1.0.
        targeted: Targeted attack if True. Defaults to False.
    """

    def __init__(
        self,
        model: nn.Module | AttackModel,
        normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
        device: torch.device | None = None,
        hook_cfg: str = '',
        eps: float = 8 / 255,
        steps: int = 10,
        alpha: float | None = None,
        decay: float = 1.0,
        sample_num_batches: int = 130,
        # lambd: float = 0.1,
        clip_min: float = 0.0,
        clip_max: float = 1.0,
        targeted: bool = False,
    ):
        # Surrogate ViT for VDC must be `timm` models or models that have the same
        # structure and same implementation/definition as `timm` models.
        super().__init__(model, normalize, device)

        if hook_cfg:
            # Explicit config name takes precedence over inferred model.model_name
            self.hook_cfg = hook_cfg
        elif isinstance(model, AttackModel):
            # If model is initialized via `torchattack.AttackModel`, the model_name
            # is automatically attached to the model during instantiation.
            self.hook_cfg = model.model_name

        self.eps = eps
        self.steps = steps
        self.alpha = alpha
        self.decay = decay

        self.sample_num_batches = sample_num_batches
        # self.lambd = lambd

        # Default (3, 224, 224) image with ViT-B/16 16x16 patches
        self.max_num_batches = int((224 / 16) ** 2)
        self.crop_length = 16

        self.clip_min = clip_min
        self.clip_max = clip_max
        self.targeted = targeted
        self.lossfn = nn.CrossEntropyLoss()

        # Global hooks and attack stage state for VDC
        self.stage: list[torch.Tensor] = []
        self.hooks: list[torch.utils.hooks.RemovableHandle] = []

        assert self.sample_num_batches <= self.max_num_batches

    def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
        """Perform VDC on a batch of images.

        Args:
            x: A batch of images. Shape: (N, C, H, W).
            y: A batch of labels. Shape: (N).

        Returns:
            The perturbed images if successful. Shape: (N, C, H, W).
        """

        g = torch.zeros_like(x)
        delta = torch.zeros_like(x, requires_grad=True)

        # If alpha is not given, set to eps / steps
        if self.alpha is None:
            self.alpha = self.eps / self.steps

        class GradientRecorder:
            """Gradient recorder for attention and MLP blocks."""

            def __init__(self) -> None:
                self.grad_rec: list[torch.Tensor] = []
                self.grad_add: list[torch.Tensor] = []

        # Perform VDC
        for _ in range(self.steps):
            # Initialize gradient recorders
            self.attn_rec = GradientRecorder()
            self.mlp_rec = GradientRecorder()

            # Stage 1: Record gradients
            self.current_mlp = 0
            self.current_attn = 0
            self._register_model_hooks(add_grad_mode=False)

            # Compute loss
            outs = self.model(self.normalize(x + delta))
            loss = self.lossfn(outs, y)

            if self.targeted:
                loss = -loss

            # Compute gradient
            loss.backward()

            if delta.grad is None:
                continue

            # Zero out gradient
            delta.grad.detach_()
            delta.grad.zero_()

            for hook in self.hooks:
                hook.remove()

            # Stage 2: Update gradients by adding recorded gradients
            self.current_mlp = 0
            self.current_attn = 0
            self._register_model_hooks(add_grad_mode=True)

            # Compute loss 2nd time
            outs = self.model(self.normalize(x + delta))
            loss = self.lossfn(outs, y)

            if self.targeted:
                loss = -loss

            # Compute gradient 2nd time
            loss.backward()

            # Apply momentum term
            g = self.decay * g + delta.grad / torch.mean(
                torch.abs(delta.grad), dim=(1, 2, 3), keepdim=True
            )

            # Update delta
            delta.data = delta.data + self.alpha * g.sign()
            delta.data = torch.clamp(delta.data, -self.eps, self.eps)
            delta.data = torch.clamp(x + delta.data, self.clip_min, self.clip_max) - x

            # Zero out gradient
            delta.grad.detach_()
            delta.grad.zero_()

            for hook in self.hooks:
                hook.remove()

        return x + delta

    def _register_model_hooks(self, add_grad_mode: bool = False) -> None:
        """Register hooks to either record or add gradients during the backward pass.

        Args:
            add_grad_mode: If False, register hooks to record gradients. If True,
                register hooks to modify the gradients by adding pre-recorded gradients
                during the backward pass.
        """

        def mlp_record_vit_stage(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_mlp))
            if self.current_mlp == 0:
                grad_add = torch.zeros_like(grad_record)
                grad_add[:, 0, :] = self.norm_list[:, 0, :] * 0.1 * (0.5)
                self.mlp_rec.grad_add.append(grad_add)
                self.mlp_rec.grad_rec.append(grad_record + grad_add)
            else:
                self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
                self.mlp_rec.grad_rec.append(total_mlp)
            self.current_mlp += 1
            return (out_grad, grad_in[1], grad_in[2])

        def mlp_add_vit(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            # grad_record = grad_in[0].data.cpu().numpy()
            mask = torch.ones_like(grad_in[0]) * gamma
            # mask_0 = torch.zeros_like(grad_in[0])
            out_grad = mask * grad_in[0][:]
            # out_grad = torch.where(
            #     grad_in[0][:] > 0, mask * grad_in[0][:], mask_0 * grad_in[0][:]
            # )
            og = self.mlp_rec.grad_add[self.current_mlp]
            out_grad += og.clone().detach().to(self.device)
            self.current_mlp += 1
            return (out_grad, grad_in[1], grad_in[2])

        def attn_record_vit_stage(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_attn))
            if self.current_attn == 0:
                self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                self.attn_rec.grad_rec.append(grad_record)
            else:
                self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                total_attn = self.attn_rec.grad_rec[-1] + grad_record
                self.attn_rec.grad_rec.append(total_attn)

            self.current_attn += 1
            return (out_grad,)

        def attn_add_vit(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            # grad_record = grad_in[0].data.cpu().numpy()
            # mask_0 = torch.zeros_like(grad_in[0])
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            # out_grad = torch.where(
            #     grad_in[0][:] > 0, mask * grad_in[0][:], mask_0 * grad_in[0][:]
            # )
            og = self.attn_rec.grad_add[self.current_attn]
            out_grad += og.clone().detach().to(self.device)
            self.current_attn += 1
            return (out_grad,)

        def norm_record_vit(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            self.norm_list = grad_in[0].data
            return grad_in

        # pit
        def pool_record_pit(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            grad_add = grad_in[0].data
            b, c, h, w = grad_add.shape
            grad_add = grad_add.reshape((b, c, h * w)).transpose(1, 2)
            self.stage.append(grad_add)
            return grad_in

        def mlp_record_pit_stage(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            if self.current_mlp < 4:
                grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_mlp))
                if self.current_mlp == 0:
                    grad_add = torch.zeros_like(grad_record)
                    grad_add[:, 0, :] = self.norm_list[:, 0, :] * 0.03 * (0.5)
                    self.mlp_rec.grad_add.append(grad_add)
                    self.mlp_rec.grad_rec.append(grad_record + grad_add)
                else:
                    self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                    total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
                    self.mlp_rec.grad_rec.append(total_mlp)
            elif self.current_mlp < 10:
                grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_mlp))
                if self.current_mlp == 4:
                    grad_add = torch.zeros_like(grad_record)
                    grad_add[:, 1:, :] = self.stage[0] * 0.03 * (0.5)
                    self.mlp_rec.grad_add.append(grad_add)
                    self.mlp_rec.grad_rec.append(grad_record + grad_add)
                else:
                    self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                    # total_mlp = self.mlp_rec.record[-1] + grad_record
                    total_mlp = self.mlp_rec.grad_rec[-1]
                    self.mlp_rec.grad_rec.append(total_mlp)
            else:
                grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_mlp))
                if self.current_mlp == 10:
                    grad_add = torch.zeros_like(grad_record)
                    grad_add[:, 1:, :] = self.stage[1] * 0.03 * (0.5)
                    self.mlp_rec.grad_add.append(grad_add)
                    self.mlp_rec.grad_rec.append(grad_record + grad_add)
                else:
                    self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                    # total_mlp = self.mlp_rec.record[-1] + grad_record
                    total_mlp = self.mlp_rec.grad_rec[-1]
                    self.mlp_rec.grad_rec.append(total_mlp)
            self.current_mlp += 1

            return (out_grad, grad_in[1], grad_in[2])

        def mlp_add_pit(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            # grad_record = grad_in[0].data.cpu().numpy()
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            og = self.mlp_rec.grad_add[self.current_mlp]
            out_grad += og.clone().detach().to(self.device)
            self.current_mlp += 1
            return (out_grad, grad_in[1], grad_in[2])

        def attn_record_pit_stage(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            if self.current_attn < 4:
                grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_attn))
                if self.current_attn == 0:
                    self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                    self.attn_rec.grad_rec.append(grad_record)
                else:
                    self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                    total_attn = self.attn_rec.grad_rec[-1] + grad_record
                    self.attn_rec.grad_rec.append(total_attn)
            elif self.current_attn < 10:
                grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_attn))
                if self.current_attn == 4:
                    self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                    self.attn_rec.grad_rec.append(grad_record)
                else:
                    self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                    # total_attn = self.attn_rec.record[-1] + grad_record
                    total_attn = self.attn_rec.grad_rec[-1]
                    self.attn_rec.grad_rec.append(total_attn)
            else:
                grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_attn))
                if self.current_attn == 10:
                    self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                    self.attn_rec.grad_rec.append(grad_record)
                else:
                    self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                    # total_attn = self.attn_rec.record[-1] + grad_record
                    total_attn = self.attn_rec.grad_rec[-1]
                    self.attn_rec.grad_rec.append(total_attn)
            self.current_attn += 1
            return (out_grad,)

        def attn_add_pit(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            # grad_record = grad_in[0].data.cpu().numpy()
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            og = self.attn_rec.grad_add[self.current_attn]
            out_grad += og.clone().detach().to(self.device)
            self.current_attn += 1
            return (out_grad,)

        def norm_record_pit(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            grad_record = grad_in[0].data
            # mask = torch.ones_like(grad_in[0]) * gamma
            self.norm_list = grad_record
            return grad_in

        # visformer
        def pool_record_vis(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            grad_add = grad_in[0].data
            # B,C,H,W = grad_add.shape
            # grad_add = grad_add.reshape((B,C,H*W)).transpose(1,2)
            self.stage.append(grad_add)
            return grad_in

        def mlp_record_vis_stage(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            if self.current_mlp < 4:
                grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_mlp))
                if self.current_mlp == 0:
                    grad_add = torch.zeros_like(grad_record)
                    grad_add[:, 0, :] = self.norm_list[:, 0, :] * 0.1 * (0.5)
                    self.mlp_rec.grad_add.append(grad_add)
                    self.mlp_rec.grad_rec.append(grad_record + grad_add)
                else:
                    self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                    total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
                    self.mlp_rec.grad_rec.append(total_mlp)
            else:
                grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_mlp))
                if self.current_mlp == 4:
                    grad_add = torch.zeros_like(grad_record)
                    # grad_add[:,1:,:] = self.stage[0]* 0.1*(0.5)
                    self.mlp_rec.grad_add.append(grad_add)
                    self.mlp_rec.grad_rec.append(grad_record + grad_add)
                else:
                    self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                    total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
                    self.mlp_rec.grad_rec.append(total_mlp)

            self.current_mlp += 1

            return (out_grad, grad_in[1], grad_in[2])

        def mlp_add_vis(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            # grad_record = grad_in[0].data.cpu().numpy()
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            og = self.mlp_rec.grad_add[self.current_mlp]
            out_grad += og.clone().detach().to(self.device)
            self.current_mlp += 1
            return (out_grad, grad_in[1], grad_in[2])

        def norm_record_vis(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            grad_record = grad_in[0].data
            # mask = torch.ones_like(grad_in[0]) * gamma
            self.norm_list = grad_record
            return grad_in

        def attn_record_vis_stage(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            if self.current_attn < 4:
                grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_attn))
                if self.current_attn == 0:
                    self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                    self.attn_rec.grad_rec.append(grad_record)
                else:
                    self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                    total_attn = self.attn_rec.grad_rec[-1] + grad_record
                    self.attn_rec.grad_rec.append(total_attn)
            else:
                grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_attn))
                if self.current_attn == 4:
                    self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                    self.attn_rec.grad_rec.append(grad_record)
                else:
                    self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                    total_attn = self.attn_rec.grad_rec[-1] + grad_record
                    self.attn_rec.grad_rec.append(total_attn)

            self.current_attn += 1
            return (out_grad,)

        def attn_add_vis(
            module: torch.nn.Module,
            grad_in: tuple[torch.Tensor, ...],
            grad_out: tuple[torch.Tensor, ...],
            gamma: float,
        ) -> tuple[torch.Tensor, ...]:
            # grad_record = grad_in[0].data.cpu().numpy()
            mask = torch.ones_like(grad_in[0]) * gamma
            out_grad = mask * grad_in[0][:]
            og = self.attn_rec.grad_add[self.current_attn]
            out_grad += og.clone().detach().to(self.device)
            self.current_attn += 1
            return (out_grad,)

        # vit
        mlp_record_func_vit = partial(mlp_record_vit_stage, gamma=1.0)
        norm_record_func_vit = partial(norm_record_vit, gamma=1.0)
        mlp_add_func_vit = partial(mlp_add_vit, gamma=0.5)
        attn_record_func_vit = partial(attn_record_vit_stage, gamma=1.0)
        attn_add_func_vit = partial(attn_add_vit, gamma=0.25)

        # pit
        attn_record_func_pit = partial(attn_record_pit_stage, gamma=1.0)
        mlp_record_func_pit = partial(mlp_record_pit_stage, gamma=1.0)
        norm_record_func_pit = partial(norm_record_pit, gamma=1.0)
        pool_record_func_pit = partial(pool_record_pit, gamma=1.0)
        attn_add_func_pit = partial(attn_add_pit, gamma=0.25)
        mlp_add_func_pit = partial(mlp_add_pit, gamma=0.5)
        # mlp_add_func_pit = partial(mlp_add_pit, gamma=0.75)

        # visformer
        attn_record_func_vis = partial(attn_record_vis_stage, gamma=1.0)
        mlp_record_func_vis = partial(mlp_record_vis_stage, gamma=1.0)
        norm_record_func_vis = partial(norm_record_vis, gamma=1.0)
        pool_record_func_vis = partial(pool_record_vis, gamma=1.0)
        attn_add_func_vis = partial(attn_add_vis, gamma=0.25)
        mlp_add_func_vis = partial(mlp_add_vis, gamma=0.5)

        # fmt: off
        # Register hooks for supported models.
        #   * Gradient RECORD mode hooks:
        grad_record_hook_cfg = {
            'vit_base_patch16_224': [
                (norm_record_func_vit, ['norm']),
                (mlp_record_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
                (attn_record_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
            ],
            'deit_base_distilled_patch16_224': [
                (norm_record_func_vit, ['norm']),
                (mlp_record_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
                (attn_record_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
            ],
            'pit_b_224': [
                (norm_record_func_pit, ['norm']),
                (attn_record_func_pit, [f'transformers.{tid}.blocks.{i}.attn.attn_drop' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
                (mlp_record_func_pit, [f'transformers.{tid}.blocks.{i}.norm2' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
                (pool_record_func_pit, ['transformers.1.pool', 'transformers.2.pool']),
            ],
            'visformer_small': [
                (norm_record_func_vis, ['norm']),
                (attn_record_func_vis, [f'stage2.{i}.attn.attn_drop' for i in range(4)] + [f'stage3.{i}.attn.attn_drop' for i in range(4)]),
                (mlp_record_func_vis, [f'stage2.{i}.norm2' for i in range(4)] + [f'stage3.{i}.norm2' for i in range(4)]),
                (pool_record_func_vis, ['patch_embed2', 'patch_embed3']),
            ],
        }
        #   * Gradient ADD mode hooks:
        grad_add_hook_cfg = {
            'vit_base_patch16_224': [
                (mlp_add_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
                (attn_add_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
            ],
            'deit_base_distilled_patch16_224': [
                (mlp_add_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
                (attn_add_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
            ],
            'pit_b_224': [
                (attn_add_func_pit, [f'transformers.{tid}.blocks.{i}.attn.attn_drop' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
                (mlp_add_func_pit, [f'transformers.{tid}.blocks.{i}.norm2' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
            ],
            'visformer_small': [
                (attn_add_func_vis, [f'stage2.{i}.attn.attn_drop' for i in range(4)] + [f'stage3.{i}.attn.attn_drop' for i in range(4)]),
                (mlp_add_func_vis, [f'stage2.{i}.norm2' for i in range(4)] + [f'stage3.{i}.norm2' for i in range(4)]),

            ],
        }
        # fmt: on

        active_hook_cfg = grad_add_hook_cfg if add_grad_mode else grad_record_hook_cfg

        if self.hook_cfg not in active_hook_cfg:
            from warnings import warn

            warn(
                f'Hook config specified (`{self.hook_cfg}`) is not supported. '
                'Falling back to default (`vit_base_patch16_224`). '
                'This MAY NOT be intended.',
                stacklevel=2,
            )
            self.hook_cfg = 'vit_base_patch16_224'

        for hook_func, layers in active_hook_cfg[self.hook_cfg]:
            for layer in layers:
                module = rgetattr(self.model, layer)
                hook = module.register_backward_hook(hook_func)
                self.hooks.append(hook)

_register_model_hooks(add_grad_mode=False)

Register hooks to either record or add gradients during the backward pass.

Parameters:

Name Type Description Default
add_grad_mode bool

If False, register hooks to record gradients. If True, register hooks to modify the gradients by adding pre-recorded gradients during the backward pass.

False
Source code in torchattack/vdc.py
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
def _register_model_hooks(self, add_grad_mode: bool = False) -> None:
    """Register hooks to either record or add gradients during the backward pass.

    Args:
        add_grad_mode: If False, register hooks to record gradients. If True,
            register hooks to modify the gradients by adding pre-recorded gradients
            during the backward pass.
    """

    def mlp_record_vit_stage(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_mlp))
        if self.current_mlp == 0:
            grad_add = torch.zeros_like(grad_record)
            grad_add[:, 0, :] = self.norm_list[:, 0, :] * 0.1 * (0.5)
            self.mlp_rec.grad_add.append(grad_add)
            self.mlp_rec.grad_rec.append(grad_record + grad_add)
        else:
            self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
            total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
            self.mlp_rec.grad_rec.append(total_mlp)
        self.current_mlp += 1
        return (out_grad, grad_in[1], grad_in[2])

    def mlp_add_vit(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        # grad_record = grad_in[0].data.cpu().numpy()
        mask = torch.ones_like(grad_in[0]) * gamma
        # mask_0 = torch.zeros_like(grad_in[0])
        out_grad = mask * grad_in[0][:]
        # out_grad = torch.where(
        #     grad_in[0][:] > 0, mask * grad_in[0][:], mask_0 * grad_in[0][:]
        # )
        og = self.mlp_rec.grad_add[self.current_mlp]
        out_grad += og.clone().detach().to(self.device)
        self.current_mlp += 1
        return (out_grad, grad_in[1], grad_in[2])

    def attn_record_vit_stage(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_attn))
        if self.current_attn == 0:
            self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
            self.attn_rec.grad_rec.append(grad_record)
        else:
            self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
            total_attn = self.attn_rec.grad_rec[-1] + grad_record
            self.attn_rec.grad_rec.append(total_attn)

        self.current_attn += 1
        return (out_grad,)

    def attn_add_vit(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        # grad_record = grad_in[0].data.cpu().numpy()
        # mask_0 = torch.zeros_like(grad_in[0])
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        # out_grad = torch.where(
        #     grad_in[0][:] > 0, mask * grad_in[0][:], mask_0 * grad_in[0][:]
        # )
        og = self.attn_rec.grad_add[self.current_attn]
        out_grad += og.clone().detach().to(self.device)
        self.current_attn += 1
        return (out_grad,)

    def norm_record_vit(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        self.norm_list = grad_in[0].data
        return grad_in

    # pit
    def pool_record_pit(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        grad_add = grad_in[0].data
        b, c, h, w = grad_add.shape
        grad_add = grad_add.reshape((b, c, h * w)).transpose(1, 2)
        self.stage.append(grad_add)
        return grad_in

    def mlp_record_pit_stage(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        if self.current_mlp < 4:
            grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_mlp))
            if self.current_mlp == 0:
                grad_add = torch.zeros_like(grad_record)
                grad_add[:, 0, :] = self.norm_list[:, 0, :] * 0.03 * (0.5)
                self.mlp_rec.grad_add.append(grad_add)
                self.mlp_rec.grad_rec.append(grad_record + grad_add)
            else:
                self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
                self.mlp_rec.grad_rec.append(total_mlp)
        elif self.current_mlp < 10:
            grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_mlp))
            if self.current_mlp == 4:
                grad_add = torch.zeros_like(grad_record)
                grad_add[:, 1:, :] = self.stage[0] * 0.03 * (0.5)
                self.mlp_rec.grad_add.append(grad_add)
                self.mlp_rec.grad_rec.append(grad_record + grad_add)
            else:
                self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                # total_mlp = self.mlp_rec.record[-1] + grad_record
                total_mlp = self.mlp_rec.grad_rec[-1]
                self.mlp_rec.grad_rec.append(total_mlp)
        else:
            grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_mlp))
            if self.current_mlp == 10:
                grad_add = torch.zeros_like(grad_record)
                grad_add[:, 1:, :] = self.stage[1] * 0.03 * (0.5)
                self.mlp_rec.grad_add.append(grad_add)
                self.mlp_rec.grad_rec.append(grad_record + grad_add)
            else:
                self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                # total_mlp = self.mlp_rec.record[-1] + grad_record
                total_mlp = self.mlp_rec.grad_rec[-1]
                self.mlp_rec.grad_rec.append(total_mlp)
        self.current_mlp += 1

        return (out_grad, grad_in[1], grad_in[2])

    def mlp_add_pit(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        # grad_record = grad_in[0].data.cpu().numpy()
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        og = self.mlp_rec.grad_add[self.current_mlp]
        out_grad += og.clone().detach().to(self.device)
        self.current_mlp += 1
        return (out_grad, grad_in[1], grad_in[2])

    def attn_record_pit_stage(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        if self.current_attn < 4:
            grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_attn))
            if self.current_attn == 0:
                self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                self.attn_rec.grad_rec.append(grad_record)
            else:
                self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                total_attn = self.attn_rec.grad_rec[-1] + grad_record
                self.attn_rec.grad_rec.append(total_attn)
        elif self.current_attn < 10:
            grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_attn))
            if self.current_attn == 4:
                self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                self.attn_rec.grad_rec.append(grad_record)
            else:
                self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                # total_attn = self.attn_rec.record[-1] + grad_record
                total_attn = self.attn_rec.grad_rec[-1]
                self.attn_rec.grad_rec.append(total_attn)
        else:
            grad_record = grad_in[0].data * 0.03 * (0.5 ** (self.current_attn))
            if self.current_attn == 10:
                self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                self.attn_rec.grad_rec.append(grad_record)
            else:
                self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                # total_attn = self.attn_rec.record[-1] + grad_record
                total_attn = self.attn_rec.grad_rec[-1]
                self.attn_rec.grad_rec.append(total_attn)
        self.current_attn += 1
        return (out_grad,)

    def attn_add_pit(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        # grad_record = grad_in[0].data.cpu().numpy()
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        og = self.attn_rec.grad_add[self.current_attn]
        out_grad += og.clone().detach().to(self.device)
        self.current_attn += 1
        return (out_grad,)

    def norm_record_pit(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        grad_record = grad_in[0].data
        # mask = torch.ones_like(grad_in[0]) * gamma
        self.norm_list = grad_record
        return grad_in

    # visformer
    def pool_record_vis(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        grad_add = grad_in[0].data
        # B,C,H,W = grad_add.shape
        # grad_add = grad_add.reshape((B,C,H*W)).transpose(1,2)
        self.stage.append(grad_add)
        return grad_in

    def mlp_record_vis_stage(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        if self.current_mlp < 4:
            grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_mlp))
            if self.current_mlp == 0:
                grad_add = torch.zeros_like(grad_record)
                grad_add[:, 0, :] = self.norm_list[:, 0, :] * 0.1 * (0.5)
                self.mlp_rec.grad_add.append(grad_add)
                self.mlp_rec.grad_rec.append(grad_record + grad_add)
            else:
                self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
                self.mlp_rec.grad_rec.append(total_mlp)
        else:
            grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_mlp))
            if self.current_mlp == 4:
                grad_add = torch.zeros_like(grad_record)
                # grad_add[:,1:,:] = self.stage[0]* 0.1*(0.5)
                self.mlp_rec.grad_add.append(grad_add)
                self.mlp_rec.grad_rec.append(grad_record + grad_add)
            else:
                self.mlp_rec.grad_add.append(self.mlp_rec.grad_rec[-1])
                total_mlp = self.mlp_rec.grad_rec[-1] + grad_record
                self.mlp_rec.grad_rec.append(total_mlp)

        self.current_mlp += 1

        return (out_grad, grad_in[1], grad_in[2])

    def mlp_add_vis(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        # grad_record = grad_in[0].data.cpu().numpy()
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        og = self.mlp_rec.grad_add[self.current_mlp]
        out_grad += og.clone().detach().to(self.device)
        self.current_mlp += 1
        return (out_grad, grad_in[1], grad_in[2])

    def norm_record_vis(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        grad_record = grad_in[0].data
        # mask = torch.ones_like(grad_in[0]) * gamma
        self.norm_list = grad_record
        return grad_in

    def attn_record_vis_stage(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        if self.current_attn < 4:
            grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_attn))
            if self.current_attn == 0:
                self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                self.attn_rec.grad_rec.append(grad_record)
            else:
                self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                total_attn = self.attn_rec.grad_rec[-1] + grad_record
                self.attn_rec.grad_rec.append(total_attn)
        else:
            grad_record = grad_in[0].data * 0.1 * (0.5 ** (self.current_attn))
            if self.current_attn == 4:
                self.attn_rec.grad_add.append(torch.zeros_like(grad_record))
                self.attn_rec.grad_rec.append(grad_record)
            else:
                self.attn_rec.grad_add.append(self.attn_rec.grad_rec[-1])
                total_attn = self.attn_rec.grad_rec[-1] + grad_record
                self.attn_rec.grad_rec.append(total_attn)

        self.current_attn += 1
        return (out_grad,)

    def attn_add_vis(
        module: torch.nn.Module,
        grad_in: tuple[torch.Tensor, ...],
        grad_out: tuple[torch.Tensor, ...],
        gamma: float,
    ) -> tuple[torch.Tensor, ...]:
        # grad_record = grad_in[0].data.cpu().numpy()
        mask = torch.ones_like(grad_in[0]) * gamma
        out_grad = mask * grad_in[0][:]
        og = self.attn_rec.grad_add[self.current_attn]
        out_grad += og.clone().detach().to(self.device)
        self.current_attn += 1
        return (out_grad,)

    # vit
    mlp_record_func_vit = partial(mlp_record_vit_stage, gamma=1.0)
    norm_record_func_vit = partial(norm_record_vit, gamma=1.0)
    mlp_add_func_vit = partial(mlp_add_vit, gamma=0.5)
    attn_record_func_vit = partial(attn_record_vit_stage, gamma=1.0)
    attn_add_func_vit = partial(attn_add_vit, gamma=0.25)

    # pit
    attn_record_func_pit = partial(attn_record_pit_stage, gamma=1.0)
    mlp_record_func_pit = partial(mlp_record_pit_stage, gamma=1.0)
    norm_record_func_pit = partial(norm_record_pit, gamma=1.0)
    pool_record_func_pit = partial(pool_record_pit, gamma=1.0)
    attn_add_func_pit = partial(attn_add_pit, gamma=0.25)
    mlp_add_func_pit = partial(mlp_add_pit, gamma=0.5)
    # mlp_add_func_pit = partial(mlp_add_pit, gamma=0.75)

    # visformer
    attn_record_func_vis = partial(attn_record_vis_stage, gamma=1.0)
    mlp_record_func_vis = partial(mlp_record_vis_stage, gamma=1.0)
    norm_record_func_vis = partial(norm_record_vis, gamma=1.0)
    pool_record_func_vis = partial(pool_record_vis, gamma=1.0)
    attn_add_func_vis = partial(attn_add_vis, gamma=0.25)
    mlp_add_func_vis = partial(mlp_add_vis, gamma=0.5)

    # fmt: off
    # Register hooks for supported models.
    #   * Gradient RECORD mode hooks:
    grad_record_hook_cfg = {
        'vit_base_patch16_224': [
            (norm_record_func_vit, ['norm']),
            (mlp_record_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
            (attn_record_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
        ],
        'deit_base_distilled_patch16_224': [
            (norm_record_func_vit, ['norm']),
            (mlp_record_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
            (attn_record_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
        ],
        'pit_b_224': [
            (norm_record_func_pit, ['norm']),
            (attn_record_func_pit, [f'transformers.{tid}.blocks.{i}.attn.attn_drop' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
            (mlp_record_func_pit, [f'transformers.{tid}.blocks.{i}.norm2' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
            (pool_record_func_pit, ['transformers.1.pool', 'transformers.2.pool']),
        ],
        'visformer_small': [
            (norm_record_func_vis, ['norm']),
            (attn_record_func_vis, [f'stage2.{i}.attn.attn_drop' for i in range(4)] + [f'stage3.{i}.attn.attn_drop' for i in range(4)]),
            (mlp_record_func_vis, [f'stage2.{i}.norm2' for i in range(4)] + [f'stage3.{i}.norm2' for i in range(4)]),
            (pool_record_func_vis, ['patch_embed2', 'patch_embed3']),
        ],
    }
    #   * Gradient ADD mode hooks:
    grad_add_hook_cfg = {
        'vit_base_patch16_224': [
            (mlp_add_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
            (attn_add_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
        ],
        'deit_base_distilled_patch16_224': [
            (mlp_add_func_vit, [f'blocks.{i}.norm2' for i in range(12)]),
            (attn_add_func_vit, [f'blocks.{i}.attn.attn_drop' for i in range(12)]),
        ],
        'pit_b_224': [
            (attn_add_func_pit, [f'transformers.{tid}.blocks.{i}.attn.attn_drop' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
            (mlp_add_func_pit, [f'transformers.{tid}.blocks.{i}.norm2' for tid, bid in enumerate([3, 6, 4]) for i in range(bid)]),
        ],
        'visformer_small': [
            (attn_add_func_vis, [f'stage2.{i}.attn.attn_drop' for i in range(4)] + [f'stage3.{i}.attn.attn_drop' for i in range(4)]),
            (mlp_add_func_vis, [f'stage2.{i}.norm2' for i in range(4)] + [f'stage3.{i}.norm2' for i in range(4)]),

        ],
    }
    # fmt: on

    active_hook_cfg = grad_add_hook_cfg if add_grad_mode else grad_record_hook_cfg

    if self.hook_cfg not in active_hook_cfg:
        from warnings import warn

        warn(
            f'Hook config specified (`{self.hook_cfg}`) is not supported. '
            'Falling back to default (`vit_base_patch16_224`). '
            'This MAY NOT be intended.',
            stacklevel=2,
        )
        self.hook_cfg = 'vit_base_patch16_224'

    for hook_func, layers in active_hook_cfg[self.hook_cfg]:
        for layer in layers:
            module = rgetattr(self.model, layer)
            hook = module.register_backward_hook(hook_func)
            self.hooks.append(hook)

forward(x, y)

Perform VDC on a batch of images.

Parameters:

Name Type Description Default
x Tensor

A batch of images. Shape: (N, C, H, W).

required
y Tensor

A batch of labels. Shape: (N).

required

Returns:

Type Description
Tensor

The perturbed images if successful. Shape: (N, C, H, W).

Source code in torchattack/vdc.py
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
    """Perform VDC on a batch of images.

    Args:
        x: A batch of images. Shape: (N, C, H, W).
        y: A batch of labels. Shape: (N).

    Returns:
        The perturbed images if successful. Shape: (N, C, H, W).
    """

    g = torch.zeros_like(x)
    delta = torch.zeros_like(x, requires_grad=True)

    # If alpha is not given, set to eps / steps
    if self.alpha is None:
        self.alpha = self.eps / self.steps

    class GradientRecorder:
        """Gradient recorder for attention and MLP blocks."""

        def __init__(self) -> None:
            self.grad_rec: list[torch.Tensor] = []
            self.grad_add: list[torch.Tensor] = []

    # Perform VDC
    for _ in range(self.steps):
        # Initialize gradient recorders
        self.attn_rec = GradientRecorder()
        self.mlp_rec = GradientRecorder()

        # Stage 1: Record gradients
        self.current_mlp = 0
        self.current_attn = 0
        self._register_model_hooks(add_grad_mode=False)

        # Compute loss
        outs = self.model(self.normalize(x + delta))
        loss = self.lossfn(outs, y)

        if self.targeted:
            loss = -loss

        # Compute gradient
        loss.backward()

        if delta.grad is None:
            continue

        # Zero out gradient
        delta.grad.detach_()
        delta.grad.zero_()

        for hook in self.hooks:
            hook.remove()

        # Stage 2: Update gradients by adding recorded gradients
        self.current_mlp = 0
        self.current_attn = 0
        self._register_model_hooks(add_grad_mode=True)

        # Compute loss 2nd time
        outs = self.model(self.normalize(x + delta))
        loss = self.lossfn(outs, y)

        if self.targeted:
            loss = -loss

        # Compute gradient 2nd time
        loss.backward()

        # Apply momentum term
        g = self.decay * g + delta.grad / torch.mean(
            torch.abs(delta.grad), dim=(1, 2, 3), keepdim=True
        )

        # Update delta
        delta.data = delta.data + self.alpha * g.sign()
        delta.data = torch.clamp(delta.data, -self.eps, self.eps)
        delta.data = torch.clamp(x + delta.data, self.clip_min, self.clip_max) - x

        # Zero out gradient
        delta.grad.detach_()
        delta.grad.zero_()

        for hook in self.hooks:
            hook.remove()

    return x + delta