
     `i                      d Z ddlmZ ddlZddlZddlZddlZddl	m
Z
 ddlmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZmZm Z  dd	l!m"Z"m#Z#m$Z$m%Z% d
dl&m'Z'  e$j(        e)          Z*dZ+ G d dej,        j-                  Z. G d dej,        j-                  Z/ G d dej,        j-                  Z0 G d dej,        j-                  Z1 G d dej,        j-                  Z2 G d dej,        j-                  Z3 G d dej,        j-                  Z4 G d dej,        j-                  Z5 G d dej,        j-                  Z6 G d d ej,        j-                  Z7 G d! d"ej,        j-                  Z8 G d# d$ej,        j-                  Z9e G d% d&ej,        j-                              Z: G d' d(e          Z;d)Z<d*Z= e"d+e<           G d, d-e;                      Z> e"d.e<           G d/ d0e;e                      Z? e"d1e<           G d2 d3e;e                      Z@ e"d4e<           G d5 d6e;e                      ZA e"d7e<           G d8 d9e;e                      ZBg d:ZCdS );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                  B     e Zd ZdZd fdZddZ	 	 	 	 	 	 dddZ xZS )TFLayoutLMEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.configr   c                R    t                      j        di | || _        |j        | _        |j        | _        |j        | _        |j        | _        t          j        	                    |j
        d          | _        t          j                            |j                  | _        d S )N	LayerNormepsilonnamerate )super__init__r!   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      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/layoutlm/modeling_tf_layoutlm.pyr+   zTFLayoutLMEmbeddings.__init__;   s    ""6"""!-'-'E$*0*K'!'!988AV]h8ii|++1K+LL    Nc                   t          j        d          5  |                     d| j        j        | j        gt          | j                            | _        d d d            n# 1 swxY w Y   t          j        d          5  |                     d| j        j	        | j        gt          | j                            | _
        d d d            n# 1 swxY w Y   t          j        d          5  |                     d| j        | j        gt          | j                            | _        d d d            n# 1 swxY w Y   t          j        d          5  |                     d| j        | j        gt          | j                            | _        d d d            n# 1 swxY w Y   t          j        d          5  |                     d| j        | j        gt          | j                            | _        d d d            n# 1 swxY w Y   t          j        d	          5  |                     d| j        | j        gt          | j                            | _        d d d            n# 1 swxY w Y   t          j        d
          5  |                     d| j        | j        gt          | j                            | _        d d d            n# 1 swxY w Y   | j        rd S d| _        t'          | dd           `t          j        | j        j                  5  | j                            d d | j        j        g           d d d            d S # 1 swxY w Y   d S d S )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   r/   r>   type_vocab_sizerA   r-   rC   r.   rD   rE   rF   rG   builtgetattrr#   r&   buildr7   input_shapes     r:   rO   zTFLayoutLMEmbeddings.buildF   sR   ],-- 	 	//{-t/?@+D,BCC *  DK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ]233 	 	)-!{2D4DE+D,BCC *9 * *D&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ]011 	 	'+!3T5EF+D,BCC (7 ( (D$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ]233 	 	)-!68HI+D,BCC *9 * *D&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ]233 	 	)-!68HI+D,BCC *9 * *D&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ]233 	 	)-!68HI+D,BCC *9 * *D&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ]233 	 	)-!68HI+D,BCC *9 * *D&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 : 	F
4d++7t~233 L L$$dD$+2I%JKKKL L L L L L L L L L L L L L L L L L 87s   AA""A&)A&ACCC-<D55D9<D9<FF #F ;<HH
H"<I**I.1I.	<KKK(MMMF	input_idstf.Tensor | Nonebboxposition_idstoken_type_idsinputs_embedstrainingboolreturn	tf.Tensorc                4   ||J |5t          || j        j                   t          j        | j        |          }t          |          dd         }|t          j        |d          }|0t          j        t          j	        d|d                   d          }|0t          j        t          j	        d|d                   d          }|t          j        |dgz   d	          }	 t          j        | j
        |dddddf                   }t          j        | j        |ddddd
f                   }	t          j        | j
        |dddddf                   }
t          j        | j        |dddddf                   }n"# t          $ r}t          d          |d}~ww xY wt          j        | j        |dddddf         |ddddd
f         z
            }t          j        | j        |dddddf         |dddddf         z
            }t          j        | j        |          }t          j        | j        |          }||z   |z   |z   |	z   |
z   |z   |z   |z   }|                     |          }|                     ||          }|S )z
        Applies embedding based on inputs tensor.

        Returns:
            final_embeddings (`tf.Tensor`): output embedding tensor.
        N)paramsindicesr   dimsvalue)startlimitaxis   )rb   r      r   z:The `bbox`coordinate values should be within 0-1000 range.inputsrj   rX   )r   r!   rK   rH   gatherr>   r   fillexpand_dimsrangerD   rE   
