
    i                        d Z 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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mZmZ ddlm Z  ddl!m"Z"m#Z# ddl$m%Z%  ejL                  e'      Z(e ed       G d de                    Z) G d dejT                        Z+ G d dejT                        Z, G d dejT                        Z- G d dejT                        Z. G d d ejT                        Z/ G d! d"ejT                        Z0 G d# d$ejT                        Z1 G d% d&ejT                        Z2 G d' d(ejT                        Z3 G d) d*e      Z4 G d+ d,ejT                        Z5 G d- d.ejT                        Z6e G d/ d0e             Z7 G d1 d2e7      Z8e G d3 d4e7             Z9e G d5 d6e7             Z: ed7       G d8 d9e7             Z; ed:       G d; d<e7             Z<g d=Z=y)>zPyTorch Bros model.    N)	dataclass)nn)CrossEntropyLoss   )initialization)ACT2FN)GradientCheckpointingLayer)"BaseModelOutputWithCrossAttentions,BaseModelOutputWithPoolingAndCrossAttentionsTokenClassifierOutput)PreTrainedModel)Unpack)apply_chunking_to_forward)ModelOutputTransformersKwargsauto_docstringcan_return_tuplelogging)merge_with_config_defaults)OutputRecordercapture_outputs   )
BrosConfigz@
    Base class for outputs of token classification models.
    )custom_introc                       e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
eej                     dz  ed<   dZeej                     dz  ed<   y)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   torchFloatTensor__annotations__r   r   r    tupler!        w/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/models/bros/modeling_bros.pyr   r   +   s~     &*D%

d
")59%++d298<U..5<59M5**+d2926Je''(4/6r+   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )BrosPositionalEmbedding1Dc                     t         |           |j                  | _        ddt        j                  d| j                  d      | j                  z  z  z  }| j                  d|       y )Nr   '                 @inv_freq)super__init__dim_bbox_sinusoid_emb_1dr&   arangeregister_buffer)selfconfigr3   	__class__s      r,   r5   z"BrosPositionalEmbedding1D.__init__E   s^    (.(G(G%ell3(E(EsKdNkNkkl
 	Z2r+   pos_seqreturnc                    |j                         }|\  }}}|j                  |||d      | j                  j                  ddd| j                  dz        z  }t	        j
                  |j                         |j                         gd      }|S )Nr      dim)sizeviewr3   r6   r&   catsincos)r9   r<   seq_sizeb1b2b3sinusoid_inppos_embs           r,   forwardz!BrosPositionalEmbedding1D.forwardO   s    <<>
