
    9i*                         S SK JrJr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Jr  S S	KJr  S S
KJr  \R.                  " \R0                  \R2                  S9 " S S\\5      5       rg)    )AnyDict	GeneratorOptionalN)	Pipelines)DistributedGPT3)DistributedPipeline)	PIPELINES)TextGenerationJiebaPreprocessor)
FrameworksTasks)device_placement)PipelineStreamingOutputMixin)module_namec                   *  ^  \ rS rSrSrSrSU 4S jjr\S 5       r\S\	\
\4   S\	\
\4   4S j5       rS\	\
\4   S\	\
\
4   4S	 jrS
 rS\	\
\4   S\	\
\4   S\	\
\4   S\4S jr\S\	\
\4   SS4S j5       r\S\S\\	\
\4      4S j5       rSrU =r$ )DistributedGPT3Pipeline   z6This class is used to instantiate the gpt3 model.
    Nc                    > Uc  [        U5      n[        TU ]  " U4SU0UD6  [        US5      (       d   e[	        5       U l        SU l        g)z

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__s       r/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/nlp/distributed_gpt3_pipeline.pyr    DistributedGPT3Pipeline.__init__   sQ     :5ALD\DVD|[111113
"    c                 Z    [        X!40 UD6U l        U R                  R                  5         g N)r   r   eval)clsrank	model_dirr   s       r   _instantiate_one(DistributedGPT3Pipeline._instantiate_one*   s!    #I>v>			r!   inputsreturnc                     US   S   R                  [        R                   R                  5       5      nU R                  R                  " U40 US   D6$ Nr*   	input_idsforward_params)cudatorchcurrent_devicer   generater%   r*   tokenss      r   _forward_one$DistributedGPT3Pipeline._forward_one/   sL    !+.33JJ%%')yy!!&EF3C,DEEr!   c                     SSK Jn  UR                  U R                  R                  R                  UR                  S   R                  5       5      0$ )zprocess the prediction results

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

Returns:
    Dict[str, str]: the prediction results
r   )
OutputKeys)modelscope.outputsr9   TEXTr   r   
detokenize	sequencestolist)r   r*   postprocess_paramsr9   s       r   postprocess#DistributedGPT3Pipeline.postprocess5   sI     	2OO''22  #**,.
 	
r!   c                     0 U0 4$ r#    )r   pipeline_parameterss     r   _sanitize_parameters,DistributedGPT3Pipeline._sanitize_parametersF   s    &**r!   model_inputr/   r?   c              #   6  #    [        U R                  U R                  5         U R                  (       a  U R	                  U5      nXS.nU R
                  R                  U R                  R                  U/U R                  -  5        S S S 5         U R
                  R                  U R                  R                  [        U R                  5      5      nUS   c  g U R                  " US   40 UD6nU R                  U5        Uv   Mw  ! , (       d  f       N= f7f)N)r*   r/   r   )r   	frameworkdevice_name_auto_collate_collate_fn
model_poolmapr   _stream_one
world_size	_next_oneranger@   _check_output)r   rG   r/   r?   r*   resouts          r   _stream_single&DistributedGPT3Pipeline._stream_singleI   s      dnnd.>.>?!!"..{; +NFOO : :!'4?? :<	 @ //%%dnn&>&>&+DOO&<>C1v~""3q6@-?@Cs#I  @?s   !DA%DB D
DDc                     US   S   R                  [        R                   R                  5       5      nU R                  R                  " U40 US   D6U l        g r-   )r0   r1   r2   r   stream_generate_streamr4   s      r   rO   #DistributedGPT3Pipeline._stream_one]   sT    !+.33JJ%%')ii// L289I2JLr!   idxc                 N     [        U R                  5      $ ! [         a     g f = fr#   )nextrZ   StopIteration)r%   r\   s     r   rQ   !DistributedGPT3Pipeline._next_oned   s(    	$$ 		s    
$$)r   r   r#   )__name__
__module____qualname____firstlineno____doc__r   r   classmethodr(   r   strr   r6   r@   rE   r   rV   rO   intr   rQ   __static_attributes____classcell__)r   s   @r   r   r      s     E#    F$sCx. FT#s(^ F F

$sCx. 
-1#s(^
"+$sCx. '+CH~+/S>>G( Lc3h LD L L C HT#s(^$<  r!   r   )typingr   r   r   r   r1   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   rC   r!   r   <module>rw      sh    2 1  ) 1 9 2 D 7 4 J 	y'@'@BV1:VBVr!   