
    bi              	       0   d dl Z d dlmZ d dlZd dlmZ dej                  dej                  fdZd Zdej                  dej                  fdZ
	 ddej                  d	eeej                        d
eej                     dej                  fdZdedefdZy)    N)Optionalxreturnc                     | dd| j                   d   dz  f   }| d| j                   d   dz  df   }t        j                  | |fd      S )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/cdr/jupyterlab/.venv/lib/python3.12/site-packages/peft/tuners/adaption_prompt/utils.pyllama_rotate_halfr      s\     
3"!''"+"""	#B	
3q ""	#B99rc2YB''    c                    t        |j                        dk(  r|ddddddf   }|j                  d|j                  d   d|j                  d         }t        j                  |j                  |j                  d   ddd      d|      }t        j                  |j                  |j                  d   ddd      d|      }n(||   j                  d      }||   j                  d      }| |z  t        |       |z  z   }|S )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     399~%aq$&67'..q#))A,399Q<Pll3::n&:&:1&=q!QGN[ll3::n&:&:1&=q!QGN[ ,))!,,))!,3w,Q/#56GNr   modelc                 N   |j                  d      }|j                  d      }|j                  d      }|j                         \  }}}t        | d      r| j                  }n| j                  j
                  }| j                  |      j                  |||| j                        j                  dd      }	| j                  j                  | j                  j                  z  }
| j                  |      j                  ||||
z  | j                        j                  dd      }|}|Dt        |t              r||d   j                   d   z  }n||j#                  | j$                        z  }d	|v r>|d	   \  }}|j'                  d      }|j'                  d      }|	|z  t)        |	      |z  z   S dt+        j,                  | j.                  j0                        j2                  vr$| j/                  ||
      \  }}t5        |	|||      S d}|z|&t7        j8                  |||z   |j:                        }nA|j=                  || j$                        }t7        j8                  |||z   |j:                        }|j'                  d      }d|i}dt+        j,                  | j.                  j0                        j2                  v r||z   |d<    | j.                  |fi |\  }}t?        |j                         dk(  r"|j'                  d      }|j'                  d      }|	|z  t)        |	      |z  z   S )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_value	num_headsr   r   r   position_embeddings)seq_len)devicer)   r   ) getsizehasattrr&   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_r&   query_statesfactorvalue_statesr)   r   r   past_seen_tokensnew_cache_positionsrotary_emb_kwargss                     r   llama_compute_query_statesrM   =   s    JJ/M::n-LZZ 01N!&&(MCuk"OO	LL44	<<.33C	5>>ZddefhijL\\%%)B)BBF<<.33CfATW\WeWefppqrtuvLG!ne,~a(..r22G ~44U__EEG &/0SmmAmmAs"'8'F'LMM W..u/?/?/G/GHSSS##L'#BS),S,OO!"',,ueemLL_L_"`-??uW"',,/?AQTYAYbnbubu"v*44Q7'6G%%e&6&6&>&>?JJJ',/?'?)$uB0ABHC 399~mmAmmA3#4\#BS#HIIr   r$   encoder_hidden_statesc                 n   |At        | d      s#t        d| j                  j                   d      | j	                  |      }n0| j                  |      j                  | j                  d      \  }}}g |j                  dd d| j                  }|j                  |      j                  dd      }|S )	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   )r-   
ValueError	__class____name__rP   c_attnsplit
split_sizer   r2   r1   r3   )r"   r$   rN   rG   rF   shape_qs         r   gpt2_compute_query_statesrX      s     (uh'u//0 1i j  ||M2"\\-8>>u?O?OUV>Wa<""3B'<<U^^<G$$W-771=Lr   paramsc                 H    | j                  d      d   j                  d      S )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!   ModulerM   r9   FloatTensorrX   strboolr^    r   r   <module>rh      s       ( (%,, (2CJbii CJell CJR 5999E%"3"345 $ELL1 \\	29 9 9r   