File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed
tests/framework/callbacks
torchtnt/framework/callbacks Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -272,6 +272,10 @@ def test_log_for_epoch(self) -> None:
272
272
throughput_logger .on_train_step_end (state , unit )
273
273
self .assertEqual (throughput_logger ._steps_in_epoch [ActivePhase .TRAIN ], 1 )
274
274
275
+ # Make sure we don't log or fail if the _epoch_start_times dict is not initialized
276
+ throughput_logger ._log_for_epoch (state , epoch_logging_for = 15 )
277
+ logger .log .assert_not_called ()
278
+
275
279
with patch ("time.perf_counter" , return_value = 0.5 ):
276
280
throughput_logger .on_train_epoch_start (state , MagicMock (spec = TrainUnit ))
277
281
self .assertEqual (throughput_logger ._epoch_start_times [ActivePhase .TRAIN ], 0.5 )
Original file line number Diff line number Diff line change 7
7
# pyre-strict
8
8
9
9
10
+ import logging
10
11
import time
11
12
from collections import defaultdict
12
13
from typing import Dict , Mapping
30
31
ActivePhase .PREDICT : "Predict" ,
31
32
}
32
33
34
+ logger : logging .Logger = logging .getLogger (__name__ )
35
+
33
36
34
37
class ThroughputLogger (Callback ):
35
38
"""
@@ -192,6 +195,18 @@ def _log_for_epoch(
192
195
* ,
193
196
epoch_logging_for : int ,
194
197
) -> None :
198
+
199
+ # Avoid key errors if active phase is not set. This may happen if we restore
200
+ # from an intra-epoch checkpoint.
201
+ if (
202
+ state .active_phase not in self ._epoch_start_times
203
+ or state .active_phase not in self ._steps_in_epoch
204
+ ):
205
+ logger .warning (
206
+ f"Missing troughput data for epoch { epoch_logging_for } , phase { state .active_phase } . Ommiting troughput logging."
207
+ )
208
+ return
209
+
195
210
time_since_epoch_start = (
196
211
time .perf_counter () - self ._epoch_start_times [state .active_phase ]
197
212
)
You can’t perform that action at this time.
0 commit comments