Skip to content

Commit fe7af72

Browse files
author
Vijay Vasudevan
committed
Merge commit for internal changes
2 parents e8aa19b + 2c4cba8 commit fe7af72

55 files changed

Lines changed: 1316 additions & 547 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎tensorflow/contrib/distributions/python/kernel_tests/bernoulli_test.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,12 @@ def testEntropyWithBatch(self):
174174
self.assertAllClose(dist.entropy().eval(), [[entropy(0.1), entropy(0.7)],
175175
[entropy(0.2), entropy(0.6)]])
176176

177-
def _testSample(self):
178-
# DISABLED: Please enable this test once b/issues/30149644 is resolved.
177+
def testSample(self):
179178
with self.test_session():
180179
p = [0.2, 0.6]
181180
dist = tf.contrib.distributions.Bernoulli(p=p)
182181
n = 1000
183-
samples = dist.sample(n)
182+
samples = dist.sample(n, seed=123)
184183
samples.set_shape([n, 2])
185184
self.assertEqual(samples.dtype, tf.int32)
186185
sample_values = samples.eval()

‎tensorflow/contrib/distributions/python/kernel_tests/normal_test.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ def testNormalSample(self):
190190
self.assertAllEqual(expected_samples_shape, samples.get_shape())
191191
self.assertAllEqual(expected_samples_shape, sample_values.shape)
192192

193-
def _testNormalSampleMultiDimensional(self):
194-
# DISABLED: Please enable this test once b/issues/30149644 is resolved.
193+
def testNormalSampleMultiDimensional(self):
195194
with self.test_session():
196195
batch_size = 2
197196
mu = tf.constant([[3.0, -3.0]] * batch_size)
@@ -200,7 +199,7 @@ def _testNormalSampleMultiDimensional(self):
200199
sigma_v = [np.sqrt(10.0), np.sqrt(15.0)]
201200
n = tf.constant(100000)
202201
normal = tf.contrib.distributions.Normal(mu=mu, sigma=sigma)
203-
samples = normal.sample(n)
202+
samples = normal.sample(n, seed=137)
204203
sample_values = samples.eval()
205204
self.assertEqual(samples.get_shape(), (100000, batch_size, 2))
206205
self.assertAllClose(sample_values[:, 0, 0].mean(), mu_v[0], atol=1e-2)

‎tensorflow/contrib/distributions/python/kernel_tests/student_t_test.py‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ def testStudentSample(self):
120120
atol=.25)
121121
self._checkKLApprox(df_v, mu_v, sigma_v, sample_values)
122122

123-
def _testStudentSampleMultiDimensional(self):
124-
# DISABLED: Please enable this test once b/issues/30149644 is resolved.
123+
def testStudentSampleMultiDimensional(self):
125124
with tf.Session():
126125
batch_size = 7
127126
df = tf.constant([[3.0, 7.0]] * batch_size)
@@ -132,7 +131,7 @@ def _testStudentSampleMultiDimensional(self):
132131
sigma_v = [np.sqrt(10.0), np.sqrt(15.0)]
133132
n = tf.constant(100000)
134133
student = tf.contrib.distributions.StudentT(df=df, mu=mu, sigma=sigma)
135-
samples = student.sample(n)
134+
samples = student.sample(n, seed=137)
136135
sample_values = samples.eval()
137136
self.assertEqual(samples.get_shape(), (100000, batch_size, 2))
138137
self.assertAllClose(sample_values[:, 0, 0].mean(), mu_v[0], atol=.15)
@@ -336,12 +335,11 @@ def testMode(self):
336335
mode = student.mode().eval()
337336
self.assertAllClose([-1., 0, 1], mode)
338337

339-
def _testPdfOfSample(self):
340-
# DISABLED: Please enable this test once b/issues/30149644 is resolved.
338+
def testPdfOfSample(self):
341339
with tf.Session() as sess:
342340
student = tf.contrib.distributions.StudentT(df=3., mu=np.pi, sigma=1.)
343341
num = 20000
344-
samples = student.sample(num)
342+
samples = student.sample(num, seed=137)
345343
pdfs = student.pdf(samples)
346344
mean = student.mean()
347345
mean_pdf = student.pdf(student.mean())
@@ -356,14 +354,13 @@ def _testPdfOfSample(self):
356354
# Verify integral over sample*pdf ~= 1.
357355
self._assertIntegral(sample_vals, pdf_vals)
358356

