
    cCi                   4   S r SSKJr  SSKrSSKrSSKrSSKrSSK	J
r
  SSKJrJrJrJrJrJr  SSKJrJrJrJrJrJrJrJrJr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'  \$RP                  " \)5      r*Sr+ " S S\RX                  RZ                  5      r. " S S\RX                  RZ                  5      r/ " S S\RX                  RZ                  5      r0 " S S\RX                  RZ                  5      r1 " S S\RX                  RZ                  5      r2 " S S\RX                  RZ                  5      r3 " S S\RX                  RZ                  5      r4 " S S\RX                  RZ                  5      r5 " S S\RX                  RZ                  5      r6 " S S \RX                  RZ                  5      r7 " S! S"\RX                  RZ                  5      r8 " S# S$\RX                  RZ                  5      r9\ " S% S&\RX                  RZ                  5      5       r: " S' S(\5      r;S)r<S*r=\"" S+\<5       " S, S-\;5      5       r>\"" S.\<5       " S/ S0\;\5      5       r?\"" S1\<5       " S2 S3\;\5      5       r@\"" S4\<5       " S5 S6\;\5      5       rA\"" S7\<5       " S8 S9\;\5      5       rB/ S:QrCg);zTF 2.0 LayoutLM model.    )annotationsN   )get_tf_activation)+TFBaseModelOutputWithPastAndCrossAttentions.TFBaseModelOutputWithPoolingAndCrossAttentionsTFMaskedLMOutputTFQuestionAnsweringModelOutputTFSequenceClassifierOutputTFTokenClassifierOutput)
