Skip to content

Commit d58b2f8

Browse files
committed
tests
1 parent c3fd9c2 commit d58b2f8

File tree

8 files changed

+47
-24
lines changed

8 files changed

+47
-24
lines changed

tests/accelerators/legacy/test_ddp_spawn.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
from pytorch_lightning.trainer import Trainer
2222
from pytorch_lightning.trainer.states import TrainerState
2323
from tests.base import EvalModelTemplate
24+
from tests.helpers import BoringModel
25+
from tests.helpers.datamodules import ClassifDataModule
26+
from tests.helpers.simple_models import ClassificationModel
2427

2528

2629
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
@@ -38,8 +41,9 @@ def test_multi_gpu_early_stop_ddp_spawn(tmpdir):
3841
accelerator='ddp_spawn',
3942
)
4043

41-
model = EvalModelTemplate()
42-
tpipes.run_model_test(trainer_options, model)
44+
dm = ClassifDataModule()
45+
model = ClassificationModel()
46+
tpipes.run_model_test(trainer_options, model, dm)
4347

4448

4549
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
@@ -56,7 +60,7 @@ def test_multi_gpu_model_ddp_spawn(tmpdir):
5660
progress_bar_refresh_rate=0,
5761
)
5862

59-
model = EvalModelTemplate()
63+
model = BoringModel()
6064

6165
tpipes.run_model_test(trainer_options, model)
6266

tests/accelerators/legacy/test_dp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
from pytorch_lightning.callbacks import EarlyStopping
2121
from pytorch_lightning.core import memory
2222
from tests.base import EvalModelTemplate
23+
from tests.helpers import BoringModel
24+
from tests.helpers.datamodules import ClassifDataModule
25+
from tests.helpers.simple_models import ClassificationModel
2326

2427
PRETEND_N_OF_GPUS = 16
2528

@@ -39,8 +42,9 @@ def test_multi_gpu_early_stop_dp(tmpdir):
3942
accelerator='dp',
4043
)
4144

42-
model = EvalModelTemplate()
43-
tpipes.run_model_test(trainer_options, model)
45+
dm = ClassifDataModule()
46+
model = ClassificationModel()
47+
tpipes.run_model_test(trainer_options, model, dm)
4448

4549

4650
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
@@ -57,7 +61,7 @@ def test_multi_gpu_model_dp(tmpdir):
5761
progress_bar_refresh_rate=0,
5862
)
5963

60-
model = EvalModelTemplate()
64+
model = BoringModel()
6165

6266
tpipes.run_model_test(trainer_options, model)
6367

tests/helpers/pipelines.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ def run_model_test_without_loggers(
5454

5555

5656
def run_model_test(
57-
trainer_options, model, on_gpu: bool = True, version=None, with_hpc: bool = True, min_acc: float = 0.25
57+
trainer_options,
58+
model: LightningModule,
59+
data: LightningDataModule = None,
60+
on_gpu: bool = True,
61+
version=None,
62+
with_hpc: bool = True,
63+
min_acc: float = 0.25
5864
):
5965

6066
reset_seed()
@@ -66,7 +72,7 @@ def run_model_test(
6672

6773
trainer = Trainer(**trainer_options)
6874
initial_values = torch.tensor([torch.sum(torch.abs(x)) for x in model.parameters()])
69-
trainer.fit(model)
75+
trainer.fit(model, datamodule=data)
7076
post_train_values = torch.tensor([torch.sum(torch.abs(x)) for x in model.parameters()])
7177

7278
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"
@@ -78,7 +84,7 @@ def run_model_test(
7884
pretrained_model = load_model_from_checkpoint(logger, trainer.checkpoint_callback.best_model_path, type(model))
7985

8086
# test new model accuracy
81-
test_loaders = model.test_dataloader()
87+
test_loaders = model.test_dataloader() if not data else data.test_dataloader()
8288
if not isinstance(test_loaders, list):
8389
test_loaders = [test_loaders]
8490

tests/models/test_amp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pytorch_lightning.utilities import _APEX_AVAILABLE
2626
from pytorch_lightning.utilities.exceptions import MisconfigurationException
2727
from tests.base import EvalModelTemplate
28+
from tests.helpers import BoringModel
2829

2930

3031
@pytest.mark.skip(reason='dp + amp not supported currently') # TODO
@@ -156,7 +157,7 @@ def test_cpu_model_with_amp(tmpdir):
156157
precision=16,
157158
)
158159

159-
model = EvalModelTemplate()
160+
model = BoringModel()
160161

161162
with pytest.raises((MisconfigurationException, ModuleNotFoundError)):
162163
tpipes.run_model_test(trainer_options, model, on_gpu=False)

tests/models/test_cpu.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from pytorch_lightning import Trainer
2424
from pytorch_lightning.callbacks import Callback, EarlyStopping, ModelCheckpoint
2525
from pytorch_lightning.trainer.states import TrainerState
26-
from tests.base import EvalModelTemplate
2726
from tests.helpers import BoringModel
2827
from tests.helpers.datamodules import ClassifDataModule
2928
from tests.helpers.simple_models import ClassificationModel
@@ -304,7 +303,7 @@ def test_cpu_model(tmpdir):
304303
limit_val_batches=0.4
305304
)
306305

307-
model = EvalModelTemplate()
306+
model = BoringModel()
308307

309308
tpipes.run_model_test(trainer_options, model, on_gpu=False)
310309

tests/models/test_gpu.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pytorch_lightning.utilities import device_parser
2626
from pytorch_lightning.utilities.exceptions import MisconfigurationException
2727
from tests.helpers import BoringModel
28+
from tests.helpers.datamodules import ClassifDataModule
2829
from tests.helpers.simple_models import ClassificationModel
2930

3031
PRETEND_N_OF_GPUS = 16
@@ -43,8 +44,9 @@ def test_multi_gpu_none_backend(tmpdir):
4344
gpus=2,
4445
)
4546

