U
    0-e\k                     @   s   d dl Z ddlmZmZmZmZmZmZmZ ddl	m
Z
mZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZ dd	 ZdS )
    N   )ComputeEnvironmentDistributedTypeis_deepspeed_availableis_mps_availableis_npu_availableis_transformers_availableis_xpu_available)DEEPSPEED_MULTINODE_LAUNCHERSFSDP_AUTO_WRAP_POLICYFSDP_BACKWARD_PREFETCHFSDP_SHARDING_STRATEGYFSDP_STATE_DICT_TYPETORCH_DYNAMO_MODES   )ClusterConfig)DYNAMO_BACKENDS
_ask_field_ask_options_convert_distributed_mode_convert_dynamo_backend_convert_mixed_precision_convert_yes_no_to_boolc            0         s	  t dddddddgt} d}d	}d	}d }d }d }d
}d}d}	| tjtjtjtjfkrtdtd	d}|d	krt dt	t
|t}td}tdt}tdtddd}|stdd
d}tdtddd}	| tjkrtdtddd}
n| tjkrd}
nd}
i }|
rtdtddd|d< |
sFt rF| tjtjtjfkrFtdtddd|d< i }tdtddd}|rd}t ddd  tD td!d||d" < td#tddd}|rt d$td%d& dd||d' < td(tddd||d) < td*tddd||d+ < |
 ot }i }| tjtjfkr.|s.td,tddd}|r>tj} t s>td-| tjkr.td.tddd}|rvtd/td0d|d1< n t d2dd	d!d3gtd!d|d4< d0d5d6g |d4 d!krt d7  fd8d&|d9< t d:  fd;d&|d<< |d< d6krtd=td>d|d?< |d9 d6krtd@td>d|dA< tdBtd	d|dC< tdDtddd}|rTtdEtdFd|dG< |d4 d3krvtdHtddd|dI< tdJtddd|dK< |dK rt stdL|d	kr.dM}t |tdNd& |dO< |dO td	 kr.tdPt|dQ< tdRtddd}|r