TFMaskedLanguageModelingLossTFModelInputTypeTFPreTrainedModelTFQuestionAnsweringLossTFSequenceClassificationLossTFTokenClassificationLossget_initializerkeraskeras_serializableunpack_inputs)check_embeddings_within_bounds
shape_liststable_softmax)add_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstrings   )LayoutLMConfigr   c                  n   ^  \ rS rSrSrSU 4S jjrSS jr      S	             S
S jjrSrU =r	$ )TFLayoutLMEmbeddings8   zGConstruct the embeddings from word, position and token_type embeddings.c                l  > [         TU ]  " S0 UD6  Xl        UR                  U l        UR                  U l        UR
                  U l        UR                  U l        [        R                  R                  UR                  SS9U l        [        R                  R                  UR                  S9U l        g )N	LayerNormepsilonnamerate )super__init__confighidden_sizemax_position_embeddingsmax_2d_position_embeddingsinitializer_ranger   layersLayerNormalizationlayer_norm_epsr#   Dropouthidden_dropout_probdropoutselfr,   kwargs	__class__s      k/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/layoutlm/modeling_tf_layoutlm.pyr+   TFLayoutLMEmbeddings.__init__;   s    "6"!--'-'E'E$*0*K*K'!'!9!988AVAV]h8i||++1K1K+L    c                   [         R                  " S5         U R                  SU R                  R                  U R
                  /[        U R                  5      S9U l        S S S 5        [         R                  " S5         U R                  SU R                  R                  U R
                  /[        U R                  5      S9U l
        S S S 5        [         R                  " S5         U R                  SU R                  U R
                  /[        U R                  5      S9U l        S S S 5        [         R                  " S5         U R                  SU R                  U R
                  /[        U R                  5      S9U l        S S S 5        [         R                  " S5         U R                  SU R                  U R
                  /[        U R                  5      S9U l        S S S 5        [         R                  " S	5         U R                  SU R                  U R
                  /[        U R                  5      S9U l        S S S 5        [         R                  " S
5         U R                  SU R                  U R
                  /[        U R                  5      S9U l        S S S 5        U R$                  (       a  g SU l        ['        U SS 5      bf  [         R                  " U R(                  R*                  5         U R(                  R-                  S S U R                  R
                  /5        S S S 5        g g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN9= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNU= f! , (       d  f       GN	= f! , (       d  f       g = f)Nword_embeddingsweight)r&   shapeinitializertoken_type_embeddings
embeddingsposition_embeddingsx_position_embeddingsy_position_embeddingsh_position_embeddingsw_position_embeddingsTr#   )tf
name_scope
add_weightr,   
vocab_sizer-   r   r0   r@   type_vocab_sizerC   r.   rE   r/   rF   rG   rH   rI   builtgetattrr#   r&   buildr8   input_shapes     r;   rQ   TFLayoutLMEmbeddings.buildF   s   ]],-//{{--t/?/?@+D,B,BC * DK . ]]23)-!{{22D4D4DE+D,B,BC *9 *D& 4 ]]01'+!33T5E5EF+D,B,BC (7 (D$ 2 ]]23)-!668H8HI+D,B,BC *9 *D& 4 ]]23)-!668H8HI+D,B,BC *9 *D& 4 ]]23)-!668H8HI+D,B,BC *9 *D& 4 ]]23)-!668H8HI+D,B,BC *9 *D& 4 ::
4d+7t~~223$$dD$++2I2I%JK 43 8i .- 43 21 43 43 43 43 43sf   A
L5?A
M'A MA M+#A M=A NA N!83N35
M
M
M(+
M:=
N
N!
N03
Oc                8   Uc  Uc   eUb>  [        XR                  R                  5        [        R                  " U R
                  US9n[        U5      SS nUc  [        R                  " USS9nUc+  [        R                  " [        R                  " SUS   S9SS9nUc+  [        R                  " [        R                  " SUS   S9SS9nUc  [        R                  " US/-   SS	9n [        R                  " U R                  USS2SS2S4   5      n[        R                  " U R                  USS2SS2S
4   5      n	[        R                  " U R                  USS2SS2S4   5      n
[        R                  " U R                  USS2SS2S4   5      n[        R                  " U R                  USS2SS2S4   USS2SS2S
4   -
  5      n[        R                  " U R                  USS2SS2S4   USS2SS2S4   -
  5      n[        R                  " U R                  US9n[        R                  " U R                   US9nUU-   U-   U-   U	-   U
-   U-   U-   U-   nU R#                  US9nU R%                  UUS9nU$ ! [         a  n[        S5      UeSnAff = f)zr
Applies embedding based on inputs tensor.

Returns:
    final_embeddings (`tf.Tensor`): output embedding tensor.
N)paramsindicesr   dimsvalue)startlimitaxis   )r[   r      r   z:The `bbox`coordinate values should be within 0-1000 range.inputsrc   training)r   r,   rM   rJ   gatherr@   r   fillexpand_dimsrangerF   rG   
IndexErrorrH   rI   rE   rC   r#   r6   )r8   	input_idsbboxposition_idstoken_type_idsinputs_embedsre   rS   left_position_embeddingsupper_position_embeddingsright_position_embeddingslower_position_embeddingserH   rI   position_embedstoken_type_embedsfinal_embeddingss                     r;   callTFLayoutLMEmbeddings.call   s    %-*?@@ *9kk6L6LMIIT[[)LM /4!WW+Q?N>>"((+b/*RYZ[L>>"((+b/*RYZ[L<77;!,A6D	b')yy1K1KTRSUVXYRY]'[$(*		$2L2LdSTVWYZSZm(\%(*		$2L2LdSTVWYZSZm(\%(*		$2L2LdSTVWYZSZm(\% !#		$*D*Dd1aQR7mVZ[\^_ab[bVcFc d "		$*D*Dd1aQR7mVZ[\^_ab[bVcFc d))4+C+C\ZIIT-G-GQ_`  '' (	(
 (( (( $$ $$ 	  >>1A>B<</?(<S+  	bYZ`aa	bs   )B,I> >
JJJ)r#   rO   r,   r6   rH   r-   r0   r/   r.   rE   rC   rI   r@   rF   rG   r,   r   N)NNNNNF)rk   tf.Tensor | Nonerl   r|   rm   r|   rn   r|   ro   r|   re   boolreturn	tf.Tensor)
__name__
__module____qualname____firstlineno____doc__r+   rQ   rx   __static_attributes____classcell__r:   s   @r;   r    r    8   su    Q	M7Lv '+!%)-+/*.< #<  <  '	< 
 )<  (<  <  
<  < r=   r    c                  r   ^  \ rS rSrSU 4S jjrSS jr S	                 S
S jjrSS jrSrU =r	$ )TFLayoutLMSelfAttention   c                  > [         TU ]  " S
0 UD6  UR                  UR                  -  S:w  a&  [	        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                  5      U l
        [        R                  R                  U R                  [        UR                  5      SS9U l        [        R                  R                  U R                  [        UR                  5      SS9U l        [        R                  R                  U R                  [        UR                  5      SS9U l        [        R                  R'                  UR(                  S	9U l        UR,                  U l        Xl        g )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()queryunitskernel_initializerr&   keyr[   r'   r)   )r*   r+   r-   num_attention_heads
ValueErrorintattention_head_sizeall_head_sizemathsqrtsqrt_att_head_sizer   r1   Denser   r0   r   r   r[   r4   attention_probs_dropout_probr6   
is_decoderr,   r7   s      r;   r+    TFLayoutLMSelfAttention.__init__   s   "6" : ::a?#F$6$6#7 8''-'A'A&B!E 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PP"&))D,D,D"E\\''$$IaIa9bip ( 

 <<%%$$IaIa9bin & 
 \\''$$IaIa9bip ( 

 ||++1T1T+U ++r=   c                    [         R                  " XSU R                  U R                  4S9n[         R                  " U/ SQS9$ )NrX   tensorrA   r   ra   r   r   perm)rJ   reshaper   r   	transpose)r8   r   
batch_sizes      r;   transpose_for_scores,TFLayoutLMSelfAttention.transpose_for_scores   s9    6b$BZBZ\`\t\t1uv ||F66r=   c	                   [        U5      S   n	U R                  US9n
US LnU(       a  Ub  US   nUS   nUnOU(       aA  U R                  U R                  US9U	5      nU R                  U R	                  US9U	5      nUnOUbs  U R                  U R                  US9U	5      nU R                  U R	                  US9U	5      n[
        R                  " US   U/SS9n[
        R                  " US   U/SS9nO>U R                  U R                  US9U	5      nU R                  U R	                  US9U	5      nU R                  X5      nU R                  (       a  X4n[
        R                  " XSS9n[
        R                  " U R                  UR                  S9n[
        R                  " UU5      nUb  [
        R                  " X5      n[        US	S
9nU R                  UUS9nUb  [
        R                   " UU5      n[
        R                  " UU5      n[
        R"                  " U/ SQS9n[
        R$                  " UU	S	U R&                  4S9nU(       a  UU4OU4nU R                  (       a  UU4-   nU$ )Nr   rb   r   ra   r^   T)transpose_bdtyperX   )logitsr_   rd   r   r   r   )r   r   r   r   r[   rJ   concatr   matmulcastr   r   divideaddr   r6   multiplyr   r   r   )r8   hidden_statesattention_mask	head_maskencoder_hidden_statesencoder_attention_maskpast_key_valueoutput_attentionsre   r   mixed_query_layeris_cross_attention	key_layervalue_layerquery_layerattention_scoresdkattention_probsattention_outputoutputss                       r;   rx   TFLayoutLMSelfAttention.call   sq     .q1
 JJmJ<
 3$>."<&q)I(+K3N11$((BW(2XZdeI33DJJF[J4\^hiK3N'11$((-(2PR\]I33DJJmJ4TV`aK		>!#4i"@qII))^A%6$D1MK11$((-(2PR\]I33DJJmJ4TV`aK//0AN?? (5N 99[NWWT,,4D4J4JK99%5r:%!vv&6G )0@rJ ,,o,Q   kk/9EO99_kB<<(8|L ::-=jRTVZVhVhEij9J#_5QaPc?? 11Gr=   c                P   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)NTr   r   r[   )rO   rP   rJ   rK   r   r&   rQ   r,   r-   r   r[   rR   s     r;   rQ   TFLayoutLMSelfAttention.build4  s   ::
4$'3tzz/

  $dkk.E.E!FG 04%1txx}}-dDKK,C,CDE .4$'3tzz/

  $dkk.E.E!FG 0/ 4 0/ .- 0/s$   3E53F83F5
F
F
F%)r   r   rO   r,   r6   r   r   r   r   r   r[   rz   )r   r   r   r   r~   r   F)r   r   r   r   r   r   r   r   r   r   r   tuple[tf.Tensor]r   r}   re   r}   r~   r   r{   )
r   r   r   r   r+   r   rx   rQ   r   r   r   s   @r;   r   r      s    87  O O "O 	O
  )O !*O )O  O O 
ObH Hr=   r   c                  D   ^  \ rS rSrSU 4S jjrSSS jjrS	S jrSrU =r$ )
TFLayoutLMSelfOutputiD  c                f  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SS9U l        [        R                  R                  UR                  SS9U l        [        R                  R                  UR                  S9U l        Xl        g Ndenser   r#   r$   r'   r)   r*   r+   r   r1   r   r-   r   r0   r   r2   r3   r#   r4   r5   r6   r,   r7   s      r;   r+   TFLayoutLMSelfOutput.__init__E      "6"\\''$$IaIa9bip ( 

 88AVAV]h8i||++1K1K+Lr=   c                d    U R                  US9nU R                  XS9nU R                  X-   S9nU$ Nrb   rd   r   r6   r#   r8   r   input_tensorre   s       r;   rx   TFLayoutLMSelfOutput.callO  ;    

-
8MMm.JKr=   c                H   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = fNTr   r#   
rO   rP   rJ   rK   r   r&   rQ   r,   r-   r#   rR   s     r;   rQ   TFLayoutLMSelfOutput.buildV      ::
4$'3tzz/

  $dkk.E.E!FG 04d+7t~~223$$dD$++2I2I%JK 43 8 0/ 43   3D3D
D
D!r#   rO   r,   r   r6   rz   r   r   r   r   r   re   r}   r~   r   r{   	r   r   r   r   r+   rx   rQ   r   r   r   s   @r;   r   r   D      	L 	Lr=   r   c                  n   ^  \ rS rSrSU 4S jjrS r S                 S	S jjrS
S jrSrU =r	$ )TFLayoutLMAttentionic  c                b   > [         TU ]  " S0 UD6  [        USS9U l        [	        USS9U l        g )Nr8   r&   outputr)   )r*   r+   r   self_attentionr   dense_outputr7   s      r;   r+   TFLayoutLMAttention.__init__d  s1    "6"5f6J0hGr=   c                    [         er{   NotImplementedError)r8   headss     r;   prune_headsTFLayoutLMAttention.prune_headsj  s    !!r=   c	                j    U R                  UUUUUUUUS9n	U R                  U	S   XS9n
U
4U	SS  -   nU$ )Nr   r   r   r   r   r   r   re   r   r   r   re   r   )r   r   )r8   r   r   r   r   r   r   r   re   self_outputsr   r   s               r;   rx   TFLayoutLMAttention.callm  sm     **&)"7#9)/ + 	
  ,,&q/ - 
 $%QR(88r=   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       Nl= f! , (       d  f       g = f)NTr   r   )rO   rP   rJ   rK   r   r&   rQ   r   rR   s     r;   rQ   TFLayoutLMAttention.build  s    ::
4)40<t22778##))$/ 94.:t00556!!''- 76 ; 98 76   C.C%
C"%
C3)rO   r   r   rz   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r}   re   r}   r~   r   r{   )
r   r   r   r   r+   r   rx   rQ   r   r   r   s   @r;   r   r   c  sz    H"  " 	
  ) !* )    
:	. 	.r=   r   c                  @   ^  \ rS rSrSU 4S jjrSS jrSS jrSrU =r$ )	TFLayoutLMIntermediatei  c                J  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SS9U l        [        UR                  [        5      (       a  [        UR                  5      U l        OUR                  U l        Xl        g )Nr   r   r)   )r*   r+   r   r1   r   intermediate_sizer   r0   r   
isinstance
hidden_actstrr   intermediate_act_fnr,   r7   s      r;   r+   TFLayoutLMIntermediate.__init__  s    "6"\\''**vOgOg?hov ( 

 f''--'89J9J'KD$'-'8'8D$r=   c                F    U R                  US9nU R                  U5      nU$ Nrb   )r   r   r8   r   s     r;   rx   TFLayoutLMIntermediate.call  s(    

-
800?r=   c                @   U R                   (       a  g SU l         [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       g = fNTr   	rO   rP   rJ   rK   r   r&   rQ   r,   r-   rR   s     r;   rQ   TFLayoutLMIntermediate.build  m    ::
4$'3tzz/

  $dkk.E.E!FG 0/ 4//   3B
B)rO   r,   r   r   rz   r   r   r~   r   r{   r   r   s   @r;   r   r     s    H Hr=   r   c                  D   ^  \ rS rSrSU 4S jjrSSS jjrS	S jrSrU =r$ )
TFLayoutLMOutputi  c                f  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SS9U l        [        R                  R                  UR                  SS9U l        [        R                  R                  UR                  S9U l        Xl        g r   r   r7   s      r;   r+   TFLayoutLMOutput.__init__  r   r=   c                d    U R                  US9nU R                  XS9nU R                  X-   S9nU$ r   r   r   s       r;   rx   TFLayoutLMOutput.call  r   r=   c                H   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = fr   )rO   rP   rJ   rK   r   r&   rQ   r,   r   r#   r-   rR   s     r;   rQ   TFLayoutLMOutput.build  s    ::
4$'3tzz/

  $dkk.K.K!LM 04d+7t~~223$$dD$++2I2I%JK 43 8 0/ 43r   r   rz   r   r   r{   r   r   s   @r;   r  r    r   r=   r  c                  h   ^  \ rS rSrSU 4S jjr S                 SS jjrS	S jrSrU =r$ )
TFLayoutLMLayeri  c                B  > [         TU ]  " S0 UD6  [        USS9U l        UR                  U l        UR
                  U l        U R
                  (       a.  U R                  (       d  [        U  S35      e[        USS9U l        [        USS9U l	        [        USS9U l        g )N	attentionr   z> should be used as a decoder model if cross attention is addedcrossattentionintermediater   r)   )r*   r+   r   r  r   add_cross_attentionr   r  r   r  r  bert_outputr7   s      r;   r+   TFLayoutLMLayer.__init__  s    "6",V+F ++#)#=#= ##?? D6)g!hii"5fCS"TD26O+FBr=   c	                   Ub  US S OS n	U R                  UUUS S U	UUS9n
U
S   nU R                  (       a  U
SS nU
S   nOU
SS  nS nU R                  (       aZ  UbW  [        U S5      (       d  [        SU  S35      eUb  US	S  OS nU R	                  UUUUUUUUS9nUS   nUUSS -   nUS   nWU-   nU R                  US
9nU R                  UXS9nU4U-   nU R                  (       a  UW4-   nU$ )Nra   )r   r   r   r   r   r   r   re   r   r   rX   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   hasattrr   r  r  r  )r8   r   r   r   r   r   r   r   re   self_attn_past_key_valueself_attention_outputsr   r   present_key_valuecross_attn_present_key_valuecross_attn_past_key_valuecross_attention_outputsintermediate_outputlayer_outputs                      r;   rx   TFLayoutLMLayer.call  s    :H9S>"1#5Y] !%&)"&#'3/ "0 	"
 2!4 ??,Qr2G 6r :,QR0G'+$??4@4!122 =dV DD D  @N?Yrs(;_c%&*&9&9--#&;'=8"3! ': 	'#  7q9 7" ==G ,C2+F( 14P P"//>N/O''-<L ( 
  /G+ ??!2 44Gr=   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       GN%= f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)NTr  r  r  r  )
rO   rP   rJ   rK   r  r&   rQ   r  r  r  rR   s     r;   rQ   TFLayoutLMLayer.build)  s<   ::
4d+7t~~223$$T* 44.:t00556!!''- 74-9t//445  &&t, 64)40<t22778##))$/ 98 = 43 76 65 98s0   F.F
F/&G 
F
F,/
F= 
G)r  r  r  rO   r  r  r   rz   r   )r   r   r   r   r   r   r   r|   r   r|   r   ztuple[tf.Tensor] | Noner   r}   re   r}   r~   r   r{   r   r   s   @r;   r  r    s    C, E E "E 	E
  0E !1E 0E  E E 
EN0 0r=   r  c                  t   ^  \ rS rSrSU 4S jjr S                       SS jjrS	S jrSrU =r$ )
TFLayoutLMEncoderi<  c                   > [         TU ]  " S0 UD6  Xl        [        UR                  5       Vs/ s H  n[        USU 3S9PM     snU l        g s  snf )Nzlayer_._r   r)   )r*   r+   r,   ri   num_hidden_layersr  layer)r8   r,   r9   ir:   s       r;   r+   TFLayoutLMEncoder.__init__=  sL    "6"LQRXRjRjLklLkqofXaS>BLkl
ls   Ac                &   U	(       a  SOS nU(       a  SOS nU(       a  U R                   R                  (       a  SOS nU(       a  SOS n[        U R                  5       H~  u  nnU	(       a  X4-   nUb  UU   OS nU" UUUU   UUUUUS9nUS   nU(       a	  UUS   4-  nU(       d  MJ  UUS   4-   nU R                   R                  (       d  Mp  Uc  Mu  UUS   4-   nM     U	(       a  X4-   nU
(       d  [	        S XX4 5       5      $ [        UUUUUS9$ )	Nr)   r   r   rX   r   ra   c              3  .   #    U  H  oc  M  Uv   M     g 7fr{   r)   ).0vs     r;   	<genexpr>)TFLayoutLMEncoder.call.<locals>.<genexpr>t  s      cacs   	)last_hidden_statepast_key_valuesr   
attentionscross_attentions)r,   r  	enumerater1  tupler   )r8   r   r   r   r   r   r;  	use_cacher   output_hidden_statesreturn_dictre   all_hidden_statesall_attentionsall_cross_attentionsnext_decoder_cacher2  layer_moduler   layer_outputss                       r;   rx   TFLayoutLMEncoder.callB  sK    #7BD0d%64;;;Z;Zr`d#,R$(4OA|#$58H$H!3B3N_Q/TXN(+-#A,&;'=-"3!	M *!,M"}R'8&::"  !/=3C2E!E;;2227L7X+?=QRCSBU+U(1  56   14D D )nc   ;+.+%1
 	
r=   c                   U R                   (       a  g SU l         [        U SS 5      bN  U R                   H=  n[        R                  " UR
                  5         UR                  S 5        S S S 5        M?     g g ! , (       d  f       MR  = f)NTr1  )rO   rP   r1  rJ   rK   r&   rQ   )r8   rS   r1  s      r;   rQ   TFLayoutLMEncoder.build  s`    ::
4$'3]]5::.KK% /. $ 4..s   A77
B	)rO   r,   r1  rz   r   )r   r   r   r   r   r   r   r|   r   r|   r;  ztuple[tuple[tf.Tensor]] | Noner@  bool | Noner   r}   rA  r}   rB  r}   re   r}   r~   z>TFBaseModelOutputWithPastAndCrossAttentions | tuple[tf.Tensor]r{   r   r   s   @r;   r.  r.  <  s    m" <
 <
 "<
 	<

  0<
 !1<
 8<
 <
  <
 #<
 <
 <
 
H<
|& &r=   r.  c                  @   ^  \ rS rSrSU 4S jjrSS jrSS jrSrU =r$ )	TFLayoutLMPooleri  c                   > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SSS9U l        Xl	        g )Ntanhr   )r   r   
activationr&   r)   )
r*   r+   r   r1   r   r-   r   r0   r   r,   r7   s      r;   r+   TFLayoutLMPooler.__init__  sR    "6"\\''$$.v/G/GH	 ( 

 r=   c                6    US S 2S4   nU R                  US9nU$ )Nr   rb   )r   )r8   r   first_token_tensorpooled_outputs       r;   rx   TFLayoutLMPooler.call  s*     +1a40

*<
=r=   c                @   U R                   (       a  g SU l         [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       g = fr  r  rR   s     r;   rQ   TFLayoutLMPooler.build  r
  r  )rO   r,   r   rz   r  r{   r   r   s   @r;   rN  rN    s    	H Hr=   rN  c                  @   ^  \ rS rSrSU 4S jjrSS jrSS jrSrU =r$ )	!TFLayoutLMPredictionHeadTransformi  c                  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SS9U l        [        UR                  [        5      (       a  [        UR                  5      U l        OUR                  U l        [        R                  R                  UR                  SS9U l        Xl        g )Nr   r   r#   r$   r)   )r*   r+   r   r1   r   r-   r   r0   r   r   r   r   r   transform_act_fnr2   r3   r#   r,   r7   s      r;   r+   *TFLayoutLMPredictionHeadTransform.__init__  s    "6"\\''$$.v/G/GH ( 

 f''--$5f6G6G$HD!$*$5$5D!88AVAV]h8ir=   c                d    U R                  US9nU R                  U5      nU R                  US9nU$ r  )r   r\  r#   r  s     r;   rx   &TFLayoutLMPredictionHeadTransform.call  s8    

-
8--m<m<r=   c                H   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = fr   r   rR   s     r;   rQ   'TFLayoutLMPredictionHeadTransform.build  r   r   )r#   rO   r,   r   r\  rz   r  r{   r   r   s   @r;   rZ  rZ    s    "	L 	Lr=   rZ  c                  h   ^  \ rS rSrS
U 4S jjrSS jrSS jrSS jrSS jrSS jr	SS jr
S	rU =r$ )TFLayoutLMLMPredictionHeadi  c                ~   > [         TU ]  " S0 UD6  Xl        UR                  U l        [	        USS9U l        X l        g )N	transformr   r)   )r*   r+   r,   r-   rZ  re  input_embeddingsr8   r,   rf  r9   r:   s       r;   r+   #TFLayoutLMLMPredictionHead.__init__  s<    "6"!--:6T !1r=   c                j   U R                  U R                  R                  4SSSS9U l        U R                  (       a  g SU l        [        U SS 5      bO  [        R                  " U R                  R                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       g = f)NzerosTbias)rA   rB   	trainabler&   re  )rL   r,   rM   rk  rO   rP   rJ   rK   re  r&   rQ   rR   s     r;   rQ    TFLayoutLMLMPredictionHead.build  s    OO4;;+A+A*CQXdhouOv	::
4d+7t~~223$$T* 43 833s   >B$$
B2c                    U R                   $ r{   )rf  r8   s    r;   get_output_embeddings0TFLayoutLMLMPredictionHead.get_output_embeddings  s    $$$r=   c                ^    XR                   l        [        U5      S   U R                   l        g Nr   )rf  r@   r   rM   r8   r[   s     r;   set_output_embeddings0TFLayoutLMLMPredictionHead.set_output_embeddings  s&    ',$+5e+<Q+?(r=   c                    SU R                   0$ )Nrk  )rk  ro  s    r;   get_bias#TFLayoutLMLMPredictionHead.get_bias  s    		""r=   c                X    US   U l         [        US   5      S   U R                  l        g )Nrk  r   )rk  r   r,   rM   rt  s     r;   set_bias#TFLayoutLMLMPredictionHead.set_bias  s'    &M	!+E&M!:1!=r=   c                x   U R                  US9n[        U5      S   n[        R                  " USU R                  /S9n[        R
                  " XR                  R                  SS9n[        R                  " USX R                  R                  /S9n[        R                  R                  XR                  S9nU$ )Nr   r   rX   r   T)abr   )r[   rk  )re  r   rJ   r   r-   r   rf  r@   r,   rM   nnbias_addrk  )r8   r   
seq_lengths      r;   rx   TFLayoutLMLMPredictionHead.call  s    ]C.q1


-DDTDT?UV		M5J5J5Q5Q_cd

-JP[P[PfPf?gh]Kr=   )rk  rO   r,   r-   rf  re  r,   r   rf  keras.layers.Layerr{   r~   r  r[   ztf.Variable)r~   zdict[str, tf.Variable]r  )r   r   r   r   r+   rQ   rp  ru  rx  r{  rx   r   r   r   s   @r;   rc  rc    s,    
1+%@#> r=   rc  c                  @   ^  \ rS rSrSU 4S jjrSS jrSS jrSrU =r$ )	TFLayoutLMMLMHeadi   c                D   > [         TU ]  " S0 UD6  [        XSS9U l        g )Npredictionsr   r)   )r*   r+   rc  r  rg  s       r;   r+   TFLayoutLMMLMHead.__init__  s#    "6"5fUbcr=   c                $    U R                  US9nU$ )Nr   )r  )r8   sequence_outputprediction_scoress      r;   rx   TFLayoutLMMLMHead.call  s     ,,?,K  r=   c                   U R                   (       a  g SU l         [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       g = f)NTr  )rO   rP   rJ   rK   r  r&   rQ   rR   s     r;   rQ   TFLayoutLMMLMHead.build  sb    ::
4-9t//445  &&t, 65 :55   A88
B)rO   r  r  )r  r   r~   r   r{   r   r   s   @r;   r  r     s    d
!
- -r=   r  c                     ^  \ rS rSr\rS	S
U 4S jjjrSS jrSS jrS r	\
             S                           SS jj5       rSS jrSrU =r$ )TFLayoutLMMainLayeri  c                   > [         TU ]  " S0 UD6  Xl        [        USS9U l        [        USS9U l        U(       a  [        USS9U l        g S U l        g )NrD   r   encoderpoolerr)   )	r*   r+   r,   r    rD   r.  r  rN  r  )r8   r,   add_pooling_layerr9   r:   s       r;   r+   TFLayoutLMMainLayer.__init__  sL    "6".vLI(i@AR&vH=X\r=   c                    U R                   $ r{   )rD   ro  s    r;   get_input_embeddings(TFLayoutLMMainLayer.get_input_embeddings!  s    r=   c                ^    XR                   l        [        U5      S   U R                   l        g rs  )rD   r@   r   rM   rt  s     r;   set_input_embeddings(TFLayoutLMMainLayer.set_input_embeddings$  s"    !&%/%6q%9"r=   c                    [         e)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
r   )r8   heads_to_prunes     r;   _prune_heads TFLayoutLMMainLayer._prune_heads(  s
    
 "!r=   c                   Ub  Ub  [        S5      eUb  [        U5      nOUb  [        U5      S S nO[        S5      eUc  [        R                  " USS9nUc  [        R                  " USS9nUc  [        R                  " US/-   SS9nU R	                  UUUUUUS9n[        R
                  " X>S   SSUS   45      n[        R                  " UUR                  S	9n[        R                  " S
UR                  S	9n[        R                  " SUR                  S	9n[        R                  " [        R                  " UU5      U5      nUb  [        eS /U R                  R                  -  nU R                  UUUUS S SU
UUUS9nUS   nU R                  b  U R                  US9OS nU(       d
  UU4USS  -   $ [!        UUUR"                  UR$                  UR&                  S9$ )NzDYou cannot specify both input_ids and inputs_embeds at the same timerX   z5You have to specify either input_ids or inputs_embedsr   rY   r   r`   )rk   rl   rm   rn   ro   re   r   g      ?g     F)r   r   r   r   r   r;  r@  r   rA  rB  re   r   )r:  pooler_outputr   r<  r=  )r   r   rJ   rg   rD   r   r   r   constantr   subtractr   r,   r0  r  r  r   r   r<  r=  )r8   rk   rl   r   rn   rm   r   ro   r   r   r   rA  rB  re   rS   embedding_outputextended_attention_maskone_cstten_thousand_cstencoder_outputsr  rU  s                         r;   rx   TFLayoutLMMainLayer.call/  s   "  ]%>cdd"$Y/K&$]3CR8KTUU!WW+Q?N!WW+Q?N<77qc 1;D??%)' + 
 #%**^!naQRT_`aTb=c"d #%''*AIYI_I_"`++c)9)?)?@;;x7G7M7MN"$++bkk'CZ.[]m"n  %%!>!>>I,,*2"7#' /!5# ' 
 *!,FJkkF]/Bcg  #$ $
 >-')77&11,==
 	
r=   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       N}= f! , (       d  f       g = f)NTrD   r  r  )	rO   rP   rJ   rK   rD   r&   rQ   r  r  rR   s     r;   rQ   TFLayoutLMMainLayer.build  s    ::
4t,8t334%%d+ 54D)5t||001""4( 244(4t{{//0!!$' 10 5 54 21 10s$   D0.E
E0
D>
E
E )rO   r,   rD   r  r  )T)r,   r   r  r}   r  r  NNNNNNNNNNNNF)rk   TFModelInputType | Nonerl   np.ndarray | tf.Tensor | Noner   r  rn   r  rm   r  r   r  ro   r  r   r  r   r  r   rL  rA  rL  rB  rL  re   r}   r~   ATFBaseModelOutputWithPoolingAndCrossAttentions | tuple[tf.Tensor]r{   )r   r   r   r   r   config_classr+   r  r  r  r   rx   rQ   r   r   r   s   @r;   r  r    s   !L] ]:"  .2.28<8<6:377;?C@D)-,0#'c
*c
 ,c
 6	c

 6c
 4c
 1c
 5c
  =c
 !>c
 'c
 *c
 !c
 c
 
Kc
 c
J( (r=   r  c                  >   ^  \ rS rSrSr\rSr\U 4S j5       r	Sr
U =r$ )TFLayoutLMPreTrainedModeli  zz
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
models.
layoutlmc                j   > [         TU ]  n[        R                  " S[        R                  SS9US'   U$ )N)NNr`   rl   )rA   r   r&   )r*   input_signaturerJ   
TensorSpecint32)r8   	signaturer:   s     r;   r  )TFLayoutLMPreTrainedModel.input_signature  s/    G+	MMrxxV\]	&r=   r)   )r   r   r   r   r   r   r  base_model_prefixpropertyr  r   r   r   s   @r;   r  r    s'    
 "L" r=   r  az	  

    This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a [keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model) subclass. Use it
    as a regular TF 2.0 Keras Model and refer to the TF 2.0 documentation for all matter related to general usage and
    behavior.

    <Tip>

    TensorFlow models and layers in `transformers` accept two formats as input:

    - having all inputs as keyword arguments (like PyTorch models), or
    - having all inputs as a list, tuple or dict in the first positional argument.

    The reason the second format is supported is that Keras methods prefer this format when passing inputs to models
    and layers. Because of this support, when using methods like `model.fit()` things should "just work" for you - just
    pass your inputs and labels in any format that `model.fit()` supports! If, however, you want to use the second
    format outside of Keras methods like `fit()` and `predict()`, such as when creating your own layers or models with
    the Keras `Functional` API, there are three possibilities you can use to gather all the input Tensors in the first
    positional argument:

    - a single Tensor with `input_ids` only and nothing else: `model(input_ids)`
    - a list of varying length with one or several input Tensors IN THE ORDER given in the docstring:
    `model([input_ids, attention_mask])` or `model([input_ids, attention_mask, token_type_ids])`
    - a dictionary with one or several input Tensors associated to the input names given in the docstring:
    `model({"input_ids": input_ids, "token_type_ids": token_type_ids})`

    Note that when creating models and layers with
    [subclassing](https://keras.io/guides/making_new_layers_and_models_via_subclassing/) then you don't need to worry
    about any of this, as you can just pass inputs like you would to any other Python function!

    </Tip>

    Args:
        config ([`LayoutLMConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        input_ids (`Numpy array` or `tf.Tensor` of shape `({0})`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and
            [`PreTrainedTokenizer.encode`] for details.

            [What are input IDs?](../glossary#input-ids)
        bbox (`Numpy array` or `tf.Tensor` of shape `({0}, 4)`, *optional*):
            Bounding Boxes of each input sequence tokens. Selected in the range `[0, config.max_2d_position_embeddings-
            1]`.
        attention_mask (`Numpy array` or `tf.Tensor` of shape `({0})`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

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

            [What are attention masks?](../glossary#attention-mask)
        token_type_ids (`Numpy array` or `tf.Tensor` of shape `({0})`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        position_ids (`Numpy array` or `tf.Tensor` of shape `({0})`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        head_mask (`Numpy array` or `tf.Tensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        inputs_embeds (`tf.Tensor` of shape `({0}, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        training (`bool`, *optional*, defaults to `False`):
            Whether or not to use the model in training mode (some modules like dropout modules have different
            behaviors between training and evaluation).
zbThe bare LayoutLM Model transformer outputting raw hidden-states without any specific head on top.c                     ^  \ rS rSrSU 4S jjr\\" \R                  S5      5      \	" \
\S9             S	                           S
S jj5       5       5       rSS jrSrU =r$ )TFLayoutLMModeli  c                L   > [         TU ]  " U/UQ70 UD6  [        USS9U l        g )Nr  r   )r*   r+   r  r  r8   r,   rc   r9   r:   s       r;   r+   TFLayoutLMModel.__init__  s(    3&3F3+FDr=   batch_size, sequence_lengthoutput_typer  c                8    U R                  UUUUUUUU
UUUS9nU$ )aX  
Returns:

Examples:

```python
>>> from transformers import AutoTokenizer, TFLayoutLMModel
>>> import tensorflow as tf

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
>>> model = TFLayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased")

>>> words = ["Hello", "world"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
...     word_tokens = tokenizer.tokenize(word)
...     token_boxes.extend([box] * len(word_tokens))
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> encoding = tokenizer(" ".join(words), return_tensors="tf")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = tf.convert_to_tensor([token_boxes])

>>> outputs = model(
...     input_ids=input_ids, bbox=bbox, attention_mask=attention_mask, token_type_ids=token_type_ids
... )

>>> last_hidden_states = outputs.last_hidden_state
```rk   rl   r   rn   rm   r   ro   r   rA  rB  re   )r  )r8   rk   rl   r   rn   rm   r   ro   r   r   r   rA  rB  re   r   s                  r;   rx   TFLayoutLMModel.call   s@    n --))%'/!5#   
 r=   c                   U R                   (       a  g SU l         [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       g = f)NTr  )rO   rP   rJ   rK   r  r&   rQ   rR   s     r;   rQ   TFLayoutLMModel.buildg  s^    ::
4T*6t}}112##D) 32 722r  )rO   r  rz   r  )rk   r  rl   r  r   r  rn   r  rm   r  r   r  ro   r  r   r  r   r  r   rL  rA  rL  rB  rL  re   rL  r~   r  r{   )r   r   r   r   r+   r   r   LAYOUTLM_INPUTS_DOCSTRINGformatr   r   _CONFIG_FOR_DOCrx   rQ   r   r   r   s   @r;   r  r    s   
E
 *+D+K+KLi+jkBQ`
 .2.28<8<6:377;?C@D)-,0#' %@*@ ,@ 6	@
 6@ 4@ 1@ 5@  =@ !>@ '@ *@ !@ @ 
K@ l 
@D* *r=   r  z6LayoutLM Model with a `language modeling` head on top.c                     ^  \ rS rSr/ SQrSU 4S jjrSS jrSS jr\\	" \
R                  S5      5      \" \\S9            S                         SS jj5       5       5       rSS	 jrS
rU =r$ )TFLayoutLMForMaskedLMip  )r  cls.seq_relationshipzcls.predictions.decoder.weight	nsp___clsc                   > [         TU ]  " U/UQ70 UD6  UR                  (       a  [        R	                  S5        [        USSS9U l        [        XR                  R                  SS9U l	        g )NzqIf you want to use `TFLayoutLMForMaskedLM` make sure `config.is_decoder=False` for bi-directional self-attention.Tr  r  r&   	mlm___cls)rf  r&   )
r*   r+   r   loggerwarningr  r  r  rD   mlmr  s       r;   r+   TFLayoutLMForMaskedLM.__init__z  s`    3&3F3NN1
 ,FdQ[\$Vmm>V>V]hir=   c                .    U R                   R                  $ r{   )r  r  ro  s    r;   get_lm_head!TFLayoutLMForMaskedLM.get_lm_head  s    xx###r=   c                    [         R                  " S[        5        U R                  S-   U R                  R                  -   S-   U R                  R
                  R                  -   $ )NzMThe method get_prefix_bias_name is deprecated. Please use `get_bias` instead./)warningswarnFutureWarningr&   r  r  ro  s    r;   get_prefix_bias_name*TFLayoutLMForMaskedLM.get_prefix_bias_name  sG    egtuyy3.4txx7K7K7P7PPPr=   r  r  c                    U R                  UUUUUUUUU	U
US9nUS   nU R                  XS9nUc  SOU R                  XS9nU
(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR
                  S9$ )aF  
labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ...,
    config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the
    loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`

Returns:

Examples:

```python
>>> from transformers import AutoTokenizer, TFLayoutLMForMaskedLM
>>> import tensorflow as tf

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
>>> model = TFLayoutLMForMaskedLM.from_pretrained("microsoft/layoutlm-base-uncased")

>>> words = ["Hello", "[MASK]"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
...     word_tokens = tokenizer.tokenize(word)
...     token_boxes.extend([box] * len(word_tokens))
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> encoding = tokenizer(" ".join(words), return_tensors="tf")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = tf.convert_to_tensor([token_boxes])

>>> labels = tokenizer("Hello world", return_tensors="tf")["input_ids"]

>>> outputs = model(
...     input_ids=input_ids,
...     bbox=bbox,
...     attention_mask=attention_mask,
...     token_type_ids=token_type_ids,
...     labels=labels,
... )

>>> loss = outputs.loss
```r  r   )r  re   Nlabelsr   ra   lossr   r   r<  )r  r  hf_compute_lossr   r   r<  )r8   rk   rl   r   rn   rm   r   ro   r   rA  rB  r  re   r   r  r  r  r   s                     r;   rx   TFLayoutLMForMaskedLM.call  s    ~ --))%'/!5#   
 "!* HH_HX~t4+?+?v+?+h')GABK7F)-)9TGf$EvE$!//))	
 	
