-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoda.py
More file actions
235 lines (184 loc) · 6.61 KB
/
Copy pathmoda.py
File metadata and controls
235 lines (184 loc) · 6.61 KB
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
import math
import torch
__all__ = ["MODA"]
def _default_primal_momentum1(k):
return 1 / (k + 2)
class MODA(torch.optim.Optimizer):
def __init__(
self,
params,
lr=1e-3,
dual_momentum1=0,
dual_momentum2=0,
primal_momentum1=None,
norm: str = "Spectral",
norm_kwargs: dict = None,
):
if primal_momentum1 is None:
primal_momentum1 = _default_primal_momentum1
if norm_kwargs is None:
norm_kwargs = {}
if norm not in norm_dict:
raise ValueError(f"Invalid norm: {norm}")
if lr < 0.0:
raise ValueError(f"Invalid learning rate: {lr}")
if dual_momentum1 < 0.0 or dual_momentum2 < 0.0:
raise ValueError(f"Invalid momentum value: {dual_momentum1}, {dual_momentum2}")
defaults = dict(
lr=lr,
k=0,
dual_momentum1=dual_momentum1,
dual_momentum2=dual_momentum2,
primal_momentum1=primal_momentum1,
norm=norm,
norm_kwargs=norm_kwargs,
)
super().__init__(params, defaults)
@torch.no_grad()
def step(self):
for group in self.param_groups:
lr = group["lr"]
k = group["k"]
primal_momentum1 = group["primal_momentum1"]
dual_momentum1 = group["dual_momentum1"]
dual_momentum2 = group["dual_momentum2"]
norm_backend = norm_dict[group["norm"]](**group["norm_kwargs"])
for p in group["params"]:
g = p.grad
if g is None:
continue
state = self.state[p]
if "mom_buff" not in state:
state["mom_buff"] = torch.clone(g)
state["z0"] = torch.clone(p.data)
mom_buff = state["mom_buff"]
z = state["z0"]
mom_buff.mul_(1 - dual_momentum1).add_(g, alpha=dual_momentum1)
if dual_momentum2 != 0:
mom_buff = mom_buff.mul(1 - dual_momentum2).add(g, alpha=dual_momentum2)
update = norm_backend.lmo(mom_buff)
z = z.add(update, alpha=lr * (k+2))
p_mom1 = primal_momentum1(k) if callable(primal_momentum1) else primal_momentum1
p.data.mul_(1 - p_mom1).add_(z, alpha=p_mom1)
group["k"] = k + 1
@torch.compile
def zeropower_via_newtonschulz5(G, steps=5):
"""
Newton-Schulz iteration to compute the zeroth power / orthogonalization of G.
The quintic iteration coefficients are selected to maximize the slope at zero.
This does not produce exactly UV^T, but an empirically useful approximation.
"""
assert len(G.shape) == 2
a, b, c = (3.4445, -4.7750, 2.0315)
X = G.bfloat16()
if G.size(0) > G.size(1):
X = X.T
X = X / (X.norm() + 1e-7)
for _ in range(steps):
A = X @ X.T
B = b * A + c * A @ A
X = a * X + B @ X
if G.size(0) > G.size(1):
X = X.T
return X
class Norm:
def lmo(self, g):
raise NotImplementedError
class Spectral(Norm):
def __init__(self, steps=5):
self.steps = steps
def lmo(self, g):
g = zeropower_via_newtonschulz5(g.reshape(len(g), -1), steps=self.steps).view(g.shape)
d_out, d_in = g.shape
g *= (d_out / d_in) ** 0.5
return -g
class Sign(Norm):
def __init__(self, zero_init=False):
self.zero_init = zero_init
def lmo(self, g):
_, in_channels = g.shape
return -(1 / in_channels) * torch.sign(g)
class ColNorm(Norm):
"""
Column-wise normalization.
Args:
normalized (bool, optional): If True, normalizes by the input dimension. Use True only for non-input layers.
transpose (bool, optional): If True, transposes input before normalization. Use True for embedding layers
which store weights as (vocab_size, embedding_dim).
"""
def __init__(self, normalized=False, transpose=False):
self.normalized = normalized
self.transpose = transpose
def lmo(self, g):
eps = 1e-8
if self.transpose:
g = g.transpose(0, 1)
rms_values = 1/math.sqrt(g.size(0))*torch.sqrt(torch.sum(g ** 2, dim=0, keepdim=True))
if self.normalized:
rms_values *= g.size(1)
g = g / (rms_values + eps)
if self.transpose:
g = g.transpose(0, 1)
return -g
class RowNorm(Norm):
"""
Row-wise normalization.
Args:
normalized (bool, optional): If True, normalizes by the input dimension. Use False only for the input layer.
transpose (bool, optional): If True, transposes input before normalization. Use True for embedding layers
which store weights as (vocab_size, embedding_dim).
"""
def __init__(self, normalized=True, transpose=False):
self.normalized = normalized
self.transpose = transpose
def lmo(self, g):
eps = 1e-8
if self.transpose:
g = g.transpose(0, 1)
rms_values = torch.sqrt(torch.sum(g ** 2, dim=-1, keepdim=True))
if self.normalized:
rms_values *= math.sqrt(g.size(-1))
g = g / (rms_values + eps)
if self.transpose:
g = g.transpose(0, 1)
return -g
class BiasRMS(Norm):
def lmo(self, g):
eps = 1e-8
rms_values = torch.sqrt(torch.mean(g ** 2, dim=0, keepdim=True))
g = g / (rms_values + eps)
return -g
class SpectralConv(Norm):
def __init__(self, steps=5):
self.steps = steps
def lmo(self, g):
g = zeropower_via_newtonschulz5(g.reshape(len(g), -1), steps=self.steps).view(g.shape)
if g.ndim == 3: # Conv1d
out_channels, in_channels, k = g.shape
g *= (out_channels / in_channels)**0.5 / k
elif g.ndim == 4: # Conv2d
out_channels, in_channels, k, _ = g.shape
g *= (out_channels / in_channels)**0.5 / (k ** 2)
return -g
class Sinkhorn(Norm):
def __init__(self, steps=5):
self.steps = steps
def lmo(self, g):
eps = 1e-8
for _ in range(self.steps):
# Row Norm
row_rms = torch.sqrt(torch.mean(g**2, dim=1, keepdim=True) + eps)
g = g / row_rms
# Column Norm
col_rms = torch.sqrt(torch.mean(g**2, dim=0, keepdim=True) + eps)
g = g / col_rms
return -g
norm_dict = {
"Spectral": Spectral,
"SpectralConv": SpectralConv,
"Sign": Sign,
"ColNorm": ColNorm,
"RowNorm": RowNorm,
"BiasRMS": BiasRMS,
"Sinkhorn": Sinkhorn,
}