§
    “©ºj½8  ã                   ó<  — d 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mZ eeef         Zdeeeeegef                  f         fd	„Zd
edefd„Zd
edefd„Zd
edefd„Zd
edefd„Zdefd„Zd
edefd„Zd
edefd„Zd
edefd„Zd
edefd„Zd
edef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:`~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 model.ckpt

é    N)ÚAnyÚCallable)ÚPossibleUserWarning)ÚEarlyStopping)ÚModelCheckpoint©Úrank_zero_warnÚreturnc                  ó~   — 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_trackingÚ0_migrate_loop_current_epoch_to_progress_trackingÚ"_migrate_loop_batches_that_steppedÚ9_migrate_model_checkpoint_save_on_train_epoch_end_defaultÚ_drop_apex_amp_stateÚ+_migrate_loop_structure_after_tbptt_removalÚ4_migrate_loop_structure_after_optimizer_loop_removalÚ5_migrate_loop_structure_after_dataloader_loop_removal© ó    ú/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/utilities/migration/migration.pyÚ_migration_indexr   ,   s?   € õ <Ð<Ý@ÕBrÐsÝ4Ð5ÝKÐLå Ý7Ý@ÝAð	
ðð ð r   Ú
checkpointc                 óZ  — t           dft           dft           dft          dft          dfdœ}|                      d¦  «        pi | d<   |                     ¦   «         D ]P\  }}|| v rG| |         }|\  }}| d                              |¦  «        pi | 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_keys          r   r   r   <   sá   € õ 2AÐBTÐ0UÝ0?ÐARÐ/SÝ%4Ð6HÐ$IÝ%2°LÐ$AÝ)6¸
Ð(Cðð €Lð )Ÿnšn¨[Ñ9Ô9Ð?¸R€Jˆ{Ñà%×+Ò+Ñ-Ô-ð  ð  ‰ˆˆXØ�*ÐÐØ˜s”OˆEØ*2Ñ'ˆM˜<Ø5?ÀÔ5L×5PÒ5PÐQ^Ñ5_Ô5_Ð5eÐceˆJ�{Ô# MÑ2ØCHˆJ�{Ô# MÔ2°<Ñ@Ø˜3�øØÐr   c                 óB  — | d         }|                       ddt          ¦   «         i¦  «         | d                               dt          ¦   «         ¦  «         | d         d         d         }||d         d         d         d<   | d         d         d	         }||d         d<   | S )
zçSets 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_loopú3epoch_loop.batch_loop.optimizer_loop.optim_progressÚ	optimizerÚstepÚtotalÚ	completedú5epoch_loop.batch_loop.manual_loop.optim_step_progress©Ú
setdefaultÚ!_get_fit_loop_initial_state_1_6_0)r   r.   Úoptim_progressÚoptim_step_progresss       r   r   r   V   s¬   € ð ˜]Ô+€KØ×Ò˜' JÕ0QÑ0SÔ0SÐ#TÑUÔUÐUØˆwÔ×"Ò" :Õ/PÑ/RÔ/RÑSÔSÐSà Ô(¨Ô4Ð5jÔk€NØ@K€N�;Ô Ô'¨Ô0°Ñ=à$ WÔ-¨jÔ9Ð:qÔrÐØ0;Ð˜Ô  Ñ-ØÐr   c                 óè   — | d         }|                       ddt          ¦   «         i¦  «         | d                               dt          ¦   «         ¦  «         || d         d         d         d         d<   | S )záSets 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/   r0   Úepoch_progressÚcurrentr5   r7   )r   r=   s     r   r   r   k   sw   € ð �wÔ€EØ×Ò˜' JÕ0QÑ0SÔ0SÐ#TÑUÔUÐUØˆwÔ×"Ò" :Õ/PÑ/RÔ/RÑSÔSÐSØPU€JˆwÔ˜
Ô#Ð$4Ô5°iÔ@ÀÑMØÐr   c                 óf   — | d         }| d         d         d                               d|¦  «         | S )z¤Sets 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/   r0   úepoch_loop.state_dictÚ_batches_that_stepped)r8   )r   r.   s     r   r   r   {   s<   € ð ˜]Ô+€KØˆwÔ˜
Ô#Ð$;Ô<×GÒGÐH_ÐalÑmÔmÐmØÐr   c                  óä   — 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   )r5   Úready)r?   r4   )r5   rD   Ústarted)r3   Ú	zero_grad)r2   Úoptimizer_position)r5   Ú	processedrD   rE   F)r?   Úis_last_batchr4   rB   )r6   ú,epoch_loop.batch_loop.manual_loop.state_dictr1   ú/epoch_loop.batch_loop.optimizer_loop.state_dictú epoch_loop.batch_loop.state_dictzepoch_loop.batch_progresszepoch_loop.scheduler_progressrA   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   r9   r9   ˆ   sz  € ð &'°Ð3Ð3Ø#$¨qÐ1Ð1ðB
ð B
ð 9;ð 34¸aÐ$@Ð$@ÐYZÐefÐKgÐKgÐhÐhà-.¸ÀqÐIÐIØ+,°qÀQÐGÐGðð ðð ð #$ð	@
ð 	@
ð <>Ø,.à%&°QÀÈqÐQÐQØ"Ø#$°1¸qÈQÐOÐOð&
ð &
ð &'°Ð3Ð3Ø#$¨qÐ1Ð1ð*
ð *
ð #:¸1Ð!=à%&°Ð3Ð3Ø#$¨qÐ1Ð1ð4
ð 4
ð
 &'°QÀÈqÐQÐQØ"Ø#$°1¸qÈQÐOÐOð:
ð :
ð
 68Ø*,à%&°QÀÈqÐQÐQØ#$°1¸qÈQÐOÐOð
ð 
ð ðW,ð ,ð ,r   c                 ó  ‡— d| vr| S dt           dt           fd„Št          | d         ¦  «        }ˆfd„| d                              ¦   «         D ¦   «         }t          |¦  «        |k     rt          dt          ¬¦  «         | 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                 ó\   — |                       d¦  «        s| S t          j        dd| ¦  «        S )Nr   z., 'save_on_train_epoch_end': (None|True|False)Ú )Ú
startswithÚreÚsub)rO   s    r   Únew_keyzJ_migrate_model_checkpoint_save_on_train_epoch_end_default.<locals>.new_keyÅ   s3   € Ø×!Ò!Ð"3Ñ4Ô4ð 	ØˆNÝŒvÐFÈÈGÑTÔTÐTr   c                 óX   •— i | ]&\  }}t          |t          ¦  «        ¯ ‰|¦  «        |“Œ'S r   )Ú
isinstanceÚstr)Ú.0rO   ÚstaterU   s      €r   ú
<dictcomp>zM_migrate_model_checkpoint_save_on_train_epoch_end_default.<locals>.<dictcomp>Ì   sK   ø€ ð ð ð Ù$2 G¨UÕYcÐdkÕmpÑYqÔYqðØˆ�ÑÔ˜%ðð ð r   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)rX   Úlenr&   r	   r   )r   Únum_keysÚnew_callback_statesrU   s      @r   r   r   ¸   sÛ   ø€ ð ˜*Ð$Ð$ØÐðU�ð U¥ð Uð Uð Uð Uõ
 �:˜kÔ*Ñ+Ô+€Hðð ð ð Ø6@ÀÔ6M×6SÒ6SÑ6UÔ6Uðñ ô Ðõ ÐÑÔ (Ò*Ð*Ýð?õ )ð	
ñ 	
ô 	
ð 	
ð Ðà1€Jˆ{ÑØÐr   c                 ó6   — d}|| v rt          d¦  «         | |= | S )zÓApex 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(   s     r   r   r   Ý   s0   € ð €CØ
ˆjÐÐÝÐmÑnÔnÐnØ�sˆOØÐr   c                 ó,  — d| vr| S d| d         vr| S | d         d         }dddddœ}t          |                     ¦   «         ¦  «        D ]\  }}||v r||         ||<   ||= Œd|v r|d         r|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/   r0   ú*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)r6   rJ   r1   rK   rL   rA   Úold_batch_loop_state_dictN)Úlistr&   Úpop)r   r0   Úold_key_new_key_mappingÚoldÚnews        r   r   r   í   sé   € ð �jÐ Ð ØÐØ˜ GÔ,Ð,Ð,ØÐØ˜'Ô" :Ô.€Hð BnØ8[Ø?iØ;að	ð Ðõ Ð0×6Ò6Ñ8Ô8Ñ9Ô9ð ð ‰ˆˆSØ�(ˆ?ˆ?Ø$ SœMˆH�S‰MØ˜�øð *¨XÐ5Ð5¸(ÐCeÔ:fÐ5ØIQÐRtÔIuˆÐ(Ô)Ð*EÑFØ‡L‚LÐ3°TÑ:Ô:Ð:àÐr   c                 óV  — d| vr| S d| d         vr| S | d         d         }d|v r|d                               dd¦  «         d|v r0|                      d¦  «        |d<   |                      d¦  «        |d<   d	|v r0|                      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/   r0   re   rG   Nrf   z,epoch_loop.automatic_optimization.state_dictz0epoch_loop.automatic_optimization.optim_progressrd   z)epoch_loop.manual_optimization.state_dictrc   z2epoch_loop.manual_optimization.optim_step_progress)ri   )r   r0   s     r   r   r     só   € ð �jÐ Ð ØÐØ˜ GÔ,Ð,Ð,ØÐØ˜'Ô" :Ô.€Hð 2°XÐ=Ð=ØÐ;Ô<×@Ò@ÐAUÐW[Ñ\Ô\Ð\ð .°Ð9Ð9ØCKÇ<Â<ÐPvÑCwÔCwˆÐ?Ñ@ØGOÇ|Â|Ø6ñH
ô H
ˆÐCÑDð +¨hÐ6Ð6Ø@HÇÂÐMpÑ@qÔ@qˆÐ<Ñ=ØIQÏÊØ8ñJ
ô J
ˆÐEÑFð Ðr   c                 óÜ   ‡‡— d| vr| S | d         }dD ]X}||vrŒ||         Š‰                      dd¦  «         dŠˆˆfd„t          ‰¦  «        D ¦   «         }‰                     |¦  «         ŒY| 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                 ó�   •— i | ]B}|                      ‰¦  «        ¯|t          ‰¦  «        d …         ‰                     |¦  «        “ŒCS )N)rR   r]   ri   )rY   ÚkÚepoch_loop_keyÚloops     €€r   r[   zI_migrate_loop_structure_after_dataloader_loop_removal.<locals>.<dictcomp>G  sM   ø€ ÐuÐuÐuÀQÐXY×XdÒXdÐesÑXtÔXtÐu˜1�S Ñ0Ô0Ð2Ð2Ô3°T·X²X¸a±[´[ÐuÐuÐur   )ri   rh   Úupdate)r   r/   Úloop_keyÚepoch_loop_dictru   rv   s       @@r   r   r   5  sž   øø€ ð �jÐ Ð ØÐØ�wÔ€EØBð %ð %ˆØ˜5Ð Ð ØØ�XŒˆØ�ŠÐ&¨Ñ-Ô-Ð-Ø&ˆØuÐuÐuÐuÐuÍ$ÈtÉ*Ì*ÐuÑuÔuˆØ�Š�OÑ$Ô$Ð$Ð$ØÐr   )Ú__doc__rS   Útypingr   r   Ú#lightning_fabric.utilities.warningsr   Ú*pytorch_lightning.callbacks.early_stoppingr   Ú,pytorch_lightning.callbacks.model_checkpointr   Ú%pytorch_lightning.utilities.rank_zeror	   ÚdictrX   Ú_CHECKPOINTrh   r   r   r   r   r   r9   r   r   r   r   r   r   r   r   ú<module>r‚      s  ððð ð& 
€	€	€	Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à CÐ CÐ CÐ CÐ CÐ CØ DÐ DÐ DÐ DÐ DÐ DØ HÐ HÐ HÐ HÐ HÐ HØ @Ð @Ð @Ð @Ð @Ð @à�3˜�8Œn€ð˜$˜s D¨°;°-ÀÐ2LÔ)MÔ$NÐNÔOð ð ð ð ð ¸ð Èð ð ð ð ð4¸{ð È{ð ð ð ð ð*Àð ÐQ\ð ð ð ð ð 
°;ð 
À;ð 
ð 
ð 
ð 
ð-¨4ð -ð -ð -ð -ð`"È+ð "ÐZeð "ð "ð "ð "ðJ [ð °[ð ð ð ð ð $¸Kð $ÈKð $ð $ð $ð $ðNÀ[ð ÐU`ð ð ð ð ðBÀkð ÐVað ð ð ð ð ð r   