
    3j                     H   S 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	\
4S
 jjr     SS\R                  S\S\	S\S\
S\
4S jjr " S S\R                  5      r   SS\R                  S\S\
S\
4S jjr " S S\R                  5      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
    Nx	drop_prob
block_sizegamma_scale
with_noiseinplace	batchwisec                 |   U R                   u  pxpX-  n[        U[        X5      5      nUU-  U-  US-  -  X-
  S-   X-
  S-   -  -  n[        R                  " [        R                  " U
5      R                  U R                  5      [        R                  " U	5      R                  U R                  5      5      u  pXS-  :  XUS-
  S-  -
  :  -  XS-  :  XUS-
  S-  -
  :  -  -  n[        R                  " USSX45      R                  U R                  S9nU(       a.  [        R                  " SXU
4U R                  U R                  S9nO[        R                  " U 5      nSU-
  U-
  U-   S:  R                  U R                  S9n[        R                  " U* USUS-  S9* nU(       a  U(       a-  [        R                  " SXU
4U R                  U R                  S9O[        R                  " U 5      nU(       a(  U R                  U5      R!                  USU-
  -  5        U $ U U-  USU-
  -  -   n  U $ UR#                  5       UR                  [        R$                  S9R'                  5       R)                  S5      -  R                  U R                  5      nU(       a  U R                  UU-  5        U $ U U-  U-  n U $ )zDropBlock. See https://arxiv.org/pdf/1810.12890.pdf

DropBlock with an experimental gaussian noise option. This layer has been tested on a few training
runs with success, but needs further validation and possibly optimization for lower runtime impact.
      dtype)r   devicekernel_sizestridepaddinggHz>)shapemintorchmeshgridarangetor   reshaper   rand	rand_likeF
max_pool2drandn
randn_likemul_add_numelfloat32sumadd)r   r   r   r   r   r   r	   _CHW
total_sizeclipped_block_sizegammaw_ih_ivalid_blockuniform_noise
block_masknormal_noisenormalize_scales                        S/home/wildlama/miniconda3/lib/python3.13/site-packages/spandrel/util/timm/__drop.pydrop_block_2dr6      s    JA!JZQ3 	
	
	 a
	  NQ1>A#56		8 
 ~~Q188$ell1o&8&8&BHC 
a'	'C7IA7MRS6S2S,ST
q(
(S8JQ8NST7T3T-T	UWK --aA\:==AGG=LK

AqQ<qwwqxxP*u9{*]:q@DD177DSJ,,	&"a'	 J   KKA!AGGAHHE!!!$ 	
 FF:##LA
N$CD H JZ!@@A H U]]!C!G!G!I!M!Md!SS
"QWW+ 	 FF:/0 H J0AH    c                 *   U R                   u    pgnX-  n	[        U[        X5      5      n
UU-  U	-  U
S-  -  X-
  S-   Xr-
  S-   -  -  n[        R                  " U 5      R	                  U5      n[
        R                  " UR                  U R                  5      U
SU
S-  S9nU(       ab  [        R                  " U 5      R                  5       nU(       a'  U R                  SU-
  5      R                  X-  5        U $ U SU-
  -  X-  -   n  U $ SU-
  nUR                  5       UR                  [        R                  S9R                  5       R                  S5      -  R                  U R                  S9nU(       a  U R                  X-  5        U $ X-  U-  n U $ )zDropBlock. See https://arxiv.org/pdf/1810.12890.pdf

DropBlock with an experimental gaussian noise option. Simplied from above without concern for valid
block mask at edges.
r   r   r         ?r   gư>)r   r   r   
empty_like
bernoulli_r   r   r   r   normal_r!   r"   r#   r$   r%   r&   )r   r   r   r   r   r   r'   r)   r*   r+   r,   r-   r2   r3   r4   s                  r5   drop_block_fast_2dr=   ]   s    JAqQJZQ3
	
	 a
	  NQ1>A#56		8 
 !!!$//6Jagg&"a'	J ''*224FF3#$)),*CD H S:%&)BBA H ^
U]]!C!G!G!I!M!Md!SS
"177"
 	 FF:/0 H 0AHr7   c                      ^  \ 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                  4S jrSrU =r$ )DropBlock2d   z3DropBlock. See https://arxiv.org/pdf/1810.12890.pdfr   r   r   r   r   r	   fastc                 v   > [         TU ]  5         Xl        X0l        X l        X@l        XPl        X`l        Xpl        g N)	super__init__r   r   r   r   r   r	   rA   )	selfr   r   r   r   r   r	   rA   	__class__s	           r5   rE   DropBlock2d.__init__   s4     	"&$$"	r7   r   c           	         U R                   (       a  U R                  (       d  U$ U R                  (       aB  [        UU R                  U R                  U R
                  U R                  U R                  5      $ [        UU R                  U R                  U R
                  U R                  U R                  U R                  5      $ rC   )
trainingr   rA   r=   r   r   r   r   r6   r	   rF   r   s     r5   forwardDropBlock2d.forward   s    }}DNNH99%    !   r7   )r	   r   r   rA   r   r   r   )皙?   r9   FFFT)__name__
__module____qualname____firstlineno____doc__floatintboolrE   r   TensorrL   __static_attributes____classcell__rG   s   @r5   r?   r?      s    =     	
     &  r7   r?   rJ   scale_by_keepc                     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   rJ   r\   	keep_probr   random_tensors          r5   	drop_pathrd      s}     CxIIWWQZMD	
 E KK&11)<M3=9%r7   c                   d   ^  \ rS rSrSrS
S\S\4U 4S jjjrS\R                  4S jr
S rS	rU =r$ )DropPath   zYDrop paths (Stochastic Depth) per sample  (when applied in main path of residual blocks).r   r\   c                 :   > [         TU ]  5         Xl        X l        g rC   )rD   rE   r   r\   )rF   r   r\   rG   s      r5   rE   DropPath.__init__   s    "*r7   r   c                 X    [        XR                  U R                  U R                  5      $ rC   )rd   r   rJ   r\   rK   s     r5   rL   DropPath.forward   s    NNDMM4;M;MNNr7   c                 6    S[        U R                  S5      S 3$ )Nz
drop_prob=   z0.3f)roundr   )rF   s    r5   
extra_reprDropPath.extra_repr   s    E$..3D9::r7   )r   r\   )r^   T)rP   rQ   rR   rS   rT   rU   rW   rE   r   rX   rL   ro   rY   rZ   r[   s   @r5   rf   rf      s:    c+% +d + +
O O; ;r7   rf   )rN   rO   r9   FFF)rN   rO   r9   FF)r^   FT)rT   r   torch.nnnntorch.nn.functional
functionalr   rX   rU   rV   rW   r6   r=   Moduler?   rd   rf    r7   r5   <module>rw      s,  $    
 B||BB B 	B
 B B BN /||// / 	/
 / /d+")) +` 	||  	6;ryy ;r7   