o
    ziN                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dl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d	dfd
dZdddZedkrPe  dS dS )    N)ArgumentParser	NamespacePath)copyfile)List)tqdm)migrate_checkpointpl_legacy_patchargsreturnc              	   C   sl  t | j }| jdr| jnd| j }g }| s,td| d| d td |	 r3|g}|
 rLdd tjt|d d|  d	d
D }|s^td| d| d td td |D ]}|d}| rqqet|| qetd t|D ].}t  tj|| jrtdnd d}W d    n1 sw   Y  t| t|| qtd d S )N.z	The path zr does not exist. Please provide a valid path to a checkpoint file or a directory containing checkpoints ending in    c                 S   s   g | ]}t |qS  r   ).0pr   r   b/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/utilities/upgrade_checkpoint.py
<listcomp>,   s    z_upgrade.<locals>.<listcomp>z***T)	recursivez#No checkpoint files with extension z were found in z]. HINT: Try setting the `--extension` option to specify the right file extension to look for.z]Creating a backup of the existing checkpoint files before overwriting in the upgrade process.z.bakzUpgrading checkpoints ...cpu)map_locationzDone.)r   pathabsolute	extension
startswithexists_logerrorexitis_fileis_dirglobstrinfowith_suffixr   r   r
   torchload
map_to_cpudevicer	   save)r   r   r   filesfilebackup_file
checkpointr   r   r   _upgrade   sB   *


 r/   c                  C   sR   t dd} | jdtdd | jddtdd	d
 | jdddd |  }t| d S )NzA utility to upgrade old checkpoints to the format of the current Lightning version. This will also save a backup of the original files.)descriptionr   zDPath to a checkpoint file or a directory with checkpoints to upgrade)typehelpz--extensionz-ez.ckptzRThe file extension to look for when searching for checkpoint files in a directory.)r1   defaultr2   z--map-to-cpu
store_truez~Map all tensors in the checkpoint to CPU. Enable this option if you are converting a GPU checkpoint on a machine without GPUs.)actionr2   )r   add_argumentr#   
parse_argsr/   )parserr   r   r   r   mainF   s,   r9   __main__)r   N)r"   loggingargparser   r   pathlibr   shutilr   typingr   r&   r   %pytorch_lightning.utilities.migrationr	   r
   	getLogger__name__r   r/   r9   r   r   r   r   <module>   s   

)
