
    <hb                        S r SSKrSSKJr  SSKJrJr  SSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJr  SSKJrJrJr  SSKJrJrJrJr  SSKJ r   \RB                  " \"5      r#\\" SS9 " S S\5      5       5       r$ " S S\	RJ                  5      r& " S S\	RJ                  5      r' " S S\	RJ                  5      r( " S S\	RJ                  5      r) " S S\	RJ                  5      r* " S S\	RJ                  5      r+ " S  S!\	RJ                  5      r, " S" S#\	RJ                  5      r- " S$ S%\	RJ                  5      r. " S& S'\5      r/ " S( S)\	RJ                  5      r0 " S* S+\	RJ                  5      r1 " S, S-\	RJ                  5      r2\ " S. S/\5      5       r3\ " S0 S1\35      5       r4\ " S2 S3\35      5       r5\" S4S9 " S5 S6\35      5       r6\" S7S9 " S8 S9\35      5       r7/ S:Qr8g);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.
    )custom_introc                       \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\R                     \	S'   Sr\\\R                        \	S'   Sr\\\R                        \	S'   S	rg)
BrosSpadeOutput*   a  
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____firstlineno____doc__r   r   torchFloatTensor__annotations__r   r   r   tupler    __static_attributes__r!       ^/var/www/html/shao/venv/lib/python3.13/site-packages/transformers/models/bros/modeling_bros.pyr   r   *   s~     )-D(5$$
%,8<(5#4#45<;?Xe&7&78?8<M8E%"3"345<59Ju00129r,   r   c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )BrosPositionalEmbedding1DA   c                    > [         TU ]  5         UR                  U l        SS[        R                  " SU R                  S5      U R                  -  -  -  nU R                  SU5        g )Nr   i'          g       @inv_freq)super__init__dim_bbox_sinusoid_emb_1dr'   arangeregister_buffer)selfconfigr3   	__class__s      r-   r5   "BrosPositionalEmbedding1D.__init__D   s^    (.(G(G%ell3(E(EsKdNkNkkl
 	Z2r,   pos_seqreturnc                    UR                  5       nUu  p4nUR                  X4US5      U R                  R                  SSSU R                  S-  5      -  n[        R
                  " UR                  5       UR                  5       /SS9nU$ )Nr      dim)sizeviewr3   r6   r'   catsincos)r9   r=   seq_sizeb1b2b3sinusoid_inppos_embs           r-   forward!BrosPositionalEmbedding1D.forwardN   s{    <<>
||BB2T]]5G5G1aQUQnQnrsQs5tt))\--/1A1A1CD"Mr,   )r6   
r"   r#   r$   r%   r5   r'   TensorrO   r+   __classcell__r;   s   @r-   r/   r/   A   s(    3u||   r,   r/   c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )BrosPositionalEmbedding2DV   c                    > [         TU ]  5         UR                  U l        [        U5      U l        [        U5      U l        g N)r4   r5   dim_bboxr/   	x_pos_emb	y_pos_embr9   r:   r;   s     r-   r5   "BrosPositionalEmbedding2D.__init__W   s1    26:26:r,   bboxr>   c                    / n[        U R                  5       HX  nUS-  S:X  a'  UR                  U R                  USU4   5      5        M3  UR                  U R	                  USU4   5      5        MZ     [
        R                  " USS9nU$ )Nr@   r   .rA   rB   )rangerZ   appendr[   r\   r'   rF   )r9   r_   stackibbox_pos_embs        r-   rO   !BrosPositionalEmbedding2D.forward^   sw    t}}%A1uzT^^DaL9:T^^DaL9:	 &
 yyB/r,   )rZ   r[   r\   rQ   rT   s   @r-   rV   rV   V   s(    ;ELL U\\  r,   rV   c                   J   ^  \ rS rSrU 4S jrS\R                  4S jrSrU =r	$ )BrosBboxEmbeddingsi   c                    > [         TU ]  5         [        U5      U l        [        R
                  " UR                  UR                  SS9U l        g )NF)bias)	r4   r5   rV   bbox_sinusoid_embr   Lineardim_bbox_sinusoid_emb_2ddim_bbox_projectionbbox_projectionr]   s     r-   r5   BrosBboxEmbeddings.__init__j   s=    !:6!B!yy)H)H&JdJdkpqr,   r_   c                     UR                  SS5      nUS S S 2S S 2S S 24   US S 2S S S 2S S 24   -
  nU R                  U5      nU R                  U5      nU$ )Nr   r   )	transposerl   rp   )r9   r_   bbox_tbbox_posre   s        r-   rO   BrosBboxEmbeddings.forwardo   s\    1%$1a-(6!T1a-+@@--h7++L9r,   )rp   rl   rQ   rT   s   @r-   rh   rh   i   s    r
ELL  r,   rh   c                      ^  \ rS rSrSrU 4S jr    SS\\R                     S\\R                     S\\R                     S\\R                     S\R                  4
S	 jjr	S
r
U =r$ )BrosTextEmbeddingsx   zGConstruct the embeddings from word, position and token_type embeddings.c           	      Z  > [         TU ]  5         [        R                  " UR                  UR
                  UR                  S9U l        [        R                  " UR                  UR
                  5      U l	        [        R                  " UR                  UR
                  5      U l        [        R                  " UR
                  UR                  S9U l        [        R                  " UR                  5      U l        [#        USS5      U l        U R'                  S[(        R*                  " UR                  5      R-                  S5      5        U R'                  S[(        R.                  " U R0                  R3                  5       [(        R4                  U R0                  R6                  S9S	S
9  g )N)padding_idxepsposition_embedding_typeabsoluteposition_ids)r   rA   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dropoutgetattrr~   r8   r'   r7   expandzerosr   rD   longr   r]   s     r-   r5   BrosTextEmbeddings.__init__{   s8   !||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c %'\\&2H2H&J\J\%]" f&8&8f>S>STzz&"<"<='.v7PR\']$^U\\&:X:X-Y-`-`ah-ijKK!!&&(jj((//
  	 	
r,   	input_idsr   r   inputs_embedsr>   c                 `   Ub  UR                  5       nOUR                  5       S S nUS   nUc  U R                  S S 2S U24   nUcv  [        U S5      (       a-  U R                  S S 2S U24   nUR	                  US   U5      nUnO8[
        R                  " U[
        R                  U R                  R                  S9nUc  U R                  U5      nU R                  U5      n	XI-   n
U R                  S:X  a  U R                  U5      nX-  n
U R                  U
5      n
U R                  U
5      n
U
$ )NrA   r   r   r   r   r   )rD   r   hasattrr   r   r'   r   r   r   r   r   r~   r   r   r   )r9   r   r   r   r   input_shape
seq_lengthbuffered_token_type_ids buffered_token_type_ids_expandedr   
embeddingsr   s               r-   rO   BrosTextEmbeddings.forward   s:     #..*K',,.s3K ^
,,Q^<L!t-..*.*=*=a*n*M'3J3Q3QR]^_R`bl3m0!A!&[

SWSdSdSkSk!l  00;M $ : :> J":
'':5"&":":<"H-J^^J/
\\*-
r,   )r   r   r~   r   r   r   )NNNN)r"   r#   r$   r%   r&   r5   r   r'   rR   rO   r+   rS   rT   s   @r-   rx   rx   x   s{    Q
4 -115/304#ELL)# !.# u||,	#
  -# 
# #r,   rx   c                   $  ^  \ rS rSrU 4S jr     SS\R                  S\R                  S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\R                     4S jjr	Sr
U =r$ )BrosSelfAttention   c                   > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eUR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  5      U l        [#        USS5      U l        U R$                  S:X  d  U R$                  S	:X  aG  UR&                  U l        [        R(                  " S
UR&                  -  S-
  U R                  5      U l        UR,                  U l        g )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()r~   r   relative_keyrelative_key_queryr@   r   )r4   r5   r   num_attention_headsr   
