
    Z j3                     :   S r SSKrSSK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  SSKJr  SS	KJrJrJrJrJr  SS
KJr  SSKJr  SSKJrJr  SSKJr  \R<                  " \5      r  " S S\RB                  5      r" " S S\RB                  5      r# " S S\RB                  5      r$ " S S\RB                  5      r% " S S\RB                  5      r& " S S\RB                  5      r' " S S\RB                  5      r( " S S\5      r) " S S \RB                  5      r* " S! S"\RB                  5      r+\ " S# S$\5      5       r,\ " S% S&\,5      5       r-\" S'S(9 " S) S*\,5      5       r.\ " S+ S,\,5      5       r/ " S- S.\RB                  5      r0\ " S/ S0\,5      5       r1/ S1Qr2g)2zPyTorch LiLT model.    N)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )initialization)ACT2FN)GradientCheckpointingLayer)BaseModelOutputBaseModelOutputWithPoolingQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel)apply_chunking_to_forward)auto_docstringlogging   )
LiltConfigc                   F   ^  \ rS rSrU 4S jr    SS jrS rS rSrU =r	$ )LiltTextEmbeddings)   c                 
  > [         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 R#                  S[$        R&                  " UR                  5      R)                  S5      SS9  UR                  U l        [        R                  " UR                  UR
                  U R*                  S9U l	        g )Npadding_idxepsposition_idsr   F)
