forked from pytorch/translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpoint.py
408 lines (360 loc) · 16.1 KB
/
checkpoint.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env python3
import os
from collections import OrderedDict, deque
from typing import Any, Deque, Dict, List, Optional, Tuple
import torch
from fairseq import checkpoint_utils
from pytorch_translate import constants
def save_checkpoint_atomic(trainer, final_filename, extra_state):
"""Wrapper around trainer.save_checkpoint to make save atomic."""
path, filename = os.path.split(final_filename)
temp_filename = os.path.join(path, "." + filename + ".tmp")
trainer.save_checkpoint(temp_filename, extra_state)
os.rename(temp_filename, final_filename)
def load_existing_checkpoint(
checkpoint_path, trainer, restore_state=True
) -> Tuple[bool, Optional[Dict]]:
loaded = False
extra_state = None
if not os.path.isfile(checkpoint_path):
print(
f"| No existing checkpoint at {checkpoint_path}. "
f"Starting training from scratch."
)
return loaded, extra_state
if restore_state:
extra_state = trainer.load_checkpoint(checkpoint_path)
if extra_state is None:
loaded = False
print(f"| Failed to load checkpoint and state from {checkpoint_path}.")
else:
loaded = True
print(
f"| Loaded checkpoint {checkpoint_path} "
f"(epoch {extra_state['epoch']}) with restored extra state."
)
# batch_offset being None denotes this was a checkpoint saved at
# the end of an epoch (after the last batch).
if extra_state["batch_offset"] is None:
trainer.lr_step(extra_state["epoch"])
extra_state["epoch"] += 1
extra_state["batch_offset"] = 0
else:
dummy_state = trainer.load_checkpoint(checkpoint_path, reset_optimizer=True)
if dummy_state is None:
loaded = False
print(f"| Failed to load checkpoint weights from {checkpoint_path}.")
else:
loaded = True
print(f"| Loaded checkpoint weights from {checkpoint_path}.")
return loaded, extra_state
def load_to_cpu(path: str) -> Dict[str, Any]:
"""
This is just fairseq's utils.load_checkpoint_to_cpu(), except we don't try
to upgrade the state dict for backward compatibility - to make cases
where we only care about loading the model params easier to unit test.
"""
state = torch.load(
path,
map_location=(
lambda s, _: torch.serialization.default_restore_location(s, "cpu")
),
)
return state
def is_integer_tensor(tensor: torch.Tensor) -> bool:
return (
isinstance(tensor, torch.ByteTensor)
or isinstance(tensor, torch.CharTensor)
or isinstance(tensor, torch.ShortTensor)
or isinstance(tensor, torch.IntTensor)
or isinstance(tensor, torch.LongTensor)
)
def sanity_check_tensor(
tensor_name: str, old_tensor: torch.Tensor, new_tensor: torch.Tensor
) -> None:
if old_tensor.type() != new_tensor.type():
raise ValueError(
f"Type mismatch for tensor {tensor_name}. Old tensor had type of "
f"{old_tensor.type()} while new tensor has type of {new_tensor.type()}."
)
if old_tensor.size() != new_tensor.size():
raise ValueError(
f"Size mismatch for tensor {tensor_name} of type {old_tensor.type()}. "
f"Old tensor had size of {old_tensor.size()} while new tensor "
f"has size of {new_tensor.size()}."
)
if is_integer_tensor(old_tensor):
# The following sanity check is only relevant for integer tensors - which
# we expect to be index-like, and therefore should remain constant and not
# be averaged over.
if not torch.all(old_tensor == new_tensor):
raise ValueError(
f"Integer tensor {tensor_name} of type {old_tensor.type()} "
f"and size {old_tensor.size()} had "
f"{torch.sum(old_tensor != new_tensor).item()} mismatched elements."
)
def convert_tensor(tensor: torch.Tensor, clone: bool) -> torch.Tensor:
tensor = tensor.detach().cpu()
if isinstance(tensor, torch.HalfTensor):
# We convert any fp16 params to fp32 to make sure operations like
# division by a scalar value are supported.
tensor = tensor.float()
elif clone:
# tensor.float() would have effectively cloned the fp16 tensor already,
# so we don't need to do it again even if clone=True.
tensor = tensor.clone()
return tensor
class CheckpointManager:
"""Class to help manage, save, clean up, and average checkpoints.
"""
def __init__(
self,
num_avg_checkpoints: int,
auto_clear_checkpoints: bool,
log_verbose: bool,
checkpoint_files: List[str],
):
"""
Args:
num_avg_checkpoints: Number of checkpoints to average over.
auto_clear_checkpoints: If True, we automatically delete
checkpoints older than args.num_avg_checkpoints.
log_verbose:
"""
assert num_avg_checkpoints > 0, "Must average over at least one checkpoint."
self._num_avg_checkpoints: int = num_avg_checkpoints
self._auto_clear_checkpoints: bool = auto_clear_checkpoints
self._log_verbose: bool = log_verbose
self._averaged_params: OrderedDict = OrderedDict()
self._checkpoint_files: Deque[str] = deque(maxlen=self._num_avg_checkpoints)
# Updates the checkpoint files deque and discards any checkpoint files
# older than the limit.
for file in checkpoint_files:
self._checkpoint_files.append(file)
# Defers actually reading the checkpoint files until later due to
# T39501955.
self._initialized = False
def __repr__(self):
return (
f"CheckpointManager(num_avg_checkpoints={self._num_avg_checkpoints}, "
f"auto_clear_checkpoints={self._auto_clear_checkpoints}, "
f"log_verbose={self._log_verbose}, "
f"checkpoint_files={self._checkpoint_files})"
)
def _initialize(self):
# Loads and intializes the previous checkpoint params average.
for f in self._checkpoint_files:
# Loads everything to CPU memory to save space on GPU memory.
state: Dict[str, Any] = load_to_cpu(f)
model_params: OrderedDict = state["model"]
for k, v in model_params.items():
v = convert_tensor(v, clone=False)
if k not in self._averaged_params:
self._averaged_params[k] = (
v if is_integer_tensor(v) else v / len(self._checkpoint_files)
)
else:
sanity_check_tensor(
tensor_name=k, old_tensor=self._averaged_params[k], new_tensor=v
)
if not is_integer_tensor(v):
self._averaged_params[k].add_(v / len(self._checkpoint_files))
self._initialized = True
def log_if_verbose(self, msg: str):
if self._log_verbose:
print(msg, flush=True)
def get_averaged_params(self, new_params: OrderedDict) -> OrderedDict:
if not self._initialized:
self._initialize()
self.log_if_verbose(f"| Preparing to average {len(new_params)} params.")
# Special case for the first time or when we're not doing checkpoint
# averaging.
if len(self._averaged_params) == 0 or self._checkpoint_files.maxlen == 1:
copied_params: OrderedDict = OrderedDict()
for k, v in new_params.items():
copied_params[k] = convert_tensor(v, clone=True)
self.log_if_verbose(
f"| Finished copying {len(new_params)} params to CPU "
f"(no averaging needed)."
)
return copied_params
new_average: OrderedDict = OrderedDict()
new_denom: int = min( # noqa
self._checkpoint_files.maxlen, len(self._checkpoint_files) + 1
)
if len(self._checkpoint_files) == self._checkpoint_files.maxlen:
# We've reached the maximum number of checkpoints to average over,
# so the denominator won't change even when we add a new param - we
# just kick out the values from the oldest checkpoint.
self.log_if_verbose(
f"| Preparing to load old checkpoint "
f"{self._checkpoint_files[0]} to calculate average."
)
state: Dict[str, Any] = load_to_cpu(self._checkpoint_files[0])
self.log_if_verbose(
f"| Finished loading old checkpoint "
f"{self._checkpoint_files[0]} to calculate average."
)
old_params: OrderedDict = state["model"]
for k, v in old_params.items():
v = convert_tensor(v, clone=False)
sanity_check_tensor(
tensor_name=k, old_tensor=self._averaged_params[k], new_tensor=v
)
if is_integer_tensor(v):
new_average[k] = v
else:
new_average[k] = self._averaged_params[k] - (
v / len(self._checkpoint_files)
)
else:
# We haven't reached the maximum number of checkpoints to average
# over, so we simply adjust the denominator and the existing average
# to account for the larger number of checkpoints we're now
# averaging over.
for k, v in self._averaged_params.items():
if is_integer_tensor(v):
new_average[k] = v.clone()
else:
new_average[k] = v * (len(self._checkpoint_files) / new_denom)
# Actually add the new params after the existing average has been
# adjusted accordingly.
for k, v in new_params.items():
v = convert_tensor(v, clone=False)
sanity_check_tensor(tensor_name=k, old_tensor=new_average[k], new_tensor=v)
if not is_integer_tensor(v):
new_average[k].add_(v / new_denom)
self.log_if_verbose(f"| Finished averaging {len(new_params)} params.")
return new_average
def _update_state(
self, new_params_filename: str, new_averaged_params: OrderedDict
) -> Optional[str]:
# Consider making a copy of each tensor here if we run into issues in
# the future with callers later modifying the params passed in.
self._averaged_params = new_averaged_params
checkpoint_to_remove = None
if (
self._auto_clear_checkpoints
and len(self._checkpoint_files) == self._checkpoint_files.maxlen
):
# We delay actually removing this checkpoint until after the newest
# checkpoint has been successfully written.
checkpoint_to_remove = self._checkpoint_files.popleft()
# Make sure to include the checkpoint itself in its list of checkpoint
# files - this is to ensure we can still restore everything correctly
# even if the file gets copied to another name (ex: checkpoint_last.py).
self._checkpoint_files.append(new_params_filename)
return checkpoint_to_remove
def _remove_checkpoint(self, checkpoint_to_remove: Optional[str]):
if checkpoint_to_remove:
self.log_if_verbose(
f"| Preparing to remove old checkpoint {checkpoint_to_remove}."
)
try:
os.remove(checkpoint_to_remove)
except FileNotFoundError:
pass
self.log_if_verbose(
f"| Finished removing old checkpoint {checkpoint_to_remove}."
)
def remove_all_checkpoints(self):
"""
Removes all checkpoints besides
average_checkpoint.pt and last_checkpoint.pt
"""
for checkpoint_to_remove in self._checkpoint_files:
self._remove_checkpoint(checkpoint_to_remove)
def save(
self,
args,
trainer,
extra_state: Dict[str, Any],
new_averaged_params: OrderedDict,
) -> Dict[str, Any]:
"""Saves the model params contained in trainer.
Takes ownership of new_averaged_params, so the caller should not modify
them afterwards.
Args:
trainer: Trainer containing the model to be saved.
extra_state: Dictionary containing any extra information about the
model beyond the param weights.
new_averaged_params: If specified, takes ownership of the params and
sets them as current set of averaged params. If not specified,
we will recalculate the averaged params using the model params
in trainer.
Returns:
Updated extra_state dictionary.
"""
epoch = extra_state["epoch"]
batch_offset = extra_state["batch_offset"]
# batch_offset being None means that we're at the end of an epoch.
if batch_offset is None:
filename = os.path.join(args.save_dir, f"checkpoint{epoch}_end.pt")
# Otherwise, we're in the middle of an epoch.
else:
filename = os.path.join(
args.save_dir, f"checkpoint{epoch}_{batch_offset}.pt"
)
checkpoint_to_remove = self._update_state(
new_params_filename=filename, new_averaged_params=new_averaged_params
)
extra_state["checkpoint_files"] = list(self._checkpoint_files)
self.log_if_verbose(
f"| Preparing to save checkpoints for epoch {epoch}, "
f"offset {batch_offset}."
)
# Saves two copies of the checkpoint - one under a specific name
# corresponding to its epoch/offset, and another under the generic
# "checkpoint_last.py" that we restore from in case training is
# interrupted.
save_checkpoint_atomic(
trainer=trainer, final_filename=filename, extra_state=extra_state
)
# We update checkpoint_last.pt only after the new averaged checkpoint
# and epoch/offset-named copy have been written - so that in case either
# write fails, we'd still be able to resume from the previous
# checkpoint_last.pt
save_checkpoint_atomic(
trainer=trainer,
final_filename=os.path.join(
args.save_dir, constants.LAST_CHECKPOINT_FILENAME
),
extra_state=extra_state,
)
self.log_if_verbose(
f"| Finished saving checkpoints for epoch {epoch}, "
f"offset {batch_offset}."
)
# Wait until after checkpoint_last.py has been written to remove the
# oldest checkpoint. This is so that in case we fail to write a new
# checkpoint_last.py, we'd still have access to all the files listed in
# the previous checkpoint_last.py
self._remove_checkpoint(checkpoint_to_remove)
return extra_state
def save_best_averaged_checkpoint(self, args, trainer, extra_state: Dict[str, Any]):
"""
save() should always be called before calling this function - to ensure
that extra_state and self._averaged_params have been updated correctly.
"""
best_averaged_checkpoint_filename = os.path.join(
args.save_dir, constants.AVERAGED_CHECKPOINT_BEST_FILENAME
)
self.log_if_verbose(
f"| Preparing to save new best averaged checkpoint to "
f"{best_averaged_checkpoint_filename}."
)
checkpoint_utils.save_state(
filename=best_averaged_checkpoint_filename,
args=args,
model_state_dict=self._averaged_params,
criterion=trainer.criterion,
optimizer=trainer.optimizer,
lr_scheduler=trainer.lr_scheduler,
num_updates=trainer._num_updates,
optim_history=trainer._optim_history,
extra_state=extra_state,
)
self.log_if_verbose(
f"| Finished saving new best averaged checkpoint to "
f"{best_averaged_checkpoint_filename}."
)