-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
237 lines (189 loc) · 7.54 KB
/
utils.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
import numpy as onp
import os
from scipy.stats import bernoulli
import random
from EliasOmega import encode
def set_seed(seed=42):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
onp.random.seed(seed)
def rotate_func(l, n):
return l[n:] + l[:n]
def ind_sparsification(x, p):
x_sp = onp.zeros_like(x)
idx = []
for j in range(len(x)):
if bernoulli.rvs(p[j]):
idx.append(j)
x_sp[idx] = x[idx]
return x_sp
def standard_dithering(x, s, p):
# s is scalar: n_levels
y = onp.abs(x) / (onp.linalg.norm(x, ord=p) + onp.finfo(float).eps)
h = onp.minimum(1, 1 / s)
idx = y // h
upper = (idx + 1) * h
lower = idx * h
prob = (upper - y) / (upper - lower + onp.finfo(float).eps)
choice = onp.array(onp.random.uniform(size=len(x)) < prob, dtype='float')
q = lower * choice + upper * (1. - choice)
L = onp.int8(idx * choice + (idx + 1) * (1 - choice))
Q = onp.linalg.norm(x, ord=p) * onp.sign(x) * q
x_norm = onp.linalg.norm(x, ord=p)
x_sign = onp.sign(x)
bits = 32
prev_nnz = -1
code = []
nnz_lst = []
for i in range(x.size):
if L[i] > 0:
code.append(encode(L[i]))
nnz_lst.append(encode(i - prev_nnz))
bits += len(encode(i - prev_nnz)) + 1 + len(encode(L[i]))
prev_nnz = i
bits = int(onp.ceil(bits))
return bits, code, nnz_lst, x_norm, x_sign, Q
def standard_dithering_plus(x, h, p):
# h can be a scalar or a array with the same size as x
y = onp.abs(x) / (onp.linalg.norm(x, ord=p) + onp.finfo(float).eps)
idx = y // h
upper = (idx + 1) * h
lower = idx * h
prob = (upper - y) / (upper - lower + onp.finfo(float).eps)
choice = onp.array(onp.random.uniform(size=len(x)) < prob, dtype='float')
q = lower * choice + upper * (1. - choice)
L = onp.int8(idx * choice + (idx + 1) * (1 - choice))
Q = onp.linalg.norm(x, ord=p) * onp.sign(x) * q
x_norm = onp.linalg.norm(x, ord=p)
x_sign = onp.sign(x)
bits = 32
prev_nnz = -1
code = []
nnz_lst = []
for i in range(x.size):
if L[i] > 0:
code.append(encode(L[i]))
nnz_lst.append(encode(i - prev_nnz))
bits += len(encode(i - prev_nnz)) + 1 + len(encode(L[i]))
prev_nnz = i
bits = int(onp.ceil(bits))
return bits, code, nnz_lst, x_norm, x_sign, Q
def run_gd(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DCGD.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_gd_plus(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DCGD-plus.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_gd_vnl(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DCGD-vnl.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_gd_plus_fnl(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DCGD-plus-fnl.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_diana(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DIANA.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_diana_plus(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DIANA-plus.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_diana_vnl(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DIANA-vnl.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_diana_plus_fnl(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'SD-DIANA-plus-fnl.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_sparse_diana_plus(algos_path, n_workers, it_max, dataset):
file_name = algos_path + 'Sparse-DIANA-plus.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --data {3}".format(
n_workers, file_name, it_max, dataset
))
print('#', end='')
def run_block_diana(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'BL-DIANA.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_block_diana_plus(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'BL-DIANA-plus.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_block_dcgd_plus(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'BL-DCGD-plus.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_block_dcgd(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'BL-DCGD.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_block_diana_plus_fnl(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'BL-DIANA-plus-fnl.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_diag_diana_plus(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'diag-SD-DIANA-plus.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_diag_block_diana_plus(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'diag-BL-DIANA-plus.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')
def run_diag_block_diana_plus_safe(algos_path, n_workers, it_max, norm_type, dataset):
file_name = algos_path + 'diag-SD-DIANA-plus-safe.py'
os.system(
"mpiexec -n {0} python {1} --it {2} --norm_type {3} --data {4}".format(
n_workers, file_name, it_max, norm_type, dataset
))
print('#', end='')