
    ^j!                        d Z ddlmZmZmZ ddlZddlmZ ddlmc m	Z
 ddlmZ ddlmZmZmZ  G d dej"                        Ze G d	 d
ej"                               Z G d dej"                        Ze G d dej"                               Zy)a   Convolution with Weight Standardization (StdConv and ScaledStdConv)

StdConv:
@article{weightstandardization,
  author    = {Siyuan Qiao and Huiyu Wang and Chenxi Liu and Wei Shen and Alan Yuille},
  title     = {Weight Standardization},
  journal   = {arXiv preprint arXiv:1903.10520},
  year      = {2019},
}
Code: https://github.com/joe-siyuan-qiao/WeightStandardization

ScaledStdConv:
Paper: `Characterizing signal propagation to close the performance gap in unnormalized ResNets`
    - https://arxiv.org/abs/2101.08692
Official Deepmind JAX code: https://github.com/deepmind/deepmind-research/tree/master/nfnets

Hacked together by / copyright Ross Wightman, 2021.
    )OptionalTupleUnionN   )register_notrace_module)get_paddingget_padding_valuepad_samec                        e Zd ZdZ	 	 	 	 	 	 	 	 ddededeeeeef   f   deeeeef   f   deeeeeef   f      deeeeef   f   ded	ed
e	f fdZ
d Z xZS )	StdConv2dzConv2d with Weight Standardization. Used for BiT ResNet-V2 models.

    Paper: `Micro-Batch Training with Batch-Channel Normalization and Weight Standardization` -
        https://arxiv.org/abs/1903.10520v2
    
in_channelout_channelskernel_sizestridepaddingdilationgroupsbiasepsc                 d    |t        |||      }t        | 	  |||||||||
|
       |	| _        y )Nr   r   r   r   r   devicedtype)r   super__init__r   )selfr   r   r   r   r   r   r   r   r   r   r   	__class__s               _/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/layers/std_conv.pyr   zStdConv2d.__init__#   sL     ?!+vx@Gk&hvDQW_d 	 	f     c           	      f   t        j                  | j                  j                  d| j                  d      d d dd| j
                        j                  | j                        }t        j                  ||| j                  | j                  | j                  | j                  | j                        }|S Nr   T        )trainingmomentumr   )F
batch_normweightreshaper   r   
reshape_asconv2dr   r   r   r   r   r   xr(   s      r   forwardzStdConv2d.forward8   s    KK4#4#4b9
 *T[[
! 	 HHQ		4;;dmmUYU`U`ar   )r   Nr   r   Fư>NN)__name__
__module____qualname____doc__intr   r   r   boolfloatr   r.   __classcell__r   s   @r   r   r      s     34=A45  sE#s(O34	
 #uS#X./ eCsCx$89: CsCx01   *
r   r   c                        e Zd ZdZ	 	 	 	 	 	 	 	 ddededeeeeef   f   deeeeef   f   dedeeeeef   f   ded	ed
e	f fdZ
d Z xZS )StdConv2dSamezConv2d with Weight Standardization. TF compatible SAME padding. Used for ViT Hybrid model.

    Paper: `Micro-Batch Training with Batch-Channel Normalization and Weight Standardization` -
        https://arxiv.org/abs/1903.10520v2
    r   r   r   r   r   r   r   r   r   c                 x    t        ||||      \  }}t        | 	  |||||||||
|
       || _        |	| _        y )Nr   r   r   )r	   r   r   same_padr   )r   r   r   r   r   r   r   r   r   r   r   r   
is_dynamicr   s                r   r   zStdConv2dSame.__init__L   sU     0V^fgk&'\dV5 	 	B #r   c           	         | j                   r,t        || j                  | j                  | j                        }t        j                  | j                  j                  d| j                  d      d d dd| j                        j                  | j                        }t        j                  ||| j                  | j                  | j                  | j                  | j                        }|S r!   )r=   r
   r   r   r   r&   r'   r(   r)   r   r   r*   r+   r   r   r   r,   s      r   r.   zStdConv2dSame.forwarda   s    ==D,,dkk4==IAKK4#4#4b9
 *T[[
! 	 HHQ		4;;dmmUYU`U`ar   )r   SAMEr   r   Fr/   NN)r0   r1   r2   r3   r4   r   r   strr5   r6   r   r.   r7   r8   s   @r   r:   r:   E   s     34!45  sE#s(O34	
 #uS#X./  CsCx01   *r   r:   c                        e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddededeeeeef   f   deeeeef   f   deeeeeef   ef      deeeeef   f   ded	e	d
