From 511f1a651506af2e12e15346de0a3715fed7e814 Mon Sep 17 00:00:00 2001 From: awaelchli Date: Wed, 22 Jun 2022 16:57:19 +0200 Subject: [PATCH] Reroute profiler to profilers (#12308) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Carlos MocholĂ­ Co-authored-by: Akihiro Nitta --- .github/CODEOWNERS | 4 +- CHANGELOG.md | 4 + dockers/tpu-tests/tpu_test_cases.jsonnet | 2 +- docs/source-pytorch/api_references.rst | 2 +- docs/source-pytorch/common/trainer.rst | 2 +- .../tuning/profiler_advanced.rst | 4 +- docs/source-pytorch/tuning/profiler_basic.rst | 4 +- .../source-pytorch/tuning/profiler_expert.rst | 8 +- .../tuning/profiler_intermediate.rst | 12 +-- pyproject.toml | 8 +- src/pytorch_lightning/profiler/__init__.py | 31 ++++++++ src/pytorch_lightning/profiler/advanced.py | 24 ++++++ src/pytorch_lightning/profiler/base.py | 74 +++++++++++++++++++ src/pytorch_lightning/profiler/profiler.py | 30 ++++++++ src/pytorch_lightning/profiler/pytorch.py | 44 +++++++++++ src/pytorch_lightning/profiler/simple.py | 24 ++++++ src/pytorch_lightning/profiler/xla.py | 24 ++++++ .../deprecated_api/test_remove_1-8.py | 3 +- .../deprecated_api/test_remove_1-9.py | 41 ++++++++++ tests/tests_pytorch/utilities/test_cli.py | 5 +- 20 files changed, 324 insertions(+), 26 deletions(-) create mode 100644 src/pytorch_lightning/profiler/__init__.py create mode 100644 src/pytorch_lightning/profiler/advanced.py create mode 100644 src/pytorch_lightning/profiler/base.py create mode 100644 src/pytorch_lightning/profiler/profiler.py create mode 100644 src/pytorch_lightning/profiler/pytorch.py create mode 100644 src/pytorch_lightning/profiler/simple.py create mode 100644 src/pytorch_lightning/profiler/xla.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 94d4b420fc076..7a113e69dc119 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -33,8 +33,8 @@ /src/pytorch_lightning/loops @tchaton @awaelchli @justusschock @carmocca /src/pytorch_lightning/overrides @tchaton @SeanNaren @borda /src/pytorch_lightning/plugins @tchaton @SeanNaren @awaelchli @justusschock -/src/pytorch_lightning/profiler @williamfalcon @tchaton @borda @carmocca -/src/pytorch_lightning/profiler/pytorch.py @nbcsm @guotuofeng +/src/pytorch_lightning/profilers @williamfalcon @tchaton @borda @carmocca +/src/pytorch_lightning/profilers/pytorch.py @nbcsm @guotuofeng /src/pytorch_lightning/strategies @tchaton @SeanNaren @awaelchli @justusschock @kaushikb11 /src/pytorch_lightning/trainer @williamfalcon @borda @tchaton @SeanNaren @carmocca @awaelchli @justusschock @kaushikb11 /src/pytorch_lightning/trainer/connectors @tchaton @SeanNaren @carmocca @borda diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a16757aa8741..906603f55e3db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -146,6 +146,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Deprecated LightningCLI's registries in favor of importing the respective package ([#13221](https://github.com/PyTorchLightning/pytorch-lightning/pull/13221)) + +- Deprecated `pytorch_lightning.profiler` in favor of `pytorch_lightning.profilers` ([#12308](https://github.com/PyTorchLightning/pytorch-lightning/pull/12308)) + + ### Removed - Removed the deprecated `Logger.close` method ([#13149](https://github.com/PyTorchLightning/pytorch-lightning/pull/13149)) diff --git a/dockers/tpu-tests/tpu_test_cases.jsonnet b/dockers/tpu-tests/tpu_test_cases.jsonnet index 5ec110729bd93..338d09d0b6612 100644 --- a/dockers/tpu-tests/tpu_test_cases.jsonnet +++ b/dockers/tpu-tests/tpu_test_cases.jsonnet @@ -36,7 +36,7 @@ local tputests = base.BaseTest { # TODO (@kaushikb11): Add device stats tests here coverage run --source=pytorch_lightning -m pytest -v --capture=no \ strategies/test_tpu_spawn.py \ - profiler/test_xla_profiler.py \ + profilers/test_xla_profiler.py \ accelerators/test_tpu.py \ models/test_tpu.py \ plugins/environments/test_xla_environment.py diff --git a/docs/source-pytorch/api_references.rst b/docs/source-pytorch/api_references.rst index a147340d36df4..15640bc3ca81f 100644 --- a/docs/source-pytorch/api_references.rst +++ b/docs/source-pytorch/api_references.rst @@ -232,7 +232,7 @@ others profiler -------- -.. currentmodule:: pytorch_lightning.profiler +.. currentmodule:: pytorch_lightning.profilers .. autosummary:: :toctree: api diff --git a/docs/source-pytorch/common/trainer.rst b/docs/source-pytorch/common/trainer.rst index 68fca5e7ba30e..3e3734ceb35e3 100644 --- a/docs/source-pytorch/common/trainer.rst +++ b/docs/source-pytorch/common/trainer.rst @@ -1213,7 +1213,7 @@ See the :doc:`profiler documentation <../tuning/profiler>`. for more details. .. testcode:: - from pytorch_lightning.profiler import SimpleProfiler, AdvancedProfiler + from pytorch_lightning.profilers import SimpleProfiler, AdvancedProfiler # default used by the Trainer trainer = Trainer(profiler=None) diff --git a/docs/source-pytorch/tuning/profiler_advanced.rst b/docs/source-pytorch/tuning/profiler_advanced.rst index ad2ab9e2020a1..1a1794f35f0a0 100644 --- a/docs/source-pytorch/tuning/profiler_advanced.rst +++ b/docs/source-pytorch/tuning/profiler_advanced.rst @@ -12,11 +12,11 @@ Find bottlenecks in your code (advanced) ************************ Profile cloud TPU models ************************ -To profile TPU models use the :class:`~pytorch_lightning.profiler.xla.XLAProfiler` +To profile TPU models use the :class:`~pytorch_lightning.profilers.xla.XLAProfiler` .. code-block:: python - from pytorch_lightning.profiler import XLAProfiler + from pytorch_lightning.profilers import XLAProfiler profiler = XLAProfiler(port=9001) trainer = Trainer(profiler=profiler) diff --git a/docs/source-pytorch/tuning/profiler_basic.rst b/docs/source-pytorch/tuning/profiler_basic.rst index c3ddc114dce9a..02954e287c586 100644 --- a/docs/source-pytorch/tuning/profiler_basic.rst +++ b/docs/source-pytorch/tuning/profiler_basic.rst @@ -68,7 +68,7 @@ The simple profiler measures all the standard methods used in the training loop ************************************** Profile the time within every function ************************************** -To profile the time within every function, use the :class:`~pytorch_lightning.profiler.advanced.AdvancedProfiler` built on top of Python's `cProfiler `_. +To profile the time within every function, use the :class:`~pytorch_lightning.profilers.advanced.AdvancedProfiler` built on top of Python's `cProfiler `_. .. code-block:: python @@ -101,7 +101,7 @@ If the profiler report becomes too long, you can stream the report to a file: .. code-block:: python - from pytorch_lightning.profiler import AdvancedProfiler + from pytorch_lightning.profilers import AdvancedProfiler profiler = AdvancedProfiler(dirpath=".", filename="perf_logs") trainer = Trainer(profiler=profiler) diff --git a/docs/source-pytorch/tuning/profiler_expert.rst b/docs/source-pytorch/tuning/profiler_expert.rst index 64ff784ed6c0d..fe864536e4b03 100644 --- a/docs/source-pytorch/tuning/profiler_expert.rst +++ b/docs/source-pytorch/tuning/profiler_expert.rst @@ -12,12 +12,12 @@ Find bottlenecks in your code (expert) *********************** Build your own profiler *********************** -To build your own profiler, subclass :class:`~pytorch_lightning.profiler.base.Profiler` +To build your own profiler, subclass :class:`~pytorch_lightning.profilers.profiler.Profiler` and override some of its methods. Here is a simple example that profiles the first occurrence and total calls of each action: .. code-block:: python - from pytorch_lightning.profiler import Profiler + from pytorch_lightning.profilers import Profiler from collections import defaultdict import time @@ -69,7 +69,7 @@ To profile a specific action of interest, reference a profiler in the LightningM .. code-block:: python - from pytorch_lightning.profiler import SimpleProfiler, PassThroughProfiler + from pytorch_lightning.profilers import SimpleProfiler, PassThroughProfiler class MyModel(LightningModule): @@ -90,7 +90,7 @@ Here's the full code: .. code-block:: python - from pytorch_lightning.profiler import SimpleProfiler, PassThroughProfiler + from pytorch_lightning.profilers import SimpleProfiler, PassThroughProfiler class MyModel(LightningModule): diff --git a/docs/source-pytorch/tuning/profiler_intermediate.rst b/docs/source-pytorch/tuning/profiler_intermediate.rst index d2b64b5d54743..a3356cb3e2f8a 100644 --- a/docs/source-pytorch/tuning/profiler_intermediate.rst +++ b/docs/source-pytorch/tuning/profiler_intermediate.rst @@ -12,11 +12,11 @@ Find bottlenecks in your code (intermediate) ************************** Profile pytorch operations ************************** -To understand the cost of each PyTorch operation, use the :class:`~pytorch_lightning.profiler.pytorch.PyTorchProfiler` built on top of the `PyTorch profiler `__. +To understand the cost of each PyTorch operation, use the :class:`~pytorch_lightning.profilers.pytorch.PyTorchProfiler` built on top of the `PyTorch profiler `__. .. code-block:: python - from pytorch_lightning.profiler import PyTorchProfiler + from pytorch_lightning.profilers import PyTorchProfiler profiler = PyTorchProfiler() trainer = Trainer(profiler=profiler) @@ -65,11 +65,11 @@ The profiler will generate an output like this: *************************** Profile a distributed model *************************** -To profile a distributed model, use the :class:`~pytorch_lightning.profiler.pytorch.PyTorchProfiler` with the *filename* argument which will save a report per rank. +To profile a distributed model, use the :class:`~pytorch_lightning.profilers.pytorch.PyTorchProfiler` with the *filename* argument which will save a report per rank. .. code-block:: python - from pytorch_lightning.profiler import PyTorchProfiler + from pytorch_lightning.profilers import PyTorchProfiler profiler = PyTorchProfiler(filename="perf-logs") trainer = Trainer(profiler=profiler) @@ -153,11 +153,11 @@ to extend the scope of profiled functions. ***************************** Visualize profiled operations ***************************** -To visualize the profiled operations, enable **emit_nvtx** in the :class:`~pytorch_lightning.profiler.pytorch.PyTorchProfiler`. +To visualize the profiled operations, enable **emit_nvtx** in the :class:`~pytorch_lightning.profilers.pytorch.PyTorchProfiler`. .. code-block:: python - from pytorch_lightning.profiler import PyTorchProfiler + from pytorch_lightning.profilers import PyTorchProfiler profiler = PyTorchProfiler(emit_nvtx=True) trainer = Trainer(profiler=profiler) diff --git a/pyproject.toml b/pyproject.toml index 4b9f45068e089..7802f9e7dca8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -78,10 +78,10 @@ module = [ "pytorch_lightning.strategies.single_tpu", "pytorch_lightning.strategies.tpu_spawn", "pytorch_lightning.strategies.strategy", - "pytorch_lightning.profiler.advanced", - "pytorch_lightning.profiler.base", - "pytorch_lightning.profiler.pytorch", - "pytorch_lightning.profiler.simple", + "pytorch_lightning.profilers.advanced", + "pytorch_lightning.profilers.base", + "pytorch_lightning.profilers.pytorch", + "pytorch_lightning.profilers.simple", "pytorch_lightning.trainer.callback_hook", "pytorch_lightning.trainer.connectors.callback_connector", "pytorch_lightning.trainer.connectors.data_connector", diff --git a/src/pytorch_lightning/profiler/__init__.py b/src/pytorch_lightning/profiler/__init__.py new file mode 100644 index 0000000000000..f55d7223e4051 --- /dev/null +++ b/src/pytorch_lightning/profiler/__init__.py @@ -0,0 +1,31 @@ +# 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. +from pytorch_lightning.profiler.base import AbstractProfiler, BaseProfiler +from pytorch_lightning.profilers.advanced import AdvancedProfiler +from pytorch_lightning.profilers.base import PassThroughProfiler +from pytorch_lightning.profilers.profiler import Profiler +from pytorch_lightning.profilers.pytorch import PyTorchProfiler +from pytorch_lightning.profilers.simple import SimpleProfiler +from pytorch_lightning.profilers.xla import XLAProfiler + +__all__ = [ + "AbstractProfiler", + "BaseProfiler", + "Profiler", + "AdvancedProfiler", + "PassThroughProfiler", + "PyTorchProfiler", + "SimpleProfiler", + "XLAProfiler", +] diff --git a/src/pytorch_lightning/profiler/advanced.py b/src/pytorch_lightning/profiler/advanced.py new file mode 100644 index 0000000000000..1d2bbed5d96f6 --- /dev/null +++ b/src/pytorch_lightning/profiler/advanced.py @@ -0,0 +1,24 @@ +# 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. +from pytorch_lightning.profilers.advanced import AdvancedProfiler as NewAdvancedProfiler +from pytorch_lightning.utilities import rank_zero_deprecation + + +class AdvancedProfiler(NewAdvancedProfiler): + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.AdvancedProfiler` is deprecated in v1.7 and will be removed in v1.9." + " Use the equivalent `pytorch_lightning.profilers.AdvancedProfiler` class instead." + ) + super().__init__(*args, **kwargs) diff --git a/src/pytorch_lightning/profiler/base.py b/src/pytorch_lightning/profiler/base.py new file mode 100644 index 0000000000000..f2e0ad5276f2e --- /dev/null +++ b/src/pytorch_lightning/profiler/base.py @@ -0,0 +1,74 @@ +# 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. +"""Profiler to check if there are any bottlenecks in your code.""" +from abc import ABC, abstractmethod +from typing import Any + +from pytorch_lightning.profilers.base import PassThroughProfiler as NewPassThroughProfiler +from pytorch_lightning.profilers.profiler import Profiler +from pytorch_lightning.utilities.rank_zero import rank_zero_deprecation + + +class AbstractProfiler(ABC): + """Specification of a profiler. + + See deprecation warning below + + .. deprecated:: v1.6 + `AbstractProfiler` was deprecated in v1.6 and will be removed in v1.8. + Please use `Profiler` instead. + """ + + @abstractmethod + def start(self, action_name: str) -> None: + """Defines how to start recording an action.""" + + @abstractmethod + def stop(self, action_name: str) -> None: + """Defines how to record the duration once an action is complete.""" + + @abstractmethod + def summary(self) -> str: + """Create profiler summary in text format.""" + + @abstractmethod + def setup(self, **kwargs: Any) -> None: + """Execute arbitrary pre-profiling set-up steps as defined by subclass.""" + + @abstractmethod + def teardown(self, **kwargs: Any) -> None: + """Execute arbitrary post-profiling tear-down steps as defined by subclass.""" + + +class BaseProfiler(Profiler): + """ + .. deprecated:: v1.6 + `BaseProfiler` was deprecated in v1.6 and will be removed in v1.8. + Please use `Profiler` instead. + """ + + def __init__(self, *args, **kwargs): # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`BaseProfiler` was deprecated in v1.6 and will be removed in v1.8. Please use `Profiler` instead." + ) + super().__init__(*args, **kwargs) + + +class PassThroughProfiler(NewPassThroughProfiler): + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.PassThroughProfiler` is deprecated in v1.7 and will be removed in v1.9." + " Use the equivalent `pytorch_lightning.profilers.PassThroughProfiler` class instead." + ) + super().__init__(*args, **kwargs) diff --git a/src/pytorch_lightning/profiler/profiler.py b/src/pytorch_lightning/profiler/profiler.py new file mode 100644 index 0000000000000..84bea3ecae238 --- /dev/null +++ b/src/pytorch_lightning/profiler/profiler.py @@ -0,0 +1,30 @@ +# 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. +from pytorch_lightning.profilers.profiler import Profiler as NewProfiler +from pytorch_lightning.utilities import rank_zero_deprecation + + +class Profiler(NewProfiler): + """ + .. deprecated:: v1.6 + `pytorch_lightning.profiler.Profiler` is deprecated in v1.7 and will be removed in v1.9. + Use the equivalent `pytorch_lightning.profilers.Profiler` class instead. + """ + + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.Profiler` is deprecated in v1.7 and will be removed in v1.9." + " Use the equivalent `pytorch_lightning.profilers.Profiler` class instead." + ) + super().__init__(*args, **kwargs) diff --git a/src/pytorch_lightning/profiler/pytorch.py b/src/pytorch_lightning/profiler/pytorch.py new file mode 100644 index 0000000000000..d443059912602 --- /dev/null +++ b/src/pytorch_lightning/profiler/pytorch.py @@ -0,0 +1,44 @@ +# 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. +from pytorch_lightning.profilers.pytorch import PyTorchProfiler as NewPyTorchProfiler +from pytorch_lightning.profilers.pytorch import RegisterRecordFunction as NewRegisterRecordFuncion +from pytorch_lightning.profilers.pytorch import ScheduleWrapper as NewScheduleWrapper +from pytorch_lightning.utilities import rank_zero_deprecation + + +class RegisterRecordFunction(NewRegisterRecordFuncion): + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.pytorch.RegisterRecordFunction` is deprecated in v1.7 and will be removed in" + " in v1.9. Use the equivalent `pytorch_lightning.profilers.pytorch.RegisterRecordFunction` class instead." + ) + super().__init__(*args, **kwargs) + + +class ScheduleWrapper(NewScheduleWrapper): + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.pytorch.ScheduleWrapper` is deprecated in v1.7 and will be removed in v1.9." + " Use the equivalent `pytorch_lightning.profilers.pytorch.ScheduleWrapper` class instead." + ) + super().__init__(*args, **kwargs) + + +class PyTorchProfiler(NewPyTorchProfiler): + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.PyTorchProfiler` is deprecated in v1.7 and will be removed in v1.9." + " Use the equivalent `pytorch_lightning.profilers.PyTorchProfiler` class instead." + ) + super().__init__(*args, **kwargs) diff --git a/src/pytorch_lightning/profiler/simple.py b/src/pytorch_lightning/profiler/simple.py new file mode 100644 index 0000000000000..61ef7da8ae0f4 --- /dev/null +++ b/src/pytorch_lightning/profiler/simple.py @@ -0,0 +1,24 @@ +# 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. +from pytorch_lightning.profilers.simple import SimpleProfiler as NewSimpleProfiler +from pytorch_lightning.utilities import rank_zero_deprecation + + +class SimpleProfiler(NewSimpleProfiler): + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.SimpleProfiler` is deprecated in v1.7 and will be removed in v1.9." + " Use the equivalent `pytorch_lightning.profilers.SimpleProfiler` class instead." + ) + super().__init__(*args, **kwargs) diff --git a/src/pytorch_lightning/profiler/xla.py b/src/pytorch_lightning/profiler/xla.py new file mode 100644 index 0000000000000..dde858e99eeaa --- /dev/null +++ b/src/pytorch_lightning/profiler/xla.py @@ -0,0 +1,24 @@ +# 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. +from pytorch_lightning.profilers.xla import XLAProfiler as NewXLAProfiler +from pytorch_lightning.utilities import rank_zero_deprecation + + +class XLAProfiler(NewXLAProfiler): + def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def] + rank_zero_deprecation( + "`pytorch_lightning.profiler.XLAProfiler` is deprecated in v1.7 and will be removed in v1.9." + " Use the equivalent `pytorch_lightning.profilers.XLAProfiler` class instead." + ) + super().__init__(*args, **kwargs) diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-8.py b/tests/tests_pytorch/deprecated_api/test_remove_1-8.py index 685eaf1e0d5ca..758367d1dd40b 100644 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-8.py +++ b/tests/tests_pytorch/deprecated_api/test_remove_1-8.py @@ -40,7 +40,8 @@ from pytorch_lightning.plugins.training_type.single_device import SingleDevicePlugin from pytorch_lightning.plugins.training_type.single_tpu import SingleTPUPlugin from pytorch_lightning.plugins.training_type.tpu_spawn import TPUSpawnPlugin -from pytorch_lightning.profilers import AbstractProfiler, AdvancedProfiler, BaseProfiler, Profiler, SimpleProfiler +from pytorch_lightning.profiler import AbstractProfiler, BaseProfiler +from pytorch_lightning.profilers import AdvancedProfiler, Profiler, SimpleProfiler from pytorch_lightning.strategies import ParallelStrategy from pytorch_lightning.trainer.configuration_validator import _check_datamodule_checkpoint_hooks from pytorch_lightning.trainer.states import RunningStage diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-9.py b/tests/tests_pytorch/deprecated_api/test_remove_1-9.py index 74d509bd8df4b..66bbf80d4e3ea 100644 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-9.py +++ b/tests/tests_pytorch/deprecated_api/test_remove_1-9.py @@ -20,6 +20,12 @@ from pytorch_lightning import Trainer from pytorch_lightning.core.module import LightningModule from pytorch_lightning.demos.boring_classes import BoringModel +from pytorch_lightning.profiler.advanced import AdvancedProfiler +from pytorch_lightning.profiler.base import PassThroughProfiler +from pytorch_lightning.profiler.profiler import Profiler +from pytorch_lightning.profiler.pytorch import PyTorchProfiler, RegisterRecordFunction, ScheduleWrapper +from pytorch_lightning.profiler.simple import SimpleProfiler +from pytorch_lightning.profiler.xla import XLAProfiler from pytorch_lightning.utilities.cli import ( _deprecate_auto_registry_message, _deprecate_registry_message, @@ -27,7 +33,9 @@ LightningCLI, SaveConfigCallback, ) +from pytorch_lightning.utilities.imports import _KINETO_AVAILABLE from pytorch_lightning.utilities.rank_zero import rank_zero_only +from tests_pytorch.helpers.runif import RunIf def test_lightning_logger_base_deprecation_warning(): @@ -154,3 +162,36 @@ def test_lightningCLI_registries_register_automatically(): with pytest.deprecated_call(match=_deprecate_auto_registry_message): with mock.patch("sys.argv", ["any.py"]): LightningCLI(BoringModel, run=False, auto_registry=True) + + +def test_profiler_deprecation_warning(): + assert "Profiler` is deprecated in v1.7" in Profiler.__doc__ + + +@pytest.mark.parametrize( + "cls", + [ + AdvancedProfiler, + PassThroughProfiler, + PyTorchProfiler, + SimpleProfiler, + pytest.param(XLAProfiler, marks=RunIf(tpu=True)), + ], +) +def test_profiler_classes_deprecated_warning(cls): + with pytest.deprecated_call( + match=f"profiler.{cls.__name__}` is deprecated in v1.7 and will be removed in v1.9." + f" Use .*profilers.{cls.__name__}` class instead." + ): + cls() + + +@pytest.mark.skipif(not _KINETO_AVAILABLE, reason="Requires PyTorch Profiler Kineto") +def test_pytorch_profiler_schedule_wrapper_deprecation_warning(): + with pytest.deprecated_call(match="ScheduleWrapper` is deprecated in v1.7 and will be removed in v1.9."): + _ = ScheduleWrapper(None) + + +def test_pytorch_profiler_register_record_function_deprecation_warning(): + with pytest.deprecated_call(match="RegisterRecordFunction` is deprecated in v1.7 and will be removed in in v1.9."): + _ = RegisterRecordFunction(None) diff --git a/tests/tests_pytorch/utilities/test_cli.py b/tests/tests_pytorch/utilities/test_cli.py index 1920344a7d5cc..8e801299aa23c 100644 --- a/tests/tests_pytorch/utilities/test_cli.py +++ b/tests/tests_pytorch/utilities/test_cli.py @@ -36,7 +36,6 @@ from pytorch_lightning.demos.boring_classes import BoringDataModule, BoringModel from pytorch_lightning.loggers import _COMET_AVAILABLE, _NEPTUNE_AVAILABLE, _WANDB_AVAILABLE, TensorBoardLogger from pytorch_lightning.plugins.environments import SLURMEnvironment -from pytorch_lightning.profilers import PyTorchProfiler from pytorch_lightning.strategies import DDPStrategy from pytorch_lightning.trainer.states import TrainerFn from pytorch_lightning.utilities import _TPU_AVAILABLE @@ -1497,6 +1496,8 @@ def __init__(self, a_func: Callable = torch.softmax): def test_pytorch_profiler_init_args(): + from pytorch_lightning.profilers import Profiler, PyTorchProfiler + init = { "dirpath": "profiler", "row_limit": 10, @@ -1510,7 +1511,7 @@ def test_pytorch_profiler_init_args(): cli_args += [f"--trainer.profiler.{k}={v}" for k, v in init.items()] cli_args += [f"--trainer.profiler.dict_kwargs.{k}={v}" for k, v in unresolved.items()] - with mock.patch("sys.argv", ["any.py"] + cli_args): + with mock.patch("sys.argv", ["any.py"] + cli_args), mock_subclasses(Profiler, PyTorchProfiler): cli = LightningCLI(TestModel, run=False) assert isinstance(cli.config_init.trainer.profiler, PyTorchProfiler)