IndexErrorrF   rG   rC   rA   r#   r5   )r7   rR   rT   rU   rV   rW   rX   rQ   left_position_embeddingsupper_position_embeddingsright_position_embeddingslower_position_embeddingserF   rG   position_embedstoken_type_embedsfinal_embeddingss                     r:   callzTFLayoutLMEmbeddings.call   s6    %-*?*?@ *9dk6LMMMIT[)LLLM //4!W+Q???N>"(+b/*R*R*RYZ[[[L>"(+b/*R*R*RYZ[[[L<7;!,A666D	b')y1KTRSRSRSUVUVUVXYRY]'['[$(*	$2LdSTSTSTVWVWVWYZSZm(\(\%(*	$2LdSTSTSTVWVWVWYZSZm(\(\%(*	$2LdSTSTSTVWVWVWYZSZm(\(\%% 	b 	b 	bYZZ`aa	b "	$*Dd111aaaQR7mVZ[\[\[\^_^_^_ab[bVcFc d d "	$*Dd111aaaQR7mVZ[\[\[\^_^_^_ab[bVcFc d d)4+C\ZZZIT-GQ_```  '' (	(
 (( (( $$ $$ 	  >>1A>BB<</?(<SSs   .BF 
F*F%%F*r!   r   N)NNNNNF)rR   rS   rT   rS   rU   rS   rV   rS   rW   rS   rX   rY   rZ   r[   )__name__
__module____qualname____doc__r+   rO   ry   __classcell__r9   s   @r:   r    r    8   s        QQ	M 	M 	M 	M 	M 	M7L 7L 7L 7Lv '+!%)-+/*.<  <  <  <  <  <  <  <  < r;   r    c                  <     e Zd Zd fdZdd	Z	 dddZddZ xZS )TFLayoutLMSelfAttentionr!   r   c                L    t                      j        d
i | |j        |j        z  dk    r t	          d|j         d|j         d          |j        | _        t          |j        |j        z            | _        | j        | j        z  | _        t          j	        | j                  | _
        t          j                            | j        t          |j                  d          | _        t          j                            | j        t          |j                  d          | _        t          j                            | j        t          |j                  d          | _        t          j                            |j        	          | _        |j        | _        || _        d S )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()queryunitskernel_initializerr&   keyrb   r'   r)   )r*   r+   r,   num_attention_heads
ValueErrorintattention_head_sizeall_head_sizemathsqrtsqrt_att_head_sizer   r0   Denser   r/   r   r   rb   r3   attention_probs_dropout_probr5   
is_decoderr!   r6   s      r:   r+   z TFLayoutLMSelfAttention.__init__   s   ""6""" ::a??EF$6 E E'-'AE E E  
 $*#= #&v'9F<V'V#W#W !58PP"&)D,D"E"E\''$Ia9b9bip ( 
 

 <%%$Ia9b9bin & 
 
 \''$Ia9b9bip ( 
 

 |++1T+UU +r;   tensorr[   
batch_sizer   rZ   c                z    t          j        ||d| j        | j        f          }t          j        |g d          S )Nr_   r   r?   r   rh   r   r   perm)rH   reshaper   r   	transpose)r7   r   r   s      r:   transpose_for_scoresz,TFLayoutLMSelfAttention.transpose_for_scores   sA    6*b$BZ\`\t1uvvv |F6666r;   Fhidden_statesattention_mask	head_maskencoder_hidden_statesencoder_attention_maskpast_key_valuetuple[tf.Tensor]output_attentionsrY   rX   c	                \   t          |          d         }	|                     |          }
|d u}|r||d         }|d         }|}n@|rW|                     |                     |          |	          }|                     |                     |          |	          }|}n||                     |                     |          |	          }|                     |                     |          |	          }t          j        |d         |gd          }t          j        |d         |gd          }nT|                     |                     |          |	          }|                     |                     |          |	          }|                     |
|	          }| j        r||f}t          j        ||d          }t          j	        | j
        |j                  }t          j        ||          }|t          j        ||          }t          |d	
          }|                     ||          }|t          j        ||          }t          j        ||          }t          j        |g d          }t          j        ||	d	| j        f          }|r||fn|f}| j        r||fz   }|S )Nr   ri   r   rh   re   T)transpose_bdtyper_   )logitsrf   rk   r   r   r   )r   r   r   r   rb   rH   concatr   matmulcastr   r   divideaddr   r5   multiplyr   r   r   )r7   r   r   r   r   r   r   r   rX   r   mixed_query_layeris_cross_attention	key_layervalue_layerquery_layerattention_scoresdkattention_probsattention_outputoutputss                       r:   ry   zTFLayoutLMSelfAttention.call   s     ..q1
 JJmJ<<
 3$> 	b."<&q)I(+K3NN 	b11$((BW(2X2XZdeeI33DJJF[J4\4\^hiiK3NN'11$((-(2P2PR\]]I33DJJmJ4T4TV`aaK	>!#4i"@qIIII)^A%6$D1MMMKK11$((-(2P2PR\]]I33DJJmJ4T4TV`aaK//0A:NN? 	6 (5N 9[)NNNWT,4D4JKKK9%5r::%!v&6GG )0@rJJJ ,,o,QQ   k/9EEO9_kBB<(8|||LLL :-=jRTVZVhEijjj9Jc#_55QaPc? 	2 11Gr;   Nc                   | j         rd S d| _         t          | dd           ]t          j        | j        j                  5  | j                            d d | j        j        g           d d d            n# 1 swxY w Y   t          | dd           ]t          j        | j	        j                  5  | j	                            d d | j        j        g           d d d            n# 1 swxY w Y   t          | dd           `t          j        | j
        j                  5  | j
                            d d | j        j        g           d d d            d S # 1 swxY w Y   d S d S )NTr   r   rb   )rM   rN   rH   rI   r   r&   rO   r!   r,   r   rb   rP   s     r:   rO   zTFLayoutLMSelfAttention.build4  s:   : 	F
4$''3tz// H H
  $dk.E!FGGGH H H H H H H H H H H H H H H4%%1tx}-- F FdDK,CDEEEF F F F F F F F F F F F F F F4$''3tz// H H
  $dk.E!FGGGH H H H H H H H H H H H H H H H H H 43s6    (A44A8;A8.(C""C&)C&(EEErz   )r   r[   r   r   rZ   r[   F)r   r[   r   r[   r   r[   r   r[   r   r[   r   r   r   rY   rX   rY   rZ   r   r{   )r|   r}   r~   r+   r   ry   rO   r   r   s   @r:   r   r      s             87 7 7 7  O O O O ObH H H H H H H Hr;   r   c                  2     e Zd Zd fdZdddZddZ xZS )TFLayoutLMSelfOutputr!   r   c                n    t                      j        di | t          j                            |j        t          |j                  d          | _        t          j        	                    |j
        d          | _        t          j                            |j                  | _        || _        d S Ndenser   r#   r$   r'   r)   r*   r+   r   r0   r   r,   r   r/   r   r1   r2   r#   r3   r4   r5   r!   r6   s      r:   r+   zTFLayoutLMSelfOutput.__init__E      ""6"""\''$Ia9b9bip ( 
 

 88AV]h8ii|++1K+LLr;   Fr   r[   input_tensorrX   rY   rZ   c                    |                      |          }|                     ||          }|                     ||z             }|S Nri   rk   r   r5   r#   r7   r   r   rX   s       r:   ry   zTFLayoutLMSelfOutput.callO  H    

