
    ;ih                     P   S SK r S SKrS SKJr  S SKrS SKJr   " S S\ R                  5      rSSS\R                  \R                  S	S
SS\R                  SS\R                  SS.rSSSSSS.SSSSSS.S.r\R                  " \5      rS rS\S\4S jrSS jrS\S\4S jrS rg)    N)Path)CustomArgumentParserc                       \ rS rSrSrSrSrg)ConversionStatus   r    N)__name__
__module____qualname____firstlineno__NOT_YET_IMPLEMENTEDREMOVED__static_attributes__r	       l/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/accelerate/commands/to_fsdp2.pyr   r      s    Gr   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                 ~   [        U R                  5      R                  5       (       d  [        SU R                   S35      eU R                  (       d  U R
                  c  [        S5      eU R                  (       dB  [        U R
                  5      R                  5       (       a  [        SU R
                   S35      eg g )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)argss    r   _validate_to_fsdp2_argsr/   G   s      !((**,t/?/?.@
 KLL>>d..6TUU>>d4#3#34;;==T-=-=,>>hijj >>r   configreturnc                    U R                  S0 5      nU(       d  [        R                  S5        U $ 0 nUR                  SS5      S:X  a,  [        R                  S5        [        R                  S5        U $ UR	                  5        H  u  p4[
        R                  US 5      n[        U[        5      (       d  Uc  UnXBU'   M;  U[        R                  :X  a  [        R                  SU S	35        Mj  U[        R                  :X  a  [        R                  SU S
35        M  Uc  [        R                  SU S35        XBU'   M  U[        ;   a  [        U   R                  XD5      nXB[
        U   '   M     SUS'   X S'   U $ )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)r0   r3   new_fsdp_configkeyvalueconversion_statuss         r   convert_config_to_fsdp2rB   R   sj   **]B/KUVO~q)Q.OPo	
 !'')
044S$?')9::>O>W #16-. 0 8 88NNYse+[\] 0 D DDNNYse+abc$NNYse+XYZ#(C ,,.s377E9>056+ *. '(ON#+=Mr   c                     SnU b  U R                  SUS9nO	[        US9nUR                  S[        SSS9  UR                  SS	S
SS9  UR                  S[        SS S9  U b  UR	                  [
        S9  U$ )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)actionrF   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))rE   rF   rJ   )func)
add_parserr   add_argumentstrset_defaultsto_fsdp2_command)
subparsersrD   parsers      r   to_fsdp2_command_parserrS   ~   s    DK&&z{&K%+>
c8]hlm
5	    Y	   !12Mr   r'   c                     [        U 5       n[        R                  " U5      nS S S 5        W(       d  [        S5      eU$ ! , (       d  f       N"= f)NzConfig file is empty)openyaml	safe_loadr,   )r'   fr0   s      r   load_configrY      s<    	k	a" 
/00M 
	s	   ?
Ac                 D   [        U 5        [        U R                  5      nU R                  (       a  U R                  c  U R                  U l        [        U5      n[        U R                  S5       n[        R                  " X#5        S S S 5        g ! , (       d  f       g = f)Nw)	r/   rY   r'   r*   r+   rB   rU   rV   dump)r.   r0   
new_configrX   s       r   rP   rP      sn    D!))*F~~$**2++(0J	d	$		*  
%	$	$s   1B
B)N)enumloggingpathlibr   rV   accelerate.commands.utilsr   Enumr   r   r   r;   r=   	getLoggerr
   r7   r/   dictrB   rS   rN   rY   rP   r	   r   r   <module>re      s   "     :tyy  #"> 5.66-AA&F0:2/77*N0,44%D# , # ## " 
		8	$k)D )T )X6S T 
!r   