Skip to content

Commit

Permalink
test: replacing tmpdir with tmp_path in tests_pytorch/trainer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fnhirwa committed Mar 17, 2024
1 parent c66b23f commit 5687047
Show file tree
Hide file tree
Showing 23 changed files with 451 additions and 449 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ def test_precision_and_precision_plugin_raises():


@RunIf(skip_windows=True, standalone=True)
def test_strategy_choice_ddp_on_cpu(tmpdir):
def test_strategy_choice_ddp_on_cpu(tmp_path):
"""Test that selecting DDPStrategy on CPU works."""
_test_strategy_choice_ddp_and_cpu(tmpdir, ddp_strategy_class=DDPStrategy)
_test_strategy_choice_ddp_and_cpu(tmp_path, ddp_strategy_class=DDPStrategy)


def _test_strategy_choice_ddp_and_cpu(tmpdir, ddp_strategy_class):
def _test_strategy_choice_ddp_and_cpu(tmp_path, ddp_strategy_class):
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
fast_dev_run=True,
strategy=ddp_strategy_class(find_unused_parameters=True),
accelerator="cpu",
Expand All @@ -132,7 +132,7 @@ def _test_strategy_choice_ddp_and_cpu(tmpdir, ddp_strategy_class):
"SLURM_LOCALID": "0",
},
)
def test_custom_cluster_environment_in_slurm_environment(cuda_count_0, tmpdir):
def test_custom_cluster_environment_in_slurm_environment(cuda_count_0, tmp_path):
"""Test that we choose the custom cluster even when SLURM or TE flags are around."""

class CustomCluster(LightningEnvironment):
Expand All @@ -145,7 +145,7 @@ def creates_processes_externally(self) -> bool:
return True

trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
plugins=[CustomCluster()],
fast_dev_run=True,
accelerator="cpu",
Expand Down Expand Up @@ -253,8 +253,8 @@ def test_interactive_compatible_strategy_ddp_fork(monkeypatch):
],
)
@pytest.mark.parametrize("devices", [1, 2])
def test_accelerator_choice_multi_node_gpu(cuda_count_2, tmpdir, strategy, strategy_class, devices):
trainer = Trainer(default_root_dir=tmpdir, num_nodes=2, accelerator="gpu", strategy=strategy, devices=devices)
def test_accelerator_choice_multi_node_gpu(cuda_count_2, tmp_path, strategy, strategy_class, devices):
trainer = Trainer(default_root_dir=tmp_path, num_nodes=2, accelerator="gpu", strategy=strategy, devices=devices)
assert isinstance(trainer.strategy, strategy_class)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
from lightning.pytorch.trainer.connectors.callback_connector import _CallbackConnector


def test_checkpoint_callbacks_are_last(tmpdir):
def test_checkpoint_callbacks_are_last(tmp_path):
"""Test that checkpoint callbacks always get moved to the end of the list, with preserved order."""
checkpoint1 = ModelCheckpoint(tmpdir, monitor="foo")
checkpoint2 = ModelCheckpoint(tmpdir, monitor="bar")
checkpoint1 = ModelCheckpoint(tmp_path, monitor="foo")
checkpoint2 = ModelCheckpoint(tmp_path, monitor="bar")
model_summary = ModelSummary()
early_stopping = EarlyStopping(monitor="foo")
lr_monitor = LearningRateMonitor()
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_checkpoint_callbacks_are_last(tmpdir):
# with model-specific callbacks that substitute ones in Trainer
model = LightningModule()
model.configure_callbacks = lambda: [checkpoint1, early_stopping, model_summary, checkpoint2]
trainer = Trainer(callbacks=[progress_bar, lr_monitor, ModelCheckpoint(tmpdir)])
trainer = Trainer(callbacks=[progress_bar, lr_monitor, ModelCheckpoint(tmp_path)])
trainer.strategy._lightning_module = model
cb_connector = _CallbackConnector(trainer)
cb_connector._attach_model_callbacks()
Expand Down Expand Up @@ -120,17 +120,17 @@ def state_dict(self):
return {"content1": self._unique}


