o
    TiA                     @   s  d dl Z d dlZd dlZd dlmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ eejdr6ejjdusJzd dlZdZW n eyI   dZY nw d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mZmZ d	dlmZ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+m,Z, d	dl-m.Z. d	dl/m0Z0m1Z1 d	dlm2Z2m3Z3m4Z4 d	dl5m6Z6m7Z7m8Z8 d	dl9m:Z: d	dl;m<Z<m=Z= d	dl>m?Z? d	dl@mAZA d	dlBmZmCZCmDZD dd  ZEeZFeEeF\ZGZHZIeCZJeDZKdaLddddddeddddddfd!ejMjNd"eeeef  d#eejMjN d$eej5jOjP d%eee
ef  d&eQd'eeR fd(d)ZSd*d+ ZTd,d- ZUd.d/ ZVd4d0d1ZWd4d2d3ZXdS )5    N)OptionalUnion)	Optimizer)_LRScheduler)versionhipTF   )ops)module_inject)get_accelerator)TORCH_DISTRIBUTED_DEFAULT_PORT)DeepSpeedEngineDeepSpeedOptimizerCallableDeepSpeedSchedulerCallable)ADAM_OPTIMIZERLAMB_OPTIMIZER)DeepSpeedHybridEngine)PipelineEngine)InferenceEngine)DeepSpeedInferenceConfig)add_tuning_arguments)DeepSpeedConfigDeepSpeedConfigError)checkpointing)DeepSpeedTransformerLayerDeepSpeedTransformerConfig)replace_transformer_layerrevert_transformer_layerset_autotp_mode)log_distOnDevicelogger)init_distributed)zerodomino)is_compile_supported)PipelineModule)r   git_hash
git_branchc                 C   s   t | }|j|j|jfS )zHParse a version string and extract the major, minor, and patch versions.)pkg_versionparsemajorminormicro)version_strver r0   F/home/ubuntu/.local/lib/python3.10/site-packages/deepspeed/__init__.py_parse_version5   s   
r2   model	optimizermodel_parameterstraining_datalr_schedulerdistributed_portdist_init_requiredc                 C   s  t dtttdgd tj  |dusJ dddlm	a
 t  }t
j|||d |
du r6|dur6|}
d}|rItd|  t
|d	}n |
durid
|
v rid|
v ritd|
  t
|
d |
d
 fd	}t| dr| jdurtd t| dr| jdu sJ d| j| _d| _t| dr| jdur|
