o
    ߥi                     @   s   d dl Z d dlm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mZ e Zejejejd	G d
d deZdS )    N)Models)MODELS)WordAlignmentOutput)logger)Tasks   )	BertModelBertPreTrainedModel)module_namec                       sH   e Zd ZdZdgZddgZ fddZ									dd
dZ  ZS )MBertForWordAlignmentaJ  MBert Model for the Word Alignment task.

    Code for EMNLP Findings 2022 paper, "Third-Party Aligner for Neural Word Alignments".
    https://arxiv.org/abs/2211.04198

    Parameters:
        config (:class:`~modelscope.models.nlp.structbert.SbertConfig`): Model configuration class with
            all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the :meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model
            weights.
    poolerposition_idszpredictions.decoder.biasc                    sD   t  | |jrtd |dd|_t|dd| _| 	  d S )NzkIf you want to use `BertForMaskedLM` make sure `config.is_decoder=False` for bi-directional self-attention.encoder_layers   F)add_pooling_layer)
super__init__
is_decoderr   warninggetnum_hidden_layersr   bert	post_init)selfconfigkwargs	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/bert/word_alignment.pyr   3   s   zMBertForWordAlignment.__init__NMbP?Fc	           !      C   s  t  f | j|| dddd}	| j|| dddd}
d|dk|dk@ |@ ddddddf   d }d|dk|dk@ |@ ddddddf   d }|	d }|
d }t ||dd	}W d   n1 smw   Y  |d| }|d|d
d }tjd
d|}tjdd|}||k||k }|	d}|dkj
d
dd
}|dkj
d
dd
}tjd
d|t |  }tjdd|t |  }g }tt|||D ]n\}\}}}t }t |}t| D ]U\}}|s1|| |d   || |d   f} | |vr|| ||f  || < qt||  || ||f  || < q|| ||f  || d | d f< q|| qt|dS )a  
        Args: src_input_ids:
            Indices of source input sequence tokens in the vocabulary.
        src_attention_mask:
            Source mask to avoid performing attention on padding token indices.
        src_b2w_map:
            Word order numner of subword in source sequence.
        tgt_input_ids:
            Indices of target input sequence tokens in the vocabulary.
        tgt_attention_mask:
            Target mask to avoid performing attention on padding token indices.
        tgt_b2w_map:
            Word order numner of subword in target sequence.
        threshold:
            The threshold used to extract alignment.
        bpe_level:
            Return subword-level alignment or not.
        Example:
            {
            'src_input_ids': LongTensor([[2478,242,24,4]]),
            'src_attention_mask': BoolTensor([[1,1,1,1]]),
            'src_b2w_map': LongTensor([[0,1,2,3]]),
            'tgt_input_ids': LongTensor([[1056,356,934,263,7]]),
            'tgt_attention_mask': BoolTensor([[1,1,1,1,1]]),
            'tgt_b2w_map': longtensor([[0,1,1,2,3]]),
            'threshold': 0.001,
            'bpe_level': False,
            }
        Returns `modelscope.outputs.WordAlignmentOutput`
        NT)	input_idsattention_mask	head_maskinputs_embedsoutput_hidden_statesr   e   f   ir      )dim)predictions)torchno_gradr   floatbmm	transpose	unsqueezennSoftmaxsqueezesumsqrt	enumeratezipdictwhereitemmaxappendr   )!r   src_input_idssrc_attention_masksrc_b2w_maptgt_input_idstgt_attention_masktgt_b2w_map	threshold	bpe_levelsrc_encoder_outtgt_encoder_outatten_mask_srcatten_mask_tgtsrc_align_outtgt_align_outbpe_simattention_scores_srcattention_scores_tgtattention_probs_srcattention_probs_tgtalign_matrixlen_srclen_tgtword_alignsidx
line_alignb2w_srcb2w_tgtalignsnon_specialsij	word_pairr   r   r   forwardA   s   
)








zMBertForWordAlignment.forward)NNNNNNr    F)	__name__
__module____qualname____doc__"_keys_to_ignore_on_load_unexpected_keys_to_ignore_on_load_missingr   r_   __classcell__r   r   r   r   r       s    r   )r-   torch.nnr3   torch.utils.checkpointmodelscope.metainfor   modelscope.models.builderr   modelscope.outputsr   modelscope.utilsr   loggingmodelscope.utils.constantr   backboner   r	   
get_loggerregister_moduleword_alignmentr   r   r   r   r   r   <module>   s   