Skip to content

Commit 8c36dbe

Browse files
cicichen01facebook-github-bot
authored andcommitted
Split the get_random_model_and_data() method [1/n] (#1252)
Summary: As titled. The get_random_model_and_data() method is used to construct testing data for influence and it is reported as too complex by flake8 (https://www.flake8rules.com/rules/C901.html). This series of diff will split the method and abstract the common parts. This diff isolate the model part for different gpu usage settings. It also eliminate the mix usage of bool and str. Differential Revision: D55165054
1 parent f94d3ee commit 8c36dbe

File tree

6 files changed

+125
-92
lines changed

6 files changed

+125
-92
lines changed

tests/influence/_core/test_arnoldi_influence.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tempfile
2-
from typing import Callable, List, Tuple, Union
2+
from typing import Callable, List, Optional, Tuple
33

44
import torch
55

@@ -27,8 +27,8 @@
2727
generate_assymetric_matrix_given_eigenvalues,
2828
generate_symmetric_matrix_given_eigenvalues,
2929
get_random_model_and_data,
30+
is_gpu,
3031
UnpackDataset,
31-
USE_GPU_LIST,
3232
)
3333
from torch import Tensor
3434
from torch.utils.data import DataLoader
@@ -229,6 +229,17 @@ def _param_matmul(params: Tuple[Tensor]):
229229
"max",
230230
)
231231

