|
| 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 | +import torch |
| 16 | +import pytest |
| 17 | +from tests.base.boring_model import BoringModel, RandomDataset |
| 18 | +from pytorch_lightning import Trainer |
| 19 | +from pytorch_lightning.utilities import APEX_AVAILABLE |
| 20 | +from pytorch_lightning.utilities.exceptions import MisconfigurationException |
| 21 | + |
| 22 | + |
| 23 | +def test_logdir(tmpdir): |
| 24 | + """ |
| 25 | + Tests that the path is correct when checkpoint and loggers are used |
| 26 | + """ |
| 27 | + class TestModel(BoringModel): |
| 28 | + def training_step(self, batch, batch_idx): |
| 29 | + output = self.layer(batch) |
| 30 | + loss = self.loss(batch, output) |
| 31 | + |
| 32 | + expected = os.path.join(self.trainer.default_root_dir, 'lightning_logs', 'version_0') |
| 33 | + assert self.trainer.log_dir == expected |
| 34 | + return {"loss": loss} |
| 35 | + |
| 36 | + model = TestModel() |
| 37 | + |
| 38 | + limit_train_batches = 2 |
| 39 | + trainer = Trainer( |
| 40 | + default_root_dir=tmpdir, |
| 41 | + limit_train_batches=limit_train_batches, |
| 42 | + limit_val_batches=2, |
| 43 | + max_epochs=1, |
| 44 | + ) |
| 45 | + |
| 46 | + trainer.fit(model) |
| 47 | + |
| 48 | + |
| 49 | +def test_logdir_no_checkpoint_cb(tmpdir): |
| 50 | + """ |
| 51 | + Tests that the path is correct with no checkpoint |
| 52 | + """ |
| 53 | + class TestModel(BoringModel): |
| 54 | + def training_step(self, batch, batch_idx): |
| 55 | + output = self.layer(batch) |
| 56 | + loss = self.loss(batch, output) |
| 57 | + expected = os.path.join(self.trainer.default_root_dir, 'lightning_logs', 'version_0') |
| 58 | + assert self.trainer.log_dir == expected |
| 59 | + return {"loss": loss} |
| 60 | + |
| 61 | + model = TestModel() |
| 62 | + |
| 63 | + limit_train_batches = 2 |
| 64 | + trainer = Trainer( |
| 65 | + default_root_dir=tmpdir, |
| 66 | + limit_train_batches=limit_train_batches, |
| 67 | + limit_val_batches=2, |
| 68 | + max_epochs=1, |
| 69 | + checkpoint_callback=False |
| 70 | + ) |
| 71 | + |
| 72 | + trainer.fit(model) |
| 73 | + |
| 74 | + |
| 75 | +def test_logdir_no_logger(tmpdir): |
| 76 | + """ |
| 77 | + Tests that the path is correct even when there is no logger |
| 78 | + """ |
| 79 | + class TestModel(BoringModel): |
| 80 | + def training_step(self, batch, batch_idx): |
| 81 | + output = self.layer(batch) |
| 82 | + loss = self.loss(batch, output) |
| 83 | + expected = os.path.join(self.trainer.default_root_dir) |
| 84 | + assert self.trainer.log_dir == expected |
| 85 | + return {"loss": loss} |
| 86 | + |
| 87 | + model = TestModel() |
| 88 | + |
| 89 | + limit_train_batches = 2 |
| 90 | + trainer = Trainer( |
| 91 | + default_root_dir=tmpdir, |
| 92 | + limit_train_batches=limit_train_batches, |
| 93 | + limit_val_batches=2, |
| 94 | + max_epochs=1, |
| 95 | + logger=False, |
| 96 | + ) |
| 97 | + |
| 98 | + trainer.fit(model) |
| 99 | + |
| 100 | + |
| 101 | +def test_logdir_no_logger_no_checkpoint(tmpdir): |
| 102 | + """ |
| 103 | + Tests that the path is correct even when there is no logger |
| 104 | + """ |
| 105 | + class TestModel(BoringModel): |
| 106 | + def training_step(self, batch, batch_idx): |
| 107 | + output = self.layer(batch) |
| 108 | + loss = self.loss(batch, output) |
| 109 | + expected = os.path.join(self.trainer.default_root_dir) |
| 110 | + assert self.trainer.log_dir == expected |
| 111 | + return {"loss": loss} |
| 112 | + |
| 113 | + model = TestModel() |
| 114 | + |
| 115 | + limit_train_batches = 2 |
| 116 | + trainer = Trainer( |
| 117 | + default_root_dir=tmpdir, |
| 118 | + limit_train_batches=limit_train_batches, |
| 119 | + limit_val_batches=2, |
| 120 | + max_epochs=1, |
| 121 | + logger=False, |
| 122 | + checkpoint_callback=False |
| 123 | + ) |
| 124 | + |
| 125 | + trainer.fit(model) |
0 commit comments