Ë
    ÿÍ:j[   ã                   óà   — d 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	 ddl
mZ ddlZddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ  ej0                  e«      Z G d„ de«      Z G d„ de«      Zy)z
Timer
^^^^^
é    N)Ú	timedelta)ÚAnyÚOptionalÚUnion)Úoverride)ÚCallback)ÚRunningStage)ÚLightningEnum)ÚMisconfigurationException)Úrank_zero_infoc                   ó   — e Zd ZdZdZy)ÚIntervalÚstepÚepochN)Ú__name__Ú
__module__Ú__qualname__r   r   © ó    úv/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/pytorch_lightning/callbacks/timer.pyr   r   %   s   „ Ø€DØ�Er   r   c            	       ó<  ‡ — e Zd ZdZdej
                  dfdeeee	e
eef   f      dededdfˆ fd„Zej                  fd	edee   fd
„Zej                  fd	edee   fd„Zej                  fd	edefd„Zej                  fd	edee   fd„Zedd„«       Zedd„«       Zedd„«       Zedd„«       Zedd„«       Zedd„«       Zedddededdfd„«       Zedddededdfd„«       Zedddededdfd„«       Zede
eef   fd„«       Z ede
eef   ddfd„«       Z!d d„Z"ˆ xZ#S )!ÚTimeraß  The Timer callback tracks the time spent in the training, validation, and test loops and interrupts the Trainer
    if the given time limit for the training loop is reached.

    Args:
        duration: A string in the format DD:HH:MM:SS (days, hours, minutes seconds), or a :class:`datetime.timedelta`,
            or a dict containing key-value compatible with :class:`~datetime.timedelta`.
        interval: Determines if the interruption happens on epoch level or mid-epoch.
            Can be either ``"epoch"`` or ``"step"``.
        verbose: Set this to ``False`` to suppress logging messages.

    Raises:
        MisconfigurationException:
            If ``duration`` is not in the expected format.
        MisconfigurationException:
            If ``interval`` is not one of the supported choices.

    Example::

        from pytorch_lightning import Trainer
        from pytorch_lightning.callbacks import Timer

        # stop training after 12 hours
        timer = Timer(duration="00:12:00:00")

        # or provide a datetime.timedelta
        from datetime import timedelta
        timer = Timer(duration=timedelta(weeks=1))

        # or provide a dictionary
        timer = Timer(duration=dict(weeks=4, days=2))

        # force training to stop after given time limit
        trainer = Trainer(callbacks=[timer])

        # query training/validation/test time (in seconds)
        timer.time_elapsed("train")
        timer.start_time("validate")
        timer.end_time("test")

    NTÚdurationÚintervalÚverboseÚreturnc           
      ó  •— t         ‰| �  «        t        |t        «      r¥t	        j
                  d|j                  «       «      }|st        d|›d�«      ‚t        t        |j                  d«      «      t        |j                  d«      «      t        |j                  d«      «      t        |j                  d«      «      ¬«      }nt        |t        «      rt        di |¤Ž}|t        t        «      vr-t        d	|› d
