
    Ng+                         d 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
  ej        e          Z G d d          Z G d d	e
j                  Z G d
 de
j                  ZdS )zf Exponential Moving Average (EMA) of model updates

Hacked together by / Copyright 2020 Ross Wightman
    N)OrderedDict)deepcopy)Optionalc                   &    e Zd ZdZddZd Zd ZdS )	ModelEmaa   Model Exponential Moving Average (DEPRECATED)

    Keep a moving average of everything in the model state_dict (parameters and buffers).
    This version is deprecated, it does not work with scripted models. Will be removed eventually.

    This is intended to allow functionality like
    https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage

    A smoothed version of the weights is necessary for some training schemes to perform well.
    E.g. Google's hyper-params for training MNASNet, MobileNet-V3, EfficientNet, etc that use
    RMSprop with a short 2.4-3 epoch decay period and slow LR decay rate of .96-.99 requires EMA
    smoothing of weights to match results. Pay attention to the decay constant you are using
    relative to your update count per epoch.

    To keep EMA from using GPU resources, set device='cpu'. This will save a bit of memory but
    disable validation of the EMA weights. Validation will have to be done manually in a separate
    process, or after the training stops converging.

    This class is sensitive where it is initialized in the sequence of model init,
    GPU assignment and distributed training wrappers.
    H.? c                 z   t          |          | _        | j                                         || _        || _        |r| j                            |           t          | j        d          | _        |r|                     |           | j        	                                D ]}|
                    d           d S )NdevicemoduleF)r   emaevaldecayr   tohasattrema_has_module_load_checkpoint
parametersrequires_grad_)selfmodelr   r   resumeps         P/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/utils/model_ema.py__init__zModelEma.__init__&   s    E??
 	'HKKvK&&&%dh99 	*!!&)))$$&& 	$ 	$AU####	$ 	$    c                    t          j        |d          }t          |t                    sJ d|v rt	                      }|d                                         D ]0\  }}| j        r|                    d          sd|z   n|}n|}|||<   1| j        	                    |           t                              d           d S t                              d           d S )Ncpu)map_locationstate_dict_emar   module.zLoaded state_dict_emazAFailed to find state_dict_ema, starting from loaded model weights)torchload
isinstancedictr   itemsr   
startswithr   load_state_dict_loggerinfowarning)r   checkpoint_path
checkpointnew_state_dictkvnames          r   r   zModelEma._load_checkpoint4   s    ZeDDD
*d+++++z))(]]N"#34::<< ) )1& 01X0F0FM9q==ADDD'(t$$H$$^444LL011111OO_`````r   c                    t          |d          o| j         }t          j                    5  |                                }| j                                                                        D ]s\  }}|rd|z   }||                                         }| j        r|	                    | j                  }|
                    || j        z  d| j        z
  |z  z              t	 d d d            d S # 1 swxY w Y   d S )Nr   r"   r         ?)r   r   r#   no_grad
state_dictr   r'   detachr   r   copy_r   )r   r   needs_modulemsdr0   ema_vmodel_vs          r   updatezModelEma.updateE   sS   uh//K8K4K]__ 	N 	N""$$C H//117799 N N5 &!AAa&--//; =%jjj<<GEDJ."tz/W1LLMMMMN	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	Ns   B4C..C25C2N)r   r	   r	   )__name__
__module____qualname____doc__r   r   r=    r   r   r   r      sX         *$ $ $ $a a a"N N N N Nr   r   c                   <     e Zd ZdZd	 fd	Zd Zd Zd Zd Z xZ	S )

ModelEmaV2a   Model Exponential Moving Average V2

    Keep a moving average of everything in the model state_dict (parameters and buffers).
    V2 of this module is simpler, it does not match params/buffers based on name but simply
    iterates in order. It works with torchscript (JIT of full model).

    This is intended to allow functionality like
    https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage

    A smoothed version of the weights is necessary for some training schemes to perform well.
    E.g. Google's hyper-params for training MNASNet, MobileNet-V3, EfficientNet, etc that use
    RMSprop with a short 2.4-3 epoch decay period and slow LR decay rate of .96-.99 requires EMA
    smoothing of weights to match results. Pay attention to the decay constant you are using
    relative to your update count per epoch.

    To keep EMA from using GPU resources, set device='cpu'. This will save a bit of memory but
    disable validation of the EMA weights. Validation will have to be done manually in a separate
    process, or after the training stops converging.

    This class is sensitive where it is initialized in the sequence of model init,
    GPU assignment and distributed training wrappers.
    r   Nc                    t                                                       t          |          | _        | j                                         || _        || _        | j        | j                            |           d S d S Nr   )superr   r   r   r   r   r   r   )r   r   r   r   	__class__s       r   r   zModelEmaV2.__init__j   st    uoo
