a
    d                     @   s   d dl mZmZmZ d dl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 G dd deejjZG d	d
 d
eejjZdejeddddZdS )    )AnyOptionalUnionN)DataParallel)DistributedDataParallel)_DeviceDtypeModuleMixin)rank_zero_deprecationc                       s~   e Zd Zddd fddZeeedddZeeedd	d
ZeeedddZeeedddZeeedddZ	  Z
S )$_LightningPrecisionModuleWrapperBasepl.LightningModuleN)	pl_modulereturnc                    s0   t    || _t|dg }dd |D | _dS )a  Wraps the user's LightningModule. Requires overriding all ``*_step`` methods and ``forward`` so that it
        can safely be wrapped by a ``_LightningModuleWrapperBase`` and a ``*DataParallel``.

        Args:
            pl_module: the model to wrap
        !_ddp_params_and_buffers_to_ignorec                 S   s   g | ]}d | qS zmodule. .0pr   r   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/overrides/base.py
<listcomp>'       zA_LightningPrecisionModuleWrapperBase.__init__.<locals>.<listcomp>N)super__init__modulegetattrr   )selfr   r   	__class__r   r   r      s    
z-_LightningPrecisionModuleWrapperBase.__init__)argskwargsr   c                 O   s   t d S NNotImplementedErrorr   r   r   r   r   r   training_step)   s    z2_LightningPrecisionModuleWrapperBase.training_stepc                 O   s   t d S r   r    r"   r   r   r   validation_step,   s    z4_LightningPrecisionModuleWrapperBase.validation_stepc                 O   s   t d S r   r    r"   r   r   r   	test_step/   s    z._LightningPrecisionModuleWrapperBase.test_stepc                 O   s   t d S r   r    r"   r   r   r   predict_step2   s    z1_LightningPrecisionModuleWrapperBase.predict_stepc                 O   s   t d S r   r    r"   r   r   r   forward5   s    z,_LightningPrecisionModuleWrapperBase.forward)__name__
__module____qualname__r   r   r#   r$   r%   r&   r'   __classcell__r   r   r   r   r	      s   r	   c                       s   e Zd Zeedef  dd fddZeddddZe	e	e	d	d
dZ
edeedef  eedef  ddddZ  ZS )_LightningModuleWrapperBaser
   N)forward_moduler   c                    sp   t    t|tjs<tt|ddtjs<td|jj |dusHJ || _	t| j	dg }dd |D | _
dS )a  Wraps the user's LightningModule and redirects the forward call to the appropriate method, either
        ``training_step``, ``validation_step``, ``test_step``, or ``predict_step``.

        Inheriting classes may also modify the inputs or outputs of forward.

        Args:
            forward_module: The module to wrap. If it's not a LightningModule, it must have an attribute ``.module``
                pointing to a LightningModule reference.
        r   Nzk`forward_module` must be a `LightningModule` instance or have an attribute `.module` pointing to one, got: r   c                 S   s   g | ]}d | qS r   r   r   r   r   r   r   T   r   z8_LightningModuleWrapperBase.__init__.<locals>.<listcomp>)r   r   
isinstanceplLightningModuler   
ValueErrorr   r*   _forward_moduler   )r   r-   r   r   r   r   r   :   s    
z$_LightningModuleWrapperBase.__init__)r   c                 C   s   t | jtjr| jS | jjS r   )r.   r2   r/   r0   r   )r   r   r   r   lightning_moduleV   s    z,_LightningModuleWrapperBase.lightning_module)inputsr   r   c                 O   s   | j }|j}|d ur|jrL| jj|i |}|jsH|jd us@J d|j_|S |jrd| jj	|i |S |j
sp|jr| jj|i |S |jr| jj|i |S | j|i |S )NF)r3   Z_trainerZtrainingr2   r#   Zautomatic_optimizationmodelZrequire_backward_grad_syncZtestingr%   Zsanity_checkingZ
validatingr$   Z
predictingr&   )r   r4   r   r   Ztraineroutputr   r   r   r'   \   s     z#_LightningModuleWrapperBase.forward)r   r-   r   c                 C   s0   |d urt d| j d n|d u r,tdd S )NzThe argument `pl_module` in `z]` is deprecated in v1.8.0 and will be removed in v2.0.0. Please use `forward_module` instead.z&Argument `forward_module` is required.)r   r(   r1   )clsr   r-   r   r   r   _validate_init_argumentss   s    z4_LightningModuleWrapperBase._validate_init_arguments)NN)r(   r)   r*   r   r   r	   r   propertyr3   r   r'   classmethodr8   r+   r   r   r   r   r,   9   s     r,   Fr
   )wrapped_model_suppress_warningr   c                 C   sl   |st d | }t|ttfr(t|j}t|tr8|j}t|trH|j}t|t	j
shtdt| d|S )a  Recursively unwraps a :class:`~pytorch_lightning.core.module.LightningModule` by following the ``.module``
    attributes on the wrapper.

    .. deprecated:: v1.8.0
        The function ``unwrap_lightning_module`` is deprecated in v1.8.0 and will be removed in v2.0.0. Access the
        ``LightningModule`` directly through the strategy attribute ``Strategy.lightning_module``.

    Raises:
        TypeError: If the unwrapping leads to a module that is not a LightningModule and that cannot be unwrapped
            further.
    zThe function `unwrap_lightning_module` is deprecated in v1.8.0 and will be removed in v2.0.0. Access the `LightningModule` directly through the strategy attribute `Strategy.lightning_module`.z=Unwrapping the module did not yield a `LightningModule`, got z	 instead.)r   r.   r   r   unwrap_lightning_moduler   r,   r3   r	   r/   r0   	TypeErrortype)r;   r<   r5   r   r   r   r=      s    


r=   )F)typingr   r   r   ZtorchZtorch.nnnnr   Ztorch.nn.parallelr   Zpytorch_lightningr/   Z-lightning_fabric.utilities.device_dtype_mixinr   Z%pytorch_lightning.utilities.rank_zeror   Moduler	   r,   boolr=   r   r   r   r   <module>   s   J