o
    zil                     @   sx   d dl mZmZ d dlZd dlm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)Module)overridec                   @   <   e Zd ZdZededefddZededef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      dS )zJOverride this method to apply synchronization to the layers of this model.N selfr	   r   r   X/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/plugins/layer_sync.pyapply       zLayerSync.applyc                 C   r   )zEOverride this method to undo all modifications made in :meth:`apply`.Nr   r   r   r   r   revert   r   zLayerSync.revertN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s    r   c                   @   r   )	TorchSyncBatchNormzA 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	   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SyncBatchNormconvert_sync_batchnormr   r   r   r   r   +   s   zTorchSyncBatchNorm.applyc                 C   s   |}t |tjjjjrLt|j|j|j	|j
|j}|j
r7t  |j|_|j|_W d   n1 s2w   Y  |j|_|j|_|j|_t|drL|j|_| D ]\}}||| | qP~|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   _BatchNormXdnum_featuresepsmomentumaffinetrack_running_statsno_gradweightbiasrunning_meanrunning_varnum_batches_trackedhasattrr   named_children
add_moduler   )r   r	   converted_modulenamechildr   r   r   r   ;   s&   


zTorchSyncBatchNorm.revertN)r   r   r   r   r   r   r   r   r   r   r   r   r   $   s    r   c                   @   s"   e Zd ZededdfddZdS )r    inputr
   Nc                 C   s   d S )Nr   )r   r2   r   r   r   _check_input_dim_   s   z_BatchNormXd._check_input_dim)r   r   r   r   r   r3   r   r   r   r   r    ^   s    r    )abcr   r   r   r   torch.nnr   typing_extensionsr   r   r   r   r   r   
_BatchNormr    r   r   r   r   <module>   s   :