
    9i'                         d dl Z 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	  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   )AcceleratedOptimizer)AcceleratedSchedulerc                   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                    t        |t              rt        |      }nt        j                  j                  |      r7t        j                  |dd      5 }t        j                  |      }d d d        n:	 t        j                  |      j                  d      }t        j                  |      }| _        | j%                          y # 1 sw Y   !xY w# t        t        t         f$ r t!        d|       w xY w)Nrzutf-8)encodingzoExpected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: )
isinstancedictr   ospathexistsioopenjsonloadbase64urlsafe_b64decodedecodeloadsUnicodeDecodeErrorAttributeError
ValueErrorconfigset_stage_and_offload)selfconfig_file_or_dictr   fconfig_decodeds        d/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/accelerate/utils/deepspeed.py__init__zHfDeepSpeedConfig.__init__*   s    )40 12FWW^^/0,cGD &1& &!'!9!9:M!N!U!UV]!^N3 ""$& & '
C   F  GZ  F[  \ s   C49C C#C4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)r   offload_devices_validoffload_devicess      r"   r   z'HfDeepSpeedConfig.set_stage_and_offload?   s     nn%>C ==?dmmo$'$8!!NN#OPNN#KLO ?%::;a? $ @ .    c                     | j                   }|j                  d      }|j                         }|D ]  }|j                  |      }|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_nodeR   s`     !!#& 	$DZZ%F~V|#	$
 v~r1   Nc                 V    | j                  |      \  }}||S |j                  ||      S )zG
        Returns the set value or `default` if no value is set
        )r;   r6   )r   r7   defaultr   r9   s        r"   r(   zHfDeepSpeedConfig.get_value_   s3     ..{;>Nzz&'**r1   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.
        r3   NzCan't find z entry in the config: )r   r4   r6   r   r5   )r   r7   
must_existr   r8   r:   parent_configs          r"   del_config_sub_treez%HfDeepSpeedConfig.del_config_sub_treeh   s      !!#& 	D"MZZ%F~${;-?UVZVaVaUb%cdd	 $d# %r1   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r   r7   values      r"   is_truezHfDeepSpeedConfig.is_true   s%     {+u64;6r1   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).
        FrC   rE   s      r"   is_falsezHfDeepSpeedConfig.is_false   s'    
 {+u:tE{?:r1   c                      | j                   dk(  S )Nr   r)   r   s    r"   r+   zHfDeepSpeedConfig.is_zero2       {{ar1   c                      | j                   dk(  S )N   rK   rL   s    r"   r,   zHfDeepSpeedConfig.is_zero3   rM   r1   c                     | j                   S N)r*   rL   s    r"   
is_offloadzHfDeepSpeedConfig.is_offload   s    }}r1   rQ   )F)__name__
__module____qualname____doc__r#   r   r;   r(   rA   rG   rI   r+   r,   rR    r1   r"   r   r      s9     %*%&+$.7;  r1   r   c                       e Zd ZdZ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 rQ   )engine)r   r[   s     r"   r#   zDeepSpeedEngineWrapper.__init__   s	    r1   c                 r     | j                   j                  |fi | | j                   j                          y rQ   )r[   backwardstep)r   losskwargss      r"   r]   zDeepSpeedEngineWrapper.backward   s-    T,V, 	r1   N)rS   rT   rU   rV   r#   r]   rW   r1   r"   rY   rY      s    r1   rY   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)superr#   hasattr	optimizer__has_overflow__)r   ri   	__class__s     r"   r#   z"DeepSpeedOptimizerWrapper.__init__   s*    U4H '
 Cr1   c                      y rQ   rW   )r   set_to_nones     r"   	zero_gradz#DeepSpeedOptimizerWrapper.zero_grad       r1   c                      y rQ   rW   rL   s    r"   r^   zDeepSpeedOptimizerWrapper.step   ro   r1   c                 H    | j                   r| j                  j                  S y)zTWhether or not the optimizer step was done, or skipped because of gradient overflow.F)rj   ri   rf   rL   s    r"   step_was_skippedz*DeepSpeedOptimizerWrapper.step_was_skipped   s        >>***r1   rQ   )
rS   rT   rU   rV   r#   rn   r^   propertyrr   __classcell__rk   s   @r"   rb   rb      s,    D  r1   rb   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 rQ   )rg   r#   )r   	scheduler
optimizersrk   s      r"   r#   z"DeepSpeedSchedulerWrapper.__init__   s    J/r1   c                      y rQ   rW   rL   s    r"   r^   zDeepSpeedSchedulerWrapper.step   ro   r1   )rS   rT   rU   rV   r#   r^   rt   ru   s   @r"   rw   rw      s    0r1   rw   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:
            Other arguments.
    c                 <    || _         || _        || _        || _        y rQ   )paramslrweight_decayr`   )r   r   r   r   r`   s        r"   r#   zDummyOptim.__init__   s    (r1   N)gMbP?r   rS   rT   rU   rV   r#   rW   r1   r"   r}   r}      s    r1   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:
            Other arguments.
    Nc                 J    || _         || _        || _        || _        || _        y rQ   )ri   total_num_stepswarmup_num_stepslr_scheduler_callabler`   )r   ri   r   r   r   r`   s         r"   r#   zDummyScheduler.__init__  s(    ". 0%:"r1   )Nr   Nr   rW   r1   r"   r   r      s    "r1   r   )r   r   r   r   copyr   ri   r   ry   r   r   rY   rb   rw   r}   r   rW   r1   r"   <module>r      sa     	  	  , ,~ ~B 8 4 6 4 " . r1   