a
    d&                     @   sf   d Z ddlZddlmZ ddlmZ ddlZddlmZ e	e
ZG dd dZG dd dejZdS )	zf Exponential Moving Average (EMA) of model updates

Hacked together by / Copyright 2020 Ross Wightman
    N)OrderedDict)deepcopyc                   @   s*   e Zd ZdZdddZdd Zd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                 C   sl   t || _| j  || _|| _|r2| jj|d t| jd| _|rN| | | j	 D ]}|
d qXd S )NdevicemoduleF)r   emaevaldecayr   tohasattrema_has_module_load_checkpoint
parametersZrequires_grad_)selfmodelr   r   resumep r   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/timm/utils/model_ema.py__init__%   s    


zModelEma.__init__c                 C   s   t j|dd}t|tsJ d|v rt }|d  D ]2\}}| jr\|dsVd| n|}n|}|||< q6| j	| t
d n
t
d d S )Ncpu)Zmap_locationZ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
   Zload_state_dict_loggerinfowarning)r   Zcheckpoint_path
checkpointZnew_state_dictkvnamer   r   r   r   3   s    
zModelEma._load_checkpointc                 C   s   t |do| j }t z | }| j  D ]R\}}|rFd| }||  }| jrf|j	| jd}|
|| j d| j |   q2W d    n1 s0    Y  d S )Nr	   r   r         ?)r   r   r   no_grad
state_dictr
   r   detachr   r   copy_r   )r   r   Zneeds_moduleZmsdr%   ema_vmodel_vr   r   r   updateD   s    
zModelEma.updateN)r   r   r   )__name__
__module____qualname____doc__r   r   r/   r   r   r   r   r      s   
r   c                       s:   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Z  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                    sJ   t t|   t|| _| j  || _|| _| jd urF| jj|d d S Nr   )	superr4   r   r   r	   r   r   r   r   )r   r   r   r   	__class__r   r   r   i   s    


zModelEmaV2.__init__c                 C   sz   t  ^ t| j  |  D ]0\}}| jd urF|j| jd}|||| q&W d    n1 sl0    Y  d S r5   )	r   r)   zipr	   r*   valuesr   r   r,   )r   r   	update_fnr-   r.   r   r   r   _updates   s
    
$
zModelEmaV2._updatec                    s    j | fddd d S )Nc                    s    j |  d j  |  S )Nr(   )r   emr   r   r   <lambda>{       z#ModelEmaV2.update.<locals>.<lambda>r;   r<   r   r   r   r@   r   r/   z   s    zModelEmaV2.updatec                 C   s   | j |dd d d S )Nc                 S   s   |S )Nr   r=   r   r   r   rA   ~   rB   z ModelEmaV2.set.<locals>.<lambda>rC   rD   rE   r   r   r   set}   s    zModelEmaV2.set)r   N)	r0   r1   r2   r3   r   r<   r/   rF   __classcell__r   r   r7   r   r4   R   s
   
r4   )r3   loggingcollectionsr   copyr   r   Ztorch.nnnn	getLoggerr0   r!   r   Moduler4   r   r   r   r   <module>   s   
C