a
    hb                     @   sL  d Z ddlZddlmZ ddl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mZmZ ddlmZ ddlmZmZmZ ddlmZmZmZmZ ddlm Z  e!e"Z#eeddG dd deZ$G dd de	j%Z&G dd de	j%Z'G dd de	j%Z(G dd de	j%Z)G dd de	j%Z*G dd de	j%Z+G d d! d!e	j%Z,G d"d# d#e	j%Z-G d$d% d%e	j%Z.G d&d' d'eZ/G d(d) d)e	j%Z0G d*d+ d+e	j%Z1G d,d- d-e	j%Z2eG d.d/ d/eZ3eG d0d1 d1e3Z4eG d2d3 d3e3Z5ed4dG d5d6 d6e3Z6ed7dG d8d9 d9e3Z7g d:Z8dS );zPyTorch Bros model.    N)	dataclass)OptionalUnion)nn)CrossEntropyLoss   )ACT2FN)GradientCheckpointingLayer)"BaseModelOutputWithCrossAttentions,BaseModelOutputWithPoolingAndCrossAttentionsTokenClassifierOutput)PreTrainedModel)apply_chunking_to_forward find_pruneable_heads_and_indicesprune_linear_layer)ModelOutputauto_docstringcan_return_tuplelogging   )
BrosConfigz@
    Base class for outputs of token classification models.
    )Zcustom_introc                   @   st   e Zd ZU dZdZeej ed< dZ	eej ed< dZ
eej ed< dZeeej  ed< dZeeej  ed< dS )BrosSpadeOutputa  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Classification loss.
    initial_token_logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.num_labels)`):
        Classification scores for entity initial tokens (before SoftMax).
    subsequent_token_logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, sequence_length+1)`):
        Classification scores for entity sequence tokens (before SoftMax).
    Nlossinitial_token_logitssubsequent_token_logitshidden_states
attentions)__name__
__module____qualname____doc__r   r   torchFloatTensor__annotations__r   r   r   tupler    r%   r%   b/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/models/bros/modeling_bros.pyr   *   s   
	r   c                       s0   e Zd Z fddZejejdddZ  ZS )BrosPositionalEmbedding1Dc                    s@   t    |j| _ddtd| jd| j   }| d| d S )Nr   i'          g       @inv_freq)super__init__dim_bbox_sinusoid_emb_1dr!   arangeregister_buffer)selfconfigr)   	__class__r%   r&   r+   D   s    
z"BrosPositionalEmbedding1D.__init__)pos_seqreturnc                 C   sX   |  }|\}}}||||d| jddd| jd  }tj| | gdd}|S )Nr      dim)sizeviewr)   r,   r!   catsincos)r/   r3   Zseq_sizeb1b2Zb3Zsinusoid_inpZpos_embr%   r%   r&   forwardN   s
    
(z!BrosPositionalEmbedding1D.forwardr   r   r   r+   r!   Tensorr@   __classcell__r%   r%   r1   r&   r'   A   s   
r'   c                       s0   e Zd Z fddZejejdddZ  ZS )BrosPositionalEmbedding2Dc                    s*   t    |j| _t|| _t|| _d S N)r*   r+   dim_bboxr'   	x_pos_emb	y_pos_embr/   r0   r1   r%   r&   r+   W   s    

z"BrosPositionalEmbedding2D.__init__)bboxr4   c                 C   sd   g }t | jD ]B}|d dkr8|| |d|f  q|| |d|f  qtj|dd}|S )Nr5   r   .r6   r7   )rangerF   appendrG   rH   r!   r;   )r/   rJ   stackibbox_pos_embr%   r%   r&   r@   ^   s    z!BrosPositionalEmbedding2D.forwardrA   r%   r%   r1   r&   rD   V   s   rD   c                       s,   e Zd Z fddZejdddZ  ZS )BrosBboxEmbeddingsc                    s.   t    t|| _tj|j|jdd| _d S )NF)bias)	r*   r+   rD   bbox_sinusoid_embr   LinearZdim_bbox_sinusoid_emb_2dZdim_bbox_projectionbbox_projectionrI   r1   r%   r&   r+   j   s    

zBrosBboxEmbeddings.__init__)rJ   c                 C   s\   | dd}|d d d d d d d f |d d d d d d d f  }| |}| |}|S )Nr   r   )	transposerR   rT   )r/   rJ   Zbbox_tZbbox_posrO   r%   r%   r&   r@   o   s
    8

