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

[WIP] Stricter argument hygiene for passing datamodules to trainer.fit or t… #7329

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -196,6 +196,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Deprecated


- Deprecated passing datamodule as an unnamed argument to `Trainer.fit` and `Trainer.tune` in favor of explicitly specifying `Trainer.fit/tune(model, datamodule=dm)` ([#7239](https://github.com/PyTorchLightning/pytorch-lightning/pull/7329/))


- Deprecated `LightningModule.grad_norm` in favor of `pytorch_lightning.utilities.grads.grad_norm` ([#7292](https://github.com/PyTorchLightning/pytorch-lightning/pull/7292))


Expand Down
16 changes: 15 additions & 1 deletion pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
from pytorch_lightning.trainer.training_tricks import TrainerTrainingTricksMixin
from pytorch_lightning.tuner.lr_finder import _LRFinder
from pytorch_lightning.tuner.tuning import Tuner
from pytorch_lightning.utilities import DeviceType, parsing, rank_zero_warn
from pytorch_lightning.utilities import DeviceType, parsing
from pytorch_lightning.utilities.debugging import InternalDebugger
from pytorch_lightning.utilities.distributed import rank_zero_deprecation, rank_zero_warn
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.memory import recursive_detach
from pytorch_lightning.utilities.model_helpers import is_overridden
Expand Down Expand Up @@ -440,7 +441,13 @@ def fit(
self.training = True

# if a datamodule comes in as the second arg, then fix it for the user
# deprecated in v1.3 and will be removed in v1.5
if isinstance(train_dataloader, LightningDataModule):
rank_zero_deprecation(
"Passing the datamodule without using named arguments is deprecated in v1.3"
" and will be removed in v1.5."
" Pass the datamodule explicitly to trainer.fit(..., datamodule=dm)"
)
datamodule = train_dataloader
train_dataloader = None
# If you supply a datamodule you can't supply train_dataloader or val_dataloaders
Expand Down Expand Up @@ -664,9 +671,16 @@ def tune(
self.tuning = True

# if a datamodule comes in as the second arg, then fix it for the user
# deprecated in v1.3 and will be removed in v1.5
if isinstance(train_dataloader, LightningDataModule):
rank_zero_deprecation(
"Passing the datamodule without using named arguments is deprecated in v1.3"
" and will be removed in v1.5."
" Pass the datamodule explicitly to trainer.fit(..., datamodule=dm)"
)
datamodule = train_dataloader
train_dataloader = None

# If you supply a datamodule you can't supply train_dataloader or val_dataloaders
if (train_dataloader is not None or val_dataloaders is not None) and datamodule is not None:
raise MisconfigurationException(
Expand Down
17 changes: 16 additions & 1 deletion tests/deprecated_api/test_remove_1-5.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pytorch_lightning.profiler import AdvancedProfiler, BaseProfiler, PyTorchProfiler, SimpleProfiler
from pytorch_lightning.trainer.callback_hook import warning_cache as callback_warning_cache
from tests.deprecated_api import no_deprecated_call
from tests.helpers import BoringModel, BoringDataModule
from tests.helpers import BoringDataModule, BoringModel
from tests.helpers.utils import no_warning_call


Expand Down Expand Up @@ -382,6 +382,21 @@ def test_v1_5_0_lighting_module_grad_norm(tmpdir):
model.grad_norm(2)


def test_v1_5_0_datamodule_named_argument(tmpdir):
model = BoringModel()
dm = BoringDataModule()
trainer = Trainer(
default_root_dir=tmpdir,
max_epochs=1,
checkpoint_callback=False,
logger=False,
)
with pytest.deprecated_call(match="is deprecated in v1.3 and will be removed in v1.5"):
trainer.fit(model, dm)
with pytest.deprecated_call(match="is deprecated in v1.3 and will be removed in v1.5"):
trainer.tune(model, dm)


def test_v1_5_0_datamodule_setter():
model = BoringModel()
datamodule = BoringDataModule()
Expand Down