1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414# ==============================================================================
15-
1615"""Graph actions tests."""
1716
1817from __future__ import absolute_import
2625
2726from tensorflow .contrib import testing
2827from tensorflow .contrib .learn .python import learn
28+ from tensorflow .contrib .learn .python .learn .monitors import BaseMonitor
2929from tensorflow .contrib .learn .python .learn .utils import checkpoints
30+ from tensorflow .python .framework import ops
31+ from tensorflow .python .ops import variables
3032
3133
3234class _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+
5287class 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
354439if __name__ == '__main__' :
355440 tf .test .main ()
0 commit comments