Base class for backdoor training and testing. Args: train_dataset (types in support_list): Benign training dataset. test_dataset (types in support_list): Benign testing dataset. model (torch.nn.Module): Network. loss (torch.nn.Module): Loss. schedule (dic
| 42 | |
| 43 | |
| 44 | class Base(object): |
| 45 | """Base class for backdoor training and testing. |
| 46 | |
| 47 | Args: |
| 48 | train_dataset (types in support_list): Benign training dataset. |
| 49 | test_dataset (types in support_list): Benign testing dataset. |
| 50 | model (torch.nn.Module): Network. |
| 51 | loss (torch.nn.Module): Loss. |
| 52 | schedule (dict): Training or testing global schedule. Default: None. |
| 53 | seed (int): Global seed for random numbers. Default: 0. |
| 54 | deterministic (bool): Sets whether PyTorch operations must use "deterministic" algorithms. |
| 55 | That is, algorithms which, given the same input, and when run on the same software and hardware, |
| 56 | always produce the same output. When enabled, operations will use deterministic algorithms when available, |
| 57 | and if only nondeterministic algorithms are available they will throw a RuntimeError when called. Default: False. |
| 58 | """ |
| 59 | |
| 60 | def __init__(self, train_dataset, test_dataset, model, loss, schedule=None, seed=0, deterministic=False): |
| 61 | assert isinstance(train_dataset, support_list), 'train_dataset is an unsupported dataset type, train_dataset should be a subclass of our support list.' |
| 62 | self.train_dataset = train_dataset |
| 63 | |
| 64 | assert isinstance(test_dataset, support_list), 'test_dataset is an unsupported dataset type, test_dataset should be a subclass of our support list.' |
| 65 | self.test_dataset = test_dataset |
| 66 | self.model = model |
| 67 | self.loss = loss |
| 68 | self.global_schedule = deepcopy(schedule) |
| 69 | self.current_schedule = None |
| 70 | self._set_seed(seed, deterministic) |
| 71 | |
| 72 | def _set_seed(self, seed, deterministic): |
| 73 | # Use torch.manual_seed() to seed the RNG for all devices (both CPU and CUDA). |
| 74 | torch.manual_seed(seed) |
| 75 | |
| 76 | # Set python seed |
| 77 | random.seed(seed) |
| 78 | |
| 79 | # Set numpy seed (However, some applications and libraries may use NumPy Random Generator objects, |
| 80 | # not the global RNG (https://numpy.org/doc/stable/reference/random/generator.html), and those will |
| 81 | # need to be seeded consistently as well.) |
| 82 | np.random.seed(seed) |
| 83 | |
| 84 | os.environ['PYTHONHASHSEED'] = str(seed) |
| 85 | |
| 86 | if deterministic: |
| 87 | torch.backends.cudnn.benchmark = False |
| 88 | torch.use_deterministic_algorithms(True) |
| 89 | # torch.use_deterministic_algorithms(True, warn_only=True) |
| 90 | torch.backends.cudnn.deterministic = True |
| 91 | os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8' |
| 92 | # Hint: In some versions of CUDA, RNNs and LSTM networks may have non-deterministic behavior. |
| 93 | # If you want to set them deterministic, see torch.nn.RNN() and torch.nn.LSTM() for details and workarounds. |
| 94 | |
| 95 | def _seed_worker(self, worker_id): |
| 96 | worker_seed = torch.initial_seed() % 2**32 |
| 97 | np.random.seed(worker_seed) |
| 98 | random.seed(worker_seed) |
| 99 | |
| 100 | def get_model(self): |
| 101 | return self.model |
nothing calls this directly
no outgoing calls
no test coverage detected