
    Ng2                     V    d dl Z d dl mZ d dlmZmZmZmZ d dlZ G d de          ZdS )    N)ABC)AnyDictListOptionalc                   >   e Zd ZdZ	 	 	 	 	 	 	 ddej        j        ded	ed
eddf
dZ	de
eef         fdZde
eef         ddfdZej        dedee         fd            Zddededeee                  fdZddededdfdZddedefdZd Zd ZdefdZdefdZdS ) 	Schedulera   Parameter Scheduler Base Class
    A scheduler base class that can be used to schedule any optimizer parameter groups.

    Unlike the builtin PyTorch schedulers, this is intended to be consistently called
    * At the END of each epoch, before incrementing the epoch count, to calculate next epoch's value
    * At the END of each optimizer update, after incrementing the update count, to calculate next update's value

    The schedulers built on this should try to remain as stateless as possible (for simplicity).

    This family of schedulers is attempting to avoid the confusion of the meaning of 'last_epoch'
    and -1 values for special behaviour. All epoch and update counts must be tracked in the training
    code and explicitly passed in to the schedulers on the corresponding step or step_update call.

    Based on ideas from:
     * https://github.com/pytorch/fairseq/tree/master/fairseq/optim/lr_scheduler
     * https://github.com/allenai/allennlp/tree/master/allennlp/training/learning_rate_schedulers
    TNnormalq=
ףp?      ?	optimizerparam_group_fieldt_in_epochs
initializereturnc
                 L    | _         | _        d|  _        |	rZt           j         j                  D ]?\  }
}||vrt          | d|
 d          |                     j        ||                    @nBt           j         j                  D ](\  }
} j        |vrt           j         d|
 d          ) fd j         j        D              _        d  _        | _	        | _
        | _        | _        | _        ||nd _                              j                   d S )Ninitial_z missing from param_groups[]c                 *    g | ]}|j                  S  )_initial_param_group_field).0groupselfs     T/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/scheduler/scheduler.py
<listcomp>z&Scheduler.__init__.<locals>.<listcomp>3   s!    llluE$"ABlll    *   )r   r   r   	enumerateparam_groupsKeyError
setdefaultbase_valuesmetricr   noise_range_t	noise_pct
noise_type	noise_std
noise_seedupdate_groups)r   r   r   r   r%   r'   r&   r(   r)   r   ir   s   `           r   __init__zScheduler.__init__   s{    #!2*H5F*H*H' 	h%dn&ABB \ \5$E11"&7#X#XTU#X#X#XYYY  !@%HYBZ[[[[\
 &dn&ABB h h52%??"d&E#f#fbc#f#f#fggg @llllPTP^Pklll&*"$"(2(>**B4+,,,,,r   c                 H    d | j                                         D             S )Nc                 &    i | ]\  }}|d k    ||S )r   r   )r   keyvalues      r   
<dictcomp>z(Scheduler.state_dict.<locals>.<dictcomp>>   s(    YYYzsEc[FXFXUFXFXFXr   )__dict__items)r   s    r   
state_dictzScheduler.state_dict=   s$    YYT]-@-@-B-BYYYYr   r4   c                 :    | j                             |           d S N)r2   update)r   r4   s     r   load_state_dictzScheduler.load_state_dict@   s    Z(((((r   tc                     d S r6   r   )r   r9   s     r   _get_lrzScheduler._get_lrC   s    r   on_epochc                 \    |r| j         p
| o| j          }|sd S |                     |          S r6   )r   r;   )r   r9   r<   proceeds       r   _get_valueszScheduler._get_valuesG   s>    0 0\(l6[4K[G[ 	4||Ar   epochr$   c                     || _         |                     |d          }|-|                     ||          }|                     |           d S d S )NTr<   r$   r?   
_add_noiser*   )r   r@   r$   valuess       r   stepzScheduler.stepM   s\    !!%$!77__VU33Fv&&&&& r   num_updatesc                     || _         |                     |d          }|-|                     ||          }|                     |           d S d S )NFrB   rC   )r   rG   r$   rE   s       r   step_updatezScheduler.step_updateT   s\    !!+!>>__V[99Fv&&&&& r   c                     t          |t          t          f          s|gt          | j        j                  z  }t          | j        j        |          D ]'\  }}d|v r||d         z  || j        <   ||| j        <   (d S )Nlr_scale)
isinstancelisttuplelenr   r    zipr   )r   rE   param_groupr0   s       r   r*   zScheduler.update_groups[   s    &4-00 	AXDN$? @ @@F"%dn&A6"J"J 	< 	<K[((6;k*>U6UD2336;D233		< 	<r   c                 x    |                      |          r#|                     |          fd|D             }|S )Nc                      g | ]
}||z  z   S r   r   )r   vnoises     r   r   z(Scheduler._add_noise.<locals>.<listcomp>g   s!    ...Q1q5y=...r   )_is_apply_noise_calculate_noise)r   lrsr9   rU   s      @r   rD   zScheduler._add_noised   sJ    "" 	/))!,,E....#...C
r   c                     d}| j         St          | j         t          t          f          r'| j         d         |cxk    o| j         d         k     nc }n|| j         k    }|S )z(Return True if scheduler in noise range.FNr      )r%   rL   rM   rN   )r   r9   apply_noises      r   rV   zScheduler._is_apply_noisej   so    )$,tUm<< 6"03qPPPP4;Ma;PPPPP4#55r   c                 p   t          j                    }|                    | j        |z              | j        dk    rD	 t          j        d|                                          }t          |          | j        k     r|S Cdt          j	        d|                                          dz
  z  | j        z  }|S )Nr
   TrZ   )	generator   g      ?)
torch	Generatormanual_seedr)   r'   randnitemabsr&   rand)r   r9   grU   s       r   rW   zScheduler._calculate_noiset   s    O	do)***?h&&!A33388::u::.. L	! A33388::S@ADNREr   )TNr
   r   r   NT)Tr6   )__name__
__module____qualname____doc__r_   optim	Optimizerstrboolr,   r   r   r4   r8   abcabstractmethodintr   floatr;   r   r?   rF   rI   r*   rD   rV   rW   r   r   r   r	   r	      s        , !%# -  -{, -  # - 	 -  - 
 -  -  -  -DZDcN Z Z Z Z)$sCx. )T ) ) ) ) 	 e     S D HT%[<Q    ' '# 'u ' ' ' ' '' 's 'E ' ' ' '< < <  D    U      r   r	   )	ro   r   typingr   r   r   r   r_   r	   r   r   r   <module>rt      s    



       , , , , , , , , , , , , w w w w w w w w w wr   