§
    ·¯¸jü"  ã                   óP  — d Z ddlmZmZ ddlZddlmZ ddlmc mZ	 	 	 	 	 	 	 	 dd	ej
        d
ededededededefd„Z G d„ dej        ¦  «        Zdd
ededefd„Z G d„ dej        ¦  «        Z	 d dedeeee         f         dedeee         eee                  f         fd„ZdS )!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çš™™™™™¹?é   ç      ð?FTÚxÚ	drop_probÚ
block_sizeÚgamma_scaleÚ
with_noiseÚinplaceÚcouple_channelsÚscale_by_keepc           	      óF  — | j         \  }}	}
}t          ||
¦  «        t          ||¦  «        }}t          ||z  |
z  |z  ¦  «        t          ||z  ¦  «        z  t          |
|z
  dz   ||z
  dz   z  ¦  «        z  }||rdn|	|
|f}t          j        ¦   «         5  t          j        || j        | j        ¬¦  «                             |¦  «        }t          j
        |||fd|dz  |dz  f¬¦  «        }|dz  dk    s	|dz  dk    r|d|dz   dz  d…|dz   dz  d…f         }d|z
  }ddd¦  «         n# 1 swxY w Y   |ršt          j        ¦   «         5  t          j        |¦  «                             ¦   «         }|                     |¦  «         ddd¦  «         n# 1 swxY w Y   |r)|                      |¦  «                             |¦  «         nÚ| |z  |z   } nÑ|r²t          j        ¦   «         5  |                     ¦   «         |                     t          j        ¬	¦  «                             ¦   «                              d
¦  «        z  }|                     |                     | j        ¦  «        ¦  «         ddd¦  «         n# 1 swxY w Y   |r|                      |¦  «         n| |z  } | S )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   .Nr   )r   gH¯¼šò×z>)Ú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                       ú`/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/timm/layers/drop.pyÚdrop_block_2dr7      sx  € ð8 ”�J€A€qˆ!ˆQÝ�˜QÑÔ¥ Z°Ñ!3Ô!3ˆ€Bõ �+ 	Ñ)¨AÑ-°Ñ1Ñ2Ô2µU¸2À¹7±^´^ÑCÅeÈQÐQSÉVÐVWÉZÐ\]Ð`bÑ\bÐefÑ\fÑLgÑFhÔFhÑh€Eð ˜?Ð1�a�a°°1°aÐ8€KÝ	Œ‰Œð $ð $Ý”[ °A´GÀAÄHÐMÑMÔM×XÒXÐY^Ñ_Ô_ˆ
õ ”\ØØ˜R˜ØØ˜1‘W˜b A™gÐ&ð	
ñ 
ô 
ˆ
ð �‰6�QŠ;ˆ;˜"˜q™& Aš+˜+à# C¨"¨q©&°A©¨¨¸¸a¹À1¹¸¸Ð$EÔFˆJà˜‘Oˆ	ð$ð $ð $ñ $ô $ð $ð $ð $ð $ð $ð $øøøð $ð $ð $ð $ð" ð ÝŒ]‰_Œ_ð 	#ð 	#ÝÔ$ YÑ/Ô/×7Ò7Ñ9Ô9ˆEØ�JŠJ�zÑ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð ð 	&Ø�FŠF�9ÑÔ×"Ò" 5Ñ)Ô)Ð)Ð)à�I‘ Ñ%ˆAˆAàð 	<Ý”‘”ð <ð <à"+§/¢/Ñ"3Ô"3°i·l²lÍÌ°lÑ6WÔ6W×6[Ò6[Ñ6]Ô6]×6aÒ6aÐbfÑ6gÔ6gÑ"g�Ø—’˜×1Ò1°!´'Ñ:Ô:Ñ;Ô;Ð;ð<ð <ð <ñ <ô <ð <ð <ð <ð <ð <ð <øøøð <ð <ð <ð <ð
 ð 	Ø�FŠF�9ÑÔÐÐà�I‘ˆAà€Hs8   ÂB	D)Ä)D-Ä0D-Å	<FÆFÆFÇ%BI9É9I=Ê I=c                   óV   ‡ — e Zd ZdZ	 	 	 	 	 	 	 ddeded	ed
edededefˆ fd„Zd„ Zˆ xZ	S )ÚDropBlock2daa   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   FTr	   r
   r   r   r   r   r   c                 óþ   •— t          ¦   «                              ¦   «          || _        || _        || _        || _        || _        || _        || _        ddh}	|D ]#}
|
|	vrdd l	}| 
                    d|
› d�¦  «         Œ$d S )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               €r6   r?   zDropBlock2d.__init__t   s¬   ø€ õ 	‰Œ×ÒÑÔÐØ"ˆŒØ&ˆÔØ$ˆŒØ$ˆŒØˆŒØ.ˆÔØ*ˆÔð '¨Ð/ˆØð 	Vð 	VˆAØ˜Ð'Ð'Ø���Ø—’ÐTÐPQÐTÐTÐTÑUÔUÐUøð	Vð 	Vó    c           
      ó–   — | j         r| j        s|S t          || j        | j        | j        | j        | j        | j        | j        ¬¦  «        S )N)r	   r
   r   r   r   r   r   )	Útrainingr	   r7   r
   r   r   r   r   r   ©rB   r   s     r6   ÚforwardzDropBlock2d.forward�   sZ   € ØŒ}ð 	 D¤Nð 	ØˆHÝØØ”nØ”ØÔ(Ø”Ø”LØ Ô0ØÔ,ð	
ñ 	
ô 	
ð 		
rG   ©r   r   r   FFTT)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚintÚboolr?   rK   Ú__classcell__©rF   s   @r6   r9   r9   f   sÄ   ø€ € € € € ðð ð  #ØØ!$Ø$Ø!Ø$(Ø"&ðVð VàðVð ðVð ð	Vð
 ðVð ðVð "ðVð  ðVð Vð Vð Vð Vð Vð6
