
    g+                         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 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dS )    N)deepcopy   )AcceleratedOptimizer)AcceleratedScheduler   )DistributedTypec                     | j         t          j        k    rt          d          t	          | j        t                    s| j        S t          d | 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         	|V  d S N)selected).0plugins     V/var/www/html/ai-engine/env/lib/python3.11/site-packages/accelerate/utils/deepspeed.py	<genexpr>z.get_active_deepspeed_plugin.<locals>.<genexpr>)   s)      YY6YYYYYYY    )	distributed_typer   	DEEPSPEED
ValueError
isinstancedeepspeed_pluginsdictnextvalues)states    r   get_active_deepspeed_pluginr      sx     !:::,
 
 	
 e-t44 '&&YYU%<%C%C%E%EYYYYYYr   c                   R    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dS )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                    t          |t                    rt          |          }nt          j                            |          r>t          |d          5 }t          j        |          }d d d            n# 1 swxY w Y   nj	 t          j
        |                              d          }t          j        |          }n-# t          t          t          f$ r t          d|           w xY w|| _        |                                  d S )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base64urlsafe_b64decodedecodeloadsUnicodeDecodeErrorAttributeErrorr   configset_stage_and_offload)selfconfig_file_or_dictr,   fconfig_decodeds        r   __init__zHfDeepSpeedConfig.__init__=   sV   )400 	 122FFW^^/00 
	)G<<< &1& & & & & & & & & & & & & & &!'!9:M!N!N!U!UV]!^!^N33&
C     \  GZ  \  \  
 ""$$$$$s   A77A;>A;;C   *C*c                 l   |                      dd          | _        d| _        |                                 s|                                 rit          ddg          }t          |                      d          |                      d          g          }t          ||z            dk    r	d	| _        d S d S d S )
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)r.   offload_devices_validoffload_devicess      r   r-   z'HfDeepSpeedConfig.set_stage_and_offloadR   s     nn%>CC ==?? 		%dmmoo 		%$'$8$8!!NN#OPPNN#KLL O ?%::;;a?? $ @?		% 		%r   c                     | j         }|                    d          }|                                }|D ]}|                    |          }|d |fc S  ||fS )N.)r,   splitpopget)r.   ds_key_longr,   nodesds_keynodes         r   find_config_nodez"HfDeepSpeedConfig.find_config_nodee   sq     !!#&& 	$ 	$DZZ%%F~V|###  v~r   Nc                 f    |                      |          \  }}||S |                    ||          S )zG
        Returns the set value or `default` if no value is set
        )rI   rD   )r.   rE   defaultr,   rG   s        r   r7   zHfDeepSpeedConfig.get_valuer   s9     ..{;;>Nzz&'***r   Fc                     | j         }|                    d          }|D ]:}|}|                    |          }||rt          d| d| j                     dS ;||                    |           dS dS )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.
        rA   NzCan't find z entry in the config: )r,   rB   rD   r   rC   )r.   rE   
must_existr,   rF   rH   parent_configs          r   del_config_sub_treez%HfDeepSpeedConfig.del_config_sub_tree{   s      !!#&& 	 	D"MZZ%%F~ $%c;%c%cVZVa%c%cdddFF	  $d##### %$r   c                 R    |                      |          }|dn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).

        NFr7   boolr.   rE   values      r   is_truezHfDeepSpeedConfig.is_true   s)     {++uu4;;6r   c                 T    |                      |          }|dn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).
        NFrQ   rS   s      r   is_falsezHfDeepSpeedConfig.is_false   s+    
 {++uutE{{?:r   c                     | j         dk    S )Nr   r8   r.   s    r   r:   zHfDeepSpeedConfig.is_zero2       {ar   c                     | j         dk    S )N   rY   rZ   s    r   r;   zHfDeepSpeedConfig.is_zero3   r[   r   c                     | j         S r   )r9   rZ   s    r   
is_offloadzHfDeepSpeedConfig.is_offload   s
    }r   r   )F)__name__
__module____qualname____doc__r2   r-   rI   r7   rO   rU   rW   r:   r;   r_    r   r   r   r   ,   s          % % %*% % %&  + + + +$ $ $ $.7 7 7; ; ;              r   r   c                       e Zd ZdZd Zd ZdS )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                     || _         d S r   )engine)r.   rh   s     r   r2   zDeepSpeedEngineWrapper.__init__   s    r   c                 ^     | j         j        |fi | | j                                          d S r   )rh   backwardstep)r.   losskwargss      r   rj   zDeepSpeedEngineWrapper.backward   s;    T,,V,,, 	r   N)r`   ra   rb   rc   r2   rj   rd   r   r   rf   rf      s<               r   rf   c                   F     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          | _        d S )NF)device_placementscaleroverflow)superr2   hasattr	optimizer__has_overflow__)r.   rv   	__class__s     r   r2   z"DeepSpeedOptimizerWrapper.__init__   s<    U4HHH '
 C Cr   Nc                     d S r   rd   )r.   set_to_nones     r   	zero_gradz#DeepSpeedOptimizerWrapper.zero_grad       r   c                     d S r   rd   rZ   s    r   rk   zDeepSpeedOptimizerWrapper.step   r|   r   c                 ,    | j         r| j        j        S dS )zTWhether or not the optimizer step was done, or skipped because of gradient overflow.F)rw   rv   rs   rZ   s    r   step_was_skippedz*DeepSpeedOptimizerWrapper.step_was_skipped   s       	+>**ur   r   )
r`   ra   rb   rc   r2   r{   rk   propertyr   __classcell__rx   s   @r   ro   ro      s         D D D D D        X    r   ro   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                 L    t                                          ||           d S r   )rt   r2   )r.   	scheduler
optimizersrx   s      r   r2   z"DeepSpeedSchedulerWrapper.__init__   s#    J/////r   c                     d S r   rd   rZ   s    r   rk   zDeepSpeedSchedulerWrapper.step   r|   r   )r`   ra   rb   rc   r2   rk   r   r   s   @r   r   r      sQ         0 0 0 0 0      r   r   c                       e Zd ZdZddZdS )
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.
    MbP?r   c                 >    || _         || _        || _        || _        d S r   )paramslrweight_decayrm   )r.   r   r   r   rm   s        r   r2   zDummyOptim.__init__  s#    (r   N)r   r   r`   ra   rb   rc   r2   rd   r   r   r   r      s2              r   r   c                       e Zd ZdZddZdS )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.
    Nr   c                 L    || _         || _        || _        || _        || _        d S r   )rv   total_num_stepswarmup_num_stepslr_scheduler_callablerm   )r.   rv   r   r   r   rm   s         r   r2   zDummyScheduler.__init__  s,    ". 0%:"r   )Nr   Nr   rd   r   r   r   r     s2         "     r   r   )r&   r$   r    copyr   rv   r   r   r   dataclassesr   r   r   rf   ro   r   r   r   rd   r   r   <module>r      s     				       , , , , , , , , , , , , ( ( ( ( ( (Z Z Z&~ ~ ~ ~ ~ ~ ~ ~B       8     4   6     4   "       .         r   