dev0.5 version of utils
# Copyright (c) 2017 NVIDIA Corporation
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
-from six.moves import range
-from six import string_types
-import six
-import tensorflow as tf
import subprocess
-import numpy as np
import time
+
+import numpy as np
+import six
+from six import string_types
+from six.moves import range
+import tensorflow as tf
+# pylint: disable=no-name-in-module
from tensorflow.python.client import device_lib
@@ -64,6 +66,8 @@ def collect_if_horovod(value, hvd, mode='sum'):
return np.mean(values)
elif mode == 'gather':
return [item for sl in values for item in sl]
+ else:
+ raise ValueError("Incorrect mode: {}".format(mode))
def clip_last_batch(last_batch, true_size):
@@ -186,7 +190,8 @@ def iterate_data(model, sess, compute_loss, mode, verbose):
if step == 0 or len(fetches_vals) == 0 or \
(data_size > 10 and processed_batches % (data_size // 10) == 0):
deco_print("Processed {}/{} batches{}".format(
- processed_batches, data_size, ending))
+ processed_batches, data_size, ending
+ ))
else:
deco_print("Processed {} batches{}".format(processed_batches, ending),
end='\r')
@@ -199,16 +204,15 @@ def iterate_data(model, sess, compute_loss, mode, verbose):
if step > bench_start:
deco_print(
"Avg time per step{}: {:.3}s".format(
- ending, 1.0 * total_time / (step - bench_start)),
+ ending, 1.0 * total_time / (step - bench_start)
+ ),
)
if total_objects is not None:
avg_objects = 1.0 * total_objects / total_time
deco_print("Avg objects per second{}: {:.3f}".format(ending,
avg_objects))
else:
- deco_print(
- "Not enough steps for benchmarking{}".format(ending)
- )
+ deco_print("Not enough steps for benchmarking{}".format(ending))
if compute_loss:
return results_per_batch, total_loss, np.sum(total_samples)
@@ -307,12 +311,12 @@ class Logger(object):
def flatten_dict(dct):
flat_dict = {}
for key, value in dct.items():
- if isinstance(value, int) or isinstance(value, float) or \
- isinstance(value, string_types) or isinstance(value, bool):
+ if isinstance(value, (int, float, string_types, bool)):
flat_dict.update({key: value})
elif isinstance(value, dict):
flat_dict.update(
- {key + '/' + k: v for k, v in flatten_dict(dct[key]).items()})
+ {key + '/' + k: v for k, v in flatten_dict(dct[key]).items()}
+ )
return flat_dict
@@ -371,8 +375,7 @@ def text_ids_to_string(row, vocab, S_ID, EOS_ID, PAD_ID,
n = len(vocab)
if ignore_special:
f_row = []
- for i in range(0, len(row)):
- char_id = row[i]
+ for char_id in row:
if char_id == EOS_ID:
break
if char_id != PAD_ID and char_id != S_ID:
