o
    ߥi                     @   s   d dl mZ d dlmZmZ d dlZd dlmZmZ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gZejejejdG dd deeZdS )    )OrderedDict)Dict	GeneratorN)
AutoConfigAutoModelForCausalLMAutoTokenizer)Models)Tensor
TorchModel)MODELS)Tasks)read_config)StreamingOutputMixinPolyLMForTextGeneration)module_namec                       sf   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  Z	S )
r   	model_dirc                    sL   t  j|g|R i | tj|ddd| _tj|ddd| _| j  dS )zinitialize the text generation model from the `model_dir` path.

        Args:
            model_dir (str): the model path.
        F)legacyuse_fastautoT)
device_maptrust_remote_codeN)super__init__r   from_pretrained	tokenizerr   modeleval)selfr   argskwargs	__class__ `/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/polylm/text_generation.pyr      s   z PolyLMForTextGeneration.__init__inputreturnc                 K   s   | j |fi |}|S )zreturn the result by the model

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

        Returns:
            Dict[str, Tensor]: results
        )generate)r   r$   r   resr"   r"   r#   forward"   s   	zPolyLMForTextGeneration.forwardc                 K   sV   | j j}| j|dd}| j j|j|fd|j|i|}| jj|d dd}|S )Npt)return_tensorsattention_maskr   T)skip_special_tokens)r   devicer   r&   	input_idstor+   decode)r   r$   r   r-   inputsoutputspredr"   r"   r#   r&   .   s   

z PolyLMForTextGeneration.generate)
__name__
__module____qualname__strr   r   r	   r(   r&   __classcell__r"   r"   r    r#   r      s    "
)collectionsr   typingr   r   torchtransformersr   r   r   modelscope.metainfor   modelscope.models.baser	   r
   modelscope.models.builderr   modelscope.utils.constantr   modelscope.utils.hubr   !modelscope.utils.streaming_outputr   __all__register_moduletext_generationpolylmr   r"   r"   r"   r#   <module>   s   