§
    “©ºjû2  ã                   ój  — 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
mZmZ d dlZd dlm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m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# d dl$m%Z% e G d„ de¦  «        ¦   «         Z& G d„ dee&         ¦  «        Z'e(e)e
f         Z* G d„ de¦  «        Z+dS )é    )ÚOrderedDict)ÚMapping)Ú	dataclassÚfield)Úpartial)ÚAnyÚCallableÚOptionalN)ÚTensor)Ú	Optimizer)Úoverride)Ú_Loop)ÚAbstractClosureÚOutputResult)Ú_OptimizationProgress)Ú_block_parallel_sync_behavior)Úcall)ÚMisconfigurationException)ÚWarningCache)ÚSTEP_OUTPUTc                   óî   — e Zd ZU dZee         ed<    edd¬¦  «        Zee         ed<    ee	¬¦  «        Z
e	eef         ed<   dd
„Zdd„Zeddeded	d fd„¦   «         Zed	e	eef         fd„¦   «         ZdS )ÚClosureResultaU  A container to hold the result of a :class:`Closure` call.

    It is created from the output of :meth:`~pytorch_lightning.core.LightningModule.training_step`.

    Attributes:
        closure_loss: The loss with a graph attached.
        loss: A detached copy of the closure loss.
        extra: Any keys other than the loss returned.

    Úclosure_lossFN)ÚinitÚdefaultÚloss)Údefault_factoryÚextraÚreturnc                 ó.   — |                       ¦   «          d S ©N)Ú_clone_loss©Úselfs    ú~/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/loops/optimization/automatic.pyÚ__post_init__zClosureResult.__post_init__5   s   € Ø×ÒÑÔÐÐÐó    c                 óx   — | j         �2| j                              ¦   «                              ¦   «         | _        d S d S r!   )r   ÚdetachÚcloner   r#   s    r%   r"   zClosureResult._clone_loss8   s9   € ØÔÐ(àÔ)×0Ò0Ñ2Ô2×8Ò8Ñ:Ô:ˆDŒIˆIˆIð )Ð(r'   é   Útraining_step_outputÚ	normalizec                 ó8  — d i }}t          |t          ¦  «        rE|                     d¦  «        }|€t          d¦  «        ‚d„ |                     ¦   «         D ¦   «         }n)t          |t
          ¦  «        r|}n|�t          d¦  «        ‚|�||z  } | ||¬¦  «        S )Nr   zbIn automatic_optimization, when `training_step` returns a dict, the 'loss' key needs to be presentc                 ó&   — i | ]\  }}|d k    ¯||“ŒS )r   © )Ú.0ÚkÚvs      r%   ú
<dictcomp>z;ClosureResult.from_training_step_output.<locals>.<dictcomp>G   s#   € ÐRÐRÐR™d˜a ÀaÈ6ÂkÀk�Q˜ÀkÀkÀkr'   zrIn automatic optimization, `training_step` must return a Tensor, a dict, or None (where the step will be skipped).)r   )Ú
isinstancer   Úgetr   Úitemsr   )Úclsr,   r-   r   r   s        r%   Úfrom_training_step_outputz'ClosureResult.from_training_step_output=   sÑ   € à" B�eˆåÐ*­GÑ4Ô4ð 	Ø/×3Ò3°FÑ;Ô;ˆLØÐ#Ý/Øxñô ð ð SÐRÐ&:×&@Ò&@Ñ&BÔ&BÐRÑRÔRˆEˆEÝÐ,­fÑ5Ô5ð 	Ø/ˆLˆLØ!Ð-Ý+ð ñô ð ð
 Ð#ð (¨)Ñ3ˆLàˆs�< uÐ-Ñ-Ô-Ð-r'   c                 ó"   — d| j         i| j        ¥S )Nr   )r   r   r#   s    r%   ÚasdictzClosureResult.asdictW   s   € à˜œ	Ð0 T¤ZÐ0Ð0r'   ©r   N)r+   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   Ú__annotations__r   r   Údictr   Ústrr   r&   r"   Úclassmethodr   Úintr9   r   r;   r0   r'   r%   r   r   $   s  € € € € € € ð	ð 	ð ˜6Ô"Ð"Ð"Ñ"Ø"˜U¨°tÐ<Ñ<Ô<€Dˆ(�6Ô
