Skip to content

Commit 5a17564

Browse files
committed
fixing tested values
1 parent a0f7831 commit 5a17564

File tree

7 files changed

+37
-48
lines changed

7 files changed

+37
-48
lines changed

tests/helpers/pipelines.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@
1313
# limitations under the License.
1414
import torch
1515

16-
from pytorch_lightning import Trainer
16+
from pytorch_lightning import LightningDataModule, LightningModule, Trainer
17+
from pytorch_lightning.metrics.functional import accuracy
1718
from pytorch_lightning.trainer.states import TrainerState
1819
from pytorch_lightning.utilities import DistributedType
20+
from tests.base import EvalModelTemplate
1921
from tests.helpers import BoringModel
2022
from tests.helpers.utils import get_default_logger, load_model_from_checkpoint, reset_seed
2123

2224

23-
def run_model_test_without_loggers(trainer_options, model, min_acc: float = 0.50):
25+
def run_model_test_without_loggers(
26+
trainer_options: dict, model: LightningModule, data: LightningDataModule = None, min_acc: float = 0.50
27+
):
2428
reset_seed()
2529

2630
# fit model
2731
trainer = Trainer(**trainer_options)
28-
trainer.fit(model)
32+
trainer.fit(model, datamodule=data)
2933

3034
# correct result and ok accuracy
3135
assert trainer.state == TrainerState.FINISHED, f"Training failed with {trainer.state}"
@@ -35,12 +39,13 @@ def run_model_test_without_loggers(trainer_options, model, min_acc: float = 0.50
3539
)
3640

3741
# test new model accuracy
38-
test_loaders = model.test_dataloader()
42+
test_loaders = model.test_dataloader() if not data else data.test_dataloader()
3943
if not isinstance(test_loaders, list):
4044
test_loaders = [test_loaders]
4145

42-
for dataloader in test_loaders:
43-
run_prediction(pretrained_model, dataloader, min_acc=min_acc)
46+
if not isinstance(model, BoringModel):
47+
for dataloader in test_loaders:
48+
run_prediction_eval_model_template(model, dataloader, min_acc=min_acc)
4449

4550
if trainer._distrib_type in (DistributedType.DDP, DistributedType.DDP_SPAWN):
4651
# on hpc this would work fine... but need to hack it for the purpose of the test
@@ -77,8 +82,9 @@ def run_model_test(
7782
if not isinstance(test_loaders, list):
7883
test_loaders = [test_loaders]
7984

80-
for dataloader in test_loaders:
81-
run_prediction(pretrained_model, dataloader, min_acc=min_acc)
85+
if not isinstance(model, BoringModel):
86+
for dataloader in test_loaders:
87+
run_prediction_eval_model_template(model, dataloader, min_acc=min_acc)
8288

8389
if with_hpc:
8490
if trainer._distrib_type in (DistributedType.DDP, DistributedType.DDP_SPAWN, DistributedType.DDP2):
@@ -95,14 +101,7 @@ def run_model_test(
95101
trainer.checkpoint_connector.hpc_load(checkpoint_path, on_gpu=on_gpu)
96102

97103

98-
def run_prediction(trained_model, dataloader, dp=False, min_acc=0.25):
99-
if isinstance(trained_model, BoringModel):
100-
return _boring_model_run_prediction(trained_model, dataloader, dp, min_acc)
101-
else:
102-
return _eval_model_template_run_prediction(trained_model, dataloader, dp, min_acc)
103-
104-
105-
def _eval_model_template_run_prediction(trained_model, dataloader, dp=False, min_acc=0.50):
104+
def run_prediction_eval_model_template(trained_model, dataloader, dp=False, min_acc=0.50):
106105
# run prediction on 1 batch
107106
batch = next(iter(dataloader))
108107
x, y = batch
@@ -117,24 +116,6 @@ def _eval_model_template_run_prediction(trained_model, dataloader, dp=False, min
117116
else:
118117
with torch.no_grad():
119118
y_hat = trained_model(x)
120-
y_hat = y_hat.cpu()
121-
122-
# acc
123-
labels_hat = torch.argmax(y_hat, dim=1)
124-
125-
y = y.cpu()
126-
acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
127-
acc = torch.tensor(acc)
128-
acc = acc.item()
119+
acc = accuracy(y_hat.cpu(), y.cpu()).item()
129120

130121
assert acc >= min_acc, f"This model is expected to get > {min_acc} in test set (it got {acc})"
131-
132-
133-
def _boring_model_run_prediction(trained_model, dataloader, dp=False, min_acc=0.25):
134-
# run prediction on 1 batch
135-
batch = next(iter(dataloader))
136-
with torch.no_grad():
137-
output = trained_model(batch)
138-
acc = trained_model.loss(batch, output)
139-
140-
assert acc >= min_acc, f"This model is expected to get, {min_acc} in test set but got {acc}"

tests/models/data/horovod/train_default_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
print('You requested to import Horovod which is missing or not supported for your OS.')
3838

3939
from tests.base import EvalModelTemplate # noqa: E402
40-
from tests.helpers.pipelines import run_prediction # noqa: E402
40+
from tests.helpers.pipelines import run_prediction_eval_model_template # noqa: E402
4141
from tests.helpers.utils import reset_seed, set_random_master_port # noqa: E402
4242

4343
parser = argparse.ArgumentParser()
@@ -74,7 +74,7 @@ def run_test_from_config(trainer_options):
7474
test_loaders = [test_loaders]
7575

7676
for dataloader in test_loaders:
77-
run_prediction(pretrained_model, dataloader)
77+
run_prediction_eval_model_template(pretrained_model, dataloader)
7878

7979
# test HPC saving
8080
trainer.checkpoint_connector.hpc_save(ckpt_path, trainer.logger)

tests/models/test_cpu.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from pytorch_lightning.trainer.states import TrainerState
2626
from tests.base import EvalModelTemplate
2727
from tests.helpers import BoringModel
28+
from tests.helpers.datamodules import ClassifDataModule
29+
from tests.helpers.simple_models import ClassificationModel
2830

2931

3032
def test_cpu_slurm_save_load(tmpdir):
@@ -149,7 +151,7 @@ def test_multi_cpu_model_ddp(tmpdir):
149151
def test_lbfgs_cpu_model(tmpdir):
150152
"""Test each of the trainer options. Testing LBFGS optimizer"""
151153

152-
class ModelSpecifiedOptimizer(BoringModel):
154+
class ModelSpecifiedOptimizer(ClassificationModel):
153155

154156
def __init__(self, optimizer_name, learning_rate):
155157
super().__init__()
@@ -166,8 +168,9 @@ def __init__(self, optimizer_name, learning_rate):
166168
limit_val_batches=0.2,
167169
)
168170

171+
dm = ClassifDataModule()
169172
model = ModelSpecifiedOptimizer(optimizer_name="LBFGS", learning_rate=0.004)
170-
tpipes.run_model_test_without_loggers(trainer_options, model, min_acc=0.25)
173+
tpipes.run_model_test_without_loggers(trainer_options, model, dm, min_acc=0.25)
171174

172175

173176
def test_default_logger_callbacks_cpu_model(tmpdir):

tests/models/test_gpu.py

Lines changed: 3 additions & 2 deletions
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.simple_models import ClassificationModel
2829

2930
PRETEND_N_OF_GPUS = 16
3031

@@ -42,8 +43,8 @@ def test_multi_gpu_none_backend(tmpdir):
4243
gpus=2,
4344
)
4445

45-
model = BoringModel()
46-
tpipes.run_model_test(trainer_options, model, min_acc=0.20)
46+
model = ClassificationModel()
47+
tpipes.run_model_test(trainer_options, model)
4748

4849

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

tests/models/test_horovod.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from tests.base import EvalModelTemplate
3434
from tests.helpers.advanced_models import BasicGAN
3535
from tests.helpers.boring_model import BoringModel
36+
from tests.helpers.datamodules import ClassifDataModule
37+
from tests.helpers.simple_models import ClassificationModel
3638

3739
if _HOROVOD_AVAILABLE:
3840
import horovod
@@ -173,7 +175,7 @@ def test_horovod_amp(tmpdir):
173175
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
174176
def test_horovod_transfer_batch_to_gpu(tmpdir):
175177

176-
class TestTrainingStepModel(EvalModelTemplate):
178+
class TestTrainingStepModel(ClassificationModel):
177179

178180
def training_step(self, batch, *args, **kwargs):
179181
x, y = batch
@@ -200,7 +202,9 @@ def validation_step(self, batch, *args, **kwargs):
200202
deterministic=True,
201203
accelerator='horovod',
202204
)
203-
tpipes.run_model_test_without_loggers(trainer_options, model)
205+
206+
dm = ClassifDataModule()
207+
tpipes.run_model_test_without_loggers(trainer_options, model, dm, min_acc=0.25)
204208

