Skip to content

Commit

Permalink
test: replacing tmpdir with tmp_path in tests_pytorch/strategies (
Browse files Browse the repository at this point in the history
  • Loading branch information
fnhirwa committed Mar 17, 2024
1 parent e57a7bf commit ac259c6
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 76 deletions.
12 changes: 6 additions & 6 deletions tests/tests_pytorch/strategies/launchers/test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_global_state_snapshot():
@pytest.mark.parametrize("trainer_fn", [TrainerFn.FITTING, "other"])
@pytest.mark.parametrize("fake_node_rank", [0, 1])
@pytest.mark.parametrize("fake_local_rank", [0, 1])
def test_collect_rank_zero_results(trainer_fn, fake_node_rank, fake_local_rank, tmpdir):
def test_collect_rank_zero_results(trainer_fn, fake_node_rank, fake_local_rank, tmp_path):
"""Tests that the spawn strategy transfers the new weights to the main process and deletes the temporary file."""
model = Mock(wraps=BoringModel(), spec=BoringModel)
fake_global_rank = 2 * fake_node_rank + fake_local_rank
Expand All @@ -104,7 +104,7 @@ def test_collect_rank_zero_results(trainer_fn, fake_node_rank, fake_local_rank,
strategy._local_rank = fake_local_rank

launcher = _MultiProcessingLauncher(strategy=strategy)
trainer = Trainer(accelerator="cpu", default_root_dir=tmpdir, strategy=strategy)
trainer = Trainer(accelerator="cpu", default_root_dir=tmp_path, strategy=strategy)

assert strategy.node_rank == fake_node_rank
assert strategy.local_rank == fake_local_rank
Expand All @@ -128,12 +128,12 @@ def test_collect_rank_zero_results(trainer_fn, fake_node_rank, fake_local_rank,


@pytest.mark.parametrize("trainer_fn", [TrainerFn.FITTING, "other"])
def test_transfer_weights(tmpdir, trainer_fn):
def test_transfer_weights(tmp_path, trainer_fn):
"""Tests that the multiprocessing launcher transfers the new weights to the main process and deletes the temporary
file."""
model = Mock(wraps=BoringModel(), spec=BoringModel)
strategy = DDPStrategy(start_method="spawn")
trainer = Trainer(accelerator="cpu", default_root_dir=tmpdir, strategy=strategy)
trainer = Trainer(accelerator="cpu", default_root_dir=tmp_path, strategy=strategy)
trainer.strategy.connect(model)
trainer.state.fn = trainer_fn # pretend we are in a particular trainer state

Expand All @@ -151,12 +151,12 @@ def test_transfer_weights(tmpdir, trainer_fn):
assert model.load_state_dict.call_count == int(spawn_output.weights_path is not None)


def test_non_strict_loading(tmpdir):
def test_non_strict_loading(tmp_path):
"""Tests that the multiprocessing launcher loads the weights back into the main process but with strict loading
disabled, not erroring for missing keys."""
model = Mock(wraps=BoringModel(), spec=BoringModel)
strategy = DDPStrategy(start_method="spawn")
trainer = Trainer(accelerator="cpu", default_root_dir=tmpdir, strategy=strategy)
trainer = Trainer(accelerator="cpu", default_root_dir=tmp_path, strategy=strategy)
trainer.strategy.connect(model)
trainer.state.fn = TrainerFn.FITTING # state dict loading only relevant for the FITTING case

Expand Down
4 changes: 2 additions & 2 deletions tests/tests_pytorch/strategies/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
],
)
@RunIf(sklearn=True)
def test_evaluate(tmpdir, trainer_kwargs):
def test_evaluate(tmp_path, trainer_kwargs):
dm = ClassifDataModule()
model = ClassificationModel()
trainer = Trainer(
default_root_dir=tmpdir, max_epochs=2, limit_train_batches=10, limit_val_batches=10, **trainer_kwargs
default_root_dir=tmp_path, max_epochs=2, limit_train_batches=10, limit_val_batches=10, **trainer_kwargs
)

trainer.fit(model, datamodule=dm)
Expand Down
8 changes: 4 additions & 4 deletions tests/tests_pytorch/strategies/test_custom_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


@pytest.mark.parametrize("restore_optimizer_and_schedulers", [True, False])
def test_strategy_lightning_restore_optimizer_and_schedulers(tmpdir, restore_optimizer_and_schedulers):
def test_strategy_lightning_restore_optimizer_and_schedulers(tmp_path, restore_optimizer_and_schedulers):
class TestStrategy(SingleDeviceStrategy):
load_optimizer_state_dict_called = False

Expand All @@ -34,14 +34,14 @@ def load_optimizer_state_dict(self, checkpoint: Mapping[str, Any]) -> None:
self.load_optimizer_state_dict_called = True

# create ckpt to resume from
checkpoint_path = os.path.join(tmpdir, "model.ckpt")
checkpoint_path = os.path.join(tmp_path, "model.ckpt")
model = BoringModel()
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True)
trainer = Trainer(default_root_dir=tmp_path, fast_dev_run=True)
trainer.fit(model)
trainer.save_checkpoint(checkpoint_path)