def test_all_callback_states_saved_before_checkpoint_callback(tmpdir):
def test_all_callback_states_saved_before_checkpoint_callback(tmp_path):
"""Test that all callback states get saved even if the ModelCheckpoint is not given as last and when there are
multiple callbacks of the same type."""

callback0 = StatefulCallback0()
callback1 = StatefulCallback1(unique="one")
callback2 = StatefulCallback1(unique="two", other=2)
checkpoint_callback = ModelCheckpoint(dirpath=tmpdir, filename="all_states")
checkpoint_callback = ModelCheckpoint(dirpath=tmp_path, filename="all_states")
model = BoringModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
max_steps=1,
limit_val_batches=1,
callbacks=[
Expand All @@ -144,7 +144,7 @@ def test_all_callback_states_saved_before_checkpoint_callback(tmpdir):
)
trainer.fit(model)

ckpt = torch.load(str(tmpdir / "all_states.ckpt"))
ckpt = torch.load(str(tmp_path / "all_states.ckpt"))
state0 = ckpt["callbacks"]["StatefulCallback0"]
state1 = ckpt["callbacks"]["StatefulCallback1{'unique': 'one'}"]
state2 = ckpt["callbacks"]["StatefulCallback1{'unique': 'two'}"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_signal_handlers_restored_in_teardown():


@RunIf(skip_windows=True)
def test_sigterm_handler_can_be_added(tmpdir):
def test_sigterm_handler_can_be_added(tmp_path):
handler_ran = False

def handler(*_):
Expand All @@ -57,7 +57,7 @@ def training_step(self, batch, batch_idx):
os.kill(os.getpid(), signal.SIGTERM)

model = TestModel()
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, limit_train_batches=2, limit_val_batches=0)
trainer = Trainer(default_root_dir=tmp_path, max_epochs=1, limit_train_batches=2, limit_val_batches=0)

assert not trainer.received_sigterm
assert not handler_ran
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __len__(self):


@pytest.mark.parametrize("seq_type", [tuple, list])
def test_multiple_eval_dataloaders_seq(tmpdir, seq_type):
def test_multiple_eval_dataloaders_seq(tmp_path, seq_type):
class TestModel(BoringModel):
def validation_step(self, batch, batch_idx, dataloader_idx):
if dataloader_idx == 0:
Expand All @@ -61,7 +61,7 @@ def val_dataloader(self):
model = TestModel()

trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
limit_train_batches=2,
limit_val_batches=2,
max_epochs=1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
("max_epochs", "expected_val_loop_calls", "expected_val_batches"),
[(1, 0, [0]), (4, 2, [0, 2, 0, 2]), (5, 2, [0, 2, 0, 2, 0])],
)
def test_check_val_every_n_epoch(tmpdir, max_epochs, expected_val_loop_calls, expected_val_batches):
def test_check_val_every_n_epoch(tmp_path, max_epochs, expected_val_loop_calls, expected_val_batches):
class TestModel(BoringModel):
val_epoch_calls = 0
val_batches = []
Expand All @@ -34,7 +34,7 @@ def on_validation_epoch_start(self) -> None:

model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
max_epochs=max_epochs,
num_sanity_val_steps=0,
limit_val_batches=2,
Expand All @@ -48,7 +48,7 @@ def on_validation_epoch_start(self) -> None:
assert model.val_batches == expected_val_batches


def test_check_val_every_n_epoch_with_max_steps(tmpdir):
def test_check_val_every_n_epoch_with_max_steps(tmp_path):
data_samples_train = 2
check_val_every_n_epoch = 3
max_epochs = 4
Expand All @@ -67,7 +67,7 @@ def train_dataloader(self):

model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
max_steps=data_samples_train * max_epochs,
check_val_every_n_epoch=check_val_every_n_epoch,
num_sanity_val_steps=0,
Expand Down
10 changes: 5 additions & 5 deletions tests/tests_pytorch/trainer/flags/test_fast_dev_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
from lightning.pytorch.tuner.tuning import Tuner


