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

shutdown workers on failure #10463

Merged
merged 7 commits into from
Nov 15, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `CombinedLoader` and `max_size_cycle` didn't receive a `DistributedSampler` ([#10374](https://github.com/PyTorchLightning/pytorch-lightning/issues/10374))


- Fixed `shutdown workers` on failure ([#10463](https://github.com/PyTorchLightning/pytorch-lightning/issues/10463))
tchaton marked this conversation as resolved.
Show resolved Hide resolved


-


Expand Down
2 changes: 2 additions & 0 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ def _call_and_handle_interrupt(self, trainer_fn: Callable, *args: Any, **kwargs:
# reset bookkeeping
self.state.stage = None
self.on_exception(exception)
# shutdown workers
self._data_connector.teardown()
raise

def fit(
Expand Down
33 changes: 26 additions & 7 deletions tests/loops/test_loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from pl_examples.bug_report_model import RandomDataset
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.callbacks import Callback, ModelCheckpoint
from pytorch_lightning.loops import Loop, TrainingBatchLoop
from pytorch_lightning.trainer.progress import BaseProgress
from tests.helpers import BoringModel
Expand Down Expand Up @@ -912,8 +912,9 @@ def val_dataloader(self):


@RunIf(min_torch="1.8.0")
@pytest.mark.parametrize("persistent_workers", (False, True))
def test_workers_are_shutdown(tmpdir, persistent_workers):
@pytest.mark.parametrize("should_fail", [False, True])
@pytest.mark.parametrize("persistent_workers", [False, True])
def test_workers_are_shutdown(tmpdir, should_fail, persistent_workers):
tchaton marked this conversation as resolved.
Show resolved Hide resolved
# `num_workers == 1` uses `_MultiProcessingDataLoaderIter`
# `persistent_workers` makes sure `self._iterator` gets set on the `DataLoader` instance

Expand Down Expand Up @@ -941,12 +942,30 @@ def _get_iterator(self):
train_dataloader = TestDataLoader(RandomDataset(32, 64), num_workers=1, persistent_workers=persistent_workers)
val_dataloader = TestDataLoader(RandomDataset(32, 64), num_workers=1, persistent_workers=persistent_workers)

class TestCallback(Callback):
def on_train_epoch_end(self, trainer, *_):
if trainer.current_epoch == 1:
raise CustomException

max_epochs = 3

model = BoringModel()
trainer = Trainer(default_root_dir=tmpdir, limit_train_batches=2, limit_val_batches=2, max_epochs=max_epochs)
trainer.fit(model, train_dataloader, val_dataloader)
assert train_dataloader.count_shutdown_workers == (2 if persistent_workers else max_epochs)
trainer = Trainer(
default_root_dir=tmpdir,
limit_train_batches=2,
limit_val_batches=2,
max_epochs=max_epochs,
callbacks=TestCallback() if should_fail else None,
)

if should_fail:
with pytest.raises(CustomException):
trainer.fit(model, train_dataloader, val_dataloader)
else:
trainer.fit(model, train_dataloader, val_dataloader)

assert train_dataloader.count_shutdown_workers == 2 if should_fail else (2 if persistent_workers else max_epochs)
# on sanity checking end, the workers are being deleted too.
assert val_dataloader.count_shutdown_workers == (2 if persistent_workers else max_epochs + 1)
assert val_dataloader.count_shutdown_workers == 2 if persistent_workers else (3 if should_fail else max_epochs + 1)
assert train_dataloader._iterator is None
assert val_dataloader._iterator is None