B||BB2T]]5G5G1aQUQnQnrsQs5tt))\--/1A1A1CD"Mr+   r"   r#   r$   r5   r&   TensorrN   __classcell__r;   s   @r,   r.   r.   B   s#    3u||  r+   r.   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )BrosPositionalEmbedding2Dc                     t         |           |j                  | _        t        |      | _        t        |      | _        y N)r4   r5   dim_bboxr.   	x_pos_emb	y_pos_embr9   r:   r;   s     r,   r5   z"BrosPositionalEmbedding2D.__init__X   s1    26:26:r+   bboxr=   c                    g }t        | j                        D ]U  }|dz  dk(  r&|j                  | j                  |d|f                1|j                  | j	                  |d|f                W t        j                  |d      }|S )Nr?   r   .r@   rA   )rangerW   appendrX   rY   r&   rE   )r9   r[   stackibbox_pos_embs        r,   rN   z!BrosPositionalEmbedding2D.forward_   s|    t}}% 	;A1uzT^^DaL9:T^^DaL9:		;
 yyB/r+   rO   rR   s   @r,   rT   rT   W   s#    ;ELL U\\ r+   rT   c                   >     e Zd Z fdZdej
                  fdZ xZS )BrosBboxEmbeddingsc                     t         |           t        |      | _        t	        j
                  |j                  |j                  d      | _        y )NF)bias)	r4   r5   rT   bbox_sinusoid_embr   Lineardim_bbox_sinusoid_emb_2ddim_bbox_projectionbbox_projectionrZ   s     r,   r5   zBrosBboxEmbeddings.__init__k   s=    !:6!B!yy)H)H&JdJdkpqr+   r[   c                     |j                  dd      }|d d d d d d d f   |d d d d d d d f   z
  }| j                  |      }| j                  |      }|S )Nr   r   )	transposerf   rj   )r9   r[   bbox_tbbox_posra   s        r,   rN   zBrosBboxEmbeddings.forwardp   s\    1%$1a-(6!T1a-+@@--h7++L9r+   rO   rR   s   @r,   rc   rc   j   s    r
ELL r+   rc   c                        e Zd ZdZ fdZ	 	 	 	 d
dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  f
d	Z xZS )BrosTextEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                 @   t         |           t        j                  |j                  |j
                  |j                        | _        t        j                  |j                  |j
                        | _	        t        j                  |j                  |j
                        | _        t        j                  |j
                  |j                        | _        t        j                  |j                        | _        | j#                  dt%        j&                  |j                        j)                  d             | j#                  dt%        j*                  | j,                  j/                         t$        j0                  | j,                  j2                        d       y )	N)padding_idxepsposition_idsr   r@   token_type_idsdtypedeviceF)
persistent)r4   r5   r   	Embedding
vocab_sizehidden_sizepad_token_idword_embeddingsmax_position_embeddingsposition_embeddingstype_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsDropouthidden_dropout_probdropoutr8   r&   r7   expandzerosru   rC   longrz   rZ   s     r,   r5   zBrosTextEmbeddings.__init__|   s#   !||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c %'\\&2H2H&J\J\%]"f&8&8f>S>STzz&"<"<=^U\\&:X:X-Y-`-`ah-ijKK!!&&(jj((//
  	 	
r+   N	input_idsrw   ru   inputs_embedsr=   c                 6   ||j                         }n|j                         d d }|d   }|| j                  d d d |f   }|st        | d      r-| j                  d d d |f   }|j	                  |d   |      }|}n:t        j                  |t
        j                  | j                  j                        }|| j                  |      }| j                  |      }	||	z   }
| j                  |      }|
|z  }
| j                  |
      }
| j                  |
      }
|
S )Nr@   r   rw   r   rx   )rC   ru   hasattrrw   r   r&   r   r   rz   r   r   r   r   r   )r9   r   rw   ru   r   input_shape
seq_lengthbuffered_token_type_ids buffered_token_type_ids_expandedr   
embeddingsr   s               r,   rN   zBrosTextEmbeddings.forward   s/     #..*K',,.s3K ^
,,Q^<L!t-.*.*=*=a*n*M'3J3Q3QR]^_R`bl3m0!A!&[

SWSdSdSkSk!l  00;M $ : :> J"%::
"66|D))
^^J/
\\*-
r+   )NNNN)	r"   r#   r$   r%   r5   r&   rP   rN   rQ   rR   s   @r,   rp   rp   y   sv    Q
. *..2,0-1#<<$&# t+# llT)	#
 ||d*# 
#r+   rp   c                        e Zd Z fdZ	 	 	 d
dej
                  dej
                  dej
                  dz  dej
                  dz  dej
                  dz  deej
                     fd	Z xZS )BrosSelfAttentionc                    t         |           |j                  |j                  z  dk7  r2t	        |d      s&t        d|j                   d|j                   d      |j                  | _        t        |j                  |j                  z        | _        | j                  | j                  z  | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                        | _        |j"                  | _        y )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ())r4   r5   r~   num_attention_headsr   
