o
    3wi6                     @   sZ  d dl 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rPd dlmZ nd dlmZ e rbd 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de.de.fddZ/			d!ddZ0d"de-de.fddZ1d#de-fddZ2dS )$    N)Path)
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 rg   U/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/accelerate/checkpointing.pysave_accelerator_state=   s~   ,
"
"
"





ri   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'`Nrj   rk   r   r'    z.safetensorsdevicer(   )map_locationz%All model weights loaded successfullyrn   z(All optimizer states loaded successfullyz(All scheduler states loaded successfullyr   r)   r,   Fr-   r.   z1All dataloader sampler states loaded successfullyz$GradScaler state loaded successfullyr/   r"   r0   r1   r2   r3   r4   r5   r6   r8   r9   z%All random states loaded successfullyzCould not load random states)0dict	TypeErrorr   rm   r   r:   r<   r   existsr   strr   r   load_state_dictr=   r>   r   r   r
   r@   r*   r+   rA   rB   rC   set_samplerrD   r   r   rF   setstaterH   	set_staterE   set_rng_stater   rK   set_rng_state_allr   rM   r   rN   r   rO   rQ   r   rR   	Exception)#	input_dirmodelsr   r   r    r!   r#   rn   load_kwargsload_model_func_kwargsoverride_attributesrS   modelendinginput_model_filer?   rW   rX   input_optimizer_fileoptimizer_staterZ   r[   input_scheduler_filescheduler_stater]   r^   input_sampler_filer*   r+   r`   ra    input_dataloader_state_dict_fileinput_scaler_filescaler_staterd   rg   rg   rh   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_r/   zSaving the state of z to )r$   N)r   r=   r>   r   r   r?   )objpathr   r$   save_locationrg   rg   rh   save_custom_state9  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_r/   zLoading the state of z from rj   F)rn   weights_onlyN)r=   r>   r   rs   r   )r   r   r   load_locationrg   rg   rh   load_custom_stateC  s   r   )NFT)NNN)r   F)r   )3rF   pathlibr   numpyrH   rE   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_modelrR   loggingr   rT   r   __name__r=   rr   listro   intboolri   r   r   r   rg   rg   rg   rh   <module>   sZ   X
	

}
 
