-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
292 lines (251 loc) · 9.32 KB
/
util.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
import re
import subprocess
import threading
import collections
import inspect
import random
import tempfile
import numpy as np
import Pyro4
import os
import uuid
import auth
import pymysql
import time
import cloudpickle
import dill as pickle
import datetime
## misc
def flip(p=0.5): return random.random() < p
def is_small_enough(max_n_nodes, n_vars, n_clauses):
return (2 * n_vars + n_clauses) < max_n_nodes
## system
def get_caller_linenum(offset=1):
frame = inspect.stack()[1 + offset]
return os.path.basename(frame[0].f_code.co_filename), frame.lineno
def get_commit():
return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip().decode('ascii')
def get_hostname(expensive=False):
# Warning: memory usage spike from the fork, may not be worth constantly re-calling
if expensive:
return subprocess.check_output(['hostname']).strip().decode('ascii')
else:
return "<unknown>"
def timeit(f, *args, **kwargs):
start_time = time.time()
result = f(*args, **kwargs)
end_time = time.time()
return result, end_time - start_time
# mariadb
def _connect():
while True:
try:
return pymysql.connect(host=auth.get_db_host(),
user=auth.get_db_user(),
password=auth.get_db_password(),
db=auth.get_db_name(),
cursorclass=pymysql.cursors.SSDictCursor)
except:
time.sleep(random.random())
continue
def db_execute(cmd, values):
conn = _connect()
try:
with conn.cursor() as cursor:
cursor.execute(cmd, values)
conn.commit()
finally:
conn.close()
def de_numpify(v):
if type(v) in [np.float32, np.float64]:
return float(v)
elif type(v) in [np.int32, np.int64]:
return int(v)
else:
return v
def db_insert(table, **kvs):
conn = _connect()
keys = ", ".join(["`%s`" % k for k in kvs])
placeholders = ", ".join(["%s" for k in kvs])
values = tuple([kvs[k] for k in kvs])
cmd = "INSERT INTO `%s` (%s) VALUES (%s)" % (table, keys, placeholders)
try:
with conn.cursor() as cursor:
cursor.execute(cmd, values)
ident = cursor.lastrowid
conn.commit()
return ident
finally:
conn.close()
def db_update(table, where_kvs, set_kvs):
conn = _connect()
sets = ",".join(["`%s` = %s" % (set_key, "%s") for set_key in set_kvs])
wheres = " AND ".join(["`%s` = %s" % (where_key, "%s") for where_key in where_kvs])
values = [set_kvs[set_key] for set_key in set_kvs] + [where_kvs[where_key] for where_key in where_kvs]
cmd = "UPDATE `%s` SET %s WHERE %s" % (table, sets, wheres)
try:
with conn.cursor() as cursor:
cursor.execute(cmd, values)
conn.commit()
finally:
conn.close()
def db_query_one(query):
conn = _connect()
try:
with conn.cursor() as cursor:
cursor.execute(query)
return cursor.fetchone()
finally:
conn.close()
def db_lookup_one(table, kvs):
conn = _connect()
where_string = " AND ".join([("`%s`" % str(k)) + " = %s" for k in kvs])
values = tuple([kvs[k] for k in kvs])
cmd = "SELECT * FROM `%s` WHERE %s;" % (table, where_string)
try:
with conn.cursor() as cursor:
cursor.execute(cmd, values)
return cursor.fetchone()
finally:
conn.close()
def db_lookup_many(query):
conn = _connect()
try:
with conn.cursor() as cursor:
cursor.execute(query)
return list(cursor.fetchall_unbuffered())
finally:
conn.close()
def db_insert_many(table, ks, vs, max_per_call=200):
keys = ", ".join(["`%s`" % k for k in ks])
placeholders = ", ".join(["%s" for k in ks])
# Not sure if this is necessary, but suspected that a very big insert caused a previous crash
i = 0
while i < len(vs):
next_vs = vs[i:i+max_per_call]
conn = _connect()
try:
with conn.cursor() as cursor:
cursor.executemany("INSERT INTO `%s` (%s) VALUES (%s)" % (table, keys, placeholders), next_vs)
conn.commit()
finally:
conn.close()
i += max_per_call
def log(msg, author=None, n_secs=None, kind='info', p=1.0, expensive=False):
if random.random() > p: return None
caller, linenum = get_caller_linenum()
db_insert(table="events",
hostname=get_hostname(expensive=expensive),
caller=caller,
linenum=linenum,
pid=os.getpid(),
tid=threading.current_thread().name,
kind=kind,
author=author,
n_secs=n_secs,
msg=msg)
#pyro4
def set_pyro_config(threadpool_size=None, threadpool_size_min=None):
Pyro4.config.SERIALIZERS_ACCEPTED = ["marshal", "json", "serpent", "pickle", "dill"]
Pyro4.config.SERIALIZER = "dill"
# TODO(dselsam): it is WAY slower with compression
Pyro4.config.COMPRESSION = False
Pyro4.config.DETAILED_TRACEBACK = True
# TODO(dselsam): these numbers need to be adjusted depending on the experiment
if threadpool_size is not None: Pyro4.config.THREADPOOL_SIZE = threadpool_size
if threadpool_size_min is not None: Pyro4.config.THREADPOOL_SIZE_MIN = threadpool_size_min
def pyro_default_ns_host():
# Note: `persist` machine is running the name server
return "10.1.1.4"
def pyro_locate(name, ns_host=None):
if ns_host is None: ns_host = pyro_default_ns_host()
with Pyro4.locateNS(host=ns_host) as ns:
return ns.lookup(name)
# numpy
def np_top_k_idxs(arr, k):
assert(k <= np.size(arr))
return arr.argsort()[-k:][::-1]
def sample_unnormalized(qs):
if np.isnan(qs).any():
raise Exception("sample_unnormalized found nans: %s" % str(qs))
qs = np.array(qs)
ps = qs / np.sum(qs)
idx = np.random.choice(len(ps), size=1, p=ps)[0]
return idx, ps[idx]
def sample_unnormalized_dict(kvs):
qs = np.zeros(len(kvs))
keys = list(kvs.keys())
for i, key in enumerate(keys):
qs[i] = kvs[key]
idx, p = sample_unnormalized(qs)
return keys[idx], p
def np_placeholder():
return np.zeros(0)
###################### azure stuff
def enqueue(aqs, qname, elem):
aqs.put_message(qname, elem)
def dequeue(aqs, qname, visibility_n_secs, wait_n_secs, logger=None):
while True:
messages = aqs.get_messages(qname, visibility_timeout=visibility_n_secs)
if messages:
assert(len(messages) == 1)
return messages[0]
else:
time.sleep(wait_n_secs)
def to_blob(bbs, bcname, prefix, x):
blob_name = "%s_%s" % (prefix, uuid.uuid4())
with tempfile.TemporaryDirectory() as tmpdir:
local_path = os.path.join(tmpdir, blob_name)
with open(local_path, 'wb') as f:
pickle.dump(x, f, protocol=pickle.HIGHEST_PROTOCOL)
bbs.create_blob_from_path(bcname, blob_name, local_path)
return blob_name
def from_blob(bbs, bcname, blob_name, delete):
with tempfile.TemporaryDirectory() as tmpdir:
local_path = os.path.join(tmpdir, blob_name)
bbs.get_blob_to_path(bcname, blob_name, local_path)
if delete: bbs.delete_blob(bcname, blob_name)
with open(local_path, 'rb') as f:
return pickle.load(f)
def gd_scratch_bcname(gd_id): return "v2gdscratch%d" % gd_id
def gd_tfr_bcname(gd_id): return "v2gdtfrs%d" % gd_id
def gd_tfr_dir(gd_id): return "/home/pv/org/projects/pumpkin/v2data/gd/id%d/tfrs" % gd_id
def checkpoint_bcname(): return "v2checkpoints"
def checkpoint_root_dir(): return "/home/pv/org/projects/pumpkin/v2data/train/checkpoints"
def checkpoint_base_dir(train_id): return "train_id%d" % train_id
def checkpoint_dir(train_id): return os.path.join(checkpoint_root_dir(), checkpoint_base_dir(train_id=train_id))
def load_checkpoint(bbs, sess, saver, train_id):
checkpoint_info = db_lookup_one(table="train_checkpoints", kvs={'train_id', train_id})
with tempfile.TemporaryDirectory() as tmpdir:
local_path = os.path.join(tmpdir, checkpoint_info['bname'])
bbs.get_blob_to_path(checkpoint_bcname(), checkpoint_info['bname'], local_path)
subprocess.run(['tar', '-xzvf', local_path])
saver.restore(sess, os.path.join(tmpdir, checkpoint_base_dir(train_id=train_id), "checkpoint"))
def iff(b1, b2):
return (((not b1) or b2) and ((not b2) or b1))
def check_make_path(path):
if not os.path.exists(path):
os.makedirs(path)
def files_with_extension(dir_path, ext=None):
if ext is None:
dataset_files = [os.path.join(dir_path, x) for x in os.listdir(dir_path)]
else:
dataset_files = [os.path.join(dir_path, x) for x in os.listdir(dir_path) if re.search("\." + ext, x)]
return dataset_files
class AccumulateMean:
def __init__(self):
self.count = 0
self.value = 0
def add(self, new_value):
self.value = (1./(self.count+1)) * ((self.value * (self.count)) + new_value)
self.count += 1
def get(self):
return self.value
def exactly_one(bools):
FOUND_FLAG = False
for b in bools:
if b:
if FOUND_FLAG:
return False
FOUND_FLAG = True
return FOUND_FLAG