ValueErrorintattention_head_sizeall_head_sizer   rg   querykeyvaluer   attention_probs_dropout_probr   
is_decoderrZ   s     r,   r5   zBrosSelfAttention.__init__   s)    : ::a?PVXhHi#F$6$6#7 8 445Q8 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PPYYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF ++r+   Nr    ra   attention_maskencoder_hidden_statesencoder_attention_maskr=   c                    |j                   d   d| j                  | j                  f}| j                  |      j	                  |      j                  dd      }|d u}|rc| j                  |      j	                  |      j                  dd      }	| j                  |      j	                  |      j                  dd      }
|}n`| j                  |      j	                  |      j                  dd      }	| j                  |      j	                  |      j                  dd      }
t        j                  ||	j                  dd            }|j                   \  }}}}|j	                  ||||      }|j                  g d      }t        j                  d||f      }||z   }|t        j                  | j                        z  }|||z   } t        j                  d      |      }| j!                  |      }t        j                  ||
      }|j                  dddd	      j#                         }|j%                         d d | j&                  fz   } |j                  | }||fS )
Nr   r@   r   r?   )r?   r   r   r   zbnid,bijd->bnijrA   r   )shaper   r   r   rD   rl   r   r   r&   matmulpermuteeinsummathsqrtr   Softmaxr   
contiguousrC   r   )r9   r    ra   r   r   r   hidden_shapequery_layeris_cross_attention	key_layervalue_layerattention_scores
batch_sizen_headr   d_headbbox_pos_scoresattention_probscontext_layernew_context_layer_shapes                       r,   rN   zBrosSelfAttention.forward   sG    &++A.D4L4LdNfNfgjj/44\BLLQPQR
 3$>!67<<\JTTUVXYZI**%:;@@NXXYZ\]^K3N/44\BLLQPQRI**]388FPPQRTUVK !<<Y5H5HR5PQ 2=1B1B.
FJ#((ZVT#++L9,,'8;:UV+o=+dii8P8P.QQ%/.@ -"**,-=> ,,7_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S***,CDo--r+   NNN)	r"   r#   r$   r5   r&   rP   r)   rN   rQ   rR   s   @r,   r   r      s~    ,0 /3596:5.||5. ll5. t+	5.
  %||d25. !&t 35. 
u||	5.r+   r   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )BrosSelfOutputc                 (   t         |           t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                        | _
        y Nrs   )r4   r5   r   rg   r~   denser   r   r   r   r   rZ   s     r,   r5   zBrosSelfOutput.__init__  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=r+   r    input_tensorr=   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S rV   r   r   r   r9   r    r   s      r,   rN   zBrosSelfOutput.forward  7    

=1]3}|'CDr+   rO   rR   s   @r,   r   r     1    >U\\  RWR^R^ r+   r   c                        e Zd Z fdZ	 	 	 d
dej
                  dej
                  dej
                  dz  dej
                  dz  dej
                  dz  dej
                  fd	Z xZS )BrosAttentionc                 b    t         |           t        |      | _        t	        |      | _        y rV   )r4   r5   r   r9   r   outputrZ   s     r,   r5   zBrosAttention.__init__  s&    %f-	$V,r+   Nr    ra   r   r   r   r=   c                 `    |}| j                  |||||      \  }}| j                  ||      }|S )Nra   r   r   r   )r9   r   )r9   r    ra   r   r   r   residual_s           r,   rN   zBrosAttention.forward  sH     !99%)"7#9 % 
q M8<r+   r   rO   rR   s   @r,   r   r     sy    - /3596:|| ll t+	
  %||d2 !&t 3 
r+   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )BrosIntermediatec                    t         |           t        j                  |j                  |j
                        | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y rV   )r4   r5   r   rg   r~   intermediate_sizer   
isinstance
hidden_actstrr   intermediate_act_fnrZ   s     r,   r5   zBrosIntermediate.__init__/  s]    YYv1163K3KL
f''-'-f.?.?'@D$'-'8'8D$r+   r    r=   c                 J    | j                  |      }| j                  |      }|S rV   )r   r   )r9   r    s     r,   rN   zBrosIntermediate.forward7  s&    

