ó
    >:j  ã            	       ó:  • S SK r S SKJr  S SKrS SKJr  S\R                  S\R                  4S jrS rS\R                  S\R                  4S jr
 SS\R                  S	\\\R                        S
\\R                     S\R                  4S jjrS\S\4S jrg)é    N)ÚOptionalÚxÚreturnc                 ó–   • 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$ )a°  
Rotate half the hidden dims of the input.

This function was duplicated verbatim from:
https://github.com/huggingface/transformers/blob/1de8ce9ee1191ba761a593ac15d9ccbf5851bfc5/src/transformers/models/llama/modeling_llama.py#L126

This was done to eliminate the Llama transformers implementation as a dependency of this file. Note that some other
functions were also adapted from the transformers implementation but were modified.
.Néÿÿÿÿé   ©Údim)ÚshapeÚtorchÚcat)r   Úx1Úx2s      Ú^/home/mande/repo/quber/.venv/lib/python3.13/site-packages/peft/tuners/adaption_prompt/utils.pyÚllama_rotate_halfr      s\   € ð 
ˆ3Ð"�!—'‘'˜"‘+ Ñ"Ð"Ð"Ñ	#€BØ	
ˆ3�—‘˜‘˜qÑ Ñ"Ð"Ñ	#€BÜ�9Š9�r�c˜2�Y BÑ'Ð'ó    c                 óþ  • [        UR                  5      S:X  aª  USS2SSS2S4   nUR                  SUR                  S   SUR                  S   5      n[        R                  " UR                  UR                  S   SSS5      SU5      n[        R                  " UR                  UR                  S   SSS5      SU5      nO&X   R                  S5      nX#   R                  S5      nX-  [        U 5      U-  -   nU$ )a–  
Apply rotary position embedding to query states in the Llama model.

This function was adapted from:
https://github.com/huggingface/transformers/blob/1de8ce9ee1191ba761a593ac15d9ccbf5851bfc5/src/transformers/models/llama/modeling_llama.py#L133

It was modified to remove unnecessary processing of key states. The method is compatible with transformers <=
4.34.2 and also with the latest version (>=4.35).
é   Né   é   r   r   )Úlenr   Úrepeatr   ÚgatherÚ	unsqueezer   )ÚqÚcosÚsinÚposition_idsÚgather_indicesÚq_embeds         r   Úllama_apply_rotary_pos_embr!   $   sé   € ô ˆ3�9‰9ƒ~˜ÓØ%¢a¨ªq°$Ð&6Ñ7ˆØ'×.Ñ.¨q°#·)±)¸A±,ÀÀ3Ç9Á9ÈQÁ<ÓPˆÜ�lŠl˜3Ÿ:™: n×&:Ñ&:¸1Ñ&=¸qÀ!ÀQÓGÈÈNÓ[ˆÜ�lŠl˜3Ÿ:™: n×&:Ñ&:¸1Ñ&=¸qÀ!ÀQÓGÈÈNÓ[‰ð Ñ×)Ñ)¨!Ó,ˆØÑ×)Ñ)¨!Ó,ˆØ‰wÔ,¨QÓ/°#Ñ5Ñ6€GØ€Nr   Úmodelc                 ó
  • UR                  S5      nUR                  S5      nUR                  S5      nUR                  5       u  pVnU R                  R                  nU R	                  U5      R                  XVX€R                  5      R                  SS5      n	U R                  R                  U R                  R                  -  n
