o
    oi6                     @   s   d dl Z d dlmZmZ d dlmZ d dlZd dlmZ d dl	m
Z
mZ e eZdefddZd	edefd
dZedkrRe ZeeZeejZeeej dS dS )    N)ArgumentParser	Namespace)Path)_TORCH_GREATER_EQUAL_2_3)_METADATA_FILENAME_load_distributed_checkpointreturnc                  C   s2   t dd} | jdtdd | jdtdd |  S )NzConverts a distributed/sharded checkpoint into a single file that can be loaded with `torch.load()`. Only supports FSDP sharded checkpoints at the moment.)descriptioncheckpoint_folderzxPath to a checkpoint folder, containing the sharded checkpoint files saved using the `torch.distributed.checkpoint` API.)typehelpz--output_filezPath to the file where the converted checkpoint should be saved. The file should not already exist. If no path is provided, the file will be saved next to the input checkpoint folder with the same name and a '.consolidated' suffix.)r   add_argumentstr
parse_args)parser r   e/home/ubuntu/.local/lib/python3.10/site-packages/lightning/fabric/utilities/consolidate_checkpoint.py_parse_cli_args   s   	r   argsc                 C   s   t std td t| j}| s td|  td | s0td|  td |t 	 sBtd|  td | j
d u rP||jd }nt| j
}| retd|  td t||dS )	Nz;Processing distributed checkpoints requires PyTorch >= 2.3.   z/The provided checkpoint folder does not exist: zQThe provided checkpoint path must be a folder, containing the checkpoint shards: zOnly FSDP-sharded checkpoints saved with Lightning are supported for consolidation. The provided folder is not in that format: z.consolidatedzThe path for the converted checkpoint already exists. Choose a different path by providing `--output_file` or move/delete the file first: )r
   output_file)r   _logerrorexitr   r
   existsis_dirr   is_filer   with_suffixsuffixr   )r   r
   r   r   r   r   _process_cli_args(   s<   



r   __main__)loggingargparser   r   pathlibr   torch"lightning.fabric.utilities.importsr   lightning.fabric.utilities.loadr   r   	getLogger__name__r   r   r   r   configr
   
checkpointsaver   r   r   r   r   <module>   s    
#
