-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptimizer.py
223 lines (177 loc) · 8.08 KB
/
optimizer.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
import torch
from torch.optim.optimizer import Optimizer, required
class SGD_without_lars(Optimizer):
r"""Implements stochastic gradient descent (optionally with momentum).
"""
def __init__(self, params, lr=required, momentum=0, weight_decay=0):
if lr is not required and lr < 0.0:
raise ValueError("Invalid learning rate: {}".format(lr))
if momentum < 0.0:
raise ValueError("Invalid momentum value: {}".format(momentum))
if weight_decay < 0.0:
raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay)
super(SGD_without_lars, self).__init__(params, defaults)
def __setstate__(self, state):
super(SGD_without_lars, self).__setstate__(state)
def step(self, closure=None):
"""Performs a single optimization step.
Arguments:
closure (callable, optional): A closure that reevaluates the model
and returns the loss.
"""
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
weight_decay = group['weight_decay']
momentum = group['momentum']
lr = group['lr']
for p in group['params']:
#torch.cuda.nvtx.range_push('trial')
if p.grad is None:
continue
d_p = p.grad.data
torch.cuda.nvtx.range_push('weight decay')
if weight_decay != 0:
d_p.add_(weight_decay, p.data)
torch.cuda.nvtx.range_pop()
# d_p.mul_(lr)
torch.cuda.nvtx.range_push('momentum')
if momentum != 0:
param_state = self.state[p]
if 'momentum_buffer' not in param_state:
buf = param_state['momentum_buffer'] = torch.clone(d_p).detach()
else:
buf = param_state['momentum_buffer']
buf.mul_(momentum).add_(d_p)
d_p = buf
torch.cuda.nvtx.range_pop()
torch.cuda.nvtx.range_push('weight update')
p.data.add_(-lr, d_p)
torch.cuda.nvtx.range_pop()
# torch.cuda.nvtx.range_pop()
return loss
class SGD_with_lars(Optimizer):
r"""Implements stochastic gradient descent (optionally with momentum).
"""
def __init__(self, params, lr=required, momentum=0, weight_decay=0, trust_coef=1.): # need to add trust coef
if lr is not required and lr < 0.0:
raise ValueError("Invalid learning rate: {}".format(lr))
if momentum < 0.0:
raise ValueError("Invalid momentum value: {}".format(momentum))
if weight_decay < 0.0:
raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
if trust_coef < 0.0:
raise ValueError("Invalid trust_coef value: {}".format(trust_coef))
defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay, trust_coef=trust_coef)
super(SGD_with_lars, self).__init__(params, defaults)
def __setstate__(self, state):
super(SGD_with_lars, self).__setstate__(state)
def step(self, closure=None):
"""Performs a single optimization step.
Arguments:
closure (callable, optional): A closure that reevaluates the model
and returns the loss.
"""
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
weight_decay = group['weight_decay']
momentum = group['momentum']
trust_coef = group['trust_coef']
global_lr = group['lr']
for p in group['params']:
if p.grad is None:
continue
d_p = p.grad.data
p_norm = torch.norm(p.data, p=2)
d_p_norm = torch.norm(d_p, p=2).add_(momentum, p_norm)
lr = torch.div(p_norm, d_p_norm).mul_(trust_coef)
lr.mul_(global_lr)
if weight_decay != 0:
d_p.add_(weight_decay, p.data)
d_p.mul_(lr)
if momentum != 0:
param_state = self.state[p]
if 'momentum_buffer' not in param_state:
buf = param_state['momentum_buffer'] = torch.clone(d_p).detach()
else:
buf = param_state['momentum_buffer']
buf.mul_(momentum).add_(d_p)
d_p = buf
p.data.add_(-1, d_p)
return loss
class SGD_with_lars_ver2(Optimizer):
r"""Implements stochastic gradient descent (optionally with momentum).
"""
def __init__(self, params, lr=required, momentum=0, weight_decay=0, trust_coef=1.): # need to add trust coef
if lr is not required and lr < 0.0:
raise ValueError("Invalid learning rate: {}".format(lr))
if momentum < 0.0:
raise ValueError("Invalid momentum value: {}".format(momentum))
if weight_decay < 0.0:
raise ValueError("Invalid weight_decay value: {}".format(weight_decay))
if trust_coef < 0.0:
raise ValueError("Invalid trust_coef value: {}".format(trust_coef))
defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay, trust_coef=trust_coef)
super(SGD_with_lars_ver2, self).__init__(params, defaults)
def __setstate__(self, state):
super(SGD_with_lars_ver2, self).__setstate__(state)
def step(self, closure=None):
"""Performs a single optimization step.
Arguments:
closure (callable, optional): A closure that reevaluates the model
and returns the loss.
"""
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
weight_decay = group['weight_decay']
momentum = group['momentum']
trust_coef = group['trust_coef']
global_lr = group['lr']
for p in group['params']:
if p.grad is None:
continue
d_p = p.grad.data
# torch.cuda.nvtx.range_push('p_norm')
p_norm = torch.norm(p.data, p=2)
# torch.cuda.nvtx.range_pop()
# print('p_norm')
# print(p_norm)
# torch.cuda.nvtx.range_push('d_p_norm')
d_p_norm = torch.norm(d_p, p=2).add_(weight_decay, p_norm)
#torch.cuda.nvtx.range_pop()
# print('d_p_norm')
# print(torch.norm(d_p, p=2))
#torch.cuda.nvtx.range_push('div')
lr = torch.div(p_norm, d_p_norm)
#torch.cuda.nvtx.range_pop()
# print('result')
# print(torch.div(p_norm, d_p_norm))
# print('')
#torch.cuda.nvtx.range_push('calculate local lr')
lr.mul_(-global_lr*trust_coef)
#torch.cuda.nvtx.range_pop()
#torch.cuda.nvtx.range_push('weight decay')
if weight_decay != 0:
d_p.add_(weight_decay, p.data)
#torch.cuda.nvtx.range_pop()
#torch.cuda.nvtx.range_push('momentum')
if momentum != 0:
param_state = self.state[p]
if 'momentum_buffer' not in param_state:
buf = param_state['momentum_buffer'] = torch.clone(d_p).detach()
else:
buf = param_state['momentum_buffer']
buf.mul_(momentum).add_(d_p)
d_p = buf
#torch.cuda.nvtx.range_pop()
#torch.cuda.nvtx.range_push('weight update')
d_p.mul_(lr)
p.data.add_(d_p)
#torch.cuda.nvtx.range_pop()
return loss