-
Notifications
You must be signed in to change notification settings - Fork 487
/
test_feature_ablation.py
677 lines (600 loc) · 26.2 KB
/
test_feature_ablation.py
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
#!/usr/bin/env python3
import io
import unittest
import unittest.mock
from typing import Any, cast, List, Tuple, Union
import torch
from captum._utils.typing import BaselineType, TargetType, TensorOrTupleOfTensorsGeneric
from captum.attr._core.feature_ablation import FeatureAblation
from captum.attr._core.noise_tunnel import NoiseTunnel
from captum.attr._utils.attribution import Attribution
from tests.helpers.basic import assertTensorAlmostEqual, BaseTest
from tests.helpers.basic_models import (
BasicModel,
BasicModel_ConvNet_One_Conv,
BasicModel_MultiLayer,
BasicModel_MultiLayer_MultiInput,
BasicModelBoolInput,
BasicModelWithSparseInputs,
)
from torch import Tensor
class Test(BaseTest):
r"""
The following conversion tests are underlying assumptions
made by the rest of tests in this file.
We are testing them explicitly just in case they break behaviour
in the future. As in this case it will be easier to update the tests.
"""
def test_python_float_conversion(self) -> None:
x = torch.tensor(3, dtype=cast(torch.dtype, float))
self.assertEqual(x.dtype, torch.float64)
def test_python_int_conversion(self) -> None:
x = torch.tensor(5, dtype=cast(torch.dtype, int))
self.assertEqual(x.dtype, torch.int64)
def test_float32_tensor_item_conversion(self) -> None:
x = torch.tensor(5, dtype=torch.float32)
y = torch.tensor(x.item()) # .item() returns a python float
# for whatever reason it is only
# dtype == torch.float64 if you provide dtype=float
self.assertEqual(y.dtype, torch.float32)
def test_int32_tensor_item_conversion(self) -> None:
x = torch.tensor(5, dtype=torch.int32)
y = torch.tensor(x.item()) # .item() returns a python int
self.assertEqual(y.dtype, torch.int64)
def test_simple_ablation(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[20.0, 50.0, 30.0]], requires_grad=True)
self._ablation_test_assert(
ablation_algo, inp, [[80.0, 200.0, 120.0]], perturbations_per_eval=(1, 2, 3)
)
def test_simple_ablation_int_to_int(self) -> None:
ablation_algo = FeatureAblation(BasicModel())
inp = torch.tensor([[-3, 1, 2]])
self._ablation_test_assert(
ablation_algo, inp, [[-3, 0, 0]], perturbations_per_eval=(1, 2, 3)
)
def test_simple_ablation_int_to_int_nt(self) -> None:
ablation_algo = NoiseTunnel(FeatureAblation(BasicModel()))
inp = torch.tensor([[-3, 1, 2]]).float()
self._ablation_test_assert(
ablation_algo,
inp,
[[-3.0, 0.0, 0.0]],
perturbations_per_eval=(1, 2, 3),
stdevs=1e-10,
)
def test_simple_ablation_int_to_float(self) -> None:
net = BasicModel()
def wrapper_func(inp):
return net(inp).float()
ablation_algo = FeatureAblation(wrapper_func)
inp = torch.tensor([[-3, 1, 2]])
self._ablation_test_assert(
ablation_algo, inp, [[-3.0, 0.0, 0.0]], perturbations_per_eval=(1, 2, 3)
)
def test_simple_ablation_with_mask(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[20.0, 50.0, 30.0]], requires_grad=True)
self._ablation_test_assert(
ablation_algo,
inp,
[[280.0, 280.0, 120.0]],
feature_mask=torch.tensor([[0, 0, 1]]),
perturbations_per_eval=(1, 2, 3),
)
def test_simple_ablation_with_baselines(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[20.0, 50.0, 30.0]], requires_grad=True)
self._ablation_test_assert(
ablation_algo,
inp,
[[248.0, 248.0, 104.0]],
feature_mask=torch.tensor([[0, 0, 1]]),
baselines=4,
perturbations_per_eval=(1, 2, 3),
)
def test_simple_ablation_boolean(self) -> None:
ablation_algo = FeatureAblation(BasicModelBoolInput())
inp = torch.tensor([[True, False, True]])
self._ablation_test_assert(
ablation_algo,
inp,
[[40.0, 40.0, 40.0]],
feature_mask=torch.tensor([[0, 0, 1]]),
perturbations_per_eval=(1, 2, 3),
)
def test_simple_ablation_boolean_with_baselines(self) -> None:
ablation_algo = FeatureAblation(BasicModelBoolInput())
inp = torch.tensor([[True, False, True]])
self._ablation_test_assert(
ablation_algo,
inp,
[[-40.0, -40.0, 0.0]],
feature_mask=torch.tensor([[0, 0, 1]]),
baselines=True,
perturbations_per_eval=(1, 2, 3),
)
def test_multi_sample_ablation(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[2.0, 10.0, 3.0], [20.0, 50.0, 30.0]], requires_grad=True)
self._ablation_test_assert(
ablation_algo,
inp,
[[8.0, 35.0, 12.0], [80.0, 200.0, 120.0]],
perturbations_per_eval=(1, 2, 3),
)
def test_multi_sample_ablation_with_mask(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[2.0, 10.0, 3.0], [20.0, 50.0, 30.0]], requires_grad=True)
mask = torch.tensor([[0, 0, 1], [1, 1, 0]])
self._ablation_test_assert(
ablation_algo,
inp,
[[41.0, 41.0, 12.0], [280.0, 280.0, 120.0]],
feature_mask=mask,
perturbations_per_eval=(1, 2, 3),
)
def test_multi_input_ablation_with_mask(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer_MultiInput())
inp1 = torch.tensor([[23.0, 100.0, 0.0], [20.0, 50.0, 30.0]])
inp2 = torch.tensor([[20.0, 50.0, 30.0], [0.0, 100.0, 0.0]])
inp3 = torch.tensor([[0.0, 100.0, 10.0], [2.0, 10.0, 3.0]])
mask1 = torch.tensor([[1, 1, 1], [0, 1, 0]])
mask2 = torch.tensor([[0, 1, 2]])
mask3 = torch.tensor([[0, 1, 2], [0, 0, 0]])
expected = (
[[492.0, 492.0, 492.0], [200.0, 200.0, 200.0]],
[[80.0, 200.0, 120.0], [0.0, 400.0, 0.0]],
[[0.0, 400.0, 40.0], [60.0, 60.0, 60.0]],
)
self._ablation_test_assert(
ablation_algo,
(inp1, inp2, inp3),
expected,
additional_input=(1,),
feature_mask=(mask1, mask2, mask3),
)
self._ablation_test_assert(
ablation_algo,
(inp1, inp2),
expected[0:1],
additional_input=(inp3, 1),
feature_mask=(mask1, mask2),
perturbations_per_eval=(1, 2, 3),
)
expected_with_baseline = (
[[468.0, 468.0, 468.0], [184.0, 192.0, 184.0]],
[[68.0, 188.0, 108.0], [-12.0, 388.0, -12.0]],
[[-16.0, 384.0, 24.0], [12.0, 12.0, 12.0]],
)
self._ablation_test_assert(
ablation_algo,
(inp1, inp2, inp3),
expected_with_baseline,
additional_input=(1,),
feature_mask=(mask1, mask2, mask3),
baselines=(2, 3.0, 4),
perturbations_per_eval=(1, 2, 3),
)
def test_multi_input_ablation_with_mask_nt(self) -> None:
ablation_algo = NoiseTunnel(FeatureAblation(BasicModel_MultiLayer_MultiInput()))
inp1 = torch.tensor([[23.0, 100.0, 0.0], [20.0, 50.0, 30.0]])
inp2 = torch.tensor([[20.0, 50.0, 30.0], [0.0, 100.0, 0.0]])
inp3 = torch.tensor([[0.0, 100.0, 10.0], [2.0, 10.0, 3.0]])
mask1 = torch.tensor([[1, 1, 1], [0, 1, 0]])
mask2 = torch.tensor([[0, 1, 2]])
mask3 = torch.tensor([[0, 1, 2], [0, 0, 0]])
expected = (
[[492.0, 492.0, 492.0], [200.0, 200.0, 200.0]],
[[80.0, 200.0, 120.0], [0.0, 400.0, 0.0]],
[[0.0, 400.0, 40.0], [60.0, 60.0, 60.0]],
)
self._ablation_test_assert(
ablation_algo,
(inp1, inp2, inp3),
expected,
additional_input=(1,),
feature_mask=(mask1, mask2, mask3),
stdevs=1e-10,
)
self._ablation_test_assert(
ablation_algo,
(inp1, inp2),
expected[0:1],
additional_input=(inp3, 1),
feature_mask=(mask1, mask2),
perturbations_per_eval=(1, 2, 3),
stdevs=1e-10,
)
expected_with_baseline = (
[[468.0, 468.0, 468.0], [184.0, 192.0, 184.0]],
[[68.0, 188.0, 108.0], [-12.0, 388.0, -12.0]],
[[-16.0, 384.0, 24.0], [12.0, 12.0, 12.0]],
)
self._ablation_test_assert(
ablation_algo,
(inp1, inp2, inp3),
expected_with_baseline,
additional_input=(1,),
feature_mask=(mask1, mask2, mask3),
baselines=(2, 3.0, 4),
perturbations_per_eval=(1, 2, 3),
stdevs=1e-10,
)
def test_multi_input_ablation(self) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer_MultiInput())
inp1 = torch.tensor([[23.0, 100.0, 0.0], [20.0, 50.0, 30.0]])
inp2 = torch.tensor([[20.0, 50.0, 30.0], [0.0, 100.0, 0.0]])
inp3 = torch.tensor([[0.0, 100.0, 10.0], [2.0, 10.0, 3.0]])
baseline1 = torch.tensor([[3.0, 0.0, 0.0]])
baseline2 = torch.tensor([[0.0, 1.0, 0.0]])
baseline3 = torch.tensor([[1.0, 2.0, 3.0]])
self._ablation_test_assert(
ablation_algo,
(inp1, inp2, inp3),
(
[[80.0, 400.0, 0.0], [68.0, 200.0, 120.0]],
[[80.0, 196.0, 120.0], [0.0, 396.0, 0.0]],
[[-4.0, 392.0, 28.0], [4.0, 32.0, 0.0]],
),
additional_input=(1,),
baselines=(baseline1, baseline2, baseline3),
perturbations_per_eval=(1, 2, 3),
)
baseline1_exp = torch.tensor([[3.0, 0.0, 0.0], [3.0, 0.0, 2.0]])
baseline2_exp = torch.tensor([[0.0, 1.0, 0.0], [0.0, 1.0, 4.0]])
baseline3_exp = torch.tensor([[3.0, 2.0, 4.0], [1.0, 2.0, 3.0]])
self._ablation_test_assert(
ablation_algo,
(inp1, inp2, inp3),
(
[[80.0, 400.0, 0.0], [68.0, 200.0, 112.0]],
[[80.0, 196.0, 120.0], [0.0, 396.0, -16.0]],
[[-12.0, 392.0, 24.0], [4.0, 32.0, 0.0]],
),
additional_input=(1,),
baselines=(baseline1_exp, baseline2_exp, baseline3_exp),
perturbations_per_eval=(1, 2, 3),
)
def test_simple_multi_input_conv(self) -> None:
ablation_algo = FeatureAblation(BasicModel_ConvNet_One_Conv())
inp = torch.arange(16, dtype=torch.float).view(1, 1, 4, 4)
inp2 = torch.ones((1, 1, 4, 4))
self._ablation_test_assert(
ablation_algo,
(inp, inp2),
(67 * torch.ones_like(inp), 13 * torch.ones_like(inp2)),
feature_mask=(torch.tensor(0), torch.tensor(1)),
perturbations_per_eval=(1, 2, 4, 8, 12, 16),
)
self._ablation_test_assert(
ablation_algo,
(inp, inp2),
(
[
[
[
[0.0, 2.0, 4.0, 3.0],
[4.0, 9.0, 10.0, 7.0],
[4.0, 13.0, 14.0, 11.0],
[0.0, 0.0, 0.0, 0.0],
]
]
],
[
[
[
[1.0, 2.0, 2.0, 1.0],
[1.0, 2.0, 2.0, 1.0],
[1.0, 2.0, 2.0, 1.0],
[0.0, 0.0, 0.0, 0.0],
]
]
],
),
perturbations_per_eval=(1, 3, 7, 14),
)
# Remaining tests are for cases where forward function returns a scalar
# per batch, as either a float, integer, 0d tensor or 1d tensor.
def test_error_perturbations_per_eval_limit_batch_scalar(self) -> None:
net = BasicModel_MultiLayer()
inp = torch.tensor([[2.0, 10.0, 3.0], [20.0, 50.0, 30.0]], requires_grad=True)
ablation = FeatureAblation(lambda inp: torch.sum(net(inp)).item())
with self.assertRaises(AssertionError):
_ = ablation.attribute(inp, perturbations_per_eval=2)
def test_error_agg_mode_arbitrary_output(self) -> None:
net = BasicModel_MultiLayer()
# output 3 numbers for the entire batch
# note that the batch size == 2
def forward_func(inp):
pred = net(inp)
return torch.stack([pred.sum(), pred.max(), pred.min()])
inp = torch.tensor([[2.0, 10.0, 3.0], [20.0, 50.0, 30.0]], requires_grad=True)
ablation = FeatureAblation(forward_func)
with self.assertRaises(AssertionError):
_ = ablation.attribute(inp, perturbations_per_eval=2)
def test_error_agg_mode_incorrect_fm(self) -> None:
def forward_func(inp):
return inp[0].unsqueeze(0)
inp = torch.tensor([[1, 2, 3], [4, 5, 6]])
mask = torch.tensor([[0, 1, 2], [0, 0, 1]])
ablation = FeatureAblation(forward_func)
with self.assertRaises(AssertionError):
_ = ablation.attribute(inp, perturbations_per_eval=1, feature_mask=mask)
def test_empty_sparse_features(self) -> None:
ablation_algo = FeatureAblation(BasicModelWithSparseInputs())
inp1 = torch.tensor([[1.0, -2.0, 3.0], [2.0, -1.0, 3.0]])
inp2 = torch.tensor([])
exp: Tuple[List[List[float]], List[float]] = ([[9.0, -3.0, 12.0]], [0.0])
self._ablation_test_assert(ablation_algo, (inp1, inp2), exp, target=None)
def test_sparse_features(self) -> None:
ablation_algo = FeatureAblation(BasicModelWithSparseInputs())
inp1 = torch.tensor([[1.0, -2.0, 3.0], [2.0, -1.0, 3.0]])
# Length of sparse index list may not match # of examples
inp2 = torch.tensor([1, 7, 2, 4, 5, 3, 6])
self._ablation_test_assert(
ablation_algo, (inp1, inp2), ([[9.0, -3.0, 12.0]], [2.0]), target=None
)
def test_single_ablation_batch_scalar_float(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: torch.sum(net(inp)).item())
self._single_input_one_sample_batch_scalar_ablation_assert(
ablation_algo, dtype=torch.float64
)
def test_single_ablation_batch_scalar_tensor_0d(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: torch.sum(net(inp)))
self._single_input_one_sample_batch_scalar_ablation_assert(ablation_algo)
def test_single_ablation_batch_scalar_tensor_1d(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: torch.sum(net(inp)).reshape(1))
self._single_input_one_sample_batch_scalar_ablation_assert(ablation_algo)
def test_single_ablation_batch_scalar_tensor_int(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: int(torch.sum(net(inp)).item()))
self._single_input_one_sample_batch_scalar_ablation_assert(
ablation_algo, dtype=torch.int64
)
def test_multi_sample_ablation_batch_scalar_float(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: torch.sum(net(inp)).item())
self._single_input_multi_sample_batch_scalar_ablation_assert(
ablation_algo,
dtype=torch.float64,
)
def test_multi_sample_ablation_batch_scalar_tensor_0d(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: torch.sum(net(inp)))
self._single_input_multi_sample_batch_scalar_ablation_assert(ablation_algo)
def test_multi_sample_ablation_batch_scalar_tensor_1d(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: torch.sum(net(inp)).reshape(1))
self._single_input_multi_sample_batch_scalar_ablation_assert(ablation_algo)
def test_multi_sample_ablation_batch_scalar_tensor_int(self) -> None:
net = BasicModel_MultiLayer()
ablation_algo = FeatureAblation(lambda inp: int(torch.sum(net(inp)).item()))
self._single_input_multi_sample_batch_scalar_ablation_assert(
ablation_algo, dtype=torch.int64
)
def test_multi_inp_ablation_batch_scalar_float(self) -> None:
net = BasicModel_MultiLayer_MultiInput()
ablation_algo = FeatureAblation(lambda *inp: torch.sum(net(*inp)).item())
self._multi_input_batch_scalar_ablation_assert(
ablation_algo,
dtype=torch.float64,
)
def test_multi_inp_ablation_batch_scalar_tensor_0d(self) -> None:
net = BasicModel_MultiLayer_MultiInput()
ablation_algo = FeatureAblation(lambda *inp: torch.sum(net(*inp)))
self._multi_input_batch_scalar_ablation_assert(ablation_algo)
def test_multi_inp_ablation_batch_scalar_tensor_1d(self) -> None:
net = BasicModel_MultiLayer_MultiInput()
ablation_algo = FeatureAblation(lambda *inp: torch.sum(net(*inp)).reshape(1))
self._multi_input_batch_scalar_ablation_assert(ablation_algo)
def test_mutli_inp_ablation_batch_scalar_tensor_int(self) -> None:
net = BasicModel_MultiLayer_MultiInput()
ablation_algo = FeatureAblation(lambda *inp: int(torch.sum(net(*inp)).item()))
self._multi_input_batch_scalar_ablation_assert(ablation_algo, dtype=torch.int64)
def test_unassociated_output_3d_tensor(self) -> None:
def forward_func(inp):
return torch.ones(1, 5, 3, 2)
inp = torch.randn(10, 5)
mask = torch.arange(5).unsqueeze(0)
self._ablation_test_assert(
ablation_algo=FeatureAblation(forward_func),
test_input=inp,
baselines=None,
target=None,
feature_mask=mask,
perturbations_per_eval=(1,),
expected_ablation=torch.zeros((5 * 3 * 2,) + inp[0].shape),
)
def test_single_inp_ablation_multi_output_aggr(self) -> None:
def forward_func(inp):
return inp[0].unsqueeze(0)
inp = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
mask = torch.tensor([[0, 1, 2]])
self._ablation_test_assert(
ablation_algo=FeatureAblation(forward_func),
test_input=inp,
feature_mask=mask,
baselines=None,
target=None,
perturbations_per_eval=(1,),
# should just be the first input spread across each feature
expected_ablation=[[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]],
)
def test_single_inp_ablation_multi_output_aggr_mask_none(self) -> None:
def forward_func(inp):
return inp[0].unsqueeze(0)
inp = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
self._ablation_test_assert(
ablation_algo=FeatureAblation(forward_func),
test_input=inp,
feature_mask=None,
baselines=None,
target=None,
perturbations_per_eval=(1,),
# should just be the first input spread across each feature
expected_ablation=[[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]],
)
def test_single_inp_ablation_multi_output_aggr_non_standard(self) -> None:
def forward_func(inp):
return inp[0].unsqueeze(0)
inp = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
mask = torch.tensor([[0, 0, 1]])
self._ablation_test_assert(
ablation_algo=FeatureAblation(forward_func),
test_input=inp,
feature_mask=mask,
baselines=None,
target=None,
perturbations_per_eval=(1,),
expected_ablation=[[1.0, 1.0, 0.0], [2.0, 2.0, 0.0], [0.0, 0.0, 3.0]],
)
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_simple_ablation_with_show_progress(self, mock_stderr) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[20.0, 50.0, 30.0]], requires_grad=True)
# test progress output for each batch size
for bsz in (1, 2, 3):
self._ablation_test_assert(
ablation_algo,
inp,
[[80.0, 200.0, 120.0]],
perturbations_per_eval=(bsz,),
show_progress=True,
)
output = mock_stderr.getvalue()
# to test if progress calculation aligns with the actual iteration
# all perturbations_per_eval should reach progress of 100%
assert (
"Feature Ablation attribution: 100%" in output
), f"Error progress output: {repr(output)}"
mock_stderr.seek(0)
mock_stderr.truncate(0)
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_simple_ablation_with_mask_and_show_progress(self, mock_stderr) -> None:
ablation_algo = FeatureAblation(BasicModel_MultiLayer())
inp = torch.tensor([[20.0, 50.0, 30.0]], requires_grad=True)
# test progress output for each batch size
for bsz in (1, 2, 3):
self._ablation_test_assert(
ablation_algo,
inp,
[[280.0, 280.0, 120.0]],
feature_mask=torch.tensor([[0, 0, 1]]),
perturbations_per_eval=(bsz,),
show_progress=True,
)
output = mock_stderr.getvalue()
# to test if progress calculation aligns with the actual iteration
# all perturbations_per_eval should reach progress of 100%
assert (
"Feature Ablation attribution: 100%" in output
), f"Error progress output: {repr(output)}"
mock_stderr.seek(0)
mock_stderr.truncate(0)
def _single_input_one_sample_batch_scalar_ablation_assert(
self, ablation_algo: Attribution, dtype: torch.dtype = torch.float32
) -> None:
inp = torch.tensor([[2.0, 10.0, 3.0]], requires_grad=True)
mask = torch.tensor([[0, 0, 1]])
self._ablation_test_assert(
ablation_algo,
inp,
torch.tensor([[82.0, 82.0, 24.0]], dtype=dtype),
feature_mask=mask,
perturbations_per_eval=(1,),
target=None,
)
def _single_input_multi_sample_batch_scalar_ablation_assert(
self,
ablation_algo: Attribution,
dtype: torch.dtype = torch.float32,
) -> None:
inp = torch.tensor([[2.0, 10.0, 3.0], [20.0, 50.0, 30.0]], requires_grad=True)
mask = torch.tensor([[0, 0, 1]])
self._ablation_test_assert(
ablation_algo,
inp,
torch.tensor([[642.0, 642.0, 264.0]], dtype=dtype),
feature_mask=mask,
perturbations_per_eval=(1,),
target=None,
)
def _multi_input_batch_scalar_ablation_assert(
self,
ablation_algo: Attribution,
dtype: torch.dtype = torch.float32,
) -> None:
inp1 = torch.tensor([[23.0, 100.0, 0.0], [20.0, 50.0, 30.0]])
inp2 = torch.tensor([[20.0, 50.0, 30.0], [0.0, 100.0, 0.0]])
inp3 = torch.tensor([[0.0, 100.0, 10.0], [2.0, 10.0, 3.0]])
mask1 = torch.tensor([[1, 1, 1]])
mask2 = torch.tensor([[0, 1, 2]])
mask3 = torch.tensor([[0, 1, 2]])
expected = (
torch.tensor([[1784, 1784, 1784]], dtype=dtype),
torch.tensor([[160, 1200, 240]], dtype=dtype),
torch.tensor([[16, 880, 104]], dtype=dtype),
)
self._ablation_test_assert(
ablation_algo,
(inp1, inp2, inp3),
expected,
additional_input=(1,),
feature_mask=(mask1, mask2, mask3),
perturbations_per_eval=(1,),
target=None,
)
def _ablation_test_assert(
self,
ablation_algo: Attribution,
test_input: TensorOrTupleOfTensorsGeneric,
expected_ablation: Union[
Tensor,
Tuple[Tensor, ...],
# NOTE: mypy doesn't support recursive types
# would do a List[NestedList[Union[int, float]]
# or Tuple[NestedList[Union[int, float]]
# but... we can't.
#
# See https://github.com/python/mypy/issues/731
List[Any],
Tuple[List[Any], ...],
],
feature_mask: Union[None, TensorOrTupleOfTensorsGeneric] = None,
additional_input: Any = None,
perturbations_per_eval: Tuple[int, ...] = (1,),
baselines: BaselineType = None,
target: TargetType = 0,
**kwargs: Any,
) -> None:
for batch_size in perturbations_per_eval:
self.assertTrue(ablation_algo.multiplies_by_inputs)
attributions = ablation_algo.attribute(
test_input,
target=target,
feature_mask=feature_mask,
additional_forward_args=additional_input,
baselines=baselines,
perturbations_per_eval=batch_size,
**kwargs,
)
if isinstance(expected_ablation, tuple):
for i in range(len(expected_ablation)):
expected = expected_ablation[i]
if not isinstance(expected, torch.Tensor):
expected = torch.tensor(expected)
self.assertEqual(attributions[i].shape, expected.shape)
self.assertEqual(attributions[i].dtype, expected.dtype)
assertTensorAlmostEqual(self, attributions[i], expected)
else:
if not isinstance(expected_ablation, torch.Tensor):
expected_ablation = torch.tensor(expected_ablation)
self.assertEqual(attributions.shape, expected_ablation.shape)
self.assertEqual(attributions.dtype, expected_ablation.dtype)
assertTensorAlmostEqual(self, attributions, expected_ablation)
if __name__ == "__main__":
unittest.main()