-
88MHMMml.JKKr;   Nc                   | j         rd S d| _         t          | dd           ]t          j        | j        j                  5  | j                            d d | j        j        g           d d d            n# 1 swxY w Y   t          | dd           `t          j        | j	        j                  5  | j	                            d d | j        j        g           d d d            d S # 1 swxY w Y   d S d S NTr   r#   
rM   rN   rH   rI   r   r&   rO   r!   r,   r#   rP   s     r:   rO   zTFLayoutLMSelfOutput.buildV     : 	F
4$''3tz// H H
  $dk.E!FGGGH H H H H H H H H H H H H H H4d++7t~233 L L$$dD$+2I%JKKKL L L L L L L L L L L L L L L L L L 87$    (A44A8;A8.(C##C'*C'rz   r   r   r[   r   r[   rX   rY   rZ   r[   r{   r|   r}   r~   r+   ry   rO   r   r   s   @r:   r   r   D  r                 	L 	L 	L 	L 	L 	L 	L 	Lr;   r   c                  :     e Zd Zd fdZd Z	 dddZddZ xZS )TFLayoutLMAttentionr!   r   c                     t                      j        di | t          |d          | _        t	          |d          | _        d S )Nr7   r&   outputr)   )r*   r+   r   self_attentionr   dense_outputr6   s      r:   r+   zTFLayoutLMAttention.__init__d  sP    ""6"""5f6JJJ0hGGGr;   c                    t           r{   NotImplementedError)r7   headss     r:   prune_headszTFLayoutLMAttention.prune_headsj  s    !!r;   Fr   r[   r   r   r   r   r   r   r   rY   rX   rZ   c	           
         |                      ||||||||          }	|                     |	d         ||          }
|
f|	dd          z   }|S )Nr   r   r   r   r   r   r   rX   r   r   r   rX   r   )r   r   )r7   r   r   r   r   r   r   r   rX   self_outputsr   r   s               r:   ry   zTFLayoutLMAttention.callm  s{     **&)"7#9)/ + 	
 	
  ,,&q/x - 
 
 $%QRR(88r;   Nc                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTr   r   )rM   rN   rH   rI   r   r&   rO   r   rP   s     r:   rO   zTFLayoutLMAttention.build  sX   : 	F
4)400<t2788 0 0#))$///0 0 0 0 0 0 0 0 0 0 0 0 0 0 04..:t0566 . .!''---. . . . . . . . . . . . . . . . . . ;:$    A''A+.A+!C		CCrz   r   )r   r[   r   r[   r   r[   r   r[   r   r[   r   r   r   rY   rX   rY   rZ   r   r{   )r|   r}   r~   r+   r   ry   rO   r   r   s   @r:   r   r   c  s        H H H H H H" " "     :	. 	. 	. 	. 	. 	. 	. 	.r;   r   c                  0     e Zd Zd
 fdZddZdd	Z xZS )TFLayoutLMIntermediater!   r   c                D    t                      j        di | t          j                            |j        t          |j                  d          | _        t          |j
        t                    rt          |j
                  | _        n|j
        | _        || _        d S )Nr   r   r)   )r*   r+   r   r0   r   intermediate_sizer   r/   r   
isinstance
hidden_actstrr   intermediate_act_fnr!   r6   s      r:   r+   zTFLayoutLMIntermediate.__init__  s    ""6"""\''*vOg?h?hov ( 
 

 f'-- 	9'89J'K'KD$$'-'8D$r;   r   r[   rZ   c                \    |                      |          }|                     |          }|S Nri   )r   r   r7   r   s     r:   ry   zTFLayoutLMIntermediate.call  s.    

-
8800??r;   Nc                   | j         rd S d| _         t          | dd           `t          j        | j        j                  5  | j                            d d | j        j        g           d d d            d S # 1 swxY w Y   d S d S NTr   	rM   rN   rH   rI   r   r&   rO   r!   r,   rP   s     r:   rO   zTFLayoutLMIntermediate.build      : 	F
4$''3tz// H H
  $dk.E!FGGGH H H H H H H H H H H H H H H H H H 43    (A55A9<A9rz   r   r[   rZ   r[   r{   r   r   s   @r:   r   r     sm                H H H H H H H Hr;   r   c                  2     e Zd Zd fdZdddZddZ xZS )TFLayoutLMOutputr!   r   c                n    t                      j        di | t          j                            |j        t          |j                  d          | _        t          j        	                    |j
        d          | _        t          j                            |j                  | _        || _        d S r   r   r6   s      r:   r+   zTFLayoutLMOutput.__init__  r   r;   Fr   r[   r   rX   rY   rZ   c                    |                      |          }|                     ||          }|                     ||z             }|S r   r   r   s       r:   ry   zTFLayoutLMOutput.call  r   r;   Nc                   | j         rd S d| _         t          | dd           ]t          j        | j        j                  5  | j                            d d | j        j        g           d d d            n# 1 swxY w Y   t          | dd           `t          j        | j	        j                  5  | j	                            d d | j        j
        g           d d d            d S # 1 swxY w Y   d S d S r   )rM   rN   rH   rI   r   r&   rO   r!   r   r#   r,   rP   s     r:   rO   zTFLayoutLMOutput.build  s   : 	F
4$''3tz// N N
  $dk.K!LMMMN N N N N N N N N N N N N N N4d++7t~233 L L$$dD$+2I%JKKKL L L L L L L L L L L L L L L L L L 87r   rz   r   r   r{   r   r   s   @r:   r   r     r   r;   r   c                  4     e Zd Zd fdZ	 dddZddZ xZS )TFLayoutLMLayerr!   r   c                Z    t                      j        di | t          |d          | _        |j        | _        |j        | _        | j        r/| j        st          |  d          t          |d          | _        t          |d          | _	        t          |d          | _        d S )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_outputr6   s      r:   r+   zTFLayoutLMLayer.__init__  s    ""6""",V+FFF +#)#= # 	U? j D!h!h!hiii"5fCS"T"T"TD26OOO+FBBBr;   Fr   r[   r   r   r   rS   r   r   tuple[tf.Tensor] | Noner   rY   rX   rZ   r   c	           
     ,   |
