Skip to content
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

Add teardown method to BaseProfiler. #6370

Merged
merged 10 commits into from
Mar 22, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Added arg to `self.log` that enables users to give custom names when dealing with multiple dataloaders ([#6274](https://github.com/PyTorchLightning/pytorch-lightning/pull/6274))

- Added teardown method to BaseProfiler to enable subclasses define post-profiling steps outside of __del__ method.

### Changed

Expand Down
Binary file added a.out
Binary file not shown.
17 changes: 15 additions & 2 deletions pytorch_lightning/profiler/profilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def start(self, action_name: str) -> None:
def stop(self, action_name: str) -> None:
"""Defines how to record the duration once an action is complete."""

@abstractmethod
def teardown(self) -> None:
"""Execute arbitrary post-profiling tear-down steps as defined by subclass."""

@contextmanager
def profile(self, action_name: str) -> None:
"""
Expand Down Expand Up @@ -114,6 +118,9 @@ def start(self, action_name: str) -> None:
def stop(self, action_name: str) -> None:
pass

def teardown(self) -> None:
pass

def summary(self) -> str:
return ""

Expand Down Expand Up @@ -214,11 +221,14 @@ def describe(self):
if self.output_file:
self.output_file.flush()

def __del__(self):
def teardown(self) -> None:
"""Close profiler's stream."""
if self.output_file:
self.output_file.close()

def __del__(self):
self.teardown()


class AdvancedProfiler(BaseProfiler):
"""
Expand Down Expand Up @@ -286,7 +296,10 @@ def describe(self):
if self.output_file:
self.output_file.flush()

def __del__(self):
def teardown(self) -> None:
"""Close profiler's stream."""
if self.output_file:
self.output_file.close()

def __del__(self):
self.teardown()
5 changes: 4 additions & 1 deletion pytorch_lightning/profiler/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ def describe(self):
if self.output_file:
self.output_file.flush()

def __del__(self):
def teardown(self) -> None:
"""Close profiler's stream."""
if self.output_file:
self.output_file.close()

def __del__(self):
self.teardown()