
    Ng'                     D    d Z ddlZddlmZ  G 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                   *     e Zd ZdZd fd	Zd Z xZS )GlobalResponseNormz) Global Response Normalization layer
    ư>Tc                 \   t                                                       || _        |rd| _        d| _        d| _        nd| _        d| _        d| _        t          j        t          j	        |                    | _
        t          j        t          j	        |                    | _        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__s       K/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/layers/grn.pyr   zGlobalResponseNorm.__init__   s     	*%D!D)DMM%D D)DMl5;s#3#344LS!1!122			    c                 6   |                     d| j        d          }||                    | j        d          | j        z   z  }|t          j        | j                            | j	                  | j
                            | j	                  ||z            z   S )Nr	   T)pr   keepdim)r   r   )normr   meanr   r   r   addcmulr   viewr   r   )r   xx_gx_ns       r   forwardzGlobalResponseNorm.forward$   s    ffqd.f==SXX$"2DXAADHLM5=!>!>@P@PQUQ^@_@_abehahiiiir   )r   T)__name__
__module____qualname____doc__r   r'   __classcell__)r   s   @r   r   r      s]         3 3 3 3 3 3j j j j j j jr   r   )r+   r   r   Moduler    r   r   <module>r/      sl           j j j j j j j j j jr   