;"KNN&N))))) #"r   c                    t          j                    5  t          | j                                                                        |                                                                          D ]F\  }}| j        |                    | j                  }|                     |||                     G	 d d d            d S # 1 swxY w Y   d S rF   )	r#   r5   zipr   r6   valuesr   r   r8   )r   r   	update_fnr;   r<   s        r   _updatezModelEmaV2._updatet   s   ]__ 	7 	7"%dk&<&<&>&>&E&E&G&GIYIYI[I[IbIbIdId"e"e 7 7w;*%jjj<<GIIeW5566667	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   B%CCCc                 <                           | fd           d S )Nc                 4    j         | z  dj         z
  |z  z   S )Nr4   )r   )emr   s     r   <lambda>z#ModelEmaV2.update.<locals>.<lambda>|   s    4:>R$*_XYDY3Y r   rL   rM   r   r   s   ` r   r=   zModelEmaV2.update{   s)    U&Y&Y&Y&YZZZZZr   c                 6    |                      |d            d S )Nc                     |S NrB   )rP   rQ   s     r   rR   z ModelEmaV2.set.<locals>.<lambda>   s    1 r   rS   rT   rU   s     r   setzModelEmaV2.set~   s     Unn55555r   c                      | j         |i |S rX   r   r   argskwargss      r   forwardzModelEmaV2.forward       t{D+F+++r   )r   N)
r>   r?   r@   rA   r   rM   r=   rY   r_   __classcell__rH   s   @r   rD   rD   S   s         ,* * * * * *7 7 7[ [ [6 6 6, , , , , , ,r   rD   c                   "    e Zd ZdZ	 	 	 	 	 	 	 	 	 dd
ededededededeej	                 dedef fdZ
ddee         defdZ ej                    ddee         fd            Zd
efdZd
efdZ ej                    d             Zd Z xZS )
ModelEmaV3a   Model Exponential Moving Average V3

    Keep a moving average of everything in the model state_dict (parameters and buffers).
    V3 of this module leverages for_each and in-place operations for faster performance.

    Decay warmup based on code by @crowsonkb, her comments:
      If inv_gamma=1 and power=1, implements a simple average. inv_gamma=1, power=2/3 are
      good values for models you plan to train for a million or more steps (reaches decay
      factor 0.999 at 31.6K steps, 0.9999 at 1M steps), inv_gamma=1, power=3/4 for models
      you plan to train for less (reaches decay factor 0.999 at 10K steps, 0.9999 at
      215.4k steps).

    This is intended to allow functionality like
    https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage

    To keep EMA from using GPU resources, set device='cpu'. This will save a bit of memory but
    disable validation of the EMA weights. Validation will have to be done manually in a separate
    process, or after the training stops converging.

    This class is sensitive where it is initialized in the sequence of model init,
    GPU assignment and distributed training wrappers.
    r           r   Fr4   UUUUUU?NTr   	min_decayupdate_after_step
use_warmupwarmup_gammawarmup_powerr   foreachexclude_buffersc                    t                                                       t          |          | _        | j                                         || _        || _        || _        || _        || _	        || _
        |	| _        || _        |
| _        | j        N|t          |                                          j        k    r&d| _        | j                            |           d S d S d S )NFr   )rG   r   r   r   r   r   rg   rh   ri   rj   rk   rl   r   rm   nextr   r   )r   r   r   rg   rh   ri   rj   rk   r   rl   rm   rH   s              r   r   zModelEmaV3.__init__   s     	uoo
"!2$((.;"ve6F6F6H6H1I1I1P'P'P DLKNN&N))))) #"'P'Pr   stepreturnc                     || j         S t          d|| j        z
  dz
            }|dk    rdS | j        rBdd|| j        z  z   | j         z  z
  }t          t          || j                   | j                  }n| j         }|S )zN
        Compute the decay factor for the exponential moving average.
        Nr      re   )r   maxrh   ri   rj   rk   minrg   )r   rp   r   s      r   	get_decayzModelEmaV3.get_decay   s     <:1dT33a7881993? 	TD$5554;L:LLLEE4:..??EEJEr   c                     |                      |          }| j        r|                     ||           d S |                     ||           d S rX   )rv   rm   apply_update_no_buffers_apply_update_)r   r   rp   r   s       r   r=   zModelEmaV3.update   sV    t$$ 	-))%77777ue,,,,,r   c                    | j         rg }g }t          | j                                                                        |                                                                          D ]Y\  }}|                                r+|                    |           |                    |           D|                    |           Zt          t          d          rt          j
        ||d|z
             d S t          j        ||           t          j        ||d|z
             d S t          | j                                                                        |                                                                          D ]{\  }}|                                r4|                    |                    | j                  d|z
             M|                    |                    | j                             |d S )N_foreach_lerp_r4   weightscalaralphar   )rl   rJ   r   r6   rK   is_floating_pointappendr8   r   r#   r{   _foreach_mul__foreach_add_lerp_r   r   )r   r   r   ema_lerp_valuesmodel_lerp_valuesr;   r<   s          r   ry   zModelEmaV3.apply_update_   s   < 	@ O ""%dk&<&<&>&>&E&E&G&GIYIYI[I[IbIbIdId"e"e ) )w**,, )#**5111%,,W5555KK((((u.// Z$_6GPRUZPZ[[[[[[#OEBBBB#O5FbSXjYYYYYY"%dk&<&<&>&>&E&E&G&GIYIYI[I[IbIbIdId"e"e @ @w**,, @KK

