o
    ߥi                     @   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.
        megatronr   )DistributedGPT3N)
super__init__r   modelscope.models.nlpr   modelr
   from_pretrainedr   	tokenizer)selfr   argskwargsr   	__class__ ^/home/ubuntu/.local/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   r1   E   s   z%GPT3ForTextGeneration.save_pretrainedN Fc                 C   s   | j |||S r0   )r   
state_dict)r   destinationprefix	keep_varsr   r   r   r3   J   s   z GPT3ForTextGeneration.state_dictTr3   zOrderedDict[str, Tensor]strictc                 C   s   | j ||S r0   )r   load_state_dict)r   r3   r7   r   r   r   r8   M   s   z%GPT3ForTextGeneration.load_state_dictc                 K   s.   |d }|  |d }| jj|fd|i|S )Nr$   r%   r&   )r)   r   streaming_generater*   r   r   r   stream_generateR   s   z%GPT3ForTextGeneration.stream_generate)Nr2   F)T)__name__
__module____qualname__strr   r   r   r"   r(   staticmethodtorchr)   r1   r3   boolr8   r   r:   __classcell__r   r   r   r   r      s"    "

	

)collectionsr   typingr   r   r@   transformersr   modelscope.metainfor   modelscope.models.baser   r   modelscope.models.builderr	   modelscope.models.nlp.gpt3r
   modelscope.utils.constantr   modelscope.utils.hubr   !modelscope.utils.streaming_outputr   __all__register_moduletext_generationgpt3r   r   r   r   r   <module>   s   