zBrosBboxEmbeddings.forwardrA   r%   r%   r1   r&   rP   i   s   rP   c                       sR   e Zd ZdZ fddZdeej eej eej eej ejdddZ  Z	S )	BrosTextEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                    s   t    tj|j|j|jd| _t|j|j| _	t|j
|j| _tj|j|jd| _t|j| _t|dd| _| dt|jd | jdtj| j tj| jjdd	d
 d S )N)padding_idxepsposition_embedding_typeabsoluteposition_ids)r   r6   token_type_idsdtypedeviceF)
persistent)r*   r+   r   	EmbeddingZ
vocab_sizehidden_sizeZpad_token_idword_embeddingsmax_position_embeddingsposition_embeddingsZtype_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsDropouthidden_dropout_probdropoutgetattrrZ   r.   r!   r-   expandzerosr\   r9   longr`   rI   r1   r%   r&   r+   {   s"    
zBrosTextEmbeddings.__init__N)	input_idsr]   r\   inputs_embedsr4   c                 C   s   |d ur|  }n|  d d }|d }|d u rH| jd d d |f }|d u rt| dr| jd d d |f }||d |}|}ntj|tj| jjd}|d u r| 	|}| 
|}	||	 }
| jdkr| |}|
|7 }
| |
}
| |
}
|
S )Nr6   r   r]   r   r^   r[   )r9   r\   hasattrr]   rn   r!   ro   rp   r`   rd   rg   rZ   rf   rh   rl   )r/   rq   r]   r\   rr   input_shape
seq_lengthbuffered_token_type_ids buffered_token_type_ids_expandedrg   
embeddingsrf   r%   r%   r&   r@      s,    







zBrosTextEmbeddings.forward)NNNN)
r   r   r   r    r+   r   r!   rB   r@   rC   r%   r%   r1   r&   rV   x   s       rV   c                
       sb   e Zd Z fddZdejejeej eej eej eej eej eej dddZ  Z	S )	BrosSelfAttentionc                    s   t    |j|j dkr>t|ds>td|j d|j d|j| _t|j|j | _| j| j | _t	
|j| j| _t	
|j| j| _t	
|j| j| _t	|j| _t|dd| _| jdks| jd	kr|j| _t	d
|j d | j| _|j| _d S )Nr   Zembedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()rZ   r[   relative_keyrelative_key_queryr5   r   )r*   r+   rc   num_attention_headsrs   
ValueErrorintattention_head_sizeall_head_sizer   rS   querykeyvaluerj   Zattention_probs_dropout_probrl   rm   rZ   re   rb   distance_embedding
is_decoderrI   r1   r%   r&   r+      s&    

zBrosSelfAttention.__init__NFr   rO   attention_mask	head_maskencoder_hidden_statesencoder_attention_maskoutput_attentionsr4   c                 C   s  |j d d| j| jf}| ||dd}	|d u}
|
rp| ||dd}| ||dd}|}n0| ||dd}| ||dd}t	|	|dd}| j
dks| j
dkr| d }tj|tj|jddd}tj|tj|jddd}|| }| || j d }|j|	jd	}| j
dkr\td
|	|}|| }n4| j
dkrtd
|	|}td||}|| | }|	j \}}}}|||||}|g d}td|	|f}|| }|t| j }|d ur|| }tjdd|}| |}|d ur"|| }t	||}|dddd }| d d | jf }|j| }|rr||fn|f}| jr|d }|S )Nr   r6   r   r5   r{   r|   r^   )r_   zbhld,lrd->bhlrzbhrd,lrd->bhlr)r5   r   r   r   zbnid,bijd->bnijr7   r   rE   )shaper}   r   r   r:   rU   r   r   r!   matmulrZ   r9   r-   rp   r`   r   re   tor_   Zeinsumpermutemathsqrtr   ZSoftmaxrl   
contiguousr   r   )r/   r   rO   r   r   r   r   r   Zhidden_shapequery_layerZis_cross_attention	key_layerZvalue_layerZattention_scoresru   Zposition_ids_lZposition_ids_rZdistanceZpositional_embeddingZrelative_position_scoresZrelative_position_scores_queryZrelative_position_scores_key
batch_sizeZn_headZd_headZbbox_pos_scoresZattention_probsZcontext_layerZnew_context_layer_shapeoutputsr%   r%   r&   r@      sX    