=100?r+   rO   rR   s   @r,   r   r   .  s#    9U\\ ell r+   r   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )
BrosOutputc                 (   t         |           t        j                  |j                  |j
                        | _        t        j                  |j
                  |j                        | _        t        j                  |j                        | _        y r   )r4   r5   r   rg   r   r~   r   r   r   r   r   r   rZ   s     r,   r5   zBrosOutput.__init__>  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=r+   r    r   r=   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S rV   r   r   s      r,   rN   zBrosOutput.forwardD  r   r+   rO   rR   s   @r,   r   r   =  r   r+   r   c                        e Zd Z fdZ	 	 	 ddej
                  dej
                  dej                  dz  dej                  dz  dej                  dz  dee   d	ej
                  fd
Z	d Z
 xZS )	BrosLayerc                 b   t         |           |j                  | _        d| _        t	        |      | _        |j                  | _        |j                  | _        | j                  r*| j                  st        |  d      t	        |      | _	        t        |      | _        t        |      | _        y )Nr   z> should be used as a decoder model if cross attention is added)r4   r5   chunk_size_feed_forwardseq_len_dimr   	attentionr   add_cross_attention	Exceptioncrossattentionr   intermediater   r   rZ   s     r,   r5   zBrosLayer.__init__L  s    '-'E'E$&v. ++#)#=#= ##??4&(f ghh"/"7D,V4 (r+   Nr    ra   r   r   r   kwargsr=   c                    | j                  |||      }| j                  r7|5t        | d      rt        d|  d       | j                  |f|||d|\  }}t        | j                  | j                  | j                  |      }|S )N)ra   r   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   r   r   r   r   feed_forward_chunkr   r   )r9   r    ra   r   r   r   r   r   s           r,   rN   zBrosLayer.forwardZ  s     %) ' 
 ??4@t-.=dV  Dd  e   3t22 -&;'=	 
  M1 2##((	
 r+   c                 L    | j                  |      }| j                  ||      }|S rV   )r   r   )r9   attention_outputintermediate_outputlayer_outputs       r,   r   zBrosLayer.feed_forward_chunk  s,    "//0@A{{#68HIr+   r   )r"   r#   r$   r5   r&   rP   r'   r   r   rN   r   rQ   rR   s   @r,   r   r   K  s    )$ 48:>;?$||$ ll$ ))D0	$
  %0047$ !& 1 1D 8$ +,$ 
$Lr+   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )
BrosPoolerc                     t         |           t        j                  |j                  |j                        | _        t        j                         | _        y rV   )r4   r5   r   rg   r~   r   Tanh
activationrZ   s     r,   r5   zBrosPooler.__init__  s9    YYv1163E3EF
'')r+   r    r=   c                 \    |d d df   }| j                  |      }| j                  |      }|S )Nr   )r   r   )r9   r    first_token_tensorpooled_outputs       r,   rN   zBrosPooler.forward  s6     +1a40

#566r+   rO   rR   s   @r,   r   r     s#    $
U\\ ell r+   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )BrosRelationExtractorc                 R   t         |           |j                  | _        |j                  | _        |j                  | _        |j                  | _        t        j                  | j                        | _	        t        j                  | j                  | j                  | j
                  z        | _        t        j                  | j                  | j                  | j
                  z        | _        t        j                  t        j                  d| j                              | _        y )Nr   )r4   r5   n_relationsr~   backbone_hidden_sizehead_hidden_sizeclassifier_dropout_probr   r   droprg   r   r   	Parameterr&   r   
