Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/lightning/pytorch/profilers/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def start(self, action_name: str) -> None:
def stop(self, action_name: str) -> None:
pr = self.profiled_actions.get(action_name)
if pr is None:
raise ValueError(f"Attempting to stop recording an action ({action_name}) which was never started.")
return
pr.disable()

def _dump_stats(self, action_name: str, profile: cProfile.Profile) -> None:
Expand Down Expand Up @@ -116,6 +116,8 @@ def summary(self) -> str:
@override
def teardown(self, stage: Optional[str]) -> None:
super().teardown(stage=stage)
for pr in self.profiled_actions.values():
pr.disable()
self.profiled_actions.clear()

def __reduce__(self) -> tuple:
Expand Down
26 changes: 23 additions & 3 deletions tests/tests_pytorch/profilers/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,33 @@ def test_advanced_profiler_dump_states_sanitizes_filename(tmp_path, char):
def test_advanced_profiler_value_errors(advanced_profiler):
"""Ensure errors are raised where expected."""
action = "test"
with pytest.raises(ValueError, match="Attempting to stop recording*"):
advanced_profiler.stop(action)

# Starting an action already started does not raise an error since defaultdict handles it silently
advanced_profiler.start(action)
advanced_profiler.stop(action)


def test_advanced_profiler_stop_on_never_started_action(advanced_profiler):
"""Ensure AdvancedProfiler.stop() does not crash when stopping an action that was never started.

Regression test for https://github.com/Lightning-AI/pytorch-lightning/issues/9136

"""
# Should not raise ValueError
advanced_profiler.stop("never_started_action")


def test_advanced_profiler_teardown_with_active_action(advanced_profiler):
"""Ensure AdvancedProfiler.stop() does not crash after teardown clears profiled_actions.

Regression test for https://github.com/Lightning-AI/pytorch-lightning/issues/9136

"""
with advanced_profiler.profile("action"):
# teardown clears the recorded actions, mimicking describe()
advanced_profiler.teardown(stage=None)
# Exiting the 'with' block will call stop("action") which should not raise an error


def test_advanced_profiler_deepcopy(advanced_profiler):
advanced_profiler.describe()
assert deepcopy(advanced_profiler)
Expand Down