2022-02-22 05:14:18 +00:00
|
|
|
# Copyright The Lightning AI 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.
|
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
2023-02-02 10:06:45 +00:00
|
|
|
from lightning.pytorch import Trainer
|
|
|
|
from lightning.pytorch.demos.boring_classes import BoringModel
|
2022-06-15 22:10:49 +00:00
|
|
|
from tests_pytorch.helpers.datamodules import ClassifDataModule
|
|
|
|
from tests_pytorch.helpers.runif import RunIf
|
2023-02-16 05:12:08 +00:00
|
|
|
from tests_pytorch.helpers.simple_models import ClassificationModel
|
2022-02-22 05:14:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"trainer_kwargs",
|
|
|
|
(
|
2023-04-21 09:07:58 +00:00
|
|
|
pytest.param({"accelerator": "gpu", "devices": 1}, marks=RunIf(min_cuda_gpus=1)),
|
|
|
|
pytest.param({"strategy": "ddp_spawn", "accelerator": "gpu", "devices": 2}, marks=RunIf(min_cuda_gpus=2)),
|
|
|
|
pytest.param({"accelerator": "mps", "devices": 1}, marks=RunIf(mps=True)),
|
2022-02-22 05:14:18 +00:00
|
|
|
),
|
|
|
|
)
|
2022-11-01 11:40:32 +00:00
|
|
|
@RunIf(sklearn=True)
|
2022-02-22 05:14:18 +00:00
|
|
|
def test_evaluate(tmpdir, trainer_kwargs):
|
|
|
|
dm = ClassifDataModule()
|
2023-02-16 05:12:08 +00:00
|
|
|
model = ClassificationModel()
|
2022-02-22 05:14:18 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir, max_epochs=2, limit_train_batches=10, limit_val_batches=10, **trainer_kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
trainer.fit(model, datamodule=dm)
|
|
|
|
assert "ckpt" in trainer.checkpoint_callback.best_model_path
|
|
|
|
|
|
|
|
old_weights = model.layer_0.weight.clone().detach().cpu()
|
|
|
|
|
|
|
|
trainer.validate(datamodule=dm)
|
|
|
|
trainer.test(datamodule=dm)
|
|
|
|
|
|
|
|
# make sure weights didn't change
|
|
|
|
new_weights = model.layer_0.weight.clone().detach().cpu()
|
2022-10-07 14:59:04 +00:00
|
|
|
torch.testing.assert_close(old_weights, new_weights)
|
2022-02-22 05:14:18 +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()
|
|
|
|
trainer = Trainer(default_root_dir=tmpdir, limit_train_batches=2, limit_val_batches=2, max_epochs=1)
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
assert model.configure_sharded_model_called
|