dummy_noderZ   s     r,   r5   zBrosRelationExtractor.__init__  s    !--$*$6$6! & 2 2'-'E'E$JJt;;<	YYt88$:J:JTMbMb:bc
99T668H8H4K`K`8`a,,u{{1d6O6O'PQr+   r   r   c           	         | j                  | j                  |            }| j                  j                  d      j	                  d|j                  d      d      }t        j                  ||gd      }| j                  | j                  |            }|j                  |j                  d      |j                  d      | j                  | j                        }|j                  |j                  d      |j                  d      | j                  | j                        }t        j                  |j                  dddd      |j                  dddd            }|S )Nr   r   axisr?   r   )r   r   r  	unsqueezerepeatrC   r&   rE   r   rD   r   r   r   r   )r9   r   r   	dummy_vecrelation_scores        r,   rN   zBrosRelationExtractor.forward  s   jj;!78OO--a0779>>!;LaP	IIy)41=	HHTYYy12	!&&Q!1!1!!4d6F6FH]H]
 NN9>>!#4innQ6GIYIY[_[p[pq	1a+Y->->q!Q-J
 r+   rO   rR   s   @r,   r   r     s$    R5<< ELL r+   r   c                        e Zd ZU eed<   dZe eedd       eedd      dZ	 e
j                         dej                  f fd	       Z xZS )
BrosPreTrainedModelr:   brosr   r   )index
layer_namer   )r    r!   cross_attentionsmodulec                    t         |   |       | j                  j                  }t	        |t
              r"t        j                  |j                  |       yt	        |t              ryt        j                  |j                  t        j                  |j                  j                  d         j                  d             t        j                   |j"                         yt	        |t$              rUddt        j                  d|j&                  d      |j&                  z  z  z  }t        j                  |j(                  |       yy)	zInitialize the weights)stdr@   rv   r   r0   r1   r2   N)r4   _init_weightsr:   initializer_ranger   r   initnormal_r  rp   copy_ru   r&   r7   r   r   zeros_rw   r.   r6   r3   )r9   r  r  r3   r;   s       r,   r  z!BrosPreTrainedModel._init_weights  s     	f%kk++f34LL**4 23JJv**ELL9L9L9R9RSU9V,W,^,^_f,ghKK--. 9:%,,sF,K,KSQTZTsTsstH JJv1	 ;r+   )r"   r#   r$   r   r(   base_model_prefixr   r   r   _can_record_outputsr&   no_gradr   Moduler  rQ   rR   s   @r,   r  r    s\    "$%6aKX*+<ARbc U]]_2BII 2 2r+   r  c                        e Zd Z fdZee	 	 	 ddej                  dej                  dej                  dz  dej                  dz  dej                  dz  de	e
   d	eej                     ez  fd
              Z xZS )BrosEncoderc                     t         |   |       t        j                  t	        |j
                        D cg c]  }t        |       c}      | _        | j                          y c c}w rV   )	r4   r5   r   
ModuleListr]   num_hidden_layersr   layer	post_init)r9   r:   r   r;   s      r,   r5   zBrosEncoder.__init__  sK     ]]uVE]E]?^#_!If$5#_`
 $`s   A&Nr    ra   r   r   r   r   r=   c           	      X    | j                   D ]  } ||f||||d|} t        |      S )Nr   )last_hidden_state)r"  r
   )r9   r    ra   r   r   r   r   layer_modules           r,   rN   zBrosEncoder.forward  sQ     !JJ 	L()-&;'= M	 2+
 	
r+   r   )r"   r#   r$   r5   r   r   r&   rP   r'   r   r   r)   r
   rN   rQ   rR   s   @r,   r  r    s    
  
 48:>;?
||
 ll
 ))D0	

  %0047
 !& 1 1D 8
 +,
 
u||	A	A
   
r+   r  c                   x    e Zd Zd fd	Zd Zd Zee	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dej                  dz  de
e   deej                     ez  fd              Z xZS )	BrosModelc                     t         |   |       || _        t        |      | _        t        |      | _        t        |      | _        |rt        |      nd| _
        | j                          y)zv
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        N)r4   r5   r:   rp   r   rc   bbox_embeddingsr  encoderr   poolerr#  )r9   r:   add_pooling_layerr;   s      r,   r5   zBrosModel.__init__  sX    
 	 ,V41&9"6*,=j(4r+   c                 .    | j                   j                  S rV   r   r   )r9   s    r,   get_input_embeddingszBrosModel.get_input_embeddings  s    ...r+   c                 &    || j                   _        y rV   r/  )r9   r   s     r,   set_input_embeddingszBrosModel.set_input_embeddings  s    */'r+   Nr   r[   r   rw   ru   r   r   r   r   r=   c	                    |du |duz  rt        d      |t        d      | j                  ||||      }
|
j                  dd }|
j                  }|t	        j
                  ||      }| j                  ||      }| j                  j                  rE|C|j                         \  }}}||f}|t	        j
                  ||      }| j                  |      }nd}|j                  d   dk(  r|ddddg df   }|| j                  j                  z  }| j                  |      } | j                  |
f||||d	|	}|d
   }| j                  | j                  |      nd}t        |||j                   |j"                  |j$                        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
        ```Nz:You must specify exactly one of input_ids or inputs_embedszYou have to specify bbox)r   ru   rw   r   r@   )rz      )r   r   r?   r   r?   r   r   r   r   r   )r%  pooler_outputr    r!   r  )r   r   r   rz   r&   onesget_extended_attention_maskr:   r   rC   invert_attention_mask
