
    RЦi_                        S r SSKrSSKrSSKJrJrJrJrJrJ	r	  SSK
r
SSK
Jr  SSKJs  Jr  SSKJrJr  SSKJr  SSKJr  \R,                  " \5      r " S	 S
\R2                  5      r " S S\5      r   S"S\\   S\S\S\4S jjr\
R@                  r!\!4S\\\4   S\\\4   S\S\S\
RD                  S\
RF                  S\
RH                  4S jjr%\!4S\
RH                  S\
RH                  S\\\4   S\
RF                  S\
RF                  S\
RH                  4S jjr&   S"S\
RH                  S\\   S\S\S\4
S jjr' " S S\R2                  5      r( " S  S!\R2                  5      r)g)#a>  Image to Patch Embedding using Conv2d

A convolution based approach to patchifying a 2D image w/ embedding projection.

Based on code in:
  * https://github.com/google-research/vision_transformer
  * https://github.com/google-research/big_vision/tree/main/big_vision

Hacked together by / Copyright 2020 Ross Wightman
    N)CallableDictListOptionalTupleUnion)nn   )Formatnchw_to)	to_2tuple)_assertc                     ^  \ rS rSr% Sr\\S'   \R                  R                  \
   \S'               SS\\\\\\4   4      S\S\S\S	\\   S
\
S\\   S\
S\
S\
4U 4S jjjrS\\\\\4   4   4S jr  SS\\\\\\4   4      S\\\\\\4   4      4S jjrSS\\\\4   \4   4S jjrS\\\4   S\\\4   4S jrS rSrU =r$ )
PatchEmbed    2D Image to Patch Embedding
    
