o
    ॵi;                     @   s   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	 d dl
mZ d dlmZ d dlmZ ejejejd	G d
d de	ZdS )    )AnyDictN)	Pipelines)DistributedGPTMoE)DistributedPipeline)	PIPELINES)TextGenerationJiebaPreprocessor)Tasks)module_namec                       sz   e Zd ZdZdZd fdd	Zedd Ze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 )DistributedGPTMoEPipelinez9This class is used to instantiate the gpt-moe model.
    Nc                    s:   |d u rt |}t j|fd|i| t|dsJ d S )Npreprocessor	tokenizer)r   super__init__hasattr)selfmodelr   kwargs	__class__ i/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/distributed_gpt_moe_pipeline.pyr      s   z"DistributedGPTMoEPipeline.__init__c                 K   s"   t ||fi || _| j  d S N)r   r   eval)clsrank	model_dirr   r   r   r   _instantiate_one   s   z*DistributedGPTMoEPipeline._instantiate_oneinputsreturnc                 C   s$   |d d  tj  }| j|S )Nr   	input_ids)cudatorchcurrent_devicer   generate)r   r   tokensr   r   r   _forward_one"   s   z&DistributedGPTMoEPipeline._forward_onec                 K   s*   ddl m} |j| jj|jd  iS )zprocess the prediction results

        Args:
            inputs (Dict[str, Any]): _description_

        Returns:
            Dict[str, str]: the prediction results
        r   )
OutputKeys)modelscope.outputsr'   TEXTr   r   
detokenize	sequencestolist)r   r   postprocess_paramsr'   r   r   r   postprocess(   s   
z%DistributedGPTMoEPipeline.postprocessr   )__name__
__module____qualname____doc__r   r   classmethodr   r   strr   r&   r.   __classcell__r   r   r   r   r      s    
$
r   )typingr   r   r"   modelscope.metainfor   1modelscope.models.nlp.gpt_moe.distributed_gpt_moer   modelscope.pipelines.baser   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr	   register_moduletext_generationgpt_moe_generationr   r   r   r   r   <module>   s   