|d d         nd }	|                      |||d d |	||          }
|
d         }| j        r|
dd         }|
d         }n
|
dd          }d }| j        rr|pt          | d          st          d|  d          |
|d	d          nd }|                     ||||||||          }|d         }||dd         z   }|d         }||z   }|                     |
          }|                     |||          }|f|z   }| j        r||fz   }|S )Nrh   )r   r   r   r   r   r   r   rX   r   r   r_   r   z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`r   r   )r   r   hasattrr   r   r  r  )r7   r   r   r   r   r   r   r   rX   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:   ry   zTFLayoutLMLayer.call  s    :H9S>"1"#5#5Y] !%&)"&#'3/ "0 	"
 	"
 2!4 ? 	1,QrT2G 6r :,QRR0G'+$? 	Q4@4!122  Dd D D D   @N?Yrss(;(;_c%&*&9&9--#&;'=8"3! ': 	' 	'#  7q9 7" ==G ,C2+F( 14P P"//>N/OO''-<LW_ ( 
 
  /G+ ? 	5!2 44Gr;   Nc                4   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           St          j        | j	        j                  5  | j	                            d            d d d            d S # 1 swxY w Y   d S d S )NTr   r  r  r   )
rM   rN   rH   rI   r   r&   rO   r  r  r   rP   s     r:   rO   zTFLayoutLMLayer.build)  s~   : 	F
4d++7t~233 + +$$T***+ + + + + + + + + + + + + + +4..:t0566 . .!''---. . . . . . . . . . . . . . .4--9t/455 - - &&t,,,- - - - - - - - - - - - - - -4)400<t2788 0 0#))$///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 =<sH    A''A+.A+!CCCD))D-0D-#FFFrz   r   )r   r[   r   r[   r   r[   r   rS   r   rS   r   r  r   rY   rX   rY   rZ   r   r{   r   r   s   @r:   r   r     s{        C C C C C C, E E E E EN0 0 0 0 0 0 0 0r;   r   c                  4     e Zd Zd fdZ	 dddZddZ xZS )TFLayoutLMEncoderr!   r   c                     t                      j        di | | _        fdt          j                  D             | _        d S )Nc                8    g | ]}t          d |           S )zlayer_._r   )r   ).0ir!   s     r:   
<listcomp>z.TFLayoutLMEncoder.__init__.<locals>.<listcomp>@  s,    lllqof>a>>BBBlllr;   r)   )r*   r+   r!   ro   num_hidden_layerslayerr6   s    ` r:   r+   zTFLayoutLMEncoder.__init__=  sS    ""6"""llllERXRjLkLklll


r;   Fr   r[   r   r   r   rS   r   past_key_valuestuple[tuple[tf.Tensor]] | None	use_cachebool | Noner   rY   output_hidden_statesreturn_dictrX   rZ   >TFBaseModelOutputWithPastAndCrossAttentions | tuple[tf.Tensor]c                   |	rdnd }|rdnd }|r| j         j        rdnd }|rdnd }t          | j                  D ]p\  }}|	r||fz   }|||         nd } |||||         |||||          }|d         }|r||d         fz  }|r&||d         fz   }| j         j        r|||d         fz   }q|	r||fz   }|
st	          d ||||fD                       S t          |||||          S )	Nr)   r   r   r_   r   rh   c              3     K   | ]}||V  	d S r{   r)   )r  vs     r:   	<genexpr>z)TFLayoutLMEncoder.call.<locals>.<genexpr>t  s1        ghgtgtgtgtgt r;   )last_hidden_stater  r   
attentionscross_attentions)r!   r  	enumerater  tupler   )r7   r   r   r   r   r   r  r  r   r  r   rX   all_hidden_statesall_attentionsall_cross_attentionsnext_decoder_cacher  layer_moduler   layer_outputss                       r:   ry   zTFLayoutLMEncoder.callB  s    #7@BBD0:d%6d4;;Zdrr`d#,6RR$(44 	V 	VOA|# I$58H$H!3B3N_Q//TXN(L+-#A,&;'=-"3!	 	 	M *!,M ;"}R'8&::"  V!/=3C2E!E;2 V7L7X+?=QRCSBU+U(   	E 1]4D D 	  )+<nNbc      ;+.+%1
 
 
 	
r;   Nc                    | j         rd S d| _         t          | dd           P| j        D ]J}t          j        |j                  5  |                    d            d d d            n# 1 swxY w Y   Id S d S )NTr  )rM   rN   r  rH   rI   r&   rO   )r7   rQ   r  s      r:   rO   zTFLayoutLMEncoder.build  s    : 	F
4$''3 & &]5:.. & &KK%%%& & & & & & & & & & & & & & & 43& &s   A&&A*	-A*	rz   r   )r   r[   r   r[   r   r[   r   rS   r   rS   r  r  r  r  r   rY   r  rY   r   rY   rX   rY   rZ   r!  r{   r   r   s   @r:   r  r  <  sv        m m m m m m" <
 <
 <
 <
 <
|& & & & & & & &r;   r  c                  0     e Zd Zd
 fdZddZdd	Z xZS )TFLayoutLMPoolerr!   r   c                     t                      j        di | t          j                            |j        t          |j                  dd          | _        || _	        d S )Ntanhr   )r   r   
activationr&   r)   )
r*   r+   r   r0   r   r,   r   r/   r   r!   r6   s      r:   r+   zTFLayoutLMPooler.__init__  se    ""6"""\''$.v/GHH	 ( 
 

 r;   r   r[   rZ   c                J    |d d df         }|                      |          }|S )Nr   ri   )r   )r7   r   first_token_tensorpooled_outputs       r:   ry   zTFLayoutLMPooler.call  s1     +111a40

