o
    پi'                     @   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torchzerosweightbias)selfdimr   channels_last	__class__ C/home/ubuntu/.local/lib/python3.10/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   )normr   meanr   r   r   addcmulr   viewr   r   )r   xx_g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    