model = BoringModel()
strategy = TestStrategy(torch.device("cpu"))
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, strategy=strategy, accelerator="cpu")
trainer = Trainer(default_root_dir=tmp_path, fast_dev_run=True, strategy=strategy, accelerator="cpu")
trainer.fit(model, ckpt_path=checkpoint_path)
assert strategy.load_optimizer_state_dict_called == restore_optimizer_and_schedulers
50 changes: 25 additions & 25 deletions tests/tests_pytorch/strategies/test_ddp_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@


@RunIf(min_cuda_gpus=2, standalone=True, sklearn=True)
def test_multi_gpu_model_ddp_fit_only(tmpdir):
def test_multi_gpu_model_ddp_fit_only(tmp_path):
dm = ClassifDataModule()
model = ClassificationModel()
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp")
trainer = Trainer(default_root_dir=tmp_path, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp")
trainer.fit(model, datamodule=dm)


@RunIf(min_cuda_gpus=2, standalone=True, sklearn=True)
def test_multi_gpu_model_ddp_test_only(tmpdir):
def test_multi_gpu_model_ddp_test_only(tmp_path):
dm = ClassifDataModule()
model = ClassificationModel()
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp")
trainer = Trainer(default_root_dir=tmp_path, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp")
trainer.test(model, datamodule=dm)


@RunIf(min_cuda_gpus=2, standalone=True, sklearn=True)
def test_multi_gpu_model_ddp_fit_test(tmpdir):
def test_multi_gpu_model_ddp_fit_test(tmp_path):
seed_everything(4321)
dm = ClassifDataModule()
model = ClassificationModel()
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp")
trainer = Trainer(default_root_dir=tmp_path, max_epochs=1, accelerator="gpu", devices=2, strategy="ddp")
trainer.fit(model, datamodule=dm)
result = trainer.test(model, datamodule=dm)

Expand All @@ -71,7 +71,7 @@ def test_multi_gpu_model_ddp_fit_test(tmpdir):
@mock.patch("torch.cuda.set_device")
@mock.patch("lightning.pytorch.accelerators.cuda._check_cuda_matmul_precision")
@mock.patch("lightning.pytorch.accelerators.cuda._clear_cuda_memory")
def test_ddp_torch_dist_is_available_in_setup(_, __, ___, cuda_count_1, mps_count_0, tmpdir):
def test_ddp_torch_dist_is_available_in_setup(_, __, ___, cuda_count_1, mps_count_0, tmp_path):
"""Test to ensure torch distributed is available within the setup hook using ddp."""

class TestModel(BoringModel):
Expand All @@ -81,7 +81,7 @@ def setup(self, stage: str) -> None:

model = TestModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
fast_dev_run=True,
strategy=DDPStrategy(process_group_backend="gloo"),
accelerator="gpu",
Expand All @@ -93,7 +93,7 @@ def setup(self, stage: str) -> None:

@RunIf(min_cuda_gpus=2, standalone=True)
@pytest.mark.parametrize("precision", ["16-mixed", "32-true"])
def test_ddp_wrapper(tmpdir, precision):
def test_ddp_wrapper(tmp_path, precision):
"""Test parameters to ignore are carried over for DDP."""

class WeirdModule(torch.nn.Module):
Expand All @@ -119,7 +119,7 @@ def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule")

model = CustomModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
fast_dev_run=True,
precision=precision,
strategy="ddp",
Expand All @@ -133,11 +133,11 @@ def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule")


@RunIf(min_cuda_gpus=2, sklearn=True)
def test_multi_gpu_early_stop_ddp_spawn(tmpdir):
def test_multi_gpu_early_stop_ddp_spawn(tmp_path):
seed_everything(42)

trainer_options = {
"default_root_dir": tmpdir,
"default_root_dir": tmp_path,
"callbacks": [EarlyStopping(monitor="train_acc")],
"max_epochs": 50,
"limit_train_batches": 10,
Expand All @@ -153,11 +153,11 @@ def test_multi_gpu_early_stop_ddp_spawn(tmpdir):


@RunIf(min_cuda_gpus=2)
def test_multi_gpu_model_ddp_spawn(tmpdir):
def test_multi_gpu_model_ddp_spawn(tmp_path):
seed_everything(42)

trainer_options = {
"default_root_dir": tmpdir,
"default_root_dir": tmp_path,
"max_epochs": 1,
"limit_train_batches": 10,
"limit_val_batches": 10,
Expand All @@ -173,12 +173,12 @@ def test_multi_gpu_model_ddp_spawn(tmpdir):


@RunIf(min_cuda_gpus=2)
def test_ddp_all_dataloaders_passed_to_fit(tmpdir):
def test_ddp_all_dataloaders_passed_to_fit(tmp_path):
"""Make sure DDP works with dataloaders passed to fit()"""
model = BoringModel()

trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
enable_progress_bar=False,
max_epochs=1,
limit_train_batches=0.2,
Expand Down Expand Up @@ -247,11 +247,11 @@ def _configure_launcher(self):


@RunIf(skip_windows=True)
def test_ddp_spawn_add_get_queue(tmpdir):
def test_ddp_spawn_add_get_queue(tmp_path):
"""Tests get_extra_results/update_main_process_results with DDPSpawnStrategy."""
ddp_spawn_strategy = TestDDPSpawnStrategy()
trainer = Trainer(
default_root_dir=tmpdir, fast_dev_run=True, accelerator="cpu", devices=2, strategy=ddp_spawn_strategy
default_root_dir=tmp_path, fast_dev_run=True, accelerator="cpu", devices=2, strategy=ddp_spawn_strategy
)

val: float = 1.0
Expand Down Expand Up @@ -287,15 +287,15 @@ def configure_optimizers(self):

@RunIf(min_cuda_gpus=2, skip_windows=True)
@pytest.mark.parametrize("strategy", [pytest.param("ddp", marks=RunIf(standalone=True)), "ddp_spawn"])
def test_ddp_strategy_checkpoint_zero_redundancy_optimizer(tmpdir, strategy):
def test_ddp_strategy_checkpoint_zero_redundancy_optimizer(tmp_path, strategy):
"""Test to ensure that checkpoint is saved correctly when using zero redundancy optimizer."""
model = BoringZeroRedundancyOptimizerModel()
trainer = Trainer(accelerator="gpu", devices=2, strategy=strategy, max_steps=1)

trainer.fit(model)

checkpoint_path = os.path.join(tmpdir, "model.pt")
# need to broadcast because tmpdir is different on each process
checkpoint_path = os.path.join(tmp_path, "model.pt")
# need to broadcast because tmp_path is different on each process
checkpoint_path = trainer.strategy.broadcast(checkpoint_path)
trainer.save_checkpoint(checkpoint_path)
saved_model = BoringModel.load_from_checkpoint(checkpoint_path)
Expand Down Expand Up @@ -409,12 +409,12 @@ def test_ddp_with_2_gpus():

@RunIf(min_cuda_gpus=4, standalone=True)
@mock.patch("torch.distributed.barrier")
def test_ddp_barrier_non_consecutive_device_ids(barrier_mock, tmpdir):
def test_ddp_barrier_non_consecutive_device_ids(barrier_mock, tmp_path):
"""Test correct usage of barriers when device ids do not start at 0 or are not consecutive."""
model = BoringModel()
gpus = [1, 3]
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
max_steps=1,
accelerator="gpu",
devices=gpus,
Expand All @@ -427,7 +427,7 @@ def test_ddp_barrier_non_consecutive_device_ids(barrier_mock, tmpdir):


@mock.patch.dict(os.environ, {"LOCAL_RANK": "1"})
def test_incorrect_ddp_script_spawning(tmpdir):
def test_incorrect_ddp_script_spawning(tmp_path):
"""Test an error message when user accidentally instructs Lightning to spawn children processes on rank > 0."""

class WronglyImplementedEnvironment(LightningEnvironment):
Expand All @@ -438,7 +438,7 @@ def creates_processes_externally(self):

model = BoringModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
strategy="ddp",
accelerator="cpu",
devices=2,
Expand Down

0 comments on commit ac259c6

Please sign in to comment.