zBrosSelfAttention.forward)NNNNF)
r   r   r   r+   r!   rB   r   r$   r@   rC   r%   r%   r1   r&   ry      s         ry   c                       s4   e Zd Z fddZejejejdddZ  ZS )BrosSelfOutputc                    sB   t    t|j|j| _tj|j|jd| _t|j	| _
d S NrX   )r*   r+   r   rS   rc   denserh   ri   rj   rk   rl   rI   r1   r%   r&   r+   '  s    
zBrosSelfOutput.__init__r   input_tensorr4   c                 C   s&   |  |}| |}| || }|S rE   r   rl   rh   r/   r   r   r%   r%   r&   r@   -  s    

zBrosSelfOutput.forwardrA   r%   r%   r1   r&   r   &  s   r   c                
       sh   e Zd Z fddZdd Zd
ejejeej eej eej eej ee e	ej ddd	Z
  ZS )BrosAttentionc                    s*   t    t|| _t|| _t | _d S rE   )r*   r+   ry   r/   r   outputsetpruned_headsrI   r1   r%   r&   r+   5  s    


zBrosAttention.__init__c                 C   s   t |dkrd S t|| jj| jj| j\}}t| jj|| j_t| jj|| j_t| jj	|| j_	t| j
j|dd| j
_| jjt | | j_| jj| jj | j_| j|| _d S )Nr   r   r7   )lenr   r/   r}   r   r   r   r   r   r   r   r   r   union)r/   headsindexr%   r%   r&   prune_heads;  s    zBrosAttention.prune_headsNFr   c              	   C   s>   | j |||||||d}| |d |}	|	f|dd   }
|
S )Nr   rO   r   r   r   r   r   r   r   )r/   r   )r/   r   rO   r   r   r   r   r   Zself_outputsattention_outputr   r%   r%   r&   r@   P  s    
	zBrosAttention.forward)NNNNF)r   r   r   r+   r   r!   rB   r   boolr$   r@   rC   r%   r%   r1   r&   r   4  s"        r   c                       s0   e Zd Z fddZejejdddZ  ZS )BrosIntermediatec                    sB   t    t|j|j| _t|jt	r6t
|j | _n|j| _d S rE   )r*   r+   r   rS   rc   intermediate_sizer   
isinstanceZ
hidden_actstrr   intermediate_act_fnrI   r1   r%   r&   r+   j  s
    
zBrosIntermediate.__init__r   r4   c                 C   s   |  |}| |}|S rE   )r   r   )r/   r   r%   r%   r&   r@   r  s    

zBrosIntermediate.forwardrA   r%   r%   r1   r&   r   i  s   r   c                       s4   e Zd Z fddZejejejdddZ  ZS )
BrosOutputc                    sB   t    t|j|j| _tj|j|jd| _t	|j
| _d S r   )r*   r+   r   rS   r   rc   r   rh   ri   rj   rk   rl   rI   r1   r%   r&   r+   y  s    
zBrosOutput.__init__r   c                 C   s&   |  |}| |}| || }|S rE   r   r   r%   r%   r&   r@     s    

zBrosOutput.forwardrA   r%   r%   r1   r&   r   x  s   r   c                
       sh   e Zd Z fddZd
ejejeej eej eej eej ee e	ej dddZ
dd	 Z  ZS )	BrosLayerc                    sn   t    |j| _d| _t|| _|j| _|j| _| jrV| jsLt|  dt|| _	t
|| _t|| _d S )Nr   z> should be used as a decoder model if cross attention is added)r*   r+   chunk_size_feed_forwardseq_len_dimr   	attentionr   add_cross_attention	Exceptioncrossattentionr   intermediater   r   rI   r1   r%   r&   r+     s    



zBrosLayer.__init__NFr   c                 C   s   | j |||||d}|d }	| jr0|dd }
n|dd  }
| jr|d urt| drdtd|  d| j|	|||||d}|d }	|
|dd  }
t| j| j| j|	}|f|
 }
