
    oi*                     j    S SK r S SKrS SKJr  SSKJr   " S S\R                  R                  5      rg)    N)gather_params_ctx   )PromptTuningInitc                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )PromptEmbedding   aV  
The model to encode virtual tokens into prompt embeddings.

Args:
    config ([`PromptTuningConfig`]): The configuration of the prompt embedding.
    word_embeddings (`torch.nn.Module`): The word embeddings of the base transformer model.

**Attributes**:
    - **embedding** (`torch.nn.Embedding`) -- The embedding layer of the prompt embedding.

Example:

```py
>>> from peft import PromptEmbedding, PromptTuningConfig

>>> config = PromptTuningConfig(
...     peft_type="PROMPT_TUNING",
...     task_type="SEQ_2_SEQ_LM",
...     num_virtual_tokens=20,
...     token_dim=768,
...     num_transformer_submodules=1,
...     num_attention_heads=12,
...     num_layers=12,
...     prompt_tuning_init="TEXT",
...     prompt_tuning_init_text="Predict if sentiment of this review is positive, negative or neutral",
...     tokenizer_name_or_path="t5-base",
... )

>>> # t5_model.shared is the word embeddings of the base model
>>> prompt_embedding = PromptEmbedding(config, t5_model.shared)
```

Input Shape: (`batch_size`, `total_virtual_tokens`)

Output Shape: (`batch_size`, `total_virtual_tokens`, `token_dim`)
c                 \  > [         TU ]  5         UR                  UR                  -  n[        R
                  R                  X1R                  5      U l        UR                  [        R                  :X  a  UR                  (       d  UR                  n[        R                  " SXC4[        R                  S9R!                  UR"                  R$                  5      n['        UR)                  5       5         U" U5      R+                  5       R-                  5       nS S S 5        WR!                  [        R.                  5      n[        R
                  R1                  U5      U R                  l        g UR                  [        R2                  :X  Ga}  UR                  (       Gdj  SSKJn  UR8                  =(       d    0 nUR;                  SS 5        UR<                  " UR>                  40 UD6n	UR@                  n
U	" U
5      S   n[C        U5      nX:  a  US U nO!X:  a  [D        RF                  " X;-  5      nX\-  nUS U n[        RH                  " U5      R!                  UR"                  R$                  5      n['        UR)                  5       5         U" U5      R+                  5       R-                  5       nS S S 5        WR!                  [        R.                  5      n[        R
                  R1                  U5      U R                  l        g g g ! , (       d  f       GN= f! , (       d  f       Np= f)Nr   )dtype)AutoTokenizertrust_remote_code	input_ids)%super__init__num_virtual_tokensnum_transformer_submodulestorchnn	Embedding	token_dim	embeddingprompt_tuning_initr   SAMPLE_VOCABinference_modenum_embeddingsrandintlongtoweightdevicer   
parametersdetachclonefloat32	ParameterTEXTtransformersr   tokenizer_kwargspopfrom_pretrainedtokenizer_name_or_pathprompt_tuning_init_textlenmathceil
LongTensor)selfconfigword_embeddingstotal_virtual_tokens
vocab_sizeinit_token_idsword_embedding_weightsr   r'   	tokenizer	init_textnum_text_tokensnum_reps	__class__s                Y/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/prompt_tuning/model.pyr   PromptEmbedding.__init__>   sz   %886;\;\\++,@BRBRS$$(8(E(EEfNcNc(77J"]]1j:QY^YcYcdgg&&--N #?#=#=#?@)8)H)O)O)Q)W)W)Y& A%;%>%>u}}%M"$)HH$6$67M$NDNN!&&*:*?*??H]H]H]2%66<"   !4d;%55f6S6ShWghI66I&y1+>N!.1O5!/0E1E!F 799%9%KL!/!:+,A-ABN"--n=@@AWAWA^A^_N"?#=#=#?@)8)H)O)O)Q)W)W)Y& A%;%>%>u}}%M"$)HH$6$67M$NDNN!- I^? A@0 A@s   3%L%L
L
L+c                 (    U R                  U5      nU$ )Nr   )r0   indicesprompt_embeddingss      r<   forwardPromptEmbedding.forwardf   s     NN73      r?   )	__name__
__module____qualname____firstlineno____doc__r   rB   __static_attributes____classcell__)r;   s   @r<   r   r      s    #J&OP! !rD   r   )	r-   r   peft.utils.integrationsr   r1   r   r   Moduler    rD   r<   <module>rO      s)      5 $Q!ehhoo Q!rD   