*<
==r;   Nc                   | j         rd S d| _         t          | dd           `t          j        | j        j                  5  | j                            d d | j        j        g           d d d            d S # 1 swxY w Y   d S d S r   r   rP   s     r:   rO   zTFLayoutLMPooler.build  r   r   rz   r   r{   r   r   s   @r:   r3  r3    sm        	 	 	 	 	 	   H H H H H H H Hr;   r3  c                  0     e Zd Zd
 fdZddZdd	Z xZS )!TFLayoutLMPredictionHeadTransformr!   r   c                    t                      j        di | t          j                            |j        t          |j                  d          | _        t          |j
        t                    rt          |j
                  | _        n|j
        | _        t          j                            |j        d          | _        || _        d S )Nr   r   r#   r$   r)   )r*   r+   r   r0   r   r,   r   r/   r   r   r   r   r   transform_act_fnr1   r2   r#   r!   r6   s      r:   r+   z*TFLayoutLMPredictionHeadTransform.__init__  s    ""6"""\''$.v/GHH ( 
 

 f'-- 	6$5f6G$H$HD!!$*$5D!88AV]h8iir;   r   r[   rZ   c                    |                      |          }|                     |          }|                     |          }|S r   )r   r>  r#   r   s     r:   ry   z&TFLayoutLMPredictionHeadTransform.call  sA    

-
88--m<<m<<r;   Nc                   | j         rd S d| _         t          | dd           ]t          j        | j        j                  5  | j                            d d | j        j        g           d d d            n# 1 swxY w Y   t          | dd           `t          j        | j	        j                  5  | j	                            d d | j        j        g           d d d            d S # 1 swxY w Y   d S d S r   r   rP   s     r:   rO   z'TFLayoutLMPredictionHeadTransform.build  r   r   rz   r   r{   r   r   s   @r:   r<  r<    sm             "   	L 	L 	L 	L 	L 	L 	L 	Lr;   r<  c                  P     e Zd Zd fdZddZdd	ZddZddZddZddZ	 xZ
S )TFLayoutLMLMPredictionHeadr!   r   input_embeddingskeras.layers.Layerc                     t                      j        di | || _        |j        | _        t	          |d          | _        || _        d S )N	transformr   r)   )r*   r+   r!   r,   r<  rF  rC  r7   r!   rC  r8   r9   s       r:   r+   z#TFLayoutLMLMPredictionHead.__init__  sV    ""6"""!-:6TTT !1r;   Nc                @   |                      | j        j        fddd          | _        | j        rd S d| _        t          | dd           St          j        | j        j	                  5  | j        
                    d            d d d            d S # 1 swxY w Y   d S d S )NzerosTbias)r?   r@   	trainabler&   rF  )rJ   r!   rK   rJ  rM   rN   rH   rI   rF  r&   rO   rP   s     r:   rO   z TFLayoutLMLMPredictionHead.build  s    OO4;+A*CQXdhouOvv	: 	F
4d++7t~233 + +$$T***+ + + + + + + + + + + + + + + + + + 87s   )BBBrZ   c                    | j         S r{   )rC  r7   s    r:   get_output_embeddingsz0TFLayoutLMLMPredictionHead.get_output_embeddings  s    $$r;   rb   tf.Variablec                \    || j         _        t          |          d         | j         _        d S Nr   )rC  r>   r   rK   r7   rb   s     r:   set_output_embeddingsz0TFLayoutLMLMPredictionHead.set_output_embeddings  s+    ',$+5e+<+<Q+?(((r;   dict[str, tf.Variable]c                    d| j         iS )NrJ  )rJ  rM  s    r:   get_biasz#TFLayoutLMLMPredictionHead.get_bias  s    	""r;   c                j    |d         | _         t          |d                   d         | j        _        d S )NrJ  r   )rJ  r   r!   rK   rR  s     r:   set_biasz#TFLayoutLMLMPredictionHead.set_bias  s.    &M	!+E&M!:!:1!=r;   r   r[   c                j   |                      |          }t          |          d         }t          j        |d| j        g          }t          j        || j        j        d          }t          j        |d|| j        j	        g          }t          j
                            || j                  }|S )Nr  r   r_   r   T)abr   )rb   rJ  )rF  r   rH   r   r,   r   rC  r>   r!   rK   nnbias_addrJ  )r7   r   
seq_lengths      r:   ry   zTFLayoutLMLMPredictionHead.call  s    ]CC..q1

