-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdemo_usage_of_all_preconditioners.py
254 lines (219 loc) · 7.8 KB
/
demo_usage_of_all_preconditioners.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
"""
Demo the usages of all implemented preconditioners on the classic Tensor Rank Decomposition problem
"""
import copy
import time
import matplotlib.pyplot as plt
import preconditioned_stochastic_gradient_descent as psgd
import torch
torch.set_default_device(torch.device("cuda:0"))
for mc_trial in range(100):
# let's try a bunch of MC runs.
R, I, J, K = 10, 20, 50, 100
xyz0 = [
torch.randn(R, I), # the truth for decomposition
torch.randn(R, J),
torch.randn(R, K),
]
T = torch.einsum("ri, rj, rk->ijk", xyz0[0], xyz0[1], xyz0[2]) # the target tensor
xyz0 = [
torch.randn(R, I), # now as the initial guess for the decomposition
torch.randn(R, J),
torch.randn(R, K),
]
def f(x, y, z): # the decomposition loss
Reconstructed = torch.einsum("ri, rj, rk->ijk", x, y, z)
err = T - Reconstructed
return torch.sum(err * err)
num_iterations = 2000
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
ax1.yaxis.tick_right()
ax2.yaxis.tick_right()
"""
Gradient descent as a base line (sometimes works very well; sometimes not at all. not quite reliable)
"""
xyz = copy.deepcopy(xyz0)
[w.requires_grad_(True) for w in xyz]
opt = torch.optim.SGD(
xyz, lr=0.0001
) # diverges easily with lr=0.0005; doesn't make progress with lr=0.0002
f_values = []
t0 = time.time()
for epoch in range(num_iterations):
opt.zero_grad()
f_value = f(*xyz) + 2 ** (-23) * sum([torch.sum(p * p) for p in xyz])
f_values.append(f_value.item())
f_value.backward()
opt.step()
total_time = time.time() - t0
ax1.semilogy(f_values)
ax2.loglog(
torch.arange(1, num_iterations + 1).cpu() * total_time / num_iterations,
f_values,
)
"""
LBFGS as one more base line (more reliable than SGD, but too slow per step, and fails occasionally.)
"""
xyz = copy.deepcopy(xyz0)
[w.requires_grad_(True) for w in xyz]
opt = torch.optim.LBFGS(
xyz, lr=0.1, max_iter=10, history_size=10
) # diverges easily with lr=0.5; diverges occasionally with lr=0.2
f_values = []
t0 = time.time()
for epoch in range(num_iterations):
def closure():
opt.zero_grad()
f_value = f(*xyz) + 2 ** (-23) * sum([torch.sum(p * p) for p in xyz])
f_value.backward()
return f_value
f_values.append(opt.step(closure).item())
total_time = time.time() - t0
ax1.semilogy(f_values)
ax2.loglog(
torch.arange(1, num_iterations + 1).cpu() * total_time / num_iterations,
f_values,
)
"""
XMat (matrix-free, a very simple preconditioner, but still can solve this problem reliably)
"""
xyz = copy.deepcopy(xyz0)
[w.requires_grad_(True) for w in xyz]
opt = psgd.XMat(
xyz, preconditioner_init_scale=None, lr_params=0.2, lr_preconditioner=0.1
)
f_values = []
t0 = time.time()
for _ in range(num_iterations):
def closure():
return f(*xyz) + 2 ** (-23) * sum([torch.sum(p * p) for p in xyz])
f_values.append(opt.step(closure).item())
total_time = time.time() - t0
ax1.semilogy(f_values)
ax2.loglog(
torch.arange(1, num_iterations + 1).cpu() * total_time / num_iterations,
f_values,
)
"""
Newton method (Only for problems with roughly 100K or less params, also it needs a lot of steps to fit the Hessian.)
"""
xyz = copy.deepcopy(xyz0)
[w.requires_grad_(True) for w in xyz]
opt = psgd.Newton(
xyz, preconditioner_init_scale=None, lr_params=0.5, lr_preconditioner=0.2
)
f_values = []
t0 = time.time()
for _ in range(num_iterations):
def closure():
return f(*xyz) + 2 ** (-23) * sum([torch.sum(p * p) for p in xyz])
f_values.append(opt.step(closure).item())
total_time = time.time() - t0
ax1.semilogy(f_values)
ax2.loglog(
torch.arange(1, num_iterations + 1).cpu() * total_time / num_iterations,
f_values,
)
"""
Low-rank approximation (LRA. Very reliable and cheap.)
"""
xyz = copy.deepcopy(xyz0)
[w.requires_grad_(True) for w in xyz]
opt = psgd.LRA(
xyz, preconditioner_init_scale=None, lr_params=0.2, lr_preconditioner=0.1
)
f_values = []
t0 = time.time()
for _ in range(num_iterations):
def closure():
return f(*xyz) + 2 ** (-23) * sum([torch.sum(p * p) for p in xyz])
f_values.append(opt.step(closure).item())
total_time = time.time() - t0
ax1.semilogy(f_values)
ax2.loglog(
torch.arange(1, num_iterations + 1).cpu() * total_time / num_iterations,
f_values,
)
"""
Affine (or Kronecker product preconditioner. Reliable and converges fast.)
"""
xyz = copy.deepcopy(xyz0)
[w.requires_grad_(True) for w in xyz]
opt = psgd.Affine(
xyz, preconditioner_init_scale=None, lr_params=0.2, lr_preconditioner=0.1
)
f_values = []
t0 = time.time()
for _ in range(num_iterations):
def closure():
return f(*xyz) + 2 ** (-23) * sum([torch.sum(p * p) for p in xyz])
f_values.append(opt.step(closure).item())
total_time = time.time() - t0
ax1.semilogy(f_values)
ax2.loglog(
torch.arange(1, num_iterations + 1).cpu() * total_time / num_iterations,
f_values,
)
ax1.set_xlabel("Iterations")
ax1.set_ylabel("Fitting loss")
ax1.tick_params(labelsize=7)
ax1.legend(
[
"Gradient descent",
"LM-BFGS",
"PSGD-Xmat",
"PSGD-Newton",
"PSGD-LRA",
"PSGD-Affine",
],
fontsize=8,
)
ax1.set_title("Tensor rank decomposition benchmark", loc="left")
ax2.set_xlabel("Wall time (s)")
ax2.tick_params(labelsize=7)
# ax2.set_ylabel("Fitting loss")
ax2.legend(
[
"Gradient descent",
"LM-BFGS",
"PSGD-Xmat",
"PSGD-Newton",
"PSGD-LRA",
"PSGD-Affine",
],
fontsize=8,
)
plt.savefig(f"psgd_vs_bfgs_trial{mc_trial}.svg")
plt.show()
# """
# Lastly, there is the incomplete LU (ILU) factorization preconditioner.
# I do not wrap it as a class yet.
# Looks like LRA is a better choice than ILU.
# """
# xyz = copy.deepcopy(xyz0)
# [w.requires_grad_(True) for w in xyz]
# num_paras = sum([torch.numel(w) for w in xyz])
# r = 10 # this is order of incomplete LU factorization preconditioner
# # lower triangular matrix is [L1, 0; L2, diag(l3)]; L12 is [L1; L2]
# L12 = 0.1 * torch.cat([torch.eye(r), torch.zeros(num_paras - r, r)], dim=0)
# l3 = 0.1 * torch.ones(num_paras - r, 1)
# # upper triangular matrix is [U1, U2; 0, diag(u3)]; U12 is [U1, U2]
# U12 = 0.1 * torch.cat([torch.eye(r), torch.zeros(r, num_paras - r)], dim=1)
# u3 = 0.1 * torch.ones(num_paras - r, 1)
# f_values = []
# for _ in range(num_iterations):
# loss = f(*xyz) + 2 ** (-23) * sum([torch.sum(torch.rand_like(p) * p * p) for p in xyz])
# f_values.append(loss.item())
# grads = torch.autograd.grad(loss, xyz, create_graph=True)
# vs = [torch.randn_like(w) for w in xyz]
# Hvs = torch.autograd.grad(grads, xyz, vs)
# with torch.no_grad():
# L12, l3, U12, u3 = psgd.update_precond_splu(L12, l3, U12, u3, vs, Hvs, step=0.1)
# pre_grads = psgd.precond_grad_splu(L12, l3, U12, u3, grads)
# [w.subtract_(0.2 * g) for (w, g) in zip(xyz, pre_grads)]
# plt.semilogy(f_values)
# plt.xlabel("Iterations")
# plt.ylabel("Fitting loss")
# plt.legend(["PSGD-ILU",])
# plt.show()