# 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."""Profiler to check if there are any bottlenecks in your code."""importloggingimportosimporttimefromcollectionsimportdefaultdictfrompathlibimportPathfromtypingimportDict,List,Optional,Tuple,Unionimportnumpyasnpfrompytorch_lightning.profiler.profilerimportProfilerlog=logging.getLogger(__name__)_TABLE_ROW_EXTENDED=Tuple[str,float,int,float,float]_TABLE_DATA_EXTENDED=List[_TABLE_ROW_EXTENDED]_TABLE_ROW=Tuple[str,float,float]_TABLE_DATA=List[_TABLE_ROW]
[docs]classSimpleProfiler(Profiler):"""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."""def__init__(self,dirpath:Optional[Union[str,Path]]=None,filename:Optional[str]=None,extended:bool=True,)->None:""" Args: dirpath: Directory path for the ``filename``. If ``dirpath`` is ``None`` but ``filename`` is present, the ``trainer.log_dir`` (from :class:`~pytorch_lightning.loggers.tensorboard.TensorBoardLogger`) will be used. filename: If present, filename where the profiler results will be saved instead of printing to stdout. The ``.txt`` extension will be used automatically. extended: If ``True``, adds extra columns representing number of calls and percentage of total time spent on respective action. 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. """super().__init__(dirpath=dirpath,filename=filename)self.current_actions:Dict[str,float]={}self.recorded_durations=defaultdict(list)self.extended=extendedself.start_time=time.monotonic()
[docs]defstart(self,action_name:str)->None:ifaction_nameinself.current_actions:raiseValueError(f"Attempted to start {action_name} which has already started.")self.current_actions[action_name]=time.monotonic()
[docs]defstop(self,action_name:str)->None:end_time=time.monotonic()ifaction_namenotinself.current_actions:raiseValueError(f"Attempting to stop recording an action ({action_name}) which was never started.")start_time=self.current_actions.pop(action_name)duration=end_time-start_timeself.recorded_durations[action_name].append(duration)
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. Read PyTorch Lightning's Privacy Policy.