o
    *j                     @   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)Zadd_pooling_layerr	   )super__init__configr   plmr   ZDropoutZhidden_dropout_probZdropoutZLinearZhidden_size	head_clsf	tail_clsfset_crossattention_layer)selfr   kwargs	__class__ g/var/www/html/Deteccion_Ine/venv/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   Z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torchZ
zeros_likecatZ	logsumexpmean)	r   Zy_predZy_trueZ
batch_sizeZ
y_pred_negZ
y_pred_posZzerosZneg_lossZ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'   r(   r   Zget_extended_attention_maskr%   r   )r   r+   r,   Zencoder_hidden_statesZencoder_attention_maskZcat_hidden_statesZ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'   Z	ones_liker   )r   	input_idsr,   r/   is_hintr.   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r1   r!   Zloss)r3   r%   r'   arangeexpandtodevicer-   r   squeezer   r*   )r   r0   attention_maskshint_idscross_attention_masksZhead_labelsZtail_labelsr2   r/   hint_sequence_outputhead_logitstail_logitsZ	loss_funcZ	head_lossZ	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	   r4   Tr5   r!   i)r'   r6   r%   r7   r8   r9   r3   r-   r   r:   r   )r   r2   r;   r<   r=   r/   r>   r?   r@   Z
head_probsZ
tail_probsr   r   r   fast_inference}   s$   zSiameseUieModel.fast_inference)r   )NF)__name__
__module____qualname____doc__r   r   r*   r-   r3   rA   rB   __classcell__r   r   r   r   r      s    

	

$)copyr   r'   r   Zmodelscope.metainfor   Zmodelscope.models.baser   Zmodelscope.models.builderr   Zmodelscope.utils.constantr   r   Zbackboner
   r   r   __all__Zregister_moduleZsiamese_uieZbertr   r   r   r   r   <module>   s   