o
    %ݫi                     @   s@   d 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 huggingface pretrained GPT2LMHeadModel model.

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

Authors
 * Pooneh Mousavi 2023
 * Simone Alghisi 2023
    N)HFTransformersInterface)
get_loggerc                       sl   e Zd ZdZ									d	d fddZdejdejdejfddZ	ddejdejfddZ  Z	S )GPTa  This lobe enables the integration of HuggingFace pretrained GPT model.
     Source paper whisper:
        https://life-extension.github.io/2020/05/27/GPT%E6%8A%80%E6%9C%AF%E5%88%9D%E6%8E%A2/language-models.pdf
    Transformer from HuggingFace needs to be installed:
        https://huggingface.co/transformers/installation.html

    The model can be finetuned. It will download automatically the model from
    HuggingFace or use a local path.

    Arguments
    ---------
    source : str
        HuggingFace hub name: e.g "gpt2"
    save_path : str
        Path (dir) of the downloaded model.
    freeze : bool (default: False)
        If True, the model is frozen. If False, the model will be trained
        alongside with the rest of the pipeline.
    max_new_tokens : int
        Maximum count of new tokens allowed.
    min_length : int
        Minimum count of input tokens
    top_k : int
        Top results count to keep
    top_p : float
        Proportion of top results to keep
    num_beams : int
        Number of decoder beams
    eos_token_id : int
        Index of end-of-sentence token.
    early_stopping : int
        Whether to stop training early.

    Example
    -------
    >>> model_hub = "gpt2"
    >>> save_path = "savedir"
    >>> model = GPT(model_hub, save_path)
    >>> tokens = torch.tensor([[1, 1]])
    >>> tokens_type = torch.tensor([[1, 1]])
    >>> attention_mask = torch.tensor([[1, 1]])
    >>> outputs = model(tokens, tokens_type, attention_mask)
    F      -   ?   R  TreturnNc                    s   t  j|||dd || _|| _|| _|| _|| _|
| _|	| _| j	|d dd | j
r?td | j  | j D ]}d|_q9d S d S )NT)source	save_pathfreezewith_lm_headF)r   	pad_tokenuse_fastz!huggingface_GPT - GPT  is frozen.)super__init__max_new_tokens
min_lengthtop_ktop_p	num_beamsearly_stoppingeos_token_idload_tokenizerr   loggerwarningmodeltrain
parametersrequires_grad)selfr   r   r   r   r   r   r   r   r   r   param	__class__ i/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/lobes/models/huggingface_transformers/gpt.pyr   B   s$   

zGPT.__init__	input_idstoken_type_idsattention_maskc                 C   sF   t | j  | jj|||d}W d   |S 1 sw   Y  |S )a  Takes an input a history of conversation and returns its corresponding reply.

        Arguments
        ---------
        input_ids : torch.Tensor
            A batch of input-id to transform to features.
        token_type_ids : torch.Tensor
            Token Type(Speaker) for each token in input_ids.
        attention_mask : torch.Tensor
            A batch of attention_mask.

        Returns
        -------
        output : torch.Tensor
            Reply to conversation
        )r)   r*   N)torchset_grad_enabledr   r   forward)r"   r(   r)   r*   outputr&   r&   r'   r-   b   s   
zGPT.forwardgreedyc                 C   s   t  < |dkr#| jj|||d| j| j| j| j| jd| j	| j
d}n| jj||| j| j	|d}W d   |S W d   |S 1 sCw   Y  |S )a   Takes an input a history of conversation and returns its corresponding reply.

        Arguments
        ---------
        input_ids : torch.Tensor
            A batch of input-id which are dialogue context tokens
        token_type_ids : torch.Tensor
        attention_mask : torch.Tensor
            A batch of attention_mask.
        decoder_type : str
            It shows strategy for autoregressive decoding either beam search or greedy.

        Returns
        -------
        hyp : torch.Tensor
            Conversation reply.
        beamTr   )r(   r)   r*   	do_sampler   r   r   r   r   num_return_sequencesr   r   )r)   r   r   r*   N)r+   no_gradr   generater   r   r   r   r   r   r   )r"   r(   r)   r*   decoder_typehypr&   r&   r'   r4      s<   


zGPT.generate)Fr   r   r   r   r	   r
   T)r   N)r/   )
__name__
__module____qualname____doc__r   r+   Tensorr-   r4   __classcell__r&   r&   r$   r'   r      s4    0 
#r   )	r:   r+   =speechbrain.lobes.models.huggingface_transformers.huggingfacer   speechbrain.utils.loggerr   r7   r   r   r&   r&   r&   r'   <module>   s    