tdSt|dT< tdUtddd}|r.tdVt|dW< i }| tjtjtjfkrtdXtddd}|rdtj} | tjkrdY}t |tdZd& d	d|d[< td\tddd|d]< d^}t |td_d& |d`< |d` td krtdatddd}|stdbt|dc< n$|d` td	 krtddtded|df< dg}t |tdhd& |di< dj}t |tdkd& d!d|dl< tdmtddd|dn< tdotddd|dp< tdqtddd|dr< i }| tjfkrtdstddd} | rtj } | tj krdt}tdutd	dvd||dw < ||dw  d	krtdxtddd||dy < tdztd	dvd||d{ < ||d{  d	krFtd|td	dvd||d} < td~tddd||d < tdtddd||d < tdtdFd||dG < d }!d }"d}#g }$d }%d }&d }'d}(d})| tjtjtjtjtjfkrt| !dd	 "dd}*|*dkr|*d7 }*n|*d7 }*td|* dtd	dvd}n,| tjtjtj fkrFtdtd	dvd}nd	}| tjkrz|d	krz|d	krzt#d|  d| tjtjtjtjfkr|
s|st$ rd}*nd}*td|* ddd}| tjkrd}+tddd},tdtddd})|)rtdd dd}%tdd dd}'tdddd}(tdtddd}-|-rtdtddd}.|.rjtdd dd}"t%j&'|"}"n>t(d g }!d}/|/r|!)tdd dd tdtddd}/qztddd!d}&tddd!d}$n0d},| tjkr|rd }+nt dddddgt*}+|	r|+dk	r|
	st(d | tjk	r>|+dk	r>tddd}#t+t,j-| |||+|#|||||,|||||
|||!|"|$|%|&|'|(|)||	dS )Nz$Which type of machine are you using?zNo distributed trainingz	multi-CPUz	multi-XPUz	multi-GPUz	multi-NPUTPUr   r   ZstaticTFzYHow many different machines will you use (use more than 1 for multi-node training)? [1]: )defaultz!What is the rank of this machine?zGWhat is the IP address of the machine that will host the main process? zDWhat is the port you will use to communicate with the main process? zAre all the machines on the same local network? Answer `no` if nodes are on the cloud and/or on different network hosts [YES/no]: zPlease enter yes or no.)r   error_messagez?What rendezvous backend will you use? ('static', 'c10d', ...): zShould distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: zxDo you want to run your training on CPU only (even if a GPU / Apple Silicon / Ascend NPU device is available)? [yes/NO]:zXDo you want to use Intel PyTorch Extension (IPEX) to speed up training on CPU? [yes/NO]:ZipexzDDo you want to use XPU plugin to speed up training on XPU? [yes/NO]:Zuse_xpuz?Do you wish to optimize your script with torch dynamo?[yes/NO]:Zdynamo_z+Which dynamo backend would you like to use?c                 S   s   g | ]}|  qS  )lower).0xr   r   c/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/accelerate/commands/config/cluster.py
<listcomp>   s     z%get_cluster_input.<locals>.<listcomp>   backendzGDo you want to customize the defaults sent to torch.compile? [yes/NO]: zWhich mode do you want to use?c                 S   s   t t|  S N)r   intr   r   r   r    <lambda>       z#get_cluster_input.<locals>.<lambda>modez\Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: Zuse_fullgraphz7Do you want to enable dynamic shape tracing? [yes/NO]: Zuse_dynamicz(Do you want to use DeepSpeed? [yes/NO]: zRDeepSpeed is not installed => run `pip3 install deepspeed` or build it from sourcezDDo you want to specify a json file to a DeepSpeed config? [yes/NO]: z9Please enter the path to the json DeepSpeed config file: noneZdeepspeed_config_filez8What should be your DeepSpeed's ZeRO optimization stage?r   Z
zero_stagecpuZnvmez"Where to offload optimizer states?c                    s    t |  S r$   r%   r&   Zdeepspeed_devicesr   r    r'      r(   Zoffload_optimizer_devicezWhere to offload parameters?c                    s    t |  S r$   r,   r&   r-   r   r    r'      r(   Zoffload_param_devicez Nvme Path to offload parameters?z/nvmeZoffload_param_nvme_pathz&Nvme Path to offload optimizer states?Zoffload_optimizer_nvme_pathzIHow many gradient accumulation steps you're passing in your script? [1]: Zgradient_accumulation_stepsz0Do you want to use gradient clipping? [yes/NO]: z,What is the gradient clipping value? [1.0]: g      ?Zgradient_clippingzLDo you want to save 16-bit model weights when using ZeRO Stage-3? [yes/NO]: Zzero3_save_16bit_modelzoDo you want to enable `deepspeed.zero.Init` when using ZeRO Stage-3 for constructing massive models? [yes/NO]: Zzero3_init_flagzpWhen `zero3_init_flag` is set, it requires Transformers to be installed. Please run `pip3 install transformers`.z*Which Type of launcher do you want to use?c                 S   s   t t|  S r$   )r
   r%   r&   r   r   r    r'     r(   Zdeepspeed_multinode_launcheraE  DeepSpeed configures multi-node compute resources with hostfile. Each row is of the format `hostname slots=[num_gpus]`, e.g., `localhost slots=2`; for more information please refer official [documentation](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node). Please specify the location of hostfile: Zdeepspeed_hostfilez:Do you want to specify exclusion filter string? [yes/NO]: z#DeepSpeed exclusion filter string: Zdeepspeed_exclusion_filterz:Do you want to specify inclusion filter string? [yes/NO]: z#DeepSpeed inclusion filter string: Zdeepspeed_inclusion_filterz7Do you want to use FullyShardedDataParallel? [yes/NO]: z&What should be your sharding strategy?c                 S   s   t | d S )Nr   r,   r&   r   r   r    r'   J  r(   Zfsdp_sharding_strategyzBDo you want to offload parameters and gradients to CPU? [yes/NO]: Zfsdp_offload_paramsz%What should be your auto wrap policy?c                 S   s   t t|  S r$   )r   r%   r&   r   r   r    r'   W  r(   Zfsdp_auto_wrap_policyul   Do you want to use the model's `_no_split_modules` to wrap. Only applicable for 🤗 Transformers [yes/NO]: zSpecify the comma-separated list of transformer layer class names (case-sensitive) to wrap ,e.g, :`BertLayer`, `GPTJBlock`, `T5Block`, `BertLayer,BertEmbeddings,BertSelfOutput` ...? : Z"fsdp_transformer_layer_cls_to_wrapzaWhat should be your FSDP's minimum number of parameters for Default Auto Wrapping Policy? [1e8]: i Zfsdp_min_num_paramsz4What should be your FSDP's backward prefetch policy?c                 S   s   t t|  S r$   )r   r%   r&   r   r   r    r'   p  r(   Zfsdp_backward_prefetch_policyz+What should be your FSDP's state dict type?c                 S   s   t t|  S r$   )r   r%   r&   r   r   r    r'   v  r(   Zfsdp_state_dict_typez@Do you want to enable FSDP's forward prefetch policy? [yes/NO]: Zfsdp_forward_prefetchzBDo you want to enable FSDP's `use_orig_params` feature? [yes/NO]: Zfsdp_use_orig_paramszsDo you want each individually wrapped FSDP unit to broadcast module parameters from rank 0 at the start? [YES/no]: Zfsdp_sync_module_statesz+Do you want to use Megatron-LM ? [yes/NO]: Zmegatron_lm_z0What is the Tensor Parallelism degree/size? [1]:zPlease enter an integer.Z	tp_degreez6Do you want to enable Sequence Parallelism? [YES/no]: Zsequence_parallelismz2What is the Pipeline Parallelism degree/size? [1]:Z	pp_degreez)What is the number of micro-batches? [1]:Znum_micro_batcheszDDo you want to enable selective activation recomputation? [YES/no]: Zrecompute_activationsz{Do you want to use distributed optimizer which shards optimizer state and gradients across data pralellel ranks? [YES/no]: Zuse_distributed_optimizerzSWhat is the gradient clipping value based on global L2 Norm (0 to disable)? [1.0]: no.ZMULTI_ z coresz(s)z	How many z. should be used for distributed training? [1]:z=How many GPU(s) should be used for distributed training? [1]:zSpecified distributed type zy but only using 1 GPU on a single machine. Please select `No distributed training` for the type of machine you are using.zNPU(s)zGPU(s)zWhat zV (by id) should be used for training on this machine as a comma-seperated list? [all]:allziWhat is the name of the function in your script that should be launched in all parallel scripts? [main]: mainz'Are you using a TPU cluster? [yes/NO]: z&What is the name of your TPU cluster? z*Please enter the name of your TPU cluster.z&What is the zone of your TPU cluster? z*Please enter the zone of your TPU cluster.zFTo run a python script in a TPU pod, should `sudo` be used? [yes/NO]: zCDo you have code you wish to run on startup in each pod? [yes/NO]: z1Is this code located in a bash script? [yes/NO]: z&What is the path to your bash script? z*Please enter the path to your bash script.zLPlease enter each command seperately you wish to run on startup in each pod.z(Please enter a single command to be ran zTPlease enter the commands you wish to run on startup in each pod as a single string.z.Do you wish to add another command? [yes/NO]: zqIf not using an instance group, what are the names of the Compute VM instances to be used, seperated by a comma: ,zQWhat environment variables do you wish to set in each pod, seperated by a comma: z2Do you wish to use FP16 or BF16 (mixed precision)?Zfp16Zbf16Zfp8zTorch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.zWShould `torch.float` be cast as `bfloat16` and `torch.double` remain `float32` on TPUs?)Zcompute_environmentdistributed_typenum_processesgpu_idsmixed_precisionZdowncast_bf16machine_ranknum_machinesmain_process_ipmain_process_portmain_training_functiondeepspeed_configfsdp_configmegatron_lm_configipex_configuse_cpurdzv_backendsame_networkcommandsZcommand_filetpu_envtpu_nametpu_vmtpu_zonetpu_use_sudotpu_use_clusterdynamo_configdebug).r   r   r   Z	MULTI_GPUZ	MULTI_NPUZ	MULTI_XPUZ	MULTI_CPUr   r%   listranger   NOr	   r   r   r   r   r   Z	DEEPSPEEDr   AssertionErrorstrfloatr   	Exceptionr
   ZFSDPr   r   r   r   ZMEGATRON_LMsplitreplace
ValueErrorr   ospathabspathprintappendr   r   r   ZLOCAL_MACHINE)0r4   r8   r9   r5   r6   r:   r;   rB   rC   rL   rA   r@   rK   Z
use_dynamoprefixZuse_custom_optionsZuse_mpsr=   Zuse_deepspeedZuse_deepspeed_configZuse_gradient_clippingZlauncher_queryZis_exclusion_filterZis_inclusion_filterr>   Zuse_fsdpZsharding_strategy_queryZfsdp_wrap_queryZuse_no_split_modulesZfsdp_backward_prefetch_queryZfsdp_state_dict_type_queryr?   Zuse_megatron_lmZtpu_commandsZtpu_command_fileZtpu_downcast_bf16rE   rF   rG   rH   rI   rJ   Zmachine_typer7   r<   run_commandsZuse_command_fileZanother_commandr   r-   r    get_cluster_input0   sn   
 






  
  








	










 
	


 r^   )rW   utilsr   r   r   r   r   r   r	   Zutils.constantsr
   r   r   r   r   r   Zconfig_argsr   Zconfig_utilsr   r   r   r   r   r   r   r^   r   r   r   r    <module>   s
   $	 $