ValueErrorintattention_head_sizeall_head_sizer   rm   querykeyvaluer   attention_probs_dropout_probr   r   r~   r   r   distance_embedding
is_decoderr]   s     r-   r5   BrosSelfAttention.__init__   s    : ::a?PVXhHiHi#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'.v7PR\']$''>9T=Y=Y]q=q+1+I+ID(&(ll1v7U7U3UXY3Y[_[s[s&tD# ++r,   r   re   attention_mask	head_maskencoder_hidden_statesencoder_attention_maskoutput_attentionsr>   c                    UR                   S   SU R                  U R                  4nU R                  U5      R	                  U5      R                  SS5      n	US Ln
U
(       ac  U R                  U5      R	                  U5      R                  SS5      nU R                  U5      R	                  U5      R                  SS5      nUnO`U R                  U5      R	                  U5      R                  SS5      nU R                  U5      R	                  U5      R                  SS5      n[        R                  " XR                  SS5      5      nU R                  S:X  d  U R                  S:X  GaD  UR                  5       S   n[        R                  " U[        R                  UR                  S9R	                  SS5      n[        R                  " U[        R                  UR                  S9R	                  SS5      nUU-
  nU R                  UU R                   -   S-
  5      nUR#                  U	R$                  S	9nU R                  S:X  a  [        R&                  " S
U	U5      nUU-   nOHU R                  S:X  a8  [        R&                  " S
U	U5      n[        R&                  " SUU5      nUU-   U-   nU	R                   u  nnnnUR	                  XUU5      nUR)                  / SQ5      n[        R&                  " SX45      nUU-   nU[*        R,                  " U R                  5      -  nUb  X-   n[.        R0                  " SS9" U5      nU R3                  U5      nUb  UU-  n[        R                  " UU5      nUR)                  SSSS5      R5                  5       nUR                  5       S S U R6                  4-   nUR                  " U6 nU(       a  UU4OU4nU R8                  (       a  US-   nU$ )Nr   rA   r   r@   r   r   r   )r   zbhld,lrd->bhlrzbhrd,lrd->bhlr)r@   r   r   r   zbnid,bijd->bnijrB   r   rY   )shaper   r   r   rE   rs   r   r   r'   matmulr~   rD   r7   r   r   r   r   tor   einsumpermutemathsqrtr   Softmaxr   
contiguousr   r   )r9   r   re   r   r   r   r   r   hidden_shapequery_layeris_cross_attention	key_layervalue_layerattention_scoresr   position_ids_lposition_ids_rdistancepositional_embeddingrelative_position_scoresrelative_position_scores_queryrelative_position_scores_key
batch_sizen_headd_headbbox_pos_scoresattention_probscontext_layernew_context_layer_shapeoutputss                                 r-   rO   BrosSelfAttention.forward   s    &++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 !<<5H5HR5PQ''>9T=Y=Y]q=q&++-a0J"\\*EJJ}OcOcdiijlnopN"\\*EJJ}OcOcdiijkmopN%6H#'#:#:8dFbFb;bef;f#g #7#:#:ARAR#:#S ++~=+0<<8H+Wk+l(#36N#N --1EE16>NP[]q1r./4||<LiYm/n,#36T#TWs#s  2=1B1B.
FJ#((VT#++L9,,'8;:UV+o=+dii8P8P.QQ%/@ **,-=> ,,7  -	9O_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**,CD6G=/2mM]??'Gr,   )r   r   r   r   r   r   r   r   r~   r   r   NNNNF)r"   r#   r$   r%   r5   r'   rR   r   r*   rO   r+   rS   rT   s   @r-   r   r      s    ,8 26,08<9=49P||P llP !.	P
 ELL)P  (5P !) 6P $ELL1P 
u||	P Pr,   r   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )BrosSelfOutputi&  c                 (  > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " UR                  UR                  S9U l        [        R                  " UR                  5      U l
        g Nr|   )r4   r5   r   rm   r   denser   r   r   r   r   r]   s     r-   r5   BrosSelfOutput.__init__'  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=r,   r   input_tensorr>   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ rY   r   r   r   r9   r   r   s      r-   rO   BrosSelfOutput.forward-  5    

=1]3}'CDr,   r   r   r   rQ   rT   s   @r-   r   r   &  6    >U\\  RWR^R^  r,   r   c                     ^  \ rS rSrU 4S jrS r     SS\R                  S\R                  S\\R                     S\\R                     S\\R                     S	\\R                     S
\\	   S\
\R                     4S jjrSrU =r$ )BrosAttentioni4  c                    > [         TU ]  5         [        U5      U l        [	        U5      U l        [        5       U l        g rY   )r4   r5   r   r9   r   outputsetpruned_headsr]   s     r-   r5   BrosAttention.__init__5  s0    %f-	$V,Er,   c                 8   [        U5      S:X  a  g [        UU R                  R                  U R                  R                  U R
                  5      u  p[        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l	        [        U R                  R                  USS9U R                  l        U R                  R                  [        U5      -
  U R                  l        U R                  R                  U R                  R                  -  U R                  l        U R
                  R                  U5      U l        g )Nr   r   rB   )lenr   r9   r   r   r   r   r   r   r   r   r   r   union)r9   headsindexs      r-   prune_headsBrosAttention.prune_heads;  s   u:?7II))II))	
 -TYY__eD		*499==%@		,TYY__eD		.t{{/@/@%QO )-		(E(EE
(R		%"&))"?"?$))B_B_"_		 --33E:r,   r   re   r   r   r   r   r   r>   c           
      l    U R                  UUUUUUUS9nU R                  US   U5      n	U	4USS  -   n
U
$ )Nr   re   r   r   r   r   r   r   r   )r9   r   )r9   r   re   r   r   r   r   r   self_outputsattention_outputr   s              r-   rO   BrosAttention.forwardP  s\     yy'%)"7#9/ ! 
  ;;|AF#%QR(88r,   )r   r   r9   r   )r"   r#   r$   r%   r5   r   r'   rR   r   boolr*   rO   r+   rS   rT   s   @r-   r   r   4  s    ";2 26,08<9=,1|| ll !.	
 ELL)  (5 !) 6 $D> 
u||	 r,   r   c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )BrosIntermediateii  c                   > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        UR                  [        5      (       a  [        UR                     U l        g UR                  U l        g rY   )r4   r5   r   rm   r   intermediate_sizer   
isinstance
hidden_actstrr	   intermediate_act_fnr]   s     r-   r5   BrosIntermediate.__init__j  s`    YYv1163K3KL
f''--'-f.?.?'@D$'-'8'8D$r,   r   r>   c                 J    U R                  U5      nU R                  U5      nU$ rY   r   r  )r9   r   s     r-   rO   BrosIntermediate.forwardr  s&    

=100?r,   r
  rQ   rT   s   @r-   r  r  i  s(    9U\\ ell  r,   r  c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )
BrosOutputix  c                 (  > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        R                  " UR
                  UR                  S9U l        [        R                  " UR                  5      U l        g r   )r4   r5   r   rm   r  r   r   r   r   r   r   r   r]   s     r-   r5   BrosOutput.__init__y  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=r,   r   r   r>   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ rY   r   r   s      r-   rO   BrosOutput.forward  r   r,   r   rQ   rT   s   @r-   r  r  x  r   r,   r  c                     ^  \ rS rSrU 4S jr     SS\R                  S\R                  S\\R                     S\\R                     S\\R                     S\\R                     S	\\	   S
\
\R                     4S jjrS rSrU =r$ )	BrosLayeri  c                 v  > [         TU ]  5         UR                  U l        SU l        [	        U5      U l        UR                  U l        UR                  U l        U R                  (       a/  U R                  (       d  [        U  S35      e[	        U5      U l	        [        U5      U l        [        U5      U l        g )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   r]   s     r-   r5   BrosLayer.__init__  s    '-'E'E$&v. ++#)#=#= ##??4&(f ghh"/"7D,V4 (r,   r   re   r   r   r   r   r   r>   c           	         U R                  UUUUUS9nUS   n	U R                  (       a  USS n
OUSS  n
U R                  (       aC  Ub@  [        U S5      (       a  [        SU  S35      eU R	                  U	UUUUUS9nUS   n	XSS -   n
[        U R                  U R                  U R                  U	5      nU4U
-   n
U R                  (       a  U
S	-   n
U
$ )
N)re   r   r   r   r   r   rA   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   rY   )	r  r   r   r  r  r   feed_forward_chunkr  r  )r9   r   re   r   r   r   r   r   self_attention_outputsr   r   cross_attention_outputslayer_outputs                r-   rO   BrosLayer.forward  s%    "&%)/ "0 "
 2!4 ??,Qr2G,QR0G??4@t-..=dV  Dd  e  '+&9&9 -#&;'="3 ': '#  7q9" ==G0##((	
  /G+ ??'Gr,   c                 J    U R                  U5      nU R                  X!5      nU$ rY   )r  r   )r9   r   intermediate_outputr!  s       r-   r  BrosLayer.feed_forward_chunk  s)    "//0@A{{#6Ir,   )r  r  r  r  r  r   r   r  r   )r"   r#   r$   r%   r5   r'   rR   r   r(   r   r*   rO   r  r+   rS   rT   s   @r-   r  r    s    )$ 7;15=A>B,16||6 ll6 !!2!23	6
 E--.6  ((9(9:6 !)):): ;6 $D>6 
u||	6p r,   r  c                   <  ^  \ rS rSrU 4S jr\       SS\R                  S\R                  S\\R                     S\\R                     S\\R                     S\\R                     S	\\
   S
\\
   S\\
   S\\\R                     \4   4S jj5       rSrU =r$ )BrosEncoderi  c                    > [         TU ]  5         Xl        [        R                  " [        UR                  5       Vs/ sH  n[        U5      PM     sn5      U l        g s  snf rY   )	r4   r5   r:   r   
ModuleListra   num_hidden_layersr  layer)r9   r:   _r;   s      r-   r5   BrosEncoder.__init__  sG    ]]uVE]E]?^#_?^!If$5?^#_`
#_s   Ar   re   r   r   r   r   r   output_hidden_statesreturn_dictr>   c
                    U(       a  SOS n
U(       a  SOS nU(       a  U R                   R                  (       a  SOS n[        U R                  5       Hc  u  pU(       a  X4-   n
Ub  XM   OS nU" UUUUUUUS9nUS   nU(       d  M4  UUS   4-   nU R                   R                  (       d  MZ  UUS   4-   nMe     U(       a  X4-   n
[	        UU
UUS9$ )Nr!   r   r   r   r@   )last_hidden_stater   r    cross_attentions)r:   r  	enumerater+  r   )r9   r   re   r   r   r   r   r   r.  r/  all_hidden_statesall_self_attentionsall_cross_attentionsrd   layer_modulelayer_head_masklayer_outputss                    r-   rO   BrosEncoder.forward  s     #7BD$5b4%64;;;Z;Zr`d(4OA#$58H$H!.7.CilO(+)-)&;'="3M *!,M  &9]1=M<O&O#;;222+?=QRCSBU+U()  5,   14D D1++*1	
 	
r,   )r:   r+  )NNNNFFT)r"   r#   r$   r%   r5   r   r'   rR   r   r(   r   r   r*   r   rO   r+   rS   rT   s   @r-   r'  r'    s    a
 
 7;15=A>B,1/4&*.
||.
 ll.
 !!2!23	.

 E--..
  ((9(9:.
 !)):): ;.
 $D>.
 'tn.
 d^.
 
uU\\"$FF	G.
 .
r,   r'  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )
BrosPooleri  c                    > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " 5       U l        g rY   )r4   r5   r   rm   r   r   Tanh
activationr]   s     r-   r5   BrosPooler.__init__  s9    YYv1163E3EF
'')r,   r   r>   c                 \    US S 2S4   nU R                  U5      nU R                  U5      nU$ )Nr   )r   r?  )r9   r   first_token_tensorpooled_outputs       r-   rO   BrosPooler.forward  s6     +1a40

