Skip to content

Commit b314bec

Browse files
committed
_val_info has a performance bug with torch tensors
Inordinate amounts of time are spent in formatting torch tesnors to strings. This is particularly problematic in the inception example with large numbers of high dimensional tensors. This cannot be alleviated by passing verbose=False to the EvaluableGraph because string formatting of the tensors happens regardless of whether they are printed. For now, I simple guarded all print statements with if verbose, for now. We should probably rewrite this code eventually to use logging and to not format tensors unless messages are set to display. Not sure the cleanest way to do this.
1 parent 0546d96 commit b314bec

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

src/modelspec/utils.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,19 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
221221
if array_format == FORMAT_TENSORFLOW:
222222
import tensorflow as tf
223223

224-
print_(
225-
" > Evaluating: [%s] which is a: %s, vs parameters: %s (using %s arrays)..."
226-
% (expr, type(expr).__name__, _params_info(parameters), array_format),
227-
verbose,
228-
)
224+
if verbose:
225+
print_(
226+
" > Evaluating: [%s] which is a: %s, vs parameters: %s (using %s arrays)..."
227+
% (expr, type(expr).__name__, _params_info(parameters), array_format),
228+
verbose,
229+
)
229230
try:
230231
if type(expr) == str and expr in parameters:
231232
expr = parameters[
232233
expr
233234
] # replace with the value in parameters & check whether it's float/int...
234-
print_("Using for that param: %s" % _val_info(expr), verbose)
235+
if verbose:
236+
print_("Using for that param: %s" % _val_info(expr), verbose)
235237

236238
if type(expr) == str:
237239
try:
@@ -250,33 +252,39 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
250252
pass
251253

252254
if type(expr) == list:
253-
print_("Returning a list in format: %s" % array_format, verbose)
255+
if verbose:
256+
print_("Returning a list in format: %s" % array_format, verbose)
254257
if array_format == FORMAT_TENSORFLOW:
255258
return tf.constant(expr, dtype=tf.float64)
256259
else:
257260
return np.array(expr)
258261

259262
if type(expr) == np.ndarray:
260-
print_("Returning a numpy array in format: %s" % array_format, verbose)
263+
if verbose:
264+
print_("Returning a numpy array in format: %s" % array_format, verbose)
261265
if array_format == FORMAT_TENSORFLOW:
262266
return tf.convert_to_tensor(expr, dtype=tf.float64)
263267
else:
264268
return np.array(expr)
265269

266270
if "Tensor" in type(expr).__name__:
267-
print_(
268-
"Returning a tensorflow Tensor in format: %s" % array_format, verbose
269-
)
271+
if verbose:
272+
print_(
273+
"Returning a tensorflow Tensor in format: %s" % array_format,
274+
verbose,
275+
)
270276
if array_format == FORMAT_NUMPY:
271277
return expr.numpy()
272278
else:
273279
return expr
274280

275281
if int(expr) == expr:
276-
print_("Returning int: %s" % int(expr), verbose)
282+
if verbose:
283+
print_("Returning int: %s" % int(expr), verbose)
277284
return int(expr)
278285
else: # will have failed if not number
279-
print_("Returning float: %s" % expr, verbose)
286+
if verbose:
287+
print_("Returning float: %s" % expr, verbose)
280288
return float(expr)
281289
except:
282290
try:
@@ -289,24 +297,33 @@ def evaluate(expr, parameters={}, rng=None, array_format=FORMAT_NUMPY, verbose=F
289297
if type(expr) == str and "numpy." in expr:
290298
parameters["numpy"] = np
291299

292-
print_(
293-
"Trying to eval [%s] with Python using %s..."
294-
% (expr, parameters.keys()),
295-
verbose,
296-
)
300+
if verbose:
301+
print_(
302+
"Trying to eval [%s] with Python using %s..."
303+
% (expr, parameters.keys()),
304+
verbose,
305+
)
297306

298307
v = eval(expr, parameters)
299-
print_("Evaluated with Python: {} = {}".format(expr, _val_info(v)), verbose)
308+
309+
if verbose:
310+
print_(
311+
"Evaluated with Python: {} = {}".format(expr, _val_info(v)), verbose
312+
)
313+
300314
if (type(v) == float or type(v) == str) and int(v) == v:
301-
print_("Returning int: %s" % int(v), verbose)
315+
316+
if verbose:
317+
print_("Returning int: %s" % int(v), verbose)
302318

303319
if array_format == FORMAT_TENSORFLOW:
304320
return tf.constant(int(v))
305321
else:
306322
return int(v)
307323
return v
308324
except Exception as e:
309-
print_(f"Returning without altering: {expr} (error: {e})", verbose)
325+
if verbose:
326+
print_(f"Returning without altering: {expr} (error: {e})", verbose)
310327
return expr
311328

312329

0 commit comments

Comments
 (0)