o
    #iA                     @   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|r-tjdtjdntjdtjd |	 D ] \}}|j
rZ|d	d
}| j||i | ||  j q:g | _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   Zuse_num_upatesnamepZs_name	__class__ 1/data/cameron/vidgen/VidTok/vidtok/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 F t| }t|  }|D ].}|| jr[| j	| }|| 
|| ||< || ||| ||    q4|| j	vsbJ q4W d    d S 1 snw   Y  d S )Nr      
   r   )r   r	   minr   no_graddictr   named_buffersr   r   type_assub_)r   r   r   Zone_minus_decaym_paramshadow_paramskeysnamer$   r$   r%   forward   s    



 "zLitEma.forwardc                 C   s\   t | }t |  }|D ]}|| jr$|| j|| j|  j q|| jvs+J qd S )N)r+   r   r,   r   r   copy_r   )r   r   r/   r0   r1   r$   r$   r%   copy_to4   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>D   s    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   r4   )r   r9   Zc_paramr7   r$   r$   r%   restoreF   s   zLitEma.restore)r   T)
__name__
__module____qualname__r   r&   r3   r5   r:   r<   __classcell__r$   r$   r"   r%   r      s    		r   )r   r   Moduler   r$   r$   r$   r%   <module>   s    