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.
|
2021-02-08 10:52:02 +00:00
|
|
|
import tests.helpers.pipelines as tpipes
|
|
|
|
import tests.helpers.utils as tutils
|
2020-10-04 21:36:47 +00:00
|
|
|
from pytorch_lightning.callbacks import EarlyStopping
|
2020-10-01 15:26:58 +00:00
|
|
|
from pytorch_lightning.trainer import Trainer
|
2021-08-03 22:08:51 +00:00
|
|
|
from pytorch_lightning.utilities import memory
|
2021-02-23 22:08:46 +00:00
|
|
|
from tests.helpers import BoringModel
|
|
|
|
from tests.helpers.datamodules import ClassifDataModule
|
2021-03-02 09:36:01 +00:00
|
|
|
from tests.helpers.runif import RunIf
|
2021-02-23 22:08:46 +00:00
|
|
|
from tests.helpers.simple_models import ClassificationModel
|
2020-10-01 15:26:58 +00:00
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_gpus=2)
|
2020-10-01 15:26:58 +00:00
|
|
|
def test_multi_gpu_early_stop_ddp_spawn(tmpdir):
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
|
|
|
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,
|
|
|
|
gpus=[0, 1],
|
2021-07-26 11:37:35 +00:00
|
|
|
accelerator="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
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_gpus=2)
|
2020-10-01 15:26:58 +00:00
|
|
|
def test_multi_gpu_model_ddp_spawn(tmpdir):
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
|
|
|
trainer_options = dict(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=1,
|
|
|
|
limit_train_batches=10,
|
|
|
|
limit_val_batches=10,
|
|
|
|
gpus=[0, 1],
|
2021-07-26 11:37:35 +00:00
|
|
|
accelerator="ddp_spawn",
|
2020-12-09 08:18:23 +00:00
|
|
|
progress_bar_refresh_rate=0,
|
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)
|
|
|
|
|
|
|
|
# test memory helper functions
|
2021-07-26 11:37:35 +00:00
|
|
|
memory.get_memory_profile("min_max")
|
2020-10-01 15:26:58 +00:00
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_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()"""
|
|
|
|
tutils.set_random_master_port()
|
|
|
|
|
2021-02-23 22:08:46 +00:00
|
|
|
model = BoringModel()
|
2021-02-06 12:28:26 +00:00
|
|
|
fit_options = dict(train_dataloader=model.train_dataloader(), val_dataloaders=model.val_dataloader())
|
2020-10-01 15:26:58 +00:00
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
progress_bar_refresh_rate=0,
|
|
|
|
max_epochs=1,
|
|
|
|
limit_train_batches=0.2,
|
|
|
|
limit_val_batches=0.2,
|
|
|
|
gpus=[0, 1],
|
2021-07-26 11:37:35 +00:00
|
|
|
accelerator="ddp_spawn",
|
2020-10-01 15:26:58 +00:00
|
|
|
)
|
2021-01-12 00:36:48 +00:00
|
|
|
trainer.fit(model, **fit_options)
|
2021-05-04 10:50:56 +00:00
|
|
|
assert trainer.state.finished, "DDP doesn't work with dataloaders passed to fit()."
|