persistent)super__init__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register_buffertorcharangeexpandr   selfconfig	__class__s     w/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/lilt/modeling_lilt.pyr#   LiltTextEmbeddings.__init__*   s   !||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c %'\\&2H2H&J\J\%]"f&8&8f>S>STzz&"<"<= 	ELL)G)GHOOPWXej 	 	

 "..#%<<**F,>,>DL\L\$
     c                 $   UcI  Ub5  U R                  XR                  5      R                  UR                  5      nOU R	                  U5      nUb  UR                  5       nOUR                  5       S S nUc8  [        R                  " U[        R                  U R                  R                  S9nUc  U R                  U5      nU R                  U5      nXF-   nU R                  U5      nXx-  nU R                  U5      nU R                  U5      nXs4$ )Nr    dtypedevice)"create_position_ids_from_input_idsr   tor@   &create_position_ids_from_inputs_embedssizer3   zeroslongr   r(   r,   r*   r-   r1   )	r7   	input_idstoken_type_idsr   inputs_embedsinput_shaper,   
embeddingsr*   s	            r:   forwardLiltTextEmbeddings.forward>   s    $#FFyRbRbcff$$   $JJ=Y #..*K',,.s3K!"[[EJJtO`O`OgOghN  00;M $ : :> J":
"66|D)
^^J/
\\*-
''r<   c                     UR                  U5      R                  5       n[        R                  " USS9R	                  U5      U-  nUR                  5       U-   $ )z
Args:
Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding
symbols are ignored. This is modified from fairseq's `utils.make_positions`.
    x: torch.Tensor x:
Returns: torch.Tensor
r   dim)neintr3   cumsumtype_asrF   )r7   rG   r   maskincremental_indicess        r:   rA   5LiltTextEmbeddings.create_position_ids_from_input_idsb   sP     ||K(,,.$||Da8@@F$N"'')K77r<   c                    UR                  5       SS nUS   n[        R                  " U R                  S-   X0R                  -   S-   [        R                  UR
                  S9nUR                  S5      R                  U5      $ )z
Args:
We are provided embeddings directly. We cannot infer which are padded so just generate sequential position ids.:
    inputs_embeds: torch.Tensor
Returns: torch.Tensor
Nr    r   r>   r   )rD   r3   r4   r   rF   r@   	unsqueezer5   )r7   rI   rJ   sequence_lengthr   s        r:   rC   9LiltTextEmbeddings.create_position_ids_from_inputs_embedso   s~     $((*3B/%a.||q /4D4D"Dq"HPUPZPZcpcwcw
 %%a(//<<r<   )r-   r1   r   r*   r,   r(   )NNNN)
__name__
__module____qualname____firstlineno__r#   rL   rA   rC   __static_attributes____classcell__r9   s   @r:   r   r   )   s+    
, "(H8= =r<   r   c                   2   ^  \ rS rSrU 4S jrSS jrSrU =r$ )LiltLayoutEmbeddings   c                   > [         TU ]  5         [        R                  " UR                  UR
                  S-  5      U l        [        R                  " UR                  UR
                  S-  5      U l        [        R                  " UR                  UR
                  S-  5      U l        [        R                  " UR                  UR
                  S-  5      U l	        UR                  U l        [        R                  " UR                  UR
                  UR                  -  U R                  S9U l        [        R                  " UR
                  UR
                  UR                  -  S9U l        [        R"                  " UR
                  UR                  -  UR$                  S9U l        [        R&                  " UR(                  5      U l        g )N   r   )in_featuresout_featuresr   )r"   r#   r   r$   max_2d_position_embeddingsr&   x_position_embeddingsy_position_embeddingsh_position_embeddingsw_position_embeddingsr'   r   r)   channel_shrink_ratiobox_position_embeddingsLinearbox_linear_embeddingsr-   r.   r/   r0   r1   r6   s     r:   r#   LiltLayoutEmbeddings.__init__   s^    &(\\&2S2SU[UgUgklUl%m"%'\\&2S2SU[UgUgklUl%m"%'\\&2S2SU[UgUgklUl%m"%'\\&2S2SU[UgUgklUl%m"!..')||**&"="==(((
$
 &(YY**9K9KvOjOj9j&
" f&8&8F<W<W&W]c]r]rszz&"<"<=r<   c                     U R                  US S 2S S 2S4   5      nU R                  US S 2S S 2S4   5      nU R                  US S 2S S 2S4   5      nU R                  US S 2S S 2S4   5      nU R                  US S 2S S 2S4   US S 2S S 2S4   -
  5      nU R	                  US S 2S S 2S4   US S 2S S 2S4   -
  5      n	[
        R                  " UUUUUU	/SS9n
U R                  U
5      n
U R                  U5      nX-   n
U R                  U
5      n
U R                  U
5      n
U
$ ! [         a  n[        S5      UeS nAff = f)Nr   r      r   z;The `bbox` coordinate values should be within 0-1000 range.r    rO   )rk   rl   
IndexErrorrm   rn   r3   catrr   rp   r-   r1   )r7   bboxr   left_position_embeddingsupper_position_embeddingsright_position_embeddingslower_position_embeddingserm   rn   spatial_position_embeddingsrp   s               r:   rL   LiltLayoutEmbeddings.forward   st   	c'+'A'A$q!Qw-'P$(,(B(B41a=(Q%(,(B(B41a=(Q%(,(B(B41a=(Q% !% : :41a=4PQSTVWPW=;X Y $ : :41a=4PQSTVWPW=;X Y&+ii()))%% 
'
# '+&@&@A\&]#"&">">|"L&A&[#&*nn5P&Q#&*ll3N&O#**3  	cZ[abb	cs   A,D# #
D>-D99D>)	r-   rr   rp   r1   rm   r   rn   rk   rl   )NN)r\   r]   r^   r_   r#   rL   r`   ra   rb   s   @r:   rd   rd      s    >*+ +r<   rd   c                   D   ^  \ rS rSrSU 4S jjrSS jr  SS jrSrU =r$ )	LiltSelfAttention   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                  UR                  -  U R                  UR                  -  5      U l        [        R                  " UR                  UR                  -  U R                  UR                  -  5      U l        [        R                  " UR                  UR                  -  U R                  UR                  -  5      U l        [        R$                  " UR&                  5      U l        UR                  U l        X l        g )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ())r"   r#   r&   num_attention_headshasattr
ValueErrorrR   attention_head_sizeall_head_sizer   rq   querykeyvaluero   layout_query
layout_keylayout_valuer/   attention_probs_dropout_probr1   	layer_idx)r7   r8   r   r9   s      r:   r#   LiltSelfAttention.__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
II&"="==t?Q?QU[UpUp?p
 ))&"="==t?Q?QU[UpUp?p
 II&"="==t?Q?QU[UpUp?p
 zz&"E"EF$*$?$?!"r<   c                     UR                  5       S S U R                  U R                  U-  4-   nUR                  " U6 nUR	                  SSSS5      $ )Nr    r   ru   r   r   )rD   r   r   viewpermute)r7   xrnew_x_shapes       r:   transpose_for_scores&LiltSelfAttention.transpose_for_scores   sR    ffhsmt'?'?AYAY]^A^&__FFK yyAq!$$r<   c                    U R                  U R                  U5      U R                  S9nU R                  U R                  U5      U R                  S9nU R                  U R	                  U5      U R                  S9nU R                  U5      nU R                  U R                  U5      5      n	U R                  U R                  U5      5      n
U R                  U5      n[        R                  " XR                  SS5      5      n[        R                  " XvR                  SS5      5      nU[        R                  " U R                  5      -  nU[        R                  " U R                  U R                  -  5      -  nX-   nX-   nUb  X-   n[        R                  " SS9" U5      nU R!                  U5      n[        R                  " UU5      nUR#                  SSSS5      R%                  5       nUR'                  5       S S U R(                  U R                  -  4-   nUR*                  " U6 nUb  X-   n[        R                  " SS9" U5      nU R!                  U5      n[        R                  " UU
5      nUR#                  SSSS5      R%                  5       nUR'                  5       S S U R(                  4-   nUR*                  " U6 nUU4nU(       a  UU4-   nU$ )	N)r   r    rO   r   ru   r   r   )r   r   ro   r   r   r   r   r   r3   matmul	transposemathsqrtr   r   Softmaxr1   r   
contiguousrD   r   r   )r7   hidden_stateslayout_inputsattention_maskoutput_attentionslayout_value_layerlayout_key_layerlayout_query_layermixed_query_layer	key_layervalue_layerquery_layerattention_scoreslayout_attention_scorestmp_attention_scorestmp_layout_attention_scoreslayout_attention_probslayout_context_layernew_context_layer_shapeattention_probscontext_layeroutputss                         r:   rL   LiltSelfAttention.forward   s    "66t7H7H7W[_[t[t6u44T__]5SW[WpWp4q!66t7H7H7W[_[t[t6u JJ}5--dhh}.EF	//

=0IJ//0AB <<5H5HR5PQ"',,/AC]C]^`bdCe"f/$))D<T<T2UU&=		$$(A(AAA
 '
# 0M"="T%&=&N# "$!34K!L "&.D!E$||,BDVW3;;Aq!QGRRT"6";";"=cr"BdFXFX\`\u\uFuEw"w388:QR%/@ **,-=> ,,7_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**,CD "67 22Gr<   )r   r   ro   r1   r   r   r   r   r   r   r   r   N)r   NF)	r\   r]   r^   r_   r#   r   rL   r`   ra   rb   s   @r:   r   r      s!    #>% A A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	$ )LiltSelfOutputi!  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   )r"   r#   r   rq   r&   denser-   r.   r/   r0   r1   r6   s     r:   r#   LiltSelfOutput.__init__"  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=r<   r   input_tensorreturnc                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ r   r   r1   r-   r7   r   r   s      r:   rL   LiltSelfOutput.forward(  5    

=1]3}'CDr<   r-   r   r1   
r\   r]   r^   r_   r#   r3   TensorrL   r`   ra   rb   s   @r:   r   r   !  6    >U\\  RWR^R^  r<   r   c                      ^  \ rS rSrSU 4S jjr  SS\R                  S\R                  S\R                  S-  S\S-  S\	\R                     4
S	 jjr
S
rU =r$ )LiltAttentioni/  Nc                    > [         TU ]  5         [        XS9U l        [	        U5      U l        UR                  nUR                  UR                  -  Ul        [	        U5      U l        X1l        g )Nr   )	r"   r#   r   r7   r   outputr&   ro   layout_output)r7   r8   r   ori_hidden_sizer9   s       r:   r#   LiltAttention.__init__0  s]    %fB	$V, ,,#//63N3NN+F3,r<   r   r   r   r   r   c                     U R                  UUUU5      nU R                  US   U5      nU R                  US   U5      nXg4USS  -   nU$ )Nr   r   ru   )r7   r   r   )	r7   r   r   r   r   self_outputsattention_outputlayout_attention_outputr   s	            r:   rL   LiltAttention.forward:  se     yy	
  ;;|AF"&"4"4\!_m"T#=QR@PPr<   )r   r   r7   r   r   )r\   r]   r^   r_   r#   r3   r   FloatTensorbooltuplerL   r`   ra   rb   s   @r:   r   r   /  sj    - 48).|| || ))D0	
  $; 
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	$ )LiltIntermediateiN  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 r   )r"   r#   r   rq   r&   intermediate_sizer   
isinstance
hidden_actstrr	   intermediate_act_fnr6   s     r:   r#   LiltIntermediate.__init__O  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$ r   r   r   )r7   r   s     r:   rL   LiltIntermediate.forwardW  s&    

=100?r<   r   r   rb   s   @r:   r   r   N  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	$ )
LiltOutputi^  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   )r"   r#   r   rq   r   r&   r   r-   r.   r/   r0   r1   r6   s     r:   r#   LiltOutput.__init___  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$ r   r   r   s      r:   rL   LiltOutput.forwarde  r   r<   r   r   rb   s   @r:   r   r   ^  r   r<   r   c                      ^  \ rS rSrSU 4S jjr  SS\R                  S\R                  S\R                  S-  S\S-  S\	\R                     4
S	 jjr
S
 rS rSrU =r$ )	LiltLayeril  Nc                   > [         TU ]  5         UR                  U l        SU l        [	        XS9U l        [        U5      U l        [        U5      U l	        UR                  nUR                  nUR                  UR                  -  Ul
        UR                  UR                  -  Ul        [        U5      U l        [        U5      U l        X1l
        XAl        g )Nr   r   )r"   r#   chunk_size_feed_forwardseq_len_dimr   	attentionr   intermediater   r   r&   r   ro   layout_intermediater   )r7   r8   r   r   ori_intermediate_sizer9   s        r:   r#   LiltLayer.__init__m  s    '-'E'E$&vC,V4 ( ,, & 8 8#//63N3NN#)#;#;v?Z?Z#Z #3F#; '/,#8 r<   r   r   r   r   r   c                    U R                  UUUUS9nUS   nUS   nUSS  n[        U R                  U R                  U R                  U5      n	[        U R
                  U R                  U R                  U5      n
X4U-   nU$ )N)r   r   r   ru   )r   r   feed_forward_chunkr   r   layout_feed_forward_chunk)r7   r   r   r   r   self_attention_outputsr   r   r   layer_outputlayout_layer_outputs              r:   rL   LiltLayer.forward~  s     "&/	 "0 "
 2!4"8";(,0##T%A%A4CSCSUe
 8**D,H,H$JZJZ\s
  5?r<   c                 J    U R                  U5      nU R                  X!5      nU$ r   )r   r   r7   r   intermediate_outputr   s       r:   r   LiltLayer.feed_forward_chunk  s)    "//0@A{{#6Ir<   c                 J    U R                  U5      nU R                  X!5      nU$ r   )r   r   r  s       r:   r   #LiltLayer.layout_feed_forward_chunk  s+    "667GH))*=Pr<   )r   r   r   r   r   r   r   r   r   )r\   r]   r^   r_   r#   r3   r   r   r   r   rL   r   r   r`   ra   rb   s   @r:   r   r   l  st    9* 48).|| || ))D0	
  $; 
u||	:
 r<   r   c                      ^  \ rS rSrU 4S jr    SS\R                  S\R                  S\R                  S-  S\S-  S\S-  S	\S-  S
\	\R                     \
-  4S jjrSrU =r$ )LiltEncoderi  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 r   )	r"   r#   r8   r   
ModuleListrangenum_hidden_layersr   layer)r7   r8   _r9   s      r:   r#   LiltEncoder.__init__  sG    ]]uVE]E]?^#_?^!If$5?^#_`
#_s   ANr   r   r   r   output_hidden_statesreturn_dictr   c                 4   U(       a  SOS nU(       a  SOS n[        U R                  5       H7  u  pU(       a  Xq4-   nU
" UUUU5      nUS   nUS   nU(       d  M/  XS   4-   nM9     U(       a  Xq4-   nU(       d  [        S UUU4 5       5      $ [        UUUS9$ )N r   r   ru   c              3   0   #    U  H  nUc  M  Uv   M     g 7fr   r  ).0vs     r:   	<genexpr>&LiltEncoder.forward.<locals>.<genexpr>  s"      A
  s   	)last_hidden_stater   