U R                  U5      R                  XVXŠ-  U R                  5      R                  SS5      nUnUbG  [        U[        5      (       a  XÄS   R                  S   -  nOXÄR                  U R                   5      -  nSU;   a<  US   u  pÞUR#                  S5      nUR#                  S5      nX�-  [%        U	5      U-  -   $ S[&        R(                  " U R*                  R,                  5      R.                  ;  a  U R+                  X¼S	9u  pÞ[1        X�Xã5      $ SnUct  Uc#  [2        R4                  " XfU-   UR6                  S
9nO=UR9                  X`R                   5      n[2        R4                  " XÿU-   UR6                  S
9nUR#                  S5      nSU0nS[&        R(                  " U R*                  R,                  5      R.                  ;   a  Xo-   US'   U R*                  " U40 UD6u  pÞ[;        UR                  5      S:X  a"  UR#                  S5      nUR#                  S5      nX�-  [%        U	5      U-  -   $ )a  
Compute query states for Llama models specifically. They need to be recomputed as the forward() method of the
original LlamaModel in the transformers library does not return them. See the related discussion in the PR:
https://github.com/huggingface/peft/pull/268
Úhidden_statesr   Úpast_key_valuer   r   r   éþÿÿÿÚposition_embeddings)Úseq_len)Údevicer(   r   )ÚgetÚsizeÚconfigÚnum_attention_headsÚq_projÚviewÚhead_dimÚ	transposeÚk_projÚin_featuresÚout_featuresÚv_projÚ
isinstanceÚtupler   Úget_seq_lengthÚ	layer_idxr   r   ÚinspectÚ	signatureÚ
rotary_embÚforwardÚ
parametersr!   r   Úaranger)   Úget_usable_lengthr   )r"   Úkwargsr$   r   r%   ÚbszÚq_lenÚ_Ú	num_headsÚquery_statesÚfactorÚvalue_statesr(   r   r   Úpast_seen_tokensÚnew_cache_positionsÚrotary_emb_kwargss                     r   Úllama_compute_query_statesrL   =   s¾  € ð —J‘J˜Ó/€MØ—:‘:˜nÓ-€LØ—Z‘ZÐ 0Ó1€NØ!×&Ñ&Ó(�M€C�Ø—‘×0Ñ0€IØ—<‘< Ó.×3Ñ3°CÀ	Ï>É>ÓZ×dÑdÐefÐhiÓj€Là�\‰\×%Ñ%¨¯©×)BÑ)BÑB€FØ—<‘< Ó.×3Ñ3°CÀÑATÐW\×WeÑWeÓf×pÑpÐqrÐtuÓv€Là€GàÑ!Ü�n¤e×,Ñ,à aÑ(×.Ñ.¨rÑ2Ñ2‰Gð ×4Ñ4°U·_±_ÓEÑEˆGð  Ó&ØÐ/Ñ0‰ˆØ�m‰m˜AÓˆØ�m‰m˜AÓˆØÑ"Ô'8¸Ó'FÈÑ'LÑMÐMð œW×.Ò.¨u×/?Ñ/?×/GÑ/GÓH×SÑSÓSà×#Ñ# LÐ#ÐB‰ˆÜ)¨,¸SÓOÐOàÐØÑàÑ!Ü"'§,¢,¨u¸e±mÈL×L_ÑL_Ñ"`Ñà-×?Ñ?ÀÇÁÓWÐÜ"'§,¢,Ð/?ÐTYÑAYÐbn×buÑbuÑ"vÐØ*×4Ñ4°QÓ7ˆà'¨Ð6Ðà”G×%Ò% e×&6Ñ&6×&>Ñ&>Ó?×JÑJÓJØ',Ñ'?Ð˜)Ñ$à×Ò ÑBÐ0AÑB�H€Cô ˆ3�9‰9ƒ~˜ÓØ�m‰m˜AÓˆØ�m‰m˜AÓˆàÑÔ#4°\Ó#BÀSÑ#HÑIÐIr   r$   Úencoder_hidden_statesc                 ót  • UbF  [        U S5      (       d#  [        SU R                  R                   S35      eU R	                  U5      nO-U R                  U5      R                  U R                  SS9u  n  n/ UR                  SS QSPU R                  P7nUR                  U5      R                  SS5      nU$ )	z¾
Compute query states for GPT2 models. They need to be recomputed as the forward() method of the GPT@ in the
transformers library does not return them. See the related discussion in the PR:
NÚq_attnzIf `zš` is used as cross attention, the weights `q_attn` must be defined. Please make sure to instantiate it with `GPT2Attention(..., is_cross_attention=True)`.r   r	   r   r   )ÚhasattrÚ
ValueErrorÚ	__class__Ú__name__rO   Úc_attnÚsplitÚ
split_sizer   r0   r/   r1   )r"   r$   rM   rF   rD   Úshape_qs         r   Úgpt2_compute_query_statesrX      sÆ   € ð Ñ(Ü�u˜h×'Ñ'ÜØ�u—‘×/Ñ/Ð0ð 1ið jóð ð —|‘| MÓ2‰à"Ÿ\™\¨-Ó8×>Ñ>¸u×?OÑ?OÐUVÐ>ÐWÑˆ�a˜à<�×"Ñ" 3 BÐ'Ð<¨Ð<¨U¯^©^Ñ<€GØ×$Ñ$ WÓ-×7Ñ7¸¸1Ó=€LàÐr   Úparamsc                 óH   • U R                  S5      S   R                  S5      $ )zEReturn True if module is trainable under adaption prompt fine-tuning.Ú.r   Ú	adaption_)rU   Ú
startswith)rY   s    r   Úis_adaption_prompt_trainabler^   ˜   s!   € à�<‰<˜Ó˜RÑ ×+Ñ+¨KÓ8Ð8r   )N)r:   Útypingr   r   Útorch.nnÚnnÚTensorr   r!   ÚModulerL   r7   ÚFloatTensorrX   ÚstrÚboolr^   © r   r   Ú<module>rh      s´   ðó Ý ã Ý ð(˜Ÿ™ð (¨%¯,©,ô (òð2?J b§i¡ið ?J¸e¿l¹lô ?JðJ 59ñØ�9‰9ðà˜E %×"3Ñ"3Ñ4Ñ5ðð $ E§L¡LÑ1ðð ‡\�\õ	ð29¨ð 9°õ 9r   