-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·436 lines (360 loc) · 17.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/home/josers2/anaconda3/bin/python
"""A hogwild style ASGD implementation of RESNET
Based on: https://github.com/pytorch/examples/tree/master/mnist_hogwild
Network and Performance modifications are:
- Use Cifar10 and {Resnet,Lenet}
- Use a step learning rate
- Use the main thread for evaluations, instead of the worker threads
(instead of waiting on a join call, it periodically checks thread status)
Usability modifications are:
- Generate CSV logs of output, rather than dumping to STDOUT
- Use python logger instead of dumping to STDOUT
Asynchronous Poisoning Attack modifications are:
- Have worker threads communicate when they find a biased , and
increase the time between when they find the batch and when they do work
with the batch. This simplifies the granularity needed by the OS to halt
them. The bias is calculated by the threads instead of over a side channel.
- Have the main thread communicate when training is ending, so the OS can
release the halted attack threads
All communication with the OS is done through files (see apa.sh)
Authors: Jose Rodrigo Sanchez Vicarte, josers2@illinois.edu
Ben Schreiber, bjschre2@illinois.edu
"""
# pylint: disable=C0103,R0903
from __future__ import print_function
import logging
import argparse
import time
import os
import sys
from shutil import rmtree, copy
import tarfile
import errno
import csv
from tqdm import tqdm
import torch # pylint: disable=F0401
import torch.multiprocessing as mp # pylint: disable=F0401
from torchvision import datasets
from models.models.resnet import ResNet18
from train import train, test
# Training settings
parser = argparse.ArgumentParser(description='APA Demonstration')
parser.add_argument('runname', help='name for output files')
# TODO fix default paths
parser.add_argument('--tmp-dir', type=str, default='/tmp',
help="Directory to run out of. Prevents files from being"
"left all over the place, or in case you don't want to run"
"out of NFS")
parser.add_argument('--final-dir', type=str,
default='outputs',
help='Directory to place final outputs in')
# options for simulated attacks
sub_parsers = parser.add_subparsers(dest='mode', help='Sub-Command help')
mlti_sim_prs = sub_parsers.add_parser('simulate-multi',
help='Simulate Stale params APA (No OS)')
mlti_sim_prs.add_argument('--step-size', default=10, type=int, metavar='S',
help='Number of threads at each multi attack stage')
mlti_sim_prs.add_argument('--num-stages', default=10, type=int, metavar='NS',
help='Number of multi attack stages')
lr_sim_prs = sub_parsers.add_parser('simulate',
help='Simulate Stale LR APA (No OS)')
lr_sim_prs.add_argument('--attack-batches', default=1, type=int,
metavar='AB',
help='Number of biased updates to apply')
sub_parsers.add_parser('baseline',
help='Enables CUDA training. '
'Useful for training checkpoints. Do not use for the '
'attack, as training must be CPU based and '
'multithreaded.')
# checkpoint options
ckpt_group = parser.add_argument_group('Checkpoint Options')
# TODO include epoch in checkpoint
ckpt_group.add_argument('--resume', default=-1, type=int, metavar='RE',
help='Use checkpoint, from epoch [RE]')
ckpt_group.add_argument('--attack-checkpoint-path', type=str, default='train',
metavar='CN', help='Checkpoint load/save name')
ckpt_group.add_argument('--baseline-checkpoint-path', type=str, default=None,
metavar='CLN', help="If specified, load from this "
"checkpoint, but don't save to it")
ckpt_group.add_argument('--prepend-logs', type=str, default=None,
metavar='PRE', help='Logs to prepend checkpoint with. '
'Useful for plotting simulations with the baseline')
# TODO implement soft-resume
# ckpt_group.add_argument('--soft-resume', action='store_true', help='Use '
# 'checkpoint iff available')
# training options
train_group = parser.add_argument_group('Training Options')
train_group.add_argument('--max-steps', default=1, type=int, metavar='MS',
help='Number of non-attack epochs to train for. '
'DOES NOT AFFECT SIMULATED ATTACK THREADS.')
train_group.add_argument('--lr', type=float, default=0.1, metavar='LR',
help='Initial learning rate (default: 0.1)')
train_group.add_argument('--num-processes', type=int, default=1, metavar='N',
help='how many training processes to use '
'(default: 2)')
train_group.add_argument('--batch-size', type=int, default=128, metavar='BS',
help='input batch size for training (default: 128)')
train_group.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum (default: 0.9)')
train_group.add_argument('--optimizer', type=str, default='sgd',
metavar='OPTIM', choices=['sgd', 'adam', 'rms'])
# attack options
atk_group = parser.add_argument_group('Attack Options; for OS managed and Sim')
atk_group.add_argument('--target', type=int, default=-1, metavar='T',
help='Target label for biased batch. -1 is target-any.')
atk_group.add_argument('--bias', type=float, default=0.2, metavar='B',
help='How biased a batch should be. To simulate an '
'indiscriminate attack, set this value to 0.10 (equal '
' distribution of all labels in each batch)')
def procs_alive(procs):
"""Returns true as long as any worker is alive
Used as a non-blocking join. """
for cp in procs:
if cp.is_alive():
return True
logging.debug('No Process alive')
return False
def setup_outfiles(dirname, final_dir, prepend=None):
"""Call this function with the output directory for logs
If the output directory does not exist, it is created.
If the output directory exists, but has old logs, they are removed.
If using a checkpoint, allows for prepending the old logs to the new ones,
for convenience when graphing."""
if prepend is not None:
assert(prepend != dirname), 'Prepend and output cannot be the same!'
# Create directory and clear files if they exist
if os.path.exists(dirname):
try:
rmtree(dirname)
logging.warning('Removed old output directory (%s)', dirname)
except OSError:
logging.error(sys.exc_info()[0])
sys.exit(1)
os.mkdir(dirname)
if not os.path.exists(final_dir):
os.mkdir(final_dir)
if prepend is not None: # prepending from checkpoint
assert(os.path.exists(prepend)), 'Prepend directory not found'
logging.info('Prepending logs from %s', prepend)
# Make sure prepend path exists, then copy the logs over
log_files = ['eval', 'conf.0', 'conf.1', 'conf.2', 'conf.3', 'conf.4',
'conf.5', 'conf.6', 'conf.7', 'conf.8', 'conf.9']
for cf in log_files:
logging.debug('Current file is %s', cf)
pre_fpath = f'{prepend}/{cf}'
assert(os.path.isfile(pre_fpath)), f"Missing {pre_fpath}"
copy(pre_fpath, f"{dirname}/{cf}")
def setup_and_load():
'''Setup checkpoints directories, and load if necessary'''
mdl = ResNet18().to(device)
# gradients are allocated lazily, so they are not shared here
mdl.share_memory()
# Make sure the directory to save checkpoints already exists
ckpt_dir = f'{args.tmp_dir}'
try:
os.mkdir(ckpt_dir)
logging.info('Created checkpoint directory (%s)', ckpt_dir)
except OSError as e:
if e.errno == errno.EEXIST:
logging.warning('Checkpoint directory already exist (%s)',
ckpt_dir)
else:
raise
# set load checkpoint name - if lckpt is set, use that otherwise use
# the same as the save name
ckpt_fname = f"{ckpt_dir}/{args.attack_checkpoint_path}.ckpt"
bestAcc = None
# load checkpoint if resume epoch is specified
if args.mode == 'simulate' or args.mode == 'simulate-multi':
assert(args.resume != -1), 'Simulate should be used with a checkpoint'
ckpt_load_fname = ckpt_fname if args.baseline_checkpoint_path is None \
else args.baseline_checkpoint_path
assert(os.path.isfile(ckpt_load_fname)), f'{ckpt_load_fname} not found'
checkpoint = torch.load(ckpt_load_fname,
map_location=lambda storage, loc: storage)
mdl.load_state_dict(checkpoint['net'])
bestAcc = checkpoint['acc']
setup_outfiles(outdir, args.final_dir, prepend=args.prepend_logs)
logging.info('Resumed from %s at %.3f', ckpt_load_fname, bestAcc)
else:
# for a full run, nothing to prepend or resume
setup_outfiles(outdir, args.final_dir)
return mdl, bestAcc, ckpt_fname
def inf_iter(procs):
'''Generator for TQDM on list of processes'''
while True:
yield procs_alive(procs)
def launch_atk_proc():
'''When simulating, run the attack thread alone'''
rank = 0
# atk_p = mp.Process(target=train, args=(rank, args, model, device,
# dataloader_kwargs))
# atk_p.start()
log = []
# eval_counter = 0
train(rank, args, model, device, dataloader_kwargs)
# while procs_alive([atk_p]):
# time.sleep(10)
# with tqdm(inf_iter([atk_p]), position=0, desc=f'{args.runname}',
# total=float("inf"), unit='Validation') as tbar:
# # while atk_p.is_alive(): # evaluate and log!
# for p_status in tbar:
# if p_status is False:
# break
#
# # evaluate without logging; logging is done by the worker
# _, val_acc = test(args, model, device, dataloader_kwargs,
# etime=None)
#
# log.append({'vacc': val_acc,
# 'time': eval_counter})
# logging.info('Attack Accuracy is %s', val_acc)
# tbar.set_postfix(acc=val_acc)
# eval_counter += 1
# # update checkpoint
# torch.save({'net': model.state_dict(), 'acc': val_acc},
# ckpt_output_fname)
# evaluate post attack
# If simulated, eval counter is the number of attack batches
# if multi sim, eval counter is the number of stages
if args.mode == 'simulate': # Variant 1 Simulation
post_attack_step = args.attack_batches
else: # Variant 2 Simulation
post_attack_step = args.num_stages
with open(f"{outdir}/eval", 'w') as eval_f:
writer = csv.DictWriter(eval_f, fieldnames=['time', 'vacc'])
for dat in log:
writer.writerow(dat)
return post_attack_step
def launch_procs(eval_counter=0, s_rank=0):
'''Launch normal workers.
If no workers would be spawned, just return. This will happen if
simulating with a single worker --- no recovery time is allowed. '''
if s_rank == args.num_processes:
_, val_acc = test(args, model, device, dataloader_kwargs,
etime=eval_counter)
return val_acc
# Spawn the worker processes. Each runs an independent call of the train
# function
processes = []
for rank in range(s_rank, args.num_processes):
p = mp.Process(target=train, args=(rank, args, model, device,
dataloader_kwargs))
p.start()
processes.append(p)
logging.info('Started %s', p.pid)
log = []
# While any process is alive, continuously evaluate accuracy - the master
# thread is the evaluation thread
with tqdm(inf_iter(processes), position=0, desc='Testing',
total=float("inf"), unit='Validation') as tbar:
for p_status in tbar:
if p_status is False:
break
# log in test
_, val_acc = test(args, model, device, dataloader_kwargs,
etime=eval_counter)
log.append({'vacc': val_acc,
'time': eval_counter})
# tqdm.write(f'Accuracy is {vacc}')
logging.info('Accuracy is %s', val_acc)
eval_counter += 1
tbar.set_postfix(acc=val_acc)
# update checkpoint
torch.save({'net': model.state_dict(), 'acc': val_acc},
ckpt_output_fname)
time.sleep(60)
# open eval log as append in case we're simulating and the attack thread
# added some data
with open(f"{outdir}/eval", 'a') as eval_f:
writer = csv.DictWriter(eval_f, fieldnames=['time', 'vacc'])
for dat in log:
writer.writerow(dat)
# There should be no processes left alive by this point, but do this anyway
# to make sure no orphaned processes are left behind
for proc in processes:
os.system("kill -9 {}".format(proc.pid))
return val_acc
if __name__ == '__main__':
args = parser.parse_args()
FORMAT = '%(message)s [%(levelno)s-%(asctime)s %(module)s:%(funcName)s]'
logging.basicConfig(level=logging.INFO, format=FORMAT,
handlers=[logging.StreamHandler(sys.stdout)])
simulating = False
if args.mode == 'baseline':
logging.info('Running a baseline')
if args.max_steps == 1:
assert(input('Training for a single epoch, is this intentional? '
'Recommended option for SGD is 350 epochs '
'y/[n]') == 'y'), 'Set the --max-steps option.'
elif args.mode == 'simulate':
simulating = True
logging.info('Running an LR simulation')
elif args.mode == 'simulate-multi':
simulating = True
logging.info('Running a multi attack baseline')
else:
logging.info('Running normal training')
# if available, train baselines on the GPU
# TODO support multiple GPU
use_cuda = torch.cuda.is_available()
# pylint: disable=E1101
device = torch.device("cuda" if use_cuda else "cpu")
logging.info('Running on %s', device)
dataloader_kwargs = {'pin_memory': True} if use_cuda else {}
if not args.mode == 'baseline' and \
not simulating and \
args.num_processes < 2:
assert(input('Are you generating a baseline on the CPU? y/[n]') ==
'y'), 'Use at least two processes for the OS based attack.'
mp.set_start_method('spawn')
# Directory to save logs to
# if changed, make sure the name in test_epoch in train.py matches
outdir = f"{args.tmp_dir}/{args.runname}.hogwild"
logging.info('Output directory is %s', outdir)
# setup checkpoint directory and load from checkpoint as needed
model, best_acc, ckpt_output_fname = setup_and_load()
torch.set_num_threads(10) # number of MKL threads for evaluation
# download dataset if not found
logging.debug('Downloading')
datasets.CIFAR10(f'{args.tmp_dir}/data/', train=True, download=True)
# Determine initial checkpoint accuracy
# necessary to get initial confidences
logging.info('Testing')
val_loss, val_accuracy = test(args, model, device, dataloader_kwargs,
etime=-1)
logging.info('Eval acc: %.3f', val_accuracy)
torch.set_num_threads(3) # number of MKL threads for evaluation
start_time = time.time()
# when simulating, attack process is the first to run
if simulating:
if args.attack_checkpoint_path != 'train':
logging.warning('Checkpoint path ignored during simulation')
step = launch_atk_proc()
# attack finished, allow for recovery if more than one worker
if args.num_processes > 1:
launch_procs(step, s_rank=1)
else:
# create status file, in case full attack script is being used
# if this is a baseline, creates the file and updates it but has no
# effect
with open(f'{args.tmp_dir}/{args.runname}.status', 'w') as sfile:
sfile.write('Starting Training\n')
launch_procs()
logging.info('Training run time: %.2f', time.time() - start_time)
# only save checkpoints if not simulating
if not simulating:
torch.set_num_threads(10) # number of MKL threads for evaluation
_, vacc = test(args, model, device, dataloader_kwargs, etime=None)
torch.save({'net': model.state_dict(), 'acc': vacc}, ckpt_output_fname)
copy(ckpt_output_fname, outdir)
# Copy generated logs out of the local directory onto the shared NFS
final_dir = f'{args.final_dir}/{args.runname}.tar.gz'
if os.path.isfile(final_dir):
os.remove(final_dir)
logging.info('Removed old output tar')
# compress output files
with tarfile.open(f'{outdir}.tar.gz', "w:gz") as tar:
tar.add(outdir, arcname=os.path.basename(outdir))
copy(f'{outdir}.tar.gz', final_dir)
logging.info('Copied logs and checkpoint to %s', final_dir)