Skip to content

Fix advanced profiler for python >=3.12 #20809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/lightning/pytorch/profilers/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import pstats
import tempfile
from collections import defaultdict
from pathlib import Path
from typing import Optional, Union

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
Loading