11import tempfile
2- from typing import Callable , List , Tuple , Union
2+ from typing import Callable , List , Optional , Tuple
33
44import torch
55
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)
3333from torch import Tensor
3434from 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
0 commit comments