-DDT?UVVV	MT5J5Q_cddd
-JPTP[Pf?ghhh]KKr;   r!   r   rC  rD  r{   rZ   rD  rb   rO  )rZ   rT  r   )r|   r}   r~   r+   rO   rN  rS  rV  rX  ry   r   r   s   @r:   rB  rB    s        
1 
1 
1 
1 
1 
1+ + + +% % % %@ @ @ @# # # #> > > >       r;   rB  c                  0     e Zd Zd fdZdd	ZddZ xZS )TFLayoutLMMLMHeadr!   r   rC  rD  c                h     t                      j        di | t          ||d          | _        d S )Npredictionsr   r)   )r*   r+   rB  re  rG  s       r:   r+   zTFLayoutLMMLMHead.__init__  s?    ""6"""5f>NUbcccr;   sequence_outputr[   rZ   c                2    |                      |          }|S )Nr  )re  )r7   rf  prediction_scoress      r:   ry   zTFLayoutLMMLMHead.call  s     ,,?,KK  r;   Nc                    | j         rd S d| _         t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTre  )rM   rN   rH   rI   re  r&   rO   rP   s     r:   rO   zTFLayoutLMMLMHead.build  s    : 	F
4--9t/455 - - &&t,,,- - - - - - - - - - - - - - - - - - :9    A((A,/A,r_  )rf  r[   rZ   r[   r{   r   r   s   @r:   rc  rc     sk        d d d d d d
! ! ! !
- - - - - - - -r;   rc  c                  x     e Zd ZeZd#d$ fdZd%d	Zd&dZd Ze		 	 	 	 	 	 	 	 	 	 	 	 	 d'd(d!            Z
d)d"Z xZS )*TFLayoutLMMainLayerTr!   r   add_pooling_layerrY   c                     t                      j        di | || _        t          |d          | _        t          |d          | _        |rt          |d          nd | _        d S )NrB   r   encoderpoolerr)   )	r*   r+   r!   r    rB   r  ro  r3  rp  )r7   r!   rm  r8   r9   s       r:   r+   zTFLayoutLMMainLayer.__init__  ss    ""6""".vLIII(i@@@AR\&vH====X\r;   rZ   rD  c                    | j         S r{   )rB   rM  s    r:   get_input_embeddingsz(TFLayoutLMMainLayer.get_input_embeddings!  s
    r;   rb   rO  c                \    || j         _        t          |          d         | j         _        d S rQ  )rB   r>   r   rK   rR  s     r:   set_input_embeddingsz(TFLayoutLMMainLayer.set_input_embeddings$  s)    !&%/%6%6q%9"""r;   c                    t           )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   )r7   heads_to_prunes     r:   _prune_headsz TFLayoutLMMainLayer._prune_heads(  s
    
 "!r;   NFrR   TFModelInputType | NonerT   np.ndarray | tf.Tensor | Noner   rV   rU   r   rW   r   r   r   r  r  r   rX   ATFBaseModelOutputWithPoolingAndCrossAttentions | tuple[tf.Tensor]c                   ||t          d          |t          |          }n)|t          |          d d         }nt          d          |t          j        |d          }|t          j        |d          }|t          j        |dgz   d          }|                     ||||||          }t          j        ||d         dd|d         f          }t          j        ||j        	          }t          j        d
|j        	          }t          j        d|j        	          }t          j	        t          j
        ||          |          }|t          d g| j        j        z  }|                     ||||d d d|
|||          }|d         }| j        |                     |          nd }|s||f|dd          z   S t!          |||j        |j        |j                  S )NzDYou cannot specify both input_ids and inputs_embeds at the same timer_   z5You have to specify either input_ids or inputs_embedsr   r`   r   rg   )rR   rT   rU   rV   rW   rX   r   g      ?g     F)r   r   r   r   r   r  r  r   r  r   rX   r  )r&  pooler_outputr   r'  r(  )r   r   rH   rm   rB   r   r   r   constantr   subtractr   r!   r  ro  rp  r   r   r'  r(  )r7   rR   rT   r   rV   rU   r   rW   r   r   r   r  r   rX   rQ   embedding_outputextended_attention_maskone_cstten_thousand_cstencoder_outputsrf  r9  s                         r:   ry   zTFLayoutLMMainLayer.call/  sU   "  ]%>cddd"$Y//KK&$]33CRC8KKTUUU!W+Q???N!W+Q???N<7qc 1;;;D??%)' + 
 
 #%*^k!naQRT_`aTb=c"d"d #%'*AIYI_"`"`"`+c)9)?@@@;x7G7MNNN"$+bk'CZ.[.[]m"n"n  %%!>>I,,*2"7#' /!5# ' 
 
 *!,FJkF]/BBBcg 	$  #$ $
 >-')7&1,=
 
 
 	
r;   c                r   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTrB   ro  rp  )	rM   rN   rH   rI   rB   r&   rO   ro  rp  rP   s     r:   rO   zTFLayoutLMMainLayer.build  s   : 	F
4t,,8t344 , ,%%d+++, , , , , , , , , , , , , , ,4D))5t|011 ) )""4((() ) ) ) ) ) ) ) ) ) ) ) ) ) )44((4t{/00 ( (!!$'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( 54s6    A''A+.A+!CCCD**D.1D.)T)r!   r   rm  rY   r`  ra  NNNNNNNNNNNNF)rR   rx  rT   ry  r   ry  rV   ry  rU   ry  r   ry  rW   ry  r   ry  r   ry  r   r  r  r  r   r  rX   rY   rZ   rz  r{   )r|   r}   r~   r   config_classr+   rr  rt  rw  r   ry   rO   r   r   s   @r:   rl  rl    s        !L] ] ] ] ] ] ]   : : : :" " "  .2.28<8<6:377;?C@D)-,0#'c
 c
 c
 c
 ]c
J( ( ( ( ( ( ( (r;   rl  c                  :     e Zd ZdZeZdZe fd            Z xZ	S )TFLayoutLMPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    layoutlmc                v    t                      j        }t          j        dt          j        d          |d<   |S )N)NNrg   rT   )r?   r   r&   )r*   input_signaturerH   
TensorSpecint32)r7   	signaturer9   s     r:   r  z)TFLayoutLMPreTrainedModel.input_signature  s3    GG+	MrxV\]]]	&r;   )
r|   r}   r~   r   r   r  base_model_prefixpropertyr  r   r   s   @r:   r  r    sY         
 "L"    X    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                       e Zd Zd fdZe ee                    d                     ee	e
          	 	 	 	 	 	 	 	 	 	 	 	 	 ddd                                    ZddZ xZS ) TFLayoutLMModelr!   r   c                n     t                      j        |g|R i | t          |d          | _        d S )Nr  r   )r*   r+   rl  r  r7   r!   rj   r8   r9   s       r:   r+   zTFLayoutLMModel.__init__  sB    3&333F333+FDDDr;   batch_size, sequence_lengthoutput_typer  NFrR   rx  rT   ry  r   rV   rU   r   rW   r   r   r   r  r  r   rX   rZ   rz  c                F    |                      ||||||||
|||          }|S )a   
        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
        ```rR   rT   r   rV   rU   r   rW   r   r  r   rX   )r  )r7   rR   rT   r   rV   rU   r   rW   r   r   r   r  r   rX   r   s                  r:   ry   zTFLayoutLMModel.call   sE    n --))%'/!5#   
 
 r;   c                    | j         rd S d| _         t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTr  )rM   rN   rH   rI   r  r&   rO   rP   s     r:   rO   zTFLayoutLMModel.buildg  s    : 	F
4T**6t}122 * *##D)))* * * * * * * * * * * * * * * * * * 76rj  rz   r  )rR   rx  rT   ry  r   ry  rV   ry  rU   ry  r   ry  rW   ry  r   ry  r   ry  r   r  r  r  r   r  rX   r  rZ   rz  r{   )r|   r}   r~   r+   r   r   LAYOUTLM_INPUTS_DOCSTRINGformatr   r   _CONFIG_FOR_DOCry   rO   r   r   s   @r:   r  r    s        
E E E E E E
 **+D+K+KLi+j+jkkBQ`  
 .2.28<8<6:377;?C@D)-,0#' %@ @ @ @  lk ]
@D* * * * * * * *r;   r  z6LayoutLM Model with a `language modeling` head on top.c                       e Zd Zg dZd  fdZd!dZd"d	Ze ee	
                    d
                     eee          	 	 	 	 	 	 	 	 	 	 	 	 d#d$d                                    Zd%dZ xZS )&TFLayoutLMForMaskedLM)rp  cls.seq_relationshipzcls.predictions.decoder.weight	nsp___clsr!   r   c                     t                      j        |g|R i | |j        rt                              d           t          |dd          | _        t          || j        j        d          | _	        d S )NzqIf you want to use `TFLayoutLMForMaskedLM` make sure `config.is_decoder=False` for bi-directional self-attention.Tr  rm  r&   	mlm___cls)rC  r&   )
r*   r+   r   loggerwarningrl  r  rc  rB   mlmr  s       r:   r+   zTFLayoutLMForMaskedLM.__init__z  s    3&333F333 	NN1  
 ,FdQ[\\\$Vdm>V]hiiir;   rZ   rD  c                    | j         j        S r{   )r  re  rM  s    r:   get_lm_headz!TFLayoutLMForMaskedLM.get_lm_head  s    x##r;   r   c                    t          j        dt                     | j        dz   | j        j        z   dz   | j        j        j        z   S )NzMThe method get_prefix_bias_name is deprecated. Please use `get_bias` instead./)warningswarnFutureWarningr&   r  re  rM  s    r:   get_prefix_bias_namez*TFLayoutLMForMaskedLM.get_prefix_bias_name  s=    egtuuuy3.4tx7K7PPPr;   r  r  NFrR   rx  rT   ry  r   rV   rU   r   rW   r   r  r  r   labelsrX   #TFMaskedLMOutput | tuple[tf.Tensor]c                $   |                      |||||||||	|
|          }|d         }|                     ||          }|dn|                     ||          }|
s|f|dd         z   }||f|z   n|S t          |||j        |j                  S )aV  
        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   )rf  rX   Nr  r   rh   lossr   r   r'  )r  r  hf_compute_lossr   r   r'  )r7   rR   rT   r   rV   rU   r   rW   r   r  r   r  rX   r   rf  rh  r  r   s                     r:   ry   zTFLayoutLMForMaskedLM.call  s    ~ --))%'/!5#   
 
 "!* HH_xHXX~tt4+?+?vVg+?+h+h 	F')GABBK7F)-)9TGf$$vE$!/)	
 
 
 	
r;   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTr  r  )rM   rN   rH   rI   r  r&   rO   r  rP   s     r:   rO   zTFLayoutLMForMaskedLM.build  sP   : 	F
4T**6t}122 * *##D)))* * * * * * * * * * * * * * *4%%1tx}-- % %t$$$% % % % % % % % % % % % % % % % % % 21r   rz   r`  )rZ   r   NNNNNNNNNNNF)rR   rx  rT   ry  r   ry  rV   ry  rU   ry  r   ry  rW   ry  r   r  r  r  r   r  r  ry  rX   r  rZ   r  r{   )r|   r}   r~   "_keys_to_ignore_on_load_unexpectedr+   r  r  r   r   r  r  r   r   r  ry   rO   r   r   s   @r:   r  r  p  s*       * * *&
j 
j 
j 
j 
j 
j$ $ $ $Q Q Q Q **+D+K+KLi+j+jkk+;/ZZZ .2.28<8<6:377;)-,0#'04 %V
 V
 V
 V
 [Z lk ]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                       e Zd Zg dZdgZd fdZe ee	                    d                     e
ee          	 	 	 	 	 	 	 	 	 	 	 	 ddd                                    Zd dZ xZS )!#TFLayoutLMForSequenceClassification)r  r  cls.predictionsr  r5   r!   r   c                d    t                      j        |g|R i | |j        | _        t          |d          | _        t
          j                            |j                  | _	        t
          j        
                    |j        t          |j                  d          | _        || _        d S )Nr  r   r'   
classifierr   r*   r+   
num_labelsrl  r  r   r0   r3   r4   r5   r   r   r/   r  r!   r  s       r:   r+   z,TFLayoutLMForSequenceClassification.__init__   s    3&333F333 ++FDDD|++1K+LL,,,#.v/GHH - 
 

 r;   r  r  NFrR   rx  rT   ry  r   rV   rU   r   rW   r   r  r  r   r  rX   rZ   -TFSequenceClassifierOutput | tuple[tf.Tensor]c                P   |                      |||||||||	|
|          }|d         }|                     ||          }|                     |          }|dn|                     ||          }|
s|f|dd         z   }||f|z   n|S t	          |||j        |j                  S )	an  
        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   rk   ri   Nr  rh   r  )r  r5   r  r  r
   r   r'  )r7   rR   rT   r   rV   rU   r   rW   r   r  r   r  rX   r   r9  r   r  r   s                     r:   ry   z(TFLayoutLMForSequenceClassification.call  s    ~ --))%'/!5#   
 
  
MHMM66~tt4+?+?vV\+?+]+] 	FY,F)-)9TGf$$vE)!/)	
 
 
 	
r;   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           `t          j        | j        j                  5  | j                            d d | j        j	        g           d d d            d S # 1 swxY w Y   d S d S NTr  r  
rM   rN   rH   rI   r  r&   rO   r  r!   r,   rP   s     r:   rO   z)TFLayoutLMForSequenceClassification.buildj  s   : 	F
4T**6t}122 * *##D)))* * * * * * * * * * * * * * *4t,,8t344 M M%%tT4;3J&KLLLM M M M M M M M M M M M M M M M M M 98$    A''A+.A+!(CCCrz   r  )rR   rx  rT   ry  r   ry  rV   ry  rU   ry  r   ry  rW   ry  r   r  r  r  r   r  r  ry  rX   r  rZ   r  r{   )r|   r}   r~   r  _keys_to_ignore_on_load_missingr+   r   r   r  r  r   r
   r  ry   rO   r   r   s   @r:   r  r    s        *s)r)r&'1l#      **+D+K+KLi+j+jkk+ETcddd .2.28<8<6:377;)-,0#'04 %W
 W
 W
 W
 ed lk ]W
r	M 	M 	M 	M 	M 	M 	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                       e Zd Zg dZdgZd fdZe ee	                    d                     e
ee          	 	 	 	 	 	 	 	 	 	 	 	 ddd                                    Zd dZ xZS )! TFLayoutLMForTokenClassificationrp  r  r  r  r  r5   r!   r   c                f    t                      j        |g|R i | |j        | _        t          |dd          | _        t
          j                            |j                  | _	        t
          j        
                    |j        t          |j                  d          | _        || _        d S )NTr  r  r'   r  r   r  r  s       r:   r+   z)TFLayoutLMForTokenClassification.__init__  s    3&333F333 ++FdQ[\\\|++1K+LL,,,#.v/GHH - 
 

 r;   r  r  NFrR   rx  rT   ry  r   rV   rU   r   rW   r   r  r  r   r  rX   rZ   *TFTokenClassifierOutput | tuple[tf.Tensor]c                P   |                      |||||||||	|
|          }|d         }|                     ||          }|                     |          }|dn|                     ||          }|
s|f|dd         z   }||f|z   n|S t	          |||j        |j                  S )	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   rk   ri   Nr  rh   r  )r  r5   r  r  r   r   r'  )r7   rR   rT   r   rV   rU   r   rW   r   r  r   r  rX   r   rf  r   r  r   s                     r:   ry   z%TFLayoutLMForTokenClassification.call  s    z --))%'/!5#   
 
 "!*,,o,QQ88~tt4+?+?vV\+?+]+] 	FY,F)-)9TGf$$vE&!/)	
 
 
 	
r;   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           `t          j        | j        j                  5  | j                            d d | j        j	        g           d d d            d S # 1 swxY w Y   d S d S r  r  rP   s     r:   rO   z&TFLayoutLMForTokenClassification.build  r  r  rz   r  )rR   rx  rT   ry  r   ry  rV   ry  rU   ry  r   ry  rW   ry  r   r  r  r  r   r  r  ry  rX   r  rZ   r  r{   )r|   r}   r~   r  r  r+   r   r   r  r  r   r   r  ry   rO   r   r   s   @r:   r  r  v  s       * * *& (2l#      **+D+K+KLi+j+jkk+BQ`aaa .2.28<8<6:377;)-,0#'04 %U
 U
 U
 U
 ba lk ]U
n	M 	M 	M 	M 	M 	M 	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                       e Zd Zg dZd fdZe ee                    d                     e	e
e          	 	 	 	 	 	 	 	 	 	 	 	 	 ddd                                    Zd dZ xZS )!TFLayoutLMForQuestionAnsweringr  r!   r   c                    t                      j        |g|R i | |j        | _        t          |dd          | _        t
          j                            |j        t          |j	                  d          | _
        || _        d S )NTr  r  
qa_outputsr   )r*   r+   r  rl  r  r   r0   r   r   r/   r  r!   r  s       r:   r+   z'TFLayoutLMForQuestionAnswering.__init__  s    3&333F333 ++FdQ[\\\,,,#.v/GHH - 
 

 r;   r  r  NFrR   rx  rT   ry  r   rV   rU   r   rW   r   r  r  r   start_positionsend_positionsrX   rZ   1TFQuestionAnsweringModelOutput | tuple[tf.Tensor]c                   |                      |||||||||	|
|          }|d         }|                     |          }t          j        |dd          \  }}t          j        |d          }t          j        |d          }d}|$|"d	|i}||d
<   |                     |||f          }|
s||f|dd         z   }||f|z   n|S t          ||||j        |j                  S )a	  
        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   ri   rh   r_   )rb   num_or_size_splitsrf   )inputrf   Nstart_positionend_positionr  )r  start_logits
end_logitsr   r'  )	r  r  rH   splitsqueezer  r	   r   r'  )r7   rR   rT   r   rV   rU   r   rW   r   r  r   r  r  rX   r   rf  r   r  r  r  r  r   s                         r:   ry   z#TFLayoutLMForQuestionAnswering.call  sD   L --))%'/!5#   
 
 "!*88#%8&QUW#X#X#X jz2>>>Zjr:::
&=+D&8F%2F>"''v|Z>X'YYD 	F"J/'!""+=F)-)9TGf$$vE-%!!/)
 
 
 	
r;   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           `t          j        | j        j                  5  | j                            d d | j        j	        g           d d d            d S # 1 swxY w Y   d S d S )NTr  r  )
rM   rN   rH   rI   r  r&   rO   r  r!   r,   rP   s     r:   rO   z$TFLayoutLMForQuestionAnswering.build  r  r  rz   r  )rR   rx  rT   ry  r   ry  rV   ry  rU   ry  r   ry  rW   ry  r   r  r  r  r   r  r  ry  r  ry  rX   r  rZ   r  r{   )r|   r}   r~   r  r+   r   r   r  r  r   r	   r  ry   rO   r   r   s   @r:   r  r    s       * * *&
 
 
 
 
 
 **+D+K+KLi+j+jkk+IXghhh .2.28<8<6:377;)-,0#'9=7; %h
 h
 h
 h
 ih lk ]h
T	M 	M 	M 	M 	M 	M 	M 	Mr;   r  )r  r  r  r  rl  r  r  )Dr   
__future__r   r   r  numpynp
tensorflowrH   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  r0   Layerr    r   r   r   r   r   r   r  r3  r<  rB  rc  rl  r  LAYOUTLM_START_DOCSTRINGr  r  r  r  r  r  __all__r)   r;   r:   <module>r     s     " " " " " "           / / / / / /                                       S R R R R R R R R R t t t t t t t t t t t t 2 2 2 2 2 2 
	H	%	%"C  C  C  C  C 5<- C  C  C NAH AH AH AH AHel0 AH AH AHJL L L L L5<- L L L>0. 0. 0. 0. 0.%,, 0. 0. 0.hH H H H HU\/ H H H<L L L L Lu|) L L L>d0 d0 d0 d0 d0el( d0 d0 d0PK& K& K& K& K&* K& K& K&^H H H H Hu|) H H H<"L "L "L "L "L(: "L "L "LL- - - - -!3 - - -b- - - - -* - - -( L( L( L( L( L(%,, L( L( L(^     1    ( T5 p h S* S* S* S* S*/ S* S*	 S*l RTlmm@% @% @% @% @%57S @% @% nm@%F   xM xM xM xM xM*CEa xM xM xMv   |M |M |M |M |M'@B[ |M |M |M~ 
  LM LM LM LM LM%>@W LM LM LM^  r;   