dj                  t        t        «      «      › �«      ‚|�|j                  «       nd | _        || _        || _        t        j'                  t(        «      | _        t        j'                  t(        «      | _        d| _        y )Nz(\d+):(\d\d):(\d\d):(\d\d)z`Timer(duration=zH)` is not a valid duration. Expected a string in the format DD:HH:MM:SS.é   é   é   é   )ÚdaysÚhoursÚminutesÚsecondsz,Unsupported parameter value `Timer(interval=z)`. Possible choices are: z, r   r   )ÚsuperÚ__init__Ú
isinstanceÚstrÚreÚ	fullmatchÚstripr   r   ÚintÚgroupÚdictÚsetr   ÚjoinÚtotal_secondsÚ	_durationÚ	_intervalÚ_verboseÚfromkeysr	   Ú_start_timeÚ	_end_timeÚ_offset)Úselfr   r   r   Úduration_matchÚ	__class__s        €r   r'   zTimer.__init__T   sS  ø€ ô 	‰ÑÔÜ�h¤Ô$ÜŸ\™\Ð*GÈÏÉÓIYÓZˆNÙ!Ü/Ø& x lð 3Cð Cóð ô !Ü˜×-Ñ-¨aÓ0Ó1Ü˜.×.Ñ.¨qÓ1Ó2Ü˜N×0Ñ0°Ó3Ó4Ü˜N×0Ñ0°Ó3Ó4ô	‰Hô ˜¤$Ô'Ü Ñ, 8Ñ,ˆHØœ3œx›=Ñ(Ü+Ø>¸x¸jð IØ—I‘Iœc¤(›mÓ,Ð-ð/óð ð 6>Ð5I˜×/Ñ/Ô1ÈtˆŒØ!ˆŒØˆŒÜ@DÇÁÌlÓ@[ˆÔÜ>B¿m¹mÌLÓ>YˆŒØˆ�r   Ústagec                 ó6   — t        |«      }| j                  |   S )z8Return the start time of a particular stage (in seconds))r	   r7   ©r:   r=   s     r   Ú
start_timezTimer.start_timev   s   € ä˜UÓ#ˆØ×Ñ Ñ&Ð&r   c                 ó6   — t        |«      }| j                  |   S )z6Return the end time of a particular stage (in seconds))r	   r8   r?   s     r   Úend_timezTimer.end_time{   s   € ä˜UÓ#ˆØ�~‰~˜eÑ$Ð$r   c                 óØ   — | j                  |«      }| j                  |«      }|t        j                  k(  r| j                  nd}|€|S |€t        j                  «       |z
  |z   S ||z
  |z   S )z;Return the time elapsed for a particular stage (in seconds)r   )r@   rB   r	   ÚTRAININGr9   ÚtimeÚ	monotonic)r:   r=   ÚstartÚendÚoffsets        r   Útime_elapsedzTimer.time_elapsed€   sk   € à—‘ Ó&ˆØ�m‰m˜EÓ"ˆØ!&¬,×*?Ñ*?Ò!?�—’ÀQˆØˆ=ØˆMØˆ;Ü—>‘>Ó# eÑ+¨fÑ4Ð4Ø�U‰{˜VÑ#Ð#r   c                 óX   — | j                   �| j                   | j                  |«      z
  S y)z=Return the time remaining for a particular stage (in seconds)N)r3   rJ   r?   s     r   Útime_remainingzTimer.time_remaining‹   s)   € à�>‰>Ð%Ø—>‘> D×$5Ñ$5°eÓ$<Ñ<Ð<Ør   Útrainerú
