
    bi9                         d dl Z d dlZd dlZd dlmZ d dlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ d Zd Z G d d      Z G d d      Z G d de      Z G d de
      Z G d d      Z G d d      Zy)    N)deepcopy)optim   )AcceleratedOptimizer)AcceleratedScheduler   )DistributedType)is_bnb_available)compare_versionsc                 
   | j                   j                         D ci c]  \  }}|dv s|| }}}ddlm} |}t	        ddd      rmd|d<   t        | t        j                        }t               r=|s;dd	l	m} t        | |j                  |j                  f      r	 | j                  d
k(  }nd}|rd|d<   t	        ddd      rht        | t        j                        }t               r:|s8dd	l	m} t        | |j                  |j                  f      r	 | j                  d
k(  }|rddlm}	 |	}t        d      r=t	        ddd      r0ddl	m}
m} t        | |
|f      r	 | j                  d
k(  }|rddlm} |} || j,                  fi |S c c}}w # t        $ r | j                  j                  d
k(  }Y w xY w# t        $ r | j                  j                  d
k(  }Y w xY w# t        $ r | j                  j                  d
k(  }Y w xY w)z
    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                 U/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/accelerate/utils/deepspeed.pymap_pytorch_optim_to_deepspeedr0      s    "+!3!3!9!9!;[AqDZ?Z1[H[ 4&O T73!&Y4g0)gmmW5G5G%HI>'22b8G  %)H\" T73Iu}}5 f0)goow7K7K%LM=&11R7F A1O H-2B;PTV^2_6i$	!23@ ) 4 4 : ?"2911>X>>y \& & >'nn772=G>( & =&^^66"<F= " @ ) 9 9R ?@s@   FFF F5 G "F21F25"GG"HHc                     | j                   t        j                  k7  rt        d      t	        | j
                  t              s| j
                  S t        d | j
                  j                         D              S )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   :   K   | ]  }|j                   s|  y wN)selected).0plugins     r/   	<genexpr>z.get_active_deepspeed_plugin.<locals>.<genexpr>t   s     Y6Ys   )	distributed_typer	   	DEEPSPEED
ValueErrorr   deepspeed_pluginsdictnextvalues)states    r/   get_active_deepspeed_pluginr@   d   sh     !:!::,
 	
 e--t4&&&YU%<%<%C%C%EYYY    c                   P    e Zd ZdZd Zd Zd ZddZddZd Z	d	 Z
d
 Zd Zd Zy)HfDeepSpeedConfigaJ  
    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                 6   t        |t              rt        |      }nbt        j                  j                  |      r,t        |d      5 }t        j                  |      }d d d        n	 	 t        j                  |      }| _        | j%                          y # 1 sw Y   !xY w# t        j                  $ r< t        j                  |      j                  d      }t        j                  |      }Y rw xY w# t        t        t         f$ r t!        d|       w xY w)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_dictrR   fconfig_decodeds        r/   __init__zHfDeepSpeedConfig.__init__   s   )40 12FWW^^/0)G< &1& &8!ZZ(;<F ""$%& & ++ 8%+%=%=>Q%R%Y%YZa%bN!ZZ7F8 '
