2020-12-06 13:01:43 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-03-06 17:00:05 +00:00
|
|
|
"""Test deprecated functionality which will be removed in vX.Y.Z"""
|
2020-11-17 23:29:09 +00:00
|
|
|
from argparse import ArgumentParser
|
2020-10-27 10:57:16 +00:00
|
|
|
from unittest import mock
|
2020-04-23 21:34:47 +00:00
|
|
|
|
2020-11-17 23:29:09 +00:00
|
|
|
import pytest
|
2020-06-16 03:06:17 +00:00
|
|
|
import torch
|
2020-03-06 17:00:05 +00:00
|
|
|
|
2020-12-04 11:28:53 +00:00
|
|
|
from pytorch_lightning import LightningModule, Trainer
|
2020-12-04 16:59:38 +00:00
|
|
|
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
|
2020-10-27 10:57:16 +00:00
|
|
|
from pytorch_lightning.profiler.profilers import PassThroughProfiler, SimpleProfiler
|
2020-10-23 04:29:12 +00:00
|
|
|
|
|
|
|
|
2020-12-21 23:23:33 +00:00
|
|
|
def test_v1_3_0_deprecated_arguments(tmpdir):
|
2020-11-02 05:18:20 +00:00
|
|
|
with pytest.deprecated_call(match='will no longer be supported in v1.3'):
|
2020-10-30 03:47:37 +00:00
|
|
|
callback = ModelCheckpoint()
|
|
|
|
Trainer(checkpoint_callback=callback, callbacks=[], default_root_dir=tmpdir)
|
|
|
|
|
2020-11-21 12:38:42 +00:00
|
|
|
# Deprecate prefix
|
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
2020-12-04 15:11:58 +00:00
|
|
|
ModelCheckpoint(prefix='temp')
|
|
|
|
|
|
|
|
# Deprecate auto mode
|
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
ModelCheckpoint(mode='auto')
|
|
|
|
|
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
EarlyStopping(mode='auto')
|
2020-11-21 12:38:42 +00:00
|
|
|
|
2020-12-04 11:28:53 +00:00
|
|
|
with pytest.deprecated_call(match="The setter for self.hparams in LightningModule is deprecated"):
|
|
|
|
class DeprecatedHparamsModel(LightningModule):
|
|
|
|
def __init__(self, hparams):
|
|
|
|
super().__init__()
|
|
|
|
self.hparams = hparams
|
|
|
|
DeprecatedHparamsModel({})
|
|
|
|
|
2020-10-30 03:47:37 +00:00
|
|
|
|
2020-12-21 23:23:33 +00:00
|
|
|
def test_v1_3_0_deprecated_metrics():
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import to_onehot
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
to_onehot(torch.tensor([1, 2, 3]))
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import to_categorical
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
to_categorical(torch.tensor([[0.2, 0.5], [0.9, 0.1]]))
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import get_num_classes
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
get_num_classes(pred=torch.tensor([0, 1]), target=torch.tensor([1, 1]))
|
|
|
|
|
|
|
|
x_binary = torch.tensor([0, 1, 2, 3])
|
|
|
|
y_binary = torch.tensor([0, 1, 2, 3])
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import roc
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
roc(pred=x_binary, target=y_binary)
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import _roc
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
_roc(pred=x_binary, target=y_binary)
|
|
|
|
|
|
|
|
x_multy = torch.tensor([[0.85, 0.05, 0.05, 0.05],
|
|
|
|
[0.05, 0.85, 0.05, 0.05],
|
|
|
|
[0.05, 0.05, 0.85, 0.05],
|
|
|
|
[0.05, 0.05, 0.05, 0.85]])
|
|
|
|
y_multy = torch.tensor([0, 1, 3, 2])
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import multiclass_roc
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
multiclass_roc(pred=x_multy, target=y_multy)
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import average_precision
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
average_precision(pred=x_binary, target=y_binary)
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import precision_recall_curve
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
precision_recall_curve(pred=x_binary, target=y_binary)
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.classification import multiclass_precision_recall_curve
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
multiclass_precision_recall_curve(pred=x_multy, target=y_multy)
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.reduction import reduce
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
reduce(torch.tensor([0, 1, 1, 0]), 'sum')
|
|
|
|
|
2020-12-12 14:55:11 +00:00
|
|
|
from pytorch_lightning.metrics.functional.reduction import class_reduce
|
2020-12-11 21:11:21 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
class_reduce(torch.randint(1, 10, (50,)).float(),
|
|
|
|
torch.randint(10, 20, (50,)).float(),
|
|
|
|
torch.randint(1, 100, (50,)).float())
|
|
|
|
|
|
|
|
|
2020-10-27 10:57:16 +00:00
|
|
|
# TODO: remove bool from Trainer.profiler param in v1.3.0, update profiler_connector.py
|
|
|
|
@pytest.mark.parametrize(['profiler', 'expected'], [
|
|
|
|
(True, SimpleProfiler),
|
|
|
|
(False, PassThroughProfiler),
|
|
|
|
])
|
|
|
|
def test_trainer_profiler_remove_in_v1_3_0(profiler, expected):
|
2020-12-12 14:55:11 +00:00
|
|
|
# remove bool from Trainer.profiler param in v1.3.0, update profiler_connector.py
|
2020-10-27 10:57:16 +00:00
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
trainer = Trainer(profiler=profiler)
|
|
|
|
assert isinstance(trainer.profiler, expected)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
['cli_args', 'expected_parsed_arg', 'expected_profiler'],
|
|
|
|
[
|
|
|
|
('--profiler', True, SimpleProfiler),
|
|
|
|
('--profiler True', True, SimpleProfiler),
|
|
|
|
('--profiler False', False, PassThroughProfiler),
|
|
|
|
],
|
|
|
|
)
|
2020-12-21 23:23:33 +00:00
|
|
|
def test_v1_3_0_trainer_cli_profiler(cli_args, expected_parsed_arg, expected_profiler):
|
2020-10-27 10:57:16 +00:00
|
|
|
cli_args = cli_args.split(' ')
|
|
|
|
with mock.patch("argparse._sys.argv", ["any.py"] + cli_args):
|
|
|
|
parser = ArgumentParser(add_help=False)
|
|
|
|
parser = Trainer.add_argparse_args(parent_parser=parser)
|
|
|
|
args = Trainer.parse_argparser(parser)
|
|
|
|
|
|
|
|
assert getattr(args, "profiler") == expected_parsed_arg
|
|
|
|
trainer = Trainer.from_argparse_args(args)
|
|
|
|
assert isinstance(trainer.profiler, expected_profiler)
|
2021-01-08 21:13:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_trainer_enable_pl_optimizer(tmpdir):
|
|
|
|
with pytest.deprecated_call(match='will be removed in v1.3'):
|
|
|
|
Trainer(enable_pl_optimizer=True)
|