205209

206210
@pytest.mark.skipif(platform.system() == "Windows", reason="Horovod is not supported on Windows")

tests/models/test_restore.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_running_test_pretrained_model_distrib_dp(tmpdir):
200200
dataloaders = [dataloaders]
201201

202202
for dataloader in dataloaders:
203-
tpipes.run_prediction(pretrained_model, dataloader)
203+
tpipes.run_prediction_eval_model_template(pretrained_model, dataloader)
204204

205205

206206
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
@@ -251,7 +251,7 @@ def test_running_test_pretrained_model_distrib_ddp_spawn(tmpdir):
251251
dataloaders = [dataloaders]
252252

253253
for dataloader in dataloaders:
254-
tpipes.run_prediction(pretrained_model, dataloader)
254+
tpipes.run_prediction_eval_model_template(pretrained_model, dataloader)
255255

256256

257257
def test_running_test_pretrained_model_cpu(tmpdir):
@@ -394,7 +394,7 @@ def assert_good_acc():
394394
dp_model.module.module.running_stage = RunningStage.EVALUATING
395395

396396
dataloader = trainer.train_dataloader
397-
tpipes.run_prediction(dp_model, dataloader, dp=True)
397+
tpipes.run_prediction_eval_model_template(dp_model, dataloader, dp=True)
398398

399399
# new model
400400
model = EvalModelTemplate(**hparams)

tests/trainer/test_dataloaders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def test_multiple_val_dataloader(tmpdir):
131131

132132
# make sure predictions are good for each val set
133133
for dataloader in trainer.val_dataloaders:
134-
tpipes.run_prediction(trainer.model, dataloader)
134+
tpipes.run_prediction_eval_model_template(trainer.model, dataloader)
135135

136136

137137
@pytest.mark.parametrize('ckpt_path', [None, 'best', 'specific'])
@@ -168,7 +168,7 @@ def test_step(self, batch, batch_idx, *args, **kwargs):
168168

169169
# make sure predictions are good for each test set
170170
for dataloader in trainer.test_dataloaders:
171-
tpipes.run_prediction(trainer.model, dataloader)
171+
tpipes.run_prediction_eval_model_template(trainer.model, dataloader)
172172

173173
# run the test method
174174
trainer.test(ckpt_path=ckpt_path)

0 commit comments

Comments
 (0)