359-
def _testPdfOfSampleMultiDims(self):
360-
# DISABLED: Please enable this test once b/issues/30149644 is resolved.
357+
def testPdfOfSampleMultiDims(self):
361358
with tf.Session() as sess:
362359
student = tf.contrib.distributions.StudentT(df=[7., 11.],
363360
mu=[[5.], [6.]],
364361
sigma=3.)
365362
num = 50000
366-
samples = student.sample(num)
363+
samples = student.sample(num, seed=137)
367364
pdfs = student.pdf(samples)
368365
sample_vals, pdf_vals = sess.run([samples, pdfs])
369366
self.assertEqual(samples.get_shape(), (num, 2, 2))
@@ -372,10 +369,10 @@ def _testPdfOfSampleMultiDims(self):
372369
self.assertNear(6., np.mean(sample_vals[:, 1, :]), err=.03)
373370
self.assertNear(stats.t.var(7., loc=0., scale=3.), # loc d.n. effect var
374371
np.var(sample_vals[:, :, 0]),
375-
err=.3)
372+
err=.25)
376373
self.assertNear(stats.t.var(11., loc=0., scale=3.), # loc d.n. effect var
377374
np.var(sample_vals[:, :, 1]),
378-
err=.3)
375+
err=.25)
379376
self._assertIntegral(sample_vals[:, 0, 0], pdf_vals[:, 0, 0], err=0.02)
380377
self._assertIntegral(sample_vals[:, 0, 1], pdf_vals[:, 0, 1], err=0.02)
381378
self._assertIntegral(sample_vals[:, 1, 0], pdf_vals[:, 1, 0], err=0.02)

‎tensorflow/contrib/distributions/python/kernel_tests/uniform_test.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ def testUniformSample(self):
146146
self.assertFalse(np.any(sample_values[::, 1] < a2_v) or np.any(
147147
sample_values >= b_v))
148148

149-
def _testUniformSampleMultiDimensional(self):
150-
# DISABLED: Please enable this test once b/issues/30149644 is resolved.
149+
def testUniformSampleMultiDimensional(self):
151150
with self.test_session():
152151
batch_size = 2
153152
a_v = [3.0, 22.0]
@@ -159,7 +158,7 @@ def _testUniformSampleMultiDimensional(self):
159158

160159
n_v = 100000
161160
n = tf.constant(n_v)
162-
samples = uniform.sample(n)
161+
samples = uniform.sample(n, seed=138)
163162
self.assertEqual(samples.get_shape(), (n_v, batch_size, 2))
164163

165164
sample_values = samples.eval()

‎tensorflow/contrib/learn/python/learn/graph_actions.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,19 @@ def _train_internal(graph,
280280
summary_writer = (get_summary_writer(output_dir)
281281
if supervisor_is_chief else None)
282282

283-
# TODO(ipolosukhin): Replace all functionality of Supervisor with Monitors.
284-
if not supervisor_is_chief:
285-
# monitors should run only on the chief.
286-
monitors = []
287-
elif not monitors:
283+
# Add default chief monitors if none were provided.
284+
if not monitors:
288285
monitors = monitors_lib.get_default_monitors(
289286
loss_op=loss_op,
290287
summary_op=logging_ops.get_summary_op(),
291288
save_summary_steps=supervisor_save_summaries_steps,
292-
summary_writer=summary_writer)
289+
summary_writer=summary_writer) if supervisor_is_chief else []
290+
291+
# TODO(ipolosukhin): Replace all functionality of Supervisor
292+
# with Chief-Exclusive Monitors.
293+
if not supervisor_is_chief:
294+
# Prune list of monitor to the ones runnable on all workers.
295+
monitors = [monitor for monitor in monitors if monitor.run_on_all_workers]
293296

