Skip to content

Commit dc44a53

Browse files
awaelchlilexierule
authored andcommitted
Add warning_cache.deprecation and set warning stacklevel [1/2] (#8005)
Co-authored-by: Carlos Mocholi <[email protected]>
1 parent b3649df commit dc44a53

File tree

10 files changed

+25
-34
lines changed

10 files changed

+25
-34
lines changed

pytorch_lightning/callbacks/model_checkpoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,10 @@ def _add_backward_monitor_support(self, trainer: 'pl.Trainer') -> None:
607607
self.save_top_k = 1
608608

609609
if deprecation_warning:
610-
warning_cache.warn(
610+
warning_cache.deprecation(
611611
"Relying on `self.log('val_loss', ...)` to set the ModelCheckpoint monitor is deprecated in v1.2"
612612
" and will be removed in v1.4. Please, create your own `mc = ModelCheckpoint(monitor='your_monitor')`"
613-
" and use it as `Trainer(callbacks=[mc])`.", DeprecationWarning
613+
" and use it as `Trainer(callbacks=[mc])`.",
614614
)
615615

616616
def _validate_monitor_key(self, trainer: 'pl.Trainer') -> None:

pytorch_lightning/core/lightning.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ def example_input_array(self, example: Any) -> None:
165165

166166
@property
167167
def datamodule(self) -> Any:
168-
rank_zero_deprecation(
169-
"The `LightningModule.datamodule` property is deprecated in v1.3 and will be removed in v1.5."
170-
" Access the datamodule through using `self.trainer.datamodule` instead."
171-
)
172168
return self._datamodule
173169

174170
@datamodule.setter

pytorch_lightning/loggers/wandb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ def __init__(
115115
)
116116

117117
if sync_step is not None:
118-
warning_cache.warn(
118+
warning_cache.deprecation(
119119
"`WandbLogger(sync_step=(True|False))` is deprecated in v1.2.1 and will be removed in v1.5."
120-
" Metrics are now logged separately and automatically synchronized.", DeprecationWarning
120+
" Metrics are now logged separately and automatically synchronized."
121121
)
122122

123123
super().__init__()

pytorch_lightning/plugins/training_type/deepspeed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from pytorch_lightning.trainer.optimizers import _get_default_scheduler_config
3131
from pytorch_lightning.utilities import AMPType
3232
from pytorch_lightning.utilities.apply_func import apply_to_collection
33-
from pytorch_lightning.utilities.distributed import rank_zero_info, rank_zero_only
33+
from pytorch_lightning.utilities.distributed import _warn, rank_zero_info, rank_zero_only
3434
from pytorch_lightning.utilities.exceptions import MisconfigurationException
3535
from pytorch_lightning.utilities.imports import _DEEPSPEED_AVAILABLE
3636

pytorch_lightning/profiler/pytorch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from torch import nn, Tensor
2424
from torch.autograd.profiler import record_function
2525

26-
from pytorch_lightning.profiler.profilers import BaseProfiler
27-
from pytorch_lightning.utilities.distributed import rank_zero_warn
26+
from pytorch_lightning.profiler import BaseProfiler
27+
from pytorch_lightning.utilities.distributed import rank_zero_deprecation, rank_zero_warn
2828
from pytorch_lightning.utilities.exceptions import MisconfigurationException
2929
from pytorch_lightning.utilities.imports import _KINETO_AVAILABLE
3030

@@ -349,9 +349,9 @@ def __deprecation_check(
349349
record_functions = set()
350350

351351
if profiled_functions is not None:
352-
rank_zero_warn(
352+
rank_zero_deprecation(
353353
"`PyTorchProfiler.profiled_functions` has been renamed to"
354-
" `record_functions` in v1.3 and will be removed in v1.5", DeprecationWarning
354+
" `record_functions` in v1.3 and will be removed in v1.5",
355355
)
356356
if not record_functions:
357357
record_functions |= set(profiled_functions)

pytorch_lightning/trainer/callback_hook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ def on_train_epoch_end(self, outputs: EPOCH_OUTPUT):
9797
"""
9898
for callback in self.callbacks:
9999
if is_param_in_hook_signature(callback.on_train_epoch_end, "outputs"):
100-
warning_cache.warn(
100+
warning_cache.deprecation(
101101
"The signature of `Callback.on_train_epoch_end` has changed in v1.3."
102102
" `outputs` parameter has been removed."
103-
" Support for the old signature will be removed in v1.5", DeprecationWarning
103+
" Support for the old signature will be removed in v1.5"
104104
)
105105
callback.on_train_epoch_end(self, self.lightning_module, outputs)
106106
else:

pytorch_lightning/trainer/training_loop.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,10 @@ def _on_train_epoch_end_hook(self, processed_epoch_output) -> None:
642642
if is_overridden(hook_name, model_ref):
643643
hook_fx = getattr(model_ref, hook_name)
644644
if is_param_in_hook_signature(hook_fx, "outputs"):
645-
self.warning_cache.warn(
645+
self.warning_cache.deprecation(
646646
"The signature of `ModelHooks.on_train_epoch_end` has changed in v1.3."
647647
" `outputs` parameter has been deprecated."
648-
" Support for the old signature will be removed in v1.5", DeprecationWarning
648+
" Support for the old signature will be removed in v1.5",
649649
)
650650
model_ref.on_train_epoch_end(processed_epoch_output)
651651
else:
@@ -944,10 +944,10 @@ def build_train_args(self, batch, batch_idx, opt_idx, hiddens):
944944
if len(self.trainer.optimizers) > 1:
945945
if self.trainer.has_arg("training_step", "optimizer_idx"):
946946
if not self.trainer.lightning_module.automatic_optimization:
947-
self.warning_cache.warn(
947+
self.warning_cache.deprecation(
948948
"`training_step` hook signature has changed in v1.3."
949949
" `optimizer_idx` argument has been removed in case of manual optimization. Support for"
950-
" the old signature will be removed in v1.5", DeprecationWarning
950+
" the old signature will be removed in v1.5",
951951
)
952952
args.append(opt_idx)
953953
elif not self.trainer.has_arg(

pytorch_lightning/utilities/device_parser.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import torch
1818

19-
from pytorch_lightning.utilities import _TPU_AVAILABLE, rank_zero_warn
19+
from pytorch_lightning.utilities import _TPU_AVAILABLE, rank_zero_deprecation
2020
from pytorch_lightning.utilities.exceptions import MisconfigurationException
2121
from pytorch_lightning.utilities.imports import _compare_version
2222

@@ -121,12 +121,11 @@ def _normalize_parse_gpu_string_input(s: Union[int, str, List[int]]) -> Union[in
121121
else:
122122
num_gpus = int(s.strip())
123123
if _compare_version("pytorch_lightning", operator.lt, "1.5"):
124-
rank_zero_warn(
124+
rank_zero_deprecation(
125125
f"Parsing of the Trainer argument gpus='{s}' (string) will change in the future."
126126
" In the current version of Lightning, this will select"
127127
f" CUDA device with index {num_gpus}, but from v1.5 it will select gpus"
128128
f" {list(range(num_gpus))} (same as gpus={s} (int)).",
129-
DeprecationWarning,
130129
)
131130
return [num_gpus]
132131
return num_gpus

pytorch_lightning/utilities/warnings.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from pytorch_lightning.utilities.distributed import rank_zero_warn
14+
from pytorch_lightning.utilities.distributed import rank_zero_deprecation, rank_zero_warn
1515

1616

1717
class WarningCache:
1818

1919
def __init__(self):
2020
self.warnings = set()
2121

22+
def clear(self):
23+
self.warnings.clear()
24+
2225
def warn(self, m, *args, **kwargs):
2326
if m not in self.warnings:
2427
self.warnings.add(m)
2528
rank_zero_warn(m, *args, **kwargs)
2629

27-
def clear(self):
28-
self.warnings.clear()
30+
def deprecation(self, m, *args, **kwargs):
31+
if m not in self.warnings:
32+
self.warnings.add(m)
33+
rank_zero_deprecation(m, *args, **kwargs)

tests/deprecated_api/test_remove_1-5.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,6 @@ def test_v1_5_0_trainer_gpus_str_parsing(*_):
362362
assert gpus == [0]
363363

364364

365-
def test_v1_5_0_datamodule_setter():
366-
model = BoringModel()
367-
datamodule = BoringDataModule()
368-
with no_deprecated_call(match="The `LightningModule.datamodule`"):
369-
model.datamodule = datamodule
370-
with pytest.deprecated_call(match="The `LightningModule.datamodule`"):
371-
_ = model.datamodule
372-
373-
374365
def test_v1_5_0_trainer_tbptt_steps(tmpdir):
375366
with pytest.deprecated_call(match="is deprecated in v1.3 and will be removed in v1.5"):
376367
_ = Trainer(truncated_bptt_steps=1)

0 commit comments

Comments
 (0)