
    ^j                         d dl mZmZmZ d dlZd dlmZ dej                  dede	de
de
deej                  eej                     f   fd	Z G d
 dej                        Z G d dej                        Zy)    )OptionalTupleUnionNxprobnum_prefix_tokensorderedtrainingreturnc           	      ,   |r|dk(  r| dfS |r| ddd|f   | dd|df   } }nd}| j                   d   }| j                   d   }t        dt        |d|z
  z              }t        j                  t        j
                  ||| j                        d      ddd|f   }	|r|	j                  d      d   }	| j                  d|	j                  d      j                  d	| j                   d
d z               } |t        j                  || fd      } | |	fS )ai  
    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
             d/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/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(a0l?    c                   p     e Zd ZdZ	 	 	 d	dededef fdZdej                  dej                  fdZ
 xZS )
PatchDropoutz|
    Patch Dropout without returning indices.
    https://arxiv.org/abs/2212.00794 and https://arxiv.org/pdf/2208.07220
    r   r   r	   c                 l    t         |           d|cxk  rdk  sJ  J || _        || _        || _        y Nr   r   super__init__r   r   r	   selfr   r   r	   	__class__s       r#   r,   zPatchDropout.__init__;   ;     	D~2~~~	!2r%   r   r   c                 z    t        || j                  | j                  | j                  | j                        \  }}|S Nr$   r   r   r	   r
   )r.   r   output_s       r#   forwardzPatchDropout.forwardG   s8    )II""LLMM
	 r%   g      ?r   F)__name__
__module____qualname____doc__floatr   boolr,   r   Tensorr6   __classcell__r/   s   @r#   r'   r'   5   sO     %&!	

  #
 	
 %,, r%   r'   c                        e Zd ZdZ	 	 	 d	dededef fdZdej                  de
ej                  eej                     f   fdZ xZS )
PatchDropoutWithIndicesz
    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                 l    t         |           d|cxk  rdk  sJ  J || _        || _        || _        y r)   r*   r-   s       r#   r,   z PatchDropoutWithIndices.__init__X   r0   r%   r   r   c                 p    t        || j                  | j                  | j                  | j                        S r2   r3   )r.   r   s     r#   r6   zPatchDropoutWithIndices.forwardd   s0    $II""LLMM
 	
r%   r7   )r8   r9   r:   r;   r<   r   r=   r,   r   r>   r   r   r6   r?   r@   s   @r#   rB   rB   R   sb     %&!	

  #
 	

 
%hu||>T0T*U 
r%   rB   )typingr   r   r   r   torch.nnnnr>   r<   r   r=   r$   Moduler'   rB    r%   r#   <module>rJ      s    ) )  +<<++ + 	+
 + 5<<%,,//0+\299 :
bii 
r%   