def test_skip_on_fast_dev_run_tuner(tmpdir):
def test_skip_on_fast_dev_run_tuner(tmp_path):
"""Test that tuner algorithms are skipped if fast dev run is enabled."""
model = BoringModel()
model.lr = 0.1 # avoid no-lr-found exception
model.batch_size = 8
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
max_epochs=2,
fast_dev_run=True,
)
Expand All @@ -31,7 +31,7 @@ def test_skip_on_fast_dev_run_tuner(tmpdir):


@pytest.mark.parametrize("fast_dev_run", [1, 4])
def test_callbacks_and_logger_not_called_with_fastdevrun(tmpdir, fast_dev_run):
def test_callbacks_and_logger_not_called_with_fastdevrun(tmp_path, fast_dev_run):
"""Test that ModelCheckpoint, EarlyStopping and Logger are turned off with fast_dev_run."""

class FastDevRunModel(BoringModel):
Expand Down Expand Up @@ -68,10 +68,10 @@ def test_step(self, batch, batch_idx):
early_stopping_callback = EarlyStopping(monitor="foo")
early_stopping_callback._evaluate_stopping_criteria = Mock()
trainer_config = {
"default_root_dir": tmpdir,
"default_root_dir": tmp_path,
"fast_dev_run": fast_dev_run,
"val_check_interval": 2,
"logger": TensorBoardLogger(tmpdir),
"logger": TensorBoardLogger(tmp_path),
"log_every_n_steps": 1,
"callbacks": [checkpoint_callback, early_stopping_callback],
}
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_pytorch/trainer/flags/test_limit_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
from lightning.pytorch.trainer.states import TrainerFn


def test_num_dataloader_batches(tmpdir):
def test_num_dataloader_batches(tmp_path):
"""Tests that the correct number of batches are allocated."""
# when we have fewer batches in the dataloader we should use those instead of the limit
model = BoringModel()
trainer = Trainer(limit_val_batches=100, limit_train_batches=100, max_epochs=1, default_root_dir=tmpdir)
trainer = Trainer(limit_val_batches=100, limit_train_batches=100, max_epochs=1, default_root_dir=tmp_path)
trainer.fit(model)

assert len(model.train_dataloader()) == 64
Expand All @@ -34,7 +34,7 @@ def test_num_dataloader_batches(tmpdir):

# when we have more batches in the dataloader we should limit them
model = BoringModel()
trainer = Trainer(limit_val_batches=7, limit_train_batches=7, max_epochs=1, default_root_dir=tmpdir)
trainer = Trainer(limit_val_batches=7, limit_train_batches=7, max_epochs=1, default_root_dir=tmp_path)
trainer.fit(model)

assert len(model.train_dataloader()) == 64
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_pytorch/trainer/flags/test_min_max_epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
(None, 3, 10, -1),
],
)
def test_min_max_steps_epochs(tmpdir, min_epochs, max_epochs, min_steps, max_steps):
def test_min_max_steps_epochs(tmp_path, min_epochs, max_epochs, min_steps, max_steps):
"""Tests that max_steps can be used without max_epochs."""
model = BoringModel()

trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
min_epochs=min_epochs,
max_epochs=max_epochs,
min_steps=min_steps,
Expand Down
8 changes: 4 additions & 4 deletions tests/tests_pytorch/trainer/flags/test_overfit_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@


@pytest.mark.parametrize("overfit_batches", [1, 2, 0.1, 0.25, 1.0])
def test_overfit_basic(tmpdir, overfit_batches):
def test_overfit_basic(tmp_path, overfit_batches):
"""Tests that only training_step can be used when overfitting."""
model = BoringModel()
model.validation_step = None
total_train_samples = len(BoringModel().train_dataloader())

trainer = Trainer(
default_root_dir=tmpdir, max_epochs=1, overfit_batches=overfit_batches, enable_model_summary=False
default_root_dir=tmp_path, max_epochs=1, overfit_batches=overfit_batches, enable_model_summary=False
)
trainer.fit(model)

Expand All @@ -45,7 +45,7 @@ def test_overfit_basic(tmpdir, overfit_batches):
)


def test_overfit_batches_raises_warning_in_case_of_sequential_sampler(tmpdir):
def test_overfit_batches_raises_warning_in_case_of_sequential_sampler(tmp_path):
class NonSequentialSampler(Sampler):
def __init__(self, data_source):
self.data_source = data_source
Expand All @@ -68,7 +68,7 @@ def val_dataloader(self):
return torch.utils.data.DataLoader(dataset, sampler=sampler)

