-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.py
389 lines (294 loc) · 10.4 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# utility functions for memory_saving_gradients
import contextlib
import inspect
import math
import networkx as nx
import numpy as np
import os
import re
import tempfile
import time
import tensorflow as tf
from tensorflow.python.ops import gen_random_ops # for low overhead random
from tensorflow.contrib import graph_editor as ge
DEBUG_LOGGING = False
def report_memory(peak_memory, expected_peak):
"""Helper utility to print 2 memory stats side by side, used in memory
tests."""
parent_name = inspect.stack()[1][0].f_code.co_name
print("%s: peak memory: %.3f MB, "
"expected peak: %.3f MB" % (parent_name,
peak_memory/10**6,
expected_peak/10**6))
def enable_debug():
"""Turn on debug logging."""
global DEBUG_LOGGING
DEBUG_LOGGING = True
def disable_debug():
"""Turn off debug logging."""
global DEBUG_LOGGING
DEBUG_LOGGING = False
def format_ops(ops, sort_outputs=False):
"""Helper method for printing ops. Converts Tensor/Operation op to op.name,
rest to str(op)."""
if hasattr(ops, '__iter__') and not isinstance(ops, str):
l = [(op.name if hasattr(op, "name") else str(op)) for op in ops]
if sort_outputs:
return sorted(l)
return l
else:
return ops.name if hasattr(ops, "name") else str(ops)
# TODO(y): add support for empty s
def debug_print(s, *args):
"""Like logger.log, but also replaces all TensorFlow ops/tensors with their
names. Sensitive to value of DEBUG_LOGGING, see enable_debug/disable_debug
Usage:
debug_print("see tensors %s for %s", tensorlist, [1,2,3])
"""
if DEBUG_LOGGING:
formatted_args = [format_ops(arg) for arg in args]
print("DEBUG "+s % tuple(formatted_args))
def debug_print2(s, *args):
"""Like logger.log, but also replaces all TensorFlow ops/tensors with their
names. Not sensitive to value of DEBUG_LOGGING
Usage:
debug_print2("see tensors %s for %s", tensorlist, [1,2,3])
"""
formatted_args = [format_ops(arg, sort_outputs=False) for arg in args]
print("DEBUG2 "+s % tuple(formatted_args))
def print_tf_graph(graph):
"""Prints tensorflow graph in dictionary form."""
for node in graph:
for child in graph[node]:
print("%s -> %s" % (node.name, child.name))
def save_tf_graph(graph, fn):
"""Prints tensorflow graph in dictionary form."""
out = open(fn, 'w')
for node in graph:
for child in graph[node]:
out.write("%s -> %s\n" % (node.name, child.name))
out.close()
# TODO: turn lists into sets (required by toposort)
# TODO: ordered dict instead of dict
def tf_ops_to_graph(ops):
"""Creates op->children dictionary from list of TensorFlow ops."""
def flatten(l): return [item for sublist in l for item in sublist]
def children(op): return flatten(tensor.consumers() for tensor in op.outputs)
return {op: children(op) for op in ops}
def tf_ops_to_nx_graph(ops):
"""Convert Tensorflow graph to NetworkX graph."""
return nx.Graph(tf_ops_to_graph(ops))
def hash_graph(graph):
"""Convert graph nodes to hashes (networkx is_isomorphic needs integer
nodes)."""
return {hash(key): [hash(value) for value in graph[key]] for key in graph}
def graphs_isomorphic(graph1, graph2):
"""Check if two graphs are isomorphic."""
return nx.is_isomorphic(nx.Graph(hash_graph(graph1)),
nx.Graph(hash_graph(graph2)))
def set_equal(one, two):
"""Converts inputs to sets, tests for equality."""
return set(one) == set(two)
def make_caterpillar_graph(length=5, node_mbs=1):
"""Length is number of concats."""
n = node_mbs * 250000
n2 = int(math.sqrt(n))
dtype = tf.float32
def make_leaf(i):
name = "leaf"+str(i)
val = gen_random_ops._random_uniform((n2, n2), dtype, name=name)
return val
def make_merge(a, b, i):
name = "merge"+str(i)
merge_node = tf.matmul(a, b, name=name)
# nonlinear_node = tf.tanh(merge_node, name="tanh"+str(i))
#nonlinear_node = tf.identity(merge_node, name="tanh"+str(i))
return merge_node
leaf0 = make_leaf(0)
node0 = tf.identity(leaf0, name="merge0")
node = node0
nodes = [node]
for i in range(1, length+1):
leaf = make_leaf(i)
node = make_merge(node, leaf, i)
nodes.append(node)
return nodes
def make_chain_tanh(length=100, name_prefix="a", node_mbs=1):
"""Creates chain of length length. First node is Variable, rest are tanh.
Returns nodes. Note, if length is 1, there are no non-linearities in the
graph, hence gradients do not need to store any activations."""
dtype = np.float32
n = node_mbs * 250000
a0_ = tf.ones((n,), dtype=dtype)
a0 = tf.Variable(a0_, name=name_prefix+"00")
a = a0
nodes = [a]
for i in range(1, length):
name = "%s%02d"%(name_prefix, i)
a = tf.tanh(a, name=name)
nodes.append(a)
return nodes
def make_chain_tanh_constant(length=100, name_prefix="a", node_mbs=1):
"""Creates chain of length length. First node is constant, rest are tanh.
Returns list of nodes. Advantage over make_chain_tanh is that
unlike Variable, memory for constant is allocated in the same run call, so
easier to track."""
dtype = np.float32
n = node_mbs * 250000
a0 = tf.ones((n,), dtype=dtype, name=name_prefix+"00")
a = a0
nodes = [a]
for i in range(1, length):
name = "%s%02d"%(name_prefix, i)
a = tf.tanh(a, name=name)
nodes.append(a)
return nodes
def make_chain_tanh_fill(length=100, name_prefix="a", node_mbs=1):
"""Creates chain of length length. First node is Variable, rest are tanh.
Returns nodes. Note, if length is 1, there are no non-linearities in the
graph, hence gradients do not need to store any activations."""
dtype = np.float32
n = node_mbs * 250000
val = tf.constant(1, dtype=dtype)
a0 = tf.fill((n,), val)
a = a0
nodes = [a]
for i in range(1, length):
name = "%s%02d"%(name_prefix, i)
a = tf.tanh(a, name=name)
nodes.append(a)
return nodes
def make_resnet(length=100, name_prefix="a", node_mbs=1):
"""Creates resnet-like chain of length length. First node is constant,
rest are tanh. Returns list of nodes. Has length - 2 articulation points (ie
for length=3 there is 1 articulation point."""
dtype = np.float32
n = node_mbs * 250000
a0 = tf.ones((n,), dtype=dtype, name=name_prefix+"00")
a = a0
nodes = [a]
for i in range(1, length):
name = "%s%02d"%(name_prefix, i)
a_nonlin = tf.tanh(a, name=name+"_tanh")
a = tf.add(a, a_nonlin, name=name+"_add")
nodes.append(a)
return nodes
def make_resnet_custom(length=100, name_prefix="a", node_mbs=1):
"""Creates resnet-like chain of length length. First node is constant,
rest are tanh. Returns list of nodes. Has length - 2 articulation points (ie
for length=3 there is 1 articulation point."""
dtype = np.float32
n = node_mbs * 250000
a0 = tf.ones((n,), dtype=dtype, name=name_prefix+"00")
a = a0
nodes = [a]
for i in range(1, length):
name = "%s%02d"%(name_prefix, i)
a_nonlin = tf.tanh(a, name=name+"_tanh")
a = tf.sigmoid(tf.add(a, a_nonlin, name=name+"_add"))
nodes.append(a)
return nodes
STDOUT=1
STDERR=2
class capture_stderr:
"""Utility to capture output, use as follows
with util.capture_stderr() as stderr:
sess = tf.Session()
print("Captured:", stderr.getvalue()).
"""
def __init__(self, fd=STDERR):
self.fd = fd
self.prevfd = None
def __enter__(self):
t = tempfile.NamedTemporaryFile()
self.prevfd = os.dup(self.fd)
os.dup2(t.fileno(), self.fd)
return TemporaryFileHelper(t)
def __exit__(self, exc_type, exc_value, traceback):
os.dup2(self.prevfd, self.fd)
class capture_stdout:
"""Utility to capture output, use as follows
with util.capture_stderr() as stderr:
sess = tf.Session()
print("Captured:", stderr.getvalue()).
"""
def __init__(self, fd=STDOUT):
self.fd = fd
self.prevfd = None
def __enter__(self):
t = tempfile.NamedTemporaryFile()
self.prevfd = os.dup(self.fd)
os.dup2(t.fileno(), self.fd)
return TemporaryFileHelper(t)
def __exit__(self, exc_type, exc_value, traceback):
os.dup2(self.prevfd, self.fd)
class TemporaryFileHelper:
"""Provides a way to fetch contents of temporary file."""
def __init__(self, temporary_file):
self.temporary_file = temporary_file
def getvalue(self):
return open(self.temporary_file.name).read()
@contextlib.contextmanager
def capture_ops():
"""Decorator to capture ops created in the block.
with capture_ops() as ops:
# create some ops
print(ops) # => prints ops created.
"""
micros = int(time.time()*10**6)
scope_name = str(micros)
op_list = []
with tf.name_scope(scope_name):
yield op_list
g = tf.get_default_graph()
op_list.extend(ge.select_ops(scope_name+"/.*", graph=g))
def pick_every_k(l, k):
"""Picks out every k'th element from the sequence, using round()
when k is not integer."""
result = []
x = k
while round(x) < len(l):
result.append(l[int(round(x))])
print("picking ", int(round(x)))
x += k
return result
def pick_n_equispaced(l, n):
"""Picks out n points out of list, at roughly equal intervals."""
assert len(l) >= n
r = (len(l) - n)/float(n)
result = []
pos = r
while pos < len(l):
result.append(l[int(pos)])
pos += 1+r
return result
def sort(nodes, total_order, dedup=False):
"""Sorts nodes according to order provided.
Args:
nodes: nodes to sort
total_order: list of nodes in correct order
dedup: if True, also discards duplicates in nodes
Returns:
Iterable of nodes in sorted order.
"""
total_order_idx = {}
for i, node in enumerate(total_order):
total_order_idx[node] = i
if dedup:
nodes = set(nodes)
return sorted(nodes, key=lambda n: total_order_idx[n])
def to_ops(iterable):
if not is_iterable(iterable):
return iterable
return [to_op(i) for i in iterable]
def intercept_op_creation(op_type_name_to_intercept):
"""Drops into PDB when particular op type is added to graph."""
from tensorflow.python.framework import op_def_library
old_apply_op = op_def_library.OpDefLibrary.apply_op
def my_apply_op(obj, op_type_name, name=None, **keywords):
import pdb; pdb.set_trace()
print(op_type_name+"-"+str(name))
if op_type_name == op_type_name_to_intercept:
import pdb; pdb.set_trace()
return(old_apply_op(obj, op_type_name, name=name, **keywords))
op_def_library.OpDefLibrary.apply_op=my_apply_op