From b9e35e68bdd06cd3de2cb41c5677244a52089c0b Mon Sep 17 00:00:00 2001 From: Danielle Pintz Date: Fri, 15 Oct 2021 20:26:53 -0700 Subject: [PATCH 01/37] small fixes for device stats revamp --- .../logger_connector/logger_connector.py | 4 ---- tests/loggers/test_mlflow.py | 1 - .../logging_/test_train_loop_logging.py | 22 ------------------- tests/trainer/test_trainer_cli.py | 1 - tests/utilities/test_cli.py | 1 - 5 files changed, 29 deletions(-) diff --git a/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py b/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py index 21684d6831a65..d44b33867b6fb 100644 --- a/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py +++ b/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py @@ -324,10 +324,6 @@ def gpus_metrics(self) -> Dict[str, float]: .. deprecated:: v1.5 Will be removed in v1.7. """ - rank_zero_deprecation( - "The property `LoggerConnector.gpus_metrics` was deprecated in v1.5" - " and will be removed in 1.7. Use the `DeviceStatsMonitor` callback instead." - ) if self.trainer._device_type == DeviceType.GPU and self.log_gpu_memory: mem_map = memory.get_memory_profile(self.log_gpu_memory) self._gpus_metrics.update(mem_map) diff --git a/tests/loggers/test_mlflow.py b/tests/loggers/test_mlflow.py index 7bf0ae67adb23..46c85f13e29e4 100644 --- a/tests/loggers/test_mlflow.py +++ b/tests/loggers/test_mlflow.py @@ -171,7 +171,6 @@ def training_epoch_end(self, *args, **kwargs): max_epochs=1, limit_train_batches=limit_batches, limit_val_batches=limit_batches, - log_gpu_memory=True, ) trainer.fit(model) assert set(os.listdir(tmpdir / exp_id)) == {run_id, "meta.yaml"} diff --git a/tests/trainer/logging_/test_train_loop_logging.py b/tests/trainer/logging_/test_train_loop_logging.py index ee18b2905a6ec..7cd44a85b77b1 100644 --- a/tests/trainer/logging_/test_train_loop_logging.py +++ b/tests/trainer/logging_/test_train_loop_logging.py @@ -672,28 +672,6 @@ def training_step(self, batch, batch_idx): assert "val_loss" not in trainer.progress_bar_metrics -@RunIf(min_gpus=2) -@pytest.mark.parametrize("log_gpu_memory", ["all", "min_max"]) -def test_log_gpu_memory_without_logging_on_step(tmpdir, log_gpu_memory): - - model = BoringModel() - trainer = Trainer( - default_root_dir=tmpdir, - max_epochs=1, - limit_train_batches=1, - limit_val_batches=0, - log_gpu_memory=log_gpu_memory, - log_every_n_steps=1, - gpus=[1], - ) - trainer.fit(model) - if log_gpu_memory == "min_max": - assert "min_gpu_mem" in trainer.logged_metrics - assert "max_gpu_mem" in trainer.logged_metrics - else: - assert "gpu_id: 1/memory.used (MB)" in trainer.logged_metrics - - @RunIf(min_gpus=1) def test_move_metrics_to_cpu(tmpdir): class TestModel(BoringModel): diff --git a/tests/trainer/test_trainer_cli.py b/tests/trainer/test_trainer_cli.py index 6512170084a94..1edb2aead91bc 100644 --- a/tests/trainer/test_trainer_cli.py +++ b/tests/trainer/test_trainer_cli.py @@ -128,7 +128,6 @@ def _raise(): # They should not be changed by the argparse interface. "min_steps": None, "max_steps": None, - "log_gpu_memory": None, "accelerator": None, "weights_save_path": None, "resume_from_checkpoint": None, diff --git a/tests/utilities/test_cli.py b/tests/utilities/test_cli.py index c9c2078c2af8f..7385fcd31c6cb 100644 --- a/tests/utilities/test_cli.py +++ b/tests/utilities/test_cli.py @@ -134,7 +134,6 @@ def _raise(): # interface. min_steps=None, max_steps=None, - log_gpu_memory=None, distributed_backend=None, weights_save_path=None, resume_from_checkpoint=None, From 318a4f059ddb3a560204d9262fa27516d6aea5f7 Mon Sep 17 00:00:00 2001 From: Danielle Pintz Date: Fri, 15 Oct 2021 21:46:41 -0700 Subject: [PATCH 02/37] fix doc --- docs/source/common/debugging.rst | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/docs/source/common/debugging.rst b/docs/source/common/debugging.rst index 6e5a721dd092a..7c85c55fe6544 100644 --- a/docs/source/common/debugging.rst +++ b/docs/source/common/debugging.rst @@ -56,19 +56,6 @@ argument of :class:`~pytorch_lightning.trainer.trainer.Trainer`) ---------------- -Log GPU usage -------------- -Logs (to a logger) the GPU usage for each GPU on the master machine. - -(See: :paramref:`~pytorch_lightning.trainer.trainer.Trainer.log_gpu_memory` -argument of :class:`~pytorch_lightning.trainer.trainer.Trainer`) - -.. testcode:: - - trainer = Trainer(log_gpu_memory=True) - ----------------- - Make model overfit on subset of data ------------------------------------ From 8f2bf065c5e4a1dd9f4e63fea5d6685f37db28dc Mon Sep 17 00:00:00 2001 From: Danielle Pintz Date: Fri, 15 Oct 2021 21:48:05 -0700 Subject: [PATCH 03/37] fix test --- tests/deprecated_api/test_remove_1-7.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/deprecated_api/test_remove_1-7.py b/tests/deprecated_api/test_remove_1-7.py index 32d97ebf6203d..03b6fc3bc4a43 100644 --- a/tests/deprecated_api/test_remove_1-7.py +++ b/tests/deprecated_api/test_remove_1-7.py @@ -376,9 +376,6 @@ def test_v1_7_0_trainer_log_gpu_memory(tmpdir): match="Setting `log_gpu_memory` with the trainer flag is deprecated in v1.5 and will be removed" ): trainer = Trainer(log_gpu_memory="min_max") - with pytest.deprecated_call(match="The property `LoggerConnector.gpus_metrics` was deprecated in v1.5"): - lg = LoggerConnector(trainer) - _ = lg.gpus_metrics @RunIf(min_gpus=1) From 455ec0235692ef89bb4d8ac0b74faf39cb6bf372 Mon Sep 17 00:00:00 2001 From: Danielle Pintz Date: Fri, 15 Oct 2021 23:26:42 -0700 Subject: [PATCH 04/37] small fix --- tests/deprecated_api/test_remove_1-7.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/deprecated_api/test_remove_1-7.py b/tests/deprecated_api/test_remove_1-7.py index 03b6fc3bc4a43..b5dc1f6ed9932 100644 --- a/tests/deprecated_api/test_remove_1-7.py +++ b/tests/deprecated_api/test_remove_1-7.py @@ -21,7 +21,6 @@ from pytorch_lightning.callbacks.gpu_stats_monitor import GPUStatsMonitor from pytorch_lightning.callbacks.xla_stats_monitor import XLAStatsMonitor from pytorch_lightning.loggers import LoggerCollection, TestTubeLogger -from pytorch_lightning.trainer.connectors.logger_connector import LoggerConnector from tests.deprecated_api import _soft_unimport_module from tests.helpers import BoringModel from tests.helpers.datamodules import MNISTDataModule @@ -375,7 +374,7 @@ def test_v1_7_0_trainer_log_gpu_memory(tmpdir): with pytest.deprecated_call( match="Setting `log_gpu_memory` with the trainer flag is deprecated in v1.5 and will be removed" ): - trainer = Trainer(log_gpu_memory="min_max") + _ = Trainer(log_gpu_memory="min_max") @RunIf(min_gpus=1) From db042f16b40d194463d15feaeaf60d898aaac3ec Mon Sep 17 00:00:00 2001 From: Danielle Pintz Date: Sat, 16 Oct 2021 17:15:12 -0700 Subject: [PATCH 05/37] fix docs --- docs/source/common/debugging.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/source/common/debugging.rst b/docs/source/common/debugging.rst index 7c85c55fe6544..5bf5fbbbc6455 100644 --- a/docs/source/common/debugging.rst +++ b/docs/source/common/debugging.rst @@ -56,6 +56,20 @@ argument of :class:`~pytorch_lightning.trainer.trainer.Trainer`) ---------------- +Log device stats +---------------- +Monitors and logs device stats during training. + +(See: :class:`~pytorch_lightning.callbacks.device_stats_monitor.DeviceStatsMonitor`) + +.. testcode:: + + from pytorch_lightning.callbacks import DeviceStatsMonitor + + trainer = Trainer(callbacks=[DeviceStatsMonitor()]) + +---------------- + Make model overfit on subset of data ------------------------------------ From a4fb1397435428c2c4a5f5ed1df81e3cae3e9a10 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Fri, 15 Oct 2021 04:14:54 +0200 Subject: [PATCH 06/37] Fail the test when a `DeprecationWarning` is raised --- setup.cfg | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 7c430870977db..7946f2c8a814b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -21,10 +21,15 @@ python_files = test_*.py # doctest_plus = disabled addopts = - --strict + --strict-markers --doctest-modules --color=yes --disable-pytest-warnings +filterwarnings = + # error out on deprecation warnings - ensures the code and tests are kept up-to-date + error::DeprecationWarning + # TensorBoard is using NumPy deprecations: ignore them + ignore::DeprecationWarning:tensorboard.* junit_duration_report = call From 40a8ebf3ed6d40db6283035bba49d7d33e314ee4 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Fri, 15 Oct 2021 04:35:51 +0200 Subject: [PATCH 07/37] Fixes for LightningCLI --- .../connectors/logger_connector/logger_connector.py | 2 +- pytorch_lightning/trainer/trainer.py | 2 +- tests/helpers/boring_model.py | 3 --- tests/utilities/test_cli.py | 8 +++++--- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py b/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py index 21684d6831a65..88ae028ff145f 100644 --- a/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py +++ b/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py @@ -228,7 +228,7 @@ def update_train_step_metrics(self) -> None: return # TODO: remove this call in v1.7 - self._log_gpus_metrics() + # self._log_gpus_metrics() # when metrics should be logged assert not self._epoch_end_reached diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py index e6d8ccde91d71..ab7c76b0e7e6b 100644 --- a/pytorch_lightning/trainer/trainer.py +++ b/pytorch_lightning/trainer/trainer.py @@ -1415,7 +1415,7 @@ def call_hook( # 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 + output = ttp_output if output is None else output # call the ttp hook if hook_name not in ("setup", "teardown", "on_train_start") and hasattr( diff --git a/tests/helpers/boring_model.py b/tests/helpers/boring_model.py index 4036d34663a9f..d51fb44bff0d2 100644 --- a/tests/helpers/boring_model.py +++ b/tests/helpers/boring_model.py @@ -158,18 +158,15 @@ def prepare_data(self): def setup(self, stage: Optional[str] = None): if stage == "fit" or stage is None: self.random_train = Subset(self.random_full, indices=range(64)) - self.dims = self.random_train[0].shape if stage in ("fit", "validate") or stage is None: self.random_val = Subset(self.random_full, indices=range(64, 64 * 2)) if stage == "test" or stage is None: self.random_test = Subset(self.random_full, indices=range(64 * 2, 64 * 3)) - self.dims = getattr(self, "dims", self.random_test[0].shape) if stage == "predict" or stage is None: self.random_predict = Subset(self.random_full, indices=range(64 * 3, 64 * 4)) - self.dims = getattr(self, "dims", self.random_predict[0].shape) def train_dataloader(self): return DataLoader(self.random_train) diff --git a/tests/utilities/test_cli.py b/tests/utilities/test_cli.py index 025287de423ee..2d038291083b1 100644 --- a/tests/utilities/test_cli.py +++ b/tests/utilities/test_cli.py @@ -574,7 +574,7 @@ def add_arguments_to_parser(self, parser): class EarlyExitTestModel(BoringModel): def on_fit_start(self): - raise Exception("Error on fit start") + raise MisconfigurationException("Error on fit start") @pytest.mark.parametrize("logger", (False, True)) @@ -586,8 +586,10 @@ def on_fit_start(self): pytest.param({"tpu_cores": 1}, marks=RunIf(tpu=True)), ), ) -def test_cli_ddp_spawn_save_config_callback(tmpdir, logger, trainer_kwargs): - with mock.patch("sys.argv", ["any.py", "fit"]), pytest.raises(Exception, match=r"Error on fit start"): +def test_cli_distributed_save_config_callback(tmpdir, logger, trainer_kwargs): + with mock.patch("sys.argv", ["any.py", "fit"]), pytest.raises( + MisconfigurationException, match=r"Error on fit start" + ): LightningCLI( EarlyExitTestModel, trainer_defaults={ From 31cf0682cabcd159d16b7a96a2bb22fc10bc5935 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Fri, 15 Oct 2021 19:04:57 +0200 Subject: [PATCH 08/37] Fixes --- tests/profiler/test_profiler.py | 2 +- tests/trainer/test_supporters.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/profiler/test_profiler.py b/tests/profiler/test_profiler.py index 9e22c3f8ec9f7..b5a4cc60908b8 100644 --- a/tests/profiler/test_profiler.py +++ b/tests/profiler/test_profiler.py @@ -147,7 +147,7 @@ def test_simple_profiler_logs(tmpdir, caplog, simple_profiler): """Ensure that the number of printed logs is correct.""" model = BoringModel() trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=2, profiler=simple_profiler, logger=False) - with caplog.at_level(logging.INFO, logger="pytorch_lightning.profiler.profilers"): + with caplog.at_level(logging.INFO, logger="pytorch_lightning.profiler"): trainer.fit(model) trainer.test(model) diff --git a/tests/trainer/test_supporters.py b/tests/trainer/test_supporters.py index 204f3079f544b..f1c02ad0682bd 100644 --- a/tests/trainer/test_supporters.py +++ b/tests/trainer/test_supporters.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import os -from collections import Sequence +from typing import Sequence from unittest import mock import pytest From e759ce9615416a6fe2d75b76db4682a049006bc0 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Mon, 18 Oct 2021 03:11:19 +0200 Subject: [PATCH 09/37] Undo changes --- .../trainer/connectors/logger_connector/logger_connector.py | 2 +- pytorch_lightning/trainer/trainer.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py b/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py index 88ae028ff145f..21684d6831a65 100644 --- a/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py +++ b/pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py @@ -228,7 +228,7 @@ def update_train_step_metrics(self) -> None: return # TODO: remove this call in v1.7 - # self._log_gpus_metrics() + self._log_gpus_metrics() # when metrics should be logged assert not self._epoch_end_reached diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py index ab7c76b0e7e6b..e6d8ccde91d71 100644 --- a/pytorch_lightning/trainer/trainer.py +++ b/pytorch_lightning/trainer/trainer.py @@ -1415,7 +1415,7 @@ def call_hook( # 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 = ttp_output if output is None else output + 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( From ec6eca6083ebf7b1ab6f5267b607b6ca37caad34 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Mon, 18 Oct 2021 03:32:50 +0200 Subject: [PATCH 10/37] Fix one --- tests/callbacks/test_early_stopping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/callbacks/test_early_stopping.py b/tests/callbacks/test_early_stopping.py index df6a430680e32..bb7626ddb7a7a 100644 --- a/tests/callbacks/test_early_stopping.py +++ b/tests/callbacks/test_early_stopping.py @@ -184,7 +184,7 @@ def training_epoch_end(self, outputs): def test_pickling(tmpdir): - early_stopping = EarlyStopping() + early_stopping = EarlyStopping("foo") early_stopping_pickled = pickle.dumps(early_stopping) early_stopping_loaded = pickle.loads(early_stopping_pickled) From 5ee5902a9cf90c14863022f025bd739771b767f0 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 19 Oct 2021 04:25:13 +0200 Subject: [PATCH 11/37] Remove comet --- setup.cfg | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 7946f2c8a814b..3eb3cf0ac694c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,8 +28,9 @@ addopts = filterwarnings = # error out on deprecation warnings - ensures the code and tests are kept up-to-date error::DeprecationWarning - # TensorBoard is using NumPy deprecations: ignore them + # internal deprecations from dependencies ignore::DeprecationWarning:tensorboard.* + ignore::DeprecationWarning:comet_ml.* junit_duration_report = call From 27ce421f31aa85f8004252eb9b84ebcf9b464598 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 19 Oct 2021 04:32:26 +0200 Subject: [PATCH 12/37] Remove mlflow --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 3eb3cf0ac694c..39bf3facf223d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,6 +31,7 @@ filterwarnings = # internal deprecations from dependencies ignore::DeprecationWarning:tensorboard.* ignore::DeprecationWarning:comet_ml.* + ignore::DeprecationWarning:mlflow.* junit_duration_report = call From bd61df9529fa0311a0d583f5c255b23cb94567a0 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 19 Oct 2021 04:37:19 +0200 Subject: [PATCH 13/37] Only ours --- setup.cfg | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/setup.cfg b/setup.cfg index 39bf3facf223d..440ab737c308e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,11 +27,7 @@ addopts = --disable-pytest-warnings filterwarnings = # error out on deprecation warnings - ensures the code and tests are kept up-to-date - error::DeprecationWarning - # internal deprecations from dependencies - ignore::DeprecationWarning:tensorboard.* - ignore::DeprecationWarning:comet_ml.* - ignore::DeprecationWarning:mlflow.* + error::pytorch_lightning.utilities.warnings.LightningDeprecationWarning junit_duration_report = call From 86de0ba9a1e6cd0d3829990c14ef4e4f134fbc12 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 19 Oct 2021 04:57:56 +0200 Subject: [PATCH 14/37] Fix accelerator connector tests --- .../connectors/accelerator_connector.py | 5 ++- setup.cfg | 5 +++ .../test_accelerator_connector.py | 40 +++++++++++++------ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/pytorch_lightning/trainer/connectors/accelerator_connector.py b/pytorch_lightning/trainer/connectors/accelerator_connector.py index 53f95ae4c8a14..732acb774d053 100644 --- a/pytorch_lightning/trainer/connectors/accelerator_connector.py +++ b/pytorch_lightning/trainer/connectors/accelerator_connector.py @@ -301,7 +301,10 @@ def _handle_accelerator_and_distributed_backend( f" HINT: Use just `Trainer(strategy={self.strategy!r})` instead." ) - if accelerator is not None and accelerator in list(DistributedType): + deprecated_accelerators = [ + t for t in DistributedType if t not in (DistributedType.TPU_SPAWN, DistributedType.DDP_CPU) + ] + if accelerator is not None and accelerator in deprecated_accelerators: rank_zero_deprecation( f"Passing `Trainer(accelerator={accelerator!r})` has been deprecated" f" in v1.5 and will be removed in v1.7. Use `Trainer(strategy={accelerator!r})` instead." diff --git a/setup.cfg b/setup.cfg index 440ab737c308e..cec6869819769 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,6 +28,11 @@ addopts = filterwarnings = # error out on deprecation warnings - ensures the code and tests are kept up-to-date error::pytorch_lightning.utilities.warnings.LightningDeprecationWarning + # deprecated modules + ignore:'parameter_validation` is deprecated in v1.5':pytorch_lightning.utilities.warnings.LightningDeprecationWarning + ignore:'moved to `pytorch_lightning.utilities.memory` since v1.5':pytorch_lightning.utilities.warnings.LightningDeprecationWarning + ignore:'pytorch_lightning.profiler.profilers`` is deprecated in v1.4':pytorch_lightning.utilities.warnings.LightningDeprecationWarning + ignore:'moved to `pytorch_lightning.core.mixins.device_dtype_mixin` since v1.4':pytorch_lightning.utilities.warnings.LightningDeprecationWarning junit_duration_report = call diff --git a/tests/accelerators/test_accelerator_connector.py b/tests/accelerators/test_accelerator_connector.py index d39eb82cc95cb..9c5b5d09aa944 100644 --- a/tests/accelerators/test_accelerator_connector.py +++ b/tests/accelerators/test_accelerator_connector.py @@ -68,7 +68,8 @@ def test_accelerator_choice_ddp_cpu(tmpdir, num_processes: int, num_nodes: int): @mock.patch("torch.cuda.device_count", return_value=2) @mock.patch("torch.cuda.is_available", return_value=True) def test_accelerator_choice_ddp(cuda_available_mock, device_count_mock): - trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=1) + with pytest.deprecated_call(match=r"accelerator='ddp'\)` has been deprecated"): + trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=1) assert isinstance(trainer.accelerator, GPUAccelerator) assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, LightningEnvironment) @@ -78,7 +79,8 @@ def test_accelerator_choice_ddp(cuda_available_mock, device_count_mock): @mock.patch("torch.cuda.device_count", return_value=2) @mock.patch("torch.cuda.is_available", return_value=True) def test_accelerator_choice_ddp_spawn(cuda_available_mock, device_count_mock): - trainer = Trainer(fast_dev_run=True, accelerator="ddp_spawn", gpus=1) + with pytest.deprecated_call(match=r"accelerator='ddp_spawn'\)` has been deprecated"): + trainer = Trainer(fast_dev_run=True, accelerator="ddp_spawn", gpus=1) assert isinstance(trainer.accelerator, GPUAccelerator) assert isinstance(trainer.training_type_plugin, DDPSpawnPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, LightningEnvironment) @@ -311,7 +313,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, CPUAccelerator) assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, SLURMEnvironment) - assert trainer.training_type_plugin.task_idx == 0 + assert trainer.training_type_plugin.local_rank == 0 raise SystemExit() model = BoringModel() @@ -434,7 +436,7 @@ class CB(Callback): def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, CPUAccelerator) assert isinstance(trainer.training_type_plugin, DDPPlugin) - assert trainer.training_type_plugin.task_idx == 0 + assert trainer.training_type_plugin.local_rank == 0 raise SystemExit() model = BoringModel() @@ -448,10 +450,10 @@ def on_fit_start(self, trainer, pl_module): @mock.patch("torch.cuda.device_count", return_value=2) def test_ipython_incompatible_backend_error(*_): with pytest.raises(MisconfigurationException, match=r"strategy='ddp'\)`.*is not compatible"): - Trainer(accelerator="ddp", gpus=2) + Trainer(strategy="ddp", gpus=2) with pytest.raises(MisconfigurationException, match=r"strategy='ddp2'\)`.*is not compatible"): - Trainer(accelerator="ddp2", gpus=2) + Trainer(strategy="ddp2", gpus=2) @mock.patch("pytorch_lightning.utilities._IS_INTERACTIVE", return_value=True) @@ -462,10 +464,16 @@ def test_ipython_compatible_backend(*_): @pytest.mark.parametrize(["accelerator", "plugin"], [("ddp_spawn", "ddp_sharded"), (None, "ddp_sharded")]) def test_plugin_accelerator_choice(accelerator: Optional[str], plugin: str): """Ensure that when a plugin and accelerator is passed in, that the plugin takes precedent.""" - trainer = Trainer(accelerator=accelerator, plugins=plugin, num_processes=2) + if accelerator is None: + with pytest.deprecated_call(match="Passing .* `strategy` to the `plugins`"): + trainer = Trainer(accelerator=accelerator, plugins=plugin, num_processes=2) + else: + with pytest.deprecated_call(match=r"accelerator=.*\)` has been deprecated"): + trainer = Trainer(accelerator=accelerator, plugins=plugin, num_processes=2) assert isinstance(trainer.accelerator.training_type_plugin, DDPShardedPlugin) - trainer = Trainer(plugins=plugin, num_processes=2) + with pytest.deprecated_call(match="Passing .* `strategy` to the `plugins`"): + trainer = Trainer(plugins=plugin, num_processes=2) assert isinstance(trainer.accelerator.training_type_plugin, DDPShardedPlugin) @@ -485,7 +493,8 @@ def test_plugin_accelerator_choice(accelerator: Optional[str], plugin: str): def test_accelerator_choice_multi_node_gpu( mock_is_available, mock_device_count, tmpdir, accelerator: str, plugin: ParallelPlugin, gpus: int ): - trainer = Trainer(accelerator=accelerator, default_root_dir=tmpdir, num_nodes=2, gpus=gpus) + with pytest.deprecated_call(match=r"accelerator=.*\)` has been deprecated"): + trainer = Trainer(accelerator=accelerator, default_root_dir=tmpdir, num_nodes=2, gpus=gpus) assert isinstance(trainer.training_type_plugin, plugin) @@ -629,18 +638,23 @@ def test_unsupported_distrib_types_on_cpu(training_type): def test_accelerator_ddp_for_cpu(tmpdir): - trainer = Trainer(accelerator="ddp", num_processes=2) + with pytest.deprecated_call(match=r"accelerator='ddp'\)` has been deprecated"): + trainer = Trainer(accelerator="ddp", num_processes=2) assert isinstance(trainer.accelerator, CPUAccelerator) assert isinstance(trainer.training_type_plugin, DDPPlugin) def test_exception_when_strategy_used_with_distributed_backend(): - with pytest.raises(MisconfigurationException, match="but have also passed"): + with pytest.raises(MisconfigurationException, match="but have also passed"), pytest.deprecated_call( + match=r"distributed_backend='ddp_cpu'\)` has been deprecated" + ): Trainer(distributed_backend="ddp_cpu", strategy="ddp_spawn") def test_exception_when_strategy_used_with_accelerator(): - with pytest.raises(MisconfigurationException, match="but have also passed"): + with pytest.raises(MisconfigurationException, match="but have also passed"), pytest.deprecated_call( + match=r"accelerator='ddp'\)` has been deprecated" + ): Trainer(accelerator="ddp", strategy="ddp_spawn") @@ -973,7 +987,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, CPUAccelerator) assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, SLURMEnvironment) - assert trainer.training_type_plugin.task_idx == 0 + assert trainer.training_type_plugin.local_rank == 0 raise SystemExit() model = BoringModel() From f56f3b67235d254b86b7598f516653d85590f0c5 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 19 Oct 2021 05:06:56 +0200 Subject: [PATCH 15/37] Fix --- setup.cfg | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/setup.cfg b/setup.cfg index cec6869819769..5a6c6471f6b23 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,13 +26,15 @@ addopts = --color=yes --disable-pytest-warnings filterwarnings = - # error out on deprecation warnings - ensures the code and tests are kept up-to-date + # error out on our deprecation warnings - ensures the code and tests are kept up-to-date error::pytorch_lightning.utilities.warnings.LightningDeprecationWarning - # deprecated modules - ignore:'parameter_validation` is deprecated in v1.5':pytorch_lightning.utilities.warnings.LightningDeprecationWarning - ignore:'moved to `pytorch_lightning.utilities.memory` since v1.5':pytorch_lightning.utilities.warnings.LightningDeprecationWarning - ignore:'pytorch_lightning.profiler.profilers`` is deprecated in v1.4':pytorch_lightning.utilities.warnings.LightningDeprecationWarning - ignore:'moved to `pytorch_lightning.core.mixins.device_dtype_mixin` since v1.4':pytorch_lightning.utilities.warnings.LightningDeprecationWarning + # warnings from deprecated modules on import + # TODO: remove in 1.7 + ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.core.decorators + ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.core.memory + # TODO: remove in 1.6 + ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.profiler.profilers + ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.utilities.device_dtype_mixin junit_duration_report = call From d8220972a6b704e309dbf8c09e4088a443e5e48a Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 19 Oct 2021 05:24:37 +0200 Subject: [PATCH 16/37] task_idx -> local_rank --- .../test_accelerator_connector.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/accelerators/test_accelerator_connector.py b/tests/accelerators/test_accelerator_connector.py index 9c5b5d09aa944..9a69be49038c1 100644 --- a/tests/accelerators/test_accelerator_connector.py +++ b/tests/accelerators/test_accelerator_connector.py @@ -107,7 +107,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, SLURMEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -139,7 +139,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDP2Plugin) assert isinstance(trainer.training_type_plugin.cluster_environment, SLURMEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -170,7 +170,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, TorchElasticEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -201,7 +201,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDP2Plugin) assert isinstance(trainer.training_type_plugin.cluster_environment, TorchElasticEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -223,7 +223,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, TorchElasticEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -254,7 +254,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, KubeflowEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 0 - assert trainer.training_type_plugin.task_idx == 0 + assert trainer.training_type_plugin.local_rank == 0 raise SystemExit() model = BoringModel() @@ -283,7 +283,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, KubeflowEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 0 - assert trainer.training_type_plugin.task_idx == 0 + assert trainer.training_type_plugin.local_rank == 0 raise SystemExit() model = BoringModel() @@ -781,7 +781,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, SLURMEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -813,7 +813,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDP2Plugin) assert isinstance(trainer.training_type_plugin.cluster_environment, SLURMEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -844,7 +844,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, TorchElasticEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -875,7 +875,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDP2Plugin) assert isinstance(trainer.training_type_plugin.cluster_environment, TorchElasticEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -897,7 +897,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, TorchElasticEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 1 - assert trainer.training_type_plugin.task_idx == 1 + assert trainer.training_type_plugin.local_rank == 1 raise SystemExit() model = BoringModel() @@ -928,7 +928,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, KubeflowEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 0 - assert trainer.training_type_plugin.task_idx == 0 + assert trainer.training_type_plugin.local_rank == 0 raise SystemExit() model = BoringModel() @@ -957,7 +957,7 @@ def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.training_type_plugin, DDPPlugin) assert isinstance(trainer.training_type_plugin.cluster_environment, KubeflowEnvironment) assert trainer.training_type_plugin.cluster_environment.local_rank() == 0 - assert trainer.training_type_plugin.task_idx == 0 + assert trainer.training_type_plugin.local_rank == 0 raise SystemExit() model = BoringModel() From e37933813ae5b65a53ef8c0c88a3425a00cb1ada Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 9 Nov 2021 18:37:50 +0100 Subject: [PATCH 17/37] undo changge --- tests/callbacks/test_early_stopping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/callbacks/test_early_stopping.py b/tests/callbacks/test_early_stopping.py index c796f37239314..2b4fe9f05eb87 100644 --- a/tests/callbacks/test_early_stopping.py +++ b/tests/callbacks/test_early_stopping.py @@ -183,7 +183,7 @@ def training_epoch_end(self, outputs): def test_pickling(tmpdir): - early_stopping = EarlyStopping("foo") + early_stopping = EarlyStopping() early_stopping_pickled = pickle.dumps(early_stopping) early_stopping_loaded = pickle.loads(early_stopping_pickled) From 343a9c3febc91b4656c7b32cb53d619d5576714e Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 10 Nov 2021 03:51:41 +0100 Subject: [PATCH 18/37] Remove ignore after merging master --- setup.cfg | 3 --- 1 file changed, 3 deletions(-) diff --git a/setup.cfg b/setup.cfg index 00db8d3760722..4494537280e79 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,9 +34,6 @@ filterwarnings = # TODO: remove in 1.7 ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.core.decorators ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.core.memory - # TODO: remove in 1.6 - ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.profiler.profilers - ignore::pytorch_lightning.utilities.warnings.LightningDeprecationWarning:pytorch_lightning.utilities.device_dtype_mixin junit_duration_report = call From df8f512b2302e2eea064b93a48463b154de4210c Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 10 Nov 2021 04:39:34 +0100 Subject: [PATCH 19/37] Update test --- tests/plugins/test_amp_plugins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins/test_amp_plugins.py b/tests/plugins/test_amp_plugins.py index c482e8a83d7b6..8f563f0e410e2 100644 --- a/tests/plugins/test_amp_plugins.py +++ b/tests/plugins/test_amp_plugins.py @@ -266,7 +266,7 @@ def test_precision_selection_raises(monkeypatch): with mock.patch("torch.cuda.device_count", return_value=1), pytest.raises( MisconfigurationException, match="Sharded plugins are not supported with apex" ): - Trainer(amp_backend="apex", precision=16, gpus=1, accelerator="ddp_fully_sharded") + Trainer(amp_backend="apex", precision=16, gpus=1, strategy="ddp_fully_sharded") import pytorch_lightning.plugins.precision.apex_amp as apex From ae5227f6db9fc07052d1ce4ce0a735c5a373272b Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Fri, 12 Nov 2021 18:52:14 +0100 Subject: [PATCH 20/37] Merge 10470 --- pytorch_lightning/core/lightning.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/core/lightning.py b/pytorch_lightning/core/lightning.py index bf335ec8b7acc..e51eaae4a1746 100644 --- a/pytorch_lightning/core/lightning.py +++ b/pytorch_lightning/core/lightning.py @@ -115,6 +115,8 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: self._param_requires_grad_state = {} self._metric_attributes: Optional[Dict[int, str]] = None self._should_prevent_trainer_and_dataloaders_deepcopy: bool = False + # TODO: remove after the 1.6 release + self._running_torchscript = False self._register_sharded_tensor_state_dict_hooks_if_available() @@ -1908,6 +1910,8 @@ def to_torchscript( """ mode = self.training + self._running_torchscript = True + if method == "script": torchscript_module = torch.jit.script(self.eval(), **kwargs) elif method == "trace": @@ -1933,6 +1937,8 @@ def to_torchscript( with fs.open(file_path, "wb") as f: torch.jit.save(torchscript_module, f) + self._running_torchscript = False + return torchscript_module @property @@ -1942,11 +1948,12 @@ def model_size(self) -> float: Note: This property will not return correct value for Deepspeed (stage 3) and fully-sharded training. """ - rank_zero_deprecation( - "The `LightningModule.model_size` property was deprecated in v1.5 and will be removed in v1.7." - " Please use the `pytorch_lightning.utilities.memory.get_model_size_mb`.", - stacklevel=5, - ) + if not self._running_torchscript: # remove with the deprecation removal + rank_zero_deprecation( + "The `LightningModule.model_size` property was deprecated in v1.5 and will be removed in v1.7." + " Please use the `pytorch_lightning.utilities.memory.get_model_size_mb`.", + stacklevel=5, + ) return get_model_size_mb(self) def add_to_queue(self, queue: torch.multiprocessing.SimpleQueue) -> None: From aae80469e19107748c1be40db2c8d82d11b83d8c Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Mon, 15 Nov 2021 22:58:57 +0100 Subject: [PATCH 21/37] Fix tests --- tests/trainer/test_trainer.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/trainer/test_trainer.py b/tests/trainer/test_trainer.py index d2e5f771a9c40..31a8eb32792c9 100644 --- a/tests/trainer/test_trainer.py +++ b/tests/trainer/test_trainer.py @@ -903,8 +903,8 @@ def training_step(self, batch, batch_idx): model = CurrentModel() - # fit model - trainer = Trainer(default_root_dir=tmpdir, max_steps=(model.test_batch_inf + 1), terminate_on_nan=True) + with pytest.deprecated_call(match="terminate_on_nan` was deprecated in v1.5"): + trainer = Trainer(default_root_dir=tmpdir, max_steps=(model.test_batch_inf + 1), terminate_on_nan=True) with pytest.raises(ValueError, match=r".*The loss returned in `training_step` is.*"): trainer.fit(model) @@ -916,7 +916,9 @@ def training_step(self, batch, batch_idx): def test_invalid_terminate_on_nan(tmpdir): - with pytest.raises(TypeError, match="`terminate_on_nan` should be a bool"): + with pytest.raises(TypeError, match="`terminate_on_nan` should be a bool"), pytest.deprecated_call( + match="terminate_on_nan` was deprecated in v1.5" + ): Trainer(default_root_dir=tmpdir, terminate_on_nan="False") @@ -937,7 +939,9 @@ def on_after_backward(self): torch.nn.init.constant_(self.layer.bias, math.nan) model = CurrentModel() - trainer = Trainer(default_root_dir=tmpdir, max_steps=(model.test_batch_nan + 1), terminate_on_nan=True) + + with pytest.deprecated_call(match="terminate_on_nan` was deprecated in v1.5"): + trainer = Trainer(default_root_dir=tmpdir, max_steps=(model.test_batch_nan + 1), terminate_on_nan=True) with pytest.raises(ValueError, match=r".*Detected nan and/or inf values in `layer.bias`.*"): trainer.fit(model) @@ -991,11 +995,14 @@ def on_keyboard_interrupt(self, trainer, pl_module): assert not trainer.interrupted assert handle_interrupt_callback.exception is None assert handle_interrupt_callback.exc_info is None - trainer.fit(model) + with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): + trainer.fit(model) assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, KeyboardInterrupt) assert isinstance(handle_interrupt_callback.exc_info[1], KeyboardInterrupt) - with pytest.raises(MisconfigurationException): + with pytest.raises(MisconfigurationException), pytest.deprecated_call( + match="on_keyboard_interrupt` callback hook was deprecated in v1.5" + ): trainer.test(model) assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, MisconfigurationException) @@ -1218,7 +1225,8 @@ def test_trainer_config(trainer_kwargs, expected, monkeypatch): if trainer_kwargs["gpus"] is not None: monkeypatch.setattr(torch.cuda, "is_available", lambda: True) monkeypatch.setattr(torch.cuda, "device_count", lambda: trainer_kwargs["gpus"]) - trainer = Trainer(**trainer_kwargs) + with pytest.deprecated_call(match=r"accelerator='.*'\)` has been deprecated in v1.5"): + trainer = Trainer(**trainer_kwargs) assert len(expected) == 4 for k, v in expected.items(): assert getattr(trainer, k) == v, f"Failed {k}: {v}" @@ -1314,7 +1322,6 @@ def training_step(self, *args, **kwargs): trainer = Trainer( default_root_dir=tmpdir, log_every_n_steps=log_interval, - flush_logs_every_n_steps=log_interval, limit_train_batches=train_batches, limit_val_batches=0, max_steps=max_steps, @@ -1809,7 +1816,7 @@ def on_predict_start(self) -> None: @pytest.mark.parametrize( - "strategy,num_processes", [(None, 1), pytest.param("ddp_spawn", 2, marks=RunIf(skip_windows=True))] + "strategy,num_processes", [(None, 1), pytest.param("ddp_spawn", 1, marks=RunIf(skip_windows=True))] ) def test_model_in_correct_mode_during_stages(tmpdir, strategy, num_processes): model = TrainerStagesModel() @@ -1985,7 +1992,7 @@ def on_predict_start(self) -> None: "strategy,num_processes", [ (None, 1), - pytest.param("ddp_spawn", 2, marks=RunIf(skip_windows=True)), + pytest.param("ddp_spawn", 1, marks=RunIf(skip_windows=True)), ], ) def test_error_handling_all_stages(tmpdir, strategy, num_processes): From 134024279cbb4c244b8d3892848f5534415762a7 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 01:13:12 +0100 Subject: [PATCH 22/37] Remove deprecated usage for enum --- pytorch_lightning/utilities/enums.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pytorch_lightning/utilities/enums.py b/pytorch_lightning/utilities/enums.py index 18b0336b82d5f..cbb4f68bedfac 100644 --- a/pytorch_lightning/utilities/enums.py +++ b/pytorch_lightning/utilities/enums.py @@ -103,13 +103,6 @@ def supported_types() -> List[str]: class DistributedType(LightningEnum, metaclass=_OnAccessEnumMeta): """Define type of training strategy. - >>> # you can match the type with string - >>> DistributedType.DDP == 'ddp' - True - >>> # which is case invariant - >>> DistributedType.DDP2 in ('ddp2', ) - True - Deprecated since v1.6.0 and will be removed in v1.8.0. Use `_StrategyType` instead. From 167a362ff688d5f0edc2add4f61482defb4f0d70 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 01:50:06 +0100 Subject: [PATCH 23/37] Disable propagate --- pytorch_lightning/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pytorch_lightning/__init__.py b/pytorch_lightning/__init__.py index 2996b5182cd94..fe3ba60467fb0 100644 --- a/pytorch_lightning/__init__.py +++ b/pytorch_lightning/__init__.py @@ -12,7 +12,6 @@ # if root logger has handlers, propagate messages up and let root logger process them if not _root_logger.hasHandlers(): _logger.addHandler(logging.StreamHandler()) - _logger.propagate = False _PACKAGE_ROOT = os.path.dirname(__file__) _PROJECT_ROOT = os.path.dirname(_PACKAGE_ROOT) From afe98e672179a9662b7e6143affae18d8fa0a91a Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 02:14:41 +0100 Subject: [PATCH 24/37] Update accel connector tests --- .../test_accelerator_connector.py | 72 ++++++++++++------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/tests/accelerators/test_accelerator_connector.py b/tests/accelerators/test_accelerator_connector.py index e70d862b048e0..a3f2092a0f307 100644 --- a/tests/accelerators/test_accelerator_connector.py +++ b/tests/accelerators/test_accelerator_connector.py @@ -86,7 +86,6 @@ def test_accelerator_choice_ddp_spawn(cuda_available_mock, device_count_mock): assert isinstance(trainer.training_type_plugin.cluster_environment, LightningEnvironment) -@RunIf(min_gpus=2) @mock.patch.dict( os.environ, { @@ -98,8 +97,10 @@ def test_accelerator_choice_ddp_spawn(cuda_available_mock, device_count_mock): "SLURM_LOCALID": "1", }, ) +@mock.patch("torch.cuda.set_device") +@mock.patch("torch.cuda.device_count", return_value=2) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_accelerator_choice_ddp_slurm(setup_distributed_mock): +def test_accelerator_choice_ddp_slurm(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert trainer._accelerator_connector._is_slurm_managing_tasks @@ -111,13 +112,13 @@ def on_fit_start(self, trainer, pl_module): raise SystemExit() model = BoringModel() - trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=2, callbacks=[CB()]) + with pytest.deprecated_call(match=r"accelerator='ddp'\)` has been deprecated in v1.5"): + trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=2, callbacks=[CB()]) with pytest.raises(SystemExit): trainer.fit(model) -@RunIf(min_gpus=2) @mock.patch.dict( os.environ, { @@ -129,9 +130,10 @@ def on_fit_start(self, trainer, pl_module): "SLURM_LOCALID": "1", }, ) +@mock.patch("torch.cuda.set_device") @mock.patch("torch.cuda.device_count", return_value=2) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_accelerator_choice_ddp2_slurm(device_count_mock, setup_distributed_mock): +def test_accelerator_choice_ddp2_slurm(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert trainer._accelerator_connector._is_slurm_managing_tasks @@ -143,13 +145,15 @@ def on_fit_start(self, trainer, pl_module): raise SystemExit() model = BoringModel() - trainer = Trainer(fast_dev_run=True, accelerator="ddp2", gpus=2, callbacks=[CB()]) + with pytest.deprecated_call(match=r"accelerator='ddp2'\)` has been deprecated in v1.5"): + trainer = Trainer(fast_dev_run=True, accelerator="ddp2", gpus=2, callbacks=[CB()]) with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + -@RunIf(min_gpus=1) @mock.patch.dict( os.environ, { @@ -161,9 +165,10 @@ def on_fit_start(self, trainer, pl_module): "GROUP_RANK": "0", }, ) -@mock.patch("torch.cuda.device_count", return_value=2) +@mock.patch("torch.cuda.set_device") +@mock.patch("torch.cuda.device_count", return_value=1) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_accelerator_choice_ddp_te(device_count_mock, setup_distributed_mock): +def test_accelerator_choice_ddp_te(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, GPUAccelerator) @@ -174,13 +179,15 @@ def on_fit_start(self, trainer, pl_module): raise SystemExit() model = BoringModel() - trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=2, callbacks=[CB()]) + with pytest.deprecated_call(match=r"accelerator='ddp'\)` has been deprecated in v1.5"): + trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=2, callbacks=[CB()]) with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + -@RunIf(min_gpus=1) @mock.patch.dict( os.environ, { @@ -192,9 +199,10 @@ def on_fit_start(self, trainer, pl_module): "GROUP_RANK": "0", }, ) -@mock.patch("torch.cuda.device_count", return_value=2) +@mock.patch("torch.cuda.set_device") +@mock.patch("torch.cuda.device_count", return_value=1) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_accelerator_choice_ddp2_te(device_count_mock, setup_distributed_mock): +def test_accelerator_choice_ddp2_te(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, GPUAccelerator) @@ -205,11 +213,14 @@ def on_fit_start(self, trainer, pl_module): raise SystemExit() model = BoringModel() - trainer = Trainer(fast_dev_run=True, accelerator="ddp2", gpus=2, callbacks=[CB()]) + with pytest.deprecated_call(match=r"accelerator='ddp2'\)` has been deprecated in v1.5"): + trainer = Trainer(fast_dev_run=True, accelerator="ddp2", gpus=2, callbacks=[CB()]) with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + @mock.patch.dict( os.environ, {"WORLD_SIZE": "2", "LOCAL_WORLD_SIZE": "2", "RANK": "1", "LOCAL_RANK": "1", "GROUP_RANK": "0"} @@ -233,7 +244,6 @@ def on_fit_start(self, trainer, pl_module): trainer.fit(model) -@RunIf(min_gpus=1) @mock.patch.dict( os.environ, { @@ -245,9 +255,10 @@ def on_fit_start(self, trainer, pl_module): "RANK": "1", }, ) +@mock.patch("torch.cuda.set_device") @mock.patch("torch.cuda.device_count", return_value=1) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_accelerator_choice_ddp_kubeflow(device_count_mock, setup_distributed_mock): +def test_accelerator_choice_ddp_kubeflow(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, GPUAccelerator) @@ -258,11 +269,14 @@ def on_fit_start(self, trainer, pl_module): raise SystemExit() model = BoringModel() - trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=1, callbacks=[CB()]) + with pytest.deprecated_call(match=r"accelerator='ddp'\)` has been deprecated in v1.5"): + trainer = Trainer(fast_dev_run=True, accelerator="ddp", gpus=1, callbacks=[CB()]) with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + @mock.patch.dict( os.environ, @@ -793,7 +807,6 @@ def on_fit_start(self, trainer, pl_module): trainer.fit(model) -@RunIf(min_gpus=2) @mock.patch.dict( os.environ, { @@ -805,10 +818,11 @@ def on_fit_start(self, trainer, pl_module): "SLURM_LOCALID": "1", }, ) +@mock.patch("torch.cuda.set_device") @mock.patch("torch.cuda.device_count", return_value=2) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) @pytest.mark.parametrize("strategy", ["ddp2", DDP2Plugin()]) -def test_strategy_choice_ddp2_slurm(device_count_mock, setup_distributed_mock, strategy): +def test_strategy_choice_ddp2_slurm(set_device_mock, device_count_mock, setup_distributed_mock, strategy): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert trainer._accelerator_connector._is_slurm_managing_tasks @@ -825,8 +839,9 @@ def on_fit_start(self, trainer, pl_module): with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + -@RunIf(min_gpus=1) @mock.patch.dict( os.environ, { @@ -838,9 +853,10 @@ def on_fit_start(self, trainer, pl_module): "GROUP_RANK": "0", }, ) +@mock.patch("torch.cuda.set_device") @mock.patch("torch.cuda.device_count", return_value=2) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_strategy_choice_ddp_te(device_count_mock, setup_distributed_mock): +def test_strategy_choice_ddp_te(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, GPUAccelerator) @@ -856,8 +872,9 @@ def on_fit_start(self, trainer, pl_module): with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + -@RunIf(min_gpus=1) @mock.patch.dict( os.environ, { @@ -869,9 +886,10 @@ def on_fit_start(self, trainer, pl_module): "GROUP_RANK": "0", }, ) +@mock.patch("torch.cuda.set_device") @mock.patch("torch.cuda.device_count", return_value=2) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_strategy_choice_ddp2_te(device_count_mock, setup_distributed_mock): +def test_strategy_choice_ddp2_te(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, GPUAccelerator) @@ -887,6 +905,8 @@ def on_fit_start(self, trainer, pl_module): with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + @mock.patch.dict( os.environ, {"WORLD_SIZE": "2", "LOCAL_WORLD_SIZE": "2", "RANK": "1", "LOCAL_RANK": "1", "GROUP_RANK": "0"} @@ -910,7 +930,6 @@ def on_fit_start(self, trainer, pl_module): trainer.fit(model) -@RunIf(min_gpus=1) @mock.patch.dict( os.environ, { @@ -922,9 +941,10 @@ def on_fit_start(self, trainer, pl_module): "RANK": "1", }, ) +@mock.patch("torch.cuda.set_device") @mock.patch("torch.cuda.device_count", return_value=1) @mock.patch("pytorch_lightning.plugins.DDPPlugin.setup_distributed", autospec=True) -def test_strategy_choice_ddp_kubeflow(device_count_mock, setup_distributed_mock): +def test_strategy_choice_ddp_kubeflow(set_device_mock, device_count_mock, setup_distributed_mock): class CB(Callback): def on_fit_start(self, trainer, pl_module): assert isinstance(trainer.accelerator, GPUAccelerator) @@ -940,6 +960,8 @@ def on_fit_start(self, trainer, pl_module): with pytest.raises(SystemExit): trainer.fit(model) + set_device_mock.assert_called_once() + @mock.patch.dict( os.environ, From bfb18dac4cc9ae56e42b09ed0014051da765dc60 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 02:37:57 +0100 Subject: [PATCH 25/37] Fix a few --- pytorch_lightning/loggers/tensorboard.py | 1 + tests/accelerators/test_cpu.py | 2 +- tests/callbacks/test_gpu_stats_monitor.py | 22 +++++++++------- tests/callbacks/test_stochastic_weight_avg.py | 24 +++++++++++------- tests/core/test_datamodules.py | 15 +++++++---- tests/deprecated_api/test_remove_1-7.py | 22 ++++++++-------- tests/loggers/test_all.py | 25 +++++++++++++++---- 7 files changed, 72 insertions(+), 39 deletions(-) diff --git a/pytorch_lightning/loggers/tensorboard.py b/pytorch_lightning/loggers/tensorboard.py index f26fc75ac58db..ccfd82137d4fe 100644 --- a/pytorch_lightning/loggers/tensorboard.py +++ b/pytorch_lightning/loggers/tensorboard.py @@ -240,6 +240,7 @@ def log_graph(self, model: "pl.LightningModule", input_array=None): if input_array is not None: input_array = model._apply_batch_transfer_handler(input_array) + model._running_torchscript = True self.experiment.add_graph(model, input_array) else: rank_zero_warn( diff --git a/tests/accelerators/test_cpu.py b/tests/accelerators/test_cpu.py index b4d00fef3626b..41e73431495b9 100644 --- a/tests/accelerators/test_cpu.py +++ b/tests/accelerators/test_cpu.py @@ -35,7 +35,7 @@ def setup_optimizers_in_pre_dispatch(self) -> bool: return delay_dispatch model = TestModel() - trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, plugins=CustomPlugin(device=torch.device("cpu"))) + trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, strategy=CustomPlugin(device=torch.device("cpu"))) trainer.fit(model) diff --git a/tests/callbacks/test_gpu_stats_monitor.py b/tests/callbacks/test_gpu_stats_monitor.py index 5ed3f533b5588..41a0a46c98196 100644 --- a/tests/callbacks/test_gpu_stats_monitor.py +++ b/tests/callbacks/test_gpu_stats_monitor.py @@ -31,7 +31,8 @@ def test_gpu_stats_monitor(tmpdir): """Test GPU stats are logged using a logger.""" model = BoringModel() - gpu_stats = GPUStatsMonitor(intra_step_time=True) + with pytest.deprecated_call(match="GPUStatsMonitor` callback was deprecated in v1.5"): + gpu_stats = GPUStatsMonitor(intra_step_time=True) logger = CSVLogger(tmpdir) log_every_n_steps = 2 @@ -65,12 +66,13 @@ def test_gpu_stats_monitor(tmpdir): def test_gpu_stats_monitor_no_queries(tmpdir): """Test GPU logger doesn't fail if no "nvidia-smi" queries are to be performed.""" model = BoringModel() - gpu_stats = GPUStatsMonitor( - memory_utilization=False, - gpu_utilization=False, - intra_step_time=True, - inter_step_time=True, - ) + with pytest.deprecated_call(match="GPUStatsMonitor` callback was deprecated in v1.5"): + gpu_stats = GPUStatsMonitor( + memory_utilization=False, + gpu_utilization=False, + intra_step_time=True, + inter_step_time=True, + ) trainer = Trainer( default_root_dir=tmpdir, max_epochs=1, @@ -101,7 +103,8 @@ def test_gpu_stats_monitor_cpu_machine(tmpdir): def test_gpu_stats_monitor_no_logger(tmpdir): """Test GPUStatsMonitor with no logger in Trainer.""" model = BoringModel() - gpu_stats = GPUStatsMonitor() + with pytest.deprecated_call(match="GPUStatsMonitor` callback was deprecated in v1.5"): + gpu_stats = GPUStatsMonitor() trainer = Trainer(default_root_dir=tmpdir, callbacks=[gpu_stats], max_epochs=1, gpus=1, logger=False) @@ -113,7 +116,8 @@ def test_gpu_stats_monitor_no_logger(tmpdir): def test_gpu_stats_monitor_no_gpu_warning(tmpdir): """Test GPUStatsMonitor raises a warning when not training on GPU device.""" model = BoringModel() - gpu_stats = GPUStatsMonitor() + with pytest.deprecated_call(match="GPUStatsMonitor` callback was deprecated in v1.5"): + gpu_stats = GPUStatsMonitor() trainer = Trainer(default_root_dir=tmpdir, callbacks=[gpu_stats], max_steps=1, gpus=None) diff --git a/tests/callbacks/test_stochastic_weight_avg.py b/tests/callbacks/test_stochastic_weight_avg.py index 8bed31bc48ac8..978806cd1f52b 100644 --- a/tests/callbacks/test_stochastic_weight_avg.py +++ b/tests/callbacks/test_stochastic_weight_avg.py @@ -171,7 +171,8 @@ def test_swa_callback_scheduler_step(tmpdir, interval: str): def test_swa_warns(tmpdir, caplog): model = SwaTestModel(interval="step") - trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, stochastic_weight_avg=True) + swa = StochasticWeightAveraging() + trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, callbacks=swa) with caplog.at_level(level=logging.INFO), pytest.warns(UserWarning, match="SWA is currently only supported"): trainer.fit(model) assert "Swapping scheduler `StepLR` for `SWALR`" in caplog.text @@ -199,14 +200,19 @@ def configure_optimizers(self): return optimizer model = TestModel() - trainer = Trainer( - default_root_dir=tmpdir, - callbacks=StochasticWeightAveraging(swa_lrs=1e-3) if use_callbacks else None, - stochastic_weight_avg=stochastic_weight_avg, - limit_train_batches=4, - limit_val_batches=4, - max_epochs=2, - ) + kwargs = { + "default_root_dir": tmpdir, + "callbacks": StochasticWeightAveraging(swa_lrs=1e-3) if use_callbacks else None, + "stochastic_weight_avg": stochastic_weight_avg, + "limit_train_batches": 4, + "limit_val_batches": 4, + "max_epochs": 2, + } + if stochastic_weight_avg: + with pytest.deprecated_call(match=r"stochastic_weight_avg=True\)` is deprecated in v1.5"): + trainer = Trainer(**kwargs) + else: + trainer = Trainer(**kwargs) trainer.fit(model) if use_callbacks or stochastic_weight_avg: assert sum(1 for cb in trainer.callbacks if isinstance(cb, StochasticWeightAveraging)) == 1 diff --git a/tests/core/test_datamodules.py b/tests/core/test_datamodules.py index 7fe3032058e2d..d35941ac2cb15 100644 --- a/tests/core/test_datamodules.py +++ b/tests/core/test_datamodules.py @@ -442,20 +442,24 @@ def test_hyperparameters_saving(): def test_define_as_dataclass(): + class BoringDataModule(LightningDataModule): + def __init__(self, foo=None): + super().__init__() + # makes sure that no functionality is broken and the user can still manually make # super().__init__ call with parameters # also tests all the dataclass features that can be enabled without breaking anything @dataclass(init=True, repr=True, eq=True, order=True, unsafe_hash=True, frozen=False) - class BoringDataModule1(LightningDataModule): + class BoringDataModule1(BoringDataModule): batch_size: int - dims: int = 2 + foo: int = 2 def __post_init__(self): - super().__init__(dims=self.dims) + super().__init__(foo=self.foo) # asserts for the different dunder methods added by dataclass, when __init__ is implemented, i.e. # __repr__, __eq__, __lt__, __le__, etc. - assert BoringDataModule1(batch_size=64).dims == 2 + assert BoringDataModule1(batch_size=64).foo == 2 assert BoringDataModule1(batch_size=32) assert hasattr(BoringDataModule1, "__repr__") assert BoringDataModule1(batch_size=32) == BoringDataModule1(batch_size=32) @@ -477,7 +481,8 @@ def test_inconsistent_prepare_data_per_node(tmpdir): with pytest.raises(MisconfigurationException, match="Inconsistent settings found for `prepare_data_per_node`."): model = BoringModel() dm = BoringDataModule() - trainer = Trainer(prepare_data_per_node=False) + with pytest.deprecated_call(match="prepare_data_per_node` with the trainer flag is deprecated"): + trainer = Trainer(prepare_data_per_node=False) trainer.model = model trainer.datamodule = dm trainer._data_connector.prepare_data() diff --git a/tests/deprecated_api/test_remove_1-7.py b/tests/deprecated_api/test_remove_1-7.py index 4da10fb0b666a..8ca3d0e58be19 100644 --- a/tests/deprecated_api/test_remove_1-7.py +++ b/tests/deprecated_api/test_remove_1-7.py @@ -410,13 +410,6 @@ def test_v1_7_0_deprecated_max_steps_none(tmpdir): def test_v1_7_0_resume_from_checkpoint_trainer_constructor(tmpdir): - with pytest.deprecated_call(match=r"Setting `Trainer\(resume_from_checkpoint=\)` is deprecated in v1.5"): - trainer = Trainer(resume_from_checkpoint="a") - with pytest.deprecated_call( - match=r"trainer.resume_from_checkpoint` is deprecated in v1.5 and will be removed in v1.7." - ): - _ = trainer.resume_from_checkpoint - # test resume_from_checkpoint still works until v1.7 deprecation model = BoringModel() callback = OldStatefulCallback(state=111) @@ -425,14 +418,22 @@ def test_v1_7_0_resume_from_checkpoint_trainer_constructor(tmpdir): ckpt_path = trainer.checkpoint_callback.best_model_path callback = OldStatefulCallback(state=222) - trainer = Trainer(default_root_dir=tmpdir, max_steps=2, callbacks=[callback], resume_from_checkpoint=ckpt_path) + with pytest.deprecated_call(match=r"Setting `Trainer\(resume_from_checkpoint=\)` is deprecated in v1.5"): + trainer = Trainer(default_root_dir=tmpdir, max_steps=2, callbacks=[callback], resume_from_checkpoint=ckpt_path) + with pytest.deprecated_call( + match=r"trainer.resume_from_checkpoint` is deprecated in v1.5 and will be removed in v1.7." + ): + _ = trainer.resume_from_checkpoint assert trainer.checkpoint_connector.resume_checkpoint_path is None assert trainer.checkpoint_connector.resume_from_checkpoint_fit_path == ckpt_path trainer.validate(model=model, ckpt_path=ckpt_path) assert callback.state == 222 assert trainer.checkpoint_connector.resume_checkpoint_path is None assert trainer.checkpoint_connector.resume_from_checkpoint_fit_path == ckpt_path - trainer.fit(model) + with pytest.deprecated_call( + match=r"trainer.resume_from_checkpoint` is deprecated in v1.5 and will be removed in v1.7." + ): + trainer.fit(model) assert callback.state == 111 assert trainer.checkpoint_connector.resume_checkpoint_path is None assert trainer.checkpoint_connector.resume_from_checkpoint_fit_path is None @@ -445,7 +446,8 @@ def test_v1_7_0_resume_from_checkpoint_trainer_constructor(tmpdir): # test fit(ckpt_path=) precedence over Trainer(resume_from_checkpoint=) path model = BoringModel() - trainer = Trainer(resume_from_checkpoint="trainer_arg_path") + with pytest.deprecated_call(match=r"Setting `Trainer\(resume_from_checkpoint=\)` is deprecated in v1.5"): + trainer = Trainer(resume_from_checkpoint="trainer_arg_path") with pytest.raises(FileNotFoundError, match="Checkpoint at fit_arg_ckpt_path not found. Aborting training."): trainer.fit(model, ckpt_path="fit_arg_ckpt_path") diff --git a/tests/loggers/test_all.py b/tests/loggers/test_all.py index 271ffce811fe5..25050a72d610a 100644 --- a/tests/loggers/test_all.py +++ b/tests/loggers/test_all.py @@ -76,7 +76,9 @@ def test_loggers_fit_test_all(tmpdir, monkeypatch): with mock.patch("pytorch_lightning.loggers.neptune.neptune", new_callable=create_neptune_mock): _test_loggers_fit_test(tmpdir, NeptuneLogger) - with mock.patch("pytorch_lightning.loggers.test_tube.Experiment"): + with mock.patch("pytorch_lightning.loggers.test_tube.Experiment"), pytest.deprecated_call( + match="TestTubeLogger is deprecated since v1.5" + ): _test_loggers_fit_test(tmpdir, TestTubeLogger) with mock.patch("pytorch_lightning.loggers.wandb.wandb") as wandb: @@ -176,7 +178,9 @@ def test_loggers_save_dir_and_weights_save_path_all(tmpdir, monkeypatch): ): _test_loggers_save_dir_and_weights_save_path(tmpdir, MLFlowLogger) - with mock.patch("pytorch_lightning.loggers.test_tube.Experiment"): + with mock.patch("pytorch_lightning.loggers.test_tube.Experiment"), pytest.deprecated_call( + match="TestTubeLogger is deprecated since v1.5" + ): _test_loggers_save_dir_and_weights_save_path(tmpdir, TestTubeLogger) with mock.patch("pytorch_lightning.loggers.wandb.wandb"): @@ -247,7 +251,11 @@ def test_loggers_pickle_all(tmpdir, monkeypatch, logger_class): """ _patch_comet_atexit(monkeypatch) try: - _test_loggers_pickle(tmpdir, monkeypatch, logger_class) + if logger_class is TestTubeLogger: + with pytest.deprecated_call(match="TestTubeLogger is deprecated since v1.5"): + _test_loggers_pickle(tmpdir, monkeypatch, logger_class) + else: + _test_loggers_pickle(tmpdir, monkeypatch, logger_class) except (ImportError, ModuleNotFoundError): pytest.xfail(f"pickle test requires {logger_class.__class__} dependencies to be installed.") @@ -327,7 +335,12 @@ def test_logger_created_on_rank_zero_only(tmpdir, monkeypatch, logger_class): """Test that loggers get replaced by dummy loggers on global rank > 0.""" _patch_comet_atexit(monkeypatch) try: - _test_logger_created_on_rank_zero_only(tmpdir, logger_class) + + if logger_class is TestTubeLogger: + with pytest.deprecated_call(match="TestTubeLogger is deprecated since v1.5"): + _test_logger_created_on_rank_zero_only(tmpdir, logger_class) + else: + _test_logger_created_on_rank_zero_only(tmpdir, logger_class) except (ImportError, ModuleNotFoundError): pytest.xfail(f"multi-process test requires {logger_class.__class__} dependencies to be installed.") @@ -385,7 +398,9 @@ def test_logger_with_prefix_all(tmpdir, monkeypatch): logger.experiment.add_scalar.assert_called_once_with("tmp-test", 1.0, 0) # TestTube - with mock.patch("pytorch_lightning.loggers.test_tube.Experiment"): + with mock.patch("pytorch_lightning.loggers.test_tube.Experiment"), pytest.deprecated_call( + match="TestTubeLogger is deprecated since v1.5" + ): logger = _instantiate_logger(TestTubeLogger, save_dir=tmpdir, prefix=prefix) logger.log_metrics({"test": 1.0}, step=0) logger.experiment.log.assert_called_once_with({"tmp-test": 1.0}, global_step=0) From af0eb7c89673440633ed84604018b0cd4f7e6b36 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 02:53:27 +0100 Subject: [PATCH 26/37] Updatte --- tests/plugins/test_ddp_spawn_plugin.py | 7 +++-- tests/profiler/test_profiler.py | 5 +++- tests/trainer/flags/test_min_max_epochs.py | 6 ++--- tests/trainer/test_dataloaders.py | 31 +++++++--------------- tests/trainer/test_trainer.py | 5 +++- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/tests/plugins/test_ddp_spawn_plugin.py b/tests/plugins/test_ddp_spawn_plugin.py index c389cf9290c78..2bdbb303dbeb3 100644 --- a/tests/plugins/test_ddp_spawn_plugin.py +++ b/tests/plugins/test_ddp_spawn_plugin.py @@ -11,6 +11,7 @@ # 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. +import pytest import torch from torch.nn.parallel.distributed import DistributedDataParallel @@ -76,7 +77,8 @@ def test_ddp_spawn_extra_parameters(tmpdir): val_name: str = "val_acc" model = BoringCallbackDDPSpawnModel(val_name, val) dm = BoringDataModule() - trainer.fit(model, datamodule=dm) + with pytest.deprecated_call(match="add_to_queue` method was deprecated in v1.5"): + trainer.fit(model, datamodule=dm) assert trainer.callback_metrics[val_name] == torch.tensor(val) assert model.test_val == "test_val" @@ -102,7 +104,8 @@ def test_ddp_spawn_add_get_queue(tmpdir): val_name: str = "val_acc" model = BoringCallbackDDPSpawnModel(val_name, val) dm = BoringDataModule() - trainer.fit(model, datamodule=dm) + with pytest.deprecated_call(match="add_to_queue` method was deprecated in v1.5"): + trainer.fit(model, datamodule=dm) assert trainer.callback_metrics[val_name] == torch.tensor(val) assert ddp_spawn_plugin.new_test_val == "new_test_val" diff --git a/tests/profiler/test_profiler.py b/tests/profiler/test_profiler.py index 37756fcc62351..38d4e00020ee5 100644 --- a/tests/profiler/test_profiler.py +++ b/tests/profiler/test_profiler.py @@ -22,6 +22,7 @@ import torch from pytorch_lightning import Callback, Trainer +from pytorch_lightning.callbacks import StochasticWeightAveraging from pytorch_lightning.loggers.base import LoggerCollection from pytorch_lightning.loggers.tensorboard import TensorBoardLogger from pytorch_lightning.profiler import AdvancedProfiler, PassThroughProfiler, PyTorchProfiler, SimpleProfiler @@ -287,7 +288,9 @@ def test_pytorch_profiler_describe(pytorch_profiler): def test_advanced_profiler_cprofile_deepcopy(tmpdir): """Checks for pickle issue reported in #6522.""" model = BoringModel() - trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, profiler="advanced", stochastic_weight_avg=True) + trainer = Trainer( + default_root_dir=tmpdir, fast_dev_run=True, profiler="advanced", callbacks=StochasticWeightAveraging() + ) trainer.fit(model) diff --git a/tests/trainer/flags/test_min_max_epochs.py b/tests/trainer/flags/test_min_max_epochs.py index 4201c89350c21..989dde6e79360 100644 --- a/tests/trainer/flags/test_min_max_epochs.py +++ b/tests/trainer/flags/test_min_max_epochs.py @@ -7,13 +7,13 @@ @pytest.mark.parametrize( ["min_epochs", "max_epochs", "min_steps", "max_steps"], [ - (None, 3, None, None), + (None, 3, None, -1), (None, None, None, 20), (None, 3, None, 20), (None, None, 10, 20), - (1, 3, None, None), + (1, 3, None, -1), (1, None, None, 20), - (None, 3, 10, None), + (None, 3, 10, -1), ], ) def test_min_max_steps_epochs(tmpdir, min_epochs, max_epochs, min_steps, max_steps): diff --git a/tests/trainer/test_dataloaders.py b/tests/trainer/test_dataloaders.py index 1ffc957659ef0..59659806cee5f 100644 --- a/tests/trainer/test_dataloaders.py +++ b/tests/trainer/test_dataloaders.py @@ -1466,7 +1466,7 @@ def predict_dataloader(self): def test_request_dataloader(tmpdir): - """This test asserts dataloader can be modified and properly set to the trainer.""" + """This test asserts dataloader can be wrapped.""" class DataLoaderWrapper: def __init__(self, loader): @@ -1480,46 +1480,35 @@ def __iter__(self): def __next__(self): return next(self._iter) - class DataLoaderFunc: - def __init__(self, loader): - self.loader = loader - - def __call__(self): - return self.loader - class TestModel(BoringModel): def __init__(self): super().__init__() - self.on_train_dataloader_called = False self.on_train_batch_start_called = False - self.on_val_dataloader_called = False self.on_val_batch_start_called = False - def on_train_dataloader(self) -> None: - loader = self.train_dataloader() - self.train_dataloader = DataLoaderFunc(DataLoaderWrapper(loader)) - self.on_train_dataloader_called = True + def train_dataloader(self): + loader = super().train_dataloader() + return DataLoaderWrapper(loader) def on_train_batch_start(self, batch, batch_idx: int) -> None: assert isinstance(self.trainer.train_dataloader.loaders, DataLoaderWrapper) self.on_train_batch_start_called = True - def on_val_dataloader(self) -> None: - loader = self.val_dataloader() - self.val_dataloader = DataLoaderFunc(DataLoaderWrapper(loader)) - self.on_val_dataloader_called = True + def val_dataloader(self): + loader = super().val_dataloader() + return DataLoaderWrapper(loader) def on_validation_batch_start(self, batch, batch_idx: int, dataloader_idx: int) -> None: assert isinstance(self.trainer.val_dataloaders[0], DataLoaderWrapper) self.on_val_batch_start_called = True - trainer = Trainer(default_root_dir=tmpdir, limit_train_batches=2, limit_val_batches=2, max_epochs=1) + trainer = Trainer( + default_root_dir=tmpdir, limit_train_batches=2, limit_val_batches=2, limit_test_batches=2, max_epochs=1 + ) model = TestModel() trainer.fit(model) trainer.test(model) - assert model.on_train_dataloader_called assert model.on_train_batch_start_called - assert model.on_val_dataloader_called assert model.on_val_batch_start_called diff --git a/tests/trainer/test_trainer.py b/tests/trainer/test_trainer.py index d24020b3ff928..a71f4f3322b30 100644 --- a/tests/trainer/test_trainer.py +++ b/tests/trainer/test_trainer.py @@ -1225,8 +1225,11 @@ def test_trainer_config(trainer_kwargs, expected, monkeypatch): if trainer_kwargs["gpus"] is not None: monkeypatch.setattr(torch.cuda, "is_available", lambda: True) monkeypatch.setattr(torch.cuda, "device_count", lambda: trainer_kwargs["gpus"]) - with pytest.deprecated_call(match=r"accelerator='.*'\)` has been deprecated in v1.5"): + if trainer_kwargs["accelerator"] in (None, "ddp_cpu"): trainer = Trainer(**trainer_kwargs) + else: + with pytest.deprecated_call(match=r"accelerator='.*'\)` has been deprecated in v1.5"): + trainer = Trainer(**trainer_kwargs) assert len(expected) == 4 for k, v in expected.items(): assert getattr(trainer, k) == v, f"Failed {k}: {v}" From ce60e93bab78ec9844afdd7d7c68885993f8ace1 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 03:06:41 +0100 Subject: [PATCH 27/37] Try fixing IPUs --- pytorch_lightning/loggers/tensorboard.py | 1 + .../plugins/training_type/ipu.py | 20 +++++++++++-------- tests/callbacks/test_stochastic_weight_avg.py | 3 +-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pytorch_lightning/loggers/tensorboard.py b/pytorch_lightning/loggers/tensorboard.py index ccfd82137d4fe..1ceadb8658a3d 100644 --- a/pytorch_lightning/loggers/tensorboard.py +++ b/pytorch_lightning/loggers/tensorboard.py @@ -242,6 +242,7 @@ def log_graph(self, model: "pl.LightningModule", input_array=None): input_array = model._apply_batch_transfer_handler(input_array) model._running_torchscript = True self.experiment.add_graph(model, input_array) + model._running_torchscript = False else: rank_zero_warn( "Could not log computational graph since the" diff --git a/pytorch_lightning/plugins/training_type/ipu.py b/pytorch_lightning/plugins/training_type/ipu.py index 4d9f937c58467..46fb49991acae 100644 --- a/pytorch_lightning/plugins/training_type/ipu.py +++ b/pytorch_lightning/plugins/training_type/ipu.py @@ -237,21 +237,25 @@ def to_tensor(x): args = apply_to_collection(args, dtype=(int, float), function=to_tensor) return args - def training_step(self, *args, **kwargs): + def _step(self, stage: RunningStage, *args: Any, **kwargs: Any): args = self._prepare_input(args) - return self.poptorch_models[RunningStage.TRAINING](*args, **kwargs) + poptorch_model = self.poptorch_models[stage] + self.lightning_module._running_torchscript = True + compiled_model = poptorch_model(*args, **kwargs) + self.lightning_module._running_torchscript = False + return compiled_model + + def training_step(self, *args, **kwargs): + return self._step(RunningStage.TRAINING, *args, **kwargs) def validation_step(self, *args, **kwargs): - args = self._prepare_input(args) - return self.poptorch_models[RunningStage.VALIDATING](*args, **kwargs) + return self._step(RunningStage.VALIDATING, *args, **kwargs) def test_step(self, *args, **kwargs): - args = self._prepare_input(args) - return self.poptorch_models[RunningStage.TESTING](*args, **kwargs) + return self._step(RunningStage.TESTING, *args, **kwargs) def predict_step(self, *args, **kwargs): - args = self._prepare_input(args) - return self.poptorch_models[RunningStage.PREDICTING](*args, **kwargs) + return self._step(RunningStage.PREDICTING, *args, **kwargs) def teardown(self) -> None: # undo dataloader patching diff --git a/tests/callbacks/test_stochastic_weight_avg.py b/tests/callbacks/test_stochastic_weight_avg.py index 978806cd1f52b..bfad2a09f1501 100644 --- a/tests/callbacks/test_stochastic_weight_avg.py +++ b/tests/callbacks/test_stochastic_weight_avg.py @@ -171,8 +171,7 @@ def test_swa_callback_scheduler_step(tmpdir, interval: str): def test_swa_warns(tmpdir, caplog): model = SwaTestModel(interval="step") - swa = StochasticWeightAveraging() - trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, callbacks=swa) + trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, callbacks=StochasticWeightAveraging()) with caplog.at_level(level=logging.INFO), pytest.warns(UserWarning, match="SWA is currently only supported"): trainer.fit(model) assert "Swapping scheduler `StepLR` for `SWALR`" in caplog.text From 4188cc7d687dad34ff3af639851065160d8426e2 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 03:23:13 +0100 Subject: [PATCH 28/37] Fix last few --- tests/callbacks/test_lambda_function.py | 14 ++++++++++---- tests/models/test_hooks.py | 17 +++++++++++------ tests/trainer/logging_/test_logger_connector.py | 9 ++++++--- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/tests/callbacks/test_lambda_function.py b/tests/callbacks/test_lambda_function.py index f2fa040b43c78..59bfb20976665 100644 --- a/tests/callbacks/test_lambda_function.py +++ b/tests/callbacks/test_lambda_function.py @@ -13,6 +13,8 @@ # limitations under the License. from functools import partial +import pytest + from pytorch_lightning import seed_everything, Trainer from pytorch_lightning.callbacks import Callback, LambdaCallback from tests.helpers.boring_model import BoringModel @@ -46,7 +48,8 @@ def call(hook, *_, **__): limit_val_batches=1, callbacks=[LambdaCallback(**hooks_args)], ) - trainer.fit(model) + with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): + trainer.fit(model) ckpt_path = trainer.checkpoint_callback.best_model_path @@ -60,8 +63,11 @@ def call(hook, *_, **__): limit_predict_batches=1, callbacks=[LambdaCallback(**hooks_args)], ) - trainer.fit(model, ckpt_path=ckpt_path) - trainer.test(model) - trainer.predict(model) + with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): + trainer.fit(model, ckpt_path=ckpt_path) + with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): + trainer.test(model) + with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): + trainer.predict(model) assert checker == hooks diff --git a/tests/models/test_hooks.py b/tests/models/test_hooks.py index 6b34553ff313b..760ad1115ce78 100644 --- a/tests/models/test_hooks.py +++ b/tests/models/test_hooks.py @@ -487,7 +487,8 @@ def training_step(self, batch, batch_idx): dict(name="Callback.on_init_start", args=(trainer,)), dict(name="Callback.on_init_end", args=(trainer,)), ] - trainer.fit(model) + with pytest.deprecated_call(match="on_train_dataloader` is deprecated in v1.5"): + trainer.fit(model) saved_ckpt = { "callbacks": ANY, "epoch": 1, @@ -589,7 +590,8 @@ def test_trainer_model_hook_system_fit_no_val_and_resume(tmpdir): enable_model_summary=False, callbacks=[HookedCallback([])], ) - trainer.fit(model) + with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): + trainer.fit(model) best_model_path = trainer.checkpoint_callback.best_model_path # resume from checkpoint with HookedModel @@ -611,7 +613,8 @@ def test_trainer_model_hook_system_fit_no_val_and_resume(tmpdir): dict(name="Callback.on_init_start", args=(trainer,)), dict(name="Callback.on_init_end", args=(trainer,)), ] - trainer.fit(model, ckpt_path=best_model_path) + with pytest.deprecated_call(match="on_train_dataloader` is deprecated in v1.5"): + trainer.fit(model, ckpt_path=best_model_path) saved_ckpt = { "callbacks": ANY, "epoch": 2, # TODO: wrong saved epoch @@ -706,7 +709,8 @@ def test_trainer_model_hook_system_eval(tmpdir, batches, verb, noun, dataloader, dict(name="Callback.on_init_end", args=(trainer,)), ] fn = getattr(trainer, verb) - fn(model, verbose=False) + with pytest.deprecated_call(match=f"on_{dataloader}_dataloader` is deprecated in v1.5"): + fn(model, verbose=False) hooks = [ dict(name="train", args=(False,)), dict(name=f"on_{noun}_model_eval"), @@ -750,7 +754,8 @@ def test_trainer_model_hook_system_predict(tmpdir): dict(name="Callback.on_init_start", args=(trainer,)), dict(name="Callback.on_init_end", args=(trainer,)), ] - trainer.predict(model) + with pytest.deprecated_call(match="on_predict_dataloader` is deprecated in v1.5"): + trainer.predict(model) expected = [ dict(name="Callback.on_init_start", args=(trainer,)), dict(name="Callback.on_init_end", args=(trainer,)), @@ -866,7 +871,7 @@ def call(hook, fn, *args, **kwargs): limit_predict_batches=batches, enable_progress_bar=False, enable_model_summary=False, - reload_dataloaders_every_epoch=True, + reload_dataloaders_every_n_epochs=True, ) called = [] diff --git a/tests/trainer/logging_/test_logger_connector.py b/tests/trainer/logging_/test_logger_connector.py index d26245a377897..656c5ce664800 100644 --- a/tests/trainer/logging_/test_logger_connector.py +++ b/tests/trainer/logging_/test_logger_connector.py @@ -250,7 +250,8 @@ def test_fx_validator_integration(tmpdir): limit_predict_batches=1, callbacks=callback, ) - trainer.fit(model) + with pytest.deprecated_call(match="on_train_dataloader` is deprecated in v1.5"): + trainer.fit(model) not_supported.update( { @@ -262,7 +263,8 @@ def test_fx_validator_integration(tmpdir): "on_test_end": "You can't", } ) - trainer.test(model, verbose=False) + with pytest.deprecated_call(match="on_test_dataloader` is deprecated in v1.5"): + trainer.test(model, verbose=False) not_supported.update({k: "ResultCollection` is not registered yet" for k in not_supported}) not_supported.update( @@ -279,7 +281,8 @@ def test_fx_validator_integration(tmpdir): "on_predict_end": "ResultCollection` is not registered yet", } ) - trainer.predict(model) + with pytest.deprecated_call(match="on_predict_dataloader` is deprecated in v1.5"): + trainer.predict(model) @RunIf(min_gpus=2) From 96ddbb7d58071e5eaf946a3d94af24868b019b59 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 05:18:47 +0100 Subject: [PATCH 29/37] Fix --- tests/accelerators/test_accelerator_connector.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/accelerators/test_accelerator_connector.py b/tests/accelerators/test_accelerator_connector.py index a3f2092a0f307..5669c81e9ffff 100644 --- a/tests/accelerators/test_accelerator_connector.py +++ b/tests/accelerators/test_accelerator_connector.py @@ -340,26 +340,25 @@ def on_fit_start(self, trainer, pl_module): @RunIf(special=True) def test_accelerator_choice_ddp_cpu_and_plugin(tmpdir): """Test that accelerator="ddp_cpu" can work together with an instance of DDPPlugin.""" - _test_accelerator_choice_ddp_cpu_and_plugin(tmpdir, ddp_plugin_class=DDPPlugin) + _test_accelerator_choice_ddp_cpu_and_plugin(tmpdir, ddp_strategy_class=DDPPlugin) -@RunIf(special=True) def test_accelerator_choice_ddp_cpu_and_plugin_spawn(tmpdir): """Test that accelerator="ddp_cpu" can work together with an instance of DDPPSpawnPlugin.""" - _test_accelerator_choice_ddp_cpu_and_plugin(tmpdir, ddp_plugin_class=DDPSpawnPlugin) + _test_accelerator_choice_ddp_cpu_and_plugin(tmpdir, ddp_strategy_class=DDPSpawnPlugin) -def _test_accelerator_choice_ddp_cpu_and_plugin(tmpdir, ddp_plugin_class): +def _test_accelerator_choice_ddp_cpu_and_plugin(tmpdir, ddp_strategy_class): model = BoringModel() trainer = Trainer( default_root_dir=tmpdir, - plugins=[ddp_plugin_class(find_unused_parameters=True)], + strategy=ddp_strategy_class(find_unused_parameters=True), fast_dev_run=True, accelerator="ddp_cpu", num_processes=2, ) - assert isinstance(trainer.training_type_plugin, ddp_plugin_class) + assert isinstance(trainer.training_type_plugin, ddp_strategy_class) assert isinstance(trainer.accelerator, CPUAccelerator) assert trainer.training_type_plugin.num_processes == 2 assert trainer.training_type_plugin.parallel_devices == [torch.device("cpu")] * 2 From 952cd217f6327f0385155c2ef4fa3364caf4faec Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 06:45:03 +0100 Subject: [PATCH 30/37] Fix --- tests/plugins/test_ddp_plugin_with_comm_hook.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/plugins/test_ddp_plugin_with_comm_hook.py b/tests/plugins/test_ddp_plugin_with_comm_hook.py index 6497b39ffa516..efcb089487c5b 100644 --- a/tests/plugins/test_ddp_plugin_with_comm_hook.py +++ b/tests/plugins/test_ddp_plugin_with_comm_hook.py @@ -30,7 +30,7 @@ def test_ddp_fp16_compress_comm_hook(tmpdir): """Test for DDP FP16 compress hook.""" model = BoringModel() - training_type_plugin = DDPPlugin(ddp_comm_hook=default.fp16_compress_hook, sync_batchnorm=True) + training_type_plugin = DDPPlugin(ddp_comm_hook=default.fp16_compress_hook) trainer = Trainer( max_epochs=1, gpus=2, @@ -53,7 +53,6 @@ def test_ddp_sgd_comm_hook(tmpdir): training_type_plugin = DDPPlugin( ddp_comm_state=powerSGD.PowerSGDState(process_group=None), ddp_comm_hook=powerSGD.powerSGD_hook, - sync_batchnorm=True, ) trainer = Trainer( max_epochs=1, @@ -78,7 +77,6 @@ def test_ddp_fp16_compress_wrap_sgd_comm_hook(tmpdir): ddp_comm_state=powerSGD.PowerSGDState(process_group=None), ddp_comm_hook=powerSGD.powerSGD_hook, ddp_comm_wrapper=default.fp16_compress_wrapper, - sync_batchnorm=True, ) trainer = Trainer( max_epochs=1, @@ -99,7 +97,7 @@ def test_ddp_fp16_compress_wrap_sgd_comm_hook(tmpdir): def test_ddp_spawn_fp16_compress_comm_hook(tmpdir): """Test for DDP Spawn FP16 compress hook.""" model = BoringModel() - training_type_plugin = DDPSpawnPlugin(ddp_comm_hook=default.fp16_compress_hook, sync_batchnorm=True) + training_type_plugin = DDPSpawnPlugin(ddp_comm_hook=default.fp16_compress_hook) trainer = Trainer( max_epochs=1, gpus=2, @@ -125,7 +123,6 @@ def test_ddp_post_local_sgd_comm_hook(tmpdir): ), ddp_comm_hook=post_localSGD.post_localSGD_hook, model_averaging_period=4, - sync_batchnorm=True, ) trainer = Trainer( fast_dev_run=True, From 2ed3067a7506ea17da674edc0ee0f408b86bb2fb Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Tue, 16 Nov 2021 06:56:47 +0100 Subject: [PATCH 31/37] Whitespace --- tests/loggers/test_all.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/loggers/test_all.py b/tests/loggers/test_all.py index a1984a07babcf..803a13cbb11ea 100644 --- a/tests/loggers/test_all.py +++ b/tests/loggers/test_all.py @@ -335,7 +335,6 @@ def test_logger_created_on_rank_zero_only(tmpdir, monkeypatch, logger_class): """Test that loggers get replaced by dummy loggers on global rank > 0.""" _patch_comet_atexit(monkeypatch) try: - if logger_class is TestTubeLogger: with pytest.deprecated_call(match="TestTubeLogger is deprecated since v1.5"): _test_logger_created_on_rank_zero_only(tmpdir, logger_class) From f8135a5d89ee4b0f54c302e0d414e8d58458204a Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 17 Nov 2021 00:12:18 +0100 Subject: [PATCH 32/37] Update code --- pl_examples/basic_examples/mnist_datamodule.py | 4 +--- tests/callbacks/test_gpu_stats_monitor.py | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pl_examples/basic_examples/mnist_datamodule.py b/pl_examples/basic_examples/mnist_datamodule.py index 1d2371c702ce0..335a36f0a3380 100644 --- a/pl_examples/basic_examples/mnist_datamodule.py +++ b/pl_examples/basic_examples/mnist_datamodule.py @@ -81,7 +81,6 @@ def __init__( ) num_workers = 0 - self.dims = (1, 28, 28) self.data_dir = data_dir self.val_split = val_split self.num_workers = num_workers @@ -90,7 +89,6 @@ def __init__( self.batch_size = batch_size self.dataset_train = ... self.dataset_val = ... - self.test_transforms = self.default_transforms @property def num_classes(self): @@ -134,7 +132,7 @@ def val_dataloader(self): def test_dataloader(self): """MNIST test set uses the test split.""" - extra = dict(transform=self.test_transforms) if self.test_transforms else {} + extra = dict(transform=self.default_transforms) if self.default_transforms else {} dataset = MNIST(self.data_dir, train=False, download=False, **extra) loader = DataLoader( dataset, diff --git a/tests/callbacks/test_gpu_stats_monitor.py b/tests/callbacks/test_gpu_stats_monitor.py index 41a0a46c98196..d2e454dc0fc11 100644 --- a/tests/callbacks/test_gpu_stats_monitor.py +++ b/tests/callbacks/test_gpu_stats_monitor.py @@ -95,7 +95,9 @@ def test_gpu_stats_monitor_no_queries(tmpdir): @pytest.mark.skipif(torch.cuda.is_available(), reason="test requires CPU machine") def test_gpu_stats_monitor_cpu_machine(tmpdir): """Test GPUStatsMonitor on CPU machine.""" - with pytest.raises(MisconfigurationException, match="NVIDIA driver is not installed"): + with pytest.raises(MisconfigurationException, match="NVIDIA driver is not installed"), pytest.deprecated_call( + match="GPUStatsMonitor` callback was deprecated in v1.5" + ): GPUStatsMonitor() From 59a1473465d70eefc1e321de772d04cad4967526 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 17 Nov 2021 01:17:51 +0100 Subject: [PATCH 33/37] Add workaround --- pytorch_lightning/__init__.py | 5 +---- tests/conftest.py | 12 ++++++++++++ tests/utilities/test_warnings.py | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/pytorch_lightning/__init__.py b/pytorch_lightning/__init__.py index fe3ba60467fb0..c9d914573fe71 100644 --- a/pytorch_lightning/__init__.py +++ b/pytorch_lightning/__init__.py @@ -1,7 +1,6 @@ """Root package info.""" import logging -import os from pytorch_lightning.__about__ import * # noqa: F401, F403 @@ -12,9 +11,7 @@ # if root logger has handlers, propagate messages up and let root logger process them if not _root_logger.hasHandlers(): _logger.addHandler(logging.StreamHandler()) - -_PACKAGE_ROOT = os.path.dirname(__file__) -_PROJECT_ROOT = os.path.dirname(_PACKAGE_ROOT) + _logger.propagate = False from pytorch_lightning.callbacks import Callback # noqa: E402 from pytorch_lightning.core import LightningDataModule, LightningModule # noqa: E402 diff --git a/tests/conftest.py b/tests/conftest.py index 3d5548b7bd0ae..9b044e53ceeac 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -99,6 +99,18 @@ def reset_deterministic_algorithm(): torch.set_deterministic(False) +@pytest.yield_fixture +def caplog(caplog): + """Workaround for https://github.com/pytest-dev/pytest/issues/3697.""" + import logging + + lightning_logger = logging.getLogger("pytorch_lightning") + propagate = lightning_logger.propagate + lightning_logger.propagate = True + yield caplog + lightning_logger.propagate = propagate + + @pytest.fixture def tmpdir_server(tmpdir): if sys.version_info >= (3, 7): diff --git a/tests/utilities/test_warnings.py b/tests/utilities/test_warnings.py index d1222672b7595..6ef3793b5e0f3 100644 --- a/tests/utilities/test_warnings.py +++ b/tests/utilities/test_warnings.py @@ -50,3 +50,30 @@ assert "test_warnings.py:39: UserWarning: test6" in output assert "test_warnings.py:40: LightningDeprecationWarning: test7" in output + + # check that logging is properly configured + import logging + + root_logger = logging.getLogger() + lightning_logger = logging.getLogger("pytorch_lightning") + # should have a `StreamHandler` + assert lightning_logger.hasHandlers() and len(lightning_logger.handlers) == 1 + # set our own stream for testing + handler = lightning_logger.handlers[0] + assert isinstance(handler, logging.StreamHandler) + stderr = StringIO() + # necessary with `propagate = False` + lightning_logger.handlers[0].stream = stderr + + # necessary with `propagate = True` + with redirect_stderr(stderr): + # Lightning should not configure the root `logging` logger by default + logging.info("test1") + root_logger.info("test1") + # but our logger instance + lightning_logger.info("test2") + # level is set to INFO + lightning_logger.debug("test3") + + output = stderr.getvalue() + assert output == "test2\n", repr(output) From 26dfd56358be0f35226b601b068a4da19d42dea5 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 17 Nov 2021 16:28:30 +0100 Subject: [PATCH 34/37] Bad merge --- tests/conftest.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2b3382b7862a3..c5c86f780f912 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -99,18 +99,6 @@ def reset_deterministic_algorithm(): torch.set_deterministic(False) -@pytest.yield_fixture -def caplog(caplog): - """Workaround for https://github.com/pytest-dev/pytest/issues/3697.""" - import logging - - lightning_logger = logging.getLogger("pytorch_lightning") - propagate = lightning_logger.propagate - lightning_logger.propagate = True - yield caplog - lightning_logger.propagate = propagate - - @pytest.fixture def caplog(caplog): """Workaround for https://github.com/pytest-dev/pytest/issues/3697. From 459187f1a34c150f88a9cb01fe0d97419e018ae6 Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 17 Nov 2021 17:05:11 +0100 Subject: [PATCH 35/37] Update benchmarks --- benchmarks/test_basic_parity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/test_basic_parity.py b/benchmarks/test_basic_parity.py index 2144be39394cb..7d3ad6af4bac3 100644 --- a/benchmarks/test_basic_parity.py +++ b/benchmarks/test_basic_parity.py @@ -160,8 +160,8 @@ def lightning_loop(cls_model, idx, device_type: str = "cuda", num_epochs=10): max_epochs=num_epochs if idx > 0 else 1, enable_progress_bar=False, enable_model_summary=False, + enable_checkpointing=False, gpus=1 if device_type == "cuda" else 0, - checkpoint_callback=False, logger=False, replace_sampler_ddp=False, ) From 0c4fbf709cf83e84549560b698fc7e6d7a8d04ab Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 17 Nov 2021 18:36:56 +0100 Subject: [PATCH 36/37] Add hanging fix --- tests/deprecated_api/test_remove_1-7.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tests/deprecated_api/test_remove_1-7.py b/tests/deprecated_api/test_remove_1-7.py index e7639061b7697..12b2816ca09c8 100644 --- a/tests/deprecated_api/test_remove_1-7.py +++ b/tests/deprecated_api/test_remove_1-7.py @@ -15,7 +15,6 @@ from unittest import mock import pytest -import torch from pytorch_lightning import Callback, LightningDataModule, Trainer from pytorch_lightning.callbacks.gpu_stats_monitor import GPUStatsMonitor @@ -233,22 +232,16 @@ def test_v1_7_0_flush_logs_every_n_steps_trainer_constructor(tmpdir): class BoringCallbackDDPSpawnModel(BoringModel): - def __init__(self): - super().__init__() + def add_to_queue(self, queue): + ... - def add_to_queue(self, queue: torch.multiprocessing.SimpleQueue) -> None: - queue.put("test_val") - return super().add_to_queue(queue) + def get_from_queue(self, queue): + ... - def get_from_queue(self, queue: torch.multiprocessing.SimpleQueue) -> None: - self.test_val = queue.get() - return super().get_from_queue(queue) - -@RunIf(skip_windows=True, skip_49370=True) def test_v1_7_0_deprecate_add_get_queue(tmpdir): model = BoringCallbackDDPSpawnModel() - trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, num_processes=2, strategy="ddp_spawn") + trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) with pytest.deprecated_call(match=r"`LightningModule.add_to_queue` method was deprecated in v1.5"): trainer.fit(model) From 4d4dbdb981dc6eaee1faab87cb567fab0de37b0e Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Wed, 17 Nov 2021 23:12:30 +0100 Subject: [PATCH 37/37] skip --- tests/accelerators/test_accelerator_connector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/accelerators/test_accelerator_connector.py b/tests/accelerators/test_accelerator_connector.py index d005c48757330..6078a92d24f6d 100644 --- a/tests/accelerators/test_accelerator_connector.py +++ b/tests/accelerators/test_accelerator_connector.py @@ -343,7 +343,7 @@ def test_accelerator_choice_ddp_cpu_and_strategy(tmpdir): _test_accelerator_choice_ddp_cpu_and_strategy(tmpdir, ddp_strategy_class=DDPPlugin) -@RunIf(skip_windows=True) +@RunIf(skip_windows=True, skip_49370=True) def test_accelerator_choice_ddp_cpu_and_strategy_spawn(tmpdir): """Test that accelerator="ddp_cpu" can work together with an instance of DDPPSpawnPlugin.""" _test_accelerator_choice_ddp_cpu_and_strategy(tmpdir, ddp_strategy_class=DDPSpawnPlugin)