a
    d                     @   sl   d dl mZmZ d dlZd dlmZ d dlmZ G dd deZG dd deZG d	d
 d
ej	j
jjZdS )    )ABCabstractmethodN)Tensor)Modulec                   @   s8   e Zd ZdZeeedddZeeedddZdS )	LayerSynczxAbstract base class for creating plugins that wrap layers of a model with synchronization logic for
    multiprocessing.modelreturnc                 C   s   dS )zJOverride this method to apply synchronization to the layers of this model.N selfr   r
   r
   m/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/plugins/layer_sync.pyapply   s    zLayerSync.applyc                 C   s   dS )zEOverride this method to undo all modifications made in :meth:`apply`.Nr
   r   r
   r
   r   revert   s    zLayerSync.revertN)__name__
__module____qualname____doc__r   r   r   r   r
   r
   r
   r   r      s
   r   c                   @   s0   e Zd ZdZeedddZeedddZdS )NativeSyncBatchNormzA plugin that wraps all batch normalization layers of a model with synchronization logic for
    multiprocessing.

    This plugin has no effect in single-device operation.
    r   c                 C   s   t jj|S )a  Add global batchnorm for a model spread across multiple GPUs and nodes.

        Override this method to synchronize batchnorm layers between specific process groups instead
        of the whole world.

        Args:
            model: Reference to the current LightningModule

        Return:
            LightningModule with batchnorm layers synchronized within the process groups.
        )torchnnSyncBatchNormZconvert_sync_batchnormr   r
   r
   r   r   *   s    zNativeSyncBatchNorm.applyc                 C   s   |}t |tjjjjrt|j|j|j	|j
|j}|j
rnt   |j|_|j|_W d   n1 sd0    Y  |j|_|j|_|j|_t|dr|j|_| D ]\}}||| | q~|S )a  Convert the wrapped batchnorm layers back to regular batchnorm layers.

        Args:
            model: Reference to the current LightningModule

        Return:
            LightningModule with regular batchnorm layers that will no longer sync across processes.
        Nqconfig)
isinstancer   r   modules	batchnormr   _BatchNormXdZnum_featuresZepsZmomentumZaffineZtrack_running_statsZno_gradZweightZbiasZrunning_meanZrunning_varZnum_batches_trackedhasattrr   Znamed_childrenZ
add_moduler   )r   r   Zconverted_modulenamechildr
   r
   r   r   8   s$    
&
zNativeSyncBatchNorm.revertN)r   r   r   r   r   r   r   r
   r
   r
   r   r   #   s   r   c                   @   s   e Zd ZeddddZdS )r   N)inputr	   c                 C   s   d S )Nr
   )r   r    r
   r
   r   _check_input_dimZ   s    z_BatchNormXd._check_input_dim)r   r   r   r   r!   r
   r
   r
   r   r   Y   s   r   )abcr   r   r   r   Ztorch.nnr   r   r   r   r   r   Z
_BatchNormr   r
   r
   r
   r   <module>   s   6