2020-08-20 02:03:22 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-11-13 15:05:54 +00:00
|
|
|
"""Profiler to check if there are any bottlenecks in your code."""
|
|
|
|
|
2020-02-07 03:01:21 +00:00
|
|
|
import cProfile
|
|
|
|
import io
|
2020-03-31 12:57:48 +00:00
|
|
|
import os
|
2020-03-12 16:41:37 +00:00
|
|
|
import pstats
|
|
|
|
import time
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from collections import defaultdict
|
|
|
|
from contextlib import contextmanager
|
2021-03-02 07:57:49 +00:00
|
|
|
from typing import Optional, Union
|
2020-03-12 16:41:37 +00:00
|
|
|
|
|
|
|
import numpy as np
|
2020-11-27 16:34:51 +00:00
|
|
|
|
2020-03-17 22:44:00 +00:00
|
|
|
from pytorch_lightning import _logger as log
|
2020-10-25 17:21:42 +00:00
|
|
|
from pytorch_lightning.utilities.cloud_io import get_filesystem
|
2020-03-17 22:44:00 +00:00
|
|
|
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
class BaseProfiler(ABC):
|
|
|
|
"""
|
|
|
|
If you wish to write a custom profiler, you should inhereit from this class.
|
|
|
|
"""
|
|
|
|
|
2020-10-15 21:02:50 +00:00
|
|
|
def __init__(self, output_streams: Optional[Union[list, tuple]] = None):
|
2020-03-31 12:57:48 +00:00
|
|
|
"""
|
2020-10-15 18:25:55 +00:00
|
|
|
Args:
|
|
|
|
output_streams: callable
|
2020-03-31 12:57:48 +00:00
|
|
|
"""
|
|
|
|
if output_streams:
|
|
|
|
if not isinstance(output_streams, (list, tuple)):
|
|
|
|
output_streams = [output_streams]
|
|
|
|
else:
|
|
|
|
output_streams = []
|
|
|
|
self.write_streams = output_streams
|
|
|
|
|
2020-02-07 03:01:21 +00:00
|
|
|
@abstractmethod
|
2020-03-30 22:37:02 +00:00
|
|
|
def start(self, action_name: str) -> None:
|
2020-03-19 13:14:29 +00:00
|
|
|
"""Defines how to start recording an action."""
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
2020-03-30 22:37:02 +00:00
|
|
|
def stop(self, action_name: str) -> None:
|
2020-03-19 13:14:29 +00:00
|
|
|
"""Defines how to record the duration once an action is complete."""
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
@contextmanager
|
2020-03-30 22:37:02 +00:00
|
|
|
def profile(self, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
"""
|
|
|
|
Yields a context manager to encapsulate the scope of a profiled action.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
with self.profile('load training data'):
|
|
|
|
# load training data code
|
|
|
|
|
|
|
|
The profiler will start once you've entered the context and will automatically
|
|
|
|
stop once you exit the code block.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.start(action_name)
|
|
|
|
yield action_name
|
|
|
|
finally:
|
|
|
|
self.stop(action_name)
|
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def profile_iterable(self, iterable, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
iterator = iter(iterable)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.start(action_name)
|
|
|
|
value = next(iterator)
|
|
|
|
self.stop(action_name)
|
|
|
|
yield value
|
|
|
|
except StopIteration:
|
|
|
|
self.stop(action_name)
|
|
|
|
break
|
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def describe(self) -> None:
|
2020-03-19 13:14:29 +00:00
|
|
|
"""Logs a profile report after the conclusion of the training run."""
|
2020-03-31 12:57:48 +00:00
|
|
|
for write in self.write_streams:
|
|
|
|
write(self.summary())
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def summary(self) -> str:
|
|
|
|
"""Create profiler summary in text format."""
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2021-01-26 11:48:54 +00:00
|
|
|
def on_train_start(self, local_rank: Optional[int] = None):
|
|
|
|
self.local_rank = local_rank
|
|
|
|
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
class PassThroughProfiler(BaseProfiler):
|
|
|
|
"""
|
|
|
|
This class should be used when you don't want the (small) overhead of profiling.
|
|
|
|
The Trainer uses this class by default.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2020-03-31 12:57:48 +00:00
|
|
|
super().__init__(output_streams=None)
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def start(self, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
pass
|
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def stop(self, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
pass
|
|
|
|
|
2020-03-31 12:57:48 +00:00
|
|
|
def summary(self) -> str:
|
|
|
|
return ""
|
|
|
|
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2020-03-31 12:57:48 +00:00
|
|
|
class SimpleProfiler(BaseProfiler):
|
2020-02-07 03:01:21 +00:00
|
|
|
"""
|
|
|
|
This profiler simply records the duration of actions (in seconds) and reports
|
|
|
|
the mean duration of each action and the total time spent over the entire training run.
|
|
|
|
"""
|
|
|
|
|
2020-11-27 16:34:51 +00:00
|
|
|
def __init__(self, output_filename: Optional[str] = None, extended=True):
|
2020-03-31 12:57:48 +00:00
|
|
|
"""
|
2020-10-15 18:25:55 +00:00
|
|
|
Args:
|
2020-10-25 17:21:42 +00:00
|
|
|
output_filename: optionally save profile results to file instead of printing
|
2020-03-31 12:57:48 +00:00
|
|
|
to std out when training is finished.
|
2021-03-01 13:16:06 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError:
|
|
|
|
If you attempt to start an action which has already started, or
|
|
|
|
if you attempt to stop recording an action which was never started.
|
2020-03-31 12:57:48 +00:00
|
|
|
"""
|
2020-02-07 03:01:21 +00:00
|
|
|
self.current_actions = {}
|
|
|
|
self.recorded_durations = defaultdict(list)
|
2020-11-27 16:34:51 +00:00
|
|
|
self.extended = extended
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2020-03-31 12:57:48 +00:00
|
|
|
self.output_fname = output_filename
|
2020-10-25 17:21:42 +00:00
|
|
|
self.output_file = None
|
|
|
|
if self.output_fname:
|
|
|
|
fs = get_filesystem(self.output_fname)
|
|
|
|
self.output_file = fs.open(self.output_fname, "w")
|
2020-03-31 12:57:48 +00:00
|
|
|
|
|
|
|
streaming_out = [self.output_file.write] if self.output_file else [log.info]
|
2020-11-27 16:34:51 +00:00
|
|
|
self.start_time = time.monotonic()
|
2020-03-31 12:57:48 +00:00
|
|
|
super().__init__(output_streams=streaming_out)
|
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def start(self, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
if action_name in self.current_actions:
|
2021-02-08 19:28:38 +00:00
|
|
|
raise ValueError(f"Attempted to start {action_name} which has already started.")
|
2020-02-07 03:01:21 +00:00
|
|
|
self.current_actions[action_name] = time.monotonic()
|
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def stop(self, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
end_time = time.monotonic()
|
|
|
|
if action_name not in self.current_actions:
|
2021-02-08 19:28:38 +00:00
|
|
|
raise ValueError(f"Attempting to stop recording an action ({action_name}) which was never started.")
|
2020-02-07 03:01:21 +00:00
|
|
|
start_time = self.current_actions.pop(action_name)
|
|
|
|
duration = end_time - start_time
|
|
|
|
self.recorded_durations[action_name].append(duration)
|
|
|
|
|
2020-11-27 16:34:51 +00:00
|
|
|
def make_report(self):
|
|
|
|
total_duration = time.monotonic() - self.start_time
|
|
|
|
report = [[a, d, 100. * np.sum(d) / total_duration] for a, d in self.recorded_durations.items()]
|
|
|
|
report.sort(key=lambda x: x[2], reverse=True)
|
|
|
|
return report, total_duration
|
|
|
|
|
2020-03-31 12:57:48 +00:00
|
|
|
def summary(self) -> str:
|
2020-02-07 03:01:21 +00:00
|
|
|
output_string = "\n\nProfiler Report\n"
|
|
|
|
|
2020-11-27 16:34:51 +00:00
|
|
|
if self.extended:
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2020-11-27 16:47:13 +00:00
|
|
|
if len(self.recorded_durations) > 0:
|
|
|
|
max_key = np.max([len(k) for k in self.recorded_durations.keys()])
|
|
|
|
|
|
|
|
def log_row(action, mean, num_calls, total, per):
|
|
|
|
row = f"{os.linesep}{action:<{max_key}s}\t| {mean:<15}\t|"
|
|
|
|
row += f"{num_calls:<15}\t| {total:<15}\t| {per:<15}\t|"
|
|
|
|
return row
|
|
|
|
|
2020-11-27 17:48:51 +00:00
|
|
|
output_string += log_row("Action", "Mean duration (s)", "Num calls", "Total time (s)", "Percentage %")
|
2020-11-27 16:47:13 +00:00
|
|
|
output_string_len = len(output_string)
|
|
|
|
output_string += f"{os.linesep}{'-' * output_string_len}"
|
|
|
|
report, total_duration = self.make_report()
|
|
|
|
output_string += log_row("Total", "-", "_", f"{total_duration:.5}", "100 %")
|
|
|
|
output_string += f"{os.linesep}{'-' * output_string_len}"
|
|
|
|
for action, durations, duration_per in report:
|
|
|
|
output_string += log_row(
|
2021-02-08 19:28:38 +00:00
|
|
|
action,
|
|
|
|
f"{np.mean(durations):.5}",
|
|
|
|
f"{len(durations):}",
|
|
|
|
f"{np.sum(durations):.5}",
|
|
|
|
f"{duration_per:.5}",
|
2020-11-27 16:47:13 +00:00
|
|
|
)
|
2020-11-27 16:34:51 +00:00
|
|
|
else:
|
2021-02-08 19:28:38 +00:00
|
|
|
|
2020-11-27 16:34:51 +00:00
|
|
|
def log_row(action, mean, total):
|
|
|
|
return f"{os.linesep}{action:<20s}\t| {mean:<15}\t| {total:<15}"
|
|
|
|
|
|
|
|
output_string += log_row("Action", "Mean duration (s)", "Total time (s)")
|
|
|
|
output_string += f"{os.linesep}{'-' * 65}"
|
|
|
|
|
|
|
|
for action, durations in self.recorded_durations.items():
|
2021-02-08 19:28:38 +00:00
|
|
|
output_string += log_row(action, f"{np.mean(durations):.5}", f"{np.sum(durations):.5}")
|
2020-03-31 12:57:48 +00:00
|
|
|
output_string += os.linesep
|
|
|
|
return output_string
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
"""Logs a profile report after the conclusion of the training run."""
|
|
|
|
super().describe()
|
|
|
|
if self.output_file:
|
|
|
|
self.output_file.flush()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
"""Close profiler's stream."""
|
|
|
|
if self.output_file:
|
|
|
|
self.output_file.close()
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdvancedProfiler(BaseProfiler):
|
|
|
|
"""
|
|
|
|
This profiler uses Python's cProfiler to record more detailed information about
|
|
|
|
time spent in each function call recorded during a given action. The output is quite
|
|
|
|
verbose and you should only use this if you want very detailed reports.
|
|
|
|
"""
|
|
|
|
|
2020-10-15 21:02:50 +00:00
|
|
|
def __init__(self, output_filename: Optional[str] = None, line_count_restriction: float = 1.0):
|
2020-02-07 03:01:21 +00:00
|
|
|
"""
|
2020-03-30 22:37:02 +00:00
|
|
|
Args:
|
|
|
|
output_filename: optionally save profile results to file instead of printing
|
|
|
|
to std out when training is finished.
|
|
|
|
line_count_restriction: this can be used to limit the number of functions
|
|
|
|
reported for each action. either an integer (to select a count of lines),
|
|
|
|
or a decimal fraction between 0.0 and 1.0 inclusive (to select a percentage of lines)
|
2021-03-01 13:16:06 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError:
|
|
|
|
If you attempt to stop recording an action which was never started.
|
2020-02-07 03:01:21 +00:00
|
|
|
"""
|
|
|
|
self.profiled_actions = {}
|
|
|
|
self.line_count_restriction = line_count_restriction
|
|
|
|
|
2020-03-31 12:57:48 +00:00
|
|
|
self.output_fname = output_filename
|
2020-10-25 17:21:42 +00:00
|
|
|
self.output_file = None
|
|
|
|
if self.output_fname:
|
|
|
|
fs = get_filesystem(self.output_fname)
|
|
|
|
self.output_file = fs.open(self.output_fname, "w")
|
2020-03-31 12:57:48 +00:00
|
|
|
|
|
|
|
streaming_out = [self.output_file.write] if self.output_file else [log.info]
|
|
|
|
super().__init__(output_streams=streaming_out)
|
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def start(self, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
if action_name not in self.profiled_actions:
|
|
|
|
self.profiled_actions[action_name] = cProfile.Profile()
|
|
|
|
self.profiled_actions[action_name].enable()
|
|
|
|
|
2020-03-30 22:37:02 +00:00
|
|
|
def stop(self, action_name: str) -> None:
|
2020-02-07 03:01:21 +00:00
|
|
|
pr = self.profiled_actions.get(action_name)
|
|
|
|
if pr is None:
|
2020-03-19 13:14:29 +00:00
|
|
|
raise ValueError( # pragma: no-cover
|
2020-02-07 03:01:21 +00:00
|
|
|
f"Attempting to stop recording an action ({action_name}) which was never started."
|
|
|
|
)
|
|
|
|
pr.disable()
|
|
|
|
|
2020-03-31 12:57:48 +00:00
|
|
|
def summary(self) -> str:
|
|
|
|
recorded_stats = {}
|
2020-02-07 03:01:21 +00:00
|
|
|
for action_name, pr in self.profiled_actions.items():
|
|
|
|
s = io.StringIO()
|
2020-02-16 04:43:43 +00:00
|
|
|
ps = pstats.Stats(pr, stream=s).strip_dirs().sort_stats('cumulative')
|
2020-02-07 03:01:21 +00:00
|
|
|
ps.print_stats(self.line_count_restriction)
|
2020-03-31 12:57:48 +00:00
|
|
|
recorded_stats[action_name] = s.getvalue()
|
|
|
|
|
|
|
|
# log to standard out
|
|
|
|
output_string = f"{os.linesep}Profiler Report{os.linesep}"
|
|
|
|
for action, stats in recorded_stats.items():
|
2021-02-08 19:28:38 +00:00
|
|
|
output_string += f"{os.linesep}Profile stats for: {action}{os.linesep}{stats}"
|
2020-03-31 12:57:48 +00:00
|
|
|
|
|
|
|
return output_string
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
"""Logs a profile report after the conclusion of the training run."""
|
|
|
|
super().describe()
|
|
|
|
if self.output_file:
|
|
|
|
self.output_file.flush()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
"""Close profiler's stream."""
|
|
|
|
if self.output_file:
|
|
|
|
self.output_file.close()
|