§
    ·¯¸j×  ã                   ó^   — d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
  G d„ de¦  «        ZdS )	a	   RMSProp modified to behave like Tensorflow impl

Originally cut & paste from PyTorch RMSProp
https://github.com/pytorch/pytorch/blob/063946d2b3f3f1e953a2a3b54e0b34f1393de295/torch/optim/rmsprop.py
Licensed under BSD-Clause 3 (ish), https://github.com/pytorch/pytorch/blob/master/LICENSE

References for added functionality:
    Cautious Optimizers: https://arxiv.org/abs/2411.16085
    Why Gradients Rapidly Increase Near the End of Training: https://arxiv.org/abs/2506.02285

Modifications Copyright 2021 Ross Wightman
é    N)Ú	Optimizeré   )Ú_add_scaled_Ú_addcdiv_scaled_Ú_init_scalarÚ_validate_scalar)ÚParamsTc                   ó¢   ‡ — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 dd	ed
edededededededededefˆ fd„Zˆ fd„Z e	j
        ¦   «         dd„¦   «         Zˆ xZS )Ú	RMSpropTFaÃ  Implements RMSprop algorithm (TensorFlow style epsilon)

    NOTE: This is a direct cut-and-paste of PyTorch RMSprop with eps applied before sqrt
    and a few other modifications to closer match Tensorflow for matching hyper-params.

    Noteworthy changes include:
    1. Epsilon applied inside square-root
    2. square_avg initialized to ones
    3. LR scaling of update accumulated in momentum buffer

    Proposed by G. Hinton in his
    `course <http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf>`_.

    The centered version first appears in `Generating Sequences
    With Recurrent Neural Networks <https://arxiv.org/pdf/1308.0850v5.pdf>`_.

    Args:
        params: iterable of parameters to optimize or dicts defining parameter groups
        lr: learning rate
        momentum: momentum factor
        alpha: smoothing (decay) constant
        eps: term added to the denominator to improve numerical stability
        centered: if ``True``, compute the centered RMSProp, the gradient is normalized by an estimation of its variance
        weight_decay: weight decay (L2 penalty) (default: 0)
        decoupled_decay: decoupled weight decay as per https://arxiv.org/abs/1711.05101
        corrected_weight_decay: apply corrected weight decay (lr**2 / max_lr) when decoupled_decay is True
        lr_in_momentum: learning rate scaling is included in the momentum buffer update as per defaults in Tensorflow
        caution: apply caution
    ç{®Gáz„?çÍÌÌÌÌÌì?ç»½×Ùß|Û=r   ç        FTÚparamsÚlrÚalphaÚepsÚweight_decayÚmomentumÚcenteredÚdecoupled_decayÚcorrected_weight_decayÚlr_in_momentumÚcautionc                 ó\  •— t          d|¦  «         t          d|¦  «         t          d|¦  «         t          d|¦  «         d|k    s"t          d                     |¦  «        ¦  «        ‚t          ||||||||	|
|¬¦
  «
        }t	          t
          | ¦  «                             ||¦  «         d S )Nzlearning rateÚepsilonr   r   r   zInvalid alpha value: {})
