From 5cac2d06c06a1bcd647efc7f40c24f4e9384e35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B6lzer?= Date: Sat, 10 May 2025 18:03:36 +0200 Subject: [PATCH 1/4] Fix advanced profiler for python >=3.12 --- src/lightning/pytorch/profilers/advanced.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lightning/pytorch/profilers/advanced.py b/src/lightning/pytorch/profilers/advanced.py index 41681fbd239f3..2efe0655cd1ac 100644 --- a/src/lightning/pytorch/profilers/advanced.py +++ b/src/lightning/pytorch/profilers/advanced.py @@ -21,6 +21,7 @@ import tempfile from pathlib import Path from typing import Optional, Union +from collections import defaultdict from typing_extensions import override @@ -66,14 +67,15 @@ def __init__( If you attempt to stop recording an action which was never started. """ super().__init__(dirpath=dirpath, filename=filename) - self.profiled_actions: dict[str, cProfile.Profile] = {} + self.profiled_actions: dict[str, cProfile.Profile] = defaultdict(cProfile.Profile) self.line_count_restriction = line_count_restriction self.dump_stats = dump_stats @override def start(self, action_name: str) -> None: - if action_name not in self.profiled_actions: - self.profiled_actions[action_name] = cProfile.Profile() + # Disable all profilers before starting a new one + for pr in self.profiled_actions.values(): + pr.disable() self.profiled_actions[action_name].enable() @override @@ -114,7 +116,7 @@ def summary(self) -> str: @override def teardown(self, stage: Optional[str]) -> None: super().teardown(stage=stage) - self.profiled_actions = {} + self.profiled_actions = defaultdict(cProfile.Profile) def __reduce__(self) -> tuple: # avoids `TypeError: cannot pickle 'cProfile.Profile' object` From 9182413458c5c60a12bd388461cfe148279115e4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 May 2025 16:16:13 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/lightning/pytorch/profilers/advanced.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning/pytorch/profilers/advanced.py b/src/lightning/pytorch/profilers/advanced.py index 2efe0655cd1ac..38a67b4dca25d 100644 --- a/src/lightning/pytorch/profilers/advanced.py +++ b/src/lightning/pytorch/profilers/advanced.py @@ -19,9 +19,9 @@ import os import pstats import tempfile +from collections import defaultdict from pathlib import Path from typing import Optional, Union -from collections import defaultdict from typing_extensions import override From 660c269f2513c072d97967c2449f233d29af39bd Mon Sep 17 00:00:00 2001 From: bhimrazy Date: Tue, 5 Aug 2025 01:45:47 +0545 Subject: [PATCH 3/4] test: add compatibility test for nested profiling in AdvancedProfiler (Python 3.12+) --- tests/tests_pytorch/profilers/test_profiler.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/tests_pytorch/profilers/test_profiler.py b/tests/tests_pytorch/profilers/test_profiler.py index d0221d12e317f..52112769702c0 100644 --- a/tests/tests_pytorch/profilers/test_profiler.py +++ b/tests/tests_pytorch/profilers/test_profiler.py @@ -336,6 +336,12 @@ def test_advanced_profiler_deepcopy(advanced_profiler): assert deepcopy(advanced_profiler) +def test_advanced_profiler_nested(advanced_profiler): + """Ensure AdvancedProfiler does not raise ValueError for nested profiling actions (Python 3.12+ compatibility).""" + with advanced_profiler.profile("outer"), advanced_profiler.profile("inner"): + pass # Should not raise ValueError + + @pytest.fixture def pytorch_profiler(tmp_path): return PyTorchProfiler(dirpath=tmp_path, filename="profiler") From 097e647634089a9c820ddddb0ff670c1a21e9898 Mon Sep 17 00:00:00 2001 From: Deependu Date: Tue, 5 Aug 2025 02:19:18 +0530 Subject: [PATCH 4/4] Update src/lightning/pytorch/profilers/advanced.py --- src/lightning/pytorch/profilers/advanced.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightning/pytorch/profilers/advanced.py b/src/lightning/pytorch/profilers/advanced.py index 38a67b4dca25d..c0b4b9953cc33 100644 --- a/src/lightning/pytorch/profilers/advanced.py +++ b/src/lightning/pytorch/profilers/advanced.py @@ -116,7 +116,7 @@ def summary(self) -> str: @override def teardown(self, stage: Optional[str]) -> None: super().teardown(stage=stage) - self.profiled_actions = defaultdict(cProfile.Profile) + self.profiled_actions.clear() def __reduce__(self) -> tuple: # avoids `TypeError: cannot pickle 'cProfile.Profile' object`