#566r,   )r?  r   rQ   rT   s   @r-   r<  r<    s(    $
U\\ ell  r,   r<  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )BrosRelationExtractori  c                 \  > [         TU ]  5         UR                  U l        UR                  U l        UR                  U l        UR                  U l        [        R                  " U R                  5      U l	        [        R                  " U R                  U R                  U R
                  -  5      U l        [        R                  " U R                  U R                  U R
                  -  5      U l        [        R                  " [        R                  " SU R                  5      5      U l        g )Nr   )r4   r5   n_relationsr   backbone_hidden_sizehead_hidden_sizeclassifier_dropout_probr   r   droprm   r   r   	Parameterr'   r   
dummy_noder]   s     r-   r5   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           	         U R                  U R                  U5      5      nU R                  R                  S5      R	                  SUR                  S5      S5      n[        R                  " X#/SS9nU R                  U R                  U5      5      nUR                  UR                  S5      UR                  S5      U R                  U R                  5      nUR                  UR                  S5      UR                  S5      U R                  U R                  5      n[        R                  " UR                  SSSS5      UR                  SSSS5      5      nU$ )Nr   r   axisr@   r   )r   rL  rN  	unsqueezerepeatrD   r'   rF   r   rE   rH  rJ  r   r   )r9   r   r   	dummy_vecrelation_scores        r-   rO   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,   )rI  rK  rL  rN  rJ  r   rH  r   rQ   rT   s   @r-   rF  rF    s)    R5<< ELL  r,   rF  c                   F    \ rS rSr% \\S'   SrS\R                  4S jr	Sr
g)BrosPreTrainedModeli=  r:   brosmodulec                 p   U R                   R                  n[        U[        R                  5      (       aW  UR
                  R                  R                  SUS9  UR                  b%  UR                  R                  R                  5         gg[        U[        R                  5      (       ad  UR
                  R                  R                  SUS9  UR                  b2  UR
                  R                  UR                     R                  5         gg[        U[        R                  5      (       aJ  UR                  R                  R                  5         UR
                  R                  R                  S5        g[        U[        5      (       a)  [        R                  R                  UR                   US9  gg)zInitialize the weightsr2   )meanstdNg      ?)r^  )r:   initializer_ranger  r   rm   weightdatanormal_rk   zero_r   r{   r   fill_rF  initrN  )r9   r[  r^  s      r-   _init_weights!BrosPreTrainedModel._init_weightsB  s2   kk++fbii(( MM&&CS&9{{&  &&( '--MM&&CS&9!!-""6#5#56<<> .--KK""$MM$$S) 566GGOOF--3O7 7r,   r!   N)r"   r#   r$   r%   r   r)   base_model_prefixr   Modulerf  r+   r!   r,   r-   rY  rY  =  s    8BII 8r,   rY  c                     ^  \ rS rSrSU 4S jjrS rS rS r\\	            SS\
\R                     S\
\R                     S\
\R                     S	\
\R                     S
\
\R                     S\
\R                     S\
\R                     S\
\R                     S\
\R                     S\
\   S\
\   S\
\   S\\\R                     \4   4S jj5       5       rSrU =r$ )	BrosModeliV  c                    > [         TU ]  U5        Xl        [        U5      U l        [        U5      U l        [        U5      U l        U(       a  [        U5      OSU l
        U R                  5         g)z^
add_pooling_layer (bool, *optional*, defaults to `True`):
    Whether to add a pooling layer
N)r4   r5   r:   rx   r   rh   bbox_embeddingsr'  encoderr<  poolerinit_weights)r9   r:   add_pooling_layerr;   s      r-   r5   BrosModel.__init__X  sX    
 	 ,V41&9"6*,=j(4r,   c                 .    U R                   R                  $ rY   r   r   )r9   s    r-   get_input_embeddingsBrosModel.get_input_embeddingsh  s    ...r,   c                 $    XR                   l        g rY   rt  )r9   r   s     r-   set_input_embeddingsBrosModel.set_input_embeddingsk  s    */'r,   c                     UR                  5        H7  u  p#U R                  R                  U   R                  R	                  U5        M9     g)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)itemsrn  r+  r  r   )r9   heads_to_pruner+  r   s       r-   _prune_headsBrosModel._prune_headsn  s<    
 +002LELLu%//;;EB 3r,   r   r_   r   r   r   r   r   r   r   r   r.  r/  r>   c                 ^   U
b  U
OU R                   R                  n
Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  Ub  [	        S5      eUb  UR                  5       nO"Ub  UR                  5       SS nO[	        S5      eUc  [	        S5      eUu  pUb  UR                  OUR                  nUc  [        R                  " UUS9nUcr  [        U R                  S5      (       a3  U R                  R                  SS2SU24   nUR                  X5      nUnO$[        R                  " U[        R                  US9nU R                  X=U5      nU R                   R                   (       aE  UbB  UR                  5       u  nnnUU4nU	c  [        R                  " UUS9n	U R#                  U	5      nOSnU R%                  X`R                   R&                  5      nU R                  UUUUS	9nUR(                  S   S
:X  a  USS2SS2/ SQ4   nX R                   R*                  -  nU R-                  U5      nU R/                  UUUUUUU
USS9	nUS   nU R0                  b  U R1                  U5      OSn[3        UUUR4                  UR6                  UR8                  S9$ )aM  
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 timerA   z5You have to specify either input_ids or inputs_embedszYou have to specify bbox)r   r   r   )r   r   r   r      )r   r   r@   r   r@   r   r   r   T)re   r   r   r   r   r   r.  r/  r   )r1  pooler_outputr   r    r2  )r:   r   r.  use_return_dictr   rD   r   r'   onesr   r   r   r   r   r   get_extended_attention_maskr   invert_attention_maskget_head_maskr*  r   
bbox_scalerm  rn  ro  r   r   r    r2  )r9   r   r_   r   r   r   r   r   r   r   r   r.  r/  r   r   r   r   r   r   extended_attention_maskencoder_batch_sizeencoder_sequence_lengthr,  encoder_hidden_shapeencoder_extended_attention_maskembedding_outputscaled_bboxbbox_position_embeddingsencoder_outputssequence_outputrC  s                                  r-   rO   BrosModel.forwardv  s   P 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B] ]%>cdd"#..*K&',,.s3KTUU<788!,
%.%:!!@T@T!"ZZFCN!t(899*.//*H*HKZK*X'3J3Q3QR\3i0!A!&[

SY!Z 150P0PQ_nt0u ;;!!&;&G=R=W=W=Y: 7$68O#P %-).4HQW)X&.2.H.HI_.`+.2+ &&y++2O2OP	??%)'	 + 
 ::b>Q1667D[[333#'#7#7#D ,,12"7#B/!5 ' 

 *!,8<8OO4UY;-')77&11,==
 	
r,   )rm  r:   r   rn  ro  )TNNNNNNNNNNNN)r"   r#   r$   r%   r5   ru  rx  r}  r   r   r   r'   rR   r   r   r*   r   rO   r+   rS   rT   s   @r-   rk  rk  V  sZ    /0C  -1'+1515/3,0048<9=,0/3&*}
ELL)}
 u||$}
 !.	}

 !.}
 u||,}
 ELL)}
  -}
  (5}
 !) 6}
 $D>}
 'tn}
 d^}
 
