
    9i                         S SK Jr  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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  S/r\R0                  " \R2                  \	R4                  S9 " S S\\5      5       rg)    )OrderedDict)Dict	GeneratorN)BertTokenizer)Models)Tensor
TorchModel)MODELS)	GPT3Model)Tasks)read_config)StreamingOutputMixinGPT3ForTextGeneration)module_namec                      ^  \ rS rSrS\4U 4S jjrS\\\4   S\\\4   4S jrS\\\4   S\\\4   4S jr	\
S	\R                  S\4S
 j5       rU 4S jrSS jr SSSS\4S jjrS\4S jrSrU =r$ )r      	model_dirc                    > [         TU ]  " U/UQ70 UD6  S[        U5      ;   a  SSKJn  U" U40 UD6U l        g[        R                  " U5      U l        [        R                  " U5      U l	        g)zlinitialize the text generation model from the `model_dir` path.

Args:
    model_dir (str): the model path.
megatronr   )DistributedGPT3N)
super__init__r   modelscope.models.nlpr   modelr   from_pretrainedr   	tokenizer)selfr   argskwargsr   	__class__s        j/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/nlp/gpt3/text_generation.pyr   GPT3ForTextGeneration.__init__   sc     	4T4V4
 Y//=(=f=DJ"229=DJ*::9EDN    inputreturnc                 &    U R                   " S0 UD6$ )zreturn the result by the model

Args:
    input (Dict[str, Tensor]): the preprocessed data

Returns:
    Dict[str, Tensor]: results

Example:
    >>> {
    >>>     'logits': Tensor([[0.54, 0.32...])]), # logits
    >>> }
 )r   )r   r$   s     r!   forwardGPT3ForTextGeneration.forward(   s     zz"E""r#   inputsc                     [        U R                  [        5      (       d  U R                  R                  " S0 UDUD6$ US   nU R	                  US   5      nU R                  R                  " U4SU0UD6$ )N	input_idsattention_maskprompt_lengthr'   )
isinstancer   r   generate_get_lengthr   r*   r   tokenslengthss        r!   r0   GPT3ForTextGeneration.generate8   so    $**i00::&&::6::$""6*:#;<zz""6KKFKKr#   r-   c                 *    U R                  S5      S-
  $ )N   )sum)r-   s    r!   r1   !GPT3ForTextGeneration._get_lengthA   s    !!"%))r#   c                    > [        U R                  [        5      (       d  U R                  R                  " U0 UD6$ [        TU ]  " U0 UD6$ N)r/   r   r   save_pretrainedr   )r   r   r   r    s      r!   r=   %GPT3ForTextGeneration.save_pretrainedE   sC    $**i00::--t>v>>w&777r#   c                 :    U R                   R                  XU5      $ r<   )r   
state_dict)r   destinationprefix	keep_varss       r!   r@    GPT3ForTextGeneration.state_dictJ   s    zz$$[)DDr#   r@   zOrderedDict[str, Tensor]strictc                 8    U R                   R                  X5      $ r<   )r   load_state_dict)r   r@   rE   s      r!   rG   %GPT3ForTextGeneration.load_state_dictM   s     zz))*==r#   c                 r    US   nU R                  US   5      nU R                  R                  " U4SU0UD6$ )Nr,   r-   r.   )r1   r   streaming_generater2   s        r!   stream_generate%GPT3ForTextGeneration.stream_generateR   sL    $""6*:#;<zz,,5")5-35 	5r#   )r   r   )N F)T)__name__
__module____qualname____firstlineno__strr   r   r   r(   r0   staticmethodtorchr1   r=   r@   boolrG   r   rK   __static_attributes____classcell__)r    s   @r!   r   r      s    F# F$#T#v+. #4V3D # LtCK0 L"3;/L *ELL *V * *8
E
 (,>$>> $>
59 5 5r#   )collectionsr   typingr   r   rT   transformersr   modelscope.metainfor   modelscope.models.baser   r	   modelscope.models.builderr
   modelscope.models.nlp.gpt3r   modelscope.utils.constantr   modelscope.utils.hubr   !modelscope.utils.streaming_outputr   __all__register_moduletext_generationgpt3r   r'   r#   r!   <module>rf      sg    # "  & & 5 , 0 + , B"
# --6;;GB5J(< B5 HB5r#   