2020-10-13 11:18:07 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-09-21 02:58:43 +00:00
|
|
|
import os
|
2020-10-05 01:49:20 +00:00
|
|
|
from unittest import mock
|
2020-11-24 00:23:12 +00:00
|
|
|
|
2020-10-05 01:49:20 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
2020-09-21 02:58:43 +00:00
|
|
|
|
2021-09-08 00:42:31 +00:00
|
|
|
from pytorch_lightning import callbacks, Trainer
|
2021-02-09 10:10:52 +00:00
|
|
|
from tests.helpers import BoringModel
|
2021-03-14 17:14:27 +00:00
|
|
|
from tests.helpers.runif import RunIf
|
2020-11-24 00:23:12 +00:00
|
|
|
|
2020-09-21 02:58:43 +00:00
|
|
|
|
2021-10-12 07:55:07 +00:00
|
|
|
def test_disabled_checkpointing(tmpdir):
|
2020-09-21 02:58:43 +00:00
|
|
|
# no callback
|
2021-10-12 07:55:07 +00:00
|
|
|
trainer = Trainer(max_epochs=3, enable_checkpointing=False)
|
2021-09-08 00:42:31 +00:00
|
|
|
assert not trainer.checkpoint_callbacks
|
|
|
|
trainer.fit(BoringModel())
|
|
|
|
assert not trainer.checkpoint_callbacks
|
2020-10-05 01:49:20 +00:00
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
@mock.patch("torch.save")
|
2021-02-06 11:07:26 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-07-26 11:37:35 +00:00
|
|
|
["epochs", "val_check_interval", "expected"], [(1, 1.0, 1), (2, 1.0, 2), (1, 0.25, 4), (2, 0.3, 6)]
|
2021-02-06 11:07:26 +00:00
|
|
|
)
|
2021-03-09 11:27:15 +00:00
|
|
|
def test_default_checkpoint_freq(save_mock, tmpdir, epochs: int, val_check_interval: float, expected: int):
|
2020-10-05 01:49:20 +00:00
|
|
|
|
|
|
|
model = BoringModel()
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=epochs,
|
2021-10-13 11:50:54 +00:00
|
|
|
enable_model_summary=False,
|
2021-02-06 11:07:26 +00:00
|
|
|
val_check_interval=val_check_interval,
|
2021-07-12 23:20:20 +00:00
|
|
|
limit_val_batches=1,
|
2021-09-25 05:53:31 +00:00
|
|
|
enable_progress_bar=False,
|
2020-10-05 01:49:20 +00:00
|
|
|
)
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
# make sure types are correct
|
|
|
|
assert save_mock.call_count == expected
|
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
@mock.patch("torch.save")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["k", "epochs", "val_check_interval", "expected"], [(1, 1, 1.0, 1), (2, 2, 1.0, 2), (2, 1, 0.25, 4), (2, 2, 0.3, 6)]
|
|
|
|
)
|
2021-07-19 11:29:00 +00:00
|
|
|
@pytest.mark.parametrize("save_last", (False, True))
|
|
|
|
def test_top_k(save_mock, tmpdir, k: int, epochs: int, val_check_interval: float, expected: int, save_last: bool):
|
2020-10-05 01:49:20 +00:00
|
|
|
class TestModel(BoringModel):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.last_coeff = 10.0
|
|
|
|
|
|
|
|
def training_step(self, batch, batch_idx):
|
|
|
|
loss = self.step(torch.ones(32))
|
|
|
|
loss = loss / (loss + 0.0000001)
|
|
|
|
loss += self.last_coeff
|
2021-07-26 11:37:35 +00:00
|
|
|
self.log("my_loss", loss)
|
2020-10-05 01:49:20 +00:00
|
|
|
self.last_coeff *= 0.999
|
|
|
|
return loss
|
|
|
|
|
|
|
|
model = TestModel()
|
|
|
|
trainer = Trainer(
|
2021-07-26 11:37:35 +00:00
|
|
|
callbacks=[callbacks.ModelCheckpoint(dirpath=tmpdir, monitor="my_loss", save_top_k=k, save_last=save_last)],
|
2020-10-05 01:49:20 +00:00
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=epochs,
|
2021-10-13 11:50:54 +00:00
|
|
|
enable_model_summary=False,
|
2021-07-26 11:37:35 +00:00
|
|
|
val_check_interval=val_check_interval,
|
2020-10-05 01:49:20 +00:00
|
|
|
)
|
|
|
|
trainer.fit(model)
|
|
|
|
|
2021-07-19 11:29:00 +00:00
|
|
|
if save_last:
|
2022-03-07 19:21:37 +00:00
|
|
|
# last epochs are saved every step (so double the save calls)
|
|
|
|
expected = expected * 2
|
2020-10-05 01:49:20 +00:00
|
|
|
assert save_mock.call_count == expected
|
2021-03-14 17:14:27 +00:00
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
@mock.patch("torch.save")
|
2021-11-26 17:13:14 +00:00
|
|
|
@RunIf(standalone=True, min_gpus=2)
|
2021-11-17 15:46:14 +00:00
|
|
|
@pytest.mark.parametrize(["k", "epochs", "val_check_interval", "expected"], [(1, 1, 1.0, 1), (2, 2, 0.3, 4)])
|
|
|
|
def test_top_k_ddp(save_mock, tmpdir, k, epochs, val_check_interval, expected):
|
2021-03-14 17:14:27 +00:00
|
|
|
class TestModel(BoringModel):
|
|
|
|
def training_step(self, batch, batch_idx):
|
|
|
|
local_rank = int(os.getenv("LOCAL_RANK"))
|
2021-07-26 11:37:35 +00:00
|
|
|
self.log("my_loss", batch_idx * (1 + local_rank), on_epoch=True)
|
2021-03-14 17:14:27 +00:00
|
|
|
return super().training_step(batch, batch_idx)
|
|
|
|
|
|
|
|
def training_epoch_end(self, outputs) -> None:
|
2021-04-22 00:14:46 +00:00
|
|
|
local_rank = int(os.getenv("LOCAL_RANK"))
|
|
|
|
if self.trainer.is_global_zero:
|
2021-07-26 11:37:35 +00:00
|
|
|
self.log("my_loss_2", (1 + local_rank), on_epoch=True, rank_zero_only=True)
|
2021-03-14 17:14:27 +00:00
|
|
|
data = str(self.global_rank)
|
2021-07-26 11:37:35 +00:00
|
|
|
obj = [[data], (data,), set(data)]
|
2021-12-22 02:11:43 +00:00
|
|
|
out = self.trainer.strategy.broadcast(obj)
|
2021-07-26 11:37:35 +00:00
|
|
|
assert obj == [[str(self.global_rank)], (str(self.global_rank),), set(str(self.global_rank))]
|
|
|
|
assert out == [["0"], ("0",), set("0")]
|
2021-03-14 17:14:27 +00:00
|
|
|
|
|
|
|
model = TestModel()
|
|
|
|
trainer = Trainer(
|
2021-07-26 11:37:35 +00:00
|
|
|
callbacks=[callbacks.ModelCheckpoint(dirpath=tmpdir, monitor="my_loss_step", save_top_k=k, mode="max")],
|
2021-03-14 17:14:27 +00:00
|
|
|
default_root_dir=tmpdir,
|
2021-09-25 05:53:31 +00:00
|
|
|
enable_progress_bar=False,
|
2021-03-14 17:14:27 +00:00
|
|
|
max_epochs=epochs,
|
2021-10-13 11:50:54 +00:00
|
|
|
enable_model_summary=False,
|
2021-03-14 17:14:27 +00:00
|
|
|
val_check_interval=val_check_interval,
|
2021-10-16 15:10:25 +00:00
|
|
|
strategy="ddp",
|
2022-01-12 05:47:01 +00:00
|
|
|
accelerator="gpu",
|
|
|
|
devices=2,
|
2021-03-14 17:14:27 +00:00
|
|
|
limit_train_batches=64,
|
|
|
|
limit_val_batches=32,
|
|
|
|
)
|
2021-04-22 00:14:46 +00:00
|
|
|
trainer.fit(model)
|
2021-03-14 17:14:27 +00:00
|
|
|
if os.getenv("LOCAL_RANK") == "0":
|
|
|
|
assert save_mock.call_count == expected
|