r=   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       Nl= f! , (       d  f       g = f)NTr  r  )rO   rP   rJ   rK   r  r&   rQ   r  rR   s     r;   rQ   TFLayoutLMForMaskedLM.build  s    ::
4T*6t}}112##D) 34%1txx}}-t$ .- 2 32 .-r   )rO   r  r  rz   r  )r~   r   NNNNNNNNNNNF)rk   r  rl   r  r   r  rn   r  rm   r  r   r  ro   r  r   rL  rA  rL  rB  rL  r  r  re   rL  r~   z#TFMaskedLMOutput | tuple[tf.Tensor]r{   )r   r   r   r   "_keys_to_ignore_on_load_unexpectedr+   r  r  r   r   r  r  r   r   r  rx   rQ   r   r   r   s   @r;   r  r  p  s   *&
j$Q *+D+K+KLi+jk+;/Z .2.28<8<6:377;)-,0#'04 %V
*V
 ,V
 6	V

 6V
 4V
 1V
 5V
 'V
 *V
 !V
 .V
 V
 
-V
 [ l V
p	% 	%r=   r  z
    LayoutLM Model transformer with a sequence classification/regression head on top (a linear layer on top of the
    pooled output) e.g. for GLUE tasks.
    c                     ^  \ rS rSr/ SQrS/rS
U 4S jjr\\" \	R                  S5      5      \" \\S9            S                         SS jj5       5       5       rSS jrS	rU =r$ )#TFLayoutLMForSequenceClassificationi  )r  r  cls.predictionsr  r6   c                T  > [         TU ]  " U/UQ70 UD6  UR                  U l        [        USS9U l        [
        R                  R                  UR                  S9U l	        [
        R                  R                  UR                  [        UR                  5      SS9U l        Xl        g )Nr  r   r'   
classifierr   r*   r+   
num_labelsr  r  r   r1   r4   r5   r6   r   r   r0   r  r,   r  s       r;   r+   ,TFLayoutLMForSequenceClassification.__init__   s    3&3F3 +++FD||++1K1K+L,,,,##.v/G/GH - 

 r=   r  r  c                   U R                  UUUUUUUUU	U
US9nUS   nU R                  XS9nU R                  US9nUc  SOU R                  XS9nU
(       d  U4USS -   nUb  U4U-   $ U$ [	        UUUR
                  UR                  S9$ )	aV  
labels (`tf.Tensor` or `np.ndarray` 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).

Returns:

Examples:

```python
>>> from transformers import AutoTokenizer, TFLayoutLMForSequenceClassification
>>> import tensorflow as tf

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
>>> model = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased")

>>> words = ["Hello", "world"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
...     word_tokens = tokenizer.tokenize(word)
...     token_boxes.extend([box] * len(word_tokens))
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> encoding = tokenizer(" ".join(words), return_tensors="tf")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = tf.convert_to_tensor([token_boxes])
>>> sequence_label = tf.convert_to_tensor([1])

>>> outputs = model(
...     input_ids=input_ids,
...     bbox=bbox,
...     attention_mask=attention_mask,
...     token_type_ids=token_type_ids,
...     labels=sequence_label,
... )

>>> loss = outputs.loss
>>> logits = outputs.logits
```r  r   rd   rb   Nr  ra   r  )r  r6   r  r  r
   r   r<  )r8   rk   rl   r   rn   rm   r   ro   r   rA  rB  r  re   r   rU  r   r  r   s                     r;   rx   (TFLayoutLMForSequenceClassification.call  s    ~ --))%'/!5#   
  
MM6~t4+?+?v+?+]Y,F)-)9TGf$EvE)!//))	
 	
