a
    d                     @   sr   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddlmZ eeZG dd deZdS )z<Profiler to check if there are any bottlenecks in your code.    N)Path)DictOptionalTupleUnion)Profilerc                       s   e Zd ZdZdeeeef  ee edd fddZ	edddd	Z
eddd
dZedddZee dd fddZedddZ  ZS )AdvancedProfilerzThis 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.
    N      ?)dirpathfilenameline_count_restrictionreturnc                    s    t  j||d i | _|| _dS )ac  
        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.
        )r
   r   N)super__init__profiled_actionsr   )selfr
   r   r   	__class__ m/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/profilers/advanced.pyr   "   s    zAdvancedProfiler.__init__)action_namer   c                 C   s*   || j vrt | j |< | j |   d S )N)r   cProfileZProfileenable)r   r   r   r   r   start=   s    
zAdvancedProfiler.startc                 C   s0   | j |}|d u r$td| d|  d S )Nz(Attempting to stop recording an action (z) which was never started.)r   get
ValueErrordisable)r   r   prr   r   r   stopB   s    zAdvancedProfiler.stop)r   c                 C   sZ   i }| j  D ]@\}}t }tj||d d}|| j	 |
 ||< q| |S )N)streamZ
cumulative)r   itemsioStringIOpstatsZStatsZ
strip_dirsZ
sort_statsZprint_statsr   getvalueZ_stats_to_str)r   Zrecorded_statsr   r   sZpsr   r   r   summaryH   s    zAdvancedProfiler.summary)stager   c                    s   t  j|d i | _d S )N)r'   )r   teardownr   )r   r'   r   r   r   r(   Q   s    zAdvancedProfiler.teardownc                 C   s   | j dt| j| j| jdfS )Nr   )r
   r   r   )r   dictr
   r   r   )r   r   r   r   
__reduce__U   s    zAdvancedProfiler.__reduce__)NNr	   )__name__
__module____qualname____doc__r   r   strr   floatr   r   r   r&   r(   r   r*   __classcell__r   r   r   r   r      s      	r   )r.   r   r!   loggingr#   pathlibr   typingr   r   r   r   Z$pytorch_lightning.profilers.profilerr   	getLoggerr+   logr   r   r   r   r   <module>   s   
