
    kh=                        d Z ddlZddlmZmZmZmZ ddlZddlmZ ddl	m
Z
 ddlmZ 	 	 	 d3d	ed
ededeej                      fdZ	 	 	 d4d	edededeej                      dej$                  f
dZddddej(                  dfdee   dededededej*                  deej                      dej$                  fdZddddddddddej(                  dfdee   deej$                     d	edededed ed!ed"eee      d#ed$edej*                  deej                      deej$                     fd%Z G d& d'ej2                        Zd( Zd)ej$                  fd*Zd)eej$                     fd+Zd)ej$                  fd,Zd- Zdddddddddej(                  dfdee   deej$                     dedededed!ed"eee      d#ed$edej*                  deej                      fd.Z  G d/ d0ej2                        Z! G d1 d2ej2                        Z"y)5zv Sin-cos, fourier, rotary position embedding modules and functions

Hacked together by / Copyright 2022 Ross Wightman
    N)ListTupleOptionalUnion)nn   )ndgrid)_assertT	num_bandsmax_freqlinear_bandsdevicec                    |r,t        j                  d|dz  | t         j                  |      }nBdt        j                  dt        j                  |d      dz
  | t         j                  |      z  }|t         j
                  z  S )N      ?   dtyper   r   r   )torchlinspacefloat32mathlogpi)r   r   r   r   bandss        X/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/layers/pos_embed_sincos.pypixel_freq_bandsr      sh     sHqL)5==Y_`U^^Atxx!'<q'@)SXS`S`iopp588         @temperaturestepreturnc                     t        j                  d| |t         j                  |      j                  t         j                        | z  }d||z  z  }|S )Nr   r   r   )r   arangeint64tor   )r   r   r    r   expr   s         r   
freq_bandsr'      sJ     ,,q)TV
L
O
OPUP]P]
^aj
jC+$%ELr   @   F
feat_shapedimreverse_coordinterleave_sin_cosr   c                    |dz  dk(  sJ d       |dz  }t        ||d|      }|r| ddd   } t        j                  t        | D 	cg c]E  }	t        j                  |	|t        j
                        j                  t        j                        G c}	            j                  d      j                  dd      }
|
j                  d      |j                  d      z  }|rd	nd}t        j                  t        j                  |      t        j                  |      g|
      j                  d      }|j                  |      S c c}	w )a  

    Args:
        feat_shape:
        dim:
        temperature:
        reverse_coord: stack grid order W, H instead of H, W
        interleave_sin_cos: sin, cos, sin, cos stack instead of sin, sin, cos, cos
        dtype:
        device:

    Returns:

       r   zHEmbed dimension must be divisible by 4 for sin-cos 2D position embeddingr   r   r    r   Nr   r   r   r*   r   )r'   r   stackr	   r#   r$   r%   r   flatten	transpose	unsqueezesincos)r)   r*   r   r+   r,   r   r   pos_dimr   sgridpos2	stack_dimpos_embs                 r   build_sincos2d_pos_embedr@   '   s   . 7a<ccc<QhGwKaOE"%
;;v 	QvU[[9<<U]]K   	
99Q? 	 >>" 22D (QIkk599T?EIIdO<)LTTUVWG::E:""s   A
E           ijr   max_resinclude_grid	in_pixelsref_feat_shapegrid_offsetgrid_indexingc           
      @   |+|rt        |t        |      ||      }n,t        ||d|      }n||j                  }||j                  }|r6| D cg c]*  }t        j                  dd||t
        j                        , }}nS| D cg c]H  }t        j                  ||t
        j                        j                  t
        j                        |	z   J }}|(t        || |      D cg c]  \  }}}||z  |z   }}}}t        j                  t        j                  ||
      d	
      }|j                  d	      }||z  }|j                         j                  |      |j!                         j                  |      }}|r|||g}|S ||g}|S c c}w c c}w c c}}}w )a  

    Args:
        feat_shape: Feature shape for embedding.
        bands: Pre-calculated frequency bands.
        num_bands: Number of frequency bands (determines output dim).
        max_res: Maximum resolution for pixel based freq.
        temperature: Temperature for non-pixel freq.
        linear_bands: Linear band spacing for pixel based freq.
        include_grid: Include the spatial grid in output.
        in_pixels: Output in pixel freq.
        ref_feat_shape: Reference feature shape for resize / fine-tune.
        grid_offset: Constant offset to add to grid for non-pixel freq.
        grid_indexing: Indexing mode for meshgrid ('ij' or 'xy')
        dtype: Output dtype.
        device: Output device.

    Returns:

    )r   r   r   r/   g      r   )stepsr   r   r1   )indexingr0   r2   r3   )r   floatr'   r   r   r   r   r   r#   r$   r%   zipr4   meshgridr7   r8   r9   )r)   r   r   rD   r   r   rE   rF   rG   rH   rI   r   r   r;   txfrr<   pospos_sinpos_cosouts                          r   build_fourier_pos_embedrX   P   s   F }$g)	E '	E >\\F=KKE  
 NN3!F%--P
 
  
 LL6=@@OR]]
 

 !&)!Z&HII71aQUQYII;;u~~a-@bID>>"D
,Cwwy||%|0#''),,u2EWG&24'
"CJ :A'8JCJ)


 Js   /FAF(Fc                   8     e Zd Z	 	 	 	 ddedef fdZd Z xZS )FourierEmbedrD   r   c                     t         |           || _        || _        || _        || _        | j                  dt        ||      d       y )Nr   F
persistent)super__init__rD   r   concat_gridkeep_spatialregister_bufferr   )selfrD   r   r`   ra   	__class__s        r   r_   zFourierEmbed.__init__   sQ     	"&(Wi0 	 	
r   c           	         |j                   d d \  }}|j                   dd  }t        || j                  | j                  |j                  |j
                        }t        j                  |d      }|j                  dd      j                  t        |            }|fd|j                  dz
  z  z   }| j                  rKt        j                  ||j                  d      j                  |      j                  dd	dd      gd      }|S t        j                  |j                  ddd	d      |j                  d      j                  |      gd      }|j!                  ||j#                         d      }|S )