output_fmtdynamic_img_padimg_size
patch_sizein_chans	embed_dim
norm_layerflattenbiasstrict_img_sizec                   > XS.n[         TU ]  5         [        U5      U l        U R	                  U5      u  U l        U l        U l        Ub  SU l        [        U5      U l
        OX`l        [        R                  U l
        Xl        Xl        [        R                  " X44X"US.UD6U l        U(       a  U" U40 UD6U l        g [        R"                  " 5       U l        g )NdevicedtypeF)kernel_sizestrider   )super__init__r   r   _init_img_sizer   	grid_sizenum_patchesr   r   r   NCHWr   r   r	   Conv2dprojIdentitynorm)selfr   r   r   r   r   r   r   r   r   r   r   r    dd	__class__s                 V/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/layers/patch_embed.pyr$   PatchEmbed.__init__    s     /#J/:>:M:Mh:W7t~t'7! DL$Z0DO #L$kkDO..IIhnzcgnkmn	3=Jy/B/	2;;=	    c                     U R                   (       d   eUc  g[        U5      n[        [        XR                   5       VVs/ s H	  u  p#X#-  PM     snn5      nUS   US   -  nXU4$ s  snnf )N)NNNr   r
   )r   r   tuplezip)r-   r   spr&   r'   s         r0   r%   PatchEmbed._init_img_sizeA   sl    #X&c(OO.LM.Lda16.LMN	lYq\1K// Ns    A'
c                 ~   S nUb  [        U5      nUGbL  X0R                  :w  Ga<  [        R                  " 5          [        R
                  " U R                  R                  U R                  R                  UUU R                  R                  S LU R                  R                  R                  U R                  R                  R                  S9nUR                  R                  [        U R                  R                  USS95        U R                  R                  b/  UR                  R                  U R                  R                  5        X@l        S S S 5        X0l        U=(       d    U R                  nXR                  :w  d  Ub%  U R!                  U5      u  U l        U l        U l        g g ! , (       d  f       Na= f)N)r!   r"   r   r   r    T)verbose)r   r   torchno_gradr	   r)   r*   in_channelsout_channelsr   weightr   r    copy_resample_patch_embedr   r%   r&   r'   )r-   r   r   new_patch_sizenew_projs        r0   set_input_sizePatchEmbed.set_input_sizeJ   sA   
 !&z2N%.OO*K99II))II** .)t399++22))**00 %%&:499;K;K^ei&jk99>>-MM''		7$	 ! -O,t}}}}$(B>B>Q>QRZ>[;DM4>4+; )C! !s   DF..
F<returnc                 R    U(       a  [        U R                  5      $ U R                  $ N)maxr   )r-   	as_scalars     r0   
feat_ratioPatchEmbed.feat_ratiof   s    t''??"r2   c                    U R                   (       aR  [        R                  " US   U R                  S   -  5      [        R                  " US   U R                  S   -  5      4$ US   U R                  S   -  US   U R                  S   -  4$ )zGet grid (feature) size for given image size taking account of dynamic padding.
NOTE: must be torchscript compatible so using fixed tuple indexing
r   r
   )r   mathceilr   )r-   r   s     r0   dynamic_feat_sizePatchEmbed.dynamic_feat_sizel   s     99Xa[4??1+==>		(ST+X\XgXghiXjJj@kkkA;$//!"44hqkT__UVEW6WWWr2   c                 4   UR                   u  p#pEU R                  b  U R                  (       a_  [        X@R                  S   :H  SU SU R                  S    S35        [        XPR                  S   :H  SU SU R                  S    S35        OuU R                  (       dd  [        X@R
                  S   -  S:H  SU SU R
                  S    S35        [        XPR
                  S   -  S:H  SU SU R
                  S    S35        U R                  (       a}  U R
                  S   X@R
                  S   -  -
  U R
                  S   -  nU R
                  S   XPR
                  S   -  -
  U R
                  S   -  n[        R                  " USUSU45      nU R                  U5      nU R                  (       a"  UR                  S5      R                  SS5      nO3U R                  [        R                  :w  a  [        XR                  5      nU R                  U5      nU$ )	Nr   zInput height (z) doesn't match model ().r
   zInput width (z%) should be divisible by patch size (   )shaper   r   r   r   r   Fpadr*   r   	transposer   r   r(   r   r,   )r-   xBCHWpad_hpad_ws           r0   forwardPatchEmbed.forwardu   s   WW
a==$##]]1--sBYZ^ZgZghiZjYkkm/no]]1--qcAXY]YfYfghYiXjjl/mn))**a/$QC'LT__]^M_L``bc **a/#A3&KDOO\]L^K__ab __Q'!ooa.@*@@DOOTUDVVE__Q'!ooa.@*@@DOOTUDVVEa!UAu-.AIIaL<<		!&&q!,A__+??+AIIaLr2   )
r   r   r&   r   r,   r'   r   r   r*   r   )            NTNTTFNN)NN)T)__name__
__module____qualname____firstlineno____doc__r   __annotations__r;   jitFinalboolr   r   intr   r   strr$   r%   rD   rK   rP   r`   __static_attributes____classcell__r/   s   @r0   r   r      s   YY__T** ?B  -1 (,$($)QuS%S/%9:;Q Q 	Q
 Q !*Q Q !Q Q "Q "Q QB0uS%S/-A'B 0 ?C@D\uS%S/%9:;\ !sE#s(O';!<=\8#E%S/32F,G #X%S/ XeCHo X r2   r   c                      ^  \ rS rSr% Sr\\S'             SS\\\	\
\	\	4   4      S\	S\	S\	S\\   S	\S\\   S
\4U 4S jjjrS\
\R                   \\	   4   4S jrSrU =r$ )PatchEmbedWithSize   r   r   r   r   r   r   r   r   r   c                 2   > [         TU ]  UUUUUUUUU	U
S9
  g )N)
