o
    *j                     @   s   d dl mZ d dlmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ dgZejeje	jdG dd deeZdS )    )OrderedDict)Dict	GeneratorN)BertTokenizer)Models)Tensor
TorchModel)MODELS)	GPT3Model)Tasks)read_config)StreamingOutputMixinGPT3ForTextGeneration)module_namec                       s   e Zd Zdef fddZdeeef deeef fddZdeeef deeef fd	d
Ze	de
jdefddZ fddZdddZ	ddddefddZdefddZ  ZS )r   	model_dirc                    sd   t  j|g|R i | dt|v r$ddlm} ||fi || _dS t|| _t|| _	dS )zinitialize the text generation model from the `model_dir` path.

        Args:
            model_dir (str): the model path.
        Zmegatronr   )DistributedGPT3N)
super__init__r   Zmodelscope.models.nlpr   modelr
   Zfrom_pretrainedr   	tokenizer)selfr   argskwargsr   	__class__ k/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/nlp/gpt3/text_generation.pyr      s   zGPT3ForTextGeneration.__init__inputreturnc                 C   s   | j di |S )a%  return the result by the model

        Args:
            input (Dict[str, Tensor]): the preprocessed data

        Returns:
            Dict[str, Tensor]: results

        Example:
            >>> {
            >>>     'logits': Tensor([[0.54, 0.32...])]), # logits
            >>> }
        Nr   )r   )r   r   r   r   r   forward(   s   zGPT3ForTextGeneration.forwardinputsc                 K   sP   t | jts| jjdi ||S |d }| |d }| jj|fd|i|S )N	input_idsattention_maskprompt_lengthr   )
isinstancer   r
   generate_get_lengthr   r    r   tokenslengthsr   r   r   r%   8   s
   zGPT3ForTextGeneration.generater"   c                 C   s   |  dd S )N   )sum)r"   r   r   r   r&   A   s   z!GPT3ForTextGeneration._get_lengthc                    s0   t | jts| jj|i |S t j|i |S N)r$   r   r
   save_pretrainedr   )r   r   r   r   r   r   r.   E   s   z%GPT3ForTextGeneration.save_pretrainedN Fc                 C   s   | j |||S r-   )r   
state_dict)r   ZdestinationprefixZ	keep_varsr   r   r   r0   J   s   z GPT3ForTextGeneration.state_dictTr0   zOrderedDict[str, Tensor]strictc                 C   s   | j ||S r-   )r   load_state_dict)r   r0   r2   r   r   r   r3   M   s   z%GPT3ForTextGeneration.load_state_dictc                 K   s.   |d }|  |d }| jj|fd|i|S )Nr!   r"   r#   )r&   r   Zstreaming_generater'   r   r   r   stream_generateR   s   z%GPT3ForTextGeneration.stream_generate)Nr/   F)T)__name__
__module____qualname__strr   r   r   r   r%   staticmethodtorchr&   r.   r0   boolr3   r   r4   __classcell__r   r   r   r   r      s"    "

	

)collectionsr   typingr   r   r:   Ztransformersr   Zmodelscope.metainfor   Zmodelscope.models.baser   r   Zmodelscope.models.builderr	   Zmodelscope.models.nlp.gpt3r
   Zmodelscope.utils.constantr   Zmodelscope.utils.hubr   Z!modelscope.utils.streaming_outputr   __all__Zregister_moduleZtext_generationZgpt3r   r   r   r   r   <module>   s   