r   r   r   r   r   r   r   r   r   r   )r   Ú
ValueErrorÚformatÚdictÚsuperr   Ú__init__)Úselfr   r   r   r   r   r   r   r   r   r   r   ÚdefaultsÚ	__class__s                €úe/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/timm/optim/rmsprop_tf.pyr!   zRMSpropTF.__init__4   sÅ   ø€ õ 	˜¨"Ñ-Ô-Ð-Ý˜ CÑ(Ô(Ð(Ý˜ XÑ.Ô.Ð.Ý˜¨Ñ6Ô6Ð6Ø�eŠ|ˆ|ÝÐ6×=Ò=¸eÑDÔDÑEÔEÐEåØØØØØØ%Ø+Ø#9Ø)Øð
ñ 
ô 
ˆõ 	�i˜ÑÔ×'Ò'¨°Ñ9Ô9Ð9Ð9Ð9ó    c                 ó¨  •— t          t          | ¦  «                             |¦  «         | j        D ] }|                     dd¦  «         |                     dd¦  «         |                     dd¦  «         |                     dd¦  «         |d         D ]=}| j                             |i ¦  «        }|rd|v rt          |d         d	¬
¦  «        |d<   Œ>Œ¡d S )Nr   r   r   Fr   r   r   ÚstepÚcpu©Údevice)r    r   Ú__setstate__Úparam_groupsÚ
setdefaultÚstateÚgetr   )r"   r/   ÚgroupÚpÚp_stater$   s        €r%   r,   zRMSpropTF.__setstate__W   sø   ø€ Ý�i˜ÑÔ×+Ò+¨EÑ2Ô2Ð2ØÔ&ð 	Rð 	RˆEØ×Ò˜Z¨Ñ+Ô+Ð+Ø×Ò˜Z¨Ñ/Ô/Ð/Ø×Ò˜Y¨Ñ.Ô.Ð.Ø×ÒÐ5°uÑ=Ô=Ð=Ø˜8”_ð Rð R�Øœ*Ÿ.š.¨¨BÑ/Ô/�Øð R˜v¨Ð0Ð0Ý&2°7¸6´?È5Ð&QÑ&QÔ&Q�G˜F‘OøðRð	Rð 	Rr&   Nc           	      ó¨  — d}|�5t          j        ¦   «         5   |¦   «         }ddd¦  «         n# 1 swxY w Y   | j        D �]}|d         D �]}|j        €Œ|j        }|j        rt          d¦  «        ‚| j        |         }t          |¦  «        dk    rlt          d¬¦  «        |d<   t          j	        |¦  «        |d<   |d	         dk    rt          j
        |¦  «        |d
<   |d         rt          j
        |¦  «        |d<   |d         }d|d         z
  }|d                              d¦  «         |d         dk    rq|d         rL|d         r|d         dz  | j        d         z  }	n|d         }	|                     d|	|d         z  z
  ¦  «         n|                     ||d         ¬¦  «        }|                     |                     d¦  «        |z
  |¬¦  «         |d         rf|d         }
|
                     ||
z
  |¬¦  «         |                     |
|
d¬¦  «                             |d         ¦  «                             ¦   «         }n-|                     |d         ¦  «                             ¦   «         }|d	         dk    r¶|d
         }|                     |d	         ¦  «         d„ }|d         rDt%          ||||d         ¦  «         |d         r |||¦  «        }|                     | ¦  «         �Œ¥|                     ||¦  «         |d         r |||¦  «        }t)          |||d          ¦  «         �Œét%          ||||d          ¦  «         �Œ�Œ|S )z¶Performs a single optimization step.

        Arguments:
            closure (callable, optional): A closure that reevaluates the model
                and returns the loss.
        Nr   z)RMSprop does not support sparse gradientsr   r)   r*   r(   Ú
square_avgr   Úmomentum_bufferr   Úgrad_avgg      ð?r   r   r   r   r   r   é   )r   éÿÿÿÿ)Úvaluer   c                 óÄ   — | |z  dk                          |j        ¦  «        }|                     |                     ¦   «                              d¬¦  «        ¦  «         | |z  S )Nr   gü©ñÒMbP?)Úmin)ÚtoÚdtypeÚdiv_ÚmeanÚclamp_)Ú_mÚ_gÚmasks      r%   Ú_apply_cautionz&RMSpropTF.step.<locals>._apply_caution¡   sT   € à " R¡¨!¢×/Ò/°´Ñ9Ô9˜ØŸ	š	 $§)¢)¡+¤+×"4Ò"4¸Ð"4Ñ">Ô">Ñ?Ô?Ð?Ø! D™yÐ(r&   r   r   )ÚtorchÚenable_gradr-   ÚgradÚ	is_sparseÚRuntimeErrorr/   Úlenr   Ú	ones_likeÚ
zeros_likeÚadd_r#   Úmul_ÚaddÚpowÚaddcmulÚsqrt_r   Úaddcdiv_r   )r"   ÚclosureÚlossr1   r2   rH   r/   r5   Úone_minus_alphaÚwd_scaler7   ÚavgÚbufrE   s                 r%   r(   zRMSpropTF.stepc   só  € ð ˆØÐÝÔ"Ñ$Ô$ð !ð !Ø�w‘y”y�ð!ð !ð !ñ !ô !ð !ð !ð !ð !ð !ð !øøøð !ð !ð !ð !ð Ô&ð D	Añ D	AˆEØ˜8”_ð CAñ CA�Ø”6�>ØØ”v�Ø”>ð TÝ&Ð'RÑSÔSÐSØœ
 1œ�õ �u‘:”: ’?�?Ý$0¸Ð$>Ñ$>Ô$>�E˜&‘MÝ*/¬/¸!Ñ*<Ô*<�E˜,Ñ'Ø˜ZÔ(¨1Ò,Ð,Ý38Ô3CÀAÑ3FÔ3F˜Ð/Ñ0Ø˜ZÔ(ð @Ý,1Ô,<¸QÑ,?Ô,?˜˜jÑ)à" <Ô0�
