-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
457 lines (383 loc) · 17.9 KB
/
helper.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import copy
import config
import numpy as np
import os
from shutil import copyfile
import math
import torch
import logging
logger = logging.getLogger("logger")
def flatten(weights_update):
param_grad = []
for name, data in weights_update.items():
param_grad = data.view(-1) if not len(
param_grad) else torch.cat((param_grad, data.view(-1)))
return param_grad
def unflatten(flattened, normal_shape):
weights_update = {}
for name, data in normal_shape.items():
n_params = len(data.view(-1))
weights_update[name] = torch.as_tensor(
flattened[:n_params]).reshape(data.size())
flattened = flattened[n_params:]
return weights_update
def multi_krum(all_updates, n_attackers, multi_k=False):
nusers = all_updates.shape[0]
candidates = []
candidate_indices = []
remaining_updates = all_updates
all_indices = np.arange(len(all_updates))
while len(remaining_updates) > 2 * n_attackers + 2:
distances = []
for update in remaining_updates:
distance = torch.norm((remaining_updates - update), dim=1) ** 2
distances = distance[None, :] if not len(
distances) else torch.cat((distances, distance[None, :]), 0)
distances = torch.sort(distances, dim=1)[0]
scores = torch.sum(
distances[:, :len(remaining_updates) - 2 - n_attackers], dim=1)
# [:len(remaining_updates) - 2 - n_attackers]
indices = torch.argsort(scores)
# if verbose: print(indices)
candidate_indices.append(all_indices[indices[0].cpu().numpy()])
all_indices = np.delete(all_indices, indices[0].cpu().numpy())
candidates = remaining_updates[indices[0]][None, :] if not len(
candidates) else torch.cat((candidates, remaining_updates[indices[0]][None, :]), 0)
remaining_updates = torch.cat(
(remaining_updates[:indices[0]], remaining_updates[indices[0] + 1:]), 0)
if not multi_k:
break
aggregate = torch.mean(candidates, dim=0)
return aggregate, np.array(candidate_indices)
def bulyan(all_updates, n_attackers):
nusers = all_updates.shape[0]
bulyan_cluster = []
candidate_indices = []
remaining_updates = all_updates
all_indices = np.arange(len(all_updates))
while len(bulyan_cluster) < (nusers - 2 * n_attackers):
distances = []
for update in remaining_updates:
distance = torch.norm((remaining_updates - update), dim=1) ** 2
distances = distance[None, :] if not len(
distances) else torch.cat((distances, distance[None, :]), 0)
distances = torch.sort(distances, dim=1)[0]
scores = torch.sum(
distances[:, :len(remaining_updates) - 2 - n_attackers], dim=1)
indices = torch.argsort(scores)[:len(
remaining_updates) - 2 - n_attackers]
# logger.info(f'scores {scores}')
# logger.info(f'indices {indices}')
if len(indices) == 0:
break
candidate_indices.append(all_indices[indices[0].cpu().numpy()])
all_indices = np.delete(all_indices, indices[0].cpu().numpy())
bulyan_cluster = remaining_updates[indices[0]][None, :] if not len(
bulyan_cluster) else torch.cat((bulyan_cluster, remaining_updates[indices[0]][None, :]), 0)
remaining_updates = torch.cat(
(remaining_updates[:indices[0]], remaining_updates[indices[0] + 1:]), 0)
# print('dim of bulyan cluster ', bulyan_cluster.shape)
n, d = bulyan_cluster.shape
param_med = torch.median(bulyan_cluster, dim=0)[0]
sort_idx = torch.argsort(torch.abs(bulyan_cluster - param_med), dim=0)
sorted_params = bulyan_cluster[sort_idx, torch.arange(d)[None, :]]
return torch.mean(sorted_params[:n - 2 * n_attackers], dim=0), np.array(candidate_indices)
def tr_mean(all_updates, n_attackers):
sorted_updates = torch.sort(all_updates, 0)[0]
out = torch.mean(sorted_updates[n_attackers:-n_attackers],
0) if n_attackers else torch.mean(sorted_updates, 0)
return out
class Helper:
def __init__(self, current_time, params, name):
self.current_time = current_time
self.target_model = None
self.local_model = None
self.train_data = None
self.test_data = None
self.poisoned_data = None
self.test_data_poison = None
self.params = params
self.name = name
self.best_loss = math.inf
self.pre_path = self.params['pre_path']
try:
os.mkdir(self.pre_path)
except FileExistsError:
logger.info('Folder already exists')
self.folder_path = f'{self.pre_path}/model_{self.name}_{current_time}'
try:
os.mkdir(self.folder_path)
except FileExistsError:
logger.info('Folder already exists')
logger.addHandler(logging.FileHandler(
filename=f'{self.folder_path}/log.txt'))
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
logger.info(f'current path: {self.folder_path}')
if not self.params.get('environment_name', False):
self.params['environment_name'] = self.name
self.params['current_time'] = self.current_time
self.params['folder_path'] = self.folder_path
def save_checkpoint(self, state, is_best, filename='checkpoint.pth.tar'):
if not self.params['save_model']:
return False
torch.save(state, filename)
if is_best:
copyfile(filename, 'model_best.pth.tar')
@staticmethod
def compute_update_norm(weights_update):
squared_sum = 0
for name, data in weights_update.items():
squared_sum += torch.sum(torch.pow(data, 2))
return math.sqrt(squared_sum)
@staticmethod
def clip_update_norm(weights_update, max_norm):
total_norm = Helper.compute_update_norm(weights_update)
clip_coef = max_norm / (total_norm + 1e-6)
if total_norm > max_norm:
for name, data in weights_update.items():
data.mul_(clip_coef)
def compute_median_norm(self, submit_params_update_dict, agent_name_keys):
local_norms = []
for i in range(0, len(agent_name_keys)):
local_update = submit_params_update_dict[agent_name_keys[i]]
local_norms.append(self.compute_update_norm(local_update))
median_norm = np.median(local_norms)
return median_norm
def compute_median_norm_per_layer(self, submit_params_update_dict, agent_name_keys):
layers_median_norm = dict()
first_update = submit_params_update_dict[agent_name_keys[0]]
for name, _ in first_update.items():
if 'num_batches_tracked' in name:
continue
norms_all_clients = []
for i in range(0, len(agent_name_keys)):
local_update = submit_params_update_dict[agent_name_keys[i]]
norms_all_clients.append(
math.sqrt(torch.sum(torch.pow(local_update[name], 2))))
layers_median_norm[name] = np.median(norms_all_clients)
return layers_median_norm
def set_max_norm_per_layer(self, submit_params_update_dict, agent_name_keys, max_norm):
layers_norm = dict()
first_update = submit_params_update_dict[agent_name_keys[0]]
for name, _ in first_update.items():
layers_norm[name] = max_norm
return layers_norm
def fedavg_clientdp_per_layer(self, submit_params_update_dict, agent_name_keys, layers_clip_norm, target_model):
"""
Perform FedAvg algorithm on model params
"""
# clip
if self.params['withDP'] == True:
for i in range(0, len(agent_name_keys)):
local_update = submit_params_update_dict[agent_name_keys[i]]
self.clip_update_norm_per_layer(local_update, layers_clip_norm)
# init the data structure
agg_params_update = dict()
for name, data in target_model.state_dict().items():
if 'num_batches_tracked' in name:
continue
agg_params_update[name] = torch.zeros_like(data)
# avg
for name, data in agg_params_update.items():
# avg
for i in range(0, len(agent_name_keys)):
client_params_update = submit_params_update_dict[agent_name_keys[i]]
temp = client_params_update[name]
data.add_(temp)
# add noise
if self.params['withDP'] == True:
noise = torch.cuda.FloatTensor(data.shape).normal_(
mean=0, std=layers_clip_norm[name] * self.params['noise_multiplier'])
data.add_(noise)
for name, layer in target_model.state_dict().items():
if 'num_batches_tracked' in name:
continue
layer.add_(agg_params_update[name] * 1.0/len(agent_name_keys))
def fedavg_clientdp(self, submit_params_update_dict, agent_name_keys, clip_norm, target_model):
"""
Perform FedAvg algorithm on model params
"""
# clip
if self.params['withDP'] == True:
for i in range(0, len(agent_name_keys)):
local_update = submit_params_update_dict[agent_name_keys[i]]
self.clip_update_norm(local_update, clip_norm)
# init the data structure
agg_params_update = dict()
for name, data in target_model.state_dict().items():
if 'num_batches_tracked' in name:
continue
agg_params_update[name] = torch.zeros_like(data)
# avg
for name, data in agg_params_update.items():
# avg
for i in range(0, len(agent_name_keys)):
client_params_update = submit_params_update_dict[agent_name_keys[i]]
temp = client_params_update[name]
data.add_(temp)
# add noise
if self.params['withDP'] == True:
noise = torch.cuda.FloatTensor(data.shape).normal_(
mean=0, std=clip_norm * self.params['noise_multiplier'])
data.add_(noise)
for name, layer in target_model.state_dict().items():
if 'num_batches_tracked' in name:
continue
layer.add_(agg_params_update[name] * 1.0/len(agent_name_keys))
def average_models_params(self, submit_params_update_dict, agent_name_keys, target_model):
"""
Perform FedAvg algorithm on model params
"""
# init the data structure
agg_params_update = dict()
for name, data in target_model.state_dict().items():
if 'num_batches_tracked' in name:
continue
agg_params_update[name] = torch.zeros_like(data)
# avg
for name, data in agg_params_update.items():
# avg
for i in range(0, len(agent_name_keys)):
client_params_update = submit_params_update_dict[agent_name_keys[i]]
temp = client_params_update[name]
data.add_(temp)
for name, layer in target_model.state_dict().items():
if 'num_batches_tracked' in name:
continue
layer.add_(agg_params_update[name] * 1.0/len(agent_name_keys))
def save_model_for_certify(self, model=None, epoch=0, run_idx=0):
if model is None:
model = self.target_model
if self.params['save_model']:
model_name = '{0}/model.pt.tar'.format(self.params['folder_path'])
saved_dict = {'state_dict': model.state_dict(), 'epoch': epoch,
'lr': self.params['lr']}
if epoch in self.params['save_on_epochs']:
logger.info(f'Saving model on epoch {epoch}')
self.save_checkpoint(
saved_dict, False, filename=f'{model_name}.epoch_{epoch}.run_{run_idx}')
def save_model(self, model=None, epoch=0, val_loss=0):
if model is None:
model = self.target_model
if self.params['save_model']:
model_name = '{0}/model_last.pt.tar'.format(
self.params['folder_path'])
saved_dict = {'state_dict': model.state_dict(), 'epoch': epoch,
'lr': self.params['lr']}
self.save_checkpoint(saved_dict, False, model_name)
if epoch % 1 == 0: # save at every epoch
logger.info(f'Saving model on epoch {epoch}')
self.save_checkpoint(
saved_dict, False, filename=f'{model_name}.epoch_{epoch}')
if val_loss < self.best_loss:
self.save_checkpoint(saved_dict, False, f'{model_name}.best')
self.best_loss = val_loss
@staticmethod
def weighted_average_oracle(points, weights):
"""Computes weighted average of atoms with specified weights
Args:
points: list, whose weighted average we wish to calculate
Each element is a list_of_np.ndarray
weights: list of weights of the same length as atoms
"""
tot_weights = torch.sum(weights)
weighted_updates = dict()
for name, data in points[0].items():
weighted_updates[name] = torch.zeros_like(data)
for w, p in zip(weights, points): # 对每一个agent
for name, data in weighted_updates.items():
temp = (w / tot_weights).float().to(config.device)
temp = temp * (p[name].float())
# temp = w / tot_weights * p[name]
if temp.dtype != data.dtype:
temp = temp.type_as(data)
data.add_(temp)
return weighted_updates
@staticmethod
def geometric_median_objective(median, points, alphas):
"""Compute geometric median objective."""
temp_sum = 0
for alpha, p in zip(alphas, points):
temp_sum += alpha * Helper.l2dist(median, p)
return temp_sum
# return sum([alpha * Helper.l2dist(median, p) for alpha, p in zip(alphas, points)])
def geometric_median_update(self, target_model, updates, maxiter=4, eps=1e-5, verbose=False, ftol=1e-6, max_update_norm=None):
"""Computes geometric median of atoms with weights alphas using Weiszfeld's Algorithm
"""
points = []
alphas = []
names = []
for name, data in updates.items():
points.append(data[1]) # update
alphas.append(data[0]) # num_samples
names.append(name)
adver_ratio = 0
for i in range(0, len(names)):
_name = names[i]
if _name in self.params['adversary_list']:
adver_ratio += alphas[i]
adver_ratio = adver_ratio / sum(alphas)
poison_fraction = adver_ratio * \
self.params['poisoning_per_batch'] / self.params['batch_size']
logger.info(
f'[rfa agg] training data poison_ratio: {adver_ratio} data num: {alphas}')
logger.info(
f'[rfa agg] considering poison per batch poison_fraction: {poison_fraction}')
alphas = np.asarray(alphas, dtype=np.float64) / sum(alphas)
alphas = torch.from_numpy(alphas).float()
# alphas.float().to(config.device)
median = Helper.weighted_average_oracle(points, alphas)
num_oracle_calls = 1
# logging
obj_val = Helper.geometric_median_objective(median, points, alphas)
logs = []
log_entry = [0, obj_val, 0, 0]
logs.append(log_entry)
if verbose:
logger.info('Starting Weiszfeld algorithm')
logger.info(log_entry)
logger.info(f'[rfa agg] init. name: {names}, weight: {alphas}')
# start
weights = torch.tensor([alpha / max(eps, Helper.l2dist(median, p)) for alpha, p in zip(alphas, points)],
dtype=alphas.dtype)
weights = weights / weights.sum()
wv = copy.deepcopy(weights)
for i in range(maxiter):
prev_median, prev_obj_val = median, obj_val
weights = torch.tensor([alpha / max(eps, Helper.l2dist(median, p)) for alpha, p in zip(alphas, points)],
dtype=alphas.dtype)
weights = weights / weights.sum()
median = Helper.weighted_average_oracle(points, weights)
num_oracle_calls += 1
obj_val = Helper.geometric_median_objective(median, points, alphas)
log_entry = [i + 1, obj_val,
(prev_obj_val - obj_val) / obj_val,
Helper.l2dist(median, prev_median)]
logs.append(log_entry)
if verbose:
logger.info(log_entry)
if abs(prev_obj_val - obj_val) < ftol * obj_val:
break
logger.info(
f'[rfa agg] iter: {i}, prev_obj_val: {prev_obj_val}, obj_val: {obj_val}, abs dis: { abs(prev_obj_val - obj_val)}')
logger.info(f'[rfa agg] iter: {i}, weight: {weights}')
wv = copy.deepcopy(weights)
alphas = [Helper.l2dist(median, p) for p in points]
update_norm = 0
for name, data in median.items():
update_norm += torch.sum(torch.pow(data, 2))
update_norm = math.sqrt(update_norm)
if max_update_norm is None or update_norm < max_update_norm:
for name, data in target_model.state_dict().items():
update_per_layer = median[name] # *(self.params["eta"]=1)
# if self.params['diff_privacy']:
# update_per_layer.add_(self.dp_noise(data, self.params['sigma']))
data.add_(update_per_layer)
is_updated = True
else:
logger.info(
'\t\t\tUpdate norm = {} is too large. Update rejected'.format(update_norm))
is_updated = False
# utils.csv_record.add_weight_result(names, wv.cpu().numpy().tolist(), alphas)
return num_oracle_calls, is_updated, names, wv.cpu().numpy().tolist(), alphas