294297
if max_steps is None:
295298
max_steps = (start_step + steps) if steps else None

‎tensorflow/contrib/learn/python/learn/monitors.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class BaseMonitor(object):
9191
"""Base class for Monitors.
9292
9393
Defines basic interfaces of Monitors.
94+
Monitors can either be run on all workers or, more commonly, restricted
95+
to run exclusively on the elected chief worker.
9496
"""
9597

9698
def __init__(self):
@@ -101,6 +103,10 @@ def __init__(self):
101103
self._init_step = None
102104
self._estimator = None
103105

106+
@property
107+
def run_on_all_workers(self):
108+
return False
109+
104110
def set_estimator(self, estimator):
105111
"""A setter called automatically by the target estimator.
106112

‎tensorflow/contrib/learn/python/learn/tests/graph_actions_test.py‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# ==============================================================================
15-
1615
"""Graph actions tests."""
1716

1817
from __future__ import absolute_import
@@ -26,7 +25,10 @@
2625

2726
from tensorflow.contrib import testing
2827
from tensorflow.contrib.learn.python import learn
28+
from tensorflow.contrib.learn.python.learn.monitors import BaseMonitor
2929
from tensorflow.contrib.learn.python.learn.utils import checkpoints
30+
from tensorflow.python.framework import ops
31+
from tensorflow.python.ops import variables
3032

3133

3234
class _Feeder(object):
@@ -49,6 +51,39 @@ def feed_fn(self):
4951
return {self._tensor: value}
5052

5153

54+
class _BaseMonitorWrapper(BaseMonitor):
55+
"""Base monitor wrapper to facilitate testing.
56+
57+
This monitor can act as either chief-exclusive or non-exclusive.
58+
"""
59+
60+
def __init__(self, run_on_all_workers):
61+
super(_BaseMonitorWrapper, self).__init__()
62+
self._run_on_all_workers = run_on_all_workers
63+
self._is_active = False
64+
self._has_step = False
65+
66+
@property
67+
def run_on_all_workers(self):
68+
return self._run_on_all_workers
69+
70+
@property
71+
def is_active(self):
72+
return self._is_active
73+
74+
@property
75+
def has_step(self):
76+
return self._has_step
77+
78+
def begin(self, max_steps=None):
79+
self._is_active = True
80+
return super(_BaseMonitorWrapper, self).begin(max_steps)
81+
82+
def step_begin(self, step):
83+
self._has_step = True
84+
return super(_BaseMonitorWrapper, self).step_begin(step)
85+
86+
5287
class GraphActionsTest(tf.test.TestCase):
5388
"""Graph actions tests."""
5489

@@ -350,6 +385,56 @@ def test_train_summaries(self):
350385
expected_summaries={1: {'loss': 2.0}})
351386
self._assert_ckpt(self._output_dir, True)
352387

388+
def test_train_chief_monitor(self):
389+
with tf.Graph().as_default() as g, self.test_session(g):
390+
with tf.control_dependencies(self._build_inference_graph()):
391+
train_op = tf.assign_add(tf.contrib.framework.get_global_step(), 1)
392+
loss_op = tf.constant(2.0)
393+
tf.scalar_summary('loss', loss_op)
394+
chief_exclusive_monitor = _BaseMonitorWrapper(False)
395+
all_workers_monitor = _BaseMonitorWrapper(True)
396+
loss = learn.graph_actions.train(
397+
g, output_dir=self._output_dir, train_op=train_op, loss_op=loss_op,
398+
supervisor_is_chief=True, steps=1,
399+
monitors=[chief_exclusive_monitor, all_workers_monitor])
400+
self.assertEqual(2.0, loss)
401+
self.assertTrue(chief_exclusive_monitor.is_active and
402+
all_workers_monitor.is_active,
403+
'All monitors must have been active.')
404+
self.assertTrue(chief_exclusive_monitor.has_step and
405+
all_workers_monitor.has_step,
406+
'All monitors must have a step.')
407+
408+
def test_train_worker_monitor(self):
409+
# We need to explicitly set device due to check on non-chief workers
410+
# requiring all variables to have a device assigned.
411+
with tf.Graph().as_default() as g, g.device('/cpu:0'):
412+
global_step = tf.contrib.framework.create_global_step(g)
413+
train_op = tf.assign_add(global_step, 1)
414+
loss_op = tf.constant(2.0)
415+
tf.scalar_summary('loss', loss_op)
416+
# Add explicit "local" init op to initialize all variables
417+
# as there's no chief to init here.
418+
init_op = variables.initialize_all_variables()
419+
ops.add_to_collection(ops.GraphKeys.LOCAL_INIT_OP, init_op)
420+
# Create worker monitors where one should be active on the worker
421+
# and the other chief exclusive.
422+
chief_exclusive_monitor = _BaseMonitorWrapper(False)
423+
all_workers_monitor = _BaseMonitorWrapper(True)
424+
with self.test_session(g):
425+
loss = learn.graph_actions.train(
426+
g, output_dir=self._output_dir,
427+
global_step_tensor=global_step,
428+
train_op=train_op, loss_op=loss_op,
429+
supervisor_is_chief=False, steps=1,
430+
monitors=[chief_exclusive_monitor, all_workers_monitor])
431+
self.assertEqual(2.0, loss)
432+
self.assertTrue(not chief_exclusive_monitor.is_active and
433+
all_workers_monitor.is_active,
434+
'Only non-chief runnable monitor must have been active.')
435+
self.assertTrue(not chief_exclusive_monitor.has_step and
436+
all_workers_monitor.has_step,
437+
'Only non-chief runnable monitor must have a step.')
353438

354439
if __name__ == '__main__':
355440
tf.test.main()

‎tensorflow/contrib/session_bundle/BUILD‎

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,13 @@ filegroup(
2323
),
2424
)
2525

26-
py_library(
27-
name = "constants",
28-
srcs = ["constants.py"],
29-
srcs_version = "PY2AND3",
30-
)
26+
load("//tensorflow/core:platform/default/build_config.bzl", "tf_proto_library")
3127

3228
py_library(
3329
name = "exporter",
3430
srcs = ["exporter.py"],
3531
srcs_version = "PY2AND3",
3632
deps = [
37-
":constants",
3833
":gc",
3934
":manifest_proto_py",
4035
"//tensorflow/python:framework",
@@ -50,7 +45,6 @@ py_test(
5045
srcs_version = "PY2AND3",
5146
visibility = ["//visibility:private"],
5247
deps = [
53-
":constants",
5448
":exporter",
5549
":gc",
5650
":manifest_proto_py",
@@ -151,39 +145,6 @@ cc_test(
151145
],
152146
)
153147

154-
py_library(
155-
name = "session_bundle_py",
156-
srcs = ["session_bundle.py"],
157-
srcs_version = "PY2AND3",
158-
visibility = ["//visibility:public"],
159-
deps = [
160-
":constants",
161-
":exporter",
162-
":manifest_proto_py",
163-
"//tensorflow:tensorflow_py",
164-
"//tensorflow/core:protos_all_py",
165-
],
166-
)
167-
168-
py_test(
169-
name = "session_bundle_py_test",
170-
size = "small",
171-
srcs = [
172-
"session_bundle_test.py",
173-
],
174-
data = [
175-
"//tensorflow/contrib/session_bundle/example:half_plus_two",
176-
],
177-
main = "session_bundle_test.py",
178-
srcs_version = "PY2AND3",
179-
deps = [
180-
":manifest_proto_py",
181-
":session_bundle_py",
182-
"//tensorflow:tensorflow_py",
183-
"//tensorflow/core:protos_all_py",
184-
],
185-
)
186-
187148
cc_library(
188149
name = "signature",
189150
srcs = ["signature.cc"],
@@ -246,8 +207,6 @@ cc_library(
246207
],
247208
)
248209

249-
load("//tensorflow/core:platform/default/build_config.bzl", "tf_proto_library")
250-
251210
tf_proto_library(
252211
name = "manifest_proto",
253212
srcs = ["manifest.proto"],

‎tensorflow/contrib/session_bundle/constants.py‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)