Ø"$ u¨W¤~Ñ"5�à�f”×"Ò" 1Ñ%Ô%Ð%à˜Ô(¨AÒ-Ð-ØÐ.Ô/ð HØ Ð!9Ô:ð 3Ø',¨T¤{°aÑ'7¸$¼-ÈÔ:MÑ'M˜H˜Hà',¨T¤{˜HØŸš˜r H¨u°^Ô/DÑ$DÑDÑEÔEÐEÐEà#Ÿxšx¨°°~Ô1F˜xÑGÔG˜ð —’ §¢¨¡¤¨jÑ 8À�ÑPÔPÐPð ˜Ô$ð ?Ø$ ZÔ0�HØ—M’M $¨¡/¸�MÑIÔIÐIØ$×,Ò,¨X°xÀrÐ,ÑJÔJ×NÒNÈuÐUZÌ|Ñ\Ô\×bÒbÑdÔd�C�Cð %Ÿ.š.¨¨u¬Ñ6Ô6×<Ò<Ñ>Ô>�Cà˜Ô$ qÒ(Ð(ØÐ 1Ô2�CØ—H’H˜U :Ô.Ñ/Ô/Ð/ð)ð )ð )ð Ð-Ô.ð ;å(¨¨d°C¸¸t¼ÑEÔEÐEØ  Ô+ð <Ø"0 .°°dÑ";Ô";˜CØŸš ˜t™œ˜™ð Ÿš T¨3Ñ/Ô/Ð/Ø  Ô+ð <Ø"0 .°°dÑ";Ô";˜CÝ$ Q¨¨e°D¬k¨\Ñ:Ô:Ð:Ñ:å$ Q¨¨c°E¸$´K°<Ñ@Ô@Ð@Ñ@ñGCAðJ ˆs   ˜/¯3¶3)
r   r   r   r   r   FFFTF)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   ÚfloatÚboolr!   r,   rF   Úno_gradr(   Ú__classcell__)r$   s   @r%   r   r      s(  ø€ € € € € ðð ðB ØØØ"#Ø Ø"Ø$)Ø+0Ø#'Ø!ð!:ð !:àð!:ð ð!:ð ð	!:ð
 ð!:ð  ð!:ð ð!:ð ð!:ð "ð!:ð %)ð!:ð !ð!:ð ð!:ð !:ð !:ð !:ð !:ð !:ðF
Rð 
Rð 
Rð 
Rð 
Rð €U„]�_„_ðRð Rð Rñ „_ðRð Rð Rð Rð Rr&   r   )r^   rF   Útorch.optimr   Ú_helpersr   r   r   r   Ú_typesr	   r   © r&   r%   ú<module>rg      s    ððð ð €€€Ø !Ð !Ð !Ð !Ð !Ð !à TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TØ Ð Ð Ð Ð Ð ðað að að að a�	ñ aô að að að ar&   