uU\\"$PP	Q}
  }
r,   rk  c                     ^  \ rS rSrS/rU 4S jr\\            SS\\	R                     S\\	R                     S\\	R                     S\\	R                     S\\	R                     S	\\	R                     S
\\	R                     S\\	R                     S\\	R                     S\\   S\\   S\\   S\\\	R                     \4   4S jj5       5       rSrU =r$ )BrosForTokenClassificationi  ro  c                 n  > [         TU ]  U5        UR                  U l        [        U5      U l        [        US5      (       a  UR                  OUR                  n[        R                  " U5      U l
        [        R                  " UR                  UR                  5      U l        U R                  5         g Nclassifier_dropout)r4   r5   
num_labelsrk  rZ  r   r  r   r   r   r   rm   r   
classifierrp  r9   r:   r  r;   s      r-   r5   #BrosForTokenClassification.__init__  s      ++f%	)09M)N)NF%%TZTnTn 	 zz"45))F$6$68I8IJr,   r   r_   r   bbox_first_token_maskr   r   r   r   labelsr   r.  r/  r>   c                    Ub  UOU R                   R                  nU R                  UUUUUUUU
USS9
nUS   nU R                  U5      nU R	                  U5      nSnU	b  [        5       nUbJ  UR                  S5      nU" UR                  SU R                  5      U   U	R                  S5      U   5      nO2U" UR                  SU R                  5      U	R                  S5      5      n[        UUUR                  UR                  S9$ )aP  
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)	r_   r   r   r   r   r   r   r.  r/  r   rA   r   logitsr   r    )r:   r  rZ  r   r  r   rE   r  r   r   r    )r9   r   r_   r   r  r   r   r   r   r  r   r.  r/  r   r  r  r   loss_fcts                     r-   rO   "BrosForTokenClassification.forward	  s   Z &1%<k$++B]B]))))%'/!5  
 "!*,,71')H$0(=(B(B2(F%KKDOO45JKV[[Y[_]rMs  B @&++b/R$!//))	
 	
r,   )rZ  r  r   r  r  r"   r#   r$   r%   "_keys_to_ignore_on_load_unexpectedr5   r   r   r   r'   rR   r   r   r*   r   rO   r+   rS   rT   s   @r-   r  r    sR   *3&  -1'+158<15/3,004)-,0/3&*O
ELL)O
 u||$O
 !.	O

  (5O
 !.O
 u||,O
 ELL)O
  -O
 &O
 $D>O
 'tnO
 d^O
 
