§
    “©ºj4  ã                   óÚ   — d Z ddlZddlmZ ddlmZmZmZ ddlZddl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mZ  ej        e¦  «        Z G d
„ de¦  «        Z G d„ de¦  «        ZdS )z]
Early Stopping
^^^^^^^^^^^^^^

Monitor a metric and stop training when it stops improving.

é    N)ÚEnum)ÚAnyÚCallableÚOptional)ÚTensor)Úoverride)ÚCallback)ÚMisconfigurationException)Úrank_prefixed_messageÚrank_zero_warnc                   ó&   — e Zd ZdZdZdZdZdZdZdS )ÚEarlyStoppingReasonz Enum for early stopping reasons.r   é   é   é   é   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚNOT_STOPPEDÚSTOPPING_THRESHOLDÚDIVERGENCE_THRESHOLDÚPATIENCE_EXHAUSTEDÚNON_FINITE_METRIC© ó    úz/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/callbacks/early_stopping.pyr   r   &   s3   € € € € € Ø*Ð*à€KØÐØÐØÐØÐÐÐr   r   c                   óJ  ‡ — e Zd ZdZej        ej        dœZdddœZ	 	 	 	 	 	 	 	 	 	 d.de	de
dedede	dededee
         dee
         dee         defˆ fd„Zeede	fd„¦   «         ¦   «         Zeddddde	dd
fd„¦   «         Zdee	ef         defd „Zede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defd%„Zed/d&„¦   «         Zed/d'„¦   «         Zd0d(„Zd)edeeee	         f         fd*„Z d)ede	fd+„Z!e"ddd,e	dedd
fd-„¦   «         Z#ˆ xZ$S )1ÚEarlyStoppinga  Monitor a metric and stop training when it stops improving.

    Args:
        monitor: quantity to be monitored.
        min_delta: minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute
            change of less than or equal to `min_delta`, will count as no improvement.
        patience: number of checks with no improvement
            after which training will be stopped. Under the default configuration, one check happens after
            every training epoch. However, the frequency of validation can be modified by setting various parameters on
            the ``Trainer``, for example ``check_val_every_n_epoch`` and ``val_check_interval``.

            .. note::

                It must be noted that the patience parameter counts the number of validation checks with
                no improvement, and not the number of training epochs. Therefore, with parameters
                ``check_val_every_n_epoch=10`` and ``patience=3``, the trainer will perform at least 40 training
                epochs before being stopped.

        verbose: verbosity mode.
        mode: one of ``'min'``, ``'max'``. In ``'min'`` mode, training will stop when the quantity
            monitored has stopped decreasing and in ``'max'`` mode it will stop when the quantity
            monitored has stopped increasing.
        strict: whether to crash the training if `monitor` is not found in the validation metrics.
        check_finite: When set ``True``, stops training when the monitor becomes NaN or infinite.
        stopping_threshold: Stop training immediately once the monitored quantity reaches this threshold.
        divergence_threshold: Stop training as soon as the monitored quantity becomes worse than this threshold.
        check_on_train_epoch_end: whether to run early stopping at the end of the training epoch.
            If this is ``False``, then the check runs at the end of the validation.
        log_rank_zero_only: When set ``True``, logs the status of the early stopping callback only for rank 0 process.

    Attributes:
        stopped_epoch: The epoch at which training was stopped. 0 if training was not stopped.
        stopping_reason: An ``EarlyStoppingReason`` enum indicating why training was stopped.
        stopping_reason_message: A human-readable message explaining why training was stopped.

    Raises:
        MisconfigurationException:
            If ``mode`` is none of ``"min"`` or ``"max"``.
        RuntimeError:
            If the metric ``monitor`` is not available.

    Example::

        >>> from pytorch_lightning import Trainer
        >>> from pytorch_lightning.callbacks import EarlyStopping
        >>> from pytorch_lightning.callbacks.early_stopping import EarlyStoppingReason
        >>> early_stopping = EarlyStopping('val_loss')
        >>> trainer = Trainer(callbacks=[early_stopping])
        >>> # After training...
        >>> if early_stopping.stopping_reason == EarlyStoppingReason.PATIENCE_EXHAUSTED:
        ...     print("Training stopped due to patience exhaustion")

    .. tip:: Saving and restoring multiple early stopping callbacks at the same time is supported under variation in the
        following arguments:

        *monitor, mode*

        Read more: :ref:`Persisting Callback State <extensions/callbacks_state:save callback state>`

    )ÚminÚmaxú<ú>ç        r   Fr!   TNÚmonitorÚ	min_deltaÚpatienceÚverboseÚmodeÚstrictÚcheck_finiteÚstopping_thresholdÚdivergence_thresholdÚcheck_on_train_epoch_endÚlog_rank_zero_onlyc                 óš  •— t          ¦   «                              ¦   «          || _        || _        || _        || _        || _        || _        || _        || _	        |	| _
        d| _        d| _        t          j        | _        d | _        |
