o
    ߥi|                     @   s   d dl Z d dlmZ d dlmZmZmZmZm	Z	 d dl
Z
d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d	gZejejejd
G dd	 d	eZdS )    N)AnyDictListOptionalTuple)Tensor)Models)
TorchModel)MODELS)Config)	ModelFileTasksCanmtForTranslation)module_namec                       sV   e Zd Z fddZdd Zdd Zdedejfd	d
Z	de
ee
f fddZ  ZS )r   c                    s   t  jdd|i| || _t|tj}t|| _	ddl
m} |t|d| _|t|d| _| || _| | j| j| j	d | _dS )	a  
            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__r   a/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/canmt/canmt_translation.pyr      s   
zCanmtForTranslation.__init__c                 C   sT   ddl m} | t|tjd}|d }||d | }|j|d |d d |S )N   )
CanmtModelcpur   r!   )	model_cfg)	canmt_modelr*   load_checkpointr   r   r   TORCH_MODEL_FILEr    load_state_dict)r$   r   r*   stater   r!   r   r   r(   r    *   s   zCanmtForTranslation.build_modelc                 C   s$   ddl m} ||||d |d dS )Nr)   )SequenceGeneratorbeamlenpen)	beam_sizelen_penalty)sequence_generatorr2   )clsr!   r   r   r2   r   r   r(   r"   3   s   z#CanmtForTranslation.build_generatorpathdevicec                 C   s    t j||d}| j|dd |S )N)map_locationF)strict)torchr   r0   )r$   r9   r:   
state_dictr   r   r(   r.   ;   s   z#CanmtForTranslation.load_checkpointinputc                 C   s   d|i}| j |S )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$   r?   r   r   r(   forward@   s   zCanmtForTranslation.forward)__name__
__module____qualname__r   r    r"   strr=   r:   r.   r   rB   __classcell__r   r   r&   r(   r      s    	)mathos.pathr9   r   typingr   r   r   r   r   numpyr=   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   r(   <module>   s"   