uU\\"$99	:O
  O
r,   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            !         ^  \ rS rSrS/rU 4S jr\\             SS\\	R                     S\\	R                     S\\	R                     S\\	R                     S\\	R                     S	\\	R                     S
\\	R                     S\\	R                     S\\	R                     S\\	R                     S\\   S\\   S\\   S\\\	R                     \4   4S jj5       5       rSrU =r$ )!BrosSpadeEEForTokenClassificationi]  ro  c           	      x  > [         TU ]  U5        Xl        UR                  U l        UR                  U l        UR
                  U l        [        U5      U l        [        US5      (       a  UR                  OUR                  n[        R                  " [        R                  " U5      [        R                  " UR
                  UR
                  5      [        R                  " U5      [        R                  " UR
                  UR                  5      5      U l        [#        U5      U l        U R'                  5         g r  )r4   r5   r:   r  rH  r   rI  rk  rZ  r   r  r   r   
Sequentialr   rm   initial_token_classifierrF  subsequent_token_classifierrp  r  s      r-   r5   *BrosSpadeEEForTokenClassification.__init__i  s      ++!--$*$6$6!f%	)09M)N)NF%%TZTnTn 	
 )+JJ)*IIf((&*<*<=JJ)*IIf((&*;*;<	)
% ,A+H(r,   r   r_   r   r  r   r   r   r   initial_token_labelssubsequent_token_labelsr   r.  r/  r>   c                    Ub  UOU R                   R                  nU R                  UUUUUUUUUSS9
nUS   nUR                  SS5      R	                  5       nU R                  U5      R                  SS5      R	                  5       nU R                  X5      R                  S5      nSU-
  nUR                  u  nnUR                  n[        R                  " U[        R                  " US/5      R                  U5      /SS9R                  5       nUR                  USS2SSS24   [        R                   " UR"                  5      R$                  5      n[        R&                  " UUS-   5      R                  U[        R                  S9nUR                  USSS2SS24   [        R                   " UR"                  5      R$                  5      nUR)                  S5      R                  5       nSnU	b  U
b  [+        5       nU	R)                  S5      n	Ub:  UR)                  S5      nU" UR)                  SU R,                  5      U   X   5      nO#U" UR)                  SU R,                  5      U	5      nU
R)                  S5      n
U" UR)                  SUS-   5      U   U
U   5      nUU-   n[/        UUUUR0                  UR2                  S	9$ )
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
r   r_   r   r   r   r   r   r   r.  r/  r   r   rQ  r   r   rA   )r   r   r   r   r    )r:   r  rZ  rs   r   r  r  squeezer   r   r'   rF   r   r   r   masked_fillfinfor   mineyerE   r   r  r   r   r    )r9   r   r_   r   r  r   r   r   r   r  r  r   r.  r/  r   last_hidden_statesr   r   inv_attention_maskr   max_seq_lengthr   invalid_token_maskself_token_masksubsequent_token_maskr   r  initial_token_losssubsequent_token_losss                                r-   rO   )BrosSpadeEEForTokenClassification.forward  s   d &1%<k$++B]B]))))%'/!5  
 %QZ/99!Q?JJL#<<=OPZZ[\^_`kkm"&"B"BCU"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,   )rI  rZ  r:   r  rH  r  r  )NNNNNNNNNNNNN)r"   r#   r$   r%   r  r5   r   r   r   r'   rR   r   r   r*   r   rO   r+   rS   rT   s   @r-   r  r  ]  sj    +4&2  -1'+158<15/3,0047;:>,0/3&*o
ELL)o
 u||$o
 !.	o

  (5o
 !.o
 u||,o
 ELL)o
  -o
 'u||4o
 "*%,,!7o
 $D>o
 'tno
 d^o
 
uU\\"O3	4o
  o
r,   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                     ^  \ rS rSrS/rU 4S jr\\            SS\\	R                     S\\	R                     S\\	R                     S\\	R                     S\\	R                     S	\\	R                     S
\\	R                     S\\	R                     S\\	R                     S\\   S\\   S\\   S\\\	R                     \4   4S jj5       5       rSrU =r$ )!BrosSpadeELForTokenClassificationi  ro  c                 H  > [         TU ]  U5        Xl        UR                  U l        UR                  U l        UR
                  U l        [        U5      U l        [        US5      (       a  UR                  OUR                    [        U5      U l        U R                  5         g r  )r4   r5   r:   r  rH  r   rI  rk  rZ  r   r  r   rF  entity_linkerrp  r]   s     r-   r5   *BrosSpadeELForTokenClassification.__init__  s      ++!--$*$6$6!f%	&-f6J&K&K	"	"QWQkQk26:r,   r   r_   r   r  r   r   r   r   r  r   r.  r/  r>   c                     Ub  UOU R                   R                  nU R                  UUUUUUUU
USS9
nUS   nUR                  SS5      R	                  5       nU R                  X5      R                  S5      nSnU	Gba  [        5       nUR                  u  nnUR                  n[        R                  " UUS-   5      R                  U[        R                  S9nUR                  S5      n[        R                  " U) [        R                   " US/[        R                  US9/SS	9nUR#                  USS2SSS24   [        R$                  " UR&                  5      R(                  5      nUR#                  USSS2SS24   [        R$                  " UR&                  5      R(                  5      nU" UR                  SUS-   5      U   U	R                  S5      U   5      n[+        UUUR,                  UR.                  S
9$ )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  rA   r   rQ  r  )r:   r  rZ  rs   r   r  r  r   r   r   r'   r  r   r   rE   rF   r   r  r  r   r  r   r   r    )r9   r   r_   r   r  r   r   r   r   r  r   r.  r/  r   r  r  r   r  r   r  r   r  masks                          r-   rO   )BrosSpadeELForTokenClassification.forward  s   X &1%<k$++B]B]))))%'/!5  
 %QZ/99!Q?JJL##$6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,   )rI  rZ  r:   r  rH  r  r  r  rT   s   @r-   r  r    sT    +4&  -1'+158<15/3,004)-,0/3&*Y
ELL)Y
 u||$Y
 !.	Y

  (5Y
 !.Y
 u||,Y
 ELL)Y
  -Y
 &Y
 $D>Y
 'tnY
 d^Y
 
uU\\"$99	:Y
  Y
r,   r  )rY  rk  r  r  r  )9r&   r   dataclassesr   typingr   r   r'   torch.utils.checkpointr   torch.nnr   activationsr	   modeling_layersr
   modeling_outputsr   r   r   modeling_utilsr   pytorch_utilsr   r   r   utilsr   r   r   r   configuration_brosr   
get_loggerr"   loggerr   ri  r/   rV   rh   rx   r   r   r   r  r  r  r'  r<  rF  rY  rk  r  r  r  __all__r!   r,   r-   <module>r     s     ! "    % ! 9 
 . l l K K * 
		H	% 
:k : :"		 *		 & > >Bi		 iZRYY 1BII 1jryy  J* JZ5
")) 5
r BII D 8/ 8 80 ^
# ^
 ^
B a
!4 a
 a
H M
(; M
M
` l
(; l
l
^r,   