
    9iI                         S SK JrJr  S SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJr  S SKJr  \R                   " \R"                  \R$                  S	9 " S
 S\	5      5       rg)    )AnyDictN)	Pipelines)DistributedPlug)DistributedPipeline)	PIPELINES)&TextGenerationTransformersPreprocessor)Tasks)module_namec                      ^  \ rS rSrSrSr   SU 4S jjr\S\\	\
4   S\\	\
4   4S j5       rS rS\\	\
4   S\\	\
4   4U 4S	 jjr\S
 5       rS\\	\
4   S\\	\	4   4S jrSrU =r$ )DistributedPlugPipeline   z6This class is used to instantiate the plug model.
    Nc                    > Uc  [        U4UUS.UD6n[        TU ]  " U4SU0UD6  UR                  R                  R
                  U l        g)aL  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__s         r/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/nlp/distributed_plug_pipeline.pyr    DistributedPlugPipeline.__init__   sa    L A- / 	L
 	D\DVD(66@@MM    inputsreturnc                     [         R                  " 5          U R                  R                  " US   40 US   D6sS S S 5        $ ! , (       d  f       g = f)Nr   forward_params)torchno_gradr   generate)clsr   s     r   _forward_one$DistributedPlugPipeline._forward_oneF   s>    ]]_99%%fX&6 B(./?(@B __s   #A
Ac                     0 U0 4$ N )r   pipeline_parameterss     r   _sanitize_parameters,DistributedPlugPipeline._sanitize_parametersL   s    &**r   c                    > US   R                   S   n[        R                  " US/U R                  [        R                  S9nXAS'   [
        TU ]  " U40 UD6nU$ )N	input_idsr      )dtypedec_input_ids)shaper#   fullr   longr   forward)r   r   r"   
batch_sizer3   resr   s         r   r7   DistributedPlugPipeline.forwardO   s^    K(..q1


J?#'#4#4).5 #0gof77
r   c                 Z    [        X!40 UD6U l        U R                  R                  5         g r*   )r   r   eval)r&   rank	model_dirr   s       r   _instantiate_one(DistributedPlugPipeline._instantiate_oneY   s!    #I>v>			r   c                     SSK Jn  US   nSR                  U R                  R                  R
                  R                  U5      5      R                  SS5      R                  SS5      nUR                  U0$ )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.outputsrB   joinr   r   r   convert_ids_to_tokensreplaceTEXT)r   r   postprocess_paramsrB   rC   s        r   postprocess#DistributedPlugPipeline.postprocess^   sp     	2!"4577++55KK "##*77E#:774;L 	 !122r   )r   )Nsentencei   )__name__
__module____qualname____firstlineno____doc__r   r   classmethodr   strr   r'   r-   r7   r?   rK   __static_attributes____classcell__)r   s   @r   r   r      s     E # *!$	-N^ B$sCx. BT#s(^ B B
+d38n %)#s(^  3$sCx. 3-1#s(^3 3r   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   <module>ra      s\      ) 6 9 2 K + 	y'@'@B\31 \3B\3r   