e
de
de
f fdZd fdZd Z xZS )ScaledStdConv2da4  Conv2d layer with Scaled Weight Standardization.

    Paper: `Characterizing signal propagation to close the performance gap in unnormalized ResNets` -
        https://arxiv.org/abs/2101.08692

    NOTE: the operations used in this impl differ slightly from the DeepMind Haiku impl. The impact is minor.
    in_channelsr   r   r   r   r   r   r   gammar   	gain_initc           	      b   ||d}|t        |||      }t        |   |||f|||||d| |	| j                  d   j	                         dz  z  | _        |
| _        || _        t        j                  t        j                  | j                  dddffi |      | _        | j                          y )Nr   r   r   r   r   r   r   r         r   )r   r   r   r(   numelscaler   rF   nn	Parametertorchemptyr   gainreset_parameters)r   rD   r   r   r   r   r   r   r   rE   r   rF   r   r   ddr   s                  r   r   zScaledStdConv2d.__init__y   s      /?!+vx@G{	,;A7]e	,(*	, T[[^113t;;
"LLd.?.?Aq-I!PR!PQ	r   c                     t        | d      rMt        j                  j                  j	                  | j
                  | j                         t        | !          y y NrQ   	hasattrrO   rM   init	constant_rQ   rF   r   rR   r   r   s    r   rR   z ScaledStdConv2d.reset_parameters   =    4 HHMM##DIIt~~>G$& !r   c           	         t        j                  | j                  j                  d| j                  d      d d | j
                  | j                  z  j                  d      dd| j                        j                  | j                        }t        j                  ||| j                  | j                  | j                  | j                  | j                        S Nr   r"   Tr#   )r(   r$   r%   r   )r&   r'   r(   r)   r   rQ   rL   viewr   r*   r+   r   r   r   r   r   r,   s      r   r.   zScaledStdConv2d.forward   s    KK4#4#4b9II

*004
 *T[[
! 	 xx6499dkk4<<X\XcXcddr   )
r   Nr   r   T      ?r/   r_   NNreturnN)r0   r1   r2   r3   r4   r   r   r   rA   r5   r6   r   rR   r.   r7   r8   s   @r   rC   rC   p   s     34BF45"     sE#s(O34	 
 #uS#X./  eCsCx#$=>?  CsCx01           <'
er   rC   c                        e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddededeeeeef   f   deeeeef   f   dedeeeeef   f   ded	ed
e	de	de	f fdZ
d fdZd Z xZS )ScaledStdConv2dSamea\  Conv2d layer with Scaled Weight Standardization and Tensorflow-like SAME padding support

    Paper: `Characterizing signal propagation to close the performance gap in unnormalized ResNets` -
        https://arxiv.org/abs/2101.08692

    NOTE: the operations used in this impl differ slightly from the DeepMind Haiku impl. The impact is minor.
    rD   r   r   r   r   r   r   r   rE   r   rF   c           	      v   ||d}t        ||||      \  }}t        |   |||f|||||d| |	| j                  d   j	                         dz  z  | _        || _        |
| _        || _        t        j                  t        j                  | j                  dddffi |      | _        | j                          y )NrH   r<   rI   r   rJ   r   )r	   r   r   r(   rK   rL   r=   r   rF   rM   rN   rO   rP   r   rQ   rR   )r   rD   r   r   r   r   r   r   r   rE   r   rF   r   r   rS   r>   r   s                   r   r   zScaledStdConv2dSame.__init__   s      //V^fg{	,;A7]e	,(*	, T[[^113t;;
""LLd.?.?Aq-I!PR!PQ	r   c                     t        | d      rMt        j                  j                  j	                  | j
                  | j                         t        | !          y y rU   rV   rZ   s    r   rR   z$ScaledStdConv2dSame.reset_parameters   r[   r   c           	          | j                   r,t        || j                  | j                  | j                        }t        j                  | j                  j                  d| j                  d      d d | j                  | j                  z  j                  d      dd| j                        j                  | j                        }t        j                  ||| j                   | j                  | j"                  | j                  | j$                        S r]   )r=   r
   r   r   r   r&   r'   r(   r)   r   rQ   rL   r^   r   r*   r+   r   r   r   r,   s      r   r.   zScaledStdConv2dSame.forward   s    ==D,,dkk4==IAKK4#4#4b9II

*004
 *T[[
! 	 xx6499dkk4<<X\XcXcddr   )
r   r@   r   r   Tr_   r/   r_   NNr`   )r0   r1   r2   r3   r4   r   r   rA   r5   r6   r   rR   r.   r7   r8   s   @r   rc   rc      s     34!45"     sE#s(O34	 
 #uS#X./    CsCx01           <'er   rc   )r3   typingr   r   r   rO   torch.nnrM   torch.nn.functional
functionalr&   _fxr   r   r   r	   r
   Conv2dr   r:   rC   rc    r   r   <module>rn      s   $ * )     ( = =%		 %P 'BII ' 'T9ebii 9ex ;e")) ;e ;er   