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
@@ -229,6 +230,17 @@ def _param_matmul(params: Tuple[Tensor]):
229
230
"max" ,
230
231
)
231
232
233
+ # TODO: for some unknow reason, this test and the test below does not work
234
+ # on `cuda_data_parallel` setting. We need to investigate why.
235
+ # Use a local version of setting list for these two tests for now
236
+ # since we have changed the default setting list to includes all options.
237
+ # (This is also used in many other tests, which also needs to be unified later).
238
+ gpu_setting_list = (
239
+ ["" , "cuda" ]
240
+ if torch .cuda .is_available () and torch .cuda .device_count () != 0
241
+ else ["" ]
242
+ )
243
+
232
244
@parameterized .expand (
233
245
[
234
246
(
@@ -237,17 +249,17 @@ def _param_matmul(params: Tuple[Tensor]):
237
249
delta ,
238
250
mode ,
239
251
unpack_inputs ,
240
- use_gpu ,
252
+ gpu_setting ,
241
253
)
242
- for use_gpu in USE_GPU_LIST
254
+ for gpu_setting in gpu_setting_list
243
255
for (influence_constructor_1 , influence_constructor_2 , delta ) in [
244
256
# compare implementations, when considering only 1 layer
245
257
(
246
258
DataInfluenceConstructor (
247
259
NaiveInfluenceFunction ,
248
260
layers = (
249
261
["module.linear1" ]
250
- if use_gpu == "cuda_dataparallel"
262
+ if gpu_setting == "cuda_dataparallel"
251
263
else ["linear1" ]
252
264
),
253
265
projection_dim = 5 ,
@@ -258,7 +270,7 @@ def _param_matmul(params: Tuple[Tensor]):
258
270
ArnoldiInfluenceFunction ,
259
271
layers = (
260
272
["module.linear1" ]
261
- if use_gpu == "cuda_dataparallel"
273
+ if gpu_setting == "cuda_dataparallel"
262
274
else ["linear1" ]
263
275
),
264
276
arnoldi_dim = 50 ,
@@ -314,7 +326,7 @@ def test_compare_implementations_trained_NN_model_and_data(
314
326
delta : float ,
315
327
mode : str ,
316
328
unpack_inputs : bool ,
317
- use_gpu : Union [ bool , str ],
329
+ gpu_setting : Optional [ str ],
318
330
) -> None :
319
331
"""
320
332
this compares 2 influence implementations on a trained 2-layer NN model.
@@ -329,14 +341,15 @@ def test_compare_implementations_trained_NN_model_and_data(
329
341
delta ,
330
342
mode ,
331
343
unpack_inputs ,
332
- use_gpu ,
344
+ gpu_setting ,
333
345
)
334
346
335
347
# this compares `ArnoldiInfluenceFunction` and `NaiveInfluenceFunction` on randomly
336
348
# generated data. because these implementations are numerically equivalent, we
337
349
# can also compare the intermediate quantities. we do not compare with
338
350
# `NaiveInfluence` because on randomly generated data, it is not comparable,
339
351
# conceptually, with the other implementations, due to numerical issues.
352
+
340
353
@parameterized .expand (
341
354
[
342
355
(
@@ -345,16 +358,16 @@ def test_compare_implementations_trained_NN_model_and_data(
345
358
delta ,
346
359
mode ,
347
360
unpack_inputs ,
348
- use_gpu ,
361
+ gpu_setting ,
349
362
)
350
- for use_gpu in USE_GPU_LIST
363
+ for gpu_setting in gpu_setting_list
351
364
for (influence_constructor_1 , influence_constructor_2 , delta ) in [
352
365
(
353
366
DataInfluenceConstructor (
354
367
NaiveInfluenceFunction ,
355
368
layers = (
356
369
["module.linear1" ]
357
- if use_gpu == "cuda_dataparallel"
370
+ if gpu_setting == "cuda_dataparallel"
358
371
else ["linear1" ]
359
372
),
360
373
show_progress = False ,
@@ -364,7 +377,7 @@ def test_compare_implementations_trained_NN_model_and_data(
364
377
ArnoldiInfluenceFunction ,
365
378
layers = (
366
379
["module.linear1" ]
367
- if use_gpu == "cuda_dataparallel"
380
+ if gpu_setting == "cuda_dataparallel"
368
381
else ["linear1" ]
369
382
),
370
383
show_progress = False ,
@@ -397,7 +410,7 @@ def test_compare_implementations_random_model_and_data(
397
410
delta : float ,
398
411
mode : str ,
399
412
unpack_inputs : bool ,
400
- use_gpu : Union [ bool , str ],
413
+ gpu_setting : Optional [ str ],
401
414
) -> None :
402
415
"""
403
416
this compares 2 influence implementations on a trained 2-layer NN model.
@@ -412,7 +425,7 @@ def test_compare_implementations_random_model_and_data(
412
425
delta ,
413
426
mode ,
414
427
unpack_inputs ,
415
- use_gpu ,
428
+ gpu_setting ,
416
429
)
417
430
418
431
def _test_compare_implementations (
@@ -423,7 +436,7 @@ def _test_compare_implementations(
423
436
delta : float ,
424
437
mode : str ,
425
438
unpack_inputs : bool ,
426
- use_gpu : Union [ bool , str ],
439
+ gpu_setting : Optional [ str ],
427
440
) -> None :
428
441
"""
429
442
checks that 2 implementations of `InfluenceFunctionBase` return the same
@@ -444,13 +457,14 @@ def _test_compare_implementations(
444
457
tmpdir ,
445
458
unpack_inputs ,
446
459
return_test_data = True ,
447
- use_gpu = use_gpu ,
460
+ gpu_setting = gpu_setting ,
448
461
return_hessian_data = True ,
449
462
model_type = model_type ,
450
463
)
451
464
452
465
train_dataset = DataLoader (train_dataset , batch_size = 5 )
453
466
467
+ use_gpu = is_gpu (gpu_setting )
454
468
hessian_dataset = (
455
469
ExplicitDataset (hessian_samples , hessian_labels , use_gpu )
456
470
if not unpack_inputs
0 commit comments