o
    ॵi*                     @   s   d dl mZmZ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mZ d d	lmZ d d
lmZ ejejejdG dd deeZdS )    )AnyDict	GeneratorOptionalN)	Pipelines)DistributedGPT3)DistributedPipeline)	PIPELINES)TextGenerationJiebaPreprocessor)
FrameworksTasks)device_placement)PipelineStreamingOutputMixin)module_namec                       s   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dd Zdee	e
f dee	e
f dee	e
f defddZedee	e
f ddfddZededeee	e
f  fddZ  ZS )DistributedGPT3Pipelinez6This class is used to instantiate the gpt3 model.
    Nc                    sH   |du rt |}t j|fd|i| t|dsJ t | _d| _dS )a  

        Args:
            model: The model piece, str is not supported.
            preprocessor: The preprocessor matched with the model.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.
        Npreprocessor	tokenizerT)r
   super__init__hasattrr   model_model_prepare)selfr   r   kwargs	__class__ f/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/distributed_gpt3_pipeline.pyr      s   	
z DistributedGPT3Pipeline.__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(DistributedGPT3Pipeline._instantiate_oneinputsreturnc                 C   s0   |d d  tj  }| jj|fi |d S Nr$   	input_idsforward_params)cudatorchcurrent_devicer   generater    r$   tokensr   r   r   _forward_one/   s   z$DistributedGPT3Pipeline._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.outputsr0   TEXTr   r   
detokenize	sequencestolist)r   r$   postprocess_paramsr0   r   r   r   postprocess5   s   
z#DistributedGPT3Pipeline.postprocessc                 K   s
   i |i fS r   r   )r   pipeline_parametersr   r   r   _sanitize_parametersF   s   
z,DistributedGPT3Pipeline._sanitize_parametersmodel_inputr(   r6   c                 c   s    t | j| j" | jr| |}||d}| j| jj|g| j	  W d    n1 s-w   Y  	 | j| jj
t| j	}|d d u rGd S | j|d fi |}| | |V  q3)N)r$   r(   Tr   )r   	frameworkdevice_name_auto_collate_collate_fn
model_poolmapr   _stream_one
world_size	_next_oneranger7   _check_output)r   r:   r(   r6   r$   resoutr   r   r   _stream_singleI   s&   



z&DistributedGPT3Pipeline._stream_singlec                 C   s6   |d d  tj  }| jj|fi |d | _d S r&   )r)   r*   r+   r   stream_generate_streamr-   r   r   r   rA   ]   s   z#DistributedGPT3Pipeline._stream_oneidxc                 C   s"   zt | jW S  ty   Y d S w r   )nextrJ   StopIteration)r    rK   r   r   r   rC   d   s
   z!DistributedGPT3Pipeline._next_oner   )__name__
__module____qualname____doc__r   r   classmethodr#   r   strr   r/   r7   r9   r   rH   rA   intr   rC   __classcell__r   r   r   r   r      s.    
$




(r   )typingr   r   r   r   r*   modelscope.metainfor   modelscope.models.nlpr   modelscope.pipelines.baser   modelscope.pipelines.builderr	   modelscope.preprocessorsr
   modelscope.utils.constantr   r   modelscope.utils.devicer   !modelscope.utils.streaming_outputr   register_moduletext_generationgpt3_generationr   r   r   r   r   <module>   s    
