o
    0i6                     @   sj  d dl Z d dlmZ d dlmZ d dlZd dlZd dlm	Z	 ddl
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ eddrVd d	lm Z  nd d	l!m Z  e rhd dl"m#  m$Z% dd
l&m'Z' ddl(m)Z) e'e*Z+			d!de,de-e. de-de-de-de/de/dee  de0de0fddZ1			d"ddZ2d#de/de0fddZ3d$de/fdd Z4dS )%    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_sdaa_availableis_torch_versionis_torch_xla_availableis_xpu_availableloadsavez>=z2.4.0)
GradScaler)
get_logger)PartialStateFT
output_dirmodel_states
optimizers
schedulersdataloadersprocess_indexstepscalersave_on_each_nodesafe_serializationc
           !      C   sL  t | } t|D ]-\}
}|	stnt}|
dkr |dd|
 d}| |}t||||	d td|  qt|D ].\}
}|	 }|
dkrKt
 dnt
 d|
 d}| |}t|||dd td|  q:t|D ].\}
}|	 }|
dkr~t dnt d|
 d}| |}t|||dd td	|  qmt|D ]e\}
}|
dkrt dnt d|
 d}| |}d
dlm}m} t|j|r| }t||rt|||dd t|ddr|
dkrdnd|
 d}| |}|	 }t|| td|
 d|  q|dur"|	 }| t}t|| td|  i }t d| d}||d< t |d< tj |d< t |d< t rNtj |d< t rZtj  |d< nt! rftj" |d< nt# rqtj$ |d< t% r|tj& |d< t' rtj( |d< t) rt* |d< | |} t||  td|   | S ) aw  
    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 Nz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_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   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_filer@   output_scaler_filestatesstates_nameoutput_states_file rh   Z/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/accelerate/checkpointing.pysave_accelerator_state>   s~   ,
"
"
"





rj   c	           #      K   s  t  }
|dvrtd|du rd}n|dkrt j}|du r i }t| } t|D ]D\}}|dkr5d| nd}| t | d	}| rSt	||fd
t
|i|	 q(| t | d}t||d}|j|fi |	 q(td t|D ]+\}}|dkrt dnt d| d}| |}t|fd|i|}|| | qvtd t|D ]'\}}|dkrt dnt d| d}| |}t|fi |}|| qtd t|D ]e\}}|dkrt dnt d| d}| |}ddlm}m} t|j|r| }t||r|t|}t|ddrA|dkr$dnd| d}| |}| rAt|fi |}|| qtd |dur_| t} t| }!||! td z|t| t d| d}"d|"v rx|"d |
d< t|"d  tj |"d  t!"|"d  t# rt!j$%|"d  t& rt!j'%|"d  n"t( rt!j)%|"d   nt* rt!j+%|"d!  nt!j,%|"d"  t- rt."|"d#  td$ W |
S  t/y   td% Y |
S w )&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
        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'`Nrk   rl   r   r(    z.safetensorsdevicer)   )map_locationz%All model weights loaded successfullyro   z(All optimizer states loaded successfullyz(All scheduler states loaded successfullyr   r*   r-   Fr.   r/   z1All dataloader sampler states loaded successfullyz$GradScaler state loaded successfullyr0   r#   r1   r2   r3   r4   r5   r6   r7   r9   r:   z%All random states loaded successfullyzCould not load random states)0dict	TypeErrorr   rn   r   r;   r=   r	   existsr   strr   r   load_state_dictr>   r?   r   r   r   rA   r+   r,   rB   rC   rD   set_samplerrE   r   r   rG   setstaterI   	set_staterF   set_rng_stater   rL   set_rng_state_allr   rN   r   rO   r   rP   rR   r   rS   	Exception)#	input_dirmodelsr   r    r!   r"   r$   ro   load_kwargsload_model_func_kwargsoverride_attributesrT   modelendinginput_model_filer@   rX   rY   input_optimizer_fileoptimizer_stater[   r\   input_scheduler_filescheduler_stater^   r_   input_sampler_filer+   r,   ra   rb    input_dataloader_state_dict_fileinput_scaler_filescaler_statere   rh   rh   ri   load_accelerator_state   s   '
"

"

"









r   indexc                 C   sD   t |d| d }tdt|  d|  t|  ||d dS )zL
    Saves the state of `obj` to `{path}/custom_checkpoint_{index}.pkl`
    custom_checkpoint_r0   zSaving the state of z to )r%   N)r   r>   r?   r   r   r@   )objpathr   r%   save_locationrh   rh   ri   save_custom_state:  s   r   c                 C   sB   | d| d}t dt|  d|  | t|ddd dS )	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_r0   zLoading the state of z from rk   F)ro   weights_onlyN)r>   r?   r   rt   r   )r   r   r   load_locationrh   rh   ri   load_custom_stateD  s   r   )NFT)NNN)r   F)r   )5rG   pathlibr   typingr   numpyrI   rF   safetensors.torchr   utilsr   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_modelrS   loggingr   rU   r   __name__r>   rs   listrp   intboolrj   r   r   r   rh   rh   rh   ri   <module>   s\   X
	

}
 
