
    ;i9                         S SK r S SKrS SKrS SKJr  S SKJr  SSKJr  SSK	J
r
  SSKJr  SS	KJr  SS
KJr  S rS r " S S5      r " S S5      r " S S\5      r " S S\
5      r " S S5      r " S S5      rg)    N)deepcopy)optim   )AcceleratedOptimizer)AcceleratedScheduler   )DistributedType)is_bnb_available)compare_versionsc                    U R                   R                  5        VVs0 s H  u  pUS;   d  M  X_M     nnnSSKJn  Un[	        SSS5      (       a  SUS'   [        U [        R                  5      n[        5       (       aF  U(       d?  SS	K	Jn  [        XR                  UR                  45      (       a   U R                  S
:H  nOSnU(       a  SUS'   [	        SSS5      (       a{  [        U [        R                  5      n[        5       (       aC  U(       d<  SS	K	Jn  [        XR                  UR                  45      (       a   U R                  S
:H  nU(       a  SSKJn	  U	n[        SS9(       aK  [	        SSS5      (       a9  SSK	Jn
Jn  [        X
U45      (       a   U R                  S
:H  nU(       a  SSKJn  UnU" U R,                  40 UD6$ s  snnf ! [         a    U R                  R                  S
:H  n GN1f = f! [         a    U R                  R                  S
:H  n Nf = f! [         a    U R                  R                  S
:H  n Nf = f)zx
Args:
    optimizer: torch.optim.Optimizer

Returns the DeepSeedCPUOptimizer (deepspeed.ops) version of the optimizer.
)lrweight_decayr   )DeepSpeedCPUAdam	deepspeedz>=z0.3.1F
adamw_modeN    Tz0.5.5)DeepSpeedCPUAdagradz0.38.0)min_versionz0.11.0)Lion	Lion32bit)DeepSpeedCPULion)defaultsitemsdeepspeed.ops.adamr   r   
isinstancer   AdamWr
   bitsandbytes.optim
AdamW32bit
optim_bitsAttributeErrorargsAdagradAdagrad32bitdeepspeed.ops.adagradr   r   r   deepspeed.ops.lionr   param_groups)	optimizerkvr   r   optimizer_classis_adawbnb_optis_adar   r   r   is_bnb_32bitsr   s                 j/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/accelerate/utils/deepspeed.pymap_pytorch_optim_to_deepspeedr0      s    "+!3!3!9!9!;[!;qDZ?Z!;H[ 4&O T733!&Y4g0)mmW5G5G%HII>'22b8G  %)H\" T733Iu}}5 f0)oow7K7K%LMM=&11R7F A1O H-2B;PTV^2_2_6i	!233@ ) 4 4 : ?"2911>X>>y \& & >'nn772=G>( & =&^^66"<F= " @ ) 9 9R ?@s@   G
G
3G  G: H# #G76G7:#H H ##I	I	c                     U R                   [        R                  :w  a  [        S5      e[	        U R
                  [        5      (       d  U R
                  $ [        S U R
                  R                  5        5       5      $ )z
Returns the currently active DeepSpeedPlugin.

Raises:
    ValueError: If DeepSpeed was not enabled and this function is called.
a!  Couldn't retrieve the active `DeepSpeedPlugin` as none were enabled. Please make sure that either `Accelerator` is configured for `deepspeed` or make sure that the desired `DeepSpeedPlugin` has been enabled (`AcceleratorState().select_deepspeed_plugin(name)`) before calling this function.c              3   J   #    U  H  oR                   (       d  M  Uv   M     g 7fN)selected).0plugins     r/   	<genexpr>.get_active_deepspeed_plugin.<locals>.<genexpr>t   s     Y%E6%Es   #	#)	distributed_typer	   	DEEPSPEED
ValueErrorr   deepspeed_pluginsdictnextvalues)states    r/   get_active_deepspeed_pluginrA   d   sk     !:!::,
 	
 e--t44&&&YU%<%<%C%C%EYYY    c                   \    \ rS rSrSrS rS rS rSS jrSS jr	S	 r
S
 rS rS rS rSrg)HfDeepSpeedConfigw   a"  
This object contains a DeepSpeed configuration dictionary and can be quickly queried for things like zero stage.

A `weakref` of this object is stored in the module's globals to be able to access the config from areas where
things like the Trainer object is not available (e.g. `from_pretrained` and `_get_resized_embeddings`). Therefore
it's important that this object remains alive while the program is still running.

[`Trainer`] uses the `HfTrainerDeepSpeedConfig` subclass instead. That subclass has logic to sync the configuration
with values of [`TrainingArguments`] by replacing special placeholder values: `"auto"`. Without this special logic
the DeepSpeed configuration is not modified in any way.

Args:
    config_file_or_dict (`Union[str, Dict]`): path to DeepSpeed config file or dict.

c                 Z   [        U[        5      (       a  [        U5      nOf[        R                  R                  U5      (       a*  [        USS9 n[        R                  " U5      nS S S 5        O  [        R                  " U5      nWU l        U R%                  5         g ! , (       d  f       N&= f! [        R                   a>    [        R                  " U5      R                  S5      n[        R                  " U5      n Nzf = f! [        [        [         4 a    [!        SU 35      ef = f)Nzutf-8)encodingzoExpected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: )r   r=   r   ospathexistsopenjsonloadloadsJSONDecodeErrorbase64urlsafe_b64decodedecodeUnicodeDecodeErrorr    r;   configset_stage_and_offload)selfconfig_file_or_dictrT   fconfig_decodeds        r/   __init__HfDeepSpeedConfig.__init__   s	   )400 12FWW^^/00)G<1 =<8!ZZ(;<F ""$% =< ++ 8%+%=%=>Q%R%Y%YZa%bN!ZZ7F8 '
C   F  GZ  F[  \ s1   B 2B1  
B.1AD D DD $D*c                 8   U R                  SS5      U l        SU l        U R                  5       (       d  U R	                  5       (       aR  [        SS/5      n[        U R                  S5      U R                  S5      /5      n[        X!-  5      S:  a  S	U l        g g g )
Nzzero_optimization.stageFcpunvmez*zero_optimization.offload_optimizer.devicez&zero_optimization.offload_param.devicer   T)	get_value_stage_offloadis_zero2is_zero3setlen)rV   offload_devices_validoffload_devicess      r/   rU   'HfDeepSpeedConfig.set_stage_and_offload   s     nn%>C ==??dmmoo$'$8!!NN#OPNN#KLO ?:;a? $ @ .rB   c                     U R                   nUR                  S5      nUR                  5       nU H  nUR                  U5      nUb  M  S U4s  $    X$4$ )N.)rT   splitpopget)rV   ds_key_longrT   nodesds_keynodes         r/   find_config_node"HfDeepSpeedConfig.find_config_node   sY     !!#&DZZ%F~V|# 
 ~rB   Nc                 T    U R                  U5      u  p4Uc  U$ UR                  XB5      $ )z7
Returns the set value or `default` if no value is set
)rs   rn   )rV   ro   defaultrT   rq   s        r/   r`   HfDeepSpeedConfig.get_value   s/     ..{;>Nzz&**rB   c                     U R                   nUR                  S5      nU H=  nUnUR                  U5      nUb  M  U(       a  [        SU SU R                    35      e  g   Wb  UR	                  W5        gg)z{
Deletes a sub-section of the config file if it's found.

Unless `must_exist` is `True` the section doesn't have to exist.
rk   NzCan't find z entry in the config: )rT   rl   rn   r;   rm   )rV   ro   
must_existrT   rp   rr   parent_configs          r/   del_config_sub_tree%HfDeepSpeedConfig.del_config_sub_tree   s      !!#&D"MZZ%F~${;-?UVZVaVaUb%cdd  $d# %rB   c                 D    U R                  U5      nUc  S$ [        U5      $ )z
Returns `True`/``False` only if the value is set, always `False` otherwise. So use this method to ask the very
specific question of whether the value is set to `True` (and it's not set to `False`` or isn't set).

Fr`   boolrV   ro   values      r/   is_trueHfDeepSpeedConfig.is_true   s%     {+u64;6rB   c                 N    U R                  U5      nUc  S$ [        U5      (       + $ )z
Returns `True`/``False` only if the value is set, always `False` otherwise. So use this method to ask the very
specific question of whether the value is set to `False` (and it's not set to `True`` or isn't set).
Fr~   r   s      r/   is_falseHfDeepSpeedConfig.is_false   s'    
 {+u:tE{?:rB   c                      U R                   S:H  $ )Nr   ra   rV   s    r/   rc   HfDeepSpeedConfig.is_zero2       {{arB   c                      U R                   S:H  $ )N   r   r   s    r/   rd   HfDeepSpeedConfig.is_zero3   r   rB   c                     U R                   $ r3   )rb   r   s    r/   
is_offloadHfDeepSpeedConfig.is_offload   s    }}rB   )rb   ra   rT   r3   )F)__name__
__module____qualname____firstlineno____doc__rZ   rU   rs   r`   r{   r   r   rc   rd   r   __static_attributes__ rB   r/   rD   rD   w   s9     %4%&+$.7;  rB   rD   c                   .    \ rS rSrSrS rSS jrS rSrg)	DeepSpeedEngineWrapper   z
Internal wrapper for deepspeed.runtime.engine.DeepSpeedEngine. This is used to follow conventional training loop.

Args:
    engine (deepspeed.runtime.engine.DeepSpeedEngine): deepspeed engine to wrap
c                     Xl         g r3   engine)rV   r   s     r/   rZ   DeepSpeedEngineWrapper.__init__  s    rB   c                     U R                   R                  US9  U R                   R                  " U40 UD6  U(       a  U R                   R                  5         g g )N)is_boundary)r   "set_gradient_accumulation_boundarybackwardstep)rV   losssync_gradientskwargss       r/   r   DeepSpeedEngineWrapper.backward  sN     	66>6R 	T,V,  KK rB   c                 |    U R                   R                  5       n[        US5      (       a  UR                  5       $ U$ )z3Get the global gradient norm from DeepSpeed engine.item)r   get_global_grad_normhasattrr   )rV   	grad_norms     r/   r   +DeepSpeedEngineWrapper.get_global_grad_norm  s4    KK446	9f%%>>##rB   r   N)T)	r   r   r   r   r   rZ   r   r   r   r   rB   r/   r   r      s    ,rB   r   c                   L   ^  \ rS rSrSrU 4S jrSS jrS r\S 5       r	Sr
U =r$ )	DeepSpeedOptimizerWrapperi'  z
Internal wrapper around a deepspeed optimizer.

Args:
    optimizer (`torch.optim.optimizer.Optimizer`):
        The optimizer to wrap.
c                 Z   > [         TU ]  USS S9  [        U R                  S5      U l        g )NF)device_placementscaleroverflow)superrZ   r   r'   __has_overflow__)rV   r'   	__class__s     r/   rZ   "DeepSpeedOptimizerWrapper.__init__0  s*    U4H '
 CrB   c                     g r3   r   )rV   set_to_nones     r/   	zero_grad#DeepSpeedOptimizerWrapper.zero_grad4      rB   c                     g r3   r   r   s    r/   r   DeepSpeedOptimizerWrapper.step7  r   rB   c                 R    U R                   (       a  U R                  R                  $ g)zTWhether or not the optimizer step was done, or skipped because of gradient overflow.F)r   r'   r   r   s    r/   step_was_skipped*DeepSpeedOptimizerWrapper.step_was_skipped:  s        >>***rB   )r   r3   )r   r   r   r   r   rZ   r   r   propertyr   r   __classcell__r   s   @r/   r   r   '  s,    D  rB   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )DeepSpeedSchedulerWrapperiB  z
Internal wrapper around a deepspeed scheduler.

Args:
    scheduler (`torch.optim.lr_scheduler.LambdaLR`):
        The scheduler to wrap.
    optimizers (one or a list of `torch.optim.Optimizer`):
c                 $   > [         TU ]  X5        g r3   )r   rZ   )rV   	scheduler
optimizersr   s      r/   rZ   "DeepSpeedSchedulerWrapper.__init__L  s    /rB   c                     g r3   r   r   s    r/   r   DeepSpeedSchedulerWrapper.stepO  r   rB   r   )	r   r   r   r   r   rZ   r   r   r   r   s   @r/   r   r   B  s    0 rB   r   c                   "    \ rS rSrSrSS jrSrg)
DummyOptimiS  a  
Dummy optimizer presents model parameters or param groups, this is primarily used to follow conventional training
loop when optimizer config is specified in the deepspeed config file.

Args:
    lr (float):
        Learning rate.
    params (iterable): iterable of parameters to optimize or dicts defining
        parameter groups
    weight_decay (float):
        Weight decay.
    **kwargs (additional keyword arguments, *optional*):
        Other arguments.
c                 4    Xl         X l        X0l        X@l        g r3   )paramsr   r   r   )rV   r   r   r   r   s        r/   rZ   DummyOptim.__init__c  s    (rB   )r   r   r   r   N)gMbP?r   r   r   r   r   r   rZ   r   r   rB   r/   r   r   S  s    rB   r   c                   "    \ rS rSrSrSS jrSrg)DummySchedulerij  a  
Dummy scheduler presents model parameters or param groups, this is primarily used to follow conventional training
loop when scheduler config is specified in the deepspeed config file.

Args:
    optimizer (`torch.optim.optimizer.Optimizer`):
        The optimizer to wrap.
    total_num_steps (int, *optional*):
        Total number of steps.
    warmup_num_steps (int, *optional*):
        Number of steps for warmup.
    lr_scheduler_callable (callable, *optional*):
        A callable function that creates an LR Scheduler. It accepts only one argument `optimizer`.
    **kwargs (additional keyword arguments, *optional*):
        Other arguments.
Nc                 @    Xl         X l        X0l        X@l        XPl        g r3   )r'   total_num_stepswarmup_num_stepslr_scheduler_callabler   )rV   r'   r   r   r   r   s         r/   rZ   DummyScheduler.__init__|  s    ". 0%:"rB   )r   r   r'   r   r   )Nr   Nr   r   rB   r/   r   r   j  s    "rB   r   )rP   rL   rH   copyr   torchr   r'   r   r   r   dataclassesr	   importsr
   versionsr   r0   rA   rD   r   r   r   r   r   r   rB   r/   <module>r      sx      	   , , ( % &D?NZ&C CL' 'T 4 6 4 " . rB   