o
    8wi                     @   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
 ddlZddlm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)OptionalUnion)override)Profilerc                	       s   e Zd ZdZ			ddeeeef  dee deddf fdd	Z	e
d
eddfddZe
d
eddfddZdeeeef fddZdefddZe
def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__ _/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pytorch_lightning/profilers/simple.pyr   (   s
   
zSimpleProfiler.__init__action_namec                 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   startE   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   end_timer   durationr   r   r   stopK   s   
zSimpleProfiler.stopc           
      C   s   t  | j }g }| j D ]&\}}t|}t|}t|	 }d| | }|
||| |||f q|jdd dd tdd |D }	||	|fS )Ng      Y@c                 S      | d S )N   r   xr   r   r   <lambda>`       z6SimpleProfiler._make_report_extended.<locals>.<lambda>Tkeyreversec                 s   s    | ]}|d  V  qdS )   Nr   ).0r%   r   r   r   	<genexpr>a       z7SimpleProfiler._make_report_extended.<locals>.<genexpr>)r   r   r   r   itemstorchtensorlensumitemr   sort)
r   total_durationreportadd_tensorlen_dsum_dpercentage_dtotal_callsr   r   r   _make_report_extendedT   s   

z$SimpleProfiler._make_report_extendedc                 C   s^   g }| j  D ]\}}t|}t| }|||t| |f q|jdd dd |S )Nc                 S   r"   )N   r   r$   r   r   r   r&   l   r'   z-SimpleProfiler._make_report.<locals>.<lambda>Tr(   )	r   r/   r0   r1   r3   r4   r   r2   r5   )r   r7   actionr9   r:   r<   r   r   r   _make_reportd   s   
zSimpleProfiler._make_reportc                    s  t jd}| jd ur|| j  d7 }|d 7 }| jrt| jdkrtdd | jD  dtdtd	td
tdtdtf fdd}|ddddd}t|	 } d|  }||| | 7 }| 
 \}}}||dd| |dd7 }||7 }|D ]\}	}
}}}|||	|
d| |d|d7 }q|||7 }nRtdd | jD  dtdtd
tdtf fdd}|ddd}t|	 } d|  }||| | 7 }|  }|D ]\}	}
}|||	|
d|d7 }q||7 }|7 }|S )N  zProfiler Reportr   c                 s       | ]}t |V  qd S Nr2   r,   kr   r   r   r-   y   r.   z)SimpleProfiler.summary.<locals>.<genexpr>rA   mean	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   )rA   rJ   rK   rL   rM   rowmax_keysepr   r   log_row_extended{   s   ""z0SimpleProfiler.summary.<locals>.log_row_extendedActionzMean duration (s)z	Num callszTotal time (s)zPercentage %-Totalz.5z100 %c                 s   rE   rF   rG   rH   r   r   r   r-      r.   c                    s*    d| d  dd|dd|ddS )NrN   rO   rP   rQ   rR   rS   r   )rA   rJ   rL   rU   r   r   log_row   s   *z'SimpleProfiler.summary.<locals>.log_row)oslinesep_stageupperr   r2   r   maxstr
expandtabsr?   rB   )r   output_stringrX   header_stringoutput_string_len	sep_linesreport_extendedr>   r6   rA   mean_durationrK   duration_perr\   r7   r   rU   r   summaryo   sP   
(
 zSimpleProfiler.summary)NNT)__name__
__module____qualname____doc__r   r   rb   r   boolr   r   r   r!   tuple_TABLE_DATA_EXTENDEDfloatr?   _TABLE_DATArB   rk   __classcell__r   r   r   r   r   $   s,    r   )ro   loggingr]   r   collectionsr   pathlibr   typingr   r   r0   typing_extensionsr   $pytorch_lightning.profilers.profilerr   	getLoggerrl   logrq   rb   rs   int_TABLE_ROW_EXTENDEDr   rr   
_TABLE_ROWrt   r   r   r   r   r   <module>   s    
