-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaggregators.py
389 lines (316 loc) · 13.9 KB
/
aggregators.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
import copy
import sys
import numpy as np
from scipy.stats import beta
from torch import nn
from logger import logPrint
from threading import Thread
from sklearn.metrics import confusion_matrix
from torch.utils.data import DataLoader
import torch
class Aggregator:
def __init__(self, clients, model, rounds, device, useAsyncClients=False):
self.model = model.to(device)
self.clients = clients
self.rounds = rounds
self.device = device
self.useAsyncClients = useAsyncClients
def trainAndTest(self, testDataset):
raise Exception("Train method should be override by child class, "
"specific to the aggregation strategy.")
def _shareModelAndTrainOnClients(self):
if self.useAsyncClients:
threads = []
for client in self.clients:
t = Thread(target=(lambda: self.__shareModelAndTrainOnClient(client)))
threads.append(t)
t.start()
for thread in threads:
thread.join()
else:
for client in self.clients:
self.__shareModelAndTrainOnClient(client)
def __shareModelAndTrainOnClient(self, client):
broadcastModel = copy.deepcopy(self.model)
client.updateModel(broadcastModel)
error, pred = client.trainModel()
def _retrieveClientModelsDict(self):
models = dict()
for client in self.clients:
# If client blocked return an the unchanged version of the model
if not client.blocked:
models[client] = client.retrieveModel()
else:
models[client] = client.model
return models
def test(self, testDataset):
dataLoader = DataLoader(testDataset, shuffle=False)
with torch.no_grad():
predLabels, testLabels = zip(*[(self.predict(self.model, x), y) for x, y in dataLoader])
predLabels = torch.tensor(predLabels, dtype=torch.long)
testLabels = torch.tensor(testLabels, dtype=torch.long)
# Confusion matrix and normalized confusion matrix
mconf = confusion_matrix(testLabels, predLabels)
errors = 1 - 1.0 * mconf.diagonal().sum() / len(testDataset)
logPrint("Error Rate: ", round(100.0 * errors, 3), "%")
return errors
# Function for computing predictions
def predict(self, net, x):
with torch.no_grad():
outputs = net(x.to(self.device))
_, predicted = torch.max(outputs.to(self.device), 1)
return predicted.to(self.device)
# Function to merge the models
@staticmethod
def _mergeModels(mOrig, mDest, alphaOrig, alphaDest):
paramsDest = mDest.named_parameters()
dictParamsDest = dict(paramsDest)
paramsOrig = mOrig.named_parameters()
for name1, param1 in paramsOrig:
if name1 in dictParamsDest:
weightedSum = alphaOrig * param1.data \
+ alphaDest * dictParamsDest[name1].data
dictParamsDest[name1].data.copy_(weightedSum)
# FEDERATED AVERAGING AGGREGATOR
class FAAggregator(Aggregator):
def trainAndTest(self, testDataset):
roundsError = torch.zeros(self.rounds)
for r in range(self.rounds):
logPrint("Round... ", r)
self._shareModelAndTrainOnClients()
models = self._retrieveClientModelsDict()
# Merge models
comb = 0.0
for client in self.clients:
self._mergeModels(models[client].to(self.device), self.model.to(self.device), client.p, comb)
comb = 1.0
roundsError[r] = self.test(testDataset)
return roundsError
# ROBUST AGGREGATION ALGORITHM - computes the median of the clients updates
class COMEDAggregator(Aggregator):
def trainAndTest(self, testDataset):
roundsError = torch.zeros(self.rounds)
for r in range(self.rounds):
logPrint("Round... ", r)
self._shareModelAndTrainOnClients()
models = self._retrieveClientModelsDict()
# Merge models
self.model = self.__medianModels(models)
roundsError[r] = self.test(testDataset)
return roundsError
def __medianModels(self, models):
client1 = self.clients[0]
model = models[client1]
modelCopy = copy.deepcopy(model)
params = model.named_parameters()
for name1, param1 in params:
m = []
for client2 in self.clients:
params2 = models[client2].named_parameters()
dictParams2 = dict(params2)
m.append(dictParams2[name1].data.view(-1).to("cpu").numpy())
# logPrint("Size: ", dictParams2[name1].data.size())
m = torch.tensor(m)
med = torch.median(m, dim=0)[0]
dictParamsm = dict(modelCopy.named_parameters())
dictParamsm[name1].data.copy_(med.view(dictParamsm[name1].data.size()))
# logPrint("Median computed, size: ", med.size())
return modelCopy.to(self.device)
class MKRUMAggregator(Aggregator):
def trainAndTest(self, testDataset):
userNo = len(self.clients)
# Number of Byzantine workers to be tolerated
f = int((userNo - 3) / 2)
th = userNo - f - 2
mk = userNo - f
roundsError = torch.zeros(self.rounds)
for r in range(self.rounds):
logPrint("Round... ", r)
self._shareModelAndTrainOnClients()
# Compute distances for all users
scores = torch.zeros(userNo)
models = self._retrieveClientModelsDict()
for client in self.clients:
distances = torch.zeros((userNo, userNo))
for client2 in self.clients:
if client.id != client2.id:
distance = self.__computeModelDistance(models[client].to(self.device),
models[client2].to(self.device))
distances[client.id - 1][client2.id - 1] = distance
dd = distances[client.id - 1][:].sort()[0]
dd = dd.cumsum(0)
scores[client.id - 1] = dd[th]
_, idx = scores.sort()
selected_users = idx[:mk - 1] + 1
# logPrint("Selected users: ", selected_users)
comb = 0.0
for client in self.clients:
if client.id in selected_users:
self._mergeModels(models[client].to(self.device), self.model.to(self.device), 1 / mk, comb)
comb = 1.0
roundsError[r] = self.test(testDataset)
return roundsError
def __computeModelDistance(self, mOrig, mDest):
paramsDest = mDest.named_parameters()
dictParamsDest = dict(paramsDest)
paramsOrig = mOrig.named_parameters()
d1 = torch.tensor([]).to(self.device)
d2 = torch.tensor([]).to(self.device)
for name1, param1 in paramsOrig:
if name1 in dictParamsDest:
d1 = torch.cat((d1, dictParamsDest[name1].data.view(-1)))
d2 = torch.cat((d2, param1.data.view(-1)))
sim = torch.norm(d1 - d2, p=2)
return sim
# ADAPTIVE FEDERATED AVERAGING
class AFAAggregator(Aggregator):
def __init__(self, clients, model, rounds, device, useAsyncClients=False):
super().__init__(clients, model, rounds, device, useAsyncClients)
self.xi = 2
self.deltaXi = 0.5
def trainAndTest(self, testDataset):
# List of malicious users blocked
maliciousBlocked = []
# List with the iteration where a malicious user was blocked
maliciousBlockedIt = []
# List of benign users blocked
benignBlocked = []
# List with the iteration where a benign user was blocked
benignBlockedIt = []
roundsError = torch.zeros(self.rounds)
for r in range(self.rounds):
logPrint("Round... ", r)
for client in self.clients:
broadcastModel = copy.deepcopy(self.model)
client.updateModel(broadcastModel)
if not client.blocked:
error, pred = client.trainModel()
models = self._retrieveClientModelsDict()
badCount = 2
slack = self.xi
while badCount != 0:
pT_epoch = 0.0
for client in self.clients:
if self.notBlockedNorBadUpdate(client):
client.pEpoch = client.n * client.score
pT_epoch = pT_epoch + client.pEpoch
for client in self.clients:
if self.notBlockedNorBadUpdate(client):
client.pEpoch = client.pEpoch / pT_epoch
comb = 0.0
for client in self.clients:
if self.notBlockedNorBadUpdate(client):
self._mergeModels(models[client].to(self.device), self.model.to(self.device), client.pEpoch,
comb)
comb = 1.0
sim = []
for client in self.clients:
if self.notBlockedNorBadUpdate(client):
client.sim = self.__modelSimilarity(self.model, models[client])
sim.append(np.asarray(client.sim.to("cpu")))
# logPrint("Similarity user ", u.id, ": ", u.sim)
sim = np.asarray(sim)
meanS = np.mean(sim)
medianS = np.median(sim)
desvS = np.std(sim)
if meanS < medianS:
th = medianS - slack * desvS
else:
th = medianS + slack * desvS
slack += self.deltaXi
badCount = 0
for client in self.clients:
if not client.badUpdate:
# Malicious self.clients are below the threshold
if meanS < medianS:
if client.sim < th:
# logPrint("Type1")
# logPrint("Bad update from user ", u.id)
client.badUpdate = True
badCount += 1
# Malicious self.clients are above the threshold
else:
if client.sim > th:
client.badUpdate = True
badCount += 1
pT = 0.0
for client in self.clients:
if not client.blocked:
self.updateUserScore(client)
client.blocked = self.checkBlockedUser(client.alpha, client.beta)
if client.blocked:
logPrint("USER ", client.id, " BLOCKED!!!")
client.p = 0
if client.byz:
maliciousBlocked.append(client.id)
maliciousBlockedIt.append(r)
else:
benignBlocked.append(client.id)
benignBlockedIt.append(r)
else:
client.p = client.n * client.score
pT = pT + client.p
for client in self.clients:
client.p = client.p / pT
# logPrint("Weight user", u.id, ": ", round(u.p,3))
# Update model with the updated scores
pT_epoch = 0.0
for client in self.clients:
if self.notBlockedNorBadUpdate(client):
client.pEpoch = client.n * client.score
pT_epoch = pT_epoch + client.pEpoch
for client in self.clients:
if self.notBlockedNorBadUpdate(client):
client.pEpoch = client.pEpoch / pT_epoch
# logPrint("Updated scores:{}".format([client.pEpoch for client in self.clients]))
comb = 0.0
for client in self.clients:
if self.notBlockedNorBadUpdate(client):
self._mergeModels(models[client].to(self.device), self.model.to(self.device), client.pEpoch, comb)
comb = 1.0
# Reset badUpdate variable
for client in self.clients:
if not client.blocked:
client.badUpdate = False
roundsError[r] = self.test(testDataset)
return roundsError
def __modelSimilarity(self, mOrig, mDest):
cos = nn.CosineSimilarity(0)
d2 = torch.tensor([]).to(self.device)
d1 = torch.tensor([]).to(self.device)
paramsOrig = mOrig.named_parameters()
paramsDest = mDest.named_parameters()
dictParamsDest = dict(paramsDest)
for name1, param1 in paramsOrig:
if name1 in dictParamsDest:
d1 = torch.cat((d1, dictParamsDest[name1].data.view(-1)))
d2 = torch.cat((d2, param1.data.view(-1)))
# d2 = param1.data
# sim = cos(d1.view(-1),d2.view(-1))
# logPrint(name1,param1.size())
# logPrint("Similarity: ",sim)
sim = cos(d1, d2)
return sim
@staticmethod
def checkBlockedUser(a, b, th=0.95):
# return beta.cdf(0.5, a, b) > th
s = beta.cdf(0.5, a, b)
blocked = False
if s > th:
blocked = True
return blocked
@staticmethod
def updateUserScore(client):
if client.badUpdate:
client.beta += 1
else:
client.alpha += 1
client.score = client.alpha / client.beta
@staticmethod
def notBlockedNorBadUpdate(client):
return client.blocked == False | client.badUpdate == False
def allAggregators():
return Aggregator.__subclasses__()
# FederatedAveraging and Adaptive Federated Averaging
def FAandAFA():
return [FAAggregator, AFAAggregator]