r=   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = fNTr  r  
rO   rP   rJ   rK   r  r&   rQ   r  r,   r-   rR   s     r;   rQ   )TFLayoutLMForSequenceClassification.buildj      ::
4T*6t}}112##D) 34t,8t334%%tT4;;3J3J&KL 54 9 32 54   C+.3C<+
C9<
D
rO   r  r,   r6   r  r  rz   r  )rk   r  rl   r  r   r  rn   r  rm   r  r   r  ro   r  r   rL  rA  rL  rB  rL  r  r  re   rL  r~   z-TFSequenceClassifierOutput | tuple[tf.Tensor]r{   )r   r   r   r   r  _keys_to_ignore_on_load_missingr+   r   r   r  r  r   r
   r  rx   rQ   r   r   r   s   @r;   r  r    s    *s&'1l# *+D+K+KLi+jk+ETcd .2.28<8<6:377;)-,0#'04 %W
*W
 ,W
 6	W

 6W
 4W
 1W
 5W
 'W
 *W
 !W
 .W
 W
 
7W
 e l W
r	M 	Mr=   r  z
    LayoutLM Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for
    Named-Entity-Recognition (NER) tasks.
    c                     ^  \ rS rSr/ SQrS/rS
U 4S jjr\\" \	R                  S5      5      \" \\S9            S                         SS jj5       5       5       rSS jrS	rU =r$ ) TFLayoutLMForTokenClassificationiv  r  r  r  r  r  r6   c                V  > [         TU ]  " U/UQ70 UD6  UR                  U l        [        USSS9U l        [
        R                  R                  UR                  S9U l	        [
        R                  R                  UR                  [        UR                  5      SS9U l        Xl        g )NTr  r  r'   r  r   r  r  s       r;   r+   )TFLayoutLMForTokenClassification.__init__  s    3&3F3 +++FdQ[\||++1K1K+L,,,,##.v/G/GH - 

 r=   r  r  c                   U R                  UUUUUUUUU	U
US9nUS   nU R                  XS9nU R                  US9nUc  SOU R                  XS9nU
(       d  U4USS -   nUb  U4U-   $ U$ [	        UUUR
                  UR                  S9$ )	a  
labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.

Returns:

Examples:

```python
>>> import tensorflow as tf
>>> from transformers import AutoTokenizer, TFLayoutLMForTokenClassification

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
>>> model = TFLayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased")

>>> words = ["Hello", "world"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
...     word_tokens = tokenizer.tokenize(word)
...     token_boxes.extend([box] * len(word_tokens))
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> encoding = tokenizer(" ".join(words), return_tensors="tf")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = tf.convert_to_tensor([token_boxes])
>>> token_labels = tf.convert_to_tensor([1, 1, 0, 0])

>>> outputs = model(
...     input_ids=input_ids,
...     bbox=bbox,
...     attention_mask=attention_mask,
...     token_type_ids=token_type_ids,
...     labels=token_labels,
... )

>>> loss = outputs.loss
>>> logits = outputs.logits
```r  r   rd   rb   Nr  ra   r  )r  r6   r  r  r   r   r<  )r8   rk   rl   r   rn   rm   r   ro   r   rA  rB  r  re   r   r  r   r  r   s                     r;   rx   %TFLayoutLMForTokenClassification.call  s    z --))%'/!5#   
 "!*,,o,Q8~t4+?+?v+?+]Y,F)-)9TGf$EvE&!//))	
 	
