
    9i                        S SK Jr  S SKJrJr  S SKrS SKJrJr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  \" 5       rS/r\R2                  " \R4                  \R6                  S9 " S S\\5      5       rg)    )OrderedDict)Dict	GeneratorN)
AutoConfigAutoModelForCausalLMAutoTokenizer)Models)Tensor
TorchModel)MODELS)Tasks)
get_logger)StreamingOutputMixinPolyLMForTextGeneration)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
U =r$ )	r      	model_dirc                 
  > [         TU ]  " U/UQ70 UD6  [        R                  " USSS9U l        U R                  SU S3US9  [        R                  " USU R                  S9U l        U R                  R                  5         g)	zlinitialize the text generation model from the `model_dir` path.

Args:
    model_dir (str): the model path.
F)legacyuse_fastz3Use trust_remote_code=True. Will invoke codes from z9. Please make sure that you can trust the external codes.)info_strr   auto)
device_maptrust_remote_codeN)
super__init__r   from_pretrained	tokenizercheck_trust_remote_coder   r   modeleval)selfr   argskwargs	__class__s       l/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/nlp/polylm/text_generation.pyr    PolyLMForTextGeneration.__init__   s     	4T4V4&66ee5 	$$A) M5 5	 	% 	! *99"446
 	

    inputreturnc                 ,    U R                   " U40 UD6nU$ )zreturn the result by the model

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

Returns:
    Dict[str, Tensor]: results
)generate)r#   r*   r%   ress       r'   forwardPolyLMForTextGeneration.forward-   s     mmE,V,
r)   c                 .   U R                   R                  nU R                  USS9nU R                   R                  " UR                  R                  U5      4SUR                  R                  U5      0UD6nU R                  R                  US   SS9nU$ )Npt)return_tensorsattention_maskr   T)skip_special_tokens)r!   devicer   r-   	input_idstor4   decode)r#   r*   r%   r6   inputsoutputspreds          r'   r-    PolyLMForTextGeneration.generate9   s    ""d;**%%'!0033F;  ~~$$WQZT$Jr)   )r!   r   )__name__
__module____qualname____firstlineno__strr   r   r
   r/   r-   __static_attributes____classcell__)r&   s   @r'   r   r      s_    # ,
T#v+. 
T#v+=N 
	d3;/ 	"3;/	 	r)   )collectionsr   typingr   r   torchtransformersr   r   r   modelscope.metainfor	   modelscope.models.baser
   r   modelscope.models.builderr   modelscope.utils.constantr   modelscope.utils.loggerr   !modelscope.utils.streaming_outputr   logger__all__register_moduletext_generationpolylmr    r)   r'   <module>rU      sk    # "  H H & 5 , + . B	$
% --6==I-j*> - J-r)   