bbox_scaler*  r+  r,  r   r    r!   r  )r9   r   r[   r   rw   ru   r   r   r   r   embedding_outputr   rz   extended_attention_maskencoder_batch_sizeencoder_sequence_lengthr   encoder_hidden_shapeencoder_extended_attention_maskscaled_bboxbbox_position_embeddingsencoder_outputssequence_outputr   s                           r,   rN   zBrosModel.forward  s   J -t";<YZZ<788??%)'	 + 
 ',,Sb1!((!"ZZFCN 150P0PQ_al0m ;;!!&;&G=R=W=W=Y: 7$68O#P %-).4HQW)X&.2.H.HI_.`+.2+ ::b>Q1667DT[[333#'#7#7#D >Jdll?
12"7#B?
 ?
 *!,8<8OO4UY;-')77&11,==
 	
r+   )TNNNNNNNN)r"   r#   r$   r5   r0  r2  r   r   r&   rP   r   r   r)   r   rN   rQ   rR   s   @r,   r(  r(    s    /0  *.$(.2.2,0-1596:\
<<$&\
 llT!\
 t+	\

 t+\
 llT)\
 ||d*\
  %||d2\
 !&t 3\
 +,\
 
u||	K	K\
  \
r+   r(  c                   p    e Zd ZdgZ fdZee	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  de	e
   deej                     ez  fd              Z xZS )BrosForTokenClassificationr,  c                 `   t         |   |       |j                  | _        t        |      | _        t        |d      r|j                  n|j                  }t        j                  |      | _
        t        j                  |j                  |j                        | _        | j                          y Nclassifier_dropout)r4   r5   
num_labelsr(  r  r   rI  r   r   r   r   rg   r~   
classifierr#  r9   r:   rI  r;   s      r,   r5   z#BrosForTokenClassification.__init__p  s      ++f%	)09M)NF%%TZTnTn 	 zz"45))F$6$68I8IJr+   Nr   r[   r   bbox_first_token_maskrw   ru   r   labelsr   r=   c	           	          | j                   |f|||||d|	}
|
d   }| j                  |      }| j                  |      }d}|t               }|J|j	                  d      } ||j	                  d| j
                        |   |j	                  d      |         }n2 ||j	                  d| j
                        |j	                  d            }t        |||
j                  |
j                        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)
        ```)r[   r   rw   ru   r   r   Nr@   r   logitsr    r!   )	r  r   rK  r   rD   rJ  r   r    r!   )r9   r   r[   r   rM  rw   ru   r   rN  r   outputsrC  rQ  r   loss_fcts                  r,   rN   z"BrosForTokenClassification.forward}  s	   R AJ		A
))%'A
 A
 "!*,,71')H$0(=(B(B2(F%KKDOO45JKV[[Y[_]rMs  B @&++b/R$!//))	
 	
