o
    8wÖi¿  ã                   @   s    d dl Z G dd„ de jjƒZdS )é    Nc                       s0   e Zd ZdZ‡ fdd„Zdejfdd„Z‡  ZS )ÚPrefixEncoderaC  
    The `torch.nn` model to encode the prefix.

    Args:
        config ([`PrefixTuningConfig`]): The configuration of the prefix encoder.

    Example:

    ```py
    >>> from peft import PrefixEncoder, PrefixTuningConfig

    >>> config = PrefixTuningConfig(
    ...     peft_type="PREFIX_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,
    ...     encoder_hidden_size=768,
    ... )
    >>> prefix_encoder = PrefixEncoder(config)
    ```

    **Attributes**:
        - **embedding** (`torch.nn.Embedding`) -- The embedding layer of the prefix encoder.
        - **transform** (`torch.nn.Sequential`) -- The two-layer MLP to transform the prefix embeddings if
          `prefix_projection` is `True`.
        - **prefix_projection** (`bool`) -- Whether to project the prefix embeddings.

    Input shape: (`batch_size`, `num_virtual_tokens`)

    Output shape: (`batch_size`, `num_virtual_tokens`, `2*layers*hidden`)
    c              	      sš   t ƒ  ¡  |j| _|j}|j}|j}|j}| jr?|js?tj	 
||¡| _tj	 tj	 ||¡tj	 ¡ tj	 ||d | ¡¡| _d S tj	 
||d | ¡| _d S )Né   )ÚsuperÚ__init__Úprefix_projectionÚ	token_dimÚ
num_layersÚencoder_hidden_sizeÚnum_virtual_tokensÚinference_modeÚtorchÚnnÚ	EmbeddingÚ	embeddingÚ
SequentialÚLinearÚTanhÚ	transform)ÚselfÚconfigr   r   r	   r
   ©Ú	__class__© ú\/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/peft/tuners/prefix_tuning/model.pyr   8   s   

ýzPrefixEncoder.__init__Úprefixc                 C   s,   | j r|  |¡}|  |¡}|S |  |¡}|S )N)r   r   r   )r   r   Úprefix_tokensÚpast_key_valuesr   r   r   ÚforwardJ   s   


ÿzPrefixEncoder.forward)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚTensorr   Ú__classcell__r   r   r   r   r      s    #r   )r   r   ÚModuler   r   r   r   r   Ú<module>   s   