2021-03-29 20:50:51 +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.
|
2021-03-11 02:46:37 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
|
|
|
import tests.helpers.utils as tutils
|
|
|
|
from pytorch_lightning import Trainer
|
2021-09-30 04:40:09 +00:00
|
|
|
from pytorch_lightning.utilities.seed import seed_everything
|
2021-03-11 02:46:37 +00:00
|
|
|
from tests.accelerators.test_dp import CustomClassificationModelDP
|
2021-03-29 20:50:51 +00:00
|
|
|
from tests.helpers.boring_model import BoringModel
|
2021-03-11 02:46:37 +00:00
|
|
|
from tests.helpers.datamodules import ClassifDataModule
|
|
|
|
from tests.helpers.runif import RunIf
|
|
|
|
|
|
|
|
|
2021-03-15 14:39:38 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-07-26 11:37:35 +00:00
|
|
|
"trainer_kwargs",
|
|
|
|
(
|
2021-03-15 14:39:38 +00:00
|
|
|
pytest.param(dict(gpus=1), marks=RunIf(min_gpus=1)),
|
2021-10-16 15:10:25 +00:00
|
|
|
pytest.param(dict(strategy="dp", gpus=2), marks=RunIf(min_gpus=2)),
|
|
|
|
pytest.param(dict(strategy="ddp_spawn", gpus=2), marks=RunIf(min_gpus=2)),
|
2021-07-26 11:37:35 +00:00
|
|
|
),
|
2021-03-15 14:39:38 +00:00
|
|
|
)
|
2021-03-11 02:46:37 +00:00
|
|
|
def test_evaluate(tmpdir, trainer_kwargs):
|
|
|
|
tutils.set_random_master_port()
|
2021-09-30 04:40:09 +00:00
|
|
|
seed_everything(1)
|
2021-03-11 02:46:37 +00:00
|
|
|
dm = ClassifDataModule()
|
|
|
|
model = CustomClassificationModelDP()
|
|
|
|
trainer = Trainer(
|
2021-09-30 04:40:09 +00:00
|
|
|
default_root_dir=tmpdir, max_epochs=2, limit_train_batches=10, limit_val_batches=10, **trainer_kwargs
|
2021-03-11 02:46:37 +00:00
|
|
|
)
|
|
|
|
|
2021-04-28 18:11:32 +00:00
|
|
|
trainer.fit(model, datamodule=dm)
|
2021-07-26 11:37:35 +00:00
|
|
|
assert "ckpt" in trainer.checkpoint_callback.best_model_path
|
2021-03-11 02:46:37 +00:00
|
|
|
|
|
|
|
old_weights = model.layer_0.weight.clone().detach().cpu()
|
|
|
|
|
2021-09-30 04:40:09 +00:00
|
|
|
trainer.validate(datamodule=dm)
|
|
|
|
trainer.test(datamodule=dm)
|
2021-03-11 02:46:37 +00:00
|
|
|
|
|
|
|
# make sure weights didn't change
|
|
|
|
new_weights = model.layer_0.weight.clone().detach().cpu()
|
|
|
|
torch.testing.assert_allclose(old_weights, new_weights)
|
2021-03-29 20:50:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_model_parallel_setup_called(tmpdir):
|
|
|
|
class TestModel(BoringModel):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.configure_sharded_model_called = False
|
|
|
|
self.layer = None
|
|
|
|
|
|
|
|
def configure_sharded_model(self):
|
|
|
|
self.configure_sharded_model_called = True
|
|
|
|
self.layer = torch.nn.Linear(32, 2)
|
|
|
|
|
|
|
|
model = TestModel()
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, limit_train_batches=2, limit_val_batches=2, max_epochs=1)
|
2021-03-29 20:50:51 +00:00
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
assert model.configure_sharded_model_called
|