a
    dW                  	   @   sR  d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ e eZeeef Zdee
e eeeee	e f f d
ddZ G dd dZ!dee
e edddZ"eedddZ#eeddddZ$eeddddZ%deee
e e&dddZ'dS )     N)
ModuleTypeTracebackType)AnyDictListOptionalTupleType)Version)_IS_WINDOWS)_PATH)PossibleUserWarning)_migration_index)rank_zero_warn)
checkpointtarget_versionreturnc                 C   s   t | }t|ttjkr<td| dtj td | i fS t }i }| D ]:\}}t| ||sdqN|D ]}|| } qhdd |D ||< qN|tjkrt	| | t
| tj | |fS )a0  Applies Lightning version migrations to a checkpoint dictionary.

    Args:
        checkpoint: A dictionary with the loaded state from the checkpoint file.
        target_version: Run migrations only up to this version (inclusive), even if migration index contains
            migration functions for newer versions than this target. Mainly useful for testing.

    Note:
        The migration happens in-place. We specifically avoid copying the dict to avoid memory spikes for large
        checkpoints and objects that do not support being deep-copied.
    z3The loaded checkpoint was produced with Lightning vz7, which is newer than your current Lightning version: v)categoryc                 S   s   g | ]
}|j qS  )__name__).0fnr   r   t/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/utilities/migration/utils.py
<listcomp>@       z&migrate_checkpoint.<locals>.<listcomp>)_get_versionr
   pl__version__r   r   r   items_should_upgrade_set_legacy_version_set_version)r   r   Zckpt_versionindexZapplied_migrationsZmigration_versionZmigration_functionsZmigration_functionr   r   r   migrate_checkpoint!   s*    


r#   c                   @   sB   e Zd ZdZd dddZeee  ee ee ddddZ	dS )	pl_legacy_patcha  Registers legacy artifacts (classes, methods, etc.) that were removed but still need to be included for
    unpickling old checkpoints. The following patches apply.

        1. ``pytorch_lightning.utilities.argparse._gpus_arg_default``: Applies to all checkpoints saved prior to
           version 1.2.8. See: https://github.com/PyTorchLightning/pytorch-lightning/pull/6898
        2. ``pytorch_lightning.utilities.argparse_utils``: A module that was deprecated in 1.2 and removed in 1.4,
           but still needs to be available for import for legacy checkpoints.

    Example:

        with pl_legacy_patch():
            torch.load("path/to/legacy/checkpoint.ckpt")
    )r   c                 C   s.   t d}|tjd< dd |_dd tjj_| S )N*pytorch_lightning.utilities.argparse_utilsc                 S   s   | S Nr   xr   r   r   <lambda>]   r   z+pl_legacy_patch.__enter__.<locals>.<lambda>c                 S   s   | S r&   r   r'   r   r   r   r)   ^   r   )r   sysmodules_gpus_arg_defaultr   	utilitiesargparse)selfZlegacy_argparse_moduler   r   r   	__enter__W   s
    

zpl_legacy_patch.__enter__N)exc_type	exc_valueexc_tracebackr   c                 C   s(   t tjjdrttjjd tjd= d S )Nr,   r%   )hasattrr   r-   r.   delattrr*   r+   )r/   r1   r2   r3   r   r   r   __exit__a   s    zpl_legacy_patch.__exit__)
r   
__module____qualname____doc__r0   r   r	   BaseExceptionr   r6   r   r   r   r   r$   H   s   
r$   )r   checkpoint_pathr   c              	   C   sv   t | }t| \} }t | }|r(|du r,| S tsDtj|t }ntj|}t	d| d| dt
| d | S )zApplies Lightning version migrations to a checkpoint dictionary and prints infos for the user.

    This function is used by the Lightning Trainer when resuming from a checkpoint.
    Nz>Lightning automatically upgraded your loaded checkpoint from vz to vzw. To apply the upgrade to your files permanently, run `python -m pytorch_lightning.utilities.upgrade_checkpoint --file `)r   r#   r   ospathrelpathgetcwdabspath_loginfostr)r   r;   Zold_versionZ
migrationsZnew_versionZ	path_hintr   r   r   _pl_migrate_checkpointl   s    rE   )r   r   c                 C   s   | d S )z*Get the version of a Lightning checkpoint.pytorch-lightning_versionr   )r   r   r   r   r      s    r   )r   versionr   c                 C   s   || d< dS )z*Set the version of a Lightning checkpoint.rF   Nr   r   rG   r   r   r   r!      s    r!   c                 C   s   |  d| dS )zXSet the legacy version of a Lightning checkpoint if a legacy version is not already set.z legacy_pytorch-lightning_versionN)
setdefaultrH   r   r   r   r       s    r    )r   targetmax_versionr   c                 C   s0   |du pt |t |k}t t| t |k o.|S )zfReturns whether a checkpoint qualifies for an upgrade when the version is lower than the given target.N)r
   r   )r   rJ   rK   Zis_lte_max_versionr   r   r   r      s    r   )N)N)N)(loggingr=   r*   typesr   r   typingr   r   r   r   r   r	   Zpackaging.versionr
   Zpytorch_lightningr   Z"lightning_fabric.utilities.importsr   Z lightning_fabric.utilities.typesr   Z#lightning_fabric.utilities.warningsr   Z/pytorch_lightning.utilities.migration.migrationr   Z%pytorch_lightning.utilities.rank_zeror   	getLoggerr   rB   rD   Z_CHECKPOINTr#   r$   rE   r   r!   r    boolr   r   r   r   r   <module>   s0    
 '$