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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated call_hook #14869

Merged
merged 4 commits into from Sep 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions docs/source-pytorch/extensions/loops.rst
Expand Up @@ -150,12 +150,7 @@ Here is a simple example how to add a new hook:

class CustomFitLoop(FitLoop):
def advance(self):
# ... whatever code before

# pass anything you want to the hook
self.trainer.call_hook("my_new_hook", *args, **kwargs)
awaelchli marked this conversation as resolved.
Show resolved Hide resolved

# ... whatever code after
"""Put your custom logic here."""

Now simply attach the correct loop in the trainer directly:

Expand Down
2 changes: 2 additions & 0 deletions src/pytorch_lightning/CHANGELOG.md
Expand Up @@ -221,6 +221,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Removed the deprecated `Trainer.use_amp` and `LightningModule.use_amp` attributes ([#14832](https://github.com/Lightning-AI/lightning/pull/14832))

- Removed the deprecated `Trainer.call_hook` in favor of `Trainer._call_callback_hooks`, `Trainer._call_lightning_module_hook`, `Trainer._call_ttp_hook`, and `Trainer._call_accelerator_hook` ([#14869](https://github.com/Lightning-AI/lightning/pull/14869))


### Fixed

Expand Down
39 changes: 0 additions & 39 deletions src/pytorch_lightning/trainer/trainer.py
Expand Up @@ -1401,45 +1401,6 @@ def _call_teardown_hook(self) -> None:
# summarize profile results
self.profiler.describe()

def call_hook(
self, hook_name: str, *args: Any, pl_module: Optional["pl.LightningModule"] = None, **kwargs: Any
) -> Any:
r"""
.. deprecated:: v1.6
The Trainer's `call_hook` method was deprecated in v1.6 and will be removed in v1.8.
"""
rank_zero_deprecation("The Trainer's `call_hook` method was deprecated in v1.6 and will be removed in v1.8.")
pl_module = self.lightning_module or pl_module
if pl_module:
prev_fx_name = pl_module._current_fx_name
pl_module._current_fx_name = hook_name

# always profile hooks
with self.profiler.profile(hook_name):

# first call trainer hook
callback_fx = getattr(self, hook_name, None)
if callable(callback_fx):
callback_fx(*args, **kwargs)

# next call hook in lightningModule
output = None
model_fx = getattr(pl_module, hook_name, None)
if callable(model_fx):
output = model_fx(*args, **kwargs)

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

if pl_module:
# restore current_fx when nested context
pl_module._current_fx_name = prev_fx_name

return output

def _call_lightning_module_hook(
self,
hook_name: str,
Expand Down
12 changes: 0 additions & 12 deletions tests/tests_pytorch/deprecated_api/test_remove_1-8.py
Expand Up @@ -56,18 +56,6 @@ def on_init_end(self, trainer):
trainer.validate(model)


def test_v1_8_0_deprecated_call_hook():
trainer = Trainer(
max_epochs=1,
limit_val_batches=0.1,
limit_train_batches=0.2,
enable_progress_bar=False,
logger=False,
)
with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8."):
trainer.call_hook("test_hook")


def test_v1_8_0_deprecated_run_stage():
trainer = Trainer()
trainer._run_stage = Mock()
Expand Down