def _test_summary_for_replica_zero_only(self, d):
    logdir = tempfile.mkdtemp()

    def run_fn():
      """Function executed for each replica."""
      with summary_writer.as_default():
        replica_id = ds_context.get_replica_context().replica_id_in_sync_group
        return summary_ops.write("a", replica_id)

    with self.cached_session() as sess, d.scope(), \
        summary_ops.always_record_summaries():
      # We need global_step because summary writing op *always* has global_step
      # as input, even when we always record summary or never record summary.
      global_step = training_util.get_or_create_global_step()
      if not context.executing_eagerly():
        # When executing eagerly, variables are initialized immediately after
        # creation, and its initializer will be None.
        global_step.initializer.run()
      summary_ops.set_step(0)
      summary_writer = summary_ops.create_file_writer(logdir)
      output = d.extended.call_for_each_replica(run_fn)
      unwrapped = d.unwrap(output)
      if not context.executing_eagerly():
        sess.run(summary_writer.init())
        sess.run(unwrapped)
        sess.run(summary_writer.close())

      events = _events_from_logdir(self, logdir)
      # There will be 2 entries: 1 summary file header entry, and 1 entry
      # written by replica 0.
      self.assertLen(events, 2)
      self.assertEqual(events[1].summary.value[0].tag, "a")
      self.assertEqual(events[1].summary.value[0].simple_value, 0.0)
    def testWrite_usingDefaultStepVariable_fromFunction(self):
        logdir = self.get_temp_dir()
        try:
            with context.eager_mode():
                writer = summary_ops.create_file_writer(logdir)

                @def_function.function
                def f():
                    with writer.as_default():
                        summary_ops.write('tag', 1.0)

                mystep = variables.Variable(0, dtype=dtypes.int64)
                summary_ops.set_step(mystep)
                f()
                mystep.assign_add(1)
                f()
                mystep.assign(10)
                f()
            events = events_from_logdir(logdir)
            self.assertEqual(4, len(events))
            self.assertEqual(0, events[1].step)
            self.assertEqual(1, events[2].step)
            self.assertEqual(10, events[3].step)
        finally:
            # Reset to default state for other tests.
            summary_ops.set_step(None)
 def testWrite_usingDefaultStepVariable_fromLegacyGraph(self):
     logdir = self.get_temp_dir()
     try:
         with context.graph_mode():
             writer = summary_ops.create_file_writer(logdir)
             mystep = variables.Variable(0, dtype=dtypes.int64)
             summary_ops.set_step(mystep)
             with writer.as_default():
                 write_op = summary_ops.write('tag', 1.0)
             first_assign_op = mystep.assign_add(1)
             second_assign_op = mystep.assign(10)
             with self.cached_session() as sess:
                 sess.run(writer.init())
                 sess.run(mystep.initializer)
                 sess.run(write_op)
                 sess.run(first_assign_op)
                 sess.run(write_op)
                 sess.run(second_assign_op)
                 sess.run(write_op)
                 sess.run(writer.flush())
         events = events_from_logdir(logdir)
         self.assertEqual(4, len(events))
         self.assertEqual(0, events[1].step)
         self.assertEqual(1, events[2].step)
         self.assertEqual(10, events[3].step)
     finally:
         # Reset to default state for other tests.
         summary_ops.set_step(None)
    def testGetSetStep_variable_fromFunction(self):
        with context.eager_mode():
            try:

                @def_function.function
                def set_step(step):
                    summary_ops.set_step(step)
                    return summary_ops.get_step()

                @def_function.function
                def get_and_increment():
                    summary_ops.get_step().assign_add(1)
                    return summary_ops.get_step()

                mystep = variables.Variable(0)
                self.assertAllEqual(0, set_step(mystep))
                self.assertAllEqual(0, summary_ops.get_step().read_value())
                self.assertAllEqual(1, get_and_increment())
                self.assertAllEqual(2, get_and_increment())
                # Check that set_step() properly maintains reference to variable.
                del mystep
                self.assertAllEqual(3, get_and_increment())
            finally:
                # Reset to default state for other tests.
                summary_ops.set_step(None)
 def testWrite_usingDefaultStepVariable_fromLegacyGraph(self):
   logdir = self.get_temp_dir()
   try:
     with context.graph_mode():
       writer = summary_ops.create_file_writer(logdir)
       mystep = variables.Variable(0, dtype=dtypes.int64)
       summary_ops.set_step(mystep)
       with writer.as_default():
         write_op = summary_ops.write('tag', 1.0)
       first_assign_op = mystep.assign_add(1)
       second_assign_op = mystep.assign(10)
       with self.cached_session() as sess:
         sess.run(writer.init())
         sess.run(mystep.initializer)
         sess.run(write_op)
         sess.run(first_assign_op)
         sess.run(write_op)
         sess.run(second_assign_op)
         sess.run(write_op)
         sess.run(writer.flush())
     events = events_from_logdir(logdir)
     self.assertEqual(4, len(events))
     self.assertEqual(0, events[1].step)
     self.assertEqual(1, events[2].step)
     self.assertEqual(10, events[3].step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testRunMetadata_usesDefaultStep(self):
   meta = config_pb2.RunMetadata()
   try:
     summary_ops.set_step(42)
     event = self.run_metadata(name='my_name', data=meta)
     self.assertEqual(42, event.step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testRunMetadataGraph_usesDefaultStep(self):
     meta = config_pb2.RunMetadata()
     try:
         summary_ops.set_step(42)
         event = self.run_metadata_graphs(name='my_name', data=meta)
         self.assertEqual(42, event.step)
     finally:
         # Reset to default state for other tests.
         summary_ops.set_step(None)
Beispiel #8
0
 def testKerasModel_usesDefaultStep(self):
   model = Sequential(
       [Dense(10, input_shape=(100,)),
        Activation('relu', name='my_relu')])
   try:
     summary_ops.set_step(42)
     event = self.keras_model(name='my_name', data=model)
     self.assertEqual(42, event.step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testKerasModel_usesDefaultStep(self):
   model = Sequential(
       [Dense(10, input_shape=(100,)),
        Activation('relu', name='my_relu')])
   try:
     summary_ops.set_step(42)
     event = self.keras_model(name='my_name', data=model)
     self.assertEqual(42, event.step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
    def testTrace_usesDefaultStep(self):
        @def_function.function
        def f():
            x = constant_op.constant(2)
            y = constant_op.constant(3)
            return x**y

        try:
            summary_ops.set_step(42)
            event = self.run_trace(f, step=None)
            self.assertEqual(42, event.step)
        finally:
            # Reset to default state for other tests.
            summary_ops.set_step(None)
  def testTrace_usesDefaultStep(self):

    @def_function.function
    def f():
      x = constant_op.constant(2)
      y = constant_op.constant(3)
      return x**y

    try:
      summary_ops.set_step(42)
      event = self.run_trace(f, step=None)
      self.assertEqual(42, event.step)
    finally:
      # Reset to default state for other tests.
      summary_ops.set_step(None)
 def testGetSetStep_variable(self):
     with context.eager_mode():
         try:
             mystep = variables.Variable(0)
             summary_ops.set_step(mystep)
             self.assertAllEqual(0, summary_ops.get_step().read_value())
             mystep.assign_add(1)
             self.assertAllEqual(1, summary_ops.get_step().read_value())
             # Check that set_step() properly maintains reference to variable.
             del mystep
             self.assertAllEqual(1, summary_ops.get_step().read_value())
             summary_ops.get_step().assign_add(1)
             self.assertAllEqual(2, summary_ops.get_step().read_value())
         finally:
             # Reset to default state for other tests.
             summary_ops.set_step(None)
 def testGetSetStep_variable(self):
   with context.eager_mode():
     try:
       mystep = variables.Variable(0)
       summary_ops.set_step(mystep)
       self.assertAllEqual(0, summary_ops.get_step().read_value())
       mystep.assign_add(1)
       self.assertAllEqual(1, summary_ops.get_step().read_value())
       # Check that set_step() properly maintains reference to variable.
       del mystep
       self.assertAllEqual(1, summary_ops.get_step().read_value())
       summary_ops.get_step().assign_add(1)
       self.assertAllEqual(2, summary_ops.get_step().read_value())
     finally:
       # Reset to default state for other tests.
       summary_ops.set_step(None)
Beispiel #14
0
 def testWrite_usingDefaultStepVariable_fromSetAsDefault(self):
   logdir = self.get_temp_dir()
   try:
     with context.eager_mode():
       writer = summary_ops.create_file_writer(logdir)
       writer.set_as_default(step=1)
       summary_ops.write('tag', 1.0)
       writer.set_as_default(step=2)
       summary_ops.write('tag', 1.0)
       writer.set_as_default()
       summary_ops.write('tag', 1.0)
       writer.flush()
     events = events_from_logdir(logdir)
     self.assertListEqual([1, 2, 2], [e.step for e in events[1:]])
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
Beispiel #15
0
 def testWrite_usingDefaultStepVariable_fromAsDefault(self):
   logdir = self.get_temp_dir()
   try:
     with context.eager_mode():
       writer = summary_ops.create_file_writer(logdir)
       mystep = variables.Variable(1, dtype=dtypes.int64)
       with writer.as_default(step=mystep):
         summary_ops.write('tag', 1.0)
         with writer.as_default():
           mystep.assign(2)
           summary_ops.write('tag', 1.0)
           with writer.as_default(step=3):
             summary_ops.write('tag', 1.0)
           summary_ops.write('tag', 1.0)
           mystep.assign(4)
         summary_ops.write('tag', 1.0)
     events = events_from_logdir(logdir)
     self.assertListEqual([1, 2, 3, 2, 4], [e.step for e in events[1:]])
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testGetSetStep_variable_fromFunction(self):
   with context.eager_mode():
     try:
       @def_function.function
       def set_step(step):
         summary_ops.set_step(step)
         return summary_ops.get_step()
       @def_function.function
       def get_and_increment():
         summary_ops.get_step().assign_add(1)
         return summary_ops.get_step()
       mystep = variables.Variable(0)
       self.assertAllEqual(0, set_step(mystep))
       self.assertAllEqual(0, summary_ops.get_step().read_value())
       self.assertAllEqual(1, get_and_increment())
       self.assertAllEqual(2, get_and_increment())
       # Check that set_step() properly maintains reference to variable.
       del mystep
       self.assertAllEqual(3, get_and_increment())
     finally:
       # Reset to default state for other tests.
       summary_ops.set_step(None)
 def testWrite_usingDefaultStepVariable_fromFunction(self):
   logdir = self.get_temp_dir()
   try:
     with context.eager_mode():
       writer = summary_ops.create_file_writer(logdir)
       @def_function.function
       def f():
         with writer.as_default():
           summary_ops.write('tag', 1.0)
       mystep = variables.Variable(0, dtype=dtypes.int64)
       summary_ops.set_step(mystep)
       f()
       mystep.assign_add(1)
       f()
       mystep.assign(10)
       f()
     events = events_from_logdir(logdir)
     self.assertEqual(4, len(events))
     self.assertEqual(0, events[1].step)
     self.assertEqual(1, events[2].step)
     self.assertEqual(10, events[3].step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testGetSetStep(self):
   try:
     self.assertIsNone(summary_ops.get_step())
     summary_ops.set_step(1)
     # Use assertAllEqual instead of assertEqual since it works in a defun.
     self.assertAllEqual(1, summary_ops.get_step())
     summary_ops.set_step(constant_op.constant(2))
     self.assertAllEqual(2, summary_ops.get_step())
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testGetSetStep(self):
     try:
         self.assertIsNone(summary_ops.get_step())
         summary_ops.set_step(1)
         # Use assertAllEqual instead of assertEqual since it works in a defun.
         self.assertAllEqual(1, summary_ops.get_step())
         summary_ops.set_step(constant_op.constant(2))
         self.assertAllEqual(2, summary_ops.get_step())
     finally:
         # Reset to default state for other tests.
         summary_ops.set_step(None)
 def testWrite_usingDefaultStep(self):
   logdir = self.get_temp_dir()
   try:
     with context.eager_mode():
       with summary_ops.create_file_writer(logdir).as_default():
         summary_ops.set_step(1)
         summary_ops.write('tag', 1.0)
         summary_ops.set_step(2)
         summary_ops.write('tag', 1.0)
         mystep = variables.Variable(10, dtype=dtypes.int64)
         summary_ops.set_step(mystep)
         summary_ops.write('tag', 1.0)
         mystep.assign_add(1)
         summary_ops.write('tag', 1.0)
     events = events_from_logdir(logdir)
     self.assertEqual(5, len(events))
     self.assertEqual(1, events[1].step)
     self.assertEqual(2, events[2].step)
     self.assertEqual(10, events[3].step)
     self.assertEqual(11, events[4].step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testWrite_usingDefaultStep(self):
     logdir = self.get_temp_dir()
     try:
         with context.eager_mode():
             with summary_ops.create_file_writer(logdir).as_default():
                 summary_ops.set_step(1)
                 summary_ops.write('tag', 1.0)
                 summary_ops.set_step(2)
                 summary_ops.write('tag', 1.0)
                 mystep = variables.Variable(10, dtype=dtypes.int64)
                 summary_ops.set_step(mystep)
                 summary_ops.write('tag', 1.0)
                 mystep.assign_add(1)
                 summary_ops.write('tag', 1.0)
         events = events_from_logdir(logdir)
         self.assertEqual(5, len(events))
         self.assertEqual(1, events[1].step)
         self.assertEqual(2, events[2].step)
         self.assertEqual(10, events[3].step)
         self.assertEqual(11, events[4].step)
     finally:
         # Reset to default state for other tests.
         summary_ops.set_step(None)
Beispiel #22
0
 def testWrite_usingDefaultStepConstant_fromFunction(self):
   logdir = self.get_temp_dir()
   try:
     with context.eager_mode():
       writer = summary_ops.create_file_writer(logdir)
       @def_function.function
       def f():
         with writer.as_default():
           summary_ops.write('tag', 1.0)
       summary_ops.set_step(1)
       f()
       summary_ops.set_step(2)
       f()
     events = events_from_logdir(logdir)
     self.assertEqual(3, len(events))
     self.assertEqual(1, events[1].step)
     # The step value will still be 1 because the value was captured at the
     # time the function was first traced.
     self.assertEqual(1, events[2].step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testWrite_usingDefaultStepConstant_fromFunction(self):
   logdir = self.get_temp_dir()
   try:
     with context.eager_mode():
       writer = summary_ops.create_file_writer(logdir)
       @def_function.function
       def f():
         with writer.as_default():
           summary_ops.write('tag', 1.0)
       summary_ops.set_step(1)
       f()
       summary_ops.set_step(2)
       f()
     events = events_from_logdir(logdir)
     self.assertEqual(3, len(events))
     self.assertEqual(1, events[1].step)
     # The step value will still be 1 because the value was captured at the
     # time the function was first traced.
     self.assertEqual(1, events[2].step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testWrite_usingDefaultStepConstant_fromLegacyGraph(self):
   logdir = self.get_temp_dir()
   try:
     with context.graph_mode():
       writer = summary_ops.create_file_writer(logdir)
       summary_ops.set_step(1)
       with writer.as_default():
         write_op = summary_ops.write('tag', 1.0)
       summary_ops.set_step(2)
       with self.cached_session() as sess:
         sess.run(writer.init())
         sess.run(write_op)
         sess.run(write_op)
         sess.run(writer.flush())
     events = events_from_logdir(logdir)
     self.assertEqual(3, len(events))
     self.assertEqual(1, events[1].step)
     # The step value will still be 1 because the value was captured at the
     # time the graph was constructed.
     self.assertEqual(1, events[2].step)
   finally:
     # Reset to default state for other tests.
     summary_ops.set_step(None)
 def testWrite_usingDefaultStepConstant_fromLegacyGraph(self):
     logdir = self.get_temp_dir()
     try:
         with context.graph_mode():
             writer = summary_ops.create_file_writer(logdir)
             summary_ops.set_step(1)
             with writer.as_default():
                 write_op = summary_ops.write('tag', 1.0)
             summary_ops.set_step(2)
             with self.cached_session() as sess:
                 sess.run(writer.init())
                 sess.run(write_op)
                 sess.run(write_op)
                 sess.run(writer.flush())
         events = events_from_logdir(logdir)
         self.assertEqual(3, len(events))
         self.assertEqual(1, events[1].step)
         # The step value will still be 1 because the value was captured at the
         # time the graph was constructed.
         self.assertEqual(1, events[2].step)
     finally:
         # Reset to default state for other tests.
         summary_ops.set_step(None)
 def set_step(step):
     summary_ops.set_step(step)
     return summary_ops.get_step()
 def set_step(step):
   summary_ops.set_step(step)
   return summary_ops.get_step()