Load training checkpoint Arguments: load_dir: Required. Directory to load the checkpoint from tag: Checkpoint tag used as a unique identifier for checkpoint, if not provided will attempt to load tag in 'latest' file load_module_strict: Optional.
(self,
load_dir,
tag=None,
load_module_strict=True,
load_optimizer_states=True,
load_lr_scheduler_states=True,
load_module_only=False,
custom_load_fn=None)
| 2557 | return ckpt_files |
| 2558 | |
| 2559 | def load_checkpoint(self, |
| 2560 | load_dir, |
| 2561 | tag=None, |
| 2562 | load_module_strict=True, |
| 2563 | load_optimizer_states=True, |
| 2564 | load_lr_scheduler_states=True, |
| 2565 | load_module_only=False, |
| 2566 | custom_load_fn=None): |
| 2567 | """ |
| 2568 | Load training checkpoint |
| 2569 | |
| 2570 | Arguments: |
| 2571 | load_dir: Required. Directory to load the checkpoint from |
| 2572 | tag: Checkpoint tag used as a unique identifier for checkpoint, if not provided will attempt to load tag in 'latest' file |
| 2573 | load_module_strict: Optional. Boolean to strictly enforce that the keys in state_dict of module and checkpoint match. |
| 2574 | load_optimizer_states: Optional. Boolean to load the training optimizer states from Checkpoint. Ex. ADAM's momentum and variance |
| 2575 | load_lr_scheduler_states: Optional. Boolean to add the learning rate scheduler states from Checkpoint. |
| 2576 | load_module_only: Optional. Boolean to load only the model weights from the checkpoint. Ex. warmstarting. |
| 2577 | custom_load_fn: Optional. Custom model load function. |
| 2578 | |
| 2579 | Returns: |
| 2580 | A tuple of ``load_path`` and ``client_state``. |
| 2581 | *``load_path``: Path of the loaded checkpoint. ``None`` if loading the checkpoint failed. |
| 2582 | *``client_state``: State dictionary used for loading required training states in the client code. |
| 2583 | |
| 2584 | Important: under ZeRO3, one cannot load checkpoint with ``engine.load_checkpoint()`` right |
| 2585 | after ``engine.save_checkpoint()``. It is because ``engine.module`` is partitioned, and |
| 2586 | ``load_checkpoint()`` wants a pristine model. If insisting to do so, please reinitialize engine |
| 2587 | before ``load_checkpoint()``. |
| 2588 | |
| 2589 | """ |
| 2590 | |
| 2591 | if tag is None: |
| 2592 | latest_tag = "latest_universal" if self.load_universal_checkpoint() else "latest" |
| 2593 | latest_path = os.path.join(load_dir, latest_tag) |
| 2594 | if os.path.isfile(latest_path): |
| 2595 | with open(latest_path, "r") as fd: |
| 2596 | tag = fd.read().strip() |
| 2597 | else: |
| 2598 | if self.load_universal_checkpoint(): |
| 2599 | raise ValueError(f'Invalid for universal checkpoint: {latest_path} does not exist') |
| 2600 | else: |
| 2601 | logger.warning( |
| 2602 | f"Unable to find latest file at {latest_path}, if trying to load latest " |
| 2603 | "checkpoint please ensure this file exists or pass an explicit checkpoint tag when loading a checkpoint." |
| 2604 | ) |
| 2605 | return None, None |
| 2606 | |
| 2607 | if self.zero_optimization_partition_weights(): |
| 2608 | # Prepare for checkpoint load by ensuring all parameters are partitioned |
| 2609 | self.optimizer.checkpoint_event_prologue() |
| 2610 | |
| 2611 | load_path, client_states = self._load_checkpoint(load_dir, |
| 2612 | tag, |
| 2613 | load_module_strict=load_module_strict, |
| 2614 | load_optimizer_states=load_optimizer_states, |
| 2615 | load_lr_scheduler_states=load_lr_scheduler_states, |
| 2616 | load_module_only=load_module_only, |
no test coverage detected