o
    Ii8                     @   sn  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mZ eddrXd d	l m!Z! nd d	l"m!Z! e rjd 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e0de0dee! de1de1fddZ2			d"ddZ3d#de0de1fddZ4d$de0fdd Z5d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_neuron_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   sb  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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_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_filerB   output_scaler_filestatesstates_nameoutput_states_file rk   L/home/ubuntu/.local/lib/python3.10/site-packages/accelerate/checkpointing.pysave_accelerator_state?   s   ,
"
"
"





rm   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 zt| 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, rt!j-%|"d"  nt. rt!j/%|"d#  nt!j0%|"d$  t1 rt2"|"d%  td& W |
S  t3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'`Nrn   ro   r   r)    z.safetensorsdevicer*   )map_locationz%All model weights loaded successfullyrr   z(All optimizer states loaded successfullyz(All scheduler states loaded successfullyr   r+   r.   Fr/   r0   z1All dataloader sampler states loaded successfullyz$GradScaler state loaded successfullyr1   r$   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   z%All random states loaded successfullyzCould not load random states)4dict	TypeErrorr   rq   r   r=   r?   r	   existsr   strr   r   load_state_dictr@   rA   r   r   r   rC   r,   r-   rD   rE   rF   set_samplerrG   r   r   rI   setstaterK   	set_staterH   set_rng_stater   rN   set_rng_state_allr   rP   r   rQ   r   rR   r   rS   r   rT   rU   r   rV   	Exception)#	input_dirmodelsr    r!   r"   r#   r%   rr   load_kwargsload_model_func_kwargsoverride_attributesrW   modelendinginput_model_filerB   r[   r\   input_optimizer_fileoptimizer_stater^   r_   input_scheduler_filescheduler_statera   rb   input_sampler_filer,   r-   rd   re    input_dataloader_state_dict_fileinput_scaler_filescaler_staterh   rk   rk   rl   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_r1   zSaving the state of z to )r&   N)r   r@   rA   r   r   rB   )objpathr   r&   save_locationrk   rk   rl   save_custom_stateA  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_r1   zLoading the state of z from rn   F)rr   weights_onlyN)r@   rA   r   rw   r   )r   r   r   load_locationrk   rk   rl   load_custom_stateK  s   r   )NFT)NNN)r   F)r   )6rI   pathlibr   typingr   numpyrK   rH   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_modelrV   loggingr   rX   r   __name__r@   rv   listrs   intboolrm   r   r   r   rk   rk   rk   rl   <module>   s\   \
	


 