| _        || _        | j        | j        vrDt)          dd                     | j                             ¦   «         ¦  «        › d| j        › �¦  «        ‚| xj        | j        t0          j        k    rdndz  c_        t1          j        t0          j        ¦  «        }| j        t0          j        k    r|n| | _        d S )Nr   z`mode` can be z, z, got r   éÿÿÿÿ)ÚsuperÚ__init__r&   r'   r(   r)   r*   r+   r,   r-   r.   Ú
wait_countÚstopped_epochr   r   Ústopping_reasonÚstopping_reason_messageÚ_check_on_train_epoch_endr0   Ú	mode_dictr
   ÚjoinÚkeysÚ
monitor_opÚtorchÚgtÚtensorÚinfÚltÚ
best_score)Úselfr&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   Ú	torch_infÚ	__class__s                €r   r4   zEarlyStopping.__init__r   s-  ø€ õ 	‰Œ×ÒÑÔÐØˆŒØ"ˆŒØ ˆŒØˆŒØˆŒ	ØˆŒØ(ˆÔØ"4ˆÔØ$8ˆÔ!ØˆŒØˆÔÝ2Ô>ˆÔØ6:ˆÔ$Ø)AˆÔ&Ø"4ˆÔàŒ9˜DœNÐ*Ð*Ý+Ð,p¸T¿YºYÀtÄ~×GZÒGZÑG\ÔG\Ñ=]Ô=]Ð,pÐ,pÐeiÔenÐ,pÐ,pÑqÔqÐqàˆŒ˜tœµ%´(Ò:Ð:˜!˜!ÀÑBˆŒÝ”L¥¤Ñ+Ô+ˆ	Ø'+¤½%¼(Ò'BÐ'B˜)˜)ÈÈ
ˆŒˆˆr   Úreturnc                 óD   — |                       | j        | j        ¬¦  «        S )N)r&   r*   )Ú_generate_state_keyr&   r*   ©rD   s    r   Ú	state_keyzEarlyStopping.state_key˜   s!   € ð ×'Ò'°´À4Ä9Ð'ÑMÔMÐMr   Útrainerú
pl.TrainerÚ	pl_moduleúpl.LightningModuleÚstagec                 óN   — | j         €|j        dk    o
|j        dk    | _         d S d S )Ng      ð?r   )r9   Úval_check_intervalÚcheck_val_every_n_epoch)rD   rL   rN   rP   s       r   ÚsetupzEarlyStopping.setup�   s:   € àÔ)Ð1ð .5Ô-GÈ3Ò-NÐ-wÐSZÔSrÐvwÒSwˆDÔ*Ð*Ð*ð 2Ð1r   Úlogsc           	      ó0  — |                      | j        ¦  «        }d| j        › dd                     t          |                     ¦   «         ¦  «        ¦  «        › d�}|€9| j        rt          |¦  «        ‚| j        dk    rt          |t          ¬¦  «         dS dS )	Nz&Early stopping conditioned on metric `zh` which is not available. Pass in or modify your `EarlyStopping` callback to use any of the following: `z`, `ú`r   )ÚcategoryFT)
Úgetr&   r;   Úlistr<   r+   ÚRuntimeErrorr)   r   ÚRuntimeWarning)rD   rU   Úmonitor_valÚ	error_msgs       r   Ú_validate_condition_metricz(EarlyStopping._validate_condition_metric¤   s¥   € Ø—h’h˜tœ|Ñ,Ô,ˆð3°T´\ð 3ð 3à—’�T $§)¢)¡+¤+Ñ.Ô.Ñ/Ô/ð3ð 3ð 3ð 	ð ÐØŒ{ð .Ý" 9Ñ-Ô-Ð-ØŒ|˜aÒÐÝ˜yµ>ÐBÑBÔBÐBà�5àˆtr   c                 ó&   — | j         | j                 S ©N)r:   r*   rJ   s    r   r=   zEarlyStopping.monitor_op·   s   € àŒ~˜dœiÔ(Ð(r   c                 óZ   — | j         | j        | j        | j        | j        j        | j        dœS )N)r5   r6   rC   r(   r7   r8   )r5   r6   rC   r(   r7   Úvaluer8   rJ   s    r   Ú
state_dictzEarlyStopping.state_dict»   s7   € ð œ/Ø!Ô/Øœ/ØœØ#Ô3Ô9Ø'+Ô'Cð
ð 
ð 	
r   rd   c                 ó  — |d         | _         |d         | _        |d         | _        |d         | _        |                     dt
          j        j        ¦  «        }t          |¦  «        | _        |                     d¦  «        | _	        d S )Nr5   r6   rC   r(   r7   r8   )
