a
    d$                     @   s   d Z ddlZddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ eeef Zeeeeegef  f dd	d
ZeedddZeedddZeedddZeedddZedddZeedddZdS )a7  Contains migration functions to upgrade legacy checkpoints to the format of the current Lightning version.

When Lightning loads a checkpoint, these migrations will be applied on the loaded checkpoint dictionary sequentially,
see :func:`~pytorch_lightning.utilities.migration.utils.migrate_checkpoint`.

For the Lightning developer: How to add a new migration?

1. Create a new function with a descriptive name and docstring that explains the details of this migration. Include
   version information as well as the specific commit or PR where the breaking change happened.
2. Add the function to the `_migration_index()` below. The key in the index is the version of Lightning in which the
   change happened. Any checkpoint with a version greater or equal to that version will apply the given function.
   Multiple migrations per version get executed in the provided list order.
3. You can test the migration on a checkpoint (backup your files first) by running:

   cp model.ckpt model.ckpt.backup
   python -m pytorch_lightning.utilities.upgrade_checkpoint --file model.ckpt
    N)AnyCallableDictList)PossibleUserWarning)EarlyStopping)ModelCheckpoint)rank_zero_warn)returnc                   C   s   t gttgtgtgdS )zQMigration functions returned here will get executed in the order they are listed.)z0.10.0z1.6.0z1.6.5z1.9.0)(_migrate_model_checkpoint_early_stopping._migrate_loop_global_step_to_progress_tracking0_migrate_loop_current_epoch_to_progress_tracking"_migrate_loop_batches_that_stepped9_migrate_model_checkpoint_save_on_train_epoch_end_default r   r   x/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/utilities/migration/migration.py_migration_index*   s
    r   )
checkpointr
   c                 C   s   t dft dft dftdftdfd}| dp0i | d< | D ]P\}}|| v r>| | }|\}}| d |pni | d |< || d | |< | |= q>| S )zbThe checkpoint and early stopping keys were renamed.

    Version: 0.10.0
    Commit: a5d1176
    Zbest_model_scoreZbest_model_pathZ
wait_countZpatience)Z$checkpoint_callback_best_model_scoreZ#checkpoint_callback_best_model_pathZcheckpoint_callback_bestZearly_stop_callback_waitZearly_stop_callback_patience	callbacks)r   r   getitems)r   Zkeys_mappingkeynew_pathvalueZcallback_typeZcallback_keyr   r   r   r   4   s    r   c                 C   sp   | d }|  ddt i | d  dt  | d d d }||d d d d< | d d d	 }||d d< | S )
zSets the `global_step` value for checkpoints before v1.6 without the progress tracking state. It will be
    overwritten by the loop's state if it was also saved.

    Version: 1.6.0
    Commit: c67b075
    PR: #13645, #11805
    global_steploopsfit_loop3epoch_loop.batch_loop.optimizer_loop.optim_progress	optimizersteptotal	completed5epoch_loop.batch_loop.manual_loop.optim_step_progress
setdefault!_get_fit_loop_initial_state_1_6_0)r   r   Zoptim_progressZoptim_step_progressr   r   r   r   M   s    r   c                 C   sH   | d }|  ddt i | d  dt  || d d d d d< | S )zSets the `current_epoch` value for checkpoints before v1.6 without the progress tracking state. It will be
    overwritten by the loop's state if it was also saved.

    Version: 1.6.0
    Commit: aea96e4
    PR: #11805
    epochr   r   epoch_progresscurrentr!   r#   )r   r&   r   r   r   r   a   s
    r   c                 C   s$   | d }| d d d  d| | S )zSets the `_batches_that_stepped` default value for checkpoints before v1.6.5 which don't have this key.

    Version: 1.6.5
    Commit: c67b075
    PR: #13645
    r   r   r   epoch_loop.state_dict_batches_that_stepped)r$   )r   r   r   r   r   r   p   s    r   c                   C   s   dddddddi dddddddddddddddddddi i dddddddddddd	dddddddd
diddddddddddddddddddd	i i dddddddddddi dS )Nr   )r!   ready)r(   r    )r!   r+   started)r   Z	zero_grad)r   Zoptimizer_position)r!   	processedr+   r,   F)r(   Zis_last_batchr    r*   )r"   z,epoch_loop.batch_loop.manual_loop.state_dictr   z/epoch_loop.batch_loop.optimizer_loop.state_dictz epoch_loop.batch_loop.state_dictzepoch_loop.batch_progresszepoch_loop.scheduler_progressr)   z'epoch_loop.val_loop.dataloader_progressz-epoch_loop.val_loop.epoch_loop.batch_progressz)epoch_loop.val_loop.epoch_loop.state_dictzepoch_loop.val_loop.state_dictr'   Z
state_dictr   r   r   r   r   r%   |   sF    


r%   c                    sj   d| vr| S t t ddd t| d } fdd| d  D }t||k r^tdtd | S || d< | S )	a9  The ``save_on_train_epoch_end`` was removed from the state-key of ``ModelCheckpoint`` in 1.9.0, and this
    migration drops it from the state-keys saved in the checkpoint dict so that the keys match when the Trainer
    loads the callback state.

    Version: 1.9.0
    Commit: f4ca56
    PR: #15300, #15606
    r   )old_keyr
   c                 S   s   |  ds| S tdd| S )Nr   z., 'save_on_train_epoch_end': (None|True|False) )
startswithresub)r.   r   r   r   new_key   s    
zJ_migrate_model_checkpoint_save_on_train_epoch_end_default.<locals>.new_keyc                    s$   i | ]\}}t |tr ||qS r   )
isinstancestr).0r.   stater3   r   r   
<dictcomp>   s   zM_migrate_model_checkpoint_save_on_train_epoch_end_default.<locals>.<dictcomp>aN  You have multiple `ModelCheckpoint` callback states in this checkpoint, but we found state keys that would end up colliding with each other after an upgrade, which means we can't differentiate which of your checkpoint callbacks needs which states. At least one of your `ModelCheckpoint` callbacks will not be able to reload the state.)category)r5   lenr   r	   r   )r   Znum_keysZnew_callback_statesr   r8   r   r      s    	

r   )__doc__r1   typingr   r   r   r   Z#lightning_fabric.utilities.warningsr   Z*pytorch_lightning.callbacks.early_stoppingr   Z,pytorch_lightning.callbacks.model_checkpointr   Z%pytorch_lightning.utilities.rank_zeror	   r5   Z_CHECKPOINTr   r   r   r   r   r%   r   r   r   r   r   <module>   s   $
0