attentions)	enumerater  r   r   )r7   r   r   r   r   r  r  all_hidden_statesall_self_attentionsilayer_modulelayer_outputss               r:   rL   LiltEncoder.forward  s     #7BD$5b4(4OA#$58H$H!(!	M *!,M)!,M  &91=M<O&O#  5"   14D D  "%'   ++*
 	
r<   )r8   r  )NFFT)r\   r]   r^   r_   r#   r3   r   r   r   r   r   rL   r`   ra   rb   s   @r:   r  r    s    a 48).,1#'.
||.
 ||.
 ))D0	.

  $;.
 #Tk.
 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	$ )
LiltPooleri  c                    > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " 5       U l        g r   )r"   r#   r   rq   r&   r   Tanh
activationr6   s     r:   r#   LiltPooler.__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%  )r7   r   first_token_tensorpooled_outputs       r:   rL   LiltPooler.forward  s6     +1a40

#566r<   )r%  r   r   rb   s   @r:   r"  r"    s(    $
U\\ ell  r<   r"  c                   @   ^  \ rS rSr% \\S'   SrSr/ rU 4S jr	Sr
U =r$ )LiltPreTrainedModeli  r8   liltTc                   > [         TU ]  U5        [        U[        5      (       a\  [        R
                  " UR                  [        R                  " UR                  R                  S   5      R                  S5      5        g g )Nr    r   )r"   _init_weightsr   r   initcopy_r   r3   r4   shaper5   )r7   moduler9   s     r:   r0  !LiltPreTrainedModel._init_weights  s^    f%f011JJv**ELL9L9L9R9RSU9V,W,^,^_f,gh 2r<   r  )r\   r]   r^   r_   r   __annotations__base_model_prefixsupports_gradient_checkpointing_no_split_modulesr0  r`   ra   rb   s   @r:   r-  r-    s'    &*#i ir<   r-  c                   X  ^  \ rS rSrSU 4S jjrS rS r\         SS\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\R                  S-  S\
S-  S\
S-  S\
S-  S\\R                     \-  4S jj5       rSrU =r$ )	LiltModeli  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)r"   r#   r8   r   rK   rd   layout_embeddingsr  encoderr"  pooler	post_init)r7   r8   add_pooling_layerr9   s      r:   r#   LiltModel.__init__  sX    
 	 ,V4!5f!="6*,=j(4 	r<   c                 .    U R                   R                  $ r   rK   r(   )r7   s    r:   get_input_embeddingsLiltModel.get_input_embeddings  s    ...r<   c                 $    XR                   l        g r   rD  )r7   r   s     r:   set_input_embeddingsLiltModel.set_input_embeddings  s    */'r<   NrG   rx   r   rH   r   rI   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                  X5        UR                  5       nO"Ub  UR                  5       SS nO[	        S5      eUu  pUb  UR                  OUR                  nUc'  [        R                  " US-   [        R                  US9nUc  [        R                  " X4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;5      nU R                  UUUUS	9u  nnU R#                  X%S
9nU R%                  UUUUUU	S9nUS   nU R&                  b  U R'                  U5      OSnU	(       d
  UU4USS -   $ [)        UUUR*                  UR,                  S9$ )a-  
bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
    Bounding boxes of each input sequence tokens. Selected in the range `[0,
    config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
    format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
    y1) represents the position of the lower right corner. See [Overview](#Overview) for normalization.

Examples:

```python
>>> from transformers import AutoTokenizer, AutoModel
>>> from datasets import load_dataset

>>> tokenizer = AutoTokenizer.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")
>>> model = AutoModel.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")

>>> dataset = load_dataset("nielsr/funsd-layoutlmv3", split="train")
>>> example = dataset[0]
>>> words = example["tokens"]
>>> boxes = example["bboxes"]

>>> encoding = tokenizer(words, boxes=boxes, return_tensors="pt")

>>> outputs = model(**encoding)
>>> last_hidden_states = outputs.last_hidden_state
```NzDYou cannot specify both input_ids and inputs_embeds at the same timer    z5You have to specify either input_ids or inputs_embeds)   r>   )r@   rH   )rG   r   rH   rI   )rx   r   )r   r   r  r  r   r   )r  pooler_outputr   r  )r8   r   r  r  r   %warn_if_padding_and_no_attention_maskrD   r@   r3   rE   rF   onesr   rK   rH   r5   get_extended_attention_maskr=  r>  r?  r   r   r  )r7   rG   rx   r   rH   r   rI   r   r  r  kwargsrJ   
batch_size
seq_lengthr@   buffered_token_type_ids buffered_token_type_ids_expandedextended_attention_maskembedding_outputlayout_embedding_outputencoder_outputssequence_outputr*  s                          r:   rL   LiltModel.forward  sF   P 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++BYBY ]%>cdd"66yQ#..*K&',,.s3KTUU!,
%.%:!!@T@T<;;{T1FSD!"ZZ*)A6RN!t(899*.//*H*HKZK*X'3J3Q3QR\3i0!A!&[

SY!Z 150P0PQ_0m)-%)'	 *9 *
&, #'"8"8d"8"^,,#2/!5# ' 
 *!,8<8OO4UY#]3oab6III)-')77&11	
 	
r<   )r8   rK   r>  r=  r?  )T)	NNNNNNNNN)r\   r]   r^   r_   r#   rE  rH  r   r3   r   r   r   r   rL   r`   ra   rb   s   @r:   r;  r;    s   "/0  *.$(.2.2,0-1)-,0#'h
<<$&h
 llT!h
 t+	h

 t+h
 llT)h
 ||d*h
  $;h
 #Tkh
 D[h
 
u||	9	9h
 h
r<   r;  z
    LiLT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled
    output) e.g. for GLUE tasks.
    )custom_introc                   h  ^  \ rS rSrU 4S jr\          SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\
