Skip to content

Commit 11968ba

Browse files
relativityhdpre-commit-ci[bot]deependujhabhimrazy
authored
Fix advanced profiler for python >=3.12 (#20809)
* Fix advanced profiler for python >=3.12 * test: add compatibility test for nested profiling in AdvancedProfiler (Python 3.12+) * Update src/lightning/pytorch/profilers/advanced.py * changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deependu <[email protected]> Co-authored-by: Bhimraj Yadav <[email protected]>
1 parent 25b1343 commit 11968ba

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/lightning/pytorch/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2626
### Fixed
2727

2828
- fix progress bar console clearing for Rich `14.1+` ([#21016](https://github.com/Lightning-AI/pytorch-lightning/pull/21016))
29+
- fix `AdvancedProfiler` to handle nested profiling actions for Python 3.12+ ([#20809](https://github.com/Lightning-AI/pytorch-lightning/pull/20809))
2930

3031

3132
---

src/lightning/pytorch/profilers/advanced.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import pstats
2121
import tempfile
22+
from collections import defaultdict
2223
from pathlib import Path
2324
from typing import Optional, Union
2425

@@ -66,14 +67,15 @@ def __init__(
6667
If you attempt to stop recording an action which was never started.
6768
"""
6869
super().__init__(dirpath=dirpath, filename=filename)
69-
self.profiled_actions: dict[str, cProfile.Profile] = {}
70+
self.profiled_actions: dict[str, cProfile.Profile] = defaultdict(cProfile.Profile)
7071
self.line_count_restriction = line_count_restriction
7172
self.dump_stats = dump_stats
7273

7374
@override
7475
def start(self, action_name: str) -> None:
75-
if action_name not in self.profiled_actions:
76-
self.profiled_actions[action_name] = cProfile.Profile()
76+
# Disable all profilers before starting a new one
77+
for pr in self.profiled_actions.values():
78+
pr.disable()
7779
self.profiled_actions[action_name].enable()
7880

7981
@override
@@ -114,7 +116,7 @@ def summary(self) -> str:
114116
@override
115117
def teardown(self, stage: Optional[str]) -> None:
116118
super().teardown(stage=stage)
117-
self.profiled_actions = {}
119+
self.profiled_actions.clear()
118120

119121
def __reduce__(self) -> tuple:
120122
# avoids `TypeError: cannot pickle 'cProfile.Profile' object`

tests/tests_pytorch/profilers/test_profiler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ def test_advanced_profiler_deepcopy(advanced_profiler):
336336
assert deepcopy(advanced_profiler)
337337

338338

339+
def test_advanced_profiler_nested(advanced_profiler):
340+
"""Ensure AdvancedProfiler does not raise ValueError for nested profiling actions (Python 3.12+ compatibility)."""
341+
with advanced_profiler.profile("outer"), advanced_profiler.profile("inner"):
342+
pass # Should not raise ValueError
343+
344+
339345
@pytest.fixture
340346
def pytorch_profiler(tmp_path):
341347
return PyTorchProfiler(dirpath=tmp_path, filename="profiler")

0 commit comments

Comments
 (0)