ð 
ð 
ð 
ð 
ð 
ð 
rG   r9   ç        rI   c                 óì   — |dk    s|s| S d|z
  }| j         d         fd| j        dz
  z  z   }|                      |¦  «                             |¦  «        }|dk    r|r|                     |¦  «         | |z  S )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.

    rU   r   r   )r   )r   ÚndimÚ	new_emptyr   Údiv_)r   r	   rI   r   Ú	keep_probr   Úrandom_tensors          r6   Ú	drop_pathr\   ž   s‡   € ð �B‚€˜h€ØˆØ�I‘€IØŒW�QŒZˆM˜D A¤F¨Q¡JÑ/Ñ/€EØ—K’K Ñ&Ô&×1Ò1°)Ñ<Ô<€MØ�3‚€˜=€Ø×Ò˜9Ñ%Ô%Ð%Øˆ}ÑÐrG   c                   ó:   ‡ — e Zd ZdZd	dedefˆ fd„Zd„ Zd„ Zˆ xZ	S )
ÚDropPathz^Drop paths (Stochastic Depth) per sample  (when applied in main path of residual blocks).
    rU   Tr	   r   c                 ód   •— t          ¦   «                              ¦   «          || _        || _        d S ©N)r>   r?   r	   r   )rB   r	   r   rF   s      €r6   r?   zDropPath.__init__µ   s.   ø€ Ý‰Œ×ÒÑÔÐØ"ˆŒØ*ˆÔÐÐrG   c                 óD   — t          || j        | j        | j        ¦  «        S r`   )r\   r	   rI   r   rJ   s     r6   rK   zDropPath.forwardº   s   € Ý˜˜DœN¨D¬M¸4Ô;MÑNÔNÐNrG   c                 ó4   — dt          | j        d¦  «        d›�S )Nz
drop_prob=é   z0.3f)Úroundr	   )rB   s    r6   Ú
extra_reprzDropPath.extra_repr½   s   € Ø:�E $¤.°Ñ3Ô3Ð:Ð:Ð:Ð:rG   )rU   T)
rM   rN   rO   rP   r   rR   r?   rK   re   rS   rT   s   @r6   r^   r^   ²   s{   ø€ € € € € ðð ð+ð + %ð +¸Tð +ð +ð +ð +ð +ð +ð
Oð Oð Oð;ð ;ð ;ð ;ð ;ð ;ð ;rG   r^   Údrop_path_rateÚdepthsÚ	stagewiseÚreturnc                 ón  — t          |t          ¦  «        r5|rt          d¦  «        ‚d„ t          j        d| |d¬¦  «        D ¦   «         }|S t          |¦  «        }|r7d„ t          j        d| |d¬¦  «                             |¦  «        D ¦   «         }|S d„ t          j        d| |d¬¦  «        D ¦   «         }|S )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 depthsc                 ó6   — g | ]}|                      ¦   «         ‘ŒS © ©Úitem©Ú.0r   s     r6   ú
<listcomp>z-calculate_drop_path_rates.<locals>.<listcomp>Û   s    € ÐYÐYÐY˜Aˆq�vŠv‰xŒxÐYÐYÐYrG   r   Úcpu)r   c                 ó6   — g | ]}|                      ¦   «         ‘ŒS rl   )Útolistro   s     r6   rq   z-calculate_drop_path_rates.<locals>.<listcomp>â   s    € ÐrÐrÐr !�1—8’8‘:”:ÐrÐrÐrrG   c                 ó6   — g | ]}|                      ¦   «         ‘ŒS rl   rm   ro   s     r6   rq   z-calculate_drop_path_rates.<locals>.<listcomp>æ   s    € ÐbÐbÐb �1—6’6‘8”8ÐbÐbÐbrG   )Ú
isinstancerQ   Ú
ValueErrorr   Úlinspacer(   Úsplit)rf   rg   rh   ÚdprÚtotal_depths        r6   Úcalculate_drop_path_ratesr|   Á   sÙ   € õ, �&�#ÑÔð àð 	\ÝÐZÑ[Ô[Ð[ØYÐY¥¤°°>À6ÐRWÐ!XÑ!XÔ!XÐYÑYÔYˆØˆ
õ ˜&‘k”kˆØð 	àrÐr¥u¤~°a¸ÈÐ]bÐ'cÑ'cÔ'c×'iÒ'iÐjpÑ'qÔ'qÐrÑrÔrˆCØˆJð cÐb¥U¤^°A°~À{Ð[`Ð%aÑ%aÔ%aÐbÑbÔbˆCØˆJrG   rL   )rU   FT)F)rP   Útypingr   r   r   Útorch.nnÚnnÚtorch.nn.functionalÚ
functionalr   ÚTensorr   rQ   rR   r7   ÚModuler9   r\   r^   r|   rl   rG   r6   ú<module>r„      sâ  ððð ð  Ð Ð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ð Ð Ð ð
 ØØ Ø ØØ $Ø"ðKð KØŒ<ðKàðKð ðKð ð	Kð
 ðKð ðKð ðKð ðKð Kð Kð Kð\5
ð 5
ð 5
ð 5
ð 5
�"”)ñ 5
ô 5
ð 5
ðpð ˜Eð °$ð Ètð ð ð ð ð(;ð ;ð ;ð ;ð ;ˆrŒyñ ;ô ;ð ;ð$  ð&ð &Øð&à�c˜4 œ9�nÔ%ð&ð ð&ð ˆ4�Œ;˜˜T %œ[Ô)Ð)Ô*ð	&ð &ð &ð &ð &ð &rG   