-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatmul.py
354 lines (291 loc) · 10.5 KB
/
matmul.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
from CPD.NLS import fast_hessian_contract, CP_fastNLS_Optimizer
from CPD.common_kernels import solve_sys, get_residual
from CPD.standard_ALS import CP_DTALS_Optimizer
import argparse
import time
import numpy as np
import sys
import os
import csv
import tensors.synthetic_tensors as synthetic_tensors
from pathlib import Path
from os.path import dirname, join
parent_dir = dirname(__file__)
results_dir = join(parent_dir, 'results')
def init_matrixmul(tenpy, m1,m2,m3, seed=1):
I1 = tenpy.speye(m2)
I2 = tenpy.speye(m1)
I3 = tenpy.speye(m3)
T = tenpy.einsum("lm,ik,nj->ijklmn", I1,I2,I3)
T = T.reshape((m1*m3,m1*m2,m2*m3))
O = T
return [T, O]
def matmul(tenpy,m1,m2,m3,R,seed_start,tries,tol_init,tol_fin,method,csv_file):
[T,O] = init_matrixmul(tenpy,m1,m2,m3,seed=1)
conv_ALS = 0
conv_GN = 0
conv_Hyb = 0
t = 0
iters=0
if method =='ALS':
total_start = time.time()
for j in range(tries):
start = time.time()
seed = 1301*seed_start +131*j
np.random.seed(seed)
s =[m1*m3,m1*m2,m2*m3]
X= []
for j in range(3):
X.append(np.random.randn(s[j],R))
num_iter = 250
Regu = 1e-02
opt = CP_DTALS_Optimizer(tenpy,T,X)
for i in range(num_iter):
X = opt.step(Regu)
res = get_residual(tenpy,T,X)
if res<tol_init:
tenpy.printf("Method Converged due to initialization tolerance in ",i,"iterations")
break
opt = CP_DTALS_Optimizer(tenpy,T,X)
num_iter = 10000
Regu = 1e-02
for i in range(num_iter):
X = opt.step(Regu)
res = get_residual(tenpy,T,X)
tenpy.printf("Residual is",res)
if res < tol_fin:
tenpy.printf('Method converged due to tolerance defined for ALS',i,'iterations')
conv_ALS+=1
end = time.time()
iters += i
t+= end - start
break
if Regu>1e-10:
Regu = Regu/2
total_end = time.time()
tenpy.printf("Number of trials converged",conv_ALS)
if tenpy.is_master_proc():
# write to csv file
if csv_file is not None:
csv_writer.writerow([m1,m2,m3,R, method, tries,conv_ALS, iters/tries,total_end-total_start,t/tries,seed_start])
csv_file.flush()
if method =='GN':
cg_iters = 0
total_start = time.time()
for j in range(tries):
start = time.time()
seed = 1301*seed_start +131*j
np.random.seed(seed)
s =[m1*m3,m1*m2,m2*m3]
X= []
for j in range(3):
X.append(np.random.randn(s[j],R))
maxiter= 3*np.max(s)*R
num_iter = 100
Regu = 1e-02
opt = CP_fastNLS_Optimizer(tenpy,T,X,maxiter,cg_tol=1e-03,num=0,diag=1,Arm=0,c=0.5,tau=0.5,arm_iters=10)
for i in range(num_iter):
[A,iters,flag] = opt.step(Regu)
res = get_residual(tenpy,T,X)
if res<tol_init:
tenpy.printf("Method Converged due to initialization tolerance in ",i,"iterations")
break
maxiter= 3*np.max(s)*R
num_iter = 300
Regu = 1e-03
varying=1
lower = 1e-07
upper = 1e-03
fact= 2
opt = CP_fastNLS_Optimizer(tenpy,T,X,maxiter,cg_tol=1e-03,num=0,diag=0,Arm=0,c=0.5,tau=0.5,arm_iters=10)
decrease= True
increase=False
for i in range(num_iter):
[A,iters,flag] = opt.step(Regu)
res = get_residual(tenpy,T,X)
tenpy.printf("Residual is",res)
if res < tol_fin:
tenpy.printf('Method converged due to final tolerance',i,'iterations')
conv_GN+=1
end = time.time()
cg_iters += iters
t+= end - start
break
if varying:
if Regu < lower:
increase=True
decrease=False
if Regu > upper:
decrease= True
increase=False
if increase:
Regu = Regu*fact
elif decrease:
Regu = Regu/fact
total_end = time.time()
tenpy.printf("Number of trials converged",conv_GN)
if tenpy.is_master_proc():
# write to csv file
if csv_file is not None:
csv_writer.writerow([m1,m2,m3,R, method, tries,conv_GN, cg_iters/tries,total_end-total_start,t/tries,seed_start])
csv_file.flush()
if method == 'HYB':
total_start = time.time()
cg_iters = 0
for j in range(tries):
start = time.time()
seed = 1301*seed_start +131*j
np.random.seed(seed)
s =[m1*m3,m1*m2,m2*m3]
X= []
for j in range(3):
X.append(np.random.randn(s[j],R))
num_iter = 150
Regu = 1e-02
opt = CP_DTALS_Optimizer(tenpy,T,X)
for i in range(num_iter):
X = opt.step(Regu)
res = get_residual(tenpy,T,X)
if res<tol_init:
tenpy.printf("Method Converged due to initialization tolerance in ",i,"iterations")
break
maxiter= 3*np.max(s)*R
num_iter = 300
Regu = 1e-02
varying=1
lower = 1e-07
upper = 1e-03
fact= 2
opt = CP_fastNLS_Optimizer(tenpy,T,X,maxiter,cg_tol=0.5,num=0,diag=0,Arm=1,c=0.5,tau=0.5,arm_iters=10)
decrease= True
increase=False
for i in range(num_iter):
[A,iters,flag] = opt.step(Regu)
res = get_residual(tenpy,T,X)
tenpy.printf("Residual is",res)
if res < tol_fin:
tenpy.printf('Method converged due to final tolerance',i,'iterations')
conv_Hyb+=1
end = time.time()
cg_iters+= iters
t+= end - start
break
if varying:
if Regu < lower:
increase=True
decrease=False
if Regu > upper:
decrease= True
increase=False
if increase:
Regu = Regu*fact
elif decrease:
Regu = Regu/fact
total_end = time.time()
tenpy.printf("Number of trials converged",conv_Hyb)
if tenpy.is_master_proc():
# write to csv file
if csv_file is not None:
csv_writer.writerow([m1,m2,m3,R, method, tries,conv_Hyb, cg_iters/tries,total_end-total_start,t/tries,seed_start])
csv_file.flush()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--tlib',
default="numpy",
metavar='string',
choices=[
'ctf',
'numpy',
],
help='choose tensor library to test, choose between numpy and ctf (default: numpy)')
parser.add_argument(
'--R',
type=int,
default=4,
metavar="int",
help="Rank for matrix multiplication tensor,default is 4")
parser.add_argument(
'--m1',
type=int,
default=3,
metavar="int",
help="first dimension for matmul, default is 3")
parser.add_argument(
'--m2',
type=int,
default=3,
metavar="int",
help="second dimension for matmul, default is 3")
parser.add_argument(
'--m3',
type=int,
default=3,
metavar="int",
help="third dimension for matmul, default is 3")
parser.add_argument(
'--tol-init',
type=float,
default=0.01,
metavar="float",
help="tolerance for initialization convergence, default is 0.01")
parser.add_argument(
'--tol-fin',
type=float,
default=1e-08,
metavar="float",
help="tolerance for final convergence, default is 1e-08")
parser.add_argument(
'--method',
default='HYB',
metavar='string',
choices=[
'GN',
'ALS',
'HYB'
],
help='choose the optimization method: GN,AL,HYB (default: HYB)')
parser.add_argument(
'--seed',
type=int,
default=1,
metavar="int",
help="seed to generate random initializations")
parser.add_argument(
'--tries',
type=int,
default=5,
metavar="int",
help="number of trials")
args, _ = parser.parse_known_args()
# Set up CSV logging
csv_path = join(results_dir, 'Matmul'+'.csv')
is_new_log = not Path(csv_path).exists()
csv_file = open(csv_path, 'a')#, newline='')
csv_writer = csv.writer(
csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
tlib = args.tlib
R = args.R
m1 = args.m1
m2 = args.m2
m3 = args.m3
tries = args.tries
tol_init = args.tol_init
tol_fin=args.tol_fin
method = args.method
seed = args.seed
if tlib == "numpy":
import backend.numpy_ext as tenpy
elif tlib == "ctf":
import backend.ctf_ext as tenpy
import ctf
if tenpy.is_master_proc():
# print the arguments
for arg in vars(args) :
print( arg+':', getattr(args, arg))
# initialize the csv file
if is_new_log:
csv_writer.writerow([
'Dim1','Dim2','Dim3','Rank','Method', 'Trials', 'Converged', 'Av iterations (after initial step)','Total Time Taken','Av Time for converged iter','seed'
])
matmul(tenpy,m1,m2,m3,R,seed,tries,tol_init,tol_fin,method,csv_file)