Ð<Ð<Ñ<Ø!˜E°$Ð7Ñ7Ô7€Eˆ4��S�Œ>Ð7Ð7Ñ7ðð ð ð ð;ð ;ð ;ð ;ð
 ð.ð .¸[ð .ÐUXð .Ðapð .ð .ð .ñ „[ð.ð2 ð1˜˜S #˜Xœð 1ð 1ð 1ñ „Xð1ð 1ð 1r'   r   c            
       ó  ‡ — e Zd ZdZ e¦   «         Z	 	 ddeg ef         deee	gdf                  deeg df                  fˆ fd„Z
e ej        ¦   «         deded	efd
„¦   «         ¦   «         Zededed	ee	         fd„¦   «         Zˆ xZS )ÚClosurea  An implementation of a :class:`AbstractClosure` for automatic optimization in Lightning that combines three
    elementary closures into one: ``training_step``, ``backward`` and ``zero_grad``.

    The Closure gets created by the training loop(s) and is then passed to the
    :meth:`torch.optim.Optimizer.step` method. An optimizer is responsible for calling the closure and optionally
    do something with the output.

    Args:
        step_fn: This is typically the :meth:`pytorch_lightning.core.module.LightningModule.training_step
            wrapped with processing for its outputs
        backward_fn: A function that takes a loss value as input, performs back-propagation and returns the loss value.
            Can be set to ``None`` to skip the backward operation.
        zero_grad_fn: A function that zeroes the gradients. Can be set to ``None`` to skip zero_grad, for example
            when accumulating gradients.

    Example:

        closure = Closure()
        optimizer = torch.optim.Adam(...)
        optimizer.step(closure)
    NÚstep_fnÚbackward_fnÚzero_grad_fnc                 ór   •— t          ¦   «                              ¦   «          || _        || _        || _        d S r!   )ÚsuperÚ__init__Ú_step_fnÚ_backward_fnÚ_zero_grad_fn)r$   rH   rI   rJ   Ú	__class__s       €r%   rM   zClosure.__init__u   s8   ø€ õ 	‰Œ×ÒÑÔÐØˆŒØ'ˆÔØ)ˆÔÐÐr'   ÚargsÚkwargsr   c                 óö   — |                       ¦   «         }|j        €| j                             d¦  «         | j        �|                      ¦   «          | j        �!|j        �|                      |j        ¦  «         |S )NzO`training_step` returned `None`. If this was on purpose, ignore this warning...)rN   r   Úwarning_cacheÚwarnrP   rO   )r$   rR   rS   Ústep_outputs       r%   ÚclosurezClosure.closure€   s{   € ð —m’m‘o”oˆàÔ#Ð+ØÔ×#Ò#Ð$uÑvÔvÐvàÔÐ)Ø×ÒÑ Ô Ð àÔÐ(¨[Ô-EÐ-QØ×Ò˜kÔ6Ñ7Ô7Ð7àÐr'   c                 ó>   —  | j         |i |¤Ž| _        | j        j        S r!   )rX   Ú_resultr   )r$   rR   rS   s      r%   Ú__call__zClosure.__call__�   s%   € à#�t”| TÐ4¨VÐ4Ð4ˆŒØŒ|Ô Ð r'   )NN)r=   r>   r?   r@   r   rU   r	   r   r
   r   rM   r   ÚtorchÚenable_gradr   rX   r[   Ú__classcell__©rQ   s   @r%   rG   rG   \   s*  ø€ € € € € ðð ð, !�L‘N”N€Mð
 ;?Ø59ð		*ð 	*à˜"˜mÐ+Ô,ð	*ð ˜h¨ x° ~Ô6Ô7ð	*ð ˜x¨¨D¨Ô1Ô2ð		*ð 	*ð 	*ð 	*ð 	*ð 	*ð Ø€UÔÑÔð˜Sð ¨Cð °Mð ð ð ñ Ôñ „Xðð ð!˜cð !¨Sð !°X¸fÔ5Eð !ð !ð !ñ „Xð!ð !ð !ð !ð !r'   rG   c            	       ób  ‡ — e Zd ZdZeZdˆ fd„Zdeded	e	de
fd
„Zd	e	dededefd„Zd	e	deg ef         fd„Zdededeeg df                  fd„Zdedeeegdf                  fd„Zdedeg ee         f         ddfd„Zdej        j        ddfd„Zdedej        j        ddfd„Zd	e	defd„Zˆ xZS )Ú_AutomaticOptimizationzNPerforms automatic optimization (forward, zero grad, backward, optimizer step)Útrainerú
pl.Trainerr   Nc                 ó~   •— t          ¦   «                              |¦  «         t          ¦   «         | _        d| _        d S )NF)rL   rM   r   Úoptim_progressÚ_skip_backward)r$   rb   rQ   s     €r%   rM   z_AutomaticOptimization.__init__ž   s7   ø€ Ý‰Œ×Ò˜Ñ!Ô!Ð!Ý5JÑ5LÔ5LˆÔØ$)ˆÔÐÐr'   Ú	optimizerÚ	batch_idxrS   c                 ó˜  — |                       |||¦  «        }| j        j        j        s\| j        j                             ¦   «         r>t          | j        j        d¬¦  «        5   |¦   «          ddd¦  «         n# 1 swxY w Y   n|                      ||¦  «         |                     ¦   «         }|j	        €i S | 
                    ¦   «         S )zóRuns closure (train step + backward) together with optimization if necessary.

        Args:
            kwargs: the kwargs passed down to the hooks
            batch_idx: the current batch index.
            optimizer: the optimizer

        T)ÚblockN)Ú_make_closurerb   ÚstrategyÚhandles_gradient_accumulationÚfit_loopÚ_should_accumulater   Ú_optimizer_stepÚconsume_resultr   r;   )r$   rg   rh   rS   rX   Úresults         r%   Úrunz_AutomaticOptimization.run£   s  € ð ×$Ò$ V¨Y¸	ÑBÔBˆð ”Ô%ÔCð	5àHLÌÔH]×HpÒHpÑHrÔHrð	5õ /¨t¬|Ô/DÈDÐQÑQÔQð ð Ø�‘	”	�	ðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð øð × Ò  ¨GÑ4Ô4Ð4à×'Ò'Ñ)Ô)ˆØŒ;ÐØˆIØ�}Š}‰ŒÐs   Á"A9Á9A=Â A=c                 ó¦   — |                       |¦  «        }|                      |¦  «        }|                      ||¦  «        }t          |||¬¦  «        S )z©Build a closure object that captures the given arguments and runs the `training_step` function and
        optionally other functions such as `backward` and `zero_grad`.)rH   rI   rJ   )Ú_make_step_fnÚ_make_backward_fnÚ_make_zero_grad_fnrG   )r$   rS   rg   rh   rH   rI   rJ   s          r%   rk   z$_AutomaticOptimization._make_closureÇ   sT   € ð ×$Ò$ VÑ,Ô,ˆØ×,Ò,¨YÑ7Ô7ˆØ×.Ò.¨y¸)ÑDÔDˆÝ˜w°KÈlÐ[Ñ[Ô[Ð[r'   c                 ó,   — t          | j        |¦  «        S )zOBuild the step function that runs the `training_step` and processes its output.)r   Ú_training_step)r$   rS   s     r%   ru   z$_AutomaticOptimization._make_step_fnÏ   s   € å�tÔ*¨FÑ3Ô3Ð3r'   c                 ó\   ‡ ‡‡— ‰ j         rdS ‰‰ j        j        z  dk    }|sdS dˆˆˆ fd„}|S )z�Build a `zero_grad` function that zeroes the gradients before back-propagation.

        Returns ``None`` in the case backward needs to be skipped.

        Nr   r   c                  ó^   •— ‰                      ‰¦  «         ‰                     ‰ ‰¦  «         d S r!   )Ú_on_before_zero_gradÚ_optimizer_zero_grad)rh   rg   r$   s   €€€r%   rJ   z?_AutomaticOptimization._make_zero_grad_fn.<locals>.zero_grad_fnà   s3   ø€ Ø×%Ò% iÑ0Ô0Ð0Ø×%Ò% i°Ñ;Ô;Ð;Ð;Ð;r'   r<   )rf   rb   Úaccumulate_grad_batches)r$   rh   rg   Úis_first_batch_to_accumulaterJ   s   ```  r%   rw   z)_AutomaticOptimization._make_zero_grad_fnÓ   sj   øøø€ ð Ôð 	Ø�4à'0°4´<Ô3WÑ'WÐ[\Ò'\Ð$Ø+ð 	Ø�4ð	<ð 	<ð 	<ð 	<ð 	<ð 	<ð 	<ð 	<ð Ðr'   c                 ó<   ‡ ‡— ‰ j         rdS dt          ddfˆˆ fd„}|S )zÌBuild a `backward` function that handles back-propagation through the output produced by the `training_step`
        function.

        Returns ``None`` in the case backward needs to be skipped.

        Nr   r   c                 ó@   •— t          j        ‰j        d| ‰¦  «         d S )NÚbackward)r   Ú_call_strategy_hookrb   )r   rg   r$   s    €€r%   rI   z=_AutomaticOptimization._make_backward_fn.<locals>.backward_fnð   s"   ø€ ÝÔ$ T¤\°:¸tÀYÑOÔOÐOÐOÐOr'   )rf   r   )r$   rg   rI   s   `` r%   rv   z(_AutomaticOptimization._make_backward_fnæ   sX   øø€ ð Ôð 	Ø�4ð	P�fð 	P¨ð 	Pð 	Pð 	Pð 	Pð 	Pð 	Pð 	Pð Ðr'   Útrain_step_and_backward_closurec                 ó>  — | j         }|j        j        d         }|j                             ¦   «         }|s#| j        j        j                             ¦   «          t          j
        |d|j        |||¦  «         |s%| j        j        j                             ¦   «          dS dS )a?  Performs the optimizer step and some sanity checking.

        Args:
            batch_idx: the index of the current batch
            train_step_and_backward_closure: the closure function performing the train step and computing the
                gradients. By default, called by the optimizer (if possible)

        r   Úoptimizer_stepN)rb   rl   Ú_lightning_optimizersrn   ro   re   rg   ÚstepÚincrement_readyr   Ú_call_lightning_module_hookÚcurrent_epochÚincrement_completed)r$   rh   r„   rb   rg   Úshould_accumulates         r%   rp   z&_AutomaticOptimization._optimizer_stepõ   s¼   € ð ”,ˆð Ô$Ô:¸1Ô=ˆ	ð $Ô,×?Ò?ÑAÔAÐØ ð 	AØÔÔ)Ô.×>Ò>Ñ@Ô@Ð@õ 	Ô(ØØØÔ!ØØØ+ñ	
ô 	
ð 	
ð !ð 	EØÔÔ)Ô.×BÒBÑDÔDÐDÐDÐDð	Eð 	Er'   c                 óø   — | j         }| j        j        j                             ¦   «          t          j        |d|¦  «         t          j        |d|¦  «         | j        j        j                             ¦   «          dS )zmCalls the ``on_before_zero_grad`` hook.

        Args:
            optimizer: the current optimizer

        Úon_before_zero_gradN)	rb   re   rg   Ú	zero_gradr‰   r   Ú_call_callback_hooksrŠ   Úincrement_started)r$   rg   rb   s      r%   r|   z+_AutomaticOptimization._on_before_zero_grad  ss   € ð ”,ˆØÔÔ%Ô/×?Ò?ÑAÔAÐAÝÔ! 'Ð+@À)ÑLÔLÐLÝÔ(¨Ð2GÈÑSÔSÐSØÔÔ%Ô/×AÒAÑCÔCÐCÐCÐCr'   c                 ó”   — | j         }t          j        |d|j        ||¦  «         | j        j        j                             ¦   «          dS )zÆZeroes out all gradients of parameters optimized by the current optimizer.

        Args:
            batch_idx: the index of the current batch
            optimizer: the current optimizer

        Úoptimizer_zero_gradN)rb   r   rŠ   r‹   re   rg   r�   rŒ   )r$   rh   rg   rb   s       r%   r}   z+_AutomaticOptimization._optimizer_zero_grad'  sK   € ð ”,ˆÝÔ(¨Ð2GÈÔI^Ð`iÐktÑuÔuÐuØÔÔ%Ô/×CÒCÑEÔEÐEÐEÐEr'   c                 ó  — | j         }t          j        |dg|                     ¦   «         ¢R Ž }| j         j                             ¦   «          |€|j        dk    rt          d¦  «        ‚| j         	                    ||j
        ¦  «        S )zÜPerforms the actual train step with the tied hooks.

        Args:
            kwargs: the kwargs passed down to the hooks.

        Returns:
            A ``ClosureResult`` containing the training step output.

        Útraining_stepNr+   zÄSkipping the `training_step` by returning None in distributed training is not supported. It is recommended that you rewrite your training logic to avoid having to skip the step in the first place.)rb   r   rƒ   Úvaluesrl   Úpost_training_stepÚ