r5   r6   rC   r(   rY   r   r   rc   r7   r8   )rD   rd   Ústopping_reason_values      r   Úload_state_dictzEarlyStopping.load_state_dictÆ   s{   € à$ \Ô2ˆŒØ'¨Ô8ˆÔØ$ \Ô2ˆŒØ" :Ô.ˆŒØ *§¢Ð/@ÕBUÔBaÔBgÑ hÔ hÐÝ2Ð3HÑIÔIˆÔØ'1§~¢~Ð6OÑ'PÔ'PˆÔ$Ð$Ð$r   c                 óF   — ddl m} |j        j        |j        k    p|j        S )Nr   )Ú	TrainerFn)Ú pytorch_lightning.trainer.statesri   ÚstateÚfnÚFITTINGÚsanity_checking)rD   rL   ri   s      r   Ú_should_skip_checkz EarlyStopping._should_skip_checkÐ   s/   € Ø>Ð>Ð>Ð>Ð>Ð>àŒ}Ô 9Ô#4Ò4ÐO¸Ô8OÐOr   c                 ól   — | j         r|                      |¦  «        rd S |                      |¦  «         d S ra   ©r9   ro   Ú_run_early_stopping_check©rD   rL   rN   s      r   Úon_train_epoch_endz EarlyStopping.on_train_epoch_endÕ   sA   € àÔ-ð 	°×1HÒ1HÈÑ1QÔ1Qð 	ØˆFØ×&Ò& wÑ/Ô/Ð/Ð/Ð/r   c                 ól   — | j         s|                      |¦  «        rd S |                      |¦  «         d S ra   rq   rs   s      r   Úon_validation_endzEarlyStopping.on_validation_endÛ   sA   € àÔ)ð 	¨T×-DÒ-DÀWÑ-MÔ-Mð 	ØˆFØ×&Ò& wÑ/Ô/Ð/Ð/Ð/r   c                 óŽ  — |j         }|j        s|                      |¦  «        sdS || j                                      ¦   «         }|                      |¦  «        \  }}|j                             |d¬¦  «        }|j        p||_        |r|j	        | _
        || _        |r%| j        r |                      ||| j        ¦  «         dS dS dS )zdChecks whether the early stopping condition is met and if so tells the trainer to stop the training.NF)Úall)Úcallback_metricsÚfast_dev_runr_   r&   ÚsqueezeÚ_evaluate_stopping_criteriaÚstrategyÚreduce_boolean_decisionÚshould_stopÚcurrent_epochr6   r8   r)   Ú	_log_infor0   )rD   rL   rU   Úcurrentr   Úreasons         r   rr   z'EarlyStopping._run_early_stopping_checká   sù   € àÔ'ˆàÔð 	 t×'FÒ'FØñ(
ô (
ð 	ð ˆFà�t”|Ô$×,Ò,Ñ.Ô.ˆØ"×>Ò>¸wÑGÔGÑˆ�Vð Ô&×>Ò>¸{ÐPUÐ>ÑVÔVˆØ%Ô1Ð@°[ˆÔØð 	2Ø!(Ô!6ˆDÔØ+1ˆDÔ(Øð 	E�d”lð 	EØ�NŠN˜7 F¨DÔ,CÑDÔDÐDÐDÐDð	Eð 	Eð 	Eð 	Er   r‚   c           	      ó‚  — d}d }| j         r@t          j        |¦  «        s,d}t          j        | _        d| j        › d|› d| j        d›d�}�nq| j        �Y|  	                    || j        ¦  «        r>d}t          j
        | _        d| j        › d|› d	| j        | j                 › d	| j        › d�	}�n| j        �Z|  	                    | | j         ¦  «        r=d}t          j        | _        d
