
    iE3                        S SK r S SKJr  S SKJrJrJr  S SKrS SKJ	r	  S\R                  S\S\4S jr     S"S	\S
\S\S\S\\   S\S\\\R                   4   4S jjrS\R                  S\R                  4S jrS\R                  S\R                  S\R                  S\R                  S\\R                  \R                  4   4
S jrS\R                  S\R                  S\R                  S\R                  4S jr " S S\R*                  R,                  5      r  S#S	\S
\S\S\S\\\R                   4   4
S jjrS rS$S jrS$S jr " S  S!\R*                  R,                  5      rg)%    N)partial)OptionalTupleUnion)	ModelArgsfreqsscale_factorhigh_freq_factorc                 v   SnSnXC-  nXB-  n/ nU  H}  nS[         R                  -  U-  n	X:  a  UR                  U5        M1  X:  a  UR                  X-  5        MK  XV:w  d   eXI-  U-
  X#-
  -  n
UR                  SU
-
  U-  U-  X-  -   5        M     [        R                  " XpR
                  U R                  S9$ )N   i       )dtypedevice)mathpiappendtorchtensorr   r   )r   r	   r
   low_freq_factorold_context_lenlow_freq_wavelenhigh_freq_wavelen	new_freqsfreqwavelensmooths              d/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/examples/models/llama/rope.pyapply_scalingr      s    OO&8':Idgg+$&T"'T01#888%//A 2F a&jD0<?&-OP  <<	U\\JJ    dimendtheta
