Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Fix SaveConfigCallback with DDP spawn #12011

Merged
merged 4 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions pytorch_lightning/utilities/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ def __init__(
self.multifile = multifile

def setup(self, trainer: Trainer, pl_module: LightningModule, stage: Optional[str] = None) -> None:
# save the config in `setup` because (1) we want it to save regardless of the trainer function run
# and we want to save before processes are spawned
log_dir = trainer.log_dir # this broadcasts the directory
assert log_dir is not None
config_path = os.path.join(log_dir, self.config_filename)
Expand All @@ -437,18 +435,14 @@ def setup(self, trainer: Trainer, pl_module: LightningModule, stage: Optional[st

# save the file on rank 0
if trainer.is_global_zero:
# save only on rank zero to avoid race conditions on DDP.
# save only on rank zero to avoid race conditions.
# the `log_dir` needs to be created as we rely on the logger to do it usually
# but it hasn't logged anything at this point
fs.makedirs(log_dir, exist_ok=True)
self.parser.save(
self.config, config_path, skip_none=False, overwrite=self.overwrite, multifile=self.multifile
)

def __reduce__(self) -> Tuple[Type["SaveConfigCallback"], Tuple, Dict]:
# `ArgumentParser` is un-pickleable. Drop it
return self.__class__, (None, self.config, self.config_filename), {}


class LightningCLI:
"""Implementation of a configurable command line tool for pytorch-lightning."""
Expand Down
28 changes: 12 additions & 16 deletions tests/utilities/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@
SaveConfigCallback,
)
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.imports import _TORCHVISION_AVAILABLE
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_8, _TORCHVISION_AVAILABLE
from tests.helpers import BoringDataModule, BoringModel
from tests.helpers.runif import RunIf
from tests.helpers.utils import no_warning_call

torchvision_version = version.parse("0")
Expand Down Expand Up @@ -577,20 +576,15 @@ def on_fit_start(self):


@pytest.mark.parametrize("logger", (False, True))
@pytest.mark.parametrize(
"trainer_kwargs",
(
# dict(strategy="ddp_spawn")
# dict(strategy="ddp")
# the previous accl_conn will choose singleDeviceStrategy for both strategy=ddp/ddp_spawn
# TODO revisit this test as it never worked with DDP or DDPSpawn
dict(strategy="single_device"),
pytest.param({"tpu_cores": 1}, marks=RunIf(tpu=True)),
),
)
def test_cli_distributed_save_config_callback(tmpdir, logger, trainer_kwargs):
@pytest.mark.parametrize("strategy", ("ddp_spawn", "ddp"))
def test_cli_distributed_save_config_callback(tmpdir, logger, strategy):
if _TORCH_GREATER_EQUAL_1_8:
from torch.multiprocessing import ProcessRaisedException
else:
ProcessRaisedException = Exception

with mock.patch("sys.argv", ["any.py", "fit"]), pytest.raises(
MisconfigurationException, match=r"Error on fit start"
(MisconfigurationException, ProcessRaisedException), match=r"Error on fit start"
):
LightningCLI(
EarlyExitTestModel,
Expand All @@ -599,7 +593,9 @@ def test_cli_distributed_save_config_callback(tmpdir, logger, trainer_kwargs):
"logger": logger,
"max_steps": 1,
"max_epochs": 1,
**trainer_kwargs,
"strategy": strategy,
"accelerator": "auto",
"devices": 1,
},
)
if logger:
Expand Down