|
| 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 | +import os |
| 15 | +from unittest.mock import Mock |
| 16 | + |
| 17 | +import torch |
| 18 | + |
| 19 | +from pytorch_lightning import Trainer |
| 20 | +from tests.helpers import BoringModel |
| 21 | + |
| 22 | + |
| 23 | +class HPCHookdedModel(BoringModel): |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + super().__init__() |
| 27 | + self.hpc_save_called = 0 |
| 28 | + self.hpc_load_called = 0 |
| 29 | + |
| 30 | + def on_hpc_save(self, checkpoint): |
| 31 | + assert "state_dict" in checkpoint |
| 32 | + self.hpc_save_called += 1 |
| 33 | + |
| 34 | + def on_hpc_load(self, checkpoint): |
| 35 | + assert "state_dict" in checkpoint |
| 36 | + self.hpc_load_called += 1 |
| 37 | + |
| 38 | + |
| 39 | +def test_hpc_hook_calls(tmpdir): |
| 40 | + model = HPCHookdedModel() |
| 41 | + trainer = Trainer( |
| 42 | + default_root_dir=tmpdir, |
| 43 | + max_steps=1, |
| 44 | + checkpoint_callback=False, |
| 45 | + logger=False, |
| 46 | + ) |
| 47 | + trainer.fit(model) |
| 48 | + connector = trainer.checkpoint_connector |
| 49 | + connector.hpc_save(tmpdir, logger=Mock()) |
| 50 | + assert model.hpc_save_called == 1 |
| 51 | + assert model.hpc_load_called == 0 |
| 52 | + |
| 53 | + # new training run, restore from hpc checkpoint file automatically |
| 54 | + assert set(os.listdir(tmpdir)) == {"hpc_ckpt_1.ckpt"} |
| 55 | + trainer = Trainer( |
| 56 | + default_root_dir=tmpdir, |
| 57 | + max_steps=1, |
| 58 | + checkpoint_callback=False, |
| 59 | + logger=False, |
| 60 | + ) |
| 61 | + trainer.fit(model) |
| 62 | + assert model.hpc_save_called == 1 |
| 63 | + assert model.hpc_load_called == 1 |
| 64 | + |
| 65 | + |
| 66 | +def test_preloaded_checkpoint_lifecycle(tmpdir): |
| 67 | + """ Tests that the preloaded checkpoint contents gets cleared from memory when it is not required anymore. """ |
| 68 | + model = BoringModel() |
| 69 | + trainer = Trainer( |
| 70 | + default_root_dir=tmpdir, |
| 71 | + max_steps=1, |
| 72 | + ) |
| 73 | + trainer.fit(model) |
| 74 | + |
| 75 | + connector = trainer.checkpoint_connector |
| 76 | + |
| 77 | + assert not trainer.resume_from_checkpoint |
| 78 | + assert not connector.resume_checkpoint_path |
| 79 | + assert not connector._loaded_checkpoint |
| 80 | + |
| 81 | + connector.resume_start() |
| 82 | + assert not connector.resume_checkpoint_path |
| 83 | + assert not connector._loaded_checkpoint |
| 84 | + connector.resume_end() |
| 85 | + assert not connector.resume_checkpoint_path |
| 86 | + assert not connector._loaded_checkpoint |
| 87 | + |
| 88 | + ckpt_path = trainer.checkpoint_callback.best_model_path |
| 89 | + trainer = Trainer(default_root_dir=tmpdir, max_steps=2, resume_from_checkpoint=ckpt_path) |
| 90 | + connector = trainer.checkpoint_connector |
| 91 | + connector.resume_start() |
| 92 | + assert connector.resume_checkpoint_path == ckpt_path |
| 93 | + assert connector._loaded_checkpoint |
| 94 | + assert isinstance(connector._loaded_checkpoint, dict) |
| 95 | + connector.resume_end() |
| 96 | + assert not connector.resume_checkpoint_path |
| 97 | + assert not connector._loaded_checkpoint |
| 98 | + |
| 99 | + |
| 100 | +def test_hpc_restore_attempt(tmpdir): |
| 101 | + """ Test that restore() attempts to restore the hpc_ckpt with highest priority. """ |
| 102 | + model = BoringModel() |
| 103 | + trainer = Trainer( |
| 104 | + default_root_dir=tmpdir, |
| 105 | + max_steps=1, |
| 106 | + checkpoint_callback=False, |
| 107 | + logger=False, |
| 108 | + ) |
| 109 | + trainer.fit(model) |
| 110 | + |
| 111 | + hpc_ckpt_path = tmpdir / "hpc_ckpt_3.ckpt" |
| 112 | + trainer.save_checkpoint(hpc_ckpt_path) |
| 113 | + assert os.listdir(tmpdir) == ["hpc_ckpt_3.ckpt"] |
| 114 | + |
| 115 | + # set weights to zero |
| 116 | + for param in model.parameters(): |
| 117 | + torch.nn.init.constant_(param, 0) |
| 118 | + |
| 119 | + # case 1: restore hpc first, no explicit resume path provided |
| 120 | + trainer = Trainer( |
| 121 | + default_root_dir=tmpdir, |
| 122 | + max_steps=2, |
| 123 | + checkpoint_callback=False, |
| 124 | + logger=False, |
| 125 | + ) |
| 126 | + trainer.fit(model) |
| 127 | + |
| 128 | + for param in model.parameters(): |
| 129 | + assert param.abs().sum() > 0 |
| 130 | + torch.nn.init.constant_(param, 0) |
| 131 | + |
| 132 | + # case 2: explicit resume path provided, restore hpc anyway |
| 133 | + trainer = Trainer(default_root_dir=tmpdir, max_steps=3, resume_from_checkpoint="not existing") |
| 134 | + trainer.fit(model) |
| 135 | + |
| 136 | + for param in model.parameters(): |
| 137 | + assert param.abs().sum() > 0 |
| 138 | + |
| 139 | + |
| 140 | +def test_hpc_max_ckpt_version(tmpdir): |
| 141 | + """ Test that the CheckpointConnector is able to find the hpc checkpoint file with the highest version. """ |
| 142 | + model = BoringModel() |
| 143 | + trainer = Trainer( |
| 144 | + default_root_dir=tmpdir, |
| 145 | + max_steps=1, |
| 146 | + ) |
| 147 | + trainer.fit(model) |
| 148 | + trainer.save_checkpoint(tmpdir / "hpc_ckpt.ckpt") |
| 149 | + trainer.save_checkpoint(tmpdir / "hpc_ckpt_0.ckpt") |
| 150 | + trainer.save_checkpoint(tmpdir / "hpc_ckpt_3.ckpt") |
| 151 | + trainer.save_checkpoint(tmpdir / "hpc_ckpt_33.ckpt") |
| 152 | + |
| 153 | + assert trainer.checkpoint_connector.hpc_resume_path == str(tmpdir / "hpc_ckpt_33.ckpt") |
| 154 | + assert trainer.checkpoint_connector.max_ckpt_version_in_folder(tmpdir) == 33 |
| 155 | + assert trainer.checkpoint_connector.max_ckpt_version_in_folder(tmpdir / "not" / "existing") is None |
0 commit comments