| jr|
d	 }
|
S )
N)rO   r   r   r   r   r   r6   r   z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`)r   r   r   r   r   rE   )	r   r   rs   r   r   r   feed_forward_chunkr   r   )r/   r   rO   r   r   r   r   r   Zself_attention_outputsr   r   Zcross_attention_outputslayer_outputr%   r%   r&   r@     sH    



zBrosLayer.forwardc                 C   s   |  |}| ||}|S rE   )r   r   )r/   r   Zintermediate_outputr   r%   r%   r&   r     s    
zBrosLayer.feed_forward_chunk)NNNNF)r   r   r   r+   r!   rB   r   r"   r   r$   r@   r   rC   r%   r%   r1   r&   r     s"        8r   c                       sx   e Zd Z fddZed	ejejeej eej eej eej ee	 ee	 ee	 e
eej ef d
ddZ  ZS )
BrosEncoderc                    s4   t     | _t fddt jD | _d S )Nc                    s   g | ]}t  qS r%   )r   ).0_r0   r%   r&   
<listcomp>      z(BrosEncoder.__init__.<locals>.<listcomp>)r*   r+   r0   r   Z
ModuleListrK   num_hidden_layerslayerrI   r1   r   r&   r+     s    
zBrosEncoder.__init__NFT)
r   rO   r   r   r   r   r   output_hidden_statesreturn_dictr4   c
              
   C   s   |rdnd }
|rdnd }|r(| j jr(dnd }t| jD ]p\}}|rL|
|f }
|d ur\|| nd }||||||||d}|d }|r6||d f }| j jr6||d f }q6|r|
|f }
t||
||dS )Nr%   r   r   r   r5   )last_hidden_stater   r   cross_attentions)r0   r   	enumerater   r
   )r/   r   rO   r   r   r   r   r   r   r   Zall_hidden_statesZall_self_attentionsZall_cross_attentionsrN   Zlayer_moduleZlayer_head_maskZlayer_outputsr%   r%   r&   r@     s:    


zBrosEncoder.forward)NNNNFFT)r   r   r   r+   r   r!   rB   r   r"   r   r   r$   r
   r@   rC   r%   r%   r1   r&   r     s*          r   c                       s0   e Zd Z fddZejejdddZ  ZS )
BrosPoolerc                    s*   t    t|j|j| _t | _d S rE   )r*   r+   r   rS   rc   r   ZTanh
activationrI   r1   r%   r&   r+     s    
zBrosPooler.__init__r   c                 C   s(   |d d df }|  |}| |}|S )Nr   )r   r   )r/   r   Zfirst_token_tensorpooled_outputr%   r%   r&   r@     s    

zBrosPooler.forwardrA   r%   r%   r1   r&   r     s   r   c                       s0   e Zd Z fddZejejdddZ  ZS )BrosRelationExtractorc                    s   t    |j| _|j| _|j| _|j| _t| j| _	t
| j| j| j | _t
| j| j| j | _ttd| j| _d S )Nr   )r*   r+   n_relationsrc   backbone_hidden_sizehead_hidden_sizeZclassifier_dropout_probr   rj   droprS   r   r   	Parameterr!   ro   
dummy_noderI   r1   r%   r&   r+     s    
zBrosRelationExtractor.__init__)r   r   c              	   C   s   |  | |}| jdd|dd}tj||gdd}| | |}|	|d|d| j
| j}|	|d|d| j
| j}t|dddd|dddd}|S )Nr   r   Zaxisr5   r   )r   r   r   Z	unsqueezerepeatr9   r!   r;   r   r:   r   r   r   r   )r/   r   r   Z	dummy_vecZrelation_scorer%   r%   r&   r@   *  s     zBrosRelationExtractor.forwardrA   r%   r%   r1   r&   r     s   r   c                   @   s*   e Zd ZU eed< dZejdddZdS )BrosPreTrainedModelr0   bros)modulec                 C   s   | j j}t|tjr>|jjjd|d |jdur|jj	  nt|tj
rz|jjjd|d |jdur|jj|j 	  nDt|tjr|jj	  |jjd nt|trtjj|j|d dS )zInitialize the weightsr(   )meanstdNg      ?)r   )r0   Zinitializer_ranger   r   rS   weightdataZnormal_rQ   Zzero_rb   rW   rh   Zfill_r   initr   )r/   r   r   r%   r%   r&   _init_weightsB  s    


z!BrosPreTrainedModel._init_weightsN)	r   r   r   r   r#   Zbase_model_prefixr   Moduler   r%   r%   r%   r&   r   =  s   
r   c                       s   e Zd Zd fdd	Zdd Zdd Zdd	 Zeede	e
j e	e
j e	e
j e	e
j e	e
j e	e
j e	e
j e	e
j e	e
j e	e e	e e	e eee
j ef dddZ  ZS )	BrosModelTc                    sN   t  | || _t|| _t|| _t|| _|r<t	|nd| _
|   dS )zv
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        N)r*   r+   r0   rV   rx   rP   bbox_embeddingsr   encoderr   poolerinit_weights)r/   r0   Zadd_pooling_layerr1   r%   r&   r+   X  s    


zBrosModel.__init__c                 C   s   | j jS rE   rx   rd   )r/   r%   r%   r&   get_input_embeddingsh  s    zBrosModel.get_input_embeddingsc                 C   s   || j _d S rE   r   )r/   r   r%   r%   r&   set_input_embeddingsk  s    zBrosModel.set_input_embeddingsc                 C   s*   |  D ]\}}| jj| j| qdS )z
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
        class PreTrainedModel
        N)itemsr   r   r   r   )r/   Zheads_to_pruner   r   r%   r%   r&   _prune_headsn  s    zBrosModel._prune_headsN)rq   rJ   r   r]   r\   r   rr   r   r   r   r   r   r4   c                 C   s8  |
dur|
n| j j}
|dur |n| j j}|dur4|n| j j}|durV|durVtdn4|durh| }n"|dur| dd }ntd|du rtd|\}}|dur|jn|j}|du rtj||d}|du r t	| j
dr| j
jddd|f }|||}|}ntj|tj|d}| |||}| j jr||dur|| \}}}||f}|	du rptj||d}	| |	}nd}| || j j}| j
||||d	}|jd d
kr|ddddg df }|| j j }| |}| j|||||||
|dd	}|d }| jdur| |nd}t|||j|j|jdS )a  
        bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'):
            Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values
            (x1, y1, x2, y2), where (x1, y1) is the top left corner, and (x2, y2) is the bottom right corner of the
            bounding box.

        Examples:

        ```python
        >>> import torch
        >>> from transformers import BrosProcessor, BrosModel

        >>> processor = BrosProcessor.from_pretrained("jinho8345/bros-base-uncased")

        >>> model = BrosModel.from_pretrained("jinho8345/bros-base-uncased")

        >>> encoding = processor("Hello, my dog is cute", add_special_tokens=False, return_tensors="pt")
        >>> bbox = torch.tensor([[[0, 0, 1, 1]]]).repeat(1, encoding["input_ids"].shape[-1], 1)
        >>> encoding["bbox"] = bbox

        >>> outputs = model(**encoding)
        >>> last_hidden_states = outputs.last_hidden_state
        ```NzDYou cannot specify both input_ids and inputs_embeds at the same timer6   z5You have to specify either input_ids or inputs_embedszYou have to specify bbox)r`   r]   r^   )rq   r\   r]   rr      )r   r   r5   r   r5   r   r   r   T)rO   r   r   r   r   r   r   r   r   )r   Zpooler_outputr   r   r   )r0   r   r   use_return_dictr~   r9   r`   r!   Zonesrs   rx   r]   rn   ro   rp   Zget_extended_attention_maskr   Zinvert_attention_maskZget_head_maskr   r   Z
bbox_scaler   r   r   r   r   r   r   )r/   rq   rJ   r   r]   r\   r   rr   r   r   r   r   r   rt   r   ru   r`   rv   rw   Zextended_attention_maskZencoder_batch_sizeZencoder_sequence_lengthr   Zencoder_hidden_shapeZencoder_extended_attention_maskZembedding_outputZscaled_bboxZbbox_position_embeddingsZencoder_outputssequence_outputr   r%   r%   r&   r@   v  s|    (




zBrosModel.forward)T)NNNNNNNNNNNN)r   r   r   r+   r   r   r   r   r   r   r!   rB   r   r   r$   r   r@   rC   r%   r%   r1   r&   r   V  sB               r   c                       s   e Zd ZdgZ fddZeedeej	 eej	 eej	 eej	 eej	 eej	 eej	 eej	 eej	 ee
 ee
 ee
 eeej	 ef dddZ  ZS )	BrosForTokenClassificationr   c                    s^   t  | |j| _t|| _t|dr.|jn|j}t	|| _