C   F  GZ  F[  \ s1   	B*B# B #AC2/C5 1C22C5 5#Dc                 $   | j                  dd      | _        d| _        | j                         s| j	                         rRt        ddg      }t        | j                  d      | j                  d      g      }t        ||z        dkD  rd	| _        y y y )
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)rT   offload_devices_validoffload_devicess      r/   rS   z'HfDeepSpeedConfig.set_stage_and_offload   s     nn%>C ==?dmmo$'$8!!NN#OPNN#KLO ?%::;a? $ @ .rA   c                     | j                   }|j                  d      }|j                         }|D ]  }|j                  |      }|d |fc S  ||fS )N.)rR   splitpopget)rT   ds_key_longrR   nodesds_keynodes         r/   find_config_nodez"HfDeepSpeedConfig.find_config_node   s`     !!#& 	$DZZ%F~V|#	$
 v~rA   Nc                 V    | j                  |      \  }}||S |j                  ||      S )zG
        Returns the set value or `default` if no value is set
        )ro   rj   )rT   rk   defaultrR   rm   s        r/   r]   zHfDeepSpeedConfig.get_value   s3     ..{;>Nzz&'**rA   c                     | j                   }|j                  d      }|D ]6  }|}|j                  |      }||rt        d| d| j                           y |j	                         yy)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.
        rg   NzCan't find z entry in the config: )rR   rh   rj   r:   ri   )rT   rk   
must_existrR   rl   rn   parent_configs          r/   del_config_sub_treez%HfDeepSpeedConfig.del_config_sub_tree   s      !!#& 	D"MZZ%F~${;-?UVZVaVaUb%cdd	 $d# %rA   c                 B    | j                  |      }|dS t        |      S )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rT   rk   values      r/   is_truezHfDeepSpeedConfig.is_true   s%     {+u64;6rA   c                 D    | j                  |      }|dS t        |       S )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).
        Frw   ry   s      r/   is_falsezHfDeepSpeedConfig.is_false   s'    
 {+u:tE{?:rA   c                      | j                   dk(  S )Nr   r^   rT   s    r/   r`   zHfDeepSpeedConfig.is_zero2       {{arA   c                      | j                   dk(  S )N   r   r   s    r/   ra   zHfDeepSpeedConfig.is_zero3   r   rA   c                     | j                   S r3   )r_   r   s    r/   
is_offloadzHfDeepSpeedConfig.is_offload   s    }}rA   r3   )F)__name__
__module____qualname____doc__rX   rS   ro   r]   ru   r{   r}   r`   ra   r    rA   r/   rC   rC   w   s9     %4%&+$.7;  rA   rC   c                   $    e Zd ZdZd ZddZd Zy)DeepSpeedEngineWrapperz
    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                     || _         y r3   )engine)rT   r   s     r/   rX   zDeepSpeedEngineWrapper.__init__  s	    rA   c                     | j                   j                  |        | j                   j                  |fi | |r| j                   j                          y y )N)is_boundary)r   "set_gradient_accumulation_boundarybackwardstep)rT   losssync_gradientskwargss       r/   r   zDeepSpeedEngineWrapper.backward  sP     	66>6R 	T,V,  KK rA   c                 r    | j                   j                         }t        |d      r|j                         S |S )z3Get the global gradient norm from DeepSpeed engine.item)r   get_global_grad_normhasattrr   )rT   	grad_norms     r/   r   z+DeepSpeedEngineWrapper.get_global_grad_norm  s1    KK446	9f%>>##rA   N)T)r   r   r   r   rX   r   r   r   rA   r/   r   r      s    ,rA   r   c                   @     e Zd ZdZ fdZddZd Zed        Z xZ	S )DeepSpeedOptimizerWrapperz
    Internal wrapper around a deepspeed optimizer.

    Args:
        optimizer (`torch.optim.optimizer.Optimizer`):
            The optimizer to wrap.
    c                 `    t         |   |dd        t        | j                  d      | _        y )NF)device_placementscaleroverflow)superrX   r   r'   __has_overflow__)rT   r'   	__class__s     r/   rX   z"DeepSpeedOptimizerWrapper.__init__0  s*    U4H '
 CrA   c                      y r3   r   )rT   set_to_nones     r/   	zero_gradz#DeepSpeedOptimizerWrapper.zero_grad4      rA   c                      y r3   r   r   s    r/   r   zDeepSpeedOptimizerWrapper.step7  r   rA   c                 H    | j                   r| j                  j                  S y)zTWhether or not the optimizer step was done, or skipped because of gradient overflow.F)r   r'   r   r   s    r/   step_was_skippedz*DeepSpeedOptimizerWrapper.step_was_skipped:  s        >>***rA   r3   )
r   r   r   r   rX   r   r   propertyr   __classcell__r   s   @r/   r   r   '  s,    D  rA   r   c                   (     e Zd ZdZ fdZd Z xZS )DeepSpeedSchedulerWrapperz
    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         |   ||       y r3   )r   rX   )rT   	scheduler
optimizersr   s      r/   rX   z"DeepSpeedSchedulerWrapper.__init__L  s    J/rA   c                      y r3   r   r   s    r/   r   zDeepSpeedSchedulerWrapper.stepO  r   rA   )r   r   r   r   rX   r   r   r   s   @r/   r   r   B  s    0rA   r   c                       e Zd ZdZddZy)
DummyOptima  
    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                 <    || _         || _        || _        || _        y r3   )paramsr   r   r   )rT   r   r   r   r   s        r/   rX   zDummyOptim.__init__c  s    (rA   N)gMbP?r   r   r   r   r   rX   r   rA   r/   r   r   S  s    rA   r   c                       e Zd ZdZddZy)DummySchedulera  
    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                 J    || _         || _        || _        || _        || _        y r3   )r'   total_num_stepswarmup_num_stepslr_scheduler_callabler   )rT   r'   r   r   r   r   s         r/   rX   zDummyScheduler.__init__|  s(    ". 0%:"rA   )Nr   Nr   r   rA   r/   r   r   j  s    "rA   r   )rN   rJ   rF   copyr   torchr   r'   r   r   r   dataclassesr	   importsr
   versionsr   r0   r@   rC   r   r   r   r   r   r   rA   r/   <module>r      sx      	   , , ( % &D?NZ&C CL' 'T 4 6 4 " . rA   