S-  S\
S-  S\
S-  S\\R                     \-  4S jj5       rSrU =r$ )LiltForSequenceClassificationi  c                    > [         TU ]  U5        UR                  U l        Xl        [	        USS9U l        [        U5      U l        U R                  5         g NF)rA  )	r"   r#   
num_labelsr8   r;  r.  LiltClassificationHead
classifierr@  r6   s     r:   r#   &LiltForSequenceClassification.__init__  sH      ++f>	08 	r<   NrG   rx   r   rH   r   rI   labelsr   r  r  r   c                 f   U
b  U
OU R                   R                  n
U R                  UUUUUUUU	U
S9	nUS   nU R                  U5      nSnUGb  UR	                  UR
                  5      nU R                   R                  c  U R                  S:X  a  SU R                   l        OoU R                  S:  aN  UR                  [        R                  :X  d  UR                  [        R                  :X  a  SU R                   l        OSU R                   l        U R                   R                  S:X  aI  [        5       nU R                  S:X  a&  U" UR                  5       UR                  5       5      nOU" X5      nOU R                   R                  S:X  a=  [        5       nU" UR                  SU R                  5      UR                  S5      5      nO,U R                   R                  S:X  a  [!        5       nU" X5      nU
(       d  U4US	S -   nUb  U4U-   $ U$ [#        UUUR$                  UR&                  S
9$ )a  
bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
    Bounding boxes of each input sequence tokens. Selected in the range `[0,
    config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
    format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
    y1) represents the position of the lower right corner. See [Overview](#Overview) for normalization.
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
    Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
    config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
    `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

Examples:

```python
>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification
>>> from datasets import load_dataset

>>> tokenizer = AutoTokenizer.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")
>>> model = AutoModelForSequenceClassification.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")

>>> dataset = load_dataset("nielsr/funsd-layoutlmv3", split="train")
>>> example = dataset[0]
>>> words = example["tokens"]
>>> boxes = example["bboxes"]

>>> encoding = tokenizer(words, boxes=boxes, return_tensors="pt")

>>> outputs = model(**encoding)
>>> predicted_class_idx = outputs.logits.argmax(-1).item()
>>> predicted_class = model.config.id2label[predicted_class_idx]
```Nrx   r   rH   r   rI   r   r  r  r   r   
regressionsingle_label_classificationmulti_label_classificationr    ru   losslogitsr   r  )r8   r  r.  rb  rB   r@   problem_typer`  r?   r3   rF   rR   r   squeezer   r   r   r   r   r  r7   rG   rx   r   rH   r   rI   rd  r   r  r  rP  r   rY  rl  rk  loss_fctr   s                     r:   rL   %LiltForSequenceClassification.forward  s   \ &1%<k$++BYBY))))%'/!5#  

 "!*1YYv}}-F{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#F3D))-JJ+-B @&++b/R))-II,./Y,F)-)9TGf$EvE'!//))	
 	
r<   )rb  r8   r.  r`  
NNNNNNNNNN)r\   r]   r^   r_   r#   r   r3   
LongTensorr   r   r   r   r   rL   r`   ra   rb   s   @r:   r]  r]    s   	  .2$(37260426*.)-,0#'_
##d*_
 llT!_
 ))D0	_

 ((4/_
 &&-_
 ((4/_
   4'_
  $;_
 #Tk_
 D[_
 
u||	7	7_
 _
r<   r]  c                   h  ^  \ rS rSrU 4S jr\          SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\	S-  S\	S-  S\	S-  S\
\R                     \-  4S jj5       rSrU =r$ )LiltForTokenClassificationi  c                 d  > [         TU ]  U5        UR                  U l        [        USS9U l        UR
                  b  UR
                  OUR                  n[        R                  " U5      U l	        [        R                  " UR                  UR                  5      U l        U R                  5         g r_  )r"   r#   r`  r;  r.  classifier_dropoutr0   r   r/   r1   rq   r&   rb  r@  r7   r8   rw  r9   s      r:   r#   #LiltForTokenClassification.__init__  s      ++f>	)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	r<   NrG   rx   r   rH   r   rI   rd  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9	nUS   nU R                  U5      nU R	                  U5      nSnUbW  UR                  UR                  5      n[        5       nU" UR                  SU R                  5      UR                  S5      5      nU
(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )a   
bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
    Bounding boxes of each input sequence tokens. Selected in the range `[0,
    config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
    format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
    y1) represents the position of the lower right corner. See [Overview](#Overview) for normalization.
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.

Examples:

```python
>>> from transformers import AutoTokenizer, AutoModelForTokenClassification
>>> from datasets import load_dataset

>>> tokenizer = AutoTokenizer.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")
>>> model = AutoModelForTokenClassification.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")

>>> dataset = load_dataset("nielsr/funsd-layoutlmv3", split="train")
>>> example = dataset[0]
>>> words = example["tokens"]
>>> boxes = example["bboxes"]

>>> encoding = tokenizer(words, boxes=boxes, return_tensors="pt")

>>> outputs = model(**encoding)
>>> predicted_class_indices = outputs.logits.argmax(-1)
```Nrf  r   r    ru   rj  )r8   r  r.  r1   rb  rB   r@   r   r   r`  r   r   r  ro  s                     r:   rL   "LiltForTokenClassification.forward  s	   V &1%<k$++BYBY))))%'/!5#  

 "!*,,71YYv}}-F')HFKKDOO<fkk"oNDY,F)-)9TGf$EvE$!//))	
 	