r=   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = fr  r  rR   s     r;   rQ   &TFLayoutLMForTokenClassification.build  r  r   r  rz   r  )rk   r  rl   r  r   r  rn   r  rm   r  r   r  ro   r  r   rL  rA  rL  rB  rL  r  r  re   rL  r~   z*TFTokenClassifierOutput | tuple[tf.Tensor]r{   )r   r   r   r   r  r  r+   r   r   r  r  r   r   r  rx   rQ   r   r   r   s   @r;   r  r  v  s   *& (2l# *+D+K+KLi+jk+BQ`a .2.28<8<6:377;)-,0#'04 %U
*U
 ,U
 6	U

 6U
 4U
 1U
 5U
 'U
 *U
 !U
 .U
 U
 
4U
 b l U
n	M 	Mr=   r  a  
    LayoutLM Model with a span classification head on top for extractive question-answering tasks such as
    [DocVQA](https://rrc.cvc.uab.es/?ch=17) (a linear layer on top of the final hidden-states output to compute `span
    start logits` and `span end logits`).
    c                     ^  \ rS rSr/ SQrS	U 4S jjr\\" \R                  S5      5      \
" \\S9             S
                           SS jj5       5       5       rSS jrSrU =r$ )TFLayoutLMForQuestionAnsweringi  r  c                   > [         TU ]  " U/UQ70 UD6  UR                  U l        [        USSS9U l        [
        R                  R                  UR                  [        UR                  5      SS9U l
        Xl        g )NTr  r  
qa_outputsr   )r*   r+   r  r  r  r   r1   r   r   r0   r  r,   r  s       r;   r+   'TFLayoutLMForQuestionAnswering.__init__  st    3&3F3 +++FdQ[\,,,,##.v/G/GH - 

 r=   r  r  c                   U R                  UUUUUUUUU	U
US9nUS   nU R                  US9n[        R                  " USSS9u  nn[        R                  " USS9n[        R                  " USS9nSnUb  Ub  S	U0nUUS
'   U R                  UUU4S9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$ )aB  
start_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*):
    Labels for position (index) of the start of the labelled span for computing the token classification loss.
    Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
    are not taken into account for computing the loss.
end_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*):
    Labels for position (index) of the end of the labelled span for computing the token classification loss.
    Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
    are not taken into account for computing the loss.

Returns:

Examples:

```python
>>> import tensorflow as tf
>>> from transformers import AutoTokenizer, TFLayoutLMForQuestionAnswering
>>> from datasets import load_dataset

>>> tokenizer = AutoTokenizer.from_pretrained("impira/layoutlm-document-qa", add_prefix_space=True)
>>> model = TFLayoutLMForQuestionAnswering.from_pretrained("impira/layoutlm-document-qa", revision="1e3ebac")

>>> dataset = load_dataset("nielsr/funsd", split="train")
>>> example = dataset[0]
>>> question = "what's his name?"
>>> words = example["words"]
>>> boxes = example["bboxes"]

>>> encoding = tokenizer(
...     question.split(), words, is_split_into_words=True, return_token_type_ids=True, return_tensors="tf"
... )
>>> bbox = []
>>> for i, s, w in zip(encoding.input_ids[0], encoding.sequence_ids(0), encoding.word_ids(0)):
...     if s == 1:
...         bbox.append(boxes[w])
...     elif i == tokenizer.sep_token_id:
...         bbox.append([1000] * 4)
...     else:
...         bbox.append([0] * 4)
>>> encoding["bbox"] = tf.convert_to_tensor([bbox])

>>> word_ids = encoding.word_ids(0)
>>> outputs = model(**encoding)
>>> loss = outputs.loss
>>> start_scores = outputs.start_logits
>>> end_scores = outputs.end_logits
>>> start, end = word_ids[tf.math.argmax(start_scores, -1)[0]], word_ids[tf.math.argmax(end_scores, -1)[0]]
>>> print(" ".join(words[start : end + 1]))
M. Hamann P. Harper, P. Martinez
```r  r   rb   ra   rX   )r[   num_or_size_splitsr_   )inputr_   Nstart_positionend_positionr  )r  start_logits
end_logitsr   r<  )	r  r  rJ   splitsqueezer  r	   r   r<  )r8   rk   rl   r   rn   rm   r   ro   r   rA  rB  start_positionsend_positionsre   r   r  r   r  r  r  r  r   s                         r;   rx   #TFLayoutLMForQuestionAnswering.call  s!   L --))%'/!5#   
 "!*8#%88&QUW#X jzz2>ZZjr:
&=+D&8F%2F>"''v|Z>X'YD"J/'!"+=F)-)9TGf$EvE-%!!//))
 	
r=   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = f)NTr  r  )
rO   rP   rJ   rK   r  r&   rQ   r  r,   r-   rR   s     r;   rQ   $TFLayoutLMForQuestionAnswering.build  r  r   )rO   r,   r  r  r  rz   r  )rk   r  rl   r  r   r  rn   r  rm   r  r   r  ro   r  r   rL  rA  rL  rB  rL  r  r  r  r  re   rL  r~   z1TFQuestionAnsweringModelOutput | tuple[tf.Tensor]r{   )r   r   r   r   r  r+   r   r   r  r  r   r	   r  rx   rQ   r   r   r   s   @r;   r  r    s!   *&
 *+D+K+KLi+jk+IXgh .2.28<8<6:377;)-,0#'9=7; %h
*h
 ,h
 6	h

 6h
 4h
 1h
 5h
 'h
 *h
 !h
 7h
 5h
 h
 
;h
 i l h
T	M 	Mr=   r  )r  r  r  r  r  r  r  )Dr   
__future__r   r   r  numpynp
tensorflowrJ   activations_tfr   modeling_tf_outputsr   r   r   r	   r
   r   modeling_tf_utilsr   r   r   r   r   r   r   r   r   r   tf_utilsr   r   r   utilsr   r   r   r   configuration_layoutlmr   
get_loggerr   r  r  r1   Layerr    r   r   r   r   r  r  r.  rN  rZ  rc  r  r  r  LAYOUTLM_START_DOCSTRINGr  r  r  r  r  r  __all__r)   r=   r;   <module>r-     s    "     /    S R t t 2 
		H	%"C 5<<-- C NAHell00 AHJL5<<-- L>0.%,,,, 0.hHU\\// H<Lu||)) L>d0ell(( d0PK&** K&^Hu||)) H<"L(:(: "LL-!3!3 -b-** -( L(%,,,, L( L(^ 1  ( T5 p hS*/ S*	S*l RTlm@%57S @% n@%F  xM*CEa xMxMv  |M'@B[ |M|M~ 
 LM%>@W LMLM^r=   