|
| 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 pytest |
| 15 | +import torch |
| 16 | + |
| 17 | +import pytorch_lightning as pl |
| 18 | +import tests.base.develop_utils as tutils |
| 19 | +from tests.base import EvalModelTemplate |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") |
| 23 | +def test_single_gpu_validate(tmpdir): |
| 24 | + tutils.set_random_master_port() |
| 25 | + |
| 26 | + model = EvalModelTemplate() |
| 27 | + trainer = pl.Trainer( |
| 28 | + default_root_dir=tmpdir, |
| 29 | + max_epochs=2, |
| 30 | + limit_train_batches=10, |
| 31 | + limit_val_batches=10, |
| 32 | + gpus=[0], |
| 33 | + ) |
| 34 | + trainer.fit(model) |
| 35 | + assert 'ckpt' in trainer.checkpoint_callback.best_model_path |
| 36 | + results = trainer.validate() |
| 37 | + assert 'val_acc' in results[0] |
| 38 | + |
| 39 | + old_weights = model.c_d1.weight.clone().detach().cpu() |
| 40 | + |
| 41 | + results = trainer.validate(model) |
| 42 | + assert 'val_acc' in results[0] |
| 43 | + |
| 44 | + # make sure weights didn't change |
| 45 | + new_weights = model.c_d1.weight.clone().detach().cpu() |
| 46 | + |
| 47 | + assert torch.all(torch.eq(old_weights, new_weights)) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine") |
| 51 | +def test_ddp_spawn_validate(tmpdir): |
| 52 | + tutils.set_random_master_port() |
| 53 | + |
| 54 | + model = EvalModelTemplate() |
| 55 | + trainer = pl.Trainer( |
| 56 | + default_root_dir=tmpdir, |
| 57 | + max_epochs=2, |
| 58 | + limit_train_batches=10, |
| 59 | + limit_val_batches=10, |
| 60 | + gpus=[0, 1], |
| 61 | + distributed_backend='ddp_spawn', |
| 62 | + ) |
| 63 | + trainer.fit(model) |
| 64 | + assert 'ckpt' in trainer.checkpoint_callback.best_model_path |
| 65 | + results = trainer.validate() |
| 66 | + assert 'val_acc' in results[0] |
| 67 | + |
| 68 | + old_weights = model.c_d1.weight.clone().detach().cpu() |
| 69 | + |
| 70 | + results = trainer.validate(model) |
| 71 | + assert 'val_acc' in results[0] |
| 72 | + |
| 73 | + # make sure weights didn't change |
| 74 | + new_weights = model.c_d1.weight.clone().detach().cpu() |
| 75 | + |
| 76 | + assert torch.all(torch.eq(old_weights, new_weights)) |
0 commit comments