
    3j{                       S SK Jr  S SKrS SKJs  Jr  S SKJr   " S S\R                  5      r " S S\R                  5      r
 " S S	\R                  5      r " S
 S\R                  5      rSS jr          SS jrg)    )annotationsN)nnc                  f   ^  \ rS rSrSr      S                   SU 4S jjjrSrU =r$ )ConvNormAct   a  Apply a sequence of Convolution, Normalization, and Activation layers.

Args:
    in_channels: Number of input channels.
    out_channels: Number of output channels.
    kernel_size: Size of the convolution kernel.
    stride: Stride of the convolution. Default: 1.
    padding: Zero-padding added to both sides of the input. Default: 0.
    groups: Number of blocked connections from input to output. Default: 1.
    norm: Normalization layer type. Default: :class:`nn.BatchNorm2d`.
    act: Activation layer type. Default: :class:`nn.ReLU`.
c
           
       > [         TU ]  5         US-  S:X  a0  US-
  S-  n
US-
  U
-
  n[        R                  " XX45      U l        SnOUS-
  S-  n[        R
                  " XX4USUS5      n[        R                  " U5      n[        R                  [        R                  [        R                  S.U   " SS9nU R                  X}5        U R                  X5        U R                  X5        g )N   r      F)relusilunoneT)inplace)super__init__r   	ZeroPad2dpadConv2dBatchNorm2dReLUSiLUIdentity__setattr__)selfin_channelsout_channelskernel_sizestrideactgroupsconv_namingnorm_naming
act_namingp1p2paddingconvnorm
activation	__class__s                   N/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/models/common.pyr   ConvNormAct.__init__'   s     	?a
 /a'Bq2%B||RR$45DHG"Q1,GyyKRSU[]bc~~l+ ggrwwLSQZ^_
++0    )r   )r
   r   r
   r&   r'   r   )r   intr   r-   r   r-   r   r-   r   strr   r-   r    r.   r!   r.   r"   r.   returnNone)__name__
__module____qualname____firstlineno____doc__r   __static_attributes____classcell__r)   s   @r*   r   r      s    $ !!11 1 	1
 1 1 1 1 1 1 
1 1r,   r   c                  V   ^  \ rS rSrSr S           SU 4S jjjrSS jrSrU =r$ )	MLPJ   a]  Implement a Multi-Layer Perceptron (MLP) for feature projection.

Args:
    input_dim: The number of input features.
    hidden_dim: The number of features in hidden layers.
    output_dim: The number of output features.
    num_layers: The total number of layers.
    sigmoid_output: Whether to apply a sigmoid to the final output. Default: False.
c                   > [         TU ]  5         X@l        U/US-
  -  n[        R                  " S [        U/UQ/ UQUP5       5       5      U l        XPl        g )Nr
   c              3  R   #    U  H  u  p[         R                  " X5      v   M     g 7fN)r   Linear).0nks      r*   	<genexpr>MLP.__init__.<locals>.<genexpr>[   s     #g@fBIIaOO@fs   %')r   r   
num_layersr   
ModuleListziplayerssigmoid_output)r   	input_dim
hidden_dim
output_dimrE   rI   hr)   s          r*   r   MLP.__init__U   s\     	$LJN+mm#gYOQROUeWXUeZdUe@f#gg,r,   c                    [        U R                  5       H;  u  p#X R                  S-
  :  a  [        R                  " U" U5      5      OU" U5      nM=     U R
                  (       a  [        R                  " U5      nU$ )Nr
   )	enumeraterH   rE   Fr   rI   sigmoid)r   xilayers       r*   forwardMLP.forward^   sX    !$++.HA$%!(;$;uQx qA /		!Ar,   )rH   rE   rI   )F)rJ   r-   rK   r-   rL   r-   rE   r-   rI   boolr/   r0   rS   torch.Tensorr/   rZ   	r1   r2   r3   r4   r5   r   rV   r6   r7   r8   s   @r*   r:   r:   J   sS     in--*--;>-LO-ae-	- - r,   r:   c                  >   ^  \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	DropPathh   zYDrop paths (Stochastic Depth) per sample  (when applied in main path of residual blocks).c                :   > [         TU ]  5         Xl        X l        g r>   )r   r   	drop_probscale_by_keep)r   r`   ra   r)   s      r*   r   DropPath.__init__k   s    "*r,   c                D   U R                   S:X  d  U R                  (       d  U$ SU R                   -
  nUR                  S   4SUR                  S-
  -  -   nUR	                  U5      R                  U5      nUS:  a"  U R                  (       a  UR                  U5        X-  $ )N        r
   r   )r
   )r`   trainingshapendim	new_empty
bernoulli_ra   div_)r   rS   	keep_probrf   random_tensors        r*   rV   DropPath.forwardp   s    >>S H&	
 33E*55i@s?t11y)  r,   )r`   ra   )rd   T)r`   floatra   rX   r/   r0   rY   r[   r8   s   @r*   r]   r]   h   s    c+ +
! !r,   r]   c                  >   ^  \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	LayerNorm2d}   z8Apply Layer Normalization over a 4D input tensor (NCHW).c                   > [         TU ]  5         [        R                  " [        R
                  " U5      5      U l        [        R                  " [        R                  " U5      5      U l        X l	        g r>   )
r   r   r   	Parametertorchonesweightzerosbiaseps)r   num_channelsry   r)   s      r*   r   LayerNorm2d.__init__   sE    ll5::l#;<LL\!:;	r,   c                   UR                  SSS9nX-
  R                  S5      R                  SSS9nX-
  X0R                  -   R                  5       -  nU R                  S S 2S S 4   U-  U R
                  S S 2S S 4   -   nU$ )Nr
   T)keepdimr	   )meanpowry   sqrtrv   rx   )r   rS   uss       r*   rV   LayerNorm2d.forward   s    FF1dF#UKKN40Uq88|))++KK4&*TYYq$}-EEr,   )rx   ry   rv   )gư>)rz   r-   ry   rn   r/   r0   rY   r[   r8   s   @r*   rp   rp   }   s    B  r,   rp   c           	     N   U R                   u  p#pEXU-  -
  U-  nXU-  -
  U-  nUS:  d  US:  a  [        R                  " U SSSUSU45      n X6-   XG-   pU R                  X(U-  XU-  X5      n U R	                  SSSSSS5      R                  5       R                  SXU5      n
XU	44$ )a'  Partition into non-overlapping windows with padding if needed.

Args:
    x: input tokens with [B, H, W, C].
    window_size: window size.

Returns:
    windows: windows after partition with [B * num_windows, window_size, window_size, C].
    (Hp, Wp): padded height and width before partition

r   r
      r	         )rf   rQ   r   viewpermute
contiguous)rS   window_sizeBHWCpad_hpad_wHpWpwindowss              r*   window_partitionr      s     JA!{?*k9E{?*k9EqyEAIEE!aAua/0Y		q#[2C[TAii1aAq)446;;BZ[\GHr,   c                8   Uu  pEUu  pgU R                   S   XE-  U-  U-  -  nU R                  XU-  XQ-  XS5      n	U	R                  SSSSSS5      R                  5       R                  XUS5      n	XF:  d  XW:  a  U	SS2SU2SU2SS24   R                  5       n	U	$ )	aY  Window unpartition into original sequences and removing padding.

Args:
    windows: input tokens with [B * num_windows, window_size, window_size, C].
    window_size: window size.
    pad_hw: padded height and width (Hp, Wp).
    hw: original height and width (H, W) before padding.

Returns:
    x: unpartitioned sequences with [B, H, W, C].

r   r   r
   r   r	   r   r   N)rf   r   r   r   )
r   r   pad_hwhwr   r   r   r   r   rS   s
             r*   window_unpartitionr      s     FBDAaRW3{BCAQk)2+<kXZ[A			!Q1a#..055aRDA	va!RaRlO&&(Hr,   )rS   rZ   r   r-   r/   z$tuple[torch.Tensor, tuple[int, int]])
r   rZ   r   r-   r   tuple[int, int]r   r   r/   rZ   )
__future__r   rt   torch.nn.functionalr   
functionalrQ   
Sequentialr   Moduler:   r]   rp   r   r    r,   r*   <module>r      s   $ #    ,1"-- ,1b")) <!ryy !*")) "2(+5DJYr,   