| j        › d|› d	| j        | j                 › d	| j        › d�	}n°|  	                    || j        z
  | j                             |j        ¦  «        ¦  «        r&d}|                      |¦  «        }|| _        d| _        nO| xj        dz  c_        | j        | j        k    r/d}t          j        | _        d| j        › d| j        › d| j        d›d�}||fS )NFTzMonitored metric z = z( is not finite. Previous best value was ú.3fz. Signaling Trainer to stop.zStopping threshold reached: ú zDivergence threshold reached: r   r   z did not improve in the last z records. Best score: )r,   r>   Úisfiniter   r   r7   r&   rC   r-   r=   r   Ú
order_dictr*   r.   r   r'   ÚtoÚdeviceÚ_improvement_messager5   r(   r   )rD   r‚   r   rƒ   s       r   r|   z)EarlyStopping._evaluate_stopping_criteriaö   sg  € ØˆØˆØÔð $	¥U¤^°GÑ%<Ô%<ð $	ØˆKÝ#6Ô#HˆDÔ ð^ D¤Lð ^ð ^°Wð ^ð ^Ø,0¬OÐ]ð^ð ^ð ^ð ‰Fð Ô$Ð0°T·_²_ÀWÈdÔNeÑ5fÔ5fÐ0ØˆKÝ#6Ô#IˆDÔ ð.Ø”Lð.ð .Ø%,ð.ð .Ø/3¬¸t¼yÔ/Ið.ð .ØLPÔLcð.ð .ð .ð ‰Fð
 Ô&Ð2°t·²ÈÀxÐRVÔRkÐQkÑ7lÔ7lÐ2ØˆKÝ#6Ô#KˆDÔ ð.Ø”Lð.ð .Ø%,ð.ð .Ø/3¬¸t¼yÔ/Ið.ð .ØLPÔLeð.ð .ð .ð ˆFð
 �_Š_˜W t¤~Ñ5°t´×7IÒ7IÈ'Ì.Ñ7YÔ7YÑZÔZð 	ØˆKØ×.Ò.¨wÑ7Ô7ˆFØ%ˆDŒOØˆDŒOˆOàˆOŒO˜qÑ ˆOŒOØŒ $¤-Ò/Ð/Ø"�Ý':Ô'M�Ô$ðV¨¬ð Vð VÐSWÔSbð Vð VØ$(¤OÐUðVð Vð Vð ð
 ˜FÐ"Ð"r   c           	      óÎ   — t          j        | j        ¦  «        r=d| j        › dt	          | j        |z
  ¦  «        d›dt	          | j        ¦  «        › d|d›�}nd| j        › d|d›�}|S )zXFormats a log message that informs the user about an improvement in the monitored score.zMetric z improved by r…   z >= min_delta = z. New best score: z improved. New best score: )r>   r‡   rC   r&   Úabsr'   )rD   r‚   Úmsgs      r   r‹   z"EarlyStopping._improvement_message!  s¡   € åŒ>˜$œ/Ñ*Ô*ð 	SðU˜$œ,ð Uð UµS¸¼È7Ñ9RÑ5SÔ5SÐ\ð Uð UÝ # D¤NÑ 3Ô 3ðUð UØGNÐTðUð Uð ˆCð
 S˜DœLÐRÐRÀWÐRÐRÐRˆCØˆ