model = TestModel()
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, overfit_batches=2)
trainer = Trainer(default_root_dir=tmp_path, max_epochs=1, overfit_batches=2)

with pytest.warns(UserWarning, match="requested to overfit but enabled train dataloader shuffling"):
trainer.fit(model)
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_pytorch/trainer/flags/test_val_check_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@pytest.mark.parametrize("max_epochs", [1, 2, 3])
@pytest.mark.parametrize("denominator", [1, 3, 4])
def test_val_check_interval(tmpdir, max_epochs, denominator):
def test_val_check_interval(tmp_path, max_epochs, denominator):
class TestModel(BoringModel):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_val_check_interval_info_message(caplog, value):

@pytest.mark.parametrize("use_infinite_dataset", [True, False])
@pytest.mark.parametrize("accumulate_grad_batches", [1, 2])
def test_validation_check_interval_exceed_data_length_correct(tmpdir, use_infinite_dataset, accumulate_grad_batches):
def test_validation_check_interval_exceed_data_length_correct(tmp_path, use_infinite_dataset, accumulate_grad_batches):
data_samples_train = 4
max_epochs = 3
max_steps = data_samples_train * max_epochs
Expand All @@ -87,7 +87,7 @@ def train_dataloader(self):

model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
limit_val_batches=1,
max_steps=max_opt_steps,
val_check_interval=3,
Expand Down
18 changes: 9 additions & 9 deletions tests/tests_pytorch/trainer/logging_/test_distributed_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def on_train_end(self):


@RunIf(skip_windows=True)
def test_all_rank_logging_ddp_cpu(tmpdir):
def test_all_rank_logging_ddp_cpu(tmp_path):
"""Check that all ranks can be logged from."""
model = TestModel()
all_rank_logger = AllRankLogger()
trainer = Trainer(
accelerator="cpu",
devices=2,
strategy="ddp_spawn",
default_root_dir=tmpdir,
default_root_dir=tmp_path,
limit_train_batches=1,
limit_val_batches=1,
max_epochs=1,
Expand All @@ -82,15 +82,15 @@ def test_all_rank_logging_ddp_cpu(tmpdir):


@RunIf(min_cuda_gpus=2)
def test_all_rank_logging_ddp_spawn(tmpdir):
def test_all_rank_logging_ddp_spawn(tmp_path):
"""Check that all ranks can be logged from."""
model = TestModel()
all_rank_logger = AllRankLogger()
trainer = Trainer(
strategy="ddp_spawn",
accelerator="gpu",
devices=2,
default_root_dir=tmpdir,
default_root_dir=tmp_path,
limit_train_batches=1,
limit_val_batches=1,
max_epochs=1,
Expand All @@ -100,7 +100,7 @@ def test_all_rank_logging_ddp_spawn(tmpdir):
trainer.fit(model)


def test_first_logger_call_in_subprocess(tmpdir):
def test_first_logger_call_in_subprocess(tmp_path):
"""Test that the Trainer does not call the logger too early.
Only when the worker processes are initialized do we have access to the rank and know which one is the main process.
Expand All @@ -121,11 +121,11 @@ def on_train_start(self, trainer, pl_module):
logger = Mock()
logger.version = "0"
logger.name = "name"
logger.save_dir = tmpdir
logger.save_dir = tmp_path

model = BoringModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
limit_train_batches=1,
limit_val_batches=1,
max_epochs=1,
Expand All @@ -135,7 +135,7 @@ def on_train_start(self, trainer, pl_module):
trainer.fit(model)


def test_logger_after_fit_predict_test_calls(tmpdir):
def test_logger_after_fit_predict_test_calls(tmp_path):
"""Make sure logger outputs are finalized after fit, prediction, and test calls."""

class BufferLogger(Logger):
Expand Down Expand Up @@ -181,7 +181,7 @@ def on_test_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") ->

model = BoringModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
limit_train_batches=1,
limit_val_batches=1,
max_epochs=1,
Expand Down

0 comments on commit 5687047

Please sign in to comment.