def testSummaryIteratorEventsAddedAfterEndOfFile(self): test_dir = os.path.join(self.get_temp_dir(), "events") with writer.FileWriter(test_dir) as w: session_log_start = event_pb2.SessionLog.START w.add_session_log(event_pb2.SessionLog(status=session_log_start), 1) w.flush() path = glob.glob(os.path.join(test_dir, "event*"))[0] rr = summary_iterator.summary_iterator(path) # The first event should list the file_version. ev = next(rr) self.assertEqual("brain.Event:2", ev.file_version) # The next event should be the START message. ev = next(rr) self.assertEqual(1, ev.step) self.assertEqual(session_log_start, ev.session_log.status) # Reached EOF. self.assertRaises(StopIteration, lambda: next(rr)) w.add_session_log(event_pb2.SessionLog(status=session_log_start), 2) w.flush() # The new event is read, after previously seeing EOF. ev = next(rr) self.assertEqual(2, ev.step) self.assertEqual(session_log_start, ev.session_log.status) # Get EOF again. self.assertRaises(StopIteration, lambda: next(rr))
def testPluginMetadataStrippedFromSubsequentEvents(self): test_dir = self._CleanTestDir("basics") sw = self._FileWriter(test_dir) sw.add_session_log(event_pb2.SessionLog(status=SessionLog.START), 1) # We add 2 summaries with the same tags. They both have metadata. The writer # should strip the metadata from the second one. value = summary_pb2.Summary.Value(tag="foo", simple_value=10.0) value.metadata.plugin_data.plugin_name = "bar" value.metadata.plugin_data.content = compat.as_bytes("... content ...") sw.add_summary(summary_pb2.Summary(value=[value]), 10) value = summary_pb2.Summary.Value(tag="foo", simple_value=10.0) value.metadata.plugin_data.plugin_name = "bar" value.metadata.plugin_data.content = compat.as_bytes("... content ...") sw.add_summary(summary_pb2.Summary(value=[value]), 10) sw.close() rr = self._EventsReader(test_dir) # The first event should list the file_version. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals("brain.Event:2", ev.file_version) # The next event should be the START message. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(1, ev.step) self.assertEquals(SessionLog.START, ev.session_log.status) # This is the first event with tag foo. It should contain SummaryMetadata. ev = next(rr) self.assertProtoEquals( """ value { tag: "foo" simple_value: 10.0 metadata { plugin_data { plugin_name: "bar" content: "... content ..." } } } """, ev.summary) # This is the second event with tag foo. It should lack SummaryMetadata # because the file writer should have stripped it. ev = next(rr) self.assertProtoEquals( """ value { tag: "foo" simple_value: 10.0 } """, ev.summary) # We should be done. self.assertRaises(StopIteration, lambda: next(rr))
def testWithStatement(self): test_dir = self._CleanTestDir("with_statement") with self._FileWriter(test_dir) as sw: sw.add_session_log(event_pb2.SessionLog(status=SessionLog.START), 1) event_paths = sorted(glob.glob(os.path.join(test_dir, "event*"))) self.assertEquals(1, len(event_paths))
def testSessionLogStartMessageDiscardsExpiredEvents(self): """Test that SessionLog.START message discards expired events. This discard logic is preferred over the out-of-order step discard logic, but this logic can only be used for event protos which have the SessionLog enum, which was introduced to event.proto for file_version >= brain.Event:2. """ gen = _EventGenerator(self) acc = ea.EventAccumulator(gen) gen.AddEvent( event_pb2.Event(wall_time=0, step=1, file_version='brain.Event:2')) gen.AddScalar('s1', wall_time=1, step=100, value=20) gen.AddScalar('s1', wall_time=1, step=200, value=20) gen.AddScalar('s1', wall_time=1, step=300, value=20) gen.AddScalar('s1', wall_time=1, step=400, value=20) gen.AddScalar('s2', wall_time=1, step=202, value=20) gen.AddScalar('s2', wall_time=1, step=203, value=20) slog = event_pb2.SessionLog(status=event_pb2.SessionLog.START) gen.AddEvent(event_pb2.Event(wall_time=2, step=201, session_log=slog)) acc.Reload() self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 200]) self.assertEqual([x.step for x in acc.Scalars('s2')], [])
def testCloseAndReopen(self): test_dir = self._CleanTestDir("close_and_reopen") sw = self._FileWriter(test_dir) sw.add_session_log(event_pb2.SessionLog(status=SessionLog.START), 1) sw.close() # Sleep at least one second to make sure we get a new event file name. time.sleep(1.2) sw.reopen() sw.add_session_log(event_pb2.SessionLog(status=SessionLog.START), 2) sw.close() # We should now have 2 events files. event_paths = sorted(glob.glob(os.path.join(test_dir, "event*"))) self.assertEquals(2, len(event_paths)) # Check the first file contents. rr = summary_iterator.summary_iterator(event_paths[0]) # The first event should list the file_version. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals("brain.Event:2", ev.file_version) # The next event should be the START message. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(1, ev.step) self.assertEquals(SessionLog.START, ev.session_log.status) # We should be done. self.assertRaises(StopIteration, lambda: next(rr)) # Check the second file contents. rr = summary_iterator.summary_iterator(event_paths[1]) # The first event should list the file_version. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals("brain.Event:2", ev.file_version) # The next event should be the START message. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(2, ev.step) self.assertEquals(SessionLog.START, ev.session_log.status) # We should be done. self.assertRaises(StopIteration, lambda: next(rr))
def testUseAfterClose(self): test_dir = self._CleanTestDir("use_after_close") sw = self._FileWriter(test_dir) sw.close() with warnings.catch_warnings(record=True) as triggered: warnings.simplefilter("always") self.assertFalse(triggered) sw.add_summary(summary_pb2.Summary()) sw.add_session_log(event_pb2.SessionLog()) sw.add_graph(ops.Graph()) self.assertEqual(len(triggered), 3) for w in triggered: self.assertEqual(w.category, UserWarning)
def _save_fn(): """Run the saver process.""" logging.info("Saving checkpoints for %d into %s.", step, self._save_path) start_time = time.time() for l in self._listeners: l.before_save(session, step) self._get_saver().save(session, self._save_path, global_step=step) self._summary_writer.add_session_log( event_pb2.SessionLog(status=event_pb2.SessionLog.CHECKPOINT, checkpoint_path=self._save_path), step) for l in self._listeners: l.after_save(session, step) end_time = time.time() logging.info("Checkpoint actual writing time: (%.3f sec)", end_time - start_time) logging.info("Checkpoint finished for %d into %s.", step, self._save_path)
def testAddingSummaryGraphAndRunMetadata(self): test_dir = self._CleanTestDir("basics") sw = self._FileWriter(test_dir) sw.add_session_log(event_pb2.SessionLog(status=SessionLog.START), 1) sw.add_summary( summary_pb2.Summary(value=[ summary_pb2.Summary.Value(tag="mee", simple_value=10.0) ]), 10) sw.add_summary( summary_pb2.Summary(value=[ summary_pb2.Summary.Value(tag="boo", simple_value=20.0) ]), 20) with ops.Graph().as_default() as g: constant_op.constant([0], name="zero") sw.add_graph(g, global_step=30) run_metadata = config_pb2.RunMetadata() device_stats = run_metadata.step_stats.dev_stats.add() device_stats.device = "test" sw.add_run_metadata(run_metadata, "test run", global_step=40) sw.close() rr = self._EventsReader(test_dir) # The first event should list the file_version. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals("brain.Event:2", ev.file_version) # The next event should be the START message. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(1, ev.step) self.assertEquals(SessionLog.START, ev.session_log.status) # The next event should have the value 'mee=10.0'. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(10, ev.step) self.assertProtoEquals( """ value { tag: 'mee' simple_value: 10.0 } """, ev.summary) # The next event should have the value 'boo=20.0'. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(20, ev.step) self.assertProtoEquals( """ value { tag: 'boo' simple_value: 20.0 } """, ev.summary) # The next event should have the graph_def. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(30, ev.step) ev_graph = graph_pb2.GraphDef() ev_graph.ParseFromString(ev.graph_def) self.assertProtoEquals(g.as_graph_def(add_shapes=True), ev_graph) # The next event should have metadata for the run. ev = next(rr) self._assertRecent(ev.wall_time) self.assertEquals(40, ev.step) self.assertEquals("test run", ev.tagged_run_metadata.tag) parsed_run_metadata = config_pb2.RunMetadata() parsed_run_metadata.ParseFromString( ev.tagged_run_metadata.run_metadata) self.assertProtoEquals(run_metadata, parsed_run_metadata) # We should be done. self.assertRaises(StopIteration, lambda: next(rr))