From 032f953b018d478ba1f19603a47c3356afcb7a67 Mon Sep 17 00:00:00 2001 From: Ankita Sharma Date: Sun, 10 Apr 2022 16:26:26 +0530 Subject: [PATCH 1/9] remove deprecated xlastatsmonitor --- CHANGELOG.md | 2 +- pytorch_lightning/callbacks/__init__.py | 2 - .../callbacks/xla_stats_monitor.py | 114 ------------------ tests/callbacks/test_xla_stats_monitor.py | 70 ----------- tests/deprecated_api/test_remove_1-7.py | 22 +--- 5 files changed, 2 insertions(+), 208 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a62d54a3ea991..1a994268cd98c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,7 +75,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed the deprecated `terminate_on_nan` argument from the `Trainer` constructor ([#12553](https://github.com/PyTorchLightning/pytorch-lightning/pull/12553)) -- +- Removed deprecated `XLAStatsMonitor` callback ([#12554](https://github.com/PyTorchLightning/pytorch-lightning/pull/12554)) - diff --git a/pytorch_lightning/callbacks/__init__.py b/pytorch_lightning/callbacks/__init__.py index f47bc115ece51..35d0841644c6e 100644 --- a/pytorch_lightning/callbacks/__init__.py +++ b/pytorch_lightning/callbacks/__init__.py @@ -28,7 +28,6 @@ from pytorch_lightning.callbacks.rich_model_summary import RichModelSummary from pytorch_lightning.callbacks.stochastic_weight_avg import StochasticWeightAveraging from pytorch_lightning.callbacks.timer import Timer -from pytorch_lightning.callbacks.xla_stats_monitor import XLAStatsMonitor __all__ = [ "BackboneFinetuning", @@ -37,7 +36,6 @@ "DeviceStatsMonitor", "EarlyStopping", "GPUStatsMonitor", - "XLAStatsMonitor", "GradientAccumulationScheduler", "LambdaCallback", "LearningRateMonitor", diff --git a/pytorch_lightning/callbacks/xla_stats_monitor.py b/pytorch_lightning/callbacks/xla_stats_monitor.py index c7fe59a59d515..e69de29bb2d1d 100644 --- a/pytorch_lightning/callbacks/xla_stats_monitor.py +++ b/pytorch_lightning/callbacks/xla_stats_monitor.py @@ -1,114 +0,0 @@ -# Copyright The PyTorch Lightning team. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -""" -XLA Stats Monitor -================= - -Monitor and logs XLA stats during training. - -""" -import time - -import pytorch_lightning as pl -from pytorch_lightning.accelerators import TPUAccelerator -from pytorch_lightning.callbacks.base import Callback -from pytorch_lightning.utilities import _TPU_AVAILABLE -from pytorch_lightning.utilities.exceptions import MisconfigurationException -from pytorch_lightning.utilities.rank_zero import rank_zero_deprecation, rank_zero_info - -if _TPU_AVAILABLE: - import torch_xla.core.xla_model as xm - - -class XLAStatsMonitor(Callback): - r""" - .. deprecated:: v1.5 - The `XLAStatsMonitor` callback was deprecated in v1.5 and will be removed in v1.7. - Please use the `DeviceStatsMonitor` callback instead. - - Automatically monitors and logs XLA stats during training stage. ``XLAStatsMonitor`` is a callback and in - order to use it you need to assign a logger in the ``Trainer``. - - Args: - verbose: Set to ``True`` to print average peak and free memory, and epoch time - every epoch. - - Raises: - MisconfigurationException: - If not running on TPUs, or ``Trainer`` has no logger. - - Example:: - - >>> from pytorch_lightning import Trainer - >>> from pytorch_lightning.callbacks import XLAStatsMonitor - >>> xla_stats = XLAStatsMonitor() # doctest: +SKIP - >>> trainer = Trainer(callbacks=[xla_stats]) # doctest: +SKIP - """ - - def __init__(self, verbose: bool = True) -> None: - super().__init__() - - rank_zero_deprecation( - "The `XLAStatsMonitor` callback was deprecated in v1.5 and will be removed in v1.7." - " Please use the `DeviceStatsMonitor` callback instead." - ) - - if not _TPU_AVAILABLE: - raise MisconfigurationException("Cannot use XLAStatsMonitor with TPUs are not available") - - self._verbose = verbose - - def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: - if not trainer.loggers: - raise MisconfigurationException("Cannot use XLAStatsMonitor callback with Trainer that has no logger.") - - if not isinstance(trainer.accelerator, TPUAccelerator): - raise MisconfigurationException( - "You are using XLAStatsMonitor but are not running on TPU." - f" The accelerator is set to {trainer.accelerator.__class__.__name__}." - ) - - device = trainer.strategy.root_device - memory_info = xm.get_memory_info(device) - total_memory = trainer.strategy.reduce(memory_info["kb_total"]) * 0.001 - rank_zero_info(f"Average Total memory: {total_memory:.2f} MB") - - def on_train_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: - self._start_time = time.time() - - def on_train_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: - if not trainer.loggers: - raise MisconfigurationException("Cannot use XLAStatsMonitor callback with Trainer that has no logger.") - - device = trainer.strategy.root_device - memory_info = xm.get_memory_info(device) - epoch_time = time.time() - self._start_time - - free_memory = memory_info["kb_free"] - peak_memory = memory_info["kb_total"] - free_memory - - free_memory = trainer.strategy.reduce(free_memory) * 0.001 - peak_memory = trainer.strategy.reduce(peak_memory) * 0.001 - epoch_time = trainer.strategy.reduce(epoch_time) - - for logger in trainer.loggers: - logger.log_metrics( - {"avg. free memory (MB)": float(free_memory), "avg. peak memory (MB)": float(peak_memory)}, - step=trainer.current_epoch, - ) - - if self._verbose: - rank_zero_info(f"Average Epoch time: {epoch_time:.2f} seconds") - rank_zero_info(f"Average Peak memory: {peak_memory:.2f} MB") - rank_zero_info(f"Average Free memory: {free_memory:.2f} MB") diff --git a/tests/callbacks/test_xla_stats_monitor.py b/tests/callbacks/test_xla_stats_monitor.py index 59cc2132e3cdb..e69de29bb2d1d 100644 --- a/tests/callbacks/test_xla_stats_monitor.py +++ b/tests/callbacks/test_xla_stats_monitor.py @@ -1,70 +0,0 @@ -# Copyright The PyTorch Lightning team. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -import os - -import numpy as np -import pytest - -from pytorch_lightning import Trainer -from pytorch_lightning.callbacks import XLAStatsMonitor -from pytorch_lightning.loggers import CSVLogger -from pytorch_lightning.loggers.csv_logs import ExperimentWriter -from pytorch_lightning.utilities.exceptions import MisconfigurationException -from tests.helpers import BoringModel -from tests.helpers.runif import RunIf - - -@RunIf(tpu=True) -def test_xla_stats_monitor(tmpdir): - """Test XLA stats are logged using a logger.""" - - model = BoringModel() - xla_stats = XLAStatsMonitor() - logger = CSVLogger(tmpdir) - - trainer = Trainer( - default_root_dir=tmpdir, - max_epochs=2, - limit_train_batches=5, - accelerator="tpu", - devices=8, - callbacks=[xla_stats], - logger=logger, - ) - - trainer.fit(model) - assert trainer.state.finished, f"Training failed with {trainer.state}" - - path_csv = os.path.join(logger.log_dir, ExperimentWriter.NAME_METRICS_FILE) - met_data = np.genfromtxt(path_csv, delimiter=",", names=True, deletechars="", replace_space=" ") - - fields = ["avg. free memory (MB)", "avg. peak memory (MB)"] - - for f in fields: - assert any(f in h for h in met_data.dtype.names) - - -@RunIf(tpu=True) -def test_xla_stats_monitor_no_logger(tmpdir): - """Test XLAStatsMonitor with no logger in Trainer.""" - - model = BoringModel() - xla_stats = XLAStatsMonitor() - - trainer = Trainer( - default_root_dir=tmpdir, callbacks=[xla_stats], max_epochs=1, accelerator="tpu", devices=[1], logger=False - ) - - with pytest.raises(MisconfigurationException, match="Trainer that has no logger."): - trainer.fit(model) diff --git a/tests/deprecated_api/test_remove_1-7.py b/tests/deprecated_api/test_remove_1-7.py index 8e575224680e8..55c866a8aa9e4 100644 --- a/tests/deprecated_api/test_remove_1-7.py +++ b/tests/deprecated_api/test_remove_1-7.py @@ -25,7 +25,6 @@ from pytorch_lightning.callbacks.gpu_stats_monitor import GPUStatsMonitor from pytorch_lightning.callbacks.lr_monitor import LearningRateMonitor from pytorch_lightning.callbacks.progress import ProgressBar -from pytorch_lightning.callbacks.xla_stats_monitor import XLAStatsMonitor from pytorch_lightning.loggers import LoggerCollection, TestTubeLogger from pytorch_lightning.overrides.distributed import IndexBatchSamplerWrapper from pytorch_lightning.plugins.environments import ( @@ -355,13 +354,7 @@ def test_v1_7_0_deprecate_gpu_stats_monitor(tmpdir): with pytest.deprecated_call(match="The `GPUStatsMonitor` callback was deprecated in v1.5"): _ = GPUStatsMonitor() - -def test_v1_7_0_deprecate_xla_stats_monitor(monkeypatch): - monkeypatch.setattr(pytorch_lightning.callbacks.xla_stats_monitor, "_TPU_AVAILABLE", True) - with pytest.deprecated_call(match="The `XLAStatsMonitor` callback was deprecated in v1.5"): - _ = XLAStatsMonitor() - - + def test_v1_7_0_progress_bar(): with pytest.deprecated_call(match="has been deprecated in v1.5 and will be removed in v1.7."): _ = ProgressBar() @@ -480,16 +473,3 @@ def post_dispatch(self, trainer): with pytest.deprecated_call(match=escape("`CustomPlugin.post_dispatch()` has been deprecated in v1.6")): CustomPlugin(torch.device("cpu")) - -def test_xla_stats_monitor_tpu_not_used(monkeypatch): - monkeypatch.setattr(pytorch_lightning.callbacks.xla_stats_monitor, "_TPU_AVAILABLE", True) - with pytest.deprecated_call(match="The `XLAStatsMonitor` callback was deprecated in v1.5"): - xla_stats = XLAStatsMonitor() - - trainer = Trainer(accelerator="cpu", callbacks=[xla_stats]) - model = BoringModel() - with pytest.raises( - MisconfigurationException, - match="You are using XLAStatsMonitor but are not running on TPU. The accelerator is set to CPUAccelerator.", - ): - trainer.fit(model) From b0841ab17b218ca45e91170c5c29ba0e70a8c5c4 Mon Sep 17 00:00:00 2001 From: Ankita Sharma Date: Sun, 10 Apr 2022 16:27:00 +0530 Subject: [PATCH 2/9] remove deprecated xlastatsmonitor --- pytorch_lightning/callbacks/xla_stats_monitor.py | 0 tests/callbacks/test_xla_stats_monitor.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 pytorch_lightning/callbacks/xla_stats_monitor.py delete mode 100644 tests/callbacks/test_xla_stats_monitor.py diff --git a/pytorch_lightning/callbacks/xla_stats_monitor.py b/pytorch_lightning/callbacks/xla_stats_monitor.py deleted file mode 100644 index e69de29bb2d1d..0000000000000 diff --git a/tests/callbacks/test_xla_stats_monitor.py b/tests/callbacks/test_xla_stats_monitor.py deleted file mode 100644 index e69de29bb2d1d..0000000000000 From ea64befb97ce1c38b77a12d1ed3a4ed5c713e21c Mon Sep 17 00:00:00 2001 From: Ankita Sharma Date: Sun, 10 Apr 2022 16:32:59 +0530 Subject: [PATCH 3/9] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a994268cd98c..22d6df6606e4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,7 +75,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed the deprecated `terminate_on_nan` argument from the `Trainer` constructor ([#12553](https://github.com/PyTorchLightning/pytorch-lightning/pull/12553)) -- Removed deprecated `XLAStatsMonitor` callback ([#12554](https://github.com/PyTorchLightning/pytorch-lightning/pull/12554)) +- Removed deprecated `XLAStatsMonitor` callback ([#12688](https://github.com/PyTorchLightning/pytorch-lightning/pull/12688)) - From d7c25f7620dbaf8f149e67f3f2e70d2d03a4449e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 10 Apr 2022 11:06:08 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- 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 deaef05b195ce..35dae7c8d3add 100644 --- a/tests/deprecated_api/test_remove_1-7.py +++ b/tests/deprecated_api/test_remove_1-7.py @@ -327,7 +327,7 @@ def test_v1_7_0_deprecate_gpu_stats_monitor(tmpdir): with pytest.deprecated_call(match="The `GPUStatsMonitor` callback was deprecated in v1.5"): _ = GPUStatsMonitor() - + def test_v1_7_0_progress_bar(): with pytest.deprecated_call(match="has been deprecated in v1.5 and will be removed in v1.7."): _ = ProgressBar() @@ -445,4 +445,3 @@ def post_dispatch(self, trainer): with pytest.deprecated_call(match=escape("`CustomPlugin.post_dispatch()` has been deprecated in v1.6")): CustomPlugin(torch.device("cpu")) - From 06c53fa5e12a63b4347a34dc221d867031844143 Mon Sep 17 00:00:00 2001 From: Ankita Sharma Date: Tue, 12 Apr 2022 17:41:21 +0530 Subject: [PATCH 5/9] remove from docs --- docs/source/api_references.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/source/api_references.rst b/docs/source/api_references.rst index 23a43990fed39..0903923a1b47a 100644 --- a/docs/source/api_references.rst +++ b/docs/source/api_references.rst @@ -99,7 +99,6 @@ Callbacks API StochasticWeightAveraging Timer TQDMProgressBar - XLAStatsMonitor Loggers API ----------- From 3cbce834ccf50b9d4bba654a36960c8bb43714cd Mon Sep 17 00:00:00 2001 From: Ankita Sharma Date: Tue, 12 Apr 2022 17:45:05 +0530 Subject: [PATCH 6/9] minor --- docs/source/extensions/callbacks.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/source/extensions/callbacks.rst b/docs/source/extensions/callbacks.rst index 19b43f4f4d127..cf8cb72e8e6d3 100644 --- a/docs/source/extensions/callbacks.rst +++ b/docs/source/extensions/callbacks.rst @@ -106,8 +106,7 @@ Lightning has a few built-in callbacks. StochasticWeightAveraging Timer TQDMProgressBar - XLAStatsMonitor - + ---------- .. _Persisting Callback State: From 066c270d7f8441ef3c6897c9006e9820c6df4948 Mon Sep 17 00:00:00 2001 From: rohitgr7 Date: Tue, 12 Apr 2022 17:47:17 +0530 Subject: [PATCH 7/9] remove from callbacks --- pytorch_lightning/callbacks/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pytorch_lightning/callbacks/__init__.py b/pytorch_lightning/callbacks/__init__.py index caa357caffef1..fc64772e00474 100644 --- a/pytorch_lightning/callbacks/__init__.py +++ b/pytorch_lightning/callbacks/__init__.py @@ -34,7 +34,6 @@ "Callback", "DeviceStatsMonitor", "EarlyStopping", - "XLAStatsMonitor", "GradientAccumulationScheduler", "LambdaCallback", "LearningRateMonitor", From 0bacc6f228de23e47989f9164dd4c3470718e452 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Apr 2022 12:17:49 +0000 Subject: [PATCH 8/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/extensions/callbacks.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/extensions/callbacks.rst b/docs/source/extensions/callbacks.rst index fb684ddccf046..f6dccff662e03 100644 --- a/docs/source/extensions/callbacks.rst +++ b/docs/source/extensions/callbacks.rst @@ -105,7 +105,7 @@ Lightning has a few built-in callbacks. StochasticWeightAveraging Timer TQDMProgressBar - + ---------- .. _Persisting Callback State: From 19e172df93dc2a3e3448306174604fd6627fb53d Mon Sep 17 00:00:00 2001 From: rohitgr7 Date: Tue, 12 Apr 2022 17:51:47 +0530 Subject: [PATCH 9/9] pre-commit --- tests/deprecated_api/test_remove_1-7.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/deprecated_api/test_remove_1-7.py b/tests/deprecated_api/test_remove_1-7.py index e161584f280e8..4d89ee9c8b5e2 100644 --- a/tests/deprecated_api/test_remove_1-7.py +++ b/tests/deprecated_api/test_remove_1-7.py @@ -20,7 +20,6 @@ import pytest import torch -import pytorch_lightning from pytorch_lightning import Callback, LightningDataModule, Trainer from pytorch_lightning.callbacks.lr_monitor import LearningRateMonitor from pytorch_lightning.loggers import LoggerCollection, TestTubeLogger @@ -33,7 +32,6 @@ TorchElasticEnvironment, ) from pytorch_lightning.strategies import SingleDeviceStrategy -from pytorch_lightning.utilities.exceptions import MisconfigurationException from tests.deprecated_api import _soft_unimport_module from tests.helpers import BoringModel from tests.helpers.datamodules import MNISTDataModule