
    RЦi                         S SK JrJrJr  S SKrS SKJr  S\R                  S\S\	S\
S\
S\\R                  \\R                     4   4S	 jr " S
 S\R                  5      r " S S\R                  5      rg)    )OptionalTupleUnionNxprobnum_prefix_tokensorderedtrainingreturnc           	      4   U(       a  US:X  a  U S4$ U(       a  U SS2SU24   U SS2US24   pOSnU R                   S   nU R                   S   n[        S[        USU-
  -  5      5      n[        R                  " [        R
                  " XgU R                  S9SS9SS2SU24   n	U(       a  U	R                  SS9S   n	U R                  SU	R                  S5      R                  S	U R                   S
S -   5      5      n Ub  [        R                  " XP4SS9n X	4$ )aA  
Common forward logic for patch dropout.

Args:
    x: Input tensor of shape (B, L, D)
    prob: Dropout probability
    num_prefix_tokens: Number of prefix tokens to preserve
    ordered: Whether to maintain patch order
    training: Whether in training mode

Returns:
    Tuple of (output tensor, keep_indices or None)
g        Nr            ?)device)dim)r   r      )shapemaxinttorchargsortrandnr   sortgather	unsqueezeexpandcat)
r   r   r   r	   r
   prefix_tokensBLnum_keepkeep_indicess
             X/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/layers/patch_dropout.pypatch_dropout_forwardr$      s   ( trz$wQ 2!2 223Qq:K:L7L5Mq	
A	
A1c!rDy/*+H==Q!((!CLQPYQYPY\ZL $((R(03	L**2.55h6LMNA II}(a0?    c                      ^  \ rS rSrSr   SS\S\S\4U 4S jjjrS\	R                  S\	R                  4S	 jrS
rU =r$ )PatchDropout5   zp
Patch Dropout without returning indices.
https://arxiv.org/abs/2212.00794 and https://arxiv.org/pdf/2208.07220
r   r   r	   c                 j   > [         TU ]  5         SUs=::  a  S:  d   e   eXl        X l        X0l        g Nr   r   super__init__r   r   r	   selfr   r   r	   	__class__s       r#   r-   PatchDropout.__init__;   5     	D~2~~~	!2r%   r   r   c                 x    [        UU R                  U R                  U R                  U R                  5      u  p#U$ Nr$   r   r   r	   r
   )r/   r   output_s       r#   forwardPatchDropout.forwardG   s6    )II""LLMM
	 r%   r   r	   r   g      ?r   F)__name__
__module____qualname____firstlineno____doc__floatr   boolr-   r   Tensorr8   __static_attributes____classcell__r0   s   @r#   r'   r'   5   sY     %&!	

  #
 	
 
 %,,  r%   r'   c                      ^  \ rS rSrSr   SS\S\S\4U 4S jjjrS\	R                  S\\	R                  \\	R                     4   4S	 jrS
rU =r$ )PatchDropoutWithIndicesR   z
Patch Dropout that returns both output and keep indices.
https://arxiv.org/abs/2212.00794 and https://arxiv.org/pdf/2208.07220
r   r   r	   c                 j   > [         TU ]  5         SUs=::  a  S:  d   e   eXl        X l        X0l        g r*   r+   r.   s       r#   r-    PatchDropoutWithIndices.__init__X   r2   r%   r   r   c                 p    [        UU R                  U R                  U R                  U R                  5      $ r4   r5   )r/   r   s     r#   r8   PatchDropoutWithIndices.forwardd   s0    $II""LLMM
 	
r%   r:   r;   )r<   r=   r>   r?   r@   rA   r   rB   r-   r   rC   r   r   r8   rD   rE   rF   s   @r#   rH   rH   R   sl     %&!	

  #
 	
 

 
%hu||>T0T*U 
 
r%   rH   )typingr   r   r   r   torch.nnnnrC   rA   r   rB   r$   Moduler'   rH    r%   r#   <module>rS      s    ) )  +<<++ + 	+
 + 5<<%,,//0+\299 :
bii 
r%   