o
    ॵiI                     @   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)DistributedPlug)DistributedPipeline)	PIPELINES)&TextGenerationTransformersPreprocessor)Tasks)module_namec                       s   e Zd ZdZdZ			d fdd	Ze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 f fddZedd Zdeee	f deeef fddZ  ZS )DistributedPlugPipelinez6This class is used to instantiate the plug model.
    Nsentence   c                    sF   |du rt |f||d|}t j|fd|i| |jjj| _dS )a4  Create a plug pipeline instance.

        Args:
        model: The model_id of plug(damo/nlp_plug_text-generation_27B).
        The default path to damo/nlp_plug_text-generation_27B can be obtained by function
        get_cache_dir("damo/nlp_plug_text-generation_27B"), the model should be downloaded to
        this path before calling this class by model_id.
        The model can be downloaded from the link on
        https://modelscope.cn/models/damo/nlp_plug_text-generation_27B/summary.
        After downloading, you should have a plug model structure like this:
        /your/path/to/damo/nlp_plug_text-generation_27B
            |_ config.json
            |_ configuration.json
            |_ ds_zero-offload_10B_config.json
            |_ vocab.txt
            |_ model <-- an empty directory

        Model binaries shall be downloaded separately to populate the model directory, so that
        the model directory would contain the following binaries:
            |_ model
                |_ mp_rank_00_model_states.pt
                |_ mp_rank_01_model_states.pt
                |_ mp_rank_02_model_states.pt
                |_ mp_rank_03_model_states.pt
                |_ mp_rank_04_model_states.pt
                |_ mp_rank_05_model_states.pt
                |_ mp_rank_06_model_states.pt
                |_ mp_rank_07_model_states.pt
        preprocessor: The optional preprocessor, if not passed in, a TextGenerationPreprocessor will
            be used as default.
        kwargs (dict, `optional`): Extra kwargs passed into the preprocessor's constructor.
        N)first_sequencesequence_lengthpreprocessor)r   super__init__nlp_tokenizer	tokenizercls_token_id)selfmodelr   r   r   kwargs	__class__ f/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/distributed_plug_pipeline.pyr      s   &z DistributedPlugPipeline.__init__inputsreturnc                 C   sH   t   | jj|d fi |d W  d    S 1 sw   Y  d S )Nr   forward_params)torchno_gradr   generate)clsr   r   r   r   _forward_oneF   s
   
$z$DistributedPlugPipeline._forward_onec                 K   s
   i |i fS Nr   )r   pipeline_parametersr   r   r   _sanitize_parametersL   s   
z,DistributedPlugPipeline._sanitize_parametersc                    sF   |d j d }tj|dg| jtjd}||d< t j|fi |}|S )N	input_idsr      )dtypedec_input_ids)shaper    fullr   longr   forward)r   r   r   
batch_sizer+   resr   r   r   r/   O   s   
zDistributedPlugPipeline.forwardc                 K   s"   t ||fi || _| j  d S r%   )r   r   eval)r#   rank	model_dirr   r   r   r   _instantiate_oneY   s   z(DistributedPlugPipeline._instantiate_onec                 K   sD   ddl m} |d }d| jjj|dddd}|j|iS )zprocess the prediction results

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

        Returns:
            Dict[str, str]: the prediction results
        r   )
OutputKeysgenerate_context z[UNK]u   “z##)	modelscope.outputsr6   joinr   r   r   convert_ids_to_tokensreplaceTEXT)r   r   postprocess_paramsr6   r7   r   r   r   postprocess^   s   


z#DistributedPlugPipeline.postprocess)Nr   r   )__name__
__module____qualname____doc__r   r   classmethodr   strr   r$   r'   r/   r5   r?   __classcell__r   r   r   r   r      s$    /$



r   )typingr   r   r    modelscope.metainfor   modelscope.models.nlp.plugr   modelscope.pipelines.baser   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr	   register_moduletext_generationplug_generationr   r   r   r   r   <module>   s   