Ë
    ÿÍ:jP  ã                   óÚ   — 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 ddl	m
Z
mZ ddlmZ ddlmZ  ej                   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	e«      Zy)
z<Profiler to check if there are any bottlenecks in your code.é    N)Údefaultdict)ÚPath)ÚOptionalÚUnion)Úoverride)ÚProfilerc            	       óÀ   ‡ — e Zd ZdZ	 	 	 ddeeeef      dee   deddfˆ fd„Z	e
deddfd	„«       Ze
deddfd
„«       Zdeeeef   fd„Zdefd„Ze
defd„«       Zˆ xZS )ÚSimpleProfilerz©This 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.NÚdirpathÚfilenameÚextendedÚreturnc                 óž   •— t         ‰| �  ||¬«       i | _        t        t        «      | _        || _        t        j                  «       | _	        y)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Úperf_counterÚ
start_time)Úselfr   r   r   Ú	__class__s       €úw/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/lightning/pytorch/profilers/simple.pyr   zSimpleProfiler.__init__(   sC   ø€ ô. 	‰Ñ °8ÐÔ<Ø13ˆÔÜ(3´DÓ(9ˆÔØ ˆŒÜ×+Ñ+Ó-ˆ�ó    Úaction_namec                 ó€   — || j                   v rt        d|› d�«      ‚t        j                  «       | j                   |<   y )NzAttempted to start z which has already started.)r   Ú
ValueErrorr   r   )r   r   s     r   ÚstartzSimpleProfiler.startE   s?   € à˜$×.Ñ.Ñ.ÜÐ2°;°-Ð?ZÐ[Ó\Ð\Ü,0×,=Ñ,=Ó,?ˆ×Ñ˜[Ò)r   c                 óâ   — t        j                  «       }|| j                  vrt        d|› d�«      ‚| j                  j	                  |«      }||z
  }| j
                  |   j                  |«       y )Nz(Attempting to stop recording an action (z) which was never started.)r   r   r   r   Úpopr   Úappend)r   r   Úend_timer   Údurations        r   ÚstopzSimpleProfiler.stopK   sm   € ä×$Ñ$Ó&ˆØ˜d×2Ñ2Ñ2ÜÐGÈÀ}ÐTnÐoÓpÐpØ×)Ñ)×-Ñ-¨kÓ:ˆ
