-
Notifications
You must be signed in to change notification settings - Fork 13
/
helper.py
160 lines (132 loc) · 5.57 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
import logging
logger = logging.getLogger('logger')
from shutil import copyfile
import math
import torch
import os
class Helper:
def __init__(self, current_time, params, name):
self.current_time = current_time
self.target_model = None
self.local_model = None
self.dataset_size = 0
self.train_dataset = None
self.test_dataset = None
self.poisoned_data = None
self.test_data_poison = None
self.params = params
self.name = name
self.best_loss = math.inf
self.folder_path = f'saved_models/model_{self.name}_{current_time}'
try:
os.mkdir(self.folder_path)
except FileExistsError:
logger.info('Folder already exists')
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_model(self, model=None, epoch=0, val_loss=0):
if model is None:
model = self.target_model
if self.params['save_model']:
# save_model
logger.info("saving 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 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}')
if val_loss < self.best_loss:
self.save_checkpoint(saved_dict, False, f'{model_name}.best')
self.best_loss = val_loss
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 norm(parameters, max_norm):
total_norm = 0
for p in parameters:
torch.sum(torch.pow(p))
clip_coef = max_norm / (total_norm + 1e-6)
for p in parameters:
p.grad.data.mul_(clip_coef)
def compute_rdp(self):
from compute_dp_sgd_privacy import apply_dp_sgd_analysis
N = self.dataset_size
logger.info(f'Dataset size: {N}. Computing RDP guarantees.')
q = self.params['batch_size'] / N # q - the sampling ratio.
orders = ([1.25, 1.5, 1.75, 2., 2.25, 2.5, 3., 3.5, 4., 4.5] +
list(range(5, 64)) + [128, 256, 512])
steps = int(math.ceil(self.params['epochs'] * N / self.params['batch_size']))
apply_dp_sgd_analysis(q, self.params['z'], steps, orders, 1e-6)
@staticmethod
def clip_grad(parameters, max_norm, norm_type=2):
parameters = list(filter(lambda p: p.grad is not None, parameters))
total_norm = 0
for p in parameters:
param_norm = p.grad.data.norm(norm_type)
total_norm += param_norm.item() ** norm_type
total_norm = total_norm ** (1. / norm_type)
clip_coef = max_norm / (total_norm + 1e-6)
if clip_coef < 1:
for p in parameters:
p.grad.data.mul_(clip_coef)
return total_norm
@staticmethod
def clip_grad_scale_by_layer_norm(parameters, max_norm, norm_type=2):
parameters = list(filter(lambda p: p.grad is not None, parameters))
total_norm_weight = 0
norm_weight = dict()
for i, p in enumerate(parameters):
param_norm = p.data.norm(norm_type)
norm_weight[i] = param_norm.item()
total_norm_weight += param_norm.item() ** norm_type
total_norm_weight = total_norm_weight ** (1. / norm_type)
total_norm = 0
norm_grad = dict()
for i, p in enumerate(parameters):
param_norm = p.grad.data.norm(norm_type)
norm_grad[i] = param_norm.item()
total_norm += param_norm.item() ** norm_type
total_norm = total_norm ** (1. / norm_type)
clip_coef = max_norm / (total_norm + 1e-6)
if clip_coef < 1:
for i, p in enumerate(parameters):
if norm_grad[i] < 1e-3:
continue
scale = norm_weight[i] / total_norm_weight
p.grad.data.mul_(math.sqrt(max_norm) * scale / norm_grad[i])
# print(total_norm)
# total_norm = 0
# norm_grad = dict()
# for i, p in enumerate(parameters):
# param_norm = p.grad.data.norm (norm_type)
# norm_grad[i] = param_norm
# total_norm += param_norm.item() ** norm_type
# total_norm = total_norm ** (1. / norm_type)
# print(total_norm)
return total_norm
@staticmethod
def get_grad_vec(model, device, requires_grad=False):
size = 0
for name, layer in model.named_parameters():
if name == 'decoder.weight':
continue
size += layer.view(-1).shape[0]
if device.type == 'cpu':
sum_var = torch.FloatTensor(size).fill_(0)
else:
sum_var = torch.cuda.FloatTensor(size).fill_(0)
size = 0
for name, layer in model.named_parameters():
if name == 'decoder.weight':
continue
sum_var[size:size + layer.view(-1).shape[0]] = (layer.grad).view(-1)
size += layer.view(-1).shape[0]
return sum_var