t|j|j| _|   d S Nclassifier_dropout)r*   r+   
num_labelsr   r   rs   r   rk   r   rj   rl   rS   rc   
classifierr   r/   r0   r   r1   r%   r&   r+     s    
z#BrosForTokenClassification.__init__Nrq   rJ   r   bbox_first_token_maskr]   r\   r   rr   labelsr   r   r   r4   c                 C   s   |dur|n| j j}| j||||||||
|dd
}|d }| |}| |}d}|	durt }|dur|d}||d| j| |	d| }n||d| j|	d}t|||j	|j
dS )a  
        bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'):
            Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values
            (x1, y1, x2, y2), where (x1, y1) is the top left corner, and (x2, y2) is the bottom right corner of the
            bounding box.
        bbox_first_token_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to indicate the first token of each bounding box. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

        Examples:

        ```python
        >>> import torch
        >>> from transformers import BrosProcessor, BrosForTokenClassification

        >>> processor = BrosProcessor.from_pretrained("jinho8345/bros-base-uncased")

        >>> model = BrosForTokenClassification.from_pretrained("jinho8345/bros-base-uncased")

        >>> encoding = processor("Hello, my dog is cute", add_special_tokens=False, return_tensors="pt")
        >>> bbox = torch.tensor([[[0, 0, 1, 1]]]).repeat(1, encoding["input_ids"].shape[-1], 1)
        >>> encoding["bbox"] = bbox

        >>> outputs = model(**encoding)
        ```NT)	rJ   r   r]   r\   r   rr   r   r   r   r   r6   r   logitsr   r   )r0   r   r   rl   r   r   r:   r   r   r   r   )r/   rq   rJ   r   r   r]   r\   r   rr   r   r   r   r   r   r   r   r   loss_fctr%   r%   r&   r@   	  s>    -