Nr   )rE   r   r   r0   r2   )r0   r   r      )shaperX   r   r`   r   r   r   catr6   r5   lenndimra   r7   expandpermutereshapenumel)rc   rQ   BCr)   embbatch_expands          r   forwardzFourierEmbed.forward   sC   wwr{1WWQR[
%JJ))''88
 ii$mmB#++C
O<teqvvz22 		1cmmA.55lCKKAqRSUVWX^_`A
  		199Q1a0#--2B2I2I,2WX^`aA		!Z--/4Ar   )rA   r(   TF)__name__
__module____qualname__intr_   rt   __classcell__rd   s   @r   rZ   rZ      s-     

 
$r   rZ   c                     t        j                  | ddd df    | dd d df   gd      j                  | j                        S )N.r   r   r0   )r   r4   rn   rh   )rQ   s    r   rotr|      sC    ;;319qcc{3R8@@IIr   rQ   c                     |j                   dk(  rP| |j                  d      j                  |       z  t        |       |j                  d      j                  |       z  z   S | |z  t        |       |z  z   S )Nrg   r   )rk   r7   	expand_asr|   )rQ   sin_embcos_embs      r   apply_rot_embedr      sm    ||q7$$Q'11!44s1v@Q@QRS@T@^@^_`@a7aaaw;Q')))r   c                     t        | t        j                        r| g} | D cg c]  }||z  t        |      |z  z    c}S c c}w N)
isinstancer   Tensorr|   )rQ   r   r   rP   s       r   apply_rot_embed_listr      s=    !U\\"C456qAK#a&7**666s   A c                    |j                  dd      \  }}|j                  dk(  rP| |j                  d      j                  |       z  t	        |       |j                  d      j                  |       z  z   S | |z  t	        |       |z  z   S )Nr   r0   rg   r   )tensor_splitrk   r7   r~   r|   )rQ   rr   r   r   s       r   apply_rot_embed_catr      s    ''2.GW||q7$$Q'11!44s1v@Q@QRS@T@^@^_`@a7aaaw;Q')))r   c           	          |j                  d      j                  | j                  d   dd      }|j                  d|j                  d      j                  dd|j                  d               }|S )Nr   r0   r   )r7   rl   rh   gather)rQ   	pos_embedkeep_indicess      r   apply_keep_indices_nlcr      se    ##A&--aggaj"bAI  L$:$:2$>$E$Eb"ioo^`Na$bcIr   c                     t        | ||dz  |||||||	||
      \  }}d}| D ]  }||z  }	 |j                  |d      j                  dd      }|j                  |d      j                  dd      }||fS )a  

    Args:
        feat_shape: Spatial shape of the target tensor for embedding.
        bands: Optional pre-generated frequency bands
        dim: Output dimension of embedding tensor.
        max_res: Maximum resolution for pixel mode.
        temperature: Temperature (inv freq) for non-pixel mode
        linear_bands: Linearly (instead of log) spaced bands for pixel mode
        in_pixels: Pixel vs language (inv freq) mode.
        ref_feat_shape: Reference feature shape for resize / fine-tune.
        grid_offset: Constant offset to add to grid for non-pixel freq.
        grid_indexing: Indexing mode for meshgrid ('ij' or 'xy')
        dtype: Output dtype.
        device: Output device.

    Returns:

    r.   )r   r   rD   r   r   rF   rG   rH   rI   r   r   r   r0   r   )rX   rn   repeat_interleave)r)   r   r*   rD   r   r   rF   rG   rH   rI   r   r   r   r   num_spatial_dimrQ   s                   r   build_rotary_pos_embedr      s    B /(!%#GW O 1ooor2DDQKGooor2DDQKGGr   c                        e Zd ZdZ	 	 	 	 	 	 	 	 ddedeee      deee      dede	f
 fdZ
ddeee      fd	Zd
 Z xZS )RotaryEmbeddinga   Rotary position embedding

    NOTE: This is my initial attempt at impl rotary embedding for spatial use, it has not
    been well tested, and will likely change. It will be moved to its own file.

    The following impl/resources were referenced for this impl:
    * https://github.com/lucidrains/vit-pytorch/blob/6f3a5fcf0bca1c5ec33a35ef48d97213709df4ba/vit_pytorch/rvt.py
    * https://blog.eleuther.ai/rotary-embeddings/
    r   r)   rG   rH   rI   c
           
         t         |           || _        || _        || _        || _        || _        || _        || _        |	| _	        |Q|rt        |dz  t        |      |      }