r+   rD  r"   r#   r$   "_keys_to_ignore_on_load_unexpectedr5   r   r   r&   rP   r   r   r)   r   rN   rQ   rR   s   @r,   rF  rF  l  s   *3&  *.$(.259.2,0-1&*F
<<$&F
 llT!F
 t+	F

  %||d2F
 t+F
 llT)F
 ||d*F
 t#F
 +,F
 
u||	4	4F
  F
r+   rF  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                       e Zd ZdgZ fdZee	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dej                  dz  de	e
   deej                     ez  fd              Z xZS )!BrosSpadeEEForTokenClassificationr,  c           	      f   t         |   |       || _        |j                  | _        |j                  | _        |j
                  | _        t        |      | _        t        |d      r|j                  n|j                  }t        j                  t        j                  |      t        j                  |j
                  |j
                        t        j                  |      t        j                  |j
                  |j                              | _        t#        |      | _        | j'                          y rH  )r4   r5   r:   rJ  r   r~   r   r(  r  r   rI  r   r   
Sequentialr   rg   initial_token_classifierr   subsequent_token_classifierr#  rL  s      r,   r5   z*BrosSpadeEEForTokenClassification.__init__  s      ++!--$*$6$6!f%	)09M)NF%%TZTnTn 	
 )+JJ)*IIf((&*<*<=JJ)*IIf((&*;*;<	)
% ,A+H(r+   Nr   r[   r   rM  rw   ru   r   initial_token_labelssubsequent_token_labelsr   r=   c
           
      h    | j                   d	||||||d|
}|d   }|j                  dd      j                         }| j                  |      j                  dd      j                         }| j	                  ||      j                  d      }d|z
  }|j                  \  }}|j                  }t        j                  |t        j                  |dg      j                  |      gd      j                         }|j                  |dddddf   t        j                  |j                        j                         }t        j"                  ||dz         j                  |t        j                        }|j                  |dddddf   t        j                  |j                        j                         }|j%                  d      j                         }d}||	t'               }|j%                  d      }|;|j%                  d      } ||j%                  d| j(                        |   ||         }n# ||j%                  d| j(                        |      }|	j%                  d      }	 ||j%                  d|dz         |   |	|         }||z   }t+        ||||j,                  |j.                        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)
        ```r   r[   r   rw   ru   r   r   r   r  Nrz   ry   r@   )r   r   r   r    r!   r*   )r  rl   r   rZ  r[  squeezer   rz   r&   rE   r   toboolmasked_fillfinfory   mineyerD   r   rJ  r   r    r!   )r9   r   r[   r   rM  rw   ru   r   r\  r]  r   rR  last_hidden_statesr   r   inv_attention_maskr   max_seq_lengthrz   invalid_token_maskself_token_masksubsequent_token_maskr   rS  initial_token_losssubsequent_token_losss                             r,   rN   z)BrosSpadeEEForTokenClassification.forward  s   \ AJ		 A
))%'A
 A
 %QZ/99!Q?JJL#<<=OPZZ[\^_`kkm"&"B"BCUWi"j"r"rst"u /%7%=%="
