-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustom_progress.py
300 lines (218 loc) · 9.01 KB
/
custom_progress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright The Lightning AI team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import asdict, dataclass, field
from typing import Type
from typing_extensions import override
@dataclass
class _BaseProgress:
"""Mixin that implements state-loading utilities for dataclasses."""
def state_dict(self) -> dict:
return asdict(self)
def load_state_dict(self, state_dict: dict) -> None:
if state_dict["completed"] == None:
state_dict["completed"] = 0
self.__dict__.update(state_dict)
@classmethod
def from_state_dict(cls, state_dict: dict) -> "_BaseProgress":
obj = cls()
obj.load_state_dict(state_dict)
return obj
def reset(self) -> None:
"""Reset the object's state."""
raise NotImplementedError
@dataclass
class _ReadyCompletedTracker(_BaseProgress):
"""Track an event's progress.
Args:
ready: Intended to track the number of events ready to start.
completed: Intended to be incremented after the event completes (e.g. after ``on_*_end`` runs).
These attributes should be increased in order, that is, :attr:`ready` first and :attr:`completed` last.
"""
ready: int = 0
completed: int = 0
@override
def reset(self) -> None:
"""Reset the state."""
self.ready = 0
self.completed = 0
def reset_on_restart(self) -> None:
"""Reset the progress on restart.
If there is a failure before all attributes are increased, restore the attributes to the last fully completed
value.
"""
self.ready = self.completed
@dataclass
class _StartedTracker(_ReadyCompletedTracker):
"""Track an event's progress.
Args:
ready: Intended to track the number of events ready to start.
started: Intended to be incremented after the event is started (e.g. after ``on_*_start`` runs).
completed: Intended to be incremented after the event completes (e.g. after ``on_*_end`` runs).
These attributes should be increased in order, that is, :attr:`ready` first and :attr:`completed` last.
"""
started: int = 0
@override
def reset(self) -> None:
super().reset()
self.started = 0
@override
def reset_on_restart(self) -> None:
super().reset_on_restart()
self.started = self.completed
@dataclass
class _ProcessedTracker(_StartedTracker):
"""Track an event's progress.
Args:
ready: Intended to track the number of events ready to start.
started: Intended to be incremented after the event is started (e.g. after ``on_*_start`` runs).
processed: Intended to be incremented after the event is processed.
completed: Intended to be incremented after the event completes (e.g. after ``on_*_end`` runs).
These attributes should be increased in order, that is, :attr:`ready` first and :attr:`completed` last.
"""
processed: int = 0
@override
def reset(self) -> None:
super().reset()
self.processed = 0
@override
def reset_on_restart(self) -> None:
super().reset_on_restart()
self.processed = self.completed
@dataclass
class _Progress(_BaseProgress):
"""Track aggregated and current progress.
Args:
total: Intended to track the total progress of an event.
current: Intended to track the current progress of an event.
"""
total: _ReadyCompletedTracker = field(default_factory=_ProcessedTracker)
current: _ReadyCompletedTracker = field(default_factory=_ProcessedTracker)
def __post_init__(self) -> None:
if self.total.__class__ is not self.current.__class__:
raise ValueError("The `total` and `current` instances should be of the same class")
def increment_ready(self) -> None:
self.total.ready += 1
self.current.ready += 1
def increment_started(self) -> None:
if not isinstance(self.total, _StartedTracker):
raise TypeError(f"`{self.total.__class__.__name__}` doesn't have a `started` attribute")
self.total.started += 1
self.current.started += 1
def increment_processed(self) -> None:
if not isinstance(self.total, _ProcessedTracker):
raise TypeError(f"`{self.total.__class__.__name__}` doesn't have a `processed` attribute")
self.total.processed += 1
self.current.processed += 1
def increment_completed(self) -> None:
self.total.completed += 1
self.current.completed += 1
@classmethod
def from_defaults(cls, tracker_cls: Type[_ReadyCompletedTracker], **kwargs: int) -> "_Progress":
"""Utility function to easily create an instance from keyword arguments to both ``Tracker``s."""
return cls(total=tracker_cls(**kwargs), current=tracker_cls(**kwargs))
@override
def reset(self) -> None:
self.total.reset()
self.current.reset()
def reset_on_run(self) -> None:
self.current.reset()
def reset_on_restart(self) -> None:
self.current.reset_on_restart()
@override
def load_state_dict(self, state_dict: dict) -> None:
if state_dict["total"]["completed"] == None:
state_dict["total"]["completed"] = 0
self.total.load_state_dict(state_dict["total"])
self.current.load_state_dict(state_dict["current"])
@dataclass
class _BatchProgress(_Progress):
"""Tracks batch progress.
These counters are local to a trainer rank. By default, they are not globally synced across all ranks.
Args:
total: Tracks the total batch progress.
current: Tracks the current batch progress.
is_last_batch: Whether the batch is the last one. This is useful for iterable datasets.
"""
is_last_batch: bool = False
@override
def reset(self) -> None:
super().reset()
self.is_last_batch = False
@override
def reset_on_run(self) -> None:
super().reset_on_run()
self.is_last_batch = False
@override
def load_state_dict(self, state_dict: dict) -> None:
if state_dict["total"]["completed"] == None:
state_dict["total"]["completed"] = 0
super().load_state_dict(state_dict)
self.is_last_batch = state_dict["is_last_batch"]
@dataclass
class _SchedulerProgress(_Progress):
"""Tracks scheduler progress.
These counters are local to a trainer rank. By default, they are not globally synced across all ranks.
Args:
total: Tracks the total scheduler progress.
current: Tracks the current scheduler progress.
"""
total: _ReadyCompletedTracker = field(default_factory=_ReadyCompletedTracker)
current: _ReadyCompletedTracker = field(default_factory=_ReadyCompletedTracker)
@dataclass
class _OptimizerProgress(_BaseProgress):
"""Track optimizer progress.
Args:
step: Tracks ``optimizer.step`` calls.
zero_grad: Tracks ``optimizer.zero_grad`` calls.
"""
step: _Progress = field(default_factory=lambda: _Progress.from_defaults(_ReadyCompletedTracker))
zero_grad: _Progress = field(default_factory=lambda: _Progress.from_defaults(_StartedTracker))
@override
def reset(self) -> None:
self.step.reset()
self.zero_grad.reset()
def reset_on_run(self) -> None:
self.step.reset_on_run()
self.zero_grad.reset_on_run()
def reset_on_restart(self) -> None:
self.step.reset_on_restart()
self.zero_grad.reset_on_restart()
@override
def load_state_dict(self, state_dict: dict) -> None:
if state_dict["step"]["total"]["completed"] == None:
state_dict["step"]["total"]["completed"] = 0
self.step.load_state_dict(state_dict["step"])
self.zero_grad.load_state_dict(state_dict["zero_grad"])
@dataclass
class _OptimizationProgress(_BaseProgress):
"""Track optimization progress.
Args:
optimizer: Tracks optimizer progress.
"""
optimizer: _OptimizerProgress = field(default_factory=_OptimizerProgress)
@property
def optimizer_steps(self) -> int:
return self.optimizer.step.total.completed
@override
def reset(self) -> None:
self.optimizer.reset()
def reset_on_run(self) -> None:
self.optimizer.reset_on_run()
def reset_on_restart(self) -> None:
self.optimizer.reset_on_restart()
@override
def load_state_dict(self, state_dict: dict) -> None:
if state_dict["optimizer"]["step"]["total"]["completed"] == None:
state_dict["optimizer"]["step"]["total"]["completed"] = 0
self.optimizer.load_state_dict(state_dict["optimizer"])