a
    dh                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZ ddlZddlmZ eeZeeeeeef Ze
e Zeeeef 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)defaultdict)Path)DictList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eeef dddZedddZedddZ  ZS )SimpleProfilerzThis 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.NT)dirpathfilenameextendedreturnc                    s4   t  j||d i | _tt| _|| _t | _	dS )a9  
        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.
        )r   r   N)
super__init__current_actionsr   listrecorded_durationsr   time	monotonic
start_time)selfr   r   r   	__class__ k/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/profilers/simple.pyr   &   s
    
zSimpleProfiler.__init__)action_namer   c                 C   s,   || j v rtd| dt | j |< d S )NzAttempted to start z which has already started.)r   
ValueErrorr   r   )r   r   r   r   r   startC   s    
zSimpleProfiler.startc                 C   sJ   t  }|| jvr"td| d| j|}|| }| j| | d S )Nz(Attempting to stop recording an action (z) which was never started.)r   r   r   r   popr   append)r   r   Zend_timer   durationr   r   r   stopH   s    
zSimpleProfiler.stop)r   c                    sT   t  | j   fdd| j D }|jdd dd tdd |D }|| fS )	Nc              
      s<   g | ]4\}}|t |t|t |d t |   fqS )g      Y@)npmeanlensum).0adtotal_durationr   r   
<listcomp>R   s   z8SimpleProfiler._make_report_extended.<locals>.<listcomp>c                 S   s   | d S )N   r   xr   r   r   <lambda>V       z6SimpleProfiler._make_report_extended.<locals>.<lambda>Tkeyreversec                 s   s   | ]}|d  V  qdS )   Nr   )r'   r/   r   r   r   	<genexpr>W   r1   z7SimpleProfiler._make_report_extended.<locals>.<genexpr>)r   r   r   r   itemssortr&   )r   reporttotal_callsr   r*   r   _make_report_extendedP   s    
z$SimpleProfiler._make_report_extendedc                 C   s*   dd | j  D }|jdd dd |S )Nc                 S   s&   g | ]\}}|t |t |fqS r   )r#   r$   r&   )r'   actionr)   r   r   r   r,   [   r1   z/SimpleProfiler._make_report.<locals>.<listcomp>c                 S   s   | d S )N   r   r.   r   r   r   r0   \   r1   z-SimpleProfiler._make_report.<locals>.<lambda>Tr2   )r   r7   r8   )r   r9   r   r   r   _make_reportZ   s    zSimpleProfiler._make_reportc              	      s  t jd}| jd ur(|| j  d7 }|d 7 }| jr2t| jdkrtdd | j D  t	t	t	t	t	t	d fdd	}|d
dddd}t|
 } d|  }||| | 7 }|  \}}}||dd| |dd7 }||7 }|D ]0\}	}
}}}|||	|
d| |d|d7 }q||7 }ntdd | jD  t	t	t	t	d fdd}|d
dd}t|
 } d|  }||| | 7 }|  }|D ]$\}	}
}|||	|
d|d7 }q||7 }|7 }|S )N  zProfiler Reportr   c                 s   s   | ]}t |V  qd S Nr%   r'   kr   r   r   r6   i   r1   z)SimpleProfiler.summary.<locals>.<genexpr>)r<   r$   	num_callstotalperr   c                    sH    d| d  dd|dd}|d|dd|dd|dd7 }|S )N|  <s	|  <15	|z  r   )r<   r$   rE   rF   rG   rowZmax_keysepr   r   log_row_extendedk   s    ""z0SimpleProfiler.summary.<locals>.log_row_extendedActionzMean duration (s)z	Num callszTotal time (s)zPercentage %-ZTotalz.5z100 %c                 s   s   | ]}t |V  qd S rA   rB   rC   r   r   r   r6      r1   )r<   r$   rF   r   c                    s*    d| d  dd|dd|ddS )NrH   rI   rJ   rK   rL   rM   r   )r<   r$   rF   rO   r   r   log_row   s    z'SimpleProfiler.summary.<locals>.log_row)oslinesepZ_stageupperr   r%   r   maxkeysstr
expandtabsr;   r>   )r   Zoutput_stringrQ   Zheader_stringZoutput_string_lenZ	sep_linesZreport_extendedr:   r+   r<   Zmean_durationrE   Zduration_perrT   r9   r   rO   r   summary_   sN    


zSimpleProfiler.summary)NNT)__name__
__module____qualname____doc__r   r   rZ   r   boolr   r   r"   r   _TABLE_DATA_EXTENDEDfloatr;   _TABLE_DATAr>   r\   __classcell__r   r   r   r   r
   "   s      
r
   )r`   loggingrU   r   collectionsr   pathlibr   typingr   r   r   r   r   numpyr#   Z$pytorch_lightning.profilers.profilerr	   	getLoggerr]   logrZ   rc   intZ_TABLE_ROW_EXTENDEDrb   Z
_TABLE_ROWrd   r
   r   r   r   r   <module>   s   