use_scaledr   c           	      |   SU[         R                  " SU SUS9S U S-   R                  5       U -  -  -  n[         R                  " XR                  S9nU(       a  Uc   e[	        XtU5      n[         R
                  " X5      R                  5       n[         R                  " U5      n	[         R                  " U5      n
X4$ )N      ?r   r   )r   )r   arangefloatr   r   outercossin)r    r!   r"   r#   r	   r
   r   r   t	freqs_cos	freqs_sins              r   precompute_freqs_cisr.   +   s     %,,q#q8C1HFLLNQTTUE 	S.A'''e3CDKK!'')E		% I		% Ir   	freqs_cisxc                 F   UR                   nU R                   nUS:X  a  U R                  UR                  S   UR                  S   UR                  S   4:X  d   e[        UR                  5       VVs/ s H!  u  pEXBS-
  :X  d  XBS-
  :X  d  XBS-
  :X  a  UOSPM#     nnnOgU R                  UR                  S   UR                  S   4:X  d   e[        UR                  5       VVs/ s H  u  pEUS:X  d  XBS-
  :X  a  UOSPM     nnnU R                  U5      $ s  snnf s  snnf )N   r   r   )ndimshape	enumerateview)r/   r0   r6   freqs_cis_ndimidr7   s          r   reshape_for_broadcastr=   A   s   66D^^N1772;QWWR["IIII "!''*
* ax-1q=AMAI* 	 
 1771:qwwr{";;;;AJ177ASTASa1fAX14AST>>%  
 Us   3(D%Dxqxkr,   r-   returnc                 N   U R                  5       R                  U R                  S S S-   5      R                  S5      u  pEUR                  5       R                  UR                  S S S-   5      R                  S5      u  pg[	        X$5      n[	        X45      nXB-  XS-  -
  nXC-  XR-  -   n	Xb-  Xs-  -
  n
Xc-  Xr-  -   n[
        R                  " X/SS9R                  S5      n[
        R                  " X/SS9R                  S5      nUR                  U 5      UR                  U5      4$ Nr5   )r5   r   r    r2   	r'   reshaper7   unbindr=   r   stackflattentype_as)r>   r?   r,   r-   xq_rxq_ixk_rxk_ixq_out_rxq_out_ixk_out_rxk_out_ixq_outxk_outs                 r   apply_rotary_embrT   R   s    ##BHHSbMG$;<CCBGJD##BHHSbMG$;<CCBGJD%i6I%i6I$"22H$"22H$"22H$"22H[[(-26>>qAF[[(-26>>qAF>>"v~~b111r   c                 >   U R                  5       R                  U R                  S S S-   5      R                  S5      u  p4[	        X5      n[	        X#5      nX1-  XB-  -
  nX2-  XA-  -   n[
        R                  " XV/SS9R                  S5      nUR                  U 5      $ rB   rD   )r?   r,   r-   rL   rM   rP   rQ   rS   s           r   apply_rotary_emb_to_krV   f   s     ##BHHSbMG$;<CCBGJD%i6I%i6I$"22H$"22H[[(-26>>qAF>>"r   c                      ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  S\R                  4S jrSrU =r	$ )	RotaryEmbeddingw   c                 "   > [         TU ]  5         g N)super__init__)self	__class__s    r   r]   RotaryEmbedding.__init__x   s    r   r>   r?   r,   r-   c                 $    [        XX45      u  pVXV4$ r[   rT   )r^   r>   r?   r,   r-   rR   rS   s          r   forwardRotaryEmbedding.forward{   s     *")G~r    )
__name__
__module____qualname____firstlineno__r]   r   Tensorrc   __static_attributes____classcell__r_   s   @r   rX   rX   w   sH    LL LL <<	
 << r   rX   partial_rotary_factorc           
         [        X-  5      n SU[        R                  " SU SU[        R                  S9R	                  5       U -  -  -  n[        R                  " XR
                  [        R                  S9R                  U5      n[        R                  " Xe5      R	                  5       n[        R                  " XU4SS9n[        R                  " U5      n[        R                  " U5      n	X4$ )Nr%   r   r   )r   r   r5   rC   )intr   r&   int64r'   r   rI   r(   catr)   r*   )
r    r!   r"   rn   r   r   r+   embr,   r-   s
             r   hf_precompute_freqs_cisrt      s     c)
*C LLC6EKKMPSS	UE 	SU[[AII	A KK!'')E
))UN
+C		#I		#Ir   c                     U SSU R                   S   S-  24   nU SU R                   S   S-  S24   n[        R                  " U* U4SS9$ )z*Rotates half the hidden dims of the input..Nr5   r   rC   )r7   r   rr   )r0   x1x2s      r   rotate_halfrx      sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r   c                 N   UR                  U5      nUR                  U5      nUR                  S   nU SSU24   U SUS24   pUSSU24   USUS24   p[        R                  " Xr-  [	        U5      U-  -   U/SS9n[        R                  " X-  [	        U	5      U-  -   U
/SS9nX4$ )a  Applies Rotary Position Embedding to the query and key tensors.

Args:
    q (`torch.Tensor`): The query tensor.
    k (`torch.Tensor`): The key tensor.
    cos (`torch.Tensor`): The cosine part of the rotary embedding.
    sin (`torch.Tensor`): The sine part of the rotary embedding.
    position_ids (`torch.Tensor`, *optional*):
        Deprecated and unused.
    unsqueeze_dim (`int`, *optional*, defaults to 1):
        The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
        sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
        that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
        k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
        cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
        the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
Returns:
    `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
r5   .NrC   )	unsqueezer7   r   rr   rx   )qkr)   r*   position_idsunsqueeze_dim
rotary_dimq_rotq_passk_rotk_passq_embedk_embeds                r   hf_apply_rotary_embr      s    ( --
&C
--
&C2Jc;J;&'3
+;)<6c;J;&'3
+;)<6ii%++e*<s*BCVLRTUGii%++e*<s*BCVLRTUGr   c                 p    UR                  U5      nUR                  U5      nX-  [        U 5      U-  -   nU$ )a+  Applies Rotary Position Embedding to the key tensors.

Args:
    k (`torch.Tensor`): The key tensor.
    cos (`torch.Tensor`): The cosine part of the rotary embedding.
    sin (`torch.Tensor`): The sine part of the rotary embedding.
    position_ids (`torch.Tensor`, *optional*):
        Deprecated and unused.
    unsqueeze_dim (`int`, *optional*, defaults to 1):
        The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
        sin[position_ids] so that they can be properly broadcasted to the dimensions of k. For example, note
        that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if
        k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
        cos[position_ids] and sin[position_ids] broadcastable to the shapes of k. Similarly, if k have
        the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
Returns:
    `torch.Tensor` the key tensor rotated using the Rotary Position Embedding.
)rz   rx   )r|   r)   r*   r}   r~   r   s         r   hf_apply_rotary_emb_to_kr      s:    & --
&C
--
&Cw;q>C/0GNr   c                      ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  S\R                  S\R                  4S jrS	\	\R                     S
\
4S jrS\R                  4S jrSrU =r$ )Rope   paramsc                 (  > [         TU ]  5         Xl        U R                  R                  (       aI  [	        [
        U R                  R                  [        U R                  SS5      S9U l        [        U l
        Ov[	        [        U R                  R                  U R                  R                  U R                  R                  [        U R                  SS5      S9U l        [        5       U l
        U R                  U R                  R                  U R                  R                   c  U R                  R"                  OU R                  R"                  S-  U R                  R$                  5      u  p#U R'                  SUSS9  U R'                  S	USS9  g )
Nr   cpu)rn   r   )r#   r	   r
   r   r   r,   F)
persistentr-   )r\   r]   r   use_hf_roper   rt   rn   getattrr.   r   rT   use_scaled_roperope_scale_factorr
   rX   head_dimffn_dim_multipliermax_context_lenrope_freq_baseregister_buffer)r^   r   r,   r-   r_   s       r   r]   Rope.__init__   s;    ;;""(/'&*kk&G&Gt{{He<)D%
 %8D!(/$;;66![[::!%!=!=t{{He<)D% %4$5D!  $88KK   ;;119 ++[[0014KK&& 
	 	[)F[)Fr   r{   r|   r,   r-   c                 &    U R                  XX45      $ r[   rb   )r^   r{   r|   r,   r-   s        r   rc   Rope.forward  s     $$Q9@@r   	input_posseq_lenc                 <   U R                   R                  (       a  Uc   S5       eU R                   R                  (       a  US   R                  5       n[        R
                  " U5        [        R                  " X0R                   R                  :  5        U R                  R                  SX25      nU R                  R                  SX25      nXE4$ U R                  U   nU R                  U   n XE4$ Ub   S5       eU R                  SU nU R                  SU nXE4$ )a:  
Get the precomputed frequencies for the given input position and sequence length.

Args:
    input_pos (torch.Tensor): The input position tensor.
    seq_len (int): The sequence length.

Returns:
    Tuple[torch.Tensor, torch.Tensor]: The precomputed frequencies for the given input position and sequence length.
Nz4input_pos must be provided when use_kv_cache is Truer5   r   z.input_pos is unused when use_kv_cache is False)r   use_kv_cacheenable_dynamic_shapeitemr   _check_is_size_checkr   r,   narrowr-   )r^   r   r   input_pos_itemr,   r-   s         r   	get_freqsRope.get_freqs  s    ;;##%FEF% {{//!*2!3!3!5$$^4^kk.I.IIJ NN11!^M	 NN11!^M	 ## !NN95	 NN95	 ## $V&VV$x0Ix0I##r   indicesc                 >    U R                   U   U R                  U   4$ )z
Get the precomputed frequencies for given input indices.

Args:
    indices (torch.Tensor): The input indices tensor.

Returns:
    Tuple[torch.Tensor, torch.Tensor]: The precomputed frequencies for given input indices.
)r,   r-   )r^   r   s     r   get_freqs_using_indicesRope.get_freqs_using_indices=  s!     ~~g&w(???r   )rT   r   r.   )rf   rg   rh   ri   r   r]   r   rj   rc   r   rp   r   r   rk   rl   rm   s   @r   r   r      s    !Gy !GFA<<A <<A <<	A
 <<A%$8ELL#9 %$C %$N
@u|| 
@ 
@r   r   )g     @FN   r   )r%   r   )Nr   )r   	functoolsr   typingr   r   r   r   +executorch.examples.models.llama.model_argsr   rj   rp   r   r'   boolstrr   r.   r=   rT   rV   nnModulerX   rt   rx   r   r   r   re   r   r   <module>r      s     ) )  A
K KS KC K4 "&', 	 	    	 
 3-    #u||#$ ,!U\\ !ell !"22,,238<<2LQLL2
5<<%&2(!&:?,,
\\"ehhoo 2 $'', 	 	    !	 
 #u||#$ :(@2^@588?? ^@r   