r   Úmessagec                 óš   — | j         dk    r| j        nd }t          ||¦  «        }|�|r|dk    rt                               |¦  «         d S d S )Nr   r   )Ú
world_sizeÚglobal_rankr   ÚlogÚinfo)rL   r�   r0   Úranks       r   r�   zEarlyStopping._log_info,  s\   € à&-Ô&8¸1Ò&<Ð&<ˆwÔ"Ð"À$ˆÝ'¨°Ñ6Ô6ˆØˆ<Ð1ˆ<°T¸Q²Y°YÝ�HŠH�WÑÔÐÐÐð 6?°Yr   )
r%   r   Fr!   TTNNNF)rL   rM   rN   rO   rG   N)rL   rM   rG   N)%r   r   r   r   r>   rB   r?   r:   rˆ   ÚstrÚfloatÚintÚboolr   r4   Úpropertyr   rK   rT   Údictr   r_   r   r=   r   rd   rg   ro   rt   rv   rr   Útupler|   r‹   Ústaticmethodr�   Ú__classcell__)rF   s   @r   r    r    0   sc  ø€ € € € € ð;ð ;ðz œ¨¬Ð2Ð2€Ià SÐ)Ð)€Jð
 ØØØØØ!Ø.2Ø04Ø37Ø#(ð$Sð $Sàð$Sð ð$Sð ð	$Sð
 ð$Sð ð$Sð ð$Sð ð$Sð % UœOð$Sð ' uœoð$Sð #+¨4¤.ð$Sð !ð$Sð $Sð $Sð $Sð $Sð $SðL ØðN˜3ð Nð Nð Nñ „Xñ „XðNð ðx˜\ð xÐ6Jð xÐSVð xÐ[_ð xð xð xñ „Xðxð¨t°C¸°KÔ/@ð ÀTð ð ð ð ð& ð)˜Hð )ð )ð )ñ „Xð)ð ð
˜D  c œNð 
ð 
ð 
ñ „Xð
ð ðQ¨$¨s°C¨x¬.ð Q¸Tð Qð Qð Qñ „XðQðP¨,ð P¸4ð Pð Pð Pð Pð
 ð0ð 0ð 0ñ „Xð0ð
 ð0ð 0ð 0ñ „Xð0ð
Eð Eð Eð Eð*)#°6ð )#¸eÀDÈ(ÐSVÌ-ÐDWÔ>Xð )#ð )#ð )#ð )#ðV	¨Fð 	°sð 	ð 	ð 	ð 	ð ð˜<ð °#ð È4ð ÐTXð ð ð ñ „\ðð ð ð ð r   r    )r   ÚloggingÚenumr   Útypingr   r   r   r>   r   Útyping_extensionsr   Úpytorch_lightningÚplÚ$pytorch_lightning.callbacks.callbackr	   Ú&pytorch_lightning.utilities.exceptionsr
   Ú%pytorch_lightning.utilities.rank_zeror   r   Ú	getLoggerr   r“   r   r    r   r   r   ú<module>r©      s9  ððð ð €€€Ø Ð Ð Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à €€€Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø LÐ LÐ LÐ LÐ LÐ LØ WÐ WÐ WÐ WÐ WÐ WÐ WÐ Wà€gÔ˜Ñ!Ô!€ðð ð ð ð ˜$ñ ô ð ðAð Að Að Að A�Hñ Aô Að Að Að Ar   