|
| 1 | +# Copyright The PyTorch Lightning team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Contains migration functions to upgrade legacy checkpoints to the format of the current Lightning version. |
| 15 | +
|
| 16 | +When Lightning loads a checkpoint, these migrations will be applied on the loaded checkpoint dictionary sequentially, |
| 17 | +see :func:`~pytorch_lightning.utilities.migration.utils.migrate_checkpoint`. |
| 18 | +
|
| 19 | +For the Lightning developer: How to add a new migration? |
| 20 | +
|
| 21 | +1. Create a new function with a descriptive name and docstring that explains the details of this migration. Include |
| 22 | + version information as well as the specific commit or PR where the breaking change happened. |
| 23 | +2. Add the function to the `_migration_index()` below. The key in the index is the version of Lightning in which the |
| 24 | + change happened. Any checkpoint with a version greater or equal to that version will apply the given function. |
| 25 | + Multiple migrations per version get executed in the provided list order. |
| 26 | +3. You can test the migration on a checkpoint (backup your files first) by running: |
| 27 | +
|
| 28 | + cp model.ckpt model.ckpt.backup |
| 29 | + python -m pytorch_lightning.utilities.upgrade_checkpoint --file model.ckpt |
| 30 | +""" |
| 31 | + |
| 32 | +from typing import Any, Callable, Dict, List |
| 33 | + |
| 34 | +from pytorch_lightning.callbacks.early_stopping import EarlyStopping |
| 35 | +from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint |
| 36 | + |
| 37 | +_CHECKPOINT = Dict[str, Any] |
| 38 | + |
| 39 | + |
| 40 | +def _migration_index() -> Dict[str, List[Callable[[_CHECKPOINT], _CHECKPOINT]]]: |
| 41 | + """Migration functions returned here will get executed in the order they are listed.""" |
| 42 | + return { |
| 43 | + "0.10.0": [_migrate_model_checkpoint_early_stopping], |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +def _migrate_model_checkpoint_early_stopping(checkpoint: _CHECKPOINT) -> _CHECKPOINT: |
| 48 | + """The checkpoint and early stopping keys were renamed. |
| 49 | +
|
| 50 | + Version: 0.10.0 |
| 51 | + Commit: a5d1176 |
| 52 | + """ |
| 53 | + keys_mapping = { |
| 54 | + "checkpoint_callback_best_model_score": (ModelCheckpoint, "best_model_score"), |
| 55 | + "checkpoint_callback_best_model_path": (ModelCheckpoint, "best_model_path"), |
| 56 | + "checkpoint_callback_best": (ModelCheckpoint, "best_model_score"), |
| 57 | + "early_stop_callback_wait": (EarlyStopping, "wait_count"), |
| 58 | + "early_stop_callback_patience": (EarlyStopping, "patience"), |
| 59 | + } |
| 60 | + checkpoint["callbacks"] = checkpoint.get("callbacks") or {} |
| 61 | + |
| 62 | + for key, new_path in keys_mapping.items(): |
| 63 | + if key in checkpoint: |
| 64 | + value = checkpoint[key] |
| 65 | + callback_type, callback_key = new_path |
| 66 | + checkpoint["callbacks"][callback_type] = checkpoint["callbacks"].get(callback_type) or {} |
| 67 | + checkpoint["callbacks"][callback_type][callback_key] = value |
| 68 | + del checkpoint[key] |
| 69 | + return checkpoint |
0 commit comments