pl.Trainerc                 ób   — t        j                  «       | j                  t        j                  <   y ©N)rE   rF   r7   r	   rD   ©r:   rM   Ú	pl_modules      r   Úon_train_startzTimer.on_train_start‘   s   € ä26·.±.Ó2Bˆ×Ñœ×.Ñ.Ò/r   c                 ób   — t        j                  «       | j                  t        j                  <   y rP   )rE   rF   r8   r	   rD   rQ   s      r   Úon_train_endzTimer.on_train_end•   s   € ä04·±Ó0@ˆ�‰”|×,Ñ,Ò-r   c                 ób   — t        j                  «       | j                  t        j                  <   y rP   )rE   rF   r7   r	   Ú
VALIDATINGrQ   s      r   Úon_validation_startzTimer.on_validation_start™   s   € ä48·N±NÓ4Dˆ×Ñœ×0Ñ0Ò1r   c                 ób   — t        j                  «       | j                  t        j                  <   y rP   )rE   rF   r8   r	   rW   rQ   s      r   Úon_validation_endzTimer.on_validation_end�   s   € ä26·.±.Ó2Bˆ�‰”|×.Ñ.Ò/r   c                 ób   — t        j                  «       | j                  t        j                  <   y rP   )rE   rF   r7   r	   ÚTESTINGrQ   s      r   Úon_test_startzTimer.on_test_start¡   s   € ä15·±Ó1Aˆ×Ñœ×-Ñ-Ò.r   c                 ób   — t        j                  «       | j                  t        j                  <   y rP   )rE   rF   r8   r	   r\   rQ   s      r   Úon_test_endzTimer.on_test_end¥   s   € ä/3¯~©~Ó/?ˆ�‰”|×+Ñ+Ò,r   ÚargsÚkwargsc                 ó@   — | j                   €y | j                  |«       y rP   )r3   Ú_check_time_remaining©r:   rM   r`   ra   s       r   Úon_fit_startzTimer.on_fit_start©   s   € ð �>‰>Ð!ØØ×"Ñ" 7Õ+r   c                 óz   — | j                   t        j                  k7  s| j                  €y | j	                  |«       y rP   )r4   r   r   r3   rc   rd   s       r   Úon_train_batch_endzTimer.on_train_batch_end±   s,   € à�>‰>œXŸ]™]Ò*¨d¯n©nÐ.DØØ×"Ñ" 7Õ+r   c                 óz   — | j                   t        j                  k7  s| j                  €y | j	                  |«       y rP   )r4   r   r   r3   rc   rd   s       r   Úon_train_epoch_endzTimer.on_train_epoch_end·   s,   € à�>‰>œXŸ^™^Ò+¨t¯~©~Ð/EØØ×"Ñ" 7Õ+r   c                 ól   — dt         D �ci c]  }|j                  | j                  |«      “Œ  c}iS c c}w )NrJ   )r	   ÚvaluerJ   r?   s     r   Ú
state_dictzTimer.state_dict½   s0   € àÔT`Ö aÈ5 §¡¨d×.?Ñ.?ÀÓ.FÑ!FÒ aÐbÐbùÒ as   Š#1rl   c                 ó†   — |j                  di «      }|j                  t        j                  j                  d«      | _        y )NrJ   r   )Úgetr	   rD   rk   r9   )r:   rl   rJ   s      r   Úload_state_dictzTimer.load_state_dictÁ   s2   € à!—~‘~ n°bÓ9ˆØ#×'Ñ'¬×(=Ñ(=×(CÑ(CÀQÓGˆ�r   c                 ó\  — | j                   €J ‚| j                  «       | j                   k\  }|j                  j                  |«      }|j                  xs ||_        |rO| j
                  rBt        t        | j                  t        j                  «      «      ¬«      }t        d|› d�«       y y y )N)r%   z$Time limit reached. Elapsed time is z. Signaling Trainer to stop.)r3   rJ   ÚstrategyÚ	broadcastÚshould_stopr5   r   r-   r	   rD   r   )r:   rM   rs   Úelapseds       r   rc   zTimer._check_time_remainingÆ   s•   € Ø�~‰~Ð)Ð)Ð)Ø×'Ñ'Ó)¨T¯^©^Ñ;ˆØ×&Ñ&×0Ñ0°Ó=ˆØ%×1Ñ1Ò@°[ˆÔÙ˜4Ÿ=š=Ü¬¨D×,=Ñ,=¼l×>SÑ>SÓ,TÓ(UÔVˆGÜÐAÀ'ÀÐJfÐgÕhð )ˆ;r   )rM   rN   rR   zpl.LightningModuler   N)rM   rN   r   N)$r   r   r   Ú__doc__r   r   r   r   r)   r   r/   r-   Úboolr'   r	   rD   Úfloatr@   rB   rJ   rL   r   rS   rU   rX   rZ   r]   r_   r   re   rg   ri   rl   ro   rc   Ú__classcell__)r<   s   @r   r   r   *   sT  ø„ ñ'ðV EIØ Ÿ™Øñ	 à˜5  i°°c¸3°h±Ð!?Ñ@ÑAð ð ð ð ð	 ð
 
õ ðD '3×&;Ñ&;ñ ' ð 'ÀÈÁó 'ð
 %1×$9Ñ$9ñ %˜cð %¸hÀu¹oó %ð
 )5×(=Ñ(=ñ 	$ #ð 	$À%ó 	$ð +7×*?Ñ*?ñ  Cð ÀHÈUÁOó ð òCó ðCð òAó ðAð òEó ðEð òCó ðCð òBó ðBð ò@ó ð@ð ð, Lð ,¸ð ,Èð ,ÐPTò ,ó ð,ð ð,¨,ð ,¸sð ,Ècð ,ÐVZò ,ó ð,ð
 ð,¨,ð ,¸sð ,Ècð ,ÐVZò ,ó ð,ð
 ðc˜D  c ™Nò có ðcð ðH¨$¨s°C¨x©.ð H¸Tò Hó ðH÷ir   r   )ru   Úloggingr*   rE   Údatetimer   Útypingr   r   r   Útyping_extensionsr   Úpytorch_lightningÚplÚ$pytorch_lightning.callbacks.callbackr   Ú pytorch_lightning.trainer.statesr	   Úpytorch_lightning.utilitiesr
   Ú&pytorch_lightning.utilities.exceptionsr   Ú%pytorch_lightning.utilities.rank_zeror   Ú	getLoggerr   Úlogr   r   r   r   r   ú<module>r†      sa   ðñó
 Û 	Û Ý ß 'Ñ 'å &ã Ý 9Ý 9Ý 5Ý LÝ @à€g×Ñ˜Ó!€ôˆ}ô ô
ciˆHõ cir   