Load a model checkpoint and return the iteration. strict (bool): whether to strictly enforce that the keys in :attr:`state_dict` of the checkpoint match the names of parameters and buffers in model.
(
model,
optimizer,
lr_scheduler,
load_arg="load",
strict=True,
)
| 299 | |
| 300 | |
| 301 | def load_checkpoint( |
| 302 | model, |
| 303 | optimizer, |
| 304 | lr_scheduler, |
| 305 | load_arg="load", |
| 306 | strict=True, |
| 307 | ): |
| 308 | """Load a model checkpoint and return the iteration. |
| 309 | strict (bool): whether to strictly enforce that the keys in |
| 310 | :attr:`state_dict` of the checkpoint match the names of |
| 311 | parameters and buffers in model. |
| 312 | """ |
| 313 | args = get_args() |
| 314 | load_dir = getattr(args, load_arg) |
| 315 | |
| 316 | if args.deepspeed: |
| 317 | loaded_dir, state_dict = model[0].load_checkpoint(load_dir) |
| 318 | if loaded_dir is None: |
| 319 | print_rank_0( |
| 320 | "WARNING: could not find the metadata file {} ".format(load_dir) |
| 321 | ) |
| 322 | print_rank_0( |
| 323 | " will not load any checkpoints and will start from " "random" |
| 324 | ) |
| 325 | return 0 |
| 326 | release = False |
| 327 | else: |
| 328 | model = utils.unwrap_model(model) |
| 329 | |
| 330 | if load_dir.endswith(".pt"): |
| 331 | checkpoint_name = load_dir |
| 332 | release = True |
| 333 | else: |
| 334 | # Read the tracker file and set the iteration. |
| 335 | tracker_filename = get_checkpoint_tracker_filename(load_dir) |
| 336 | |
| 337 | # If no tracker file, return iretation zero. |
| 338 | if not os.path.isfile(tracker_filename): |
| 339 | print_rank_0( |
| 340 | "WARNING: could not find the metadata file {} ".format(tracker_filename) |
| 341 | ) |
| 342 | iteration = 0 |
| 343 | release = True |
| 344 | checkpoint_name = get_checkpoint_name(load_dir, iteration, release) |
| 345 | if not os.path.isfile(checkpoint_name): |
| 346 | print_rank_0( |
| 347 | " will not load any checkpoints and will start from random" |
| 348 | ) |
| 349 | return 0 |
| 350 | else: |
| 351 | # Otherwise, read the tracker file and either set the iteration or |
| 352 | # mark it as a release checkpoint. |
| 353 | iteration = 0 |
| 354 | release = False |
| 355 | with open(tracker_filename, "r") as f: |
| 356 | metastring = f.read().strip() |
| 357 | try: |
| 358 | iteration = int(metastring) |