Source code for pytorch_lightning.profiler.advanced
# 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."""importcProfileimportioimportloggingimportpstatsfrompathlibimportPathfromtypingimportDict,Optional,Unionfrompytorch_lightning.profiler.profilerimportProfilerlog=logging.getLogger(__name__)
[docs]classAdvancedProfiler(Profiler):"""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. """def__init__(self,dirpath:Optional[Union[str,Path]]=None,filename:Optional[str]=None,line_count_restriction:float=1.0,)->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. 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) Raises: ValueError: 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.line_count_restriction=line_count_restriction
[docs]defstop(self,action_name:str)->None:pr=self.profiled_actions.get(action_name)ifprisNone:raiseValueError(f"Attempting to stop recording an action ({action_name}) which was never started.")pr.disable()
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.