232+
# TODO: for some unknow reason, this test and the test below does not work
233+
# on `cuda_data_parallel` setting. We need to investigate why.
234+
# Use a local version of setting list for these two tests for now
235+
# since we have changed the default setting list to includes all options.
236+
# (This is also used in many other tests, which also needs to be unified later).
237+
gpu_setting_list = (
238+
["", "cuda"]
239+
if torch.cuda.is_available() and torch.cuda.device_count() != 0
240+
else [""]
241+
)
242+
232243
@parameterized.expand(
233244
[
234245
(
@@ -237,17 +248,17 @@ def _param_matmul(params: Tuple[Tensor]):
237248
delta,
238249
mode,
239250
unpack_inputs,
240-
use_gpu,
251+
gpu_setting,
241252
)
242-
for use_gpu in USE_GPU_LIST
253+
for gpu_setting in gpu_setting_list
243254
for (influence_constructor_1, influence_constructor_2, delta) in [
244255
# compare implementations, when considering only 1 layer
245256
(
246257
DataInfluenceConstructor(
247258
NaiveInfluenceFunction,
248259
layers=(
249260
["module.linear1"]
250-
if use_gpu == "cuda_dataparallel"
261+
if gpu_setting == "cuda_dataparallel"
251262
else ["linear1"]
252263
),
253264
projection_dim=5,
@@ -258,7 +269,7 @@ def _param_matmul(params: Tuple[Tensor]):
258269
ArnoldiInfluenceFunction,
259270
layers=(
260271
["module.linear1"]
261-
if use_gpu == "cuda_dataparallel"
272+
if gpu_setting == "cuda_dataparallel"
262273
else ["linear1"]
263274
),
264275
arnoldi_dim=50,
@@ -314,7 +325,7 @@ def test_compare_implementations_trained_NN_model_and_data(
314325
delta: float,
315326
mode: str,
316327
unpack_inputs: bool,
317-
use_gpu: Union[bool, str],
328+
gpu_setting: Optional[str],
318329
) -> None:
319330
"""
320331
this compares 2 influence implementations on a trained 2-layer NN model.
@@ -329,14 +340,15 @@ def test_compare_implementations_trained_NN_model_and_data(
329340
delta,
330341
mode,
331342
unpack_inputs,
332-
use_gpu,
343+
gpu_setting,
333344
)
334345

335346
# this compares `ArnoldiInfluenceFunction` and `NaiveInfluenceFunction` on randomly
336347
# generated data. because these implementations are numerically equivalent, we
337348
# can also compare the intermediate quantities. we do not compare with
338349
# `NaiveInfluence` because on randomly generated data, it is not comparable,
339350
# conceptually, with the other implementations, due to numerical issues.
351+
340352
@parameterized.expand(
341353
[
342354
(
@@ -345,16 +357,16 @@ def test_compare_implementations_trained_NN_model_and_data(
345357
delta,
346358
mode,
347359
unpack_inputs,
348-
use_gpu,
360+
gpu_setting,
349361
)
350-
for use_gpu in USE_GPU_LIST
362+
for gpu_setting in gpu_setting_list
351363
for (influence_constructor_1, influence_constructor_2, delta) in [
352364
(
353365
DataInfluenceConstructor(
354366
NaiveInfluenceFunction,
355367
layers=(
356368
["module.linear1"]
357-
if use_gpu == "cuda_dataparallel"
369+
if gpu_setting == "cuda_dataparallel"
358370
else ["linear1"]
359371
),
360372
show_progress=False,
@@ -364,7 +376,7 @@ def test_compare_implementations_trained_NN_model_and_data(
364376
ArnoldiInfluenceFunction,
365377
layers=(
366378
["module.linear1"]
367-
if use_gpu == "cuda_dataparallel"
379+
if gpu_setting == "cuda_dataparallel"
368380
else ["linear1"]
369381
),
370382
show_progress=False,
@@ -397,7 +409,7 @@ def test_compare_implementations_random_model_and_data(
397409
delta: float,
398410
mode: str,
399411
unpack_inputs: bool,
400-
use_gpu: Union[bool, str],
412+
gpu_setting: Optional[str],
401413
) -> None:
402414
"""
403415
this compares 2 influence implementations on a trained 2-layer NN model.
@@ -412,7 +424,7 @@ def test_compare_implementations_random_model_and_data(
412424
delta,
413425
mode,
414426
unpack_inputs,
415-
use_gpu,
427+
gpu_setting,
416428
)
417429

418430
def _test_compare_implementations(
@@ -423,7 +435,7 @@ def _test_compare_implementations(
423435
delta: float,
424436
mode: str,
425437
unpack_inputs: bool,
426-
use_gpu: Union[bool, str],
438+
gpu_setting: Optional[str],
427439
) -> None:
428440
"""
429441
checks that 2 implementations of `InfluenceFunctionBase` return the same
@@ -444,13 +456,14 @@ def _test_compare_implementations(
444456
tmpdir,
445457
unpack_inputs,
446458
return_test_data=True,
447-
use_gpu=use_gpu,
459+
gpu_setting=gpu_setting,
448460
return_hessian_data=True,
449461
model_type=model_type,
450462
)
451463

452464
train_dataset = DataLoader(train_dataset, batch_size=5)
453465

466+
use_gpu = is_gpu(gpu_setting)
454467
hessian_dataset = (
455468
ExplicitDataset(hessian_samples, hessian_labels, use_gpu)
456469
if not unpack_inputs

tests/influence/_core/test_naive_influence.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tempfile
2-
from typing import Callable, List, Tuple, Union
2+
from typing import Callable, List, Optional, Tuple
33

44
import torch
55

@@ -21,9 +21,9 @@
2121
DataInfluenceConstructor,
2222
ExplicitDataset,
2323
get_random_model_and_data,
24+
is_gpu,
2425
Linear,
2526
UnpackDataset,
26-
USE_GPU_LIST,
2727
)
2828
from torch.utils.data import DataLoader
2929

@@ -51,6 +51,18 @@ def test_flatten_unflattener(self, param_shapes: List[Tuple[int, ...]]) -> None:
5151
mode="max",
5252
)
5353

54+
# TODO: for some unknow reason, this test does not work
55+
# on `cuda_data_parallel` setting. We need to investigate why.
56+
# Use a local version of setting list for these two tests for now
57+
# since we have changed the default setting list to includes all options.
58+
# (This is also used in many other tests, which also needs to be unified later).
59+
60+
gpu_setting_list = (
61+
["", "cuda"]
62+
if torch.cuda.is_available() and torch.cuda.device_count() != 0
63+
else [""]
64+
)
65+
5466
@parameterized.expand(
5567
[
5668
(
@@ -59,17 +71,17 @@ def test_flatten_unflattener(self, param_shapes: List[Tuple[int, ...]]) -> None:
5971
delta,
6072
mode,
6173
unpack_inputs,
62-
use_gpu,
74+
gpu_setting,
6375
)
6476
for reduction in ["none", "sum", "mean"]
65-
for use_gpu in USE_GPU_LIST
77+
for gpu_setting in gpu_setting_list
6678
for (influence_constructor, delta) in [
6779
(
6880
DataInfluenceConstructor(
6981
NaiveInfluenceFunction,
7082
layers=(
7183
["module.linear"]
72-
if use_gpu == "cuda_dataparallel"
84+
if gpu_setting == "cuda_dataparallel"
7385
else ["linear"]
7486
),
7587
projection_dim=None,
@@ -109,7 +121,7 @@ def test_matches_linear_regression(
109121
delta: float,
110122
mode: str,
111123
unpack_inputs: bool,
112-
use_gpu: Union[bool, str],
124+
gpu_setting: Optional[str],
113125
) -> None:
114126
"""
115127
this tests that `NaiveInfluence`, the simplest implementation, agree with the
@@ -129,13 +141,14 @@ def test_matches_linear_regression(
129141
tmpdir,
130142
unpack_inputs,
131143
return_test_data=True,
132-
use_gpu=use_gpu,
144+
gpu_setting=gpu_setting,
133145
return_hessian_data=True,
134146
model_type="trained_linear",
135147
)
136148

137149
train_dataset = DataLoader(train_dataset, batch_size=5)
138150

151+
use_gpu = is_gpu(gpu_setting)
139152
hessian_dataset = (
140153
ExplicitDataset(hessian_samples, hessian_labels, use_gpu)
141154
if not unpack_inputs

tests/influence/_core/test_tracin_k_most_influential.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tempfile
2-
from typing import Callable, Union
2+
from typing import Callable, Optional
33

44
import torch
55
import torch.nn as nn
@@ -13,22 +13,17 @@
1313
build_test_name_func,
1414
DataInfluenceConstructor,
1515
get_random_model_and_data,
16+
GPU_SETTING_LIST,
17+
is_gpu,
1618
)
1719

1820

1921
class TestTracInGetKMostInfluential(BaseTest):
20-
21-
use_gpu_list = (
22-
[False, "cuda", "cuda_data_parallel"]
23-
if torch.cuda.is_available() and torch.cuda.device_count() != 0
24-
else [False]
25-
)
26-
2722
param_list = []
2823
for batch_size, k in [(4, 7), (7, 4), (40, 5), (5, 40), (40, 45)]:
2924
for unpack_inputs in [True, False]:
3025
for proponents in [True, False]:
31-
for use_gpu in use_gpu_list:
26+
for gpu_setting in GPU_SETTING_LIST:
3227
for reduction, constr, aggregate in [
3328
(
3429
"none",
@@ -51,7 +46,7 @@ class TestTracInGetKMostInfluential(BaseTest):
5146
name="linear2",
5247
layers=(
5348
["module.linear2"]
54-
if use_gpu == "cuda_data_parallel"
49+
if gpu_setting == "cuda_data_parallel"
5550
else ["linear2"]
5651
),
5752
),
@@ -61,7 +56,7 @@ class TestTracInGetKMostInfluential(BaseTest):
6156
if not (
6257
"sample_wise_grads_per_batch" in constr.kwargs
6358
and constr.kwargs["sample_wise_grads_per_batch"]
64-
and use_gpu
59+
and is_gpu(gpu_setting)
6560
):
6661
param_list.append(
6762
(
@@ -71,7 +66,7 @@ class TestTracInGetKMostInfluential(BaseTest):
7166
proponents,
7267
batch_size,
7368
k,
74-
use_gpu,
69+
gpu_setting,
7570
aggregate,
7671
)
7772
)
@@ -88,7 +83,7 @@ def test_tracin_k_most_influential(
8883
proponents: bool,
8984
batch_size: int,
9085
k: int,
91-
use_gpu: Union[bool, str],
86+
gpu_setting: Optional[str],
9287
aggregate: bool,
9388
) -> None:
9489
"""
@@ -107,7 +102,7 @@ def test_tracin_k_most_influential(
107102
tmpdir,
108103
unpack_inputs,
109104
True,
110-
use_gpu,
105+
gpu_setting,
111106
)
112107

113108
self.assertTrue(isinstance(reduction, str))

0 commit comments

Comments
 (0)