a
    d                     @   s,   d Z ddlZddlmZ G dd deZdS )z Plateau Scheduler

Adapts PyTorch plateau scheduler and allows application of noise, warmup.

Hacked together by / Copyright 2020 Ross Wightman
    N   )	Schedulerc                       sj   e Zd ZdZd fdd	Zdd Zdd Zd fdd	Zdee	dddZ
dd Zee	dddZ  ZS )PlateauLRSchedulerzADecay the LR by a factor every time the validation loss plateaus.皙?
   T-C6?r   maxNnormalq=
ףp?      ?c              
      s   t  j|d||||||d tjjj j||||||
|	d _| _ _ jrx fdd j	D  _
t   j ndd  j	D  _
d  _d S )Nlr)noise_range_t
noise_type	noise_pct	noise_std
noise_seed
initialize)ZpatienceZfactorverbose	thresholdZcooldownmodeZmin_lrc                    s   g | ]}|  j  qS  )warmup_t).0vselfwarmup_lr_initr   b/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/timm/scheduler/plateau_lr.py
<listcomp>;       z/PlateauLRScheduler.__init__.<locals>.<listcomp>c                 S   s   g | ]}d qS )r   r   )r   _r   r   r   r   >   r   )super__init__torchZoptimlr_schedulerZReduceLROnPlateau	optimizerr   r   Zbase_valueswarmup_stepsupdate_groups
restore_lr)r   r%   Z
decay_rateZ
patience_tr   r   Z
cooldown_tr   r   Zlr_minr   r   r   r   r   r   r   	__class__r   r   r"      s6    zPlateauLRScheduler.__init__c                 C   s   | j j| j jdS )N)best
last_epochr$   r+   r,   )r   r   r   r   
state_dictA   s    zPlateauLRScheduler.state_dictc                 C   s$   |d | j _d|v r |d | j _d S )Nr+   r,   r-   )r   r.   r   r   r   load_state_dictG   s    z"PlateauLRScheduler.load_state_dictc                    s    j kr. fddjD }t | nVjd urbtjjD ]\}}j| |d< qDd _j	|  
 r  d S )Nc                    s   g | ]}j  |  qS r   )r   )r   sepochr   r   r   r   O   r   z+PlateauLRScheduler.step.<locals>.<listcomp>r   )r   r&   r!   r'   r(   	enumerater%   param_groupsr$   stepZ_is_apply_noise_apply_noise)r   r2   metricZlrsiparam_groupr)   r1   r   r5   M   s    


zPlateauLRScheduler.step)num_updatesr7   c                 C   s   d S )Nr   )r   r:   r7   r   r   r   step_update]   s    zPlateauLRScheduler.step_updatec                 C   sX   |  |}g }t| jjD ]2\}}t|d }|| |||  }||d< q|| _d S )Nr   )Z_calculate_noiser3   r%   r4   floatappendr(   )r   r2   Znoiser(   r8   r9   Zold_lrZnew_lrr   r   r   r6   `   s    


zPlateauLRScheduler._apply_noise)treturnc                 C   s   dsJ dd S )NFz*should not be called as step is overriddenr   )r   r>   r   r   r   _get_lrm   s    zPlateauLRScheduler._get_lr)r   r   Tr   r   r   r   r   r   Nr	   r
   r   NT)N)N)__name__
__module____qualname____doc__r"   r.   r/   r5   intr<   r;   r6   r@   __classcell__r   r   r)   r   r      s.                  2r   )rD   r#   Z	schedulerr   r   r   r   r   r   <module>   s   