N#**"YY(:EKKUV<X<[<[\b<c'dklmrrt"9"E"Eq$z*EKK8O8U8U,V,Z,Z#
  ))NNQ4FGJJRX`e`j`jJk"9"E"ED!QJ'5L5R5R)S)W)W#
 !/ 3 3B 7 < < >+0G0S')H $8#<#<R#@ $0(=(B(B2(F%%-(--b$//BCXY()>?&"
 &..B.G.GDOO.\^r%s"&=&B&B2&F#$,',,R!1CDEZ['(=>%!
 &(==D!5$;!//))
 	
r+   )	NNNNNNNNN)r"   r#   r$   rU  r5   r   r   r&   rP   r   r   r)   r   rN   rQ   rR   s   @r,   rW  rW    s)    +4&2  *.$(.259.2,0-1487;f
<<$&f
 llT!f
 t+	f

  %||d2f
 t+f
 llT)f
 ||d*f
 $llT1f
 "'!4f
 +,f
 
u||		.f
  f
r+   rW  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                   p    e Zd ZdgZ fdZee	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  de	e
   deej                     ez  fd              Z xZS )!BrosSpadeELForTokenClassificationr,  c                 @   t         |   |       || _        |j                  | _        |j                  | _        |j
                  | _        t        |      | _        t        |d      r|j                  n|j                   t        |      | _        | j                          y rH  )r4   r5   r:   rJ  r   r~   r   r(  r  r   rI  r   r   entity_linkerr#  rZ   s     r,   r5   z*BrosSpadeELForTokenClassification.__init__a  s      ++!--$*$6$6!f%	&-f6J&K	"	"QWQkQk26:r+   Nr   r[   r   rM  rw   ru   r   rN  r   r=   c	           
          | j                   d
||||||d|	}
|
d   }|j                  dd      j                         }| j                  ||      j	                  d      }d}|et               }|j                  \  }}|j                  }t        j                  ||dz         j                  |t        j                        }|j                  d      }t        j                  | t        j                  |dgt        j                  |      gd      }|j                  |dddddf   t        j                   |j"                        j$                        }|j                  |dddddf   t        j                   |j"                        j$                        } ||j                  d|dz         |   |j                  d      |         }t'        |||
j(                  |
j*                  	      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)
        ```r_  r   r   Nr`  r@   rx   r  rP  r*   )r  rl   r   rs  ra  r   r   rz   r&   rg  rb  rc  rD   rE   r   rd  re  ry   rf  r   r    r!   )r9   r   r[   r   rM  rw   ru   r   rN  r   rR  rh  rQ  r   rS  r   rj  rz   rl  masks                       r,   rN   z)BrosSpadeELForTokenClassification.forwardo  s   R AJ		 A
))%'A
 A
 %QZ/99!Q?JJL##$68JKSSTUV')H)7)=)=&J#**F#ii8JKNNV\didndnNoO(--b1D$)II**KKQuzz&Q %! ''(=aqj(I5;;W]WcWcKdKhKhiF''a
(CU[[QWQ]Q]E^EbEbcFFKKNQ,>?Ev{{SUW[G\]D$!//))	
 	
r+   rD  rT  rR   s   @r,   rq  rq  X  s    +4&  *.$(.259.2,0-1&*Q
<<$&Q
 llT!Q
 t+	Q

  %||d2Q
 t+Q
 llT)Q
 ||d*Q
 t#Q
 +,Q
 
u||	4	4Q
  Q
r+   rq  )r  r(  rF  rW  rq  )>r%   r   dataclassesr   r&   r   torch.nnr    r   r  activationsr   modeling_layersr	   modeling_outputsr
   r   r   modeling_utilsr   processing_utilsr   pytorch_utilsr   utilsr   r   r   r   r   utils.genericr   utils.output_capturingr   r   configuration_brosr   
get_loggerr"   loggerr   r  r.   rT   rc   rp   r   r   r   r   r   r   r   r   r  r  r(  rF  rW  rq  __all__r*   r+   r,   <module>r     s     !   % & ! 9 
 . & 6 _ _ 7 E * 
		H	% 
7k 7 7"		 *		 & ; ;|J.		 J.\RYY BII 6ryy  8* 8x BII D 2/ 2 24
% 
@ u
# u
 u
p X
!4 X
 X
v D
(; D
D
N d
(; d
d
Nr+   