r<   )rb  r1   r.  r`  rr  )r\   r]   r^   r_   r#   r   r3   rs  r   r   r   r   r   rL   r`   ra   rb   s   @r:   ru  ru    s     .2(,37260426*.)-,0#'M
##d*M
 %M
 ))D0	M

 ((4/M
 &&-M
 ((4/M
   4'M
  $;M
 #TkM
 D[M
 
u||	4	4M
 M
r<   ru  c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )ra  iX  z-Head for sentence-level classification tasks.c                 b  > [         TU ]  5         [        R                  " UR                  UR                  5      U l        UR                  b  UR                  OUR                  n[        R                  " U5      U l	        [        R                  " UR                  UR                  5      U l        g r   )r"   r#   r   rq   r&   r   rw  r0   r/   r1   r`  out_projrx  s      r:   r#   LiltClassificationHead.__init__[  s    YYv1163E3EF
)/)B)B)NF%%TZTnTn 	 zz"45		&"4"4f6G6GHr<   c                     US S 2SS S 24   nU R                  U5      nU R                  U5      n[        R                  " U5      nU R                  U5      nU R	                  U5      nU$ r(  )r1   r   r3   tanhr~  )r7   featuresrP  r   s       r:   rL   LiltClassificationHead.forwardd  sY    Q1WLLOJJqMJJqMLLOMM!r<   )r   r1   r~  )	r\   r]   r^   r_   __doc__r#   rL   r`   ra   rb   s   @r:   ra  ra  X  s    7I r<   ra  c                     ^  \ rS rSrU 4S jr\           SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\R                  S-  S\	S-  S\	S-  S\	S-  S\
\R                     \-  4S jj5       rSrU =r$ )LiltForQuestionAnsweringin  c                    > [         TU ]  U5        UR                  U l        [        USS9U l        [
        R                  " UR                  UR                  5      U l        U R                  5         g r_  )
r"   r#   r`  r;  r.  r   rq   r&   
qa_outputsr@  r6   s     r:   r#   !LiltForQuestionAnswering.__init__q  sU      ++f>	))F$6$68I8IJ 	r<   NrG   rx   r   rH   r   rI   start_positionsend_positionsr   r  r  r   c                 &   Ub  UOU R                   R                  nU R                  UUUUUUU	U
US9	nUS   nU R                  U5      nUR	                  SSS9u  nnUR                  S5      R                  5       nUR                  S5      R                  5       nSnUb  Ub  [        UR                  5       5      S:  a  UR                  S5      n[        UR                  5       5      S:  a  UR                  S5      nUR                  S5      nUR                  SU5      nUR                  SU5      n[        US9nU" UU5      nU" UU5      nUU-   S-  nU(       d  UU4USS -   nUb  U4U-   $ U$ [        UUUUR                  UR                  S	9$ )
a*  
bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
    Bounding boxes of each input sequence tokens. Selected in the range `[0,
    config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
    format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
    y1) represents the position of the lower right corner. See [Overview](#Overview) for normalization.

Examples:

```python
>>> from transformers import AutoTokenizer, AutoModelForQuestionAnswering
>>> from datasets import load_dataset

>>> tokenizer = AutoTokenizer.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")
>>> model = AutoModelForQuestionAnswering.from_pretrained("SCUT-DLVCLab/lilt-roberta-en-base")

>>> dataset = load_dataset("nielsr/funsd-layoutlmv3", split="train")
>>> example = dataset[0]
>>> words = example["tokens"]
>>> boxes = example["bboxes"]

>>> encoding = tokenizer(words, boxes=boxes, return_tensors="pt")

>>> outputs = model(**encoding)

>>> answer_start_index = outputs.start_logits.argmax()
>>> answer_end_index = outputs.end_logits.argmax()

>>> predict_answer_tokens = encoding.input_ids[0, answer_start_index : answer_end_index + 1]
>>> predicted_answer = tokenizer.decode(predict_answer_tokens)
```Nrf  r   r   r    rO   )ignore_indexru   )rk  start_logits
end_logitsr   r  )r8   r  r.  r  splitrn  r   lenrD   clampr   r   r   r  )r7   rG   rx   r   rH   r   rI   r  r  r   r  r  rP  r   rY  rl  r  r  
total_lossignored_indexrp  
start_lossend_lossr   s                           r:   rL    LiltForQuestionAnswering.forward{  s   ^ &1%<k$++BYBY))))%'/!5#  

 "!*1#)<<r<#: j#++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
M:H$x/14J"J/'!"+=F/9/EZMF*Q6Q+%!!//))
 	
r<   )r.  r`  r  )NNNNNNNNNNN)r\   r]   r^   r_   r#   r   r3   rs  r   r   r   r   r   rL   r`   ra   rb   s   @r:   r  r  n  s6     .2(,372604263715)-,0#'^
##d*^
 %^
 ))D0	^

 ((4/^
 &&-^
 ((4/^
 ))D0^
 ''$.^
  $;^
 #Tk^
 D[^
 
u||	;	;^
 ^
r<   r  )r  r]  ru  r;  r-  )3r  r   r3   r   torch.nnr   r   r    r   r1  activationsr	   modeling_layersr
   modeling_outputsr   r   r   r   r   modeling_utilsr   pytorch_utilsr   utilsr   r   configuration_liltr   
get_loggerr\   loggerModuler   rd   r   r   r   r   r   r   r  r"  r-  r;  r]  ru  ra  r  __all__r  r<   r:   <module>r     s       A A & ! 9  . 6 , * 
		H	%S= S=l5+299 5+pf		 fTRYY BII >ryy   7* 7t4
")) 4
p  	i/ 	i 	i A
# A
 A
H m
$7 m
m
` ^
!4 ^
 ^
DRYY , k
2 k
 k
\r<   