z"BrosForTokenClassification.forward)NNNNNNNNNNNNr   r   r   "_keys_to_ignore_on_load_unexpectedr+   r   r   r   r!   rB   r   r   r$   r   r@   rC   r%   r%   r1   r&   r     s>               r   a  
    Bros Model with a token classification head on top (initial_token_layers and subsequent_token_layer on top of the
    hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. The initial_token_classifier is used to
    predict the first token of each entity, and the subsequent_token_classifier is used to predict the subsequent
    tokens within an entity. Compared to BrosForTokenClassification, this model is more robust to serialization errors
    since it predicts next token from one token.
    c                       s   e Zd ZdgZ fddZeedeej	 eej	 eej	 eej	 eej	 eej	 eej	 eej	 eej	 eej	 ee
 ee
 ee
 eeej	 ef dddZ  ZS )	!BrosSpadeEEForTokenClassificationr   c              	      s   t  | || _|j| _|j| _|j| _t|| _t	|drD|j
n|j}tt|t|j|jt|t|j|j| _t|| _|   d S r   )r*   r+   r0   r   r   rc   r   r   r   rs   r   rk   r   Z
Sequentialrj   rS   initial_token_classifierr   subsequent_token_classifierr   r   r1   r%   r&   r+   i  s     

z*BrosSpadeEEForTokenClassification.__init__N)rq   rJ   r   r   r]   r\   r   rr   initial_token_labelssubsequent_token_labelsr   r   r   r4   c                 C   s  |dur|n| j j}| j|||||||||dd
}|d }|dd }| |dd }| ||d}d| }|j\}}|j	}t
j|t
|dg|gdd }||dddddf t
|jj}t
||d j|t
jd}||dddddf t
|jj}|d }d}|	dur|
durt }|	d}	|dur|d}||d| j| |	| }n||d| j|	}|
d}
||d|d | |
| }|| }t||||j|jd	S )
a>  
        bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'):
            Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values
            (x1, y1, x2, y2), where (x1, y1) is the top left corner, and (x2, y2) is the bottom right corner of the
            bounding box.
        bbox_first_token_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to indicate the first token of each bounding box. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.
        initial_token_labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for the initial token classification.
        subsequent_token_labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for the subsequent token classification.

        Examples:

        ```python
        >>> import torch
        >>> from transformers import BrosProcessor, BrosSpadeEEForTokenClassification

        >>> processor = BrosProcessor.from_pretrained("jinho8345/bros-base-uncased")

        >>> model = BrosSpadeEEForTokenClassification.from_pretrained("jinho8345/bros-base-uncased")

        >>> encoding = processor("Hello, my dog is cute", add_special_tokens=False, return_tensors="pt")
        >>> bbox = torch.tensor([[[0, 0, 1, 1]]]).repeat(1, encoding["input_ids"].shape[-1], 1)
        >>> encoding["bbox"] = bbox

        >>> outputs = model(**encoding)
        ```NT