du sJ d| j}
|
dusJ dt|tst|
||d}|jjrt| ||||||||	|
|d}n1t| ||||||||	|
||d}n |du sJ d| }t|
|}t| ||||||||	|
|d}tj  ||j|j |j!g}t"|S )ar	  Initialize the DeepSpeed Engine.

    Arguments:
        args: an object containing local_rank and deepspeed_config fields.
            This is optional if `config` is passed.

        model: Required: nn.module class before apply any wrappers

        optimizer: Optional: a user defined Optimizer or Callable that returns an Optimizer object.
            This overrides any optimizer definition in the DeepSpeed json config.

        model_parameters: Optional: An iterable of torch.Tensors or dicts.
            Specifies what Tensors should be optimized.

        training_data: Optional: Dataset of type torch.utils.data.Dataset

        lr_scheduler: Optional: Learning Rate Scheduler Object or a Callable that takes an Optimizer and returns a Scheduler object.
            The scheduler object should define a get_lr(), step(), state_dict(), and load_state_dict() methods

        distributed_port: Optional: Master node (rank 0)'s free port that needs to be used for communication during distributed training

        mpu: Optional: A model parallelism unit object that implements
            get_{model,data}_parallel_{rank,group,world_size}()

        dist_init_required: Optional: None will auto-initialize torch distributed if needed,
            otherwise the user can force it to be initialized or not via boolean.

        collate_fn: Optional: Merges a list of samples to form a
            mini-batch of Tensor(s).  Used when using batched loading from a
            map-style dataset.

        config: Optional: Instead of requiring args.deepspeed_config you can pass your deepspeed config
            as an argument instead, as a path or a dictionary.

        config_params: Optional: Same as `config`, kept for backwards compatibility.

    Returns:
        A tuple of ``engine``, ``optimizer``, ``training_dataloader``, ``lr_scheduler``

        * ``engine``: DeepSpeed runtime engine which wraps the client model for distributed training.

        * ``optimizer``: Wrapped optimizer if a user defined ``optimizer`` is supplied, or if
          optimizer is specified in json config else ``None``.

        * ``training_dataloader``: DeepSpeed dataloader if ``training_data`` was supplied,
          otherwise ``None``.

        * ``lr_scheduler``: Wrapped lr scheduler if user ``lr_scheduler`` is passed, or
          if ``lr_scheduler`` specified in JSON configuration. Otherwise ``None``.
    6DeepSpeed info: version={}, git-hash={}, git-branch={}r   ranksNz%deepspeed.initialize requires a model)comm)dist_backendr8   r9   z&mesh_param to Initialize mesh device: )data_parallelsequence_parallelsequence_parallel_sizedata_parallel_sizez"config to Initialize mesh device: deepscale_configzY************ --deepscale_config is deprecated, please use --deepspeed_config ************deepspeed_configzSNot sure how to proceed, we were given both a deepscale_config and deepspeed_configz|Not sure how to proceed, we were given deepspeed configs in the deepspeed arguments and deepspeed.initialize() function callzCDeepSpeed requires --deepspeed_config to specify configuration file)mesh_device)argsr3   r4   r5   r6   r7   mpur9   
collate_fnconfigconfig_class)rF   r3   r4   r5   r6   r7   rG   r9   rH   rI   rE   rJ   z*mpu must be None with pipeline parallelism)#r   format__version____git_hash____git_branch__r#   partition_parametersshutdown_init_context	deepspeedr=   distr   communication_backend_namer"   r!   infoinitialize_mesh_devicehasattrrC   warningrD   
isinstancer&   r   hybrid_engineenabledr   r   rG   r   restore_init_contextr4   training_dataloaderr7   tuple)rF   r3   r4   r5   r6   r7   r8   rG   r9   rH   rI   
mesh_paramconfig_paramsr>   rE   rJ   enginereturn_itemsr0   r0   r1   
initializeE   s   
?






rb   c                 C   sX   |  dd}|jddddd |jdd	td
d |jddddd |jdd	tdd | S )a  Helper (internal) function to update an argument parser with an argument group of the core DeepSpeed arguments.
        The core set of DeepSpeed arguments include the following:
        1) --deepspeed: boolean flag to enable DeepSpeed
        2) --deepspeed_config <json file path>: path of a json configuration file to configure DeepSpeed runtime.

        This is a helper function to the public add_config_arguments()

    Arguments:
        parser: argument parser
    Return:
        parser: Updated Parser
    	DeepSpeedzDeepSpeed configurationsz--deepspeedF
store_truezLEnable DeepSpeed (helper flag for user code, no impact on DeepSpeed backend))defaultactionhelpz--deepspeed_configNz"DeepSpeed json configuration file.)re   typerg   z--deepscalezWDeprecated enable DeepSpeed (helper flag for user code, no impact on DeepSpeed backend)z--deepscale_configz-Deprecated DeepSpeed json configuration file.)add_argument_groupadd_argumentstr)parsergroupr0   r0   r1   _add_core_arguments   s$   rn   c                 C   s   t | } | S )a  Update the argument parser to enabling parsing of DeepSpeed command line arguments.
        The set of DeepSpeed arguments include the following:
        1) --deepspeed: boolean flag to enable DeepSpeed
        2) --deepspeed_config <json file path>: path of a json configuration file to configure DeepSpeed runtime.

    Arguments:
        parser: argument parser
    Return:
        parser: Updated Parser
    )rn   )rl   r0   r0   r1   add_config_arguments  s   ro   c                   C   s
   t   S )zL
        Return a default DeepSpeed inference configuration dictionary.
    )r   dictr0   r0   r0   r1   default_inference_config  s   
