o
    9wi                     @   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 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)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:`~lightning.pytorch.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/lightning/pytorch/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   r2   r3   r4   r6   r7   r!   r5   r8   )r   r:   actionr<   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r5   r/   kr   r   r   r0   y   r1   z)SimpleProfiler.summary.<locals>.<genexpr>rD   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   )rD   rM   rN   rO   rP   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   rH   rI   rJ   rK   r   r   r   r0      r1   c                    s*    d| d  dd|dd|ddS )NrQ   rR   rS   rT   rU   rV   r   )rD   rM   rO   rX   r   r   log_row   s   *z'SimpleProfiler.summary.<locals>.log_row)oslinesep_stageupperr   r5   r   maxstr
expandtabsrB   rE   )r   output_stringr[   header_stringoutput_string_len	sep_linesreport_extendedrA   r9   rD   mean_durationrN   duration_perr_   r:   r   rX   r   summaryo   sP   
(
 zSimpleProfiler.summary)NNT)__name__
__module____qualname____doc__r   r   re   r   boolr   r	   r   r$   r   _TABLE_DATA_EXTENDEDfloatrB   _TABLE_DATArE   rn   __classcell__r   r   r   r   r   $   s,    r   )rr   loggingr`   r   collectionsr   pathlibr   typingr   r   r   r   r   r3   typing_extensionsr	   $lightning.pytorch.profilers.profilerr
   	getLoggerro   logre   ru   int_TABLE_ROW_EXTENDEDrt   
_TABLE_ROWrv   r   r   r   r   r   <module>   s    
