Skip to content

Commit

Permalink
Avoid deprecation warning after #9901 (#9951)
Browse files Browse the repository at this point in the history
  • Loading branch information
carmocca committed Oct 16, 2021
1 parent 1f09cf2 commit e5dfdf3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions pytorch_lightning/accelerators/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def setup(self, trainer: "pl.Trainer") -> None:
return super().setup(trainer)

def on_train_start(self) -> None:
super().on_train_start()
# clear cache before training
torch.cuda.empty_cache()

Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/plugins/training_type/ipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def on_test_end(self):
def on_predict_end(self):
self._detach_models()

def on_train_batch_start(self, batch: Any, batch_idx: int) -> None:
def on_train_batch_start(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> None:
# Updates optimizer stats if LR scheduler modified the optimizer state
optimizer = self.lightning_module.trainer.optimizers[0]
self.poptorch_models[RunningStage.TRAINING].setOptimizer(optimizer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def on_predict_end(self):
"""Called when predict ends."""
pass

def on_train_batch_start(self, batch: Any, batch_idx: int) -> None:
def on_train_batch_start(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> None:
"""Called in the training loop before anything happens for that batch."""
pass

Expand Down
17 changes: 16 additions & 1 deletion pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,15 +1401,30 @@ def call_hook(
if callable(model_fx):
output = model_fx(*args, **kwargs)

# *Bad code alert*
# The `Accelerator` mostly calls the `TrainingTypePlugin` but some of those calls are deprecated.
# The following logic selectively chooses which hooks are called on each object.
# In the case of `setup` and `teardown`, the hooks on the `LightningModule` should not call the hooks of the
# same name in these objects as they are meant to be managed outside of the `LightningModule` lifecycle.
# All of this should be fixed by #8506

# call the accelerator hook
if hook_name not in ("setup", "teardown") and hasattr(self.accelerator, hook_name):
if hook_name in ("on_train_start",) and hasattr(self.accelerator, hook_name):
accelerator_hook = getattr(self.accelerator, hook_name)
accelerator_output = accelerator_hook(*args, **kwargs)
# Rely on the accelerator output if lightningModule hook returns nothing
# Required for cases such as DataParallel where we reduce the output for the user
# todo: move this data parallel logic into the data parallel plugin
output = accelerator_output if output is None else output

# call the ttp hook
if hook_name not in ("setup", "teardown", "on_train_start") and hasattr(
self.training_type_plugin, hook_name
):
ttp_hook = getattr(self.training_type_plugin, hook_name)
ttp_output = ttp_hook(*args, **kwargs)
output = ttp_output if output is None else output

if pl_module:
# restore current_fx when nested context
pl_module._current_fx_name = prev_fx_name
Expand Down
2 changes: 1 addition & 1 deletion tests/loops/test_training_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def run_training(**trainer_kwargs):
@pytest.mark.parametrize(["max_epochs", "batch_idx_"], [(2, 5), (3, 8), (4, 12)])
def test_on_train_batch_start_return_minus_one(max_epochs, batch_idx_, tmpdir):
class CurrentModel(BoringModel):
def on_train_batch_start(self, batch, batch_idx, dataloader_idx):
def on_train_batch_start(self, batch, batch_idx):
if batch_idx == batch_idx_:
return -1

Expand Down

0 comments on commit e5dfdf3

Please sign in to comment.