o
    0ih                     @   s   d dl Z d dlZd dlmZ d dlZd dlmZ G dd de jZdddej	ej
d	d
ddej	ddej	ddZdddddddddddddZeeZdd ZdedefddZd!ddZdedefddZdd  ZdS )"    N)Path)CustomArgumentParserc                   @   s   e Zd ZdZdZdS )ConversionStatusr   N)__name__
__module____qualname__NOT_YET_IMPLEMENTEDREMOVED r   r   ^/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/accelerate/commands/to_fsdp2.pyr      s    r   fsdp_versionfsdp_reshard_after_forwardfsdp_auto_wrap_policyfsdp_cpu_ram_efficient_loadingfsdp_offload_paramsfsdp_state_dict_type"fsdp_transformer_layer_cls_to_wrapfsdp_min_num_paramsfsdp_activation_checkpointing)r   r   r   fsdp_backward_prefetchfsdp_forward_prefetchr   r   fsdp_sharding_strategyr   fsdp_sync_module_statesr   r   fsdp_use_orig_paramsr   TF)
FULL_SHARDSHARD_GRAD_OPHYBRID_SHARDHYBRID_SHARD_ZERO2NO_SHARD)r   r   c                 C   sf   t | j std| j d| js| jd u rtd| js/t | j r1td| j dd S d S )NzConfig file z
 not foundz9If --overwrite is not set, --output_file must be providedzOutput file z* already exists and --overwrite is not set)r   config_fileexistsFileNotFoundError	overwriteoutput_file
ValueErrorFileExistsError)argsr   r   r   _validate_to_fsdp2_argsG   s   r(   configreturnc                 C   s   |  di }|std | S i }| dddkr%td td | S | D ]\\}}t |d }t|ts<|d u rC|}|||< q)|tjkrRtd| d	 q)|tj	kratd| d
 q)|d u rstd| d |||< q)|t
v rt
|  ||}||t| < q)d|d< || d< | S )Nfsdp_configz?No FSDP config found in the config file, skipping conversion...r         z6Config already specifies FSDP2, skipping conversion...zaIf the config doesn't use new argument names, change `fsdp_version` to `1` and rerun the command.z	Argument z0 has been removed in FSDP2, skipping this key...z6 is not yet implemented in FSDP2, skipping this key...z- is not being converted, skipping this key...)getloggerinfowarningitemsARGUMENT_KEY_MAPPING
isinstancer   r
   r	   ARGUMENT_VALUE_MAPPING)r)   r+   new_fsdp_configkeyvalueconversion_statusr   r   r   convert_config_to_fsdp2R   s@   




r:   c                 C   st   d}| d ur| j d|d}nt|d}|jdtddd |jdd	d
dd |jdtdd d | d ur8|jtd |S )Nz0Convert an Accelerate config from FSDP1 to FSDP2zto-fsdp2)descriptionz--config_filez#The config file to convert to FSDP2T)typehelprequiredz--overwrite
store_truez&Overwrite the config file if it existsF)actionr=   defaultz--output_filezThe path to the output file to write the converted config to. If not provided, the input file will be overwritten (if --overwrite is set))r<   r=   rA   )func)
add_parserr   add_argumentstrset_defaultsto_fsdp2_command)
subparsersr;   parserr   r   r   to_fsdp2_command_parser~   s(   
rJ   r    c                 C   sB   t | }t|}W d    n1 sw   Y  |std|S )NzConfig file is empty)openyaml	safe_loadr%   )r    fr)   r   r   r   load_config   s   
rO   c                 C   sp   t |  t| j}| jr| jd u r| j| _t|}t| jd}t|| W d    d S 1 s1w   Y  d S )Nw)	r(   rO   r    r#   r$   r:   rK   rL   dump)r'   r)   
new_configrN   r   r   r   rG      s   
"rG   )N)enumloggingpathlibr   rL   accelerate.commands.utilsr   Enumr   r
   r	   r3   r5   	getLoggerr   r/   r(   dictr:   rJ   rE   rO   rG   r   r   r   r   <module>   sP   

,	