o
    ߥi                     @   s   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mZ dd	lmZmZmZ d
gZe	jejejdG dd
 d
eZdS )    )deepcopyN)nn)Models)
TorchModel)MODELS)	ModelFileTasks   )BertEncoder	BertModelBertPreTrainedModelSiameseUieModel)module_namec                       sX   e Zd ZdZ fddZdddZdd Zd	d
 Z		dddZdd Z	dd Z
  ZS )r   a  SiameseUIE general information extraction model,
        based on the construction idea of prompt (Prompt) + text (Text),
        uses pointer network (Pointer Network) to
        realize segment extraction (Span Extraction), so as to
        realize named entity recognition (NER), relation extraction (RE),
        Extraction of various tasks such as event extraction (EE),
        attribute sentiment extraction (ABSA), etc. Different from
        the existing general information extraction tasks on the market:
    c                    s\   t  | || _t| jdd| _t|j| _t	|j
d| _t	|j
d| _|   d S )NT)add_pooling_layerr	   )super__init__configr   plmr   Dropouthidden_dropout_probdropoutLinearhidden_size	head_clsf	tail_clsfset_crossattention_layer)selfr   kwargs	__class__ Z/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/bert/siamese_uie.pyr      s   zSiameseUieModel.__init__   c                 C   sd   t | j}||_| j j|8  _t|| _| jjj| jjd  | j_| jjjd | jj | jj_d S )N)r   r   num_hidden_layersr
   crossattentionr   encoderlayer)r   r#   crossattention_configr    r    r!   r   %   s   

z(SiameseUieModel.set_crossattention_layerc           	      C   s   | d}||d}||d}dd|  | }||d  }|d| d  }t|d d d df }tj||gdd}tj||gdd}tj|dd}tj|dd}||  S )Nr   r	      g   mBdim)sizeviewtorch
zeros_likecat	logsumexpmean)	r   y_predy_true
batch_size
y_pred_neg
y_pred_poszerosneg_losspos_lossr    r    r!   circle_loss/   s   
zSiameseUieModel.circle_lossc                 C   sr   t j||gdd}t j||gdd}| j|| d d }| j||dd d d d | d d d f }|S )Nr	   r*   r)   )hidden_statesattention_maskr   )r.   r0   r   get_extended_attention_maskr,   r$   )r   r<   r=   encoder_hidden_statesencoder_attention_maskcat_hidden_statescat_attention_maskr    r    r!   get_cross_attention_output=   s$   
z*SiameseUieModel.get_cross_attention_outputNFc                 C   s,   |rt |nd }| j||||dd }|S )N)r=   token_type_idsposition_idsr   )r.   	ones_liker   )r   	input_idsr=   rE   is_hintrD   sequence_outputr    r    r!   get_plm_sequence_outputL   s   z'SiameseUieModel.get_plm_sequence_outputc                 C   s   |  ||}|d|d dksJ t|dd|d }||j}| j |||dd}	| |||	|}| |	d}
| 
|	d}| j}||
|}|||}d|| iS )a  train forward

        Args:
            input_ids (Tensor): input token ids of text.
            attention_masks (Tensor): attention_masks of text.
            hint_ids (Tensor): input token ids of prompt.
            cross_attention_masks (Tensor): attention_masks of prompt.
            head_labels (Tensor): labels of start position.
            tail_labels (Tensor): labels of end position.

        Returns:
            Dict[str, float]: the loss
            Example:
            {"loss": 0.5091743}
        r	   i   r	   r(   TrH   r(   loss)rJ   r,   r.   arangeexpandtodevicerC   r   squeezer   r;   )r   rG   attention_maskshint_idscross_attention_maskshead_labelstail_labelsrI   rE   hint_sequence_outputhead_logitstail_logits	loss_func	head_loss	tail_lossr    r    r!   forwardY   s.   

zSiameseUieModel.forwardc                 C   s   t |dd|d }||j}| j|||dd}| ||||}| |	d}| 
|	d}|d| d  }	|d| d  }
|	|
fS )ao  

        Args:
            sequence_output(tensor): 3-dimension tensor (batch size, sequence length, hidden size)
            attention_masks(tensor): attention mask, 2-dimension tensor (batch size, sequence length)
            hint_ids(tensor): token ids of prompt 2-dimension tensor (batch size, sequence length)
            cross_attention_masks(tensor): cross attention mask, 2-dimension tensor (batch size, sequence length)
        Default Returns:
            head_probs(tensor): 2-dimension tensor(batch size, sequence length)
            tail_probs(tensor): 2-dimension tensor(batch size, sequence length)
        r	   rK   TrL   r(   i)r.   rN   r,   rO   rP   rQ   rJ   rC   r   rR   r   )r   rI   rS   rT   rU   rE   rX   rY   rZ   
head_probs
tail_probsr    r    r!   fast_inference}   s$   zSiameseUieModel.fast_inference)r"   )NF)__name__
__module____qualname____doc__r   r   r;   rC   rJ   r^   ra   __classcell__r    r    r   r!   r      s    

	

$)copyr   r.   r   modelscope.metainfor   modelscope.models.baser   modelscope.models.builderr   modelscope.utils.constantr   r   backboner
   r   r   __all__register_modulesiamese_uiebertr   r    r    r    r!   <module>   s   