1
1
import tempfile
2
- from typing import Callable , List , Tuple , Union
2
+ from typing import Callable , List , Optional , Tuple
3
3
4
4
import torch
5
5
27
27
generate_assymetric_matrix_given_eigenvalues ,
28
28
generate_symmetric_matrix_given_eigenvalues ,
29
29
get_random_model_and_data ,
30
+ GPU_SETTING_LIST ,
31
+ is_gpu ,
30
32
UnpackDataset ,
31
- USE_GPU_LIST ,
32
33
)
33
34
from torch import Tensor
34
35
from torch .utils .data import DataLoader
@@ -237,17 +238,17 @@ def _param_matmul(params: Tuple[Tensor]):
237
238
delta ,
238
239
mode ,
239
240
unpack_inputs ,
240
- use_gpu ,
241
+ gpu_setting ,
241
242
)
242
- for use_gpu in USE_GPU_LIST
243
+ for gpu_setting in GPU_SETTING_LIST
243
244
for (influence_constructor_1 , influence_constructor_2 , delta ) in [
244
245
# compare implementations, when considering only 1 layer
245
246
(
246
247
DataInfluenceConstructor (
247
248
NaiveInfluenceFunction ,
248
249
layers = (
249
250
["module.linear1" ]
250
- if use_gpu == "cuda_dataparallel"
251
+ if gpu_setting == "cuda_dataparallel"
251
252
else ["linear1" ]
252
253
),
253
254
projection_dim = 5 ,
@@ -258,7 +259,7 @@ def _param_matmul(params: Tuple[Tensor]):
258
259
ArnoldiInfluenceFunction ,
259
260
layers = (
260
261
["module.linear1" ]
261
- if use_gpu == "cuda_dataparallel"
262
+ if gpu_setting == "cuda_dataparallel"
262
263
else ["linear1" ]
263
264
),
264
265
arnoldi_dim = 50 ,
@@ -314,7 +315,7 @@ def test_compare_implementations_trained_NN_model_and_data(
314
315
delta : float ,
315
316
mode : str ,
316
317
unpack_inputs : bool ,
317
- use_gpu : Union [ bool , str ],
318
+ gpu_setting : Optional [ str ],
318
319
) -> None :
319
320
"""
320
321
this compares 2 influence implementations on a trained 2-layer NN model.
@@ -329,7 +330,7 @@ def test_compare_implementations_trained_NN_model_and_data(
329
330
delta ,
330
331
mode ,
331
332
unpack_inputs ,
332
- use_gpu ,
333
+ gpu_setting ,
333
334
)
334
335
335
336
# this compares `ArnoldiInfluenceFunction` and `NaiveInfluenceFunction` on randomly
@@ -345,16 +346,16 @@ def test_compare_implementations_trained_NN_model_and_data(
345
346
delta ,
346
347
mode ,
347
348
unpack_inputs ,
348
- use_gpu ,
349
+ gpu_setting ,
349
350
)
350
- for use_gpu in USE_GPU_LIST
351
+ for gpu_setting in GPU_SETTING_LIST
351
352
for (influence_constructor_1 , influence_constructor_2 , delta ) in [
352
353
(
353
354
DataInfluenceConstructor (
354
355
NaiveInfluenceFunction ,
355
356
layers = (
356
357
["module.linear1" ]
357
- if use_gpu == "cuda_dataparallel"
358
+ if gpu_setting == "cuda_dataparallel"
358
359
else ["linear1" ]
359
360
),
360
361
show_progress = False ,
@@ -364,7 +365,7 @@ def test_compare_implementations_trained_NN_model_and_data(
364
365
ArnoldiInfluenceFunction ,
365
366
layers = (
366
367
["module.linear1" ]
367
- if use_gpu == "cuda_dataparallel"
368
+ if gpu_setting == "cuda_dataparallel"
368
369
else ["linear1" ]
369
370
),
370
371
show_progress = False ,
@@ -397,7 +398,7 @@ def test_compare_implementations_random_model_and_data(
397
398
delta : float ,
398
399
mode : str ,
399
400
unpack_inputs : bool ,
400
- use_gpu : Union [ bool , str ],
401
+ gpu_setting : Optional [ str ],
401
402
) -> None :
402
403
"""
403
404
this compares 2 influence implementations on a trained 2-layer NN model.
@@ -412,7 +413,7 @@ def test_compare_implementations_random_model_and_data(
412
413
delta ,
413
414
mode ,
414
415
unpack_inputs ,
415
- use_gpu ,
416
+ gpu_setting ,
416
417
)
417
418
418
419
def _test_compare_implementations (
@@ -423,7 +424,7 @@ def _test_compare_implementations(
423
424
delta : float ,
424
425
mode : str ,
425
426
unpack_inputs : bool ,
426
- use_gpu : Union [ bool , str ],
427
+ gpu_setting : Optional [ str ],
427
428
) -> None :
428
429
"""
429
430
checks that 2 implementations of `InfluenceFunctionBase` return the same
@@ -444,13 +445,14 @@ def _test_compare_implementations(
444
445
tmpdir ,
445
446
unpack_inputs ,
446
447
return_test_data = True ,
447
- use_gpu = use_gpu ,
448
+ gpu_setting = gpu_setting ,
448
449
return_hessian_data = True ,
449
450
model_type = model_type ,
450
451
)
451
452
452
453
train_dataset = DataLoader (train_dataset , batch_size = 5 )
453
454
455
+ use_gpu = is_gpu (gpu_setting )
454
456
hessian_dataset = (
455
457
ExplicitDataset (hessian_samples , hessian_labels , use_gpu )
456
458
if not unpack_inputs
0 commit comments