nt        |dz  |d      }
| j                  d|
d       d | _        d | _        y t!        |||||| j                  | j                  | j                        \  }}d | _        | j                  d	|d       | j                  d
|d       y )Nr.   r   r   r   r    r   Fr\   r)   r*   rD   r   rF   rG   rH   rI   pos_embed_sinpos_embed_cos)r^   r_   r*   rD   r   rF   r)   rG   rH   rI   r   rM   r'   rb   r   r   r   r   )rc   r*   rD   r   rF   r   r)   rG   rH   rI   r   emb_sinemb_cosrd   s                r   r_   zRotaryEmbedding.__init__,  s7    	&"$,&*(1H'N!- #1H +
     ! 
 "&D!%D  6%)##22 ,,"00	 GW DJ    ! 
     ! r   rh   c                     | j                   G|J t        || j                   | j                  | j                  | j                  | j
                        S | j                  | j                  fS )NrF   rG   rH   rI   )r   r   rF   rG   rH   rI   r   r   )rc   rh   s     r   	get_embedzRotaryEmbedding.get_embedo  sj    ::!$$$)

..#22 ,,"00  %%t'9'999r   c                 ^    | j                  |j                  dd        \  }}t        |||      S Nr   )r   rh   r   )rc   rQ   r   r   s       r   rt   zRotaryEmbedding.forward~  s.    >>!''!"+6q'733r   rA   i'  TFNNrB   rC   r   ru   rv   rw   __doc__boolr   r   rx   rM   strr_   r   rt   ry   rz   s   @r   r   r   !  s     !&.226!#!%A A !c+A %T#Y/A A AF:xS	2 :4r   r   c                        e Zd ZdZ	 	 	 	 	 	 	 	 ddedeee      deee      dede	f
 fdZ
ddeee      fd	Zd
 Z xZS )RotaryEmbeddingCata   Rotary position embedding w/ concatenatd sin & cos

    The following impl/resources were referenced for this impl:
    * https://github.com/lucidrains/vit-pytorch/blob/6f3a5fcf0bca1c5ec33a35ef48d97213709df4ba/vit_pytorch/rvt.py
    * https://blog.eleuther.ai/rotary-embeddings/
    r   r)   rG   rH   rI   c
           
         t         |           || _        || _        || _        || _        || _        || _        || _        |	| _	        |J|rt        |dz  t        |      |      }
nt        |dz  |d      }
| j                  d|
d       d | _        y t        |||||| j                  | j                  | j                        }d | _        | j                  d	t#        j$                  |d
      d       y )Nr.   r   r   r   r   Fr\   r   r   r0   )r^   r_   r*   rD   r   rF   r)   rG   rH   rI   r   rM   r'   rb   r   r   r   r   ri   )rc   r*   rD   r   rF   r   r)   rG   rH   rI   r   embedsrd   s               r   r_   zRotaryEmbeddingCat.__init__  s    	&"$,&*(1H'N!- #1H +
     ! 
 "DN ,%)##22 ,,"00	F DJ  		&"%  ! r   rh   c                    | j                   [|Yt        || j                   | j                  | j                  | j                  | j
                        }t        j                  |d      S | j                  | j                  S J d       )Nr   r0   zPget_embed() requires pre-computed pos_embed or valid shape w/ pre-computed bands)	r   r   rF   rG   rH   rI   r   ri   r   )rc   rh   r   s      r   r   zRotaryEmbeddingCat.get_embed  sy    ::!e&7+

..#22 ,,"00F 99VR((^^'>>!lll5r   c                 V    | j                  |j                  dd        }t        ||      S r   )r   rh   r   )rc   rQ   r   s      r   rt   zRotaryEmbeddingCat.forward  s'    NN17712;/	"1i00r   r   r   r   rz   s   @r   r   r     s     !&.226!#!%; ; !c+; %T#Y/; ; ;zmxS	2 m"1r   r   )g      l@TN)r   r   N)#r   r   typingr   r   r   r   r   r   r<   r	   trace_utilsr
   rx   rM   r   r   r   r   r'   r   r   r@   r   rX   ModulerZ   r|   r   r   r   r   r   r   r    r   r   <module>r      sU    / /     
 !)-	


 
 &	
 $)-	  &	
 \\ ###("]])-&#I&#&# &# 	&#
 !&# {{&# &&# \\&#V )-#"".2!"]])-MIM%M M 	M
 M M M M !c+M M M {{M &M 
%,,M`)299 )XJ*u|| *7D. 7*5<< * )-#".2!"]])-5I5%5 5 	5
 5 5 5 !c+5 5 5 {{5 &5p`4bii `4FY1 Y1r   