U
    ¯Ê¹j'  ã                   @   s.   d Z ddlZddlmZ G dd„ dejƒZdS )aª   Global Response Normalization Module

Based on the GRN layer presented in
`ConvNeXt-V2 - Co-designing and Scaling ConvNets with Masked Autoencoders` - https://arxiv.org/abs/2301.00808

This implementation
* works for both NCHW and NHWC tensor layouts
* uses affine param names matching existing torch norm layers
* slightly improves eager mode performance via fused addcmul

Hacked together by / Copyright 2023 Ross Wightman
é    N)Únnc                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	ÚGlobalResponseNormz) Global Response Normalization layer
    ç�íµ ÷Æ°>Tc                    sb   t ƒ  ¡  || _|r(d| _d| _d| _nd| _d| _d| _t t 	|¡¡| _
t t 	|¡¡| _d S )N)é   é   éÿÿÿÿ)r   r   r   r   )r   é   r   )r   r   r   r   )ÚsuperÚ__init__ÚepsÚspatial_dimÚchannel_dimÚwb_shaper   Ú	ParameterÚtorchZzerosÚweightÚbias)ÚselfÚdimr   Zchannels_last©Ú	__class__© úN/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/layers/grn.pyr
      s    
zGlobalResponseNorm.__init__c                 C   sV   |j d| jdd�}||j| jdd�| j  }|t | j | j	¡| j
 | j	¡|| ¡ S )Nr   T)Úpr   Úkeepdim)r   r   )Znormr   Zmeanr   r   r   Zaddcmulr   Úviewr   r   )r   ÚxZx_gZx_nr   r   r   Úforward$   s    zGlobalResponseNorm.forward)r   T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   Ú__classcell__r   r   r   r   r      s   r   )r!   r   r   ÚModuler   r   r   r   r   Ú<module>   s   