
    
9j8                        S SK r S SKJr  S SKJr  S SKrS SKrS SKJ	r	  SSK
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  \" SS5      (       a  S S	K J!r!  OS S	K"J!r!  \" 5       (       a	  S SK#J$s  J%r&  SS
K'J(r(  SSK)J*r*  \(" \+5      r,   SS\-S\.\/   S\.S\.S\.S\0S\0S\\!   S\1S\14S jjr2   SS jr3SS\0S\14S jjr4SS\04S jjr5g)    N)Path)Optional)
load_model   )
MODEL_NAMEOPTIMIZER_NAMERNG_STATE_NAMESAFE_MODEL_NAMESAFE_WEIGHTS_NAMESAMPLER_NAMESCALER_NAMESCHEDULER_NAMEWEIGHTS_NAMEget_pretty_nameis_cuda_availableis_hpu_availableis_mlu_availableis_musa_availableis_neuron_availableis_sdaa_availableis_torch_versionis_torch_xla_availableis_xpu_availableloadsavez>=z2.4.0)
GradScaler)
get_logger)PartialState
output_dirmodel_states
optimizers
schedulersdataloadersprocess_indexstepscalersave_on_each_nodesafe_serializationc
                 	   [        U 5      n [        U5       Hg  u  pU	(       d  [        O[        nU
S:  a  UR	                  SSU
 S35      nU R                  U5      n[        XXS9  [        R                  SU 35        Mi     [        U5       He  u  pUR                  5       nU
S:X  a	  [         S3O[         SU
 S3nU R                  U5      n[        UUUSS9  [        R                  SU 35        Mg     [        U5       Hf  u  n
nUR                  5       nU
S:X  a	  [         S3O[         SU
 S3nU R                  U5      n[        UUUSS9  [        R                  S	U 35        Mh     [        U5       H  u  n
nU
S:X  a	  [         S3O[         SU
 S3nU R                  U5      nS
SKJnJn  [!        UR"                  U5      (       a-  UR%                  5       n[!        UU5      (       a  [        UUUSS9  ['        USS5      (       aF  U
S:X  a  SOSU
 S3nU R                  U5      nUR                  5       n[(        R                  " UU5        [        R                  SU
 SU 35        M     UbT  UR                  5       nU R                  [*        5      n[(        R                  " UU5        [        R                  SU 35        0 n[,         SU S3nUUS'   [.        R0                  " 5       US'   [2        R.                  R5                  5       US'   [(        R6                  " 5       US'   [9        5       (       a!  [(        R:                  R=                  5       US'   [?        5       (       a"  [(        R@                  R=                  5       US'   Oa[C        5       (       a"  [(        RD                  R=                  5       US'   O0[G        5       (       a!  [(        RH                  R=                  5       US'   [K        5       (       a!  [(        RL                  R=                  5       US'   [O        5       (       a!  [(        RP                  R=                  5       US'   [S        5       (       a!  [(        RT                  R=                  5       US'   [W        5       (       a  [X        R6                  " 5       US'   U R                  U5      n [(        R                  " UU 5        [        R                  SU  35        U $ ) a  
Saves the current states of the models, optimizers, scaler, and RNG generators to a given directory.

<Tip>

If `safe_serialization` is `True`, models will be saved with `safetensors` while the rest are saved using native
`pickle`.

</Tip>

Args:
    output_dir (`str` or `os.PathLike`):
        The name of the folder to save all relevant weights and states.
    model_states (`List[torch.nn.Module]`):
        A list of model states
    optimizers (`List[torch.optim.Optimizer]`):
        A list of optimizer instances
    schedulers (`List[torch.optim.lr_scheduler._LRScheduler]`):
        A list of learning rate schedulers
    dataloaders (`List[torch.utils.data.DataLoader]`):
        A list of dataloader instances to save their sampler states
    process_index (`int`):
        The current process index in the Accelerator state
    step (`int`):
        The current step in the internal step tracker
    scaler (`torch.amp.GradScaler`, *optional*):
        An optional gradient scaler instance to save;
    save_on_each_node (`bool`, *optional*):
        Whether to save on every node, or only the main node.
    safe_serialization (`bool`, *optional*, defaults to `True`):
        Whether to save the model using `safetensors` or the traditional PyTorch way (that uses `pickle`).
r   ._)r'   r(   zModel weights saved in .binFzOptimizer state saved in zScheduler state saved in r   IterableDatasetShardSeedableRandomSampleruse_stateful_dataloaderdl_state_dict.bindl_state_dict_zSampler state for dataloader z
 saved in zGradient scaler state saved in .pklr%   random_statenumpy_random_seedtorch_manual_seedtorch_xpu_manual_seedtorch_mlu_manual_seedtorch_sdaa_manual_seedtorch_musa_manual_seedtorch_hpu_manual_seedtorch_neuron_manual_seedtorch_cuda_manual_seedxm_seedzRandom states saved in )-r   	enumerater   r   replacejoinpathr   loggerinfo
state_dictr   r   r   data_loaderr.   r/   
isinstancedatasetget_samplergetattrtorchr   r	   randomgetstatenp	get_stateget_rng_stater   xpuget_rng_state_allr   mlur   sdaar   musar   hpur   neuronr   cudar   xm)!r   r    r!   r"   r#   r$   r%   r&   r'   r(   istateweights_nameoutput_model_fileoptoptimizer_nameoutput_optimizer_file	schedulerscheduler_nameoutput_scheduler_file
dataloadersampler_nameoutput_sampler_filer.   r/   samplerdataloader_state_dict_name!output_dataloader_state_dict_filerD   output_scaler_filestatesstates_nameoutput_states_files!                                    R/home/wildlama/miniconda3/lib/python3.13/site-packages/accelerate/checkpointing.pysave_accelerator_statern   ?   sh   X j!Jl++=|CTq5'//q1X>L&//=U9Jr-.?-@AB , J' 45FN+40>BRRSTUSVVZ@[ * 3 3N CU)=Nchi/0E/FGH ( "*-9$$&45FN+40>BRRSTUSVVZ@[ * 3 3N CU)=Nchi/0E/FGH . #;/:01Q,t,|nAaSPT<U(11,?Lj((*>?? ,,.G'#899W1EVkpq:8%@@@AQ)<n]^\__cLd&0:0C0CD^0_-#..0JJJz#DE3A3jAT@UVW 0$ !!#'00=

5,-56H5IJKF#$Am_D9KF6N#__.F>"$))"5"5"7F"'"5"5"7F*/))*E*E*G&'*/))*E*E*G&'			+0::+G+G+I'(			+0::+G+G+I'(*/))*E*E*G&'-2\\-K-K-M)*+0::+G+G+I'(,,.y#,,[9	JJv)*
KK)*<)=>?    c	                    [        5       n
US;  a  [        S5      eUc  SnOUS:X  a  [        5       R                  nUc  0 n[	        U 5      n [        U5       H  u  pUS:  a  SU 3OSnU R                  [         U S35      nUR                  5       (       a  [        X4S	[        U5      0U	D6  MZ  U R                  [         U S
35      n[        XS9nUR                  " U40 U	D6  M     [        R                  S5        [        U5       HS  u  nnUS:X  a	  [          S
3O[          SU S
3nU R                  U5      n[        U4SU0UD6nX+   R                  U5        MU     [        R                  S5        [        U5       HO  u  nnUS:X  a	  ["         S
3O["         SU S
3nU R                  U5      n[        U40 UD6nUR                  U5        MQ     [        R                  S5        [        U5       H  u  nnUS:X  a	  [$         S
3O[$         SU S
3nU R                  U5      nSSKJnJn  [-        UR.                  U5      (       a;  UR1                  5       n[-        UU5      (       a  UR3                  [        U5      5      n[5        USS5      (       d  M  US:X  a  SOSU S
3nU R                  U5      nUR                  5       (       d  M  [        U40 UD6nUR                  U5        M     [        R                  S5        UbF  U R                  [6        5      n [        U 5      n!UR                  U!5        [        R                  S5         [        U R                  [8         SU S35      5      n"SU";   a  U"S   U
S'   [:        R<                  " U"S   5        [>        R:                  RA                  U"S   5        [B        RD                  " U"S   5        [G        5       (       a"  [B        RH                  RK                  U"S   5        [M        5       (       a#  [B        RN                  RK                  U"S   5        O[Q        5       (       a#  [B        RR                  RK                  U"S   5        O[U        5       (       a#  [B        RV                  RK                  U"S    5        O[Y        5       (       a#  [B        RZ                  RK                  U"S!   5        OT[]        5       (       a#  [B        R^                  RK                  U"S"   5        O"[B        R`                  RK                  U"S#   5        [c        5       (       a  [d        RD                  " U"S$   5        [        R                  S%5        U
$ ! [f         a    [        R                  S&5         U
$ f = f)'a  
Loads states of the models, optimizers, scaler, and RNG generators from a given directory.

Args:
    input_dir (`str` or `os.PathLike`):
        The name of the folder to load all relevant weights and states.
    models (`List[torch.nn.Module]`):
        A list of model instances
    optimizers (`List[torch.optim.Optimizer]`):
        A list of optimizer instances
    schedulers (`List[torch.optim.lr_scheduler._LRScheduler]`):
        A list of learning rate schedulers
    dataloaders (`List[torch.utils.data.DataLoader]`):
        A list of dataloader instances used in your program
    process_index (`int`):
        The current process index in the Accelerator state
    scaler (`torch.amp.GradScaler`, *optional*):
        An optional *GradScaler* instance to load
    map_location (`str`, *optional*):
        What device to load the optimizer state onto. Should be one of either "cpu" or "on_device".
    load_kwargs (`dict`, *optional*):
        Additional arguments that can be passed to the `load` function.
    load_model_func_kwargs (`dict`, *optional*):
        Additional arguments that can be passed to the model's `load_state_dict` method.

Returns:
    `dict`: Contains the `Accelerator` attributes to override while loading the state.
)Ncpu	on_devicezaUnsupported optimizer map location passed, please choose one of `None`, `'cpu'`, or `'on_device'`rq   rr   r   r+    z.safetensorsdevicer,   )map_locationz%All model weights loaded successfullyru   z(All optimizer states loaded successfullyz(All scheduler states loaded successfullyr   r-   r0   Fr1   r2   z1All dataloader sampler states loaded successfullyz$GradScaler state loaded successfullyr3   r%   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   z%All random states loaded successfullyzCould not load random states)4dict	TypeErrorr   rt   r   r?   rA   r
   existsr   strr   r   load_state_dictrB   rC   r   r   r   rE   r.   r/   rF   rG   rH   set_samplerrI   r   r	   rK   setstaterM   	set_staterJ   set_rng_stater   rP   set_rng_state_allr   rR   r   rS   r   rT   r   rU   r   rV   rW   r   rX   	Exception)#	input_dirmodelsr!   r"   r#   r$   r&   ru   load_kwargsload_model_func_kwargsoverride_attributesrY   modelendinginput_model_filerD   r]   r^   input_optimizer_fileoptimizer_stater`   ra   input_scheduler_filescheduler_staterc   rd   input_sampler_filer.   r/   rf   rg    input_dataloader_state_dict_fileinput_scaler_filescaler_staterj   s#                                      rm   load_accelerator_stater      s   R &55o
 	
 		$#~,,YIf%E1QCr$--0A&.VW""$$ucs<7HcLbc  )11ZL2MN.JJ!!*G0FG & KK78 J'345FN+40>BRRSTUSVVZ@[(11.A3^,^R]^%%o6	 (
 KK:; "*-945FN+40>BRRSTUSVVZ@[(11.A3C{C!!/2	 .
 KK:;";/:01Q,t,|nAaSPT<U&//=Lj((*>?? ,,.G'#899$006H1IJ:8%@@@AQ)<n]^\__cLd&/8/A/AB\/],/6688!"BRkR
**:6 0  KKCD %..{;-.|,:;4i((N+;1]O4)PQRV*0.'~./
		F#678F#678II''/F(GHII''/F(GH  JJ((0H)IJ  JJ((0H)IJII''/F(GH ""LL**62L+MNJJ((0H)IJ!##VI./;<   4234s   HU! !VVindexc                     [        U5      SU S3-  n[        R                  S[        U 5       SU 35        [	        U R                  5       XCS9  g)zD
Saves the state of `obj` to `{path}/custom_checkpoint_{index}.pkl`
custom_checkpoint_r3   zSaving the state of z to )r'   N)r   rB   rC   r   r   rD   )objpathr   r'   save_locations        rm   save_custom_stater   C  sM    
 J#5eWD!AAM
KK&s';&<DPQ	=Nro   c                     U SU S3n[         R                  S[        U 5       SU 35        U R                  [	        USSS95        g)	z
Loads the state of `obj` at `{path}/custom_checkpoint_{index}.pkl`. Will always set `weights_only=False` when
loading the state.
z/custom_checkpoint_r3   zLoading the state of z from rq   F)ru   weights_onlyN)rB   rC   r   rz   r   )r   r   r   load_locations       rm   load_custom_stater   M  sN    
 f/wd;M
KK'(<'=VM?ST]USTro   )NFT)NNN)r   F)r   )6rK   pathlibr   typingr   numpyrM   rJ   safetensors.torchr   utilsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	torch.ampr   torch.cuda.amptorch_xla.core.xla_modelcore	xla_modelrX   loggingr   rZ   r   __name__rB   ry   listrv   intboolrn   r   r   r    ro   rm   <module>r      s(        (     2 D'""$)))   
H	 $(##uut*u u 	u
 u u u Z u u u~ IXO OD OU Uro   