o
    þÏ¯io  ã                   @   sr   d dl Z d dlmZ d dlmZ d dlmZ d dl mZ G dd„ dej	ƒZ
G dd„ dejƒZG d	d
„ d
ejƒZdS )é    N)Údifferentiable_all_reduce)Úget_world_size)Únnc                       ó    e Zd ZdZ‡ fdd„Z‡  ZS )ÚNaiveSyncBatchNorm1dzp
    An implementation of 1D naive sync batch normalization. See details in
    NaiveSyncBatchNorm2d below.
    c                    s8  t ƒ dks| jstƒ  |¡S |jd |jd }}tj|ddgd}tj|| ddgd}|dks5J dƒ‚tj||gdd}t|ƒdt	  ¡   }t 
||¡\}}|||  }t || j ¡}| j| }	| j||	  }
|	 ddd¡}	|
 ddd¡}
|  j| j| ¡ | j  7  _|  j| j| ¡ | j  7  _||	 |
 S )Né   r   é   ©Údimú/SyncBatchNorm does not support zero batch size.ç      ð?éÿÿÿÿ©r   ÚtrainingÚsuperÚforwardÚshapeÚtorchÚmeanÚcatr   ÚdistÚsplitÚrsqrtÚepsÚweightÚbiasÚreshapeÚrunning_meanÚmomentumÚdetachÚrunning_var©ÚselfÚinputÚBÚCr   ÚmeansqrÚvecÚvarÚinvstdÚscaler   ©Ú	__class__© úR/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/layers/batch_norm.pyr      s$   
zNaiveSyncBatchNorm1d.forward©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r-   r-   r+   r.   r   
   s    r   c                       r   )ÚNaiveSyncBatchNorm2daŽ  
    An implementation of 2D naive sync batch normalization.
    In PyTorch<=1.5, ``nn.SyncBatchNorm`` has incorrect gradient
    when the batch size on each worker is different.
    (e.g., when scale augmentation is used, or when it is applied to mask head).

    This is a slower but correct alternative to `nn.SyncBatchNorm`.

    Note:
        This module computes overall statistics by using
        statistics of each worker with equal weight.  The result is true statistics
        of all samples (as if they are all on one worker) only when all workers
        have the same (N, H, W). This mode does not support inputs with zero batch size.
    c                    s<  t ƒ dks| jstƒ  |¡S |jd |jd }}tj|g d¢d}tj|| g d¢d}|dks5J dƒ‚tj||gdd}t|ƒdt	  ¡   }t 
||¡\}}|||  }t || j ¡}| j| }	| j||	  }
|	 dddd¡}	|
 dddd¡}
|  j| j| ¡ | j  7  _|  j| j| ¡ | j  7  _||	 |
 S )Nr   r   )r   r   é   r	   r   r   r   r   r!   r+   r-   r.   r   <   s$   
zNaiveSyncBatchNorm2d.forwardr/   r-   r-   r+   r.   r5   ,   s    r5   c                       r   )ÚNaiveSyncBatchNorm3dzp
    An implementation of 3D naive sync batch normalization. See details in
    NaiveSyncBatchNorm2d above.
    c                    s@  t ƒ dks| jstƒ  |¡S |jd |jd }}tj|g d¢d}tj|| g d¢d}|dks5J dƒ‚tj||gdd}t|ƒdt	  ¡   }t 
||¡\}}|||  }t || j ¡}| j| }	| j||	  }
|	 ddddd¡}	|
 ddddd¡}
|  j| j| ¡ | j  7  _|  j| j| ¡ | j  7  _||	 |
 S )Nr   r   )r   r   r6   é   r	   r   r   r   r   r!   r+   r-   r.   r   _   s$   
zNaiveSyncBatchNorm3d.forwardr/   r-   r-   r+   r.   r7   X   s    r7   )r   Útorch.distributedÚdistributedr   Úfvcore.nn.distributedr   Úpytorchvideo.layers.distributedr   r   ÚBatchNorm1dr   ÚBatchNorm2dr5   ÚBatchNorm3dr7   r-   r-   r-   r.   Ú<module>   s   ",