o
    oi8                     @   s8  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deeeeegef  f fd	d
ZdedefddZdedefddZdedefddZdedefddZdefddZdedefddZdedefddZdedefddZdedefddZdedefddZdS ) a1  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:`~lightning.pytorch.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 lightning.pytorch.utilities.upgrade_checkpoint 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ttt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.0z2.0.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_drop_apex_amp_state+_migrate_loop_structure_after_tbptt_removal4_migrate_loop_structure_after_optimizer_loop_removal5_migrate_loop_structure_after_dataloader_loop_removal r   r   c/home/ubuntu/.local/lib/python3.10/site-packages/lightning/pytorch/utilities/migration/migration.py_migration_index,   s   r   
checkpointc                 C   s   t dft dft dftdftdfd}| dpi | d< | D ](\}}|| v rG| | }|\}}| d |p7i | d |< || d | |< | |= q| S )zcThe checkpoint and early stopping keys were renamed.

    Version: 0.10.0
    Commit: a5d1176

    best_model_scorebest_model_path
wait_countpatience)$checkpoint_callback_best_model_score#checkpoint_callback_best_model_pathcheckpoint_callback_bestearly_stop_callback_waitearly_stop_callback_patience	callbacks)r   r   getitems)r   keys_mappingkeynew_pathvaluecallback_typecallback_keyr   r   r   r   <   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+   optim_progressoptim_step_progressr   r   r   r   V   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currentr2   r4   )r   r9   r   r   r   r   k   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)r5   )r   r+   r   r   r   r   {   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   )r2   ready)r;   r1   )r2   r>   started)r0   	zero_grad)r/   optimizer_position)r2   	processedr>   r?   F)r;   is_last_batchr1   r=   )r3   ,epoch_loop.batch_loop.manual_loop.state_dictr.   /epoch_loop.batch_loop.optimizer_loop.state_dict 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:   
state_dictr   r   r   r   r   r6      sF   


r6   c                    sl   d| vr| S dt dt fdd t| d } fdd| d  D }t||k r0tdtd	 | S || d< | S )
a:  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)rH   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).0rH   staterM   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)rO   lenr$   r
   r   )r   num_keysnew_callback_statesr   rR   r   r      s   


r   c                 C   s   d}|| v rt d | |= | S )zApex support was removed in v2.0.0, and this migration drops it from the state-keys saved in the checkpoint
    dict.

    Version: 2.0.0
    Commit: e544676ff434ed96c6dd3b4e73a708bcb27ebcf1
    PR: #16149

    amp_scaling_statezTThis checkpoint contains apex AMP data, but apex support has been removed in v2.0.0.r	   )r   r&   r   r   r   r      s
   	r   c                 C   s   d| vr| S d| d vr| S | d d }ddddd}t | D ]\}}||v r2|| ||< ||= q!d|v rC|d rC|d |d	 d
< |dd | S )aX  Adjusts the loop structure since it changed when the support for truncated backpropagation was removed. The
    optimizer loop and the manual loop were previously children of the training batch loop. After its removal, they
    became the children of the training epoch loop.

    Version: 2.0.0
    Commit: 7807454
    PR: #16337, #16172

    r,   r-   *epoch_loop.manual_loop.optim_step_progress!epoch_loop.manual_loop.state_dict(epoch_loop.optimizer_loop.optim_progress$epoch_loop.optimizer_loop.state_dict)r3   rD   r.   rE   rF   r<   old_batch_loop_state_dictN)listr$   pop)r   r-   old_key_new_key_mappingoldnewr   r   r   r      s&   
r   c                 C   s   d| vr| S d| d vr| S | d d }d|v r |d  dd d|v r2| d|d< | d|d< d	|v rD| d	|d
< | d|d< | S )a)  Adjusts the loop structure since it changed when the support for multiple optimizers in automatic optimization
    mode was removed. There is no longer a loop over optimizer, and hence no position to store for resuming the loop.

    Version: 2.0.0
    Commit: 6a56586
    PR: #16539, #16598

    r,   r-   r[   rA   Nr\   z,epoch_loop.automatic_optimization.state_dictz0epoch_loop.automatic_optimization.optim_progressrZ   z)epoch_loop.manual_optimization.state_dictrY   z2epoch_loop.manual_optimization.optim_step_progress)r_   )r   r-   r   r   r   r     s$   	r   c                    sf   d| vr| S | d }dD ]$}||vrq||  dd d  fddtD }| q| S )a<  The dataloader loops (``_DataLoaderLoop``, ``_PredictionLoop`, and ``_EvaluationLoop``) were flattened into the
    ``_EvaluationEpochLoop`` (now ``_EvaluationLoop``) and ``_PredictionEpochLoop`` (now ``_PredictionLoop``).

    Version: 2.0.0
    Commit: ec4f592ecfe238edd83185f6c6905fb1e2406d61
    PR: #16726

    r,   )predict_loopvalidate_loop	test_loopdataloader_progressNzepoch_loop.c                    s.   i | ]}|  r|t d  |qS )N)rJ   rU   r_   )rP   kepoch_loop_keyloopr   r   rS   G  s   . zI_migrate_loop_structure_after_dataloader_loop_removal.<locals>.<dictcomp>)r_   r^   update)r   r,   loop_keyepoch_loop_dictr   rh   r   r   5  s   	r   )__doc__rK   typingr   r   r   r   #lightning.fabric.utilities.warningsr   *lightning.pytorch.callbacks.early_stoppingr   ,lightning.pytorch.callbacks.model_checkpointr   %lightning.pytorch.utilities.rank_zeror
   rO   _CHECKPOINTr   r   r   r   r   r6   r   r   r   r   r   r   r   r   r   <module>   s&   $0%'!