r   r   r   r   r   r   r   r   r   r    )r#   r$   )r-   r   r   r   r   r   r   r   r   r   r    r/   s              r0   r$   PatchEmbedWithSize.__init__   s5     	!!! 	 	
r2   rF   c                 6   UR                   u  p#pEU R                  bd  [        X@R                  S   -  S:H  SU SU R                  S    S35        [        XPR                  S   -  S:H  SU SU R                  S    S35        U R	                  U5      nUR                   SS  nU R
                  (       a"  UR                  S5      R                  SS5      nO3U R                  [        R                  :w  a  [        XR                  5      nU R                  U5      nX4$ )	Nr   zInput image height (z#) must be divisible by patch size (rS   r
   zInput image width (rT   )rU   r   r   r   r*   r   rX   r   r   r(   r   r,   )r-   rY   rZ   r[   r\   r]   	feat_sizes          r0   r`   PatchEmbedWithSize.forward   s   WW
a==$A**a/3GsJmnrn}n}~  oA  nB  BD  2E  FA**a/3FqcIlmqm|m|}~m  mA  AC  2D  EIIaLGGBCL	<<		!&&q!,A__+??+AIIaL|r2    )
rb   rc   rd   re   NTNTNN)rf   rg   rh   ri   rj   r   rk   r   r   ro   r   r   rn   rp   r$   r;   Tensorr   r`   rq   rr   rs   s   @r0   ru   ru      s     ?B  -1 (,
uS%S/%9:;
 
 	

 
 !*
 
 !
 
 
4E%,,S	"9:  r2   ru   new_sizeinterpolation	antialiasr:   c           	        ^^^^^^ SSK m SSKJn  [        U R                  5      S:X  d   S5       e[        T5      S:X  d   S5       eU R                  SS n[        U5      [        T5      :X  a  U $ U(       a)  [        R                  S	U R                   S
T ST S35        UU4S jmUU4S jnU" UT5      n[        R                  " TR                  R                  UR                  5      U R                  S9mUU4S jn	U" U" U	SS5      SS5      n
U R                  nU R!                  5       n U
" U 5      n U R#                  U5      n U $ ! [         a
    SSKJn   GNKf = f)a  Resample the weights of the patch embedding kernel to target resolution.
We resample the patch embedding kernel by approximately inverting the effect
of patch resizing.

Code based on:
  https://github.com/google-research/big_vision/blob/b00544b81f8694488d5f36295aeb7972f3755ffe/big_vision/models/proj/flexi/vit.py

With this resizing, we can for example load a B/8 filter into a B/16 model
and, on 2x larger input image, the result will match.

Args:
    patch_embed: original parameter to be resized.
    new_size (tuple(int, int): target shape (height, width)-only.
    interpolation (str): interpolation for resize
    antialias (bool): use anti-aliasing filter in resize
    verbose (bool): log operation
Returns:
    Resized patch embedding kernel.
r   N)vmap   zFour dimensions expectedrT   zNew shape should only be hwrz   zResize patch embedding z to z, w/ z interpolation.c                    > [         R                  " U 5      S   n[        R                  " X!TTS9S   R	                  5       nU$ )N)NN.)sizemoder   )r   r   .)r;   r~   rV   interpolatenumpy)x_np	_new_sizex_tfx_upsampledr   r   s       r0   resize(resample_patch_embed_old.<locals>.resize   sG    ||D!/2mm}	KKTVV[V[V] 	r2   c                   > / n[        TR                  U 5      5       HN  nTR                  U 5      nSUTR                  X05      '   UR	                  T" XA5      R                  S5      5        MP     TR                  U5      R                  $ )Ng      ?)rangeprodzerosunravel_indexappendreshapestackT)	_old_sizer   mati	basis_vecnpr   s        r0   get_resize_mat0resample_patch_embed_old.<locals>.get_resize_mat   ss    rwwy)*A+I8:Ib&&q45JJvi3;;B?@ + xx}r2   )r   c                 N   > TU R                  S5      -  nUR                  T5      $ )Nr   )r   )kernelresampled_kernelr   resize_mat_pinvs     r0   resample_kernel1resample_patch_embed_old.<locals>.resample_kernel   s(    *V^^B-??''11r2   r
   )r   r;   r   ImportError	functorchlenrU   r4   _loggerinfotensorlinalgpinvr   r   r    floatto)patch_embedr   r   r   r:   r   old_sizer   