world_sizeÚRuntimeErrorÚoutput_result_clsr9   r~   )r$   rS   rb   r,   s       r%   ry   z%_AutomaticOptimization._training_step3  s”   € ð ”,ˆå#Ô7¸ÀÐcÐSY×S`ÒS`ÑSbÔSbÐcÐcÐcÐØŒÔ×0Ò0Ñ2Ô2Ð2àÐ'¨GÔ,>ÀÒ,BÐ,BÝðñô ð ð Ô%×?Ò?Ð@TÐV]ÔVuÑvÔvÐvr'   )rb   rc   r   N)r=   r>   r?   r@   r   r›   rM   r   rE   r   Ú_OUTPUTS_TYPErs   rG   rk   r	   ru   r
   rw   r   rv   rp   r\   Úoptimr|   r}   ry   r^   r_   s   @r%   ra   ra   ™   s%  ø€ € € € € ØXÐXà%Ðð*ð *ð *ð *ð *ð *ð
"˜Yð "°3ð "Àð "ÐP]ð "ð "ð "ð "ðH\ Kð \¸Ið \ÐRUð \ÐZað \ð \ð \ð \ð4 Kð 4°H¸RÀÐ=NÔ4Oð 4ð 4ð 4ð 4ð¨Cð ¸Ið È(ÐS[Ð\^Ð`dÐ\dÔSeÔJfð ð ð ð ð&¨9ð ¸À(ÈFÈ8ÐUYÈ>ÔBZÔ9[ð ð ð ð ð#Eàð#Eð *2°"°h¸vÔ6FÐ2FÔ)Gð#Eð 
ð	#Eð #Eð #Eð #EðJD¨e¬kÔ.Cð DÈð Dð Dð Dð Dð
F¨cð 
F¸e¼kÔ>Sð 
FÐX\ð 
Fð 
Fð 
Fð 
Fðw [ð w°]ð wð wð wð wð wð wð wð wr'   ra   ),Úcollectionsr   Úcollections.abcr   Údataclassesr   r   Ú	functoolsr   Útypingr   r	   r
   r\   r   Útorch.optimr   Útyping_extensionsr   Úpytorch_lightningÚplÚpytorch_lightning.loops.loopr   Ú,pytorch_lightning.loops.optimization.closurer   r   Ú pytorch_lightning.loops.progressr   Ú!pytorch_lightning.loops.utilitiesr   Úpytorch_lightning.trainerr   Ú&pytorch_lightning.utilities.exceptionsr   Ú%pytorch_lightning.utilities.rank_zeror   Ú!pytorch_lightning.utilities.typesr   r   rG   rB   rC   rœ   ra   r0   r'   r%   ú<module>r¯      s  ðð $Ð #Ð #Ð #Ð #Ð #Ø #Ð #Ð #Ð #Ð #Ð #Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ø Ð Ð Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à €€€Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ø &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .Ø VÐ VÐ VÐ VÐ VÐ VÐ VÐ VØ BÐ BÐ BÐ BÐ BÐ BØ KÐ KÐ KÐ KÐ KÐ KØ *Ð *Ð *Ð *Ð *Ð *Ø LÐ LÐ LÐ LÐ LÐ LØ >Ð >Ð >Ð >Ð >Ð >Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9ð ð41ð 41ð 41ð 41ð 41�Lñ 41ô 41ñ „ð41ðn7!ð 7!ð 7!ð 7!ð 7!ˆo˜mÔ,ñ 7!ô 7!ð 7!ðt �S˜#�X”€ðpwð pwð pwð pwð pw˜Uñ pwô pwð pwð pwð pwr'   