$+
 > >rEzKRRRRKK

$+
 > >????	@ @r   c                    t          | j                                                  }t          |                                          }| j        rat	          t
          d          rt          j        ||d|z
             nzt          j        ||           t          j        ||d|z
             nIt          ||          D ]8\  }}|
                    |                    | j                  d|z
             9t          | j                                        |                                          D ]3\  }}|                    |                    | j                             4d S )Nr{   r4   r|   r~   rs   r   r   )tupler   r   rl   r   r#   r{   r   r   rJ   r   r   r   buffersr8   )	r   r   r   
ema_paramsmodel_paramsema_pmodel_pema_bmodel_bs	            r   rx   z#ModelEmaV3.apply_update_no_buffers_   sW   4;113344
U--//00< 	Ou.// O$Zb5jQQQQQ#Ju====#JAINNNNN"%j,"?"? O OwGJJdkJ::2:NNNN!$+"5"5"7"7II 	8 	8NE7KK

$+
667777	8 	8r   c                 (   t          | j                                                                        |                                                                          D ]3\  }}|                    |                    | j                             4d S rF   )rJ   r   r6   rK   r8   r   r   )r   r   r;   r<   s       r   rY   zModelEmaV3.set   s    !$+"8"8":":"A"A"C"CUEUEUEWEWE^E^E`E`aa 	8 	8NE7KK

$+
667777	8 	8r   c                      | j         |i |S rX   r[   r\   s      r   r_   zModelEmaV3.forward  r`   r   )	r   re   r   Fr4   rf   NTFrX   )r>   r?   r@   rA   floatintboolr   r#   r   r   rv   r5   r=   ry   rx   rY   r_   ra   rb   s   @r   rd   rd      s        2 ""%&$"%"%-1 $)* * * 	*
  #* *  *  * U\** * "* * * * * *< hsm u    & U]__- -(3- - - - _-@% @ @ @ @08U 8 8 8 8" U]__8 8 _8, , , , , , ,r   rd   )rA   loggingcollectionsr   copyr   typingr   r#   torch.nnnn	getLoggerr>   r*   r   ModulerD   rd   rB   r   r   <module>r      s#     # # # # # #                   
'
H
%
%@N @N @N @N @N @N @N @NF/, /, /, /, /, /, /, /,d, , , , , , , , , ,r   