rq   c           	   	   K   s   t dtttdgd |du ri }t|tr2t|d}t	|}W d   n1 s,w   Y  nt|t
r:|}n	tdt| t| | }|D ]}|| || krktd| d||  d	||  qP|| tdi |}t| |d
}|S )a  Initialize the DeepSpeed InferenceEngine.

    Description: all four cases are valid and supported in DS init_inference() API.

    # Case 1: user provides no config and no kwargs. Default config will be used.

    .. code-block:: python

        generator.model = deepspeed.init_inference(generator.model)
        string = generator("DeepSpeed is")
        print(string)

    # Case 2: user provides a config and no kwargs. User supplied config will be used.

    .. code-block:: python

        generator.model = deepspeed.init_inference(generator.model, config=config)
        string = generator("DeepSpeed is")
        print(string)

    # Case 3: user provides no config and uses keyword arguments (kwargs) only.

    .. code-block:: python

        generator.model = deepspeed.init_inference(generator.model,
                                                    tensor_parallel={"tp_size": world_size},
                                                    dtype=torch.half,
                                                    replace_with_kernel_inject=True)
        string = generator("DeepSpeed is")
        print(string)

    # Case 4: user provides config and keyword arguments (kwargs). Both config and kwargs are merged and kwargs take precedence.

    .. code-block:: python

        generator.model = deepspeed.init_inference(generator.model, config={"dtype": torch.half}, replace_with_kernel_inject=True)
        string = generator("DeepSpeed is")
        print(string)

    Arguments:
        model: Required: original nn.module object without any wrappers

        config: Optional: instead of arguments, you can pass in a DS inference config dict or path to JSON file

    Returns:
        A deepspeed.InferenceEngine wrapped model.
    r:   r   r;   Nrz5'config' argument expected string or dictionary, got zConflicting argument 'z' in 'config':z and kwargs:)rI   r0   )r   rK   rL   rM   rN   rX   rk   openjsonloadrp   
ValueErrorrh   setkeysintersectionupdater   r   )	r3   rI   kwargsfconfig_dictoverlap_keyskeyds_inference_configr`   r0   r0   r1   init_inference#  s0   
0

"
r   c                 K   sN   t | drtd dS tdd ddlm} || ||dj} t| dd | S )	aN  
    Initialize the model for tensor parallelism.

    Args:
        model (torch.nn.Module): The model to be initialized.
        tp_size (int): The tensor parallelism size.
        dtype (torch.dtype): The data type to be used for the model.

    Returns:
        torch.nn.Module: The initialized model with tensor parallelism.
    ds_autotp_parsedzQds_autotp_parsed' attribute already exists in the model, re-entry is not allowed.NT)trainingr   )TpTrainingManager)r3   tp_sizedtype)rV   r!   rW   r   !deepspeed.runtime.tensor_parallelr   modulesetattr)r3   r   r   rI   r{   r   r0   r0   r1   tp_model_initq  s   


r   )N)Ysystypesrt   typingr   r   torchtorch.optimr   torch.optim.lr_schedulerr   	packagingr   r)   rV   r   triton
HAS_TRITONImportError r	   r
   acceleratorr   	constantsr   runtime.enginer   r   r   r   r   runtime.hybrid_enginer   runtime.pipe.enginer   inference.enginer   inference.configr   runtime.lr_schedulesr   runtime.configr   r    runtime.activation_checkpointingr   ops.transformerr   r   r   r   r   utilsr   r    r!   	comm.commr"   runtimer#   r$   runtime.compilerr%   piper&   git_version_infor'   r(   r2   rL   __version_major____version_minor____version_patch__rM   rN   rR   nnModuledataDatasetintboolrb   rn   ro   rq   r   r   r0   r0   r0   r1   <module>   s   

 %#
N