Skip to content

Commit cab6afc

Browse files
galrotemfacebook-github-bot
authored andcommitted
add number of steps in prev epoch to progress (#873)
Summary: Pull Request resolved: #873 Expose number of steps in prev completed epoch Reviewed By: diego-urgell Differential Revision: D60309756 fbshipit-source-id: 8d356f2b4cba4e5e74ad16fa24935864b0f30ea6
1 parent e4e7a9d commit cab6afc

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tests/utils/test_progress.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,17 @@ def __iter__(self) -> Iterator[int]:
286286
),
287287
float("inf"),
288288
)
289+
290+
def test_num_steps_completed_in_prev_epoch(self) -> None:
291+
progress = Progress(
292+
num_epochs_completed=2,
293+
num_steps_completed=8,
294+
num_steps_completed_in_epoch=4,
295+
)
296+
self.assertEqual(progress.num_steps_completed_in_epoch, 4)
297+
self.assertEqual(progress.num_steps_completed_in_prev_epoch, 0)
298+
299+
progress.increment_epoch()
300+
301+
self.assertEqual(progress.num_steps_completed_in_epoch, 0)
302+
self.assertEqual(progress.num_steps_completed_in_prev_epoch, 4)

torchtnt/utils/progress.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
self._num_epochs_completed: int = num_epochs_completed
2323
self._num_steps_completed: int = num_steps_completed
2424
self._num_steps_completed_in_epoch: int = num_steps_completed_in_epoch
25+
self._num_steps_completed_in_prev_epoch: int = 0
2526

2627
@property
2728
def num_epochs_completed(self) -> int:
@@ -38,6 +39,11 @@ def num_steps_completed_in_epoch(self) -> int:
3839
"""Number of steps completed thus far in epoch."""
3940
return self._num_steps_completed_in_epoch
4041

42+
@property
43+
def num_steps_completed_in_prev_epoch(self) -> int:
44+
"""Number of steps completed in the previous completed epoch."""
45+
return self._num_steps_completed_in_prev_epoch
46+
4147
def increment_step(self) -> None:
4248
"""Increment the step counts completed and completed within the epoch."""
4349
self._num_steps_completed += 1
@@ -46,6 +52,7 @@ def increment_step(self) -> None:
4652
def increment_epoch(self) -> None:
4753
"""Increment the epochs completed and resets the steps completed within the epoch."""
4854
self._num_epochs_completed += 1
55+
self._num_steps_completed_in_prev_epoch = self._num_steps_completed_in_epoch
4956
self._num_steps_completed_in_epoch = 0
5057

5158
def state_dict(self) -> Dict[str, Any]:

0 commit comments

Comments
 (0)