47+
dm = ClassifDataModule()
4648
model = ClassificationModel()
47-
tpipes.run_model_test(trainer_options, model)
49+
tpipes.run_model_test(trainer_options, model, dm)
4850

4951

5052
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")

tests/models/test_onnx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_model_saves_on_multi_gpu(tmpdir):
9292
progress_bar_refresh_rate=0,
9393
)
9494

95-
model = EvalModelTemplate()
95+
model = BoringModel()
9696

9797
tpipes.run_model_test(trainer_options, model)
9898

tests/models/test_tpu.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from pytorch_lightning.utilities import _TPU_AVAILABLE
2727
from pytorch_lightning.utilities.exceptions import MisconfigurationException
2828
from tests.base import EvalModelTemplate
29+
from tests.helpers import BoringModel
2930
from tests.helpers.datasets import TrialMNIST
3031
from tests.helpers.utils import pl_multi_process_test
3132

@@ -55,7 +56,7 @@ def test_model_tpu_cores_1(tmpdir):
5556
limit_val_batches=0.4,
5657
)
5758

58-
model = EvalModelTemplate()
59+
model = BoringModel()
5960
tpipes.run_model_test(trainer_options, model, on_gpu=False, with_hpc=False)
6061

6162

@@ -73,7 +74,7 @@ def test_model_tpu_index(tmpdir, tpu_core):
7374
limit_val_batches=0.4,
7475
)
7576

76-
model = EvalModelTemplate()
77+
model = BoringModel()
7778
tpipes.run_model_test(trainer_options, model, on_gpu=False, with_hpc=False)
7879
assert torch_xla._XLAC._xla_get_default_device() == f'xla:{tpu_core}'
7980

@@ -113,7 +114,7 @@ def test_model_16bit_tpu_cores_1(tmpdir):
113114
limit_val_batches=0.4,
114115
)
115116

116-
model = EvalModelTemplate()
117+
model = BoringModel()
117118
tpipes.run_model_test(trainer_options, model, on_gpu=False)
118119
assert os.environ.get('XLA_USE_BF16') == str(1), "XLA_USE_BF16 was not set in environment variables"
119120

@@ -133,7 +134,7 @@ def test_model_16bit_tpu_index(tmpdir, tpu_core):
133134
limit_val_batches=0.2,
134135
)
135136

136-
model = EvalModelTemplate()
137+
model = BoringModel()
137138
tpipes.run_model_test(trainer_options, model, on_gpu=False)
138139
assert torch_xla._XLAC._xla_get_default_device() == f'xla:{tpu_core}'
139140
assert os.environ.get('XLA_USE_BF16') == str(1), "XLA_USE_BF16 was not set in environment variables"
@@ -192,19 +193,25 @@ def test_tpu_grad_norm(tmpdir):
192193
gradient_clip_val=0.1,
193194
)
194195

195-
model = EvalModelTemplate()
196+
model = BoringModel()
196197
tpipes.run_model_test(trainer_options, model, on_gpu=False, with_hpc=False)
197198

198199

199200
@pytest.mark.skipif(not _TPU_AVAILABLE, reason="test requires TPU machine")
200201
@pl_multi_process_test
201202
def test_dataloaders_passed_to_fit(tmpdir):
202203
"""Test if dataloaders passed to trainer works on TPU"""
203-
204-
model = EvalModelTemplate()
205-
206-
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, tpu_cores=8)
207-
trainer.fit(model, train_dataloader=model.train_dataloader(), val_dataloaders=model.val_dataloader())
204+
model = BoringModel()
205+
trainer = Trainer(
206+
default_root_dir=tmpdir,
207+
max_epochs=1,
208+
tpu_cores=8,
209+
)
210+
trainer.fit(
211+
model,
212+
train_dataloader=model.train_dataloader(),
213+
val_dataloaders=model.val_dataloader(),
214+
)
208215
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"
209216

210217

0 commit comments

Comments
 (0)