-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefence_utils.py
274 lines (225 loc) · 9.88 KB
/
defence_utils.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
import numpy as np
import torch
import torch.nn as nn
import copy
from functools import reduce
import math
def get_krum(inputs):
'''
compute krum or multi-krum of input. O(dn^2)
input : batchsize* vector dimension * n
return
krum : batchsize* vector dimension * 1
mkrum : batchsize* vector dimension * 1
'''
inputs = inputs.unsqueeze(0).permute(0, 2, 1)
n = inputs.shape[-1]
f = n // 10 # 10% malicious points
k = n - f - 2
x = inputs.permute(0, 2, 1)
cdist = torch.cdist(x, x, p=2)
# find the k+1 nbh of each point
nbhDist, nbh = torch.topk(cdist, k + 1, largest=False)
# the point closest to its nbh
i_star = torch.argmin(nbhDist.sum(2))
mkrum = inputs[:, :, nbh[:, i_star, :].view(-1)].mean(2, keepdims=True)
return mkrum, nbh[:, i_star, :].view(-1)
def get_norm(inputs):
'''
compute krum or multi-krum of input. O(dn^2)
input : batchsize* vector dimension * n
return
krum : batchsize* vector dimension * 1
mkrum : batchsize* vector dimension * 1
'''
number_to_consider = 8
inputs = inputs.unsqueeze(0).permute(0, 2, 1)
n = inputs.shape[-1]
x = inputs.permute(0, 2, 1)
norm = x.norm(2, dim=-1, keepdim=True)
norm = norm.view(-1)
sorted_norm, sorted_idx = torch.sort(norm)
used_idx = sorted_idx[:number_to_consider]
global_weight = torch.mean(x[:, used_idx, :], dim=1).view(-1)
return global_weight, used_idx
def median_opt(input):
shape = input.shape
input = input.sort()[0]
if shape[-1] % 2 != 0:
output = input[..., int((shape[-1] - 1) / 2)]
else:
output = (input[..., int(shape[-1] / 2 - 1)] + input[..., int(shape[-1] / 2)]) / 2.0
return output
def repeated_median(y):
eps = np.finfo(float).eps
num_models = y.shape[1]
total_num = y.shape[0]
y = y.sort()[0]
yyj = y.repeat(1, 1, num_models).reshape(total_num, num_models, num_models)
yyi = yyj.transpose(-1, -2)
xx = torch.FloatTensor(range(num_models)).to(y.device)
xxj = xx.repeat(total_num, num_models, 1)
xxi = xxj.transpose(-1, -2) + eps
diag = torch.Tensor([float('Inf')] * num_models).to(y.device)
diag = torch.diag(diag).repeat(total_num, 1, 1)
dividor = xxi - xxj + diag
slopes = (yyi - yyj) / dividor + diag
slopes, _ = slopes.sort()
slopes = median_opt(slopes[:, :, :-1])
slopes = median_opt(slopes)
# get intercepts (intercept of median)
yy_median = median_opt(y)
xx_median = [(num_models - 1) / 2.0] * total_num
xx_median = torch.Tensor(xx_median).to(y.device)
intercepts = yy_median - slopes * xx_median
return slopes, intercepts
def reweight_algorithm_restricted(y, LAMBDA, thresh):
num_models = y.shape[1]
total_num = y.shape[0]
slopes, intercepts = repeated_median(y)
X_pure = y.sort()[1].sort()[1].type(torch.float)
# calculate H matrix
X_pure = X_pure.unsqueeze(2)
X = torch.cat((torch.ones(total_num, num_models, 1).to(y.device), X_pure), dim=-1)
X_X = torch.matmul(X.transpose(1, 2), X)
X_X = torch.matmul(X, torch.inverse(X_X))
H = torch.matmul(X_X, X.transpose(1, 2))
diag = torch.eye(num_models).repeat(total_num, 1, 1).to(y.device)
processed_H = (torch.sqrt(1 - H) * diag).sort()[0][..., -1]
K = torch.FloatTensor([LAMBDA * np.sqrt(2. / num_models)]).to(y.device)
beta = torch.cat((intercepts.repeat(num_models, 1).transpose(0, 1).unsqueeze(2),
slopes.repeat(num_models, 1).transpose(0, 1).unsqueeze(2)), dim=-1)
line_y = (beta * X).sum(dim=-1)
residual = y - line_y
M = median_opt(residual.abs().sort()[0][..., 1:])
tau = 1.4826 * (1 + 5 / (num_models - 1)) * M + 1e-7
e = residual / tau.repeat(num_models, 1).transpose(0, 1)
reweight = processed_H / e * torch.max(-K, torch.min(K, e / processed_H))
reweight[reweight != reweight] = 1
reweight_std = reweight.std(dim=1) # its standard deviation
reshaped_std = torch.t(reweight_std.repeat(num_models, 1))
reweight_regulized = reweight * reshaped_std # reweight confidence by its standard deviation
restricted_y = y * (reweight >= thresh) + line_y * (reweight < thresh)
return reweight_regulized, restricted_y
def median_reweight_algorithm_restricted(y, LAMBDA, thresh):
num_models = y.shape[1]
total_num = y.shape[0]
X_pure = y.sort()[1].sort()[1].type(torch.float)
# calculate H matrix
X_pure = X_pure.unsqueeze(2)
X = torch.cat((torch.ones(total_num, num_models, 1).to(y.device), X_pure), dim=-1)
X_X = torch.matmul(X.transpose(1, 2), X)
X_X = torch.matmul(X, torch.inverse(X_X))
H = torch.matmul(X_X, X.transpose(1, 2))
diag = torch.eye(num_models).repeat(total_num, 1, 1).to(y.device)
processed_H = (torch.sqrt(1 - H) * diag).sort()[0][..., -1]
K = torch.FloatTensor([LAMBDA * np.sqrt(2. / num_models)]).to(y.device)
y_median = median_opt(y).unsqueeze(1).repeat(1, num_models)
residual = y - y_median
M = median_opt(residual.abs().sort()[0][..., 1:])
tau = 1.4826 * (1 + 5 / (num_models - 1)) * M + 1e-7
e = residual / tau.repeat(num_models, 1).transpose(0, 1)
reweight = processed_H / e * torch.max(-K, torch.min(K, e / processed_H))
reweight[reweight != reweight] = 1
reweight_std = reweight.std(dim=1) # its standard deviation
reshaped_std = torch.t(reweight_std.repeat(num_models, 1))
reweight_regulized = reweight * reshaped_std # reweight confidence by its standard deviation
restricted_y = y * (reweight >= thresh) + y_median * (reweight < thresh)
return reweight_regulized, restricted_y
def IRLS_median_split_restricted(w_locals, LAMBDA=2, thresh=0.1, mode='median'):
SHARD_SIZE = 2000
w = []
for net_id, net in enumerate(w_locals.values()):
net_para = net.state_dict()
w.append(net_para)
w_med = copy.deepcopy(w[0])
# w_selected = [w[i] for i in random_select(len(w))]
device = w[0][list(w[0].keys())[0]].device
reweight_sum = torch.zeros(len(w)).to(device)
for k in w_med.keys():
shape = w_med[k].shape
if len(shape) == 0:
continue
total_num = reduce(lambda x, y: x * y, shape)
y_list = torch.FloatTensor(len(w), total_num).to(device)
for i in range(len(w)):
y_list[i] = torch.reshape(w[i][k], (-1,))
transposed_y_list = torch.t(y_list)
y_result = torch.zeros_like(transposed_y_list)
assert total_num == transposed_y_list.shape[0]
if total_num < SHARD_SIZE:
reweight, restricted_y = median_reweight_algorithm_restricted(transposed_y_list, LAMBDA, thresh)
print(reweight.sum(dim=0))
reweight_sum += reweight.sum(dim=0)
y_result = restricted_y
else:
num_shards = int(math.ceil(total_num / SHARD_SIZE))
for i in range(num_shards):
y = transposed_y_list[i * SHARD_SIZE: (i + 1) * SHARD_SIZE, ...]
reweight, restricted_y = median_reweight_algorithm_restricted(y, LAMBDA, thresh)
print(reweight.sum(dim=0))
reweight_sum += reweight.sum(dim=0)
y_result[i * SHARD_SIZE: (i + 1) * SHARD_SIZE, ...] = restricted_y
# put restricted y back to w
y_result = torch.t(y_result)
for i in range(len(w)):
w[i][k] = y_result[i].reshape(w[i][k].shape).to(device)
# print(reweight_sum)
reweight_sum = reweight_sum / reweight_sum.max()
reweight_sum = reweight_sum * reweight_sum
w_med, reweight = weighted_average(w, reweight_sum)
return w_med, reweight
def weighted_average(w_list, weights):
w_avg = copy.deepcopy(w_list[0])
weights = weights / weights.sum()
assert len(weights) == len(w_list)
for k in w_avg.keys():
w_avg[k] = 0
for i in range(0, len(w_list)):
w_avg[k] += w_list[i][k] * weights[i]
# w_avg[k] = torch.div(w_avg[k], len(w_list))
return w_avg, weights
def IRLS_aggregation_split_restricted(w_locals, LAMBDA=2, thresh=0.1):
SHARD_SIZE = 2000
w = []
for net_id, net in enumerate(w_locals.values()):
net_para = net.state_dict()
w.append(net_para)
w_med = copy.deepcopy(w[0])
device = w[0][list(w[0].keys())[0]].device
reweight_sum = torch.zeros(len(w)).to(device)
for k in w_med.keys():
shape = w_med[k].shape
if len(shape) == 0:
continue
total_num = reduce(lambda x, y: x * y, shape)
y_list = torch.FloatTensor(len(w), total_num).to(device)
for i in range(len(w)):
y_list[i] = torch.reshape(w[i][k], (-1,))
transposed_y_list = torch.t(y_list)
y_result = torch.zeros_like(transposed_y_list)
if total_num < SHARD_SIZE:
reweight, restricted_y = reweight_algorithm_restricted(transposed_y_list, LAMBDA, thresh)
reweight_sum += reweight.sum(dim=0)
y_result = restricted_y
else:
num_shards = int(math.ceil(total_num / SHARD_SIZE))
for i in range(num_shards):
y = transposed_y_list[i * SHARD_SIZE: (i + 1) * SHARD_SIZE, ...]
reweight, restricted_y = reweight_algorithm_restricted(y, LAMBDA, thresh)
reweight_sum += reweight.sum(dim=0)
y_result[i * SHARD_SIZE: (i + 1) * SHARD_SIZE, ...] = restricted_y
# put restricted y back to w
y_result = torch.t(y_result)
for i in range(len(w)):
w[i][k] = y_result[i].reshape(w[i][k].shape).to(device)
reweight_sum = reweight_sum / reweight_sum.max()
reweight_sum = reweight_sum * reweight_sum
w_med, reweight = weighted_average(w, reweight_sum)
return w_med, reweight
def get_weight(model_weight):
weight_tensor_result = []
for k, v in model_weight.items():
weight_tensor_result.append(v.reshape(-1).float())
weights = torch.cat(weight_tensor_result)
return weights