a
    
d&                     @   s*   d dl Z d dl mZ G dd dejZdS )    N)nnc                       sF   e Zd Zd fdd	Zdd Zdd Zd	d
 Zdd Zdd Z  Z	S )LitEmaH.?Tc                    s   t    |dk s|dkr"tdi | _| dtj|tjd | d|rZtjdtjdntjdtjd |	 D ]@\}}|j
rt|d	d
}| j||i | ||  j qtg | _d S )Ng              ?zDecay must be between 0 and 1decaydtypenum_updatesr   . )super__init__
ValueErrorm_name2s_nameregister_buffertorchtensorfloat32intnamed_parametersrequires_gradreplaceupdateclonedetachdatacollected_params)selfmodelr   use_num_upatesnameps_name	__class__ b/var/www/html/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/modules/ema.pyr      s    
zLitEma.__init__c                 C   s    | ` | dtjdtjd d S )Nr	   r   r   )r	   r   r   r   r   )r   r&   r&   r'   reset_num_updates   s    zLitEma.reset_num_updatesc                 C   s   | j }| jdkr:|  jd7  _t| j d| j d| j  }d| }t  t| }t|  }|D ]\}|| jr| j	| }|| 
|| ||< || ||| ||    qh|| j	vshJ qhW d    n1 s0    Y  d S )Nr      
   r   )r   r	   minr   no_graddictr   named_buffersr   r   type_assub_)r   r   r   one_minus_decaym_paramshadow_paramskeysnamer&   r&   r'   forward   s    



 zLitEma.forwardc                 C   s\   t | }t |  }|D ]:}|| jrH|| j|| j|  j q|| jvsJ qd S )N)r-   r   r.   r   r   copy_r   )r   r   r2   r3   r4   r&   r&   r'   copy_to2   s    
zLitEma.copy_toc                 C   s   dd |D | _ dS )z
        Save the current parameters for restoring later.
        Args:
          parameters: Iterable of `torch.nn.Parameter`; the parameters to be
            temporarily stored.
        c                 S   s   g | ]}|  qS r&   )r   ).0paramr&   r&   r'   
<listcomp>B       z LitEma.store.<locals>.<listcomp>N)r   )r   
parametersr&   r&   r'   store;   s    zLitEma.storec                 C   s(   t | j|D ]\}}|j|j qdS )a  
        Restore the parameters stored with the `store` method.
        Useful to validate the model with EMA parameters without affecting the
        original optimization process. Store the parameters before the
        `copy_to` method. After validation (or model saving), use this to
        restore the former parameters.
        Args:
          parameters: Iterable of `torch.nn.Parameter`; the parameters to be
            updated with the stored parameters.
        N)zipr   r   r7   )r   r=   c_paramr:   r&   r&   r'   restoreD   s    zLitEma.restore)r   T)
__name__
__module____qualname__r   r(   r6   r8   r>   rA   __classcell__r&   r&   r$   r'   r      s   		r   )r   r   Moduler   r&   r&   r&   r'   <module>   s   