o
    h%                     @   s$  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
Z
ddlmZmZmZ ddlmZmZmZ ejedejed	d
dZejedZejedZejedZejesjejesmeZneZdd ZeG dd dZeG dd deZeG dd deZ dS )    N)	dataclass)Enum)ListOptionalUnion   )ComputeEnvironmentDistributedTypeSageMakerDistributedType)SAGEMAKER_PYTHON_VERSIONSAGEMAKER_PYTORCH_VERSIONSAGEMAKER_TRANSFORMERS_VERSIONHF_HOMEXDG_CACHE_HOMEz~/.cachehuggingface
acceleratezdefault_config.yamlc                 C   s   | d urt j| std|  dnt} t| dddD}| drAt|	dt
jt
jkr2t}nt}|j| dW  d    S t|	dt
jt
jkrQt}nt}|j| d	W  d    S 1 scw   Y  d S )
NzThe passed configuration file `z` does not exist. Please pass an existing file to `accelerate launch`, or use the the default one created through `accelerate config` and run `accelerate launch` without the `--config_file` argument.rutf-8encodingz.jsoncompute_environment)	json_file)	yaml_file)ospathisfileFileNotFoundErrordefault_config_fileopenendswithjsonloadgetr   LOCAL_MACHINEClusterConfigSageMakerConfigfrom_json_fileyaml	safe_loadfrom_yaml_file)config_filefconfig_class r-   \/var/www/html/ai/venv/lib/python3.10/site-packages/accelerate/commands/config/config_args.pyload_config_from_file+   s.   



$r/   c                   @   sz   e Zd ZU eed< eeef ed< eed< e	ed< e	ed< dd Z
edd	d
Zdd ZedddZdd Zdd ZdS )
BaseConfigr   distributed_typemixed_precisionuse_cpudebugc                 C   s\   | j }| D ]\}}t|tr|j||< t|tr"t|s"d ||< qdd | D }|S )Nc                 S   s   i | ]\}}|d ur||qS Nr-   ).0kvr-   r-   r.   
<dictcomp>[   s    z&BaseConfig.to_dict.<locals>.<dictcomp>)__dict__items
isinstancer   valuedictbool)selfresultkeyr=   r-   r-   r.   to_dictS   s   

zBaseConfig.to_dictNc                 C   s$  |d u rt n|}t|ddd}t|}W d    n1 sw   Y  d|vr-tj|d< d|vr?d|v r;|d r;dnd |d< d|v rF|d= d|v r[|d}|dkrUi nd|i|d	< d
|vrcd|d
< d|vrkd|d< tt|	 t| j
	  }t|dkrtd| d| d| di |S )Nr   r   r   r   r2   fp16dynamo_backendNOdynamo_configr3   Fr4   r   The config file at  had unknown keys (s), please try upgrading your `accelerate` version or fix (and potentially remove) these keys from your config file.r-   )default_json_config_filer   r    r!   r   r#   popsortedsetkeys__dataclass_fields__len
ValueError)clsr   r+   config_dictrE   
extra_keysr-   r-   r.   r&   ^   s.   

zBaseConfig.from_json_filec                 C   sV   t |ddd}tj|  dddd }|| W d    d S 1 s$w   Y  d S )Nwr   r      T)indent	sort_keys
)r   r    dumpsrC   write)r@   r   r+   contentr-   r-   r.   to_json_filey   s   "zBaseConfig.to_json_filec                 C   sB  |d u rt n|}t|ddd}t|}W d    n1 sw   Y  d|vr-tj|d< d|vr?d|v r;|d r;dnd |d< t|d trN|d sNd|d< d|v rU|d= d|v rj|d}|d	krdi nd|i|d
< d|vrrd|d< d|vrzd|d< t	t
| t
| j  }t|dkrtd| d| d| di |S )Nr   r   r   r   r2   rD   norE   rF   rG   r3   Fr4   r   rH   rI   rJ   r-   )default_yaml_config_filer   r'   r(   r   r#   r<   r?   rL   rM   rN   rO   rP   rQ   rR   )rS   r   r+   rT   rE   rU   r-   r-   r.   r)   ~   s2   

zBaseConfig.from_yaml_filec                 C   sD   t |ddd}t|  | W d    d S 1 sw   Y  d S )NrV   r   r   )r   r'   	safe_dumprC   )r@   r   r+   r-   r-   r.   to_yaml_file   s   "zBaseConfig.to_yaml_filec                 C   sb   t | jtrt| j| _t | jtr%| jtjkrt| j| _nt| j| _| jd u r/i | _d S d S r5   )	r<   r   strr   r1   AMAZON_SAGEMAKERr
   r	   rG   r@   r-   r-   r.   __post_init__   s   

zBaseConfig.__post_init__r5   )__name__
__module____qualname__r   __annotations__r   r	   r
   rc   r?   rC   classmethodr&   r^   r)   rb   rf   r-   r-   r-   r.   r0   K   s   
 r0   c                       sN  e Zd ZU eed< dZeed< dZeed< dZee	 ed< dZ
ee	 ed< dZee ed	< d
Zee	 ed< dZee ed< dZe	ed< dZeed< dZeed< dZeed< dZeed< dZeed< dZe	ed< dZe	ed< dZeed< dZeed< dZe	ed< dZee	 ed< dZee	 ed< dZee	 ed< dZeed<  fddZ   Z!S ) r$   num_processesr   machine_rank   num_machinesNgpu_idsmain_process_ipmain_process_portstaticrdzv_backendFsame_networkmainmain_training_functiondeepspeed_configfsdp_configmegatron_lm_configipex_configdowncast_bf16tpu_nametpu_zonetpu_use_clustertpu_use_sudocommand_filecommandstpu_vmtpu_envrG   c                    sJ   | j d u ri | _ | jd u ri | _| jd u ri | _| jd u r i | _t  S r5   )rx   ry   rz   r{   superrf   re   	__class__r-   r.   rf      s   




zClusterConfig.__post_init__)"rg   rh   ri   intrj   rm   ro   rp   r   rc   rq   rr   rt   ru   r?   rw   rx   r>   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   rG   rf   __classcell__r-   r-   r   r.   r$      s2   
 r$   c                   @   s   e Zd ZU eed< eed< dZee ed< dZee ed< dZeed< dZ	e
ed	< d
Zeed< de	 Zeed< eZeed< eZeed< eZeed< dZeed< dZeed< dZeed< dZeed< dS )r%   ec2_instance_typeiam_role_nameN	image_uriprofilez	us-east-1regionrn   ro   allrp   zaccelerate-sagemaker-base_job_namepytorch_versiontransformers_version
py_versionsagemaker_inputs_filesagemaker_metrics_fileadditional_argsrG   )rg   rh   ri   rc   rj   r   r   r   r   ro   r   rp   r   r   r   r   r   r   r   r   r   r   r>   rG   r-   r-   r-   r.   r%      s    
 r%   )!r    r   dataclassesr   enumr   typingr   r   r   r'   utilsr   r	   r
   utils.constantsr   r   r   r   
expandusergetenvjoinhf_cache_home	cache_dirrK   r`   r   r   r/   r0   r$   r%   r-   r-   r-   r.   <module>   s0    ^/