Ø˜jÑ(ˆØ×Ñ Ñ,×3Ñ3°HÕ=r   c                 ód  — t        j                  «       | j                  z
  }g }| j                  j	                  «       D ]F  \  }}t        |«      }t        j                  |«      }d|z  |z  }|j                  |||z  |||f«       ŒH |j                  d„ d¬«       t        d„ |D «       «      }|||fS )Ng      Y@c                 ó   — | d   S )Né   © ©Úxs    r   ú<lambda>z6SimpleProfiler._make_report_extended.<locals>.<lambda>_   ó
   €  ! A¡$€ r   T©ÚkeyÚreversec              3   ó&   K  — | ]	  }|d    –— Œ y­w)é   Nr)   )Ú.0r+   s     r   ú	<genexpr>z7SimpleProfiler._make_report_extended.<locals>.<genexpr>`   s   è ø€ Ò/ 1˜!˜A�$Ñ/ùs   ‚)r   r   r   r   ÚitemsÚlenÚmathÚfsumr"   ÚsortÚsum)	r   Útotal_durationÚreportÚaÚdÚlen_dÚsum_dÚpercentage_dÚtotal_callss	            r   Ú_make_report_extendedz$SimpleProfiler._make_report_extendedT   s°   € Ü×*Ñ*Ó,¨t¯©Ñ>ˆØˆà×+Ñ+×1Ñ1Ó3ò 	J‰DˆAˆqÜ˜“FˆEÜ—I‘I˜a“LˆEØ  5™=¨>Ñ9ˆLà�M‰M˜1˜e e™m¨U°E¸<ÐHÕIð	Jð 	�‰™°ˆÔ5ÜÑ/¨Ô/Ó/ˆØ�{ NÐ2Ð2r   c                 óà   — g }| j                   j                  «       D ]:  \  }}t        j                  |«      }|j	                  ||t        |«      z  |f«       Œ< |j                  d„ d¬«       |S )Nc                 ó   — | d   S )Né   r)   r*   s    r   r,   z-SimpleProfiler._make_report.<locals>.<lambda>j   r-   r   Tr.   )r   r5   r7   r8   r"   r6   r9   )r   r<   Úactionr>   r@   s        r   Ú_make_reportzSimpleProfiler._make_reportc   sh   € ØˆØ×0Ñ0×6Ñ6Ó8ò 	;‰IˆF�AÜ—I‘I˜a“LˆEà�M‰M˜6 5¬3¨q«6¡>°5Ð9Õ:ð	;ð
 	�‰™°ˆÔ5Øˆr   c                 óÚ  ‡‡— t         j                  Šd}| j                  � || j                  j                  «       › d�z  }|d‰› �z  }| j                  rît        | j                  «      dkD  �rxt        d„ | j                  D «       «      Šdt        dt        dt        d	t        d
t        dt        fˆˆfd„} |ddddd«      }t        |j                  «       «      }‰› d|z  › �}|||z   |z   z  }| j                  «       \  }}}| |dd|� ›|d›d«      z  }||z  }|D ]  \  }	}
}}}| ||	|
d›|› |d›|d›«      z  }Œ  ||z  }n£t        d„ | j                  D «       «      Šdt        dt        d	t        dt        fˆˆfd„} |ddd«      }t        |j                  «       «      }‰› d|z  › �}|||z   |z   z  }| j                  «       }|D ]  \  }	}
}| ||	|
d›|d›«      z  }Œ ||z  }|‰z  }|S )NÚ ú zProfiler Reportr   c              3   ó2   K  — | ]  }t        |«      –— Œ y ­w©N©r6   ©r3   Úks     r   r4   z)SimpleProfiler.summary.<locals>.<genexpr>w   s   è ø€ ÒF¨œc !ŸfÑFùó   ‚rG   ÚmeanÚ	num_callsÚtotalÚperr   c                 óN   •— ‰› d| d‰› d�›d|d›d�}|d|d›d|d›d|d›d�z  }|S )Nú|  ú<Úsú	|  ú<15ú	|z  r)   )rG   rR   rS   rT   rU   ÚrowÚmax_keyÚseps         €€r   Úlog_row_extendedz0SimpleProfiler.summary.<locals>.log_row_extendedy   sR   ø€ Ø ˜E  V¨A¨g¨Y°a¨<Ð$8¸¸dÀ3¸ZÀsÐK�CØ˜R 	¨#˜¨e°E¸#°;¸eÀCÈÀ9ÈCÐPÑP�CØ�Jr   ÚActionzMean duration (s)z	Num callszTotal time (s)zPercentage %ú-ÚTotalz.5z100 %c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wrM   rN   rO   s     r   r4   z)SimpleProfiler.summary.<locals>.<genexpr>’   s   è ø€ ÒB Qœ#˜aŸ&ÑBùrQ   c                 ó.   •— ‰› d| d‰› d�›d|d›d|d›d�S )NrW   rX   rY   rZ   r[   r\   r)   )rG   rR   rT   r^   r_   s      €€r   Úlog_rowz'SimpleProfiler.summary.<locals>.log_row”   s3   ø€ Ø˜˜c &¨¨7¨)°1¨Ð!5°U¸4À¸*ÀEÈ%ÐPSÈÐTWÐXÐXr   )ÚosÚlinesepÚ_stageÚupperr   r6   r   ÚmaxÚstrÚ
expandtabsrC   rH   )r   Úoutput_stringr`   Úheader_stringÚoutput_string_lenÚ	sep_linesÚreport_extendedrB   r;   rG   Úmean_durationrS   Úduration_perrf   r<   r^   r_   s                  @@r   ÚsummaryzSimpleProfiler.summarym   sŠ  ù€ ä�j‰jˆØˆØ�;‰;Ð"Ø §¡× 1Ñ 1Ó 3Ð4°AÐ6Ñ6ˆMØ˜?¨3¨%Ð0Ñ0ˆà�=Š=Ü�4×*Ñ*Ó+¨aÓ/ÜÑF¨d×.EÑ.EÔFÓF�ð¬Sð ¼ð Ìð ÔTWð Ô^að Ôfiö ñ
 !1ØÐ1°;Ð@PÐR`ó!�ô %(¨×(@Ñ(@Ó(BÓ$CÐ!Ø"˜e CÐ*;Ñ$;Ð#<Ð=�	Ø ¨]Ñ!:¸YÑ!FÑF�à?C×?YÑ?YÓ?[Ñ<� ¨nØÑ!1°'¸3À;ÈaÀ.ÐVdÐegÐUhÐkrÓ!sÑs�Ø Ñ*�ØVeò ÑR�F˜M¨9°nÀlØ!Ñ%5ØØ(¨Ð,Ø$˜+Ø)¨"Ð-Ø'¨Ð+ó&ñ ‘Mðð  Ñ*‘äÑB¨$×*AÑ*AÔBÓBˆGðY¤ð Y¬3ð Y´sð Y¼sö Yñ $ HÐ.AÐCSÓTˆMÜ # M×$<Ñ$<Ó$>Ó ?ÐØ˜% Ð&7Ñ 7Ð8Ð9ˆIØ˜Y¨Ñ6¸ÑBÑBˆMØ×&Ñ&Ó(ˆFà9?ò `Ñ5�˜ ~Ø¡¨°MÀ"Ð3EÈ>ÐZ\ÐJ]Ó!_Ñ_‘ð`à˜YÑ&ˆMØ˜ÑˆØÐr   )NNT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   rl   r   Úboolr   r   r   r%   ÚtupleÚ_TABLE_DATA_EXTENDEDÚfloatrC   Ú_TABLE_DATArH   ru   Ú__classcell__)r   s   @r   r
   r
   $   sØ   ø„ ñ>ð
 /3Ø"&Øñ	.à˜%  T 	Ñ*Ñ+ð.ð ˜3‘-ð.ð ð	.ð
 
õ.ð: ð@ ð @¨ò @ó ð@ð
 ð> ð >¨ò >ó ð>ð3 uÐ-AÀ5È%Ð-OÑ'Pó 3ð˜kó ð ð3˜ò 3ó ô3r   r
   )ry   Úloggingr7   rg   r   Úcollectionsr   Úpathlibr   Útypingr   r   Útyping_extensionsr   Ú$lightning.pytorch.profilers.profilerr   Ú	getLoggerrv   Úlogr{   rl   r}   ÚintÚ_TABLE_ROW_EXTENDEDr   r|   Ú
_TABLE_ROWr~   r
   r)   r   r   ú<module>r‹      sƒ   ðñ Cã Û Û 	Û Ý #Ý ß "å &å 9à€g×Ñ˜Ó!€à˜C ¨¨U°EÐ9Ñ:Ð ØÐ/Ñ0Ð Ø�3˜˜uÐ$Ñ%€
Ø�:Ñ€ô}�Xõ }r   