rq   rJ   r   r]   r\   r   rr   r   r   r   r   r   r   r`   r_   r6   )r   r   r   r   r   )r0   r   r   rU   r   r   r   squeezer   r`   r!   r;   ro   r   r   masked_fillfinfor_   mineyer:   r   r   r   r   r   )r/   rq   rJ   r   r   r]   r\   r   rr   r   r   r   r   r   r   last_hidden_statesr   r   Zinv_attention_maskr   max_seq_lengthr`   Zinvalid_token_maskself_token_maskZsubsequent_token_maskr   r   Zinitial_token_lossZsubsequent_token_lossr%   r%   r&   r@     sj    2
&  



z)BrosSpadeEEForTokenClassification.forward)NNNNNNNNNNNNN)r   r   r   r   r+   r   r   r   r!   rB   r   r   r$   r   r@   rC   r%   r%   r1   r&   r   ]  sB   
             r   z
    Bros Model with a token classification head on top (a entity_linker layer on top of the hidden-states output) e.g.
    for Entity-Linking. The entity_linker is used to predict intra-entity links (one entity to another entity).
    c                       s   e Zd ZdgZ fddZeedeej	 eej	 eej	 eej	 eej	 eej	 eej	 eej	 eej	 ee
 ee
 ee
 eeej	 ef dddZ  ZS )	!BrosSpadeELForTokenClassificationr   c                    s`   t  | || _|j| _|j| _|j| _t|| _t	|drD|j
n|j t|| _|   d S r   )r*   r+   r0   r   r   rc   r   r   r   rs   r   rk   r   entity_linkerr   rI   r1   r%   r&   r+     s    

z*BrosSpadeELForTokenClassification.__init__Nr   c                 C   sT  |dur|n| j j}| j||||||||
|dd
}|d }|dd }| ||d}d}|	dur@t }|j\}}|j	}t
||d j|t
jd}|d}t
j| t
j|dgt
j|dgdd	}||dddddf t
|jj}||dddddf t
|jj}||d|d | |	d| }t|||j|jd
S )a  
        bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'):
            Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values
            (x1, y1, x2, y2), where (x1, y1) is the top left corner, and (x2, y2) is the bottom right corner of the
            bounding box.
        bbox_first_token_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to indicate the first token of each bounding box. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

        Examples:

        ```python
        >>> import torch
        >>> from transformers import BrosProcessor, BrosSpadeELForTokenClassification

        >>> processor = BrosProcessor.from_pretrained("jinho8345/bros-base-uncased")

        >>> model = BrosSpadeELForTokenClassification.from_pretrained("jinho8345/bros-base-uncased")

        >>> encoding = processor("Hello, my dog is cute", add_special_tokens=False, return_tensors="pt")
        >>> bbox = torch.tensor([[[0, 0, 1, 1]]]).repeat(1, encoding["input_ids"].shape[-1], 1)
        >>> encoding["bbox"] = bbox

        >>> outputs = model(**encoding)
        ```NTr   r   r   r  r6   r^   r   r   )r0   r   r   rU   r   r  r  r   r   r`   r!   r  r   r   r:   r;   ro   r  r  r_   r  r   r   r   )r/   rq   rJ   r   r   r]   r\   r   rr   r   r   r   r   r   r  r   r   r   r   r  r`   r	  maskr%   r%   r&   r@     sL    ,


(($z)BrosSpadeELForTokenClassification.forward)NNNNNNNNNNNNr   r%   r%   r1   r&   r
    s>               r
  )r   r   r   r   r
  )9r    r   dataclassesr   typingr   r   r!   Ztorch.utils.checkpointr   Ztorch.nnr   Zactivationsr   Zmodeling_layersr	   Zmodeling_outputsr
   r   r   Zmodeling_utilsr   Zpytorch_utilsr   r   r   utilsr   r   r   r   Zconfiguration_brosr   Z
get_loggerr   loggerr   r   r'   rD   rP   rV   ry   r   r   r   r   r   r   r   r   r   r   r   r   r
  __all__r%   r%   r%   r&   <module>   sd   
Am5M9" "d	 o