
    9i|                        S SK r S SKJr  S SKJrJrJrJrJ	r	  S SK
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  S SKJrJr  S	/r\R6                  " \R8                  \R:                  S
9 " S S	\5      5       rg)    N)AnyDictListOptionalTuple)Tensor)Models)
TorchModel)MODELS)Config)	ModelFileTasksCanmtForTranslation)module_namec                   r   ^  \ rS rSrU 4S jrS rS rS\S\R                  4S jr
S\\\4   4S	 jrS
rU =r$ )r      c                   > [         TU ]  " SSU0UD6  X l        [        R                  " U[
        R                  5      n[        R                  " U5      U l	        SSK
Jn  UR                  [        R                  " US5      5      U l        UR                  [        R                  " US5      5      U l        U R                  U5      U l        U R#                  U R                   U R                  U R                  S   5      U l        g)	z
CanmtForTranslation implements a Competency-Aware Neural Machine Translaton,
which has both translation and self-estimation abilities.

For more details, please refer to https://aclanthology.org/2022.emnlp-main.330.pdf
	model_dirr   )
Dictionaryzdict.src.txtzdict.tgt.txtdecodeN )super__init__argsospjoinr   CONFIGURATIONr   	from_filecfgfairseq.datar   load	vocab_src	vocab_tgtbuild_modelmodelbuild_generator	generator)selfr   r   cfg_filer   	__class__s        m/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/nlp/canmt/canmt_translation.pyr   CanmtForTranslation.__init__   s     	5955	88Iy'>'>?##H-+#)^)LM#)^)LM%%i0
--djj$...2hhx.@B    c                     SSK Jn  U R                  [        R                  " U[
        R                  5      S5      nUS   nUR                  US   U 5      nUR                  US   US   S9  U$ )N   )
CanmtModelcpur   r%   )	model_cfg)	canmt_modelr0   load_checkpointr   r   r   TORCH_MODEL_FILEr$   load_state_dict)r(   r   r0   stater   r%   s         r+   r$   CanmtForTranslation.build_model*   sk    +$$HHY	 : :;UDEl&&s7|T:eGnGEr-   c                 ,    SSK Jn  U" UUUS   US   S9$ )Nr/   )SequenceGeneratorbeamlenpen)	beam_sizelen_penalty)sequence_generatorr:   )clsr%   r#   r   r:   s        r+   r&   #CanmtForTranslation.build_generator3   s&    9 6lX	( 	(r-   pathdevicec                 N    [         R                  " XS9nU R                  USS9  U$ )N)map_locationF)strict)torchr!   r6   )r(   rB   rC   
state_dicts       r+   r4   #CanmtForTranslation.load_checkpoint;   s)    ZZ:
Z6r-   inputc                 @    SU0nU R                   R                  U5      $ )a  return the result by the model

Args:
    input (Dict[str, Tensor]): the preprocessed data which contains following:
        - src_tokens: tensor with shape (2478,242,24,4),
        - src_lengths: tensor with shape (4)


Returns:
    Dict[str, Tensor]: results which contains following:
        - predictions: tokens need to be decode by tokenizer with shape [1377, 4959, 2785, 6392...]
	net_input)r'   generate)r(   rJ   s     r+   forwardCanmtForTranslation.forward@   s#     e$~~&&u--r-   )r   r   r'   r%   r"   r#   )__name__
__module____qualname____firstlineno__r   r$   r&   strrG   rC   r4   r   rN   __static_attributes____classcell__)r*   s   @r+   r   r      sC    B$(C  
.T#t)_ . .r-   )mathos.pathrB   r   typingr   r   r   r   r   numpyrG   torch.nnnnr   modelscope.metainfor	   modelscope.models.baser
   modelscope.models.builderr   modelscope.utils.configr   modelscope.utils.constantr   r   __all__register_modulecompetency_aware_translationcanmtr   r   r-   r+   <module>rf      sk      3 3     & - , * 6 
! 	&&FLLB8.* 8.B8.r-   