2020-10-13 11:18:07 +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.
|
2023-02-08 14:00:31 +00:00
|
|
|
import pytest
|
|
|
|
from torch.multiprocessing import ProcessRaisedException
|
|
|
|
|
2022-06-15 22:10:49 +00:00
|
|
|
import tests_pytorch.helpers.pipelines as tpipes
|
2023-02-02 10:06:45 +00:00
|
|
|
from lightning.pytorch.callbacks import EarlyStopping
|
|
|
|
from lightning.pytorch.demos.boring_classes import BoringModel
|
|
|
|
from lightning.pytorch.trainer import Trainer
|
2022-06-15 22:10:49 +00:00
|
|
|
from tests_pytorch.helpers.datamodules import ClassifDataModule
|
|
|
|
from tests_pytorch.helpers.runif import RunIf
|
|
|
|
from tests_pytorch.helpers.simple_models import ClassificationModel
|
2023-02-08 14:00:31 +00:00
|
|
|
from tests_pytorch.strategies.test_ddp_strategy import UnusedParametersModel
|
2020-10-01 15:26:58 +00:00
|
|
|
|
|
|
|
|
2022-11-01 11:40:32 +00:00
|
|
|
@RunIf(min_cuda_gpus=2, sklearn=True)
|
2020-10-01 15:26:58 +00:00
|
|
|
def test_multi_gpu_early_stop_ddp_spawn(tmpdir):
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
2021-07-26 11:37:35 +00:00
|
|
|
callbacks=[EarlyStopping(monitor="train_acc")],
|
2020-10-01 15:26:58 +00:00
|
|
|
max_epochs=50,
|
|
|
|
limit_train_batches=10,
|
|
|
|
limit_val_batches=10,
|
2022-03-24 14:09:39 +00:00
|
|
|
accelerator="gpu",
|
|
|
|
devices=[0, 1],
|
2021-10-16 15:10:25 +00:00
|
|
|
strategy="ddp_spawn",
|
2020-10-01 15:26:58 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 22:08:46 +00:00
|
|
|
dm = ClassifDataModule()
|
|
|
|
model = ClassificationModel()
|
|
|
|
tpipes.run_model_test(trainer_options, model, dm)
|
2020-10-01 15:26:58 +00:00
|
|
|
|
|
|
|
|
2022-05-24 12:54:05 +00:00
|
|
|
@RunIf(min_cuda_gpus=2)
|
2020-10-01 15:26:58 +00:00
|
|
|
def test_multi_gpu_model_ddp_spawn(tmpdir):
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=1,
|
|
|
|
limit_train_batches=10,
|
|
|
|
limit_val_batches=10,
|
2022-03-24 14:09:39 +00:00
|
|
|
accelerator="gpu",
|
|
|
|
devices=[0, 1],
|
2021-10-16 15:10:25 +00:00
|
|
|
strategy="ddp_spawn",
|
2021-09-25 05:53:31 +00:00
|
|
|
enable_progress_bar=False,
|
2020-10-01 15:26:58 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 22:08:46 +00:00
|
|
|
model = BoringModel()
|
2020-10-01 15:26:58 +00:00
|
|
|
|
|
|
|
tpipes.run_model_test(trainer_options, model)
|
|
|
|
|
|
|
|
|
2022-05-24 12:54:05 +00:00
|
|
|
@RunIf(min_cuda_gpus=2)
|
2020-10-01 15:26:58 +00:00
|
|
|
def test_ddp_all_dataloaders_passed_to_fit(tmpdir):
|
|
|
|
"""Make sure DDP works with dataloaders passed to fit()"""
|
2021-02-23 22:08:46 +00:00
|
|
|
model = BoringModel()
|
2020-10-01 15:26:58 +00:00
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2021-09-25 05:53:31 +00:00
|
|
|
enable_progress_bar=False,
|
2020-10-01 15:26:58 +00:00
|
|
|
max_epochs=1,
|
|
|
|
limit_train_batches=0.2,
|
|
|
|
limit_val_batches=0.2,
|
2022-03-24 14:09:39 +00:00
|
|
|
accelerator="gpu",
|
|
|
|
devices=[0, 1],
|
2021-10-16 15:10:25 +00:00
|
|
|
strategy="ddp_spawn",
|
2020-10-01 15:26:58 +00:00
|
|
|
)
|
2021-11-04 10:03:39 +00:00
|
|
|
trainer.fit(model, train_dataloaders=model.train_dataloader(), val_dataloaders=model.val_dataloader())
|
2021-05-04 10:50:56 +00:00
|
|
|
assert trainer.state.finished, "DDP doesn't work with dataloaders passed to fit()."
|
2023-02-08 14:00:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_ddp_spawn_find_unused_parameters_exception():
|
|
|
|
"""Test that the DDP strategy can change PyTorch's error message so that it's more useful for Lightning
|
|
|
|
users."""
|
|
|
|
trainer = Trainer(accelerator="cpu", devices=1, strategy="ddp_spawn", max_steps=2)
|
|
|
|
with pytest.raises(
|
|
|
|
ProcessRaisedException, match="It looks like your LightningModule has parameters that were not used in"
|
|
|
|
):
|
|
|
|
trainer.fit(UnusedParametersModel())
|