resize_matr   v_resample_kernel
orig_dtyper   r   r   s    ```        @@@r0   resample_patch_embed_oldr      sM   4 # {  !Q&B(BB&x=A<<<  %HX%/).{/@/@.AhZuUbTccrst  (3Jll299>>*,,#?HZHZ[O2 T/1a8!Q?""J##%K#K0K..,KM  #""#s   E
 
EEr   r   r    rF   c                    U u  pgUu  pXg-  n
X-  n[         R                  " XUS9nUR                  U
SXg5      n[        R                  " UUUUSS9nUR                  S5      R                  SSS5      R                  X5      nU$ )zKComputes the resize matrix basis vectors and interpolates them to new_size.r   r
   F)r   r   r   align_cornersrT   r   )r;   eyer   rV   r   squeezepermute)r   r   r   r   r   r    old_hold_wnew_hnew_w	old_total	new_total
eye_matrixbasis_vectors_batchresized_basis_vectors_batchresize_matrixs                   r0   _compute_resize_matrixr   
  s     LELEII95AJ$,,Y5H"#--# 077:BB1aKSST]iMr2   r   pinv_matrixnew_size_tupler   intermediate_dtypec                     U R                   tpVnU R                  XVS5      R                  US9n UR                  US9nX-  nUR                  " XV/UQ76 R                  US9nU$ )zNSimplified resampling w/o vmap use.
As proposed by https://github.com/stas-sl
r   )r    )rU   r   r   )	r   r   r   r   r   c_outc_in_resampled_patch_embeds	            r0   _apply_resamplingr   %  sy     "''OE!%%e2699@R9SK..'9.:K'5199%WWZZakZl  r2   c                 z   [        U R                  5      S:X  d   S5       e[        U5      S:X  d   S5       e[        U R                  SS 5      n[        U5      nXV:X  a  U $ U R                  nU R                  n[        XVX#U[        5      n	[        R                  R                  U	5      n
[        X
Xh[        5      nU$ )z4Standalone function (computes matrix on each call). r   z/Input tensor should be 4D (out_ch, in_ch, h, w)rT   z+New shape should only be hw (height, width)rz   N)r   rU   r4   r   r    r   DTYPE_INTERMEDIATEr;   r   r   r   )r   r   r   r   r:   old_size_tupler   r   r   r   r   r   s               r0   rA   rA   7  s     {  !Q&Y(YY&x=ALLL&+K,=,=bc,B&CN&+HoN'F""J'&J\J ,,##J/K-.>P ! r2   c            	          ^  \ rS rSrSr  SS\\\4   S\S\4U 4S jjjr	\
4S\\\4   S\R                  S	\R                  S
\R                  4S jjrS\R                  S\\   S
\R                  4S jrSrU =r$ ) PatchEmbedResamplerFixedOrigSizeiU  zy
Resample patch embedding weights from a fixed original size,
caching the pseudoinverse matrix based on the target size.
	orig_sizer   r   c                    > [         TU ]  5         [        U[        5      (       a  [	        U5      S:X  d   S5       eXl        X l        X0l        0 U l        g)z
Args:
    orig_size (Tuple[int, int]): The expected original (height, width) of input patch_embed tensors.
    interpolation (str): Interpolation mode.
    antialias (bool): Use anti-aliasing filter in resize.
rT   z.`orig_size` must be a tuple of (height, width)N)	r#   r$   
isinstancer4   r   r   r   r   _pinv_cache_map)r-   r   r   r   r/   s       r0   r$   )PatchEmbedResamplerFixedOrigSize.__init__Z  sP     	)U++I!0C 	=<	=C"*";=r2   r   r   r    rF   c                    UnU R                   R                  U5      nU(       a=  [        X5      (       a-  [        X5      nUR                  U:X  a  UR
                  U:X  a  U$ [        U R                  XR                  U R                  X#5      n[        R                  R                  U5      nSUS    SUS    3n[        X5      (       a  [        X5        U R                  XV5        XPR                   U'   U$ )zRRetrieves the cached pinv matrix or computes and caches it for the given new_size.pinv_r   rY   r
   )r   gethasattrgetattrr   r    r   r   r   r   r;   r   r   delattrregister_buffer)r-   r   r   r    	cache_keybuffer_namer   r   s           r0   _get_or_create_pinv_matrix;PatchEmbedResamplerFixedOrigSize._get_or_create_pinv_matrixo  s     	**..y97455!$4K!!V+0A0AU0J## ,NNH&8&8$..&

 ll''
3 hqk]!HQK=94%%T'[6*5Y'r2   r   c                 ~   [        UR                  5      S:X  d   e[        U5      S:X  d   e[        UR                  SS 5      nX0R                  :X  d   SU SU R                   35       e[        U5      nU R                  U:X  a  U$ UR                  nUR
                  nU R                  XE5      n[        XXF5      nU$ )zResamples the patch embedding weights to new_size.

Args:
    patch_embed (torch.Tensor): Original weights (out_ch, in_ch, H_orig, W_orig).
    new_size (List[int]): Target [height, width].

Returns:
    torch.Tensor: Resampled weights.
r   rT   rz   NzInput patch_embed spatial size z0 does not match module's expected original size )r   rU   r4   r   r   r    r   r   )	r-   r   r   
input_sizer   r   r   r   r   s	            r0   r`   (PatchEmbedResamplerFixedOrigSize.forward  s     ;$$%***8}!!! ;,,RS12
^^+ 	@-j\ ://3~~.>@	@+ +0/ >>^+## &&
 55nM !2+N g$$r2   )r   r   r   r   )bicubicT)rf   rg   rh   ri   rj   r   ro   rp   rn   r$   r   r;   r   r    r~   r   r   r`   rq   rr   rs   s   @r0   r   r   U  s     "+"	>S#X> > 	> >2 "4	CHo LL ;;	
 
<"%5<< "%49 "% "% "%r2   r   c                   |  ^  \ rS rSrSr    SS\\\4   S\S\S\S\4
U 4S jjjr	S	\
R                  S
\\\4   S\
R                  4S jrS	\
R                  S
\\\4   S\
R                  4S jr   SS\
R                  S\
R                  S\\
R                     S\\\\4      S\S\
R                  4S jjrSrU =r$ )PatchEmbedInterpolatori  a  Dynamically interpolates patch embedding weights for variable patch sizes.

This module wraps patch embedding weight resampling functionality to support
on-the-fly patch size variation during training. It handles both Conv2d and
Linear patch embeddings.

Args:
    base_patch_size: The original patch size the model was initialized with
    in_chans: Number of input channels
    embed_dim: Embedding dimension
    interpolation: Interpolation mode for resampling
    antialias: Whether to use antialiasing during interpolation
base_patch_sizer   r   r   r   c                 ^   > [         TU ]  5         Xl        X l        X0l        X@l        XPl        g rH   )r#   r$   r   r   r   r   r   )r-   r   r   r   r   r   r/   s         r0   r$   PatchEmbedInterpolator.__init__  s+     	. "*"r2   r?   target_patch_sizerF   c                 Z   X R                   :X  a  U$ UR                  S   nU R                   u  pEUu  pgUR                  X4XPR                  5      nUR	                  SSSS5      n[        UXg/U R                  U R                  SS9n	U	R	                  SSSS5      n
U
R                  US5      n
U
$ )zResample linear patch embedding weights for a new patch size.

Args:
    weight: Linear weight tensor of shape [embed_dim, patch_h * patch_w * in_chans]
    target_patch_size: Target (patch_h, patch_w) to resample to

Returns:
    Resampled weight tensor
r   rd   r
   rT   Fr   r   r   r:   r   )r   rU   r   r   r   rA   r   r   )r-   r?   r   r   base_phbase_pw	target_ph	target_pwweight_convweight_conv_resampledweight_resampleds              r0   resample_linear_weight-PatchEmbedInterpolator.resample_linear_weight  s      4 44MLLO	//0	 nnY--P!))!Q15 !5+,,nn!
 188Aq!D+33IrBr2   c                 |    X R                   :X  a  U$ [        U[        U5      U R                  U R                  SS9nU$ )zResample conv2d patch embedding weights for a new patch size.

Args:
    weight: Conv2d weight tensor of shape [embed_dim, in_chans, patch_h, patch_w]
    target_patch_size: Target (patch_h, patch_w) to resample to

Returns:
    Resampled weight tensor
Fr   )r   rA   listr   r   )r-   r?   r   r   s       r0   resample_conv_weight+PatchEmbedInterpolator.resample_conv_weight  sI      4 44M 0+,,,nn
  r2   patchesproj_weight	proj_biasr   	is_linearc                    Uc  U R                   nU(       a  X@R                   :w  av  UR                  S:X  d   S5       eUR                  u  pgpn
U R                  X$5      nUR	                  XgS5      n[
        R                  R                  R                  XU5      nU$ UR                  S:X  a"  UR                  u  pgpn
UR	                  XgS5      n[
        R                  R                  R                  XU5      n U$ X@R                   :w  a=  U R                  X$5      n[
        R                  R                  R                  XUUSS9nU$ [
        R                  R                  R                  XUUSS9nU$ )a  Apply patch embedding with dynamic weight resampling.

Args:
    patches: Input patches
        - For linear mode with resampling: [B, N, Ph, Pw, C]
        - For linear mode without resampling: [B, N, Ph*Pw*C]
        - For conv mode: [B, C, H, W]
    proj_weight: Original projection weight
    proj_bias: Optional projection bias
    patch_size: Current patch size (if None, uses base_patch_size)
    is_linear: Whether using linear (True) or conv2d (False) projection

Returns:
    Embedded patches
   z0Patches must be [B, N, Ph, Pw, C] for resamplingr   r   )r"   padding)r   ndimrU   r   r   r;   r	   
functionallinearr   conv2d)r-   r   r   r   r   r  rZ   NPhPwr[   r   patches_flatoutputs                 r0   r`   PatchEmbedInterpolator.forward  se   . --J111||q(\*\\(")--ba $(#>#>{#W   'qR8,,33LT]^* % <<1$&-mmOA"!%ooaB7G,,33G)T  111#'#<#<[#U ,,33y%q 4   ,,33)%q 4 
 r2   )r   r   r   r   r   )rd   re   r   T)NNT)rf   rg   rh   ri   rj   r   ro   rp   rn   r$   r;   r~   r   r   r   r`   rq   rr   rs   s   @r0   r   r     s-   "  !*"#"38_# # 	#
 # # #( LL(   %S#X(  
	( T LL   %S#X  
	 @ 1548":\\: :  -	:
 !sCx1: : 
: :r2   r   )r   TF)*rj   loggingrN   typingr   r   r   r   r   r   r;   r	   torch.nn.functionalr  rV   formatr   r   helpersr   trace_utilsr   	getLoggerrf   r   Moduler   ru   ro   rp   rn   r   float32r   r   r    r~   r   r   rA   r   r   r}   r2   r0   <module>r     s  	   ? ?     #   


H
%t tn, ,f 'Cs)C C 	C
 CL ]]  0S/S/  	
  {{ \\@ +=!\\!\\! c3h! KK	!
 "KK! \\!* '!\\!s)! ! 	!
 !<Z%ryy Z%z^RYY ^r2   