Skip to content

Commit 5cac2d0

Browse files
committed
Fix advanced profiler for python >=3.12
1 parent 1b12c4b commit 5cac2d0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/lightning/pytorch/profilers/advanced.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import tempfile
2222
from pathlib import Path
2323
from typing import Optional, Union
24+
from collections import defaultdict
2425

2526
from typing_extensions import override
2627

@@ -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 = defaultdict(cProfile.Profile)
118120

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

0 commit comments

Comments
 (0)