-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
373 lines (293 loc) · 12.7 KB
/
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
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
import torch
from torch.utils.data import Dataset, TensorDataset
from transformers import BertTokenizer
import numpy as np
import pandas as pd
from tqdm import tqdm
from copy import deepcopy
import json
import os
import pdb
from math import exp, sqrt
from scipy.special import erf
import mpmath
from mpmath import mp
def calibrateAnalyticGaussianMechanism_precision(epsilon, delta, GS, tol = 1.e-12):
"""
High-precision version of `calibrateAnalyticGaussianMechanism` function below.
Should not be used for epsilon values above 5000.
"""
if epsilon <= 1000:
mp.dps = 500 # works for epsilon = 1000
elif epsilon <= 2500 and epsilon > 1000:
mp.dps = 1100 # works for epsilon = 2500
else:
mp.dps = 2200 # works for epsilon = 5000
def Phi(t):
return 0.5*(1.0 + mpmath.erf(t/mpmath.sqrt(2.0)))
def caseA(epsilon,s):
return Phi(mpmath.sqrt(epsilon*s)) - mpmath.exp(epsilon)*Phi(-sqrt(epsilon*(s+2.0)))
def caseB(epsilon,s):
return Phi(-mpmath.sqrt(epsilon*s)) - mpmath.exp(epsilon)*Phi(-sqrt(epsilon*(s+2.0)))
def doubling_trick(predicate_stop, s_inf, s_sup):
while(not predicate_stop(s_sup)):
s_inf = s_sup
s_sup = 2.0*s_inf
return s_inf, s_sup
def binary_search(predicate_stop, predicate_left, s_inf, s_sup):
s_mid = s_inf + (s_sup-s_inf)/2.0
while(not predicate_stop(s_mid)):
if (predicate_left(s_mid)):
s_sup = s_mid
else:
s_inf = s_mid
s_mid = s_inf + (s_sup-s_inf)/2.0
return s_mid
delta_thr = caseA(epsilon, 0.0)
if (delta == delta_thr):
alpha = 1.0
else:
if (delta > delta_thr):
predicate_stop_DT = lambda s : caseA(epsilon, s) >= delta
function_s_to_delta = lambda s : caseA(epsilon, s)
predicate_left_BS = lambda s : function_s_to_delta(s) > delta
function_s_to_alpha = lambda s : mpmath.sqrt(1.0 + s/2.0) - mpmath.sqrt(s/2.0)
else:
predicate_stop_DT = lambda s : caseB(epsilon, s) <= delta
function_s_to_delta = lambda s : caseB(epsilon, s)
predicate_left_BS = lambda s : function_s_to_delta(s) < delta
function_s_to_alpha = lambda s : mpmath.sqrt(1.0 + s/2.0) + mpmath.sqrt(s/2.0)
predicate_stop_BS = lambda s : abs(function_s_to_delta(s) - delta) <= tol
s_inf, s_sup = doubling_trick(predicate_stop_DT, 0.0, 1.0)
s_final = binary_search(predicate_stop_BS, predicate_left_BS, s_inf, s_sup)
alpha = function_s_to_alpha(s_final)
sigma = alpha*GS/mpmath.sqrt(2.0*epsilon)
return float(sigma)
def calibrateAnalyticGaussianMechanism(epsilon, delta, GS, tol = 1.e-12):
""" Calibrate a Gaussian perturbation for differential privacy using the analytic Gaussian mechanism of [Balle and Wang, ICML'18]
Source: https://github.com/BorjaBalle/analytic-gaussian-mechanism
Arguments:
epsilon : target epsilon (epsilon > 0)
delta : target delta (0 < delta < 1)
GS : upper bound on L2 global sensitivity (GS >= 0)
tol : error tolerance for binary search (tol > 0)
Output:
sigma : standard deviation of Gaussian noise needed to achieve (epsilon,delta)-DP under global sensitivity GS
"""
def Phi(t):
return 0.5*(1.0 + erf(float(t)/sqrt(2.0)))
def caseA(epsilon,s):
return Phi(sqrt(epsilon*s)) - exp(epsilon)*Phi(-sqrt(epsilon*(s+2.0)))
def caseB(epsilon,s):
return Phi(-sqrt(epsilon*s)) - exp(epsilon)*Phi(-sqrt(epsilon*(s+2.0)))
def doubling_trick(predicate_stop, s_inf, s_sup):
while(not predicate_stop(s_sup)):
s_inf = s_sup
s_sup = 2.0*s_inf
return s_inf, s_sup
def binary_search(predicate_stop, predicate_left, s_inf, s_sup):
s_mid = s_inf + (s_sup-s_inf)/2.0
while(not predicate_stop(s_mid)):
if (predicate_left(s_mid)):
s_sup = s_mid
else:
s_inf = s_mid
s_mid = s_inf + (s_sup-s_inf)/2.0
return s_mid
delta_thr = caseA(epsilon, 0.0)
if (delta == delta_thr):
alpha = 1.0
else:
if (delta > delta_thr):
predicate_stop_DT = lambda s : caseA(epsilon, s) >= delta
function_s_to_delta = lambda s : caseA(epsilon, s)
predicate_left_BS = lambda s : function_s_to_delta(s) > delta
function_s_to_alpha = lambda s : sqrt(1.0 + s/2.0) - sqrt(s/2.0)
else:
predicate_stop_DT = lambda s : caseB(epsilon, s) <= delta
function_s_to_delta = lambda s : caseB(epsilon, s)
predicate_left_BS = lambda s : function_s_to_delta(s) < delta
function_s_to_alpha = lambda s : sqrt(1.0 + s/2.0) + sqrt(s/2.0)
predicate_stop_BS = lambda s : abs(function_s_to_delta(s) - delta) <= tol
s_inf, s_sup = doubling_trick(predicate_stop_DT, 0.0, 1.0)
s_final = binary_search(predicate_stop_BS, predicate_left_BS, s_inf, s_sup)
alpha = function_s_to_alpha(s_final)
sigma = alpha*GS/sqrt(2.0*epsilon)
return sigma
def get_model_type(model):
'''
Given a specified model for an experiment, return the type of model ('rnn'
or 'transformer')
'''
model_to_model_type = {
'adept': 'rnn',
'dp_bart': 'transformer',
'custom_rnn': 'rnn',
'custom_transformer': 'transformer',
'bert_downstream': 'transformer'
}
if model in model_to_model_type.keys():
model_type = model_to_model_type[model]
else:
raise Exception("Specified model not in current list of available "
"models.")
return model_type
class EarlyStopping(object):
def __init__(self, patience=10):
self.patience = patience
self.counter = 0
self.best_score = None
self.early_stop = False
def __call__(self, val_loss, checkpoint_dict, mod_name):
score = -val_loss
if self.best_score is None:
self.best_score = score
torch.save(checkpoint_dict, mod_name)
elif score < self.best_score:
self.counter += 1
print(f'EarlyStopping counter: {self.counter} out of '
f'{self.patience}')
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.counter = 0
checkpoint_dict['checkpoint_early_stopping'] = self.counter
torch.save(checkpoint_dict, mod_name)
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return super(NpEncoder, self).default(obj)
def decode_rewritten(rewritten, preprocessor, remove_special_tokens=True,
labels=False, model_type='transformer'):
if model_type == 'rnn':
decoded = decode_rewritten_rnn(
rewritten, preprocessor,
remove_special_tokens=remove_special_tokens, labels=labels)
else:
decoded = decode_rewritten_transformer(
rewritten, preprocessor,
remove_special_tokens=remove_special_tokens, labels=labels)
return decoded
def decode_rewritten_rnn(rewritten, preprocessor, remove_special_tokens=True,
labels=False):
'''
rewritten: torch tensor size batch X max_seq_len-1, type int64
preprocessor: preprocessing class from preprocessing.py
remove_special_tokens: ignore <pad>, <unk>, <sos> and <eos> tokens
decoded: list of strings, with predicted tokens separated by a space
'''
special_tokens = [0, 1, 2, 3]
decoded = []
for batch_idx in range(rewritten.shape[0]):
batch = rewritten[batch_idx, :]
if remove_special_tokens:
decoded_batch = [preprocessor.idx2word[idx.item()] for idx in batch if idx not in special_tokens]
else:
decoded_batch = [preprocessor.idx2word[idx.item()] for idx in batch]
decoded.append(decoded_batch)
if not labels:
decoded = [' '.join(batch) for batch in decoded]
# For empty strings
decoded = [doc if doc != '' else 'UNK' for doc in decoded]
return decoded
def decode_rewritten_transformer(rewritten, preprocessor,
remove_special_tokens=True, labels=False):
'''
rewritten: torch tensor size batch X max_seq_len-1, type int64
preprocessor: preprocessing class from preprocessing.py
remove_special_tokens: ignore special tokens according to huggingface's
tokenizer
decoded: list of strings, with predicted tokens separated by a space
'''
decoded = preprocessor.tokenizer.batch_decode(
rewritten, skip_special_tokens=remove_special_tokens)
if labels:
raise NotImplementedError
return decoded
def determine_neurons_to_prune(model, device='cpu', out_path='.'):
k_proj_weights = model.decoder.layers[0].encoder_attn.k_proj.weight
v_proj_weights = model.decoder.layers[0].encoder_attn.v_proj.weight
k_abs_sums = torch.sum(torch.abs(k_proj_weights), dim=0).to(device)
v_abs_sums = torch.sum(torch.abs(v_proj_weights), dim=0).to(device)
q = torch.tensor([0.25, 0.5, 0.75]).to(device)
k_quantile_values = torch.quantile(k_abs_sums, q, dim=0, keepdim=True)
k_threshold = k_quantile_values[0].item()
v_quantile_values = torch.quantile(v_abs_sums, q, dim=0, keepdim=True)
v_threshold = v_quantile_values[0].item()
k_prune_neurons = torch.where(k_abs_sums < k_threshold)[0]
v_prune_neurons = torch.where(v_abs_sums < v_threshold)[0]
print(f"Pruned neurons per token (k-projection): {k_prune_neurons.shape[0]}")
torch.save(k_prune_neurons, out_path)
return k_prune_neurons, v_prune_neurons
def add_neurons_to_prune(model, previous_k_prune_neurons, device='cpu', out_path='.'):
previous_k_prune_neurons = previous_k_prune_neurons.to(device)
k_proj_weights = model.decoder.layers[0].encoder_attn.k_proj.weight
original_indexes = torch.arange(768).to(device)
combined = torch.cat((original_indexes, previous_k_prune_neurons))
uniques, counts = combined.unique(return_counts=True)
difference = uniques[counts == 1]
k_abs_sums = torch.sum(torch.abs(k_proj_weights), dim=0)
k_abs_sums_smaller = k_abs_sums[difference]
q = torch.tensor([0.25, 0.5, 0.75]).to(device)
quantile_values = torch.quantile(k_abs_sums_smaller, q, dim=0, keepdim=True)
threshold = quantile_values[0].item()
k_prune_neurons_smaller_indexes = torch.where(
k_abs_sums_smaller < threshold)[0]
k_prune_neurons = difference[k_prune_neurons_smaller_indexes]
all_k_prune_neurons = torch.sort(torch.cat((previous_k_prune_neurons, k_prune_neurons))).values
print(f"Pruned another {k_prune_neurons.shape[0]} neurons per token (k-projection), total pruned now: {all_k_prune_neurons.shape[0]} per token")
torch.save(all_k_prune_neurons, out_path)
return all_k_prune_neurons
def load_neurons_for_pruning(in_path='.'):
k_prune_neurons = torch.load(in_path)
return k_prune_neurons
def non_intersection(tensor1, tensor2):
combined = torch.cat((tensor1, tensor2))
uniques, counts = combined.unique(return_counts=True)
non_intersection = uniques[counts == 1]
return non_intersection
def prepare_specific_experiment(ss, experiment='adept_l1norm_pretrain'):
'''
Sets up arguments to fit a given experiment, overwrites required default
values to properly fit with the experiment.
For more customizability, can leave 'experiment' as None and select one's
own parameters for the experiment.
E.g. 'adept'
'''
if experiment == 'adept_l1norm_pretrain':
ss.args.mode = 'pretrain'
ss.args.model = 'adept'
ss.args.model_type = 'rnn'
ss.args.no_clipping = False
ss.args.prepend_labels = False
ss.args.private = False
ss.args.l_norm = 1
if experiment == 'adept_l2norm_pretrain':
ss.args.mode = 'pretrain'
ss.args.model = 'adept'
ss.args.model_type = 'rnn'
ss.args.no_clipping = False
ss.args.prepend_labels = False
ss.args.private = False
ss.args.l_norm = 2
if experiment == 'adept_l1norm_rewrite':
ss.args.mode = 'rewrite'
ss.args.model = 'adept'
ss.args.model_type = 'rnn'
ss.args.prepend_labels = False
ss.args.private = True
ss.args.l_norm = 1
if experiment == 'adept_l2norm_rewrite':
ss.args.mode = 'rewrite'
ss.args.model = 'adept'
ss.args.model_type = 'rnn'
ss.args.prepend_labels = False
ss.args.private = True
ss.args.l_norm = 2
return ss