o
    %ݫi                     @   sL   d Z ddlmZ ddlZddlmZ ddlmZ eeZ	G dd deZ
dS )zThis lobe enables the integration of generic huggingface pretrained text
encoders (e.g. BERT).

Transformer from HuggingFace needs to be installed:
https://huggingface.co/transformers/installation.html

Authors
 * Sylvain de Langen 2024
    )OptionalN)HFTransformersInterface)
get_loggerc                       sN   e Zd ZdZ		ddee f fddZdefdd	ZddefddZ	  Z
S )TextEncodera  This lobe enables the integration of a generic HuggingFace text encoder
    (e.g. BERT). Requires the `AutoModel` found from the `source` to have a
    `last_hidden_state` key in the output dict.

    Arguments
    ---------
    source : str
        HuggingFace hub name: e.g "google-bert/bert-base"
    save_path : str
        Path (dir) of the downloaded model.
    freeze : bool (default: True)
        If True, the model is frozen. If False, the model will be trained
        alongside with the rest of the pipeline.
    num_layers : int, optional
        When specified, and assuming the passed LM can be truncated that way,
        the encoder for the passed model will be truncated to the specified
        layer (mutating it). This means that the embeddings will be those of the
        Nth layer rather than the last layer. The last layer is not necessarily
        the best for certain tasks.
    **kwargs
        Extra keyword arguments passed to the `from_pretrained` function.
    Example
    -------
    >>> inputs = ["La vie est belle"]
    >>> model_hub = "google-bert/bert-base-multilingual-cased"
    >>> save_path = "savedir"
    >>> model = TextEncoder(model_hub, save_path)
    >>> outputs = model(inputs)
    TN
num_layersc                    s@   t  jd|||d| | j|d |d ur| | d S d S )N)source	save_pathfreeze)r    )super__init__load_tokenizertruncate)selfr   r   r	   r   kwargs	__class__r
   q/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/lobes/models/huggingface_transformers/textencoder.pyr   6   s   zTextEncoder.__init__keep_layersc                 C   sF   |dksJ d|t | jjjksJ d| jjjd| | jj_dS )a
  Truncates the encoder to a specific layer so that output embeddings
        are the hidden state of the n-th layer.

        Arguments
        ---------
        keep_layers : int
            Number of layers to keep, e.g. 4 would keep layers `[0, 1, 2, 3]`.
        r   z`Invalid requested layer count: Must keep at least one LM layer (negative values are not allowed)z<Too few layers in LM: kept layer count requested is too highN)lenmodelencoderlayer)r   r   r
   r
   r   r   G   s   
zTextEncoder.truncateFreturn_tokensc                 C   s|   t | j - | j|ddd| jj}| jdi |j}|r+||fW  d   S |W  d   S 1 s7w   Y  dS )a  This method implements a forward of the encoder model,
        which generates batches of embeddings embeddings from input text.

        Arguments
        ---------
        input_texts : list of str
            The list of texts (required).
        return_tokens : bool
            Whether to also return the tokens.

        Returns
        -------
        (any, torch.Tensor) if `return_tokens == True`
            Respectively:
            - Tokenized sentence in the form of a padded batch tensor. In the HF
              format, as returned by the tokenizer.
            - Output embeddings of the model (i.e. the last hidden state)

        torch.Tensor if `return_tokens` == False
            Output embeddings of the model (i.e. the last hidden state)
        ptT)return_tensorspaddingNr
   )torchset_grad_enabledr	   	tokenizertor   devicelast_hidden_state)r   input_textsr   
embeddingsr
   r
   r   forwardY   s   

$zTextEncoder.forward)TN)F)__name__
__module____qualname____doc__r   intr   r   boolr%   __classcell__r
   r
   r   r   r      s    "r   )r)   typingr   r   =speechbrain.lobes.models.huggingface_transformers.huggingfacer   speechbrain.utils.loggerr   r&   loggerr   r
   r
   r
   r   <module>   s    
