
    RЦi"                     H   S r SSKJrJr  SSKrSSKJr  SSKJs  Jr	         SS\R                  S\S\S\S\S	\S
\S\4S jjr " S S\R                  5      rSS\S\S\4S jjr " S S\R                  5      r SS\S\\\\   4   S\S\\\   \\\      4   4S jjrg)a=  DropBlock, DropPath

PyTorch implementations of DropBlock and DropPath (Stochastic Depth) regularization layers.

Papers:
DropBlock: A regularization method for convolutional networks (https://arxiv.org/abs/1810.12890)

Deep Networks with Stochastic Depth (https://arxiv.org/abs/1603.09382)

Code:
DropBlock impl inspired by two Tensorflow impl that I liked:
 - https://github.com/tensorflow/tpu/blob/master/models/official/resnet/resnet_model.py#L74
 - https://github.com/clovaai/assembled-cnn/blob/master/nets/blocks.py

Hacked together by / Copyright 2020 Ross Wightman
    )ListUnionNx	drop_prob
block_sizegamma_scale
with_noiseinplacecouple_channelsscale_by_keepc           	         U R                   u  pp[        X*5      [        X+5      p[        X1-  U
-  U-  5      [        X-  5      -  [        X-
  S-   X-
  S-   -  5      -  nX(       a  SOU	X4n[        R                  " 5          [        R
                  " XR                  U R                  S9R                  U5      n[        R                  " UX4SUS-  US-  4S9nUS-  S:X  d	  US-  S:X  a  USUS-   S-  S2US-   S-  S24   nSU-
  nSSS5        U(       a  [        R                  " 5          [        R                  " W5      R                  5       nUR                  W5        SSS5        U(       a"  U R                  W5      R                  W5        U $ U W-  W-   n  U $ U(       a  [        R                  " 5          WR                  5       UR!                  [        R"                  S	9R%                  5       R'                  S
5      -  nUR                  UR!                  U R                  5      5        SSS5        U(       a  U R                  W5        U $ U W-  n U $ ! , (       d  f       GNX= f! , (       d  f       GN= f! , (       d  f       NS= f)a  DropBlock. See https://arxiv.org/pdf/1810.12890.pdf

DropBlock with an experimental gaussian noise option.

Args:
    x: Input tensor of shape (B, C, H, W).
    drop_prob: Probability of dropping a block.
    block_size: Size of the block to drop.
    gamma_scale: Scale factor for the drop probability.
    with_noise: If True, add gaussian noise to dropped regions instead of zeros.
    inplace: If True, perform operation in-place.
    couple_channels: If True, all channels share the same drop mask (per the original paper).
        If False, each channel gets an independent mask.
    scale_by_keep: If True, scale kept activations to maintain expected values.

Returns:
    Tensor with dropped blocks, same shape as input.
   )dtypedevice   )kernel_sizestridepaddingr   .N      ?)r   gHz>)shapeminfloattorchno_grademptyr   r   
bernoulli_F
max_pool2d
empty_likenormal_mul_add_numeltofloat32sumadd)r   r   r   r   r	   r
   r   r   BCHWkhkwgammanoise_shape
block_mask	keep_masknoisenormalize_scales                       O/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/layers/drop.pydrop_block_2dr5      s0   8 JA!Z!3 +)A-12U27^CeQVVWZ\]\bef\fLgFhhE ?a18K	[[GGAHHMXXY^_
 \\1WbAg&	

 6Q;"q&A+#C"q&Aa1$EFJO	 
" ]]_$$Y/779EJJz"  FF9""5) H I%A H "+//"3illl6W6[6[6]6a6abf6g"g11!'':; !
 FF9 H IAHM 
$ _ !s&   BI-6I,;A6I>
I),
I;>
Jc                   d   ^  \ rS rSrSr       SS\S\S\S\S\S\S	\4U 4S
 jjjrS r	Sr
U =r$ )DropBlock2df   a8  DropBlock. See https://arxiv.org/pdf/1810.12890.pdf

Args:
    drop_prob: Probability of dropping a block.
    block_size: Size of the block to drop.
    gamma_scale: Scale factor for the drop probability.
    with_noise: If True, add gaussian noise to dropped regions instead of zeros.
    inplace: If True, perform operation in-place.
    couple_channels: If True, all channels share the same drop mask (per the original paper).
        If False, each channel gets an independent mask.
    scale_by_keep: If True, scale kept activations to maintain expected values.
r   r   r   r	   r
   r   r   c                    > [         TU ]  5         Xl        X0l        X l        X@l        XPl        X`l        Xpl        SS1n	U H#  n
X;  d  M
  SS K	nUR                  SU
 S35        M%     g )N	batchwisefastr   z/DropBlock2d() got unexpected keyword argument '')super__init__r   r   r   r	   r
   r   r   warningswarn)selfr   r   r   r	   r
   r   r   kwargsdeprecated_argskr?   	__class__s               r4   r>   DropBlock2d.__init__t   sm     	"&$$.* '/A' OPQsRSTU     c                     U R                   (       a  U R                  (       d  U$ [        UU R                  U R                  U R                  U R
                  U R                  U R                  U R                  S9$ )N)r   r   r   r	   r
   r   r   )	trainingr   r5   r   r   r	   r
   r   r   rA   r   s     r4   forwardDropBlock2d.forward   s[    }}DNNHnn((LL 00,,	
 		
rG   )r   r   r   r   r
   r   r	   g?   r   FFTT)__name__
__module____qualname____firstlineno____doc__r   intboolr>   rK   __static_attributes____classcell__rE   s   @r4   r7   r7   f   s      #!$$!$("&VV V 	V
 V V "V  V V6
 
rG   r7   rI   c                     US:X  d  U(       d  U $ SU-
  nU R                   S   4SU R                  S-
  -  -   nU R                  U5      R                  U5      nUS:  a  U(       a  UR	                  U5        X-  $ )a  Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
'survival rate' as the argument.

        r   r   )r   )r   ndim	new_emptyr   div_)r   r   rI   r   	keep_probr   random_tensors          r4   	drop_pathr`      su     BhIIWWQZMDAFFQJ//EKK&11)<M3=9%rG   c                   H   ^  \ rS rSrSrS	S\S\4U 4S jjjrS rS r	Sr
U =r$ )
DropPath   z^Drop paths (Stochastic Depth) per sample  (when applied in main path of residual blocks).
    r   r   c                 :   > [         TU ]  5         Xl        X l        g N)r=   r>   r   r   )rA   r   r   rE   s      r4   r>   DropPath.__init__   s    "*rG   c                 X    [        XR                  U R                  U R                  5      $ re   )r`   r   rI   r   rJ   s     r4   rK   DropPath.forward   s    NNDMM4;M;MNNrG   c                 6    S[        U R                  S5      S 3$ )Nz
drop_prob=   z0.3f)roundr   )rA   s    r4   
extra_reprDropPath.extra_repr   s    E$..3D9::rG   )r   r   )rZ   T)rO   rP   rQ   rR   rS   r   rU   r>   rK   rl   rV   rW   rX   s   @r4   rb   rb      s0    +% +T + +
O; ;rG   rb   drop_path_ratedepths	stagewisereturnc                    [        U[        5      (       aG  U(       a  [        S5      e[        R                  " SXSS9 Vs/ s H  o3R                  5       PM     nnU$ [        U5      nU(       aD  [        R                  " SXSS9R                  U5       Vs/ s H  o3R                  5       PM     nnU$ [        R                  " SXSS9 Vs/ s H  o3R                  5       PM     nnU$ s  snf s  snf s  snf )a  Generate drop path rates for stochastic depth.

This function handles two common patterns for drop path rate scheduling:
1. Per-block: Linear increase from 0 to drop_path_rate across all blocks
2. Stage-wise: Linear increase across stages, with same rate within each stage

Args:
    drop_path_rate: Maximum drop path rate (at the end).
    depths: Either a single int for total depth (per-block mode) or
            list of ints for depths per stage (stage-wise mode).
    stagewise: If True, use stage-wise pattern. If False, use per-block pattern.
               When depths is a list, stagewise defaults to True.

Returns:
    For per-block mode: List of drop rates, one per block.
    For stage-wise mode: List of lists, drop rates per stage.
z;stagewise=True requires depths to be a list of stage depthsr   cpu)r   )	
isinstancerT   
ValueErrorr   linspaceitemr&   splittolist)rn   ro   rp   r   dprtotal_depths         r4   calculate_drop_path_ratesr|      s    , &#Z[[!&>RW!XY!XAvvx!XY
 &k',~~a]b'c'i'ijp'qr'q!88:'qCrJ &+^^A~[`%ab%a668%aCbJ Z s cs    C(C-C2rM   )rZ   FT)F)rS   typingr   r   r   torch.nnnntorch.nn.functional
functionalr   Tensorr   rT   rU   r5   Moduler7   r`   rb   r|    rG   r4   <module>r      s        
    $"K<<KK K 	K
 K K K K\5
")) 5
pE $ t (;ryy ;$  &&c49n%& & 4;T%[))*	&rG   