Ë
    T^(hÇ ã                  óL  — d Z ddlmZ ddlZddlZddlmZmZmZm	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)jZ                  e.«      Z/dZ0 G d„ dejb                  jd                  «      Z3 G d„ dejb                  jd                  «      Z4 G d„ dejb                  jd                  «      Z5 G d„ dejb                  jd                  «      Z6 G d„ dejb                  jd                  «      Z7 G d„ dejb                  jd                  «      Z8 G d„ dejb                  jd                  «      Z9 G d„ dejb                  jd                  «      Z: G d„ dejb                  jd                  «      Z; G d „ d!ejb                  jd                  «      Z< G d"„ d#ejb                  jd                  «      Z= G d$„ d%ejb                  jd                  «      Z>e  G d&„ d'ejb                  jd                  «      «       Z? G d(„ d)e«      Z@d*ZAd+ZB e'd,eA«       G d-„ d.e@«      «       ZC e'd/eA«       G d0„ d1e@e«      «       ZD e'd2eA«       G d3„ d4e@e«      «       ZE e'd5eA«       G d6„ d7e@e«      «       ZF e'd8eA«       G d9„ d:e@e«      «       ZGg d;¢ZHy)<zTF 2.0 LayoutLM model.é    )ÚannotationsN)ÚDictÚOptionalÚTupleÚUnioné   )Ú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                  ó\   ‡ — 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.c                óx  •— t        ‰| �  di |¤Ž || _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        t        j                  j                  |j                  d¬«      | _        t        j                  j                  |j                  ¬«      | _        y )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      €úo/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/layoutlm/modeling_tf_layoutlm.pyr.   zTFLayoutLMEmbeddings.__init__<   s“   ø€ Ü‰ÑÑ"˜6Ò"àˆŒØ!×-Ñ-ˆÔØ'-×'EÑ'EˆÔ$Ø*0×*KÑ*KˆÔ'Ø!'×!9Ñ!9ˆÔÜŸ™×8Ñ8À×AVÑAVÐ]hÐ8ÓiˆŒÜ—|‘|×+Ñ+°×1KÑ1KÐ+ÓLˆ�ó    c                óD  — t        j                  d«      5  | j                  d| j                  j                  | j
                  gt        | j                  «      ¬«      | _        d d d «       t        j                  d«      5  | j                  d| j                  j                  | j
                  gt        | j                  «      ¬«      | _
        d d d «       t        j                  d«      5  | j                  d| j                  | j
                  gt        | j                  «      ¬«      | _        d d d «       t        j                  d«      5  | j                  d| j                  | j
                  gt        | j                  «      ¬«      | _        d d d «       t        j                  d«      5  | j                  d| j                  | j
                  gt        | j                  «      ¬«      | _        d d d «       t        j                  d	«      5  | j                  d| j                  | j
                  gt        | j                  «      ¬«      | _        d d d «       t        j                  d
«      5  | j                  d| j                  | j
                  gt        | j                  «      ¬«      | _        d d d «       | j$                  ry d| _        t'        | dd «      �et        j                  | j(                  j*                  «      5  | j(                  j-                  d d | j                  j
                  g«       d d d «       y y # 1 sw Y   �ŒÛxY w# 1 sw Y   �Œ~xY w# 1 sw Y   �Œ+xY w# 1 sw Y   �ŒØxY w# 1 sw Y   �Œ…xY w# 1 sw Y   �Œ2xY w# 1 sw Y   ŒÞxY w# 1 sw Y   y xY w)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_sizer0   r   r3   rB   Útype_vocab_sizerE   r1   rG   r2   rH   rI   rJ   rK   ÚbuiltÚgetattrr&   r)   Úbuild©r;   Úinput_shapes     r>   rS   zTFLayoutLMEmbeddings.buildG   s   € Ü�]‰]Ð,Ó-ñ 	ØŸ/™/ØØ—{‘{×-Ñ-¨t×/?Ñ/?Ð@Ü+¨D×,BÑ,BÓCð *ó ˆDŒK÷	ô �]‰]Ð2Ó3ñ 	Ø)-¯©Ø!Ø—{‘{×2Ñ2°D×4DÑ4DÐEÜ+¨D×,BÑ,BÓCð *9ó *ˆDÔ&÷	ô �]‰]Ð0Ó1ñ 	Ø'+§¡Ø!Ø×3Ñ3°T×5EÑ5EÐFÜ+¨D×,BÑ,BÓCð (7ó (ˆDÔ$÷	ô �]‰]Ð2Ó3ñ 	Ø)-¯©Ø!Ø×6Ñ6¸×8HÑ8HÐIÜ+¨D×,BÑ,BÓCð *9ó *ˆDÔ&÷	ô �]‰]Ð2Ó3ñ 	Ø)-¯©Ø!Ø×6Ñ6¸×8HÑ8HÐIÜ+¨D×,BÑ,BÓCð *9ó *ˆDÔ&÷	ô �]‰]Ð2Ó3ñ 	Ø)-¯©Ø!Ø×6Ñ6¸×8HÑ8HÐIÜ+¨D×,BÑ,BÓCð *9ó *ˆDÔ&÷	ô �]‰]Ð2Ó3ñ 	Ø)-¯©Ø!Ø×6Ñ6¸×8HÑ8HÐIÜ+¨D×,BÑ,BÓCð *9ó *ˆDÔ&÷	ð �:Š:ØØˆŒ
Ü�4˜ dÓ+Ð7Ü—‘˜tŸ~™~×2Ñ2Ó3ñ LØ—‘×$Ñ$ d¨D°$·+±+×2IÑ2IÐ%JÔK÷Lð Lð 8÷i	ñ 	ú÷	ñ 	ú÷	ñ 	ú÷	ñ 	ú÷	ñ 	ú÷	ñ 	ú÷	ð 	ú÷Lð Lúsf   –AL<Â AM	Ã*AMÅ
AM#Æ*AM0È
AM=É*AN
Ë?3NÌ<MÍ	MÍM Í#M-Í0M:Í=NÎ
NÎNc                óR  — |€|€J ‚|�At        || j                  j                  «       t        j                  | j
                  |¬«      }t        |«      dd }|€t        j                  |d¬«      }|€/t        j                  t        j                  d|d   ¬«      d¬«      }|€/t        j                  t        j                  d|d   ¬«      d¬«      }|€t        j                  |dgz   d¬	«      x}}	 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   «      }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   }| j#                  |¬«      }| j%                  ||¬«      }|S # t        $ r}t        d«      |‚d}~ww xY w)z’
        Applies embedding based on inputs tensor.

        Returns:
            final_embeddings (`tf.Tensor`): output embedding tensor.
        N)ÚparamsÚindiceséÿÿÿÿr   ©ÚdimsÚvalue)ÚstartÚlimit©Úaxisé   )r\   r!   é   r   z:The `bbox`coordinate values should be within 0-1000 range.©Úinputs©rd   Útraining)r   r/   rO   rL   ÚgatherrB   r   ÚfillÚexpand_dimsÚrangerH   rI   Ú
IndexErrorrJ   rK   rG   rE   r&   r9   )r;   Ú	input_idsÚbboxÚposition_idsÚtoken_type_idsÚinputs_embedsrf   rU   Úleft_position_embeddingsÚupper_position_embeddingsÚright_position_embeddingsÚlower_position_embeddingsÚerJ   rK   Úposition_embedsÚtoken_type_embedsÚfinal_embeddingss                     r>   ÚcallzTFLayoutLMEmbeddings.call€   s´  € ð Ð%¨-Ð*?Ð@Ð@àÐ Ü*¨9°d·k±k×6LÑ6LÔMÜŸI™I¨T¯[©[À)ÔLˆMä  Ó/°°Ð4ˆàÐ!ÜŸW™W¨+¸QÔ?ˆNàÐÜŸ>™>¬"¯(©(¸À+ÈbÁ/Ô*RÐYZÔ[ˆLàÐÜŸ>™>¬"¯(©(¸À+ÈbÁ/Ô*RÐYZÔ[ˆLàˆ<ÜŸ'™' +°°Ñ"3¸1Ô=Ð=ˆD�4ð	bÜ')§y¡y°×1KÑ1KÈTÒRSÒUVÐXYÐRYÉ]Ó'[Ð$Ü(*¯	©	°$×2LÑ2LÈdÒSTÒVWÐYZÐSZÉmÓ(\Ð%Ü(*¯	©	°$×2LÑ2LÈdÒSTÒVWÐYZÐSZÉmÓ(\Ð%Ü(*¯	©	°$×2LÑ2LÈdÒSTÒVWÐYZÐSZÉmÓ(\Ð%ô !#§	¡	¨$×*DÑ*DÀdÊ1ÊaÐQRÈ7ÁmÐVZÒ[\Ò^_ÐabÐ[bÑVcÑFcÓ dÐÜ "§	¡	¨$×*DÑ*DÀdÊ1ÊaÐQRÈ7ÁmÐVZÒ[\Ò^_ÐabÐ[bÑVcÑFcÓ dÐäŸ)™)¨4×+CÑ+CÈ\ÔZˆÜŸI™I¨T×-GÑ-GÐQ_Ô`ÐàØñàñ ð 'ñ'ð (ñ	(ð
 (ñ(ð (ñ(ð $ñ$ð $ñ$ð 	ð  Ÿ>™>Ð1A˜>ÓBÐØŸ<™<Ð/?È(˜<ÓSÐàÐøô+ ò 	bÜÐYÓZÐ`aÐaûð	bús   Ã3B(J Ê	J&ÊJ!Ê!J&©r/   r"   ©N)NNNNNF)rl   úOptional[tf.Tensor]rm   r|   rn   r|   ro   r|   rp   r|   rf   ÚboolÚreturnú	tf.Tensor)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   rS   ry   Ú__classcell__©r=   s   @r>   r$   r$   9   sp   ø„ ÙQõ	Mó7Lðv *.Ø$(Ø,0Ø.2Ø-1Øð< à&ð< ð "ð< ð *ð	< ð
 ,ð< ð +ð< ð ð< ð 
÷< r?   r$   c                  ó^   ‡ — e Zd Zdˆ fd„Zdd„Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zd	d„Zˆ xZS )
ÚTFLayoutLMSelfAttentionc                óÂ  •— t        ‰| �  d
i |¤Ž |j                  |j                  z  dk7  r&t	        d|j                  › d|j                  › d�«      ‚|j                  | _        t        |j                  |j                  z  «      | _        | j                  | j                  z  | _        t        j                  | j                  «      | _
        t        j                  j                  | j                  t        |j                  «      d¬«      | _        t        j                  j                  | j                  t        |j                  «      d¬«      | _        t        j                  j                  | j                  t        |j                  «      d¬«      | _        t        j                  j'                  |j(                  ¬	«      | _        |j,                  | _        || _        y )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.   r0   Únum_attention_headsÚ
ValueErrorÚintÚattention_head_sizeÚall_head_sizeÚmathÚsqrtÚsqrt_att_head_sizer   r4   ÚDenser   r3   rŠ   rŽ   r\   r7   Úattention_probs_dropout_probr9   Ú
is_decoderr/   r:   s      €r>   r.   z TFLayoutLMSelfAttention.__init__Á   s“  ø€ Ü‰ÑÑ"˜6Ò"à×Ñ × :Ñ :Ñ:¸aÒ?ÜØ# F×$6Ñ$6Ð#7ð 8'Ø'-×'AÑ'AÐ&BÀ!ðEóð ð
 $*×#=Ñ#=ˆÔ Ü#& v×'9Ñ'9¸F×<VÑ<VÑ'VÓ#WˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔÜ"&§)¡)¨D×,DÑ,DÓ"EˆÔä—\‘\×'Ñ'Ø×$Ñ$¼È×IaÑIaÓ9bÐipð (ó 
ˆŒ
ô —<‘<×%Ñ%Ø×$Ñ$¼È×IaÑIaÓ9bÐinð &ó 
ˆŒô —\‘\×'Ñ'Ø×$Ñ$¼È×IaÑIaÓ9bÐipð (ó 
ˆŒ
ô —|‘|×+Ñ+°×1TÑ1TÐ+ÓUˆŒà ×+Ñ+ˆŒØˆ�r?   c                ó’   — t        j                  ||d| j                  | j                  f¬«      }t        j                  |g d¢¬«      S )NrY   ©ÚtensorrC   ©r   rb   r!   r   ©Úperm)rL   Úreshaper�   r’   Ú	transpose)r;   rœ   Ú
batch_sizes      r>   Útranspose_for_scoresz,TFLayoutLMSelfAttention.transpose_for_scoresÝ   s;   € ä—‘ 6°*¸bÀ$×BZÑBZÐ\`×\tÑ\tÐ1uÔvˆô �|‰|˜FªÔ6Ð6r?   c	                óü  — t        |«      d   }	| j                  |¬«      }
|d u}|r|�|d   }|d   }|}�n|rG| j                  | j                  |¬«      |	«      }| j                  | j	                  |¬«      |	«      }|}nÃ|�}| j                  | j                  |¬«      |	«      }| j                  | j	                  |¬«      |	«      }t        j                  |d   |gd¬«      }t        j                  |d   |gd¬«      }nD| j                  | j                  |¬«      |	«      }| j                  | j	                  |¬«      |	«      }| j                  |
|	«      }| j                  r||f}t        j                  ||d¬«      }t        j                  | j                  |j                  ¬«      }t        j                  ||«      }|�t        j                  ||«      }t        |d	¬
«      }| j                  ||¬«      }|�t        j                   ||«      }t        j                  ||«      }t        j"                  |g d¢¬«      }t        j$                  ||	d	| j&                  f¬«      }|r||fn|f}| j                  r||fz   }|S )Nr   rc   r!   rb   r_   T)Útranspose_b©ÚdtyperY   )Úlogitsr`   re   r�   rž   r›   )r   rŠ   r£   rŽ   r\   rL   Úconcatr™   ÚmatmulÚcastr–   r§   ÚdivideÚaddr   r9   Úmultiplyr¡   r    r“   )r;   Úhidden_statesÚattention_maskÚ	head_maskÚencoder_hidden_statesÚencoder_attention_maskÚpast_key_valueÚoutput_attentionsrf   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ä   sy  € ô   Ó.¨qÑ1ˆ
Ø ŸJ™J¨m˜JÓ<Ðð
 3¸$Ð>Ðá .Ð"<à& qÑ)ˆIØ(¨Ñ+ˆKØ3ŠNÙØ×1Ñ1°$·(±(ÐBW°(Ó2XÐZdÓeˆIØ×3Ñ3°D·J±JÐF[°JÓ4\Ð^hÓiˆKØ3‰NØÐ'Ø×1Ñ1°$·(±(À-°(Ó2PÐR\Ó]ˆIØ×3Ñ3°D·J±JÀm°JÓ4TÐV`ÓaˆKÜŸ	™	 >°!Ñ#4°iÐ"@ÀqÔIˆIÜŸ)™) ^°AÑ%6¸Ð$DÈ1ÔM‰Kà×1Ñ1°$·(±(À-°(Ó2PÐR\Ó]ˆIØ×3Ñ3°D·J±JÀm°JÓ4TÐV`ÓaˆKà×/Ñ/Ð0AÀ:ÓNˆà�?Š?ð (¨Ð5ˆNô Ÿ9™9 [°)ÈÔNÐÜ�W‰W�T×,Ñ,Ð4D×4JÑ4JÔKˆÜŸ9™9Ð%5°rÓ:ÐàÐ%ä!Ÿv™vÐ&6¸ÓGÐô )Ð0@ÀrÔJˆð Ÿ,™,¨oÈ˜,ÓQˆð Ð Ü Ÿk™k¨/¸9ÓEˆOäŸ9™9 _°kÓBÐÜŸ<™<Ð(8º|ÔLÐô Ÿ:™:Ð-=ÀjÐRTÐVZ×VhÑVhÐEiÔjÐÙ9JÐ# _Ñ5ÐQaÐPcˆà�?Š?Ø Ð 1Ñ1ˆGØˆr?   c                ó  — | j                   ry d| _         t        | dd «      �dt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       t        | dd «      �dt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   ŒíxY w# 1 sw Y   ŒˆxY w# 1 sw Y   y xY w)NTrŠ   rŽ   r\   )rQ   rR   rL   rM   rŠ   r)   rS   r/   r0   rŽ   r\   rT   s     r>   rS   zTFLayoutLMSelfAttention.build5  s9  € Ø�:Š:ØØˆŒ
Ü�4˜ $Ó'Ð3Ü—‘˜tŸz™zŸ™Ó/ñ HØ—
‘
× Ñ  $¨¨d¯k©k×.EÑ.EÐ!FÔG÷Hä�4˜ Ó%Ð1Ü—‘˜tŸx™xŸ}™}Ó-ñ FØ—‘—‘  d¨D¯K©K×,CÑ,CÐDÔE÷Fä�4˜ $Ó'Ð3Ü—‘˜tŸz™zŸ™Ó/ñ HØ—
‘
× Ñ  $¨¨d¯k©k×.EÑ.EÐ!FÔG÷Hð Hð 4÷Hð Hú÷Fð Fú÷Hð Hús$   Á3E*Â<3E6Ä-3FÅ*E3Å6E?ÆFrz   )rœ   r   r¢   r‘   r~   r   ©F)r¯   r   r°   r   r±   r   r²   r   r³   r   r´   úTuple[tf.Tensor]rµ   r}   rf   r}   r~   rÂ   r{   )r€   r�   r‚   r.   r£   ry   rS   r„   r…   s   @r>   r‡   r‡   À   s€   ø„ õó87ð  ðOà ðOð "ðOð ð	Oð
  )ðOð !*ðOð )ðOð  ðOð ðOð 
óO÷bHr?   r‡   c                  ó2   ‡ — e Zd Zdˆ fd„Zddd„Zdd„Zˆ xZS )ÚTFLayoutLMSelfOutputc                óx  •— t        ‰| �  di |¤Ž t        j                  j	                  |j
                  t        |j                  «      d¬«      | _        t        j                  j                  |j                  d¬«      | _        t        j                  j                  |j                  ¬«      | _        || _        y ©NÚdenser‹   r&   r'   r*   r,   ©r-   r.   r   r4   r—   r0   r   r3   rÇ   r5   r6   r&   r7   r8   r9   r/   r:   s      €r>   r.   zTFLayoutLMSelfOutput.__init__F  ó‘   ø€ Ü‰ÑÑ"˜6Ò"ä—\‘\×'Ñ'Ø×$Ñ$¼È×IaÑIaÓ9bÐipð (ó 
ˆŒ
ô Ÿ™×8Ñ8À×AVÑAVÐ]hÐ8ÓiˆŒÜ—|‘|×+Ñ+°×1KÑ1KÐ+ÓLˆŒØˆ�r?   c                óz   — | j                  |¬«      }| j                  ||¬«      }| j                  ||z   ¬«      }|S ©Nrc   re   ©rÇ   r9   r&   ©r;   r¯   Úinput_tensorrf   s       r>   ry   zTFLayoutLMSelfOutput.callP  ó?   € ØŸ
™
¨-˜
Ó8ˆØŸ™¨MÀH˜ÓMˆØŸ™¨m¸lÑ.J˜ÓKˆàÐr?   c                ó"  — | j                   ry d| _         t        | dd «      �dt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY w©NTrÇ   r&   ©
rQ   rR   rL   rM   rÇ   r)   rS   r/   r0   r&   rT   s     r>   rS   zTFLayoutLMSelfOutput.buildW  óÞ   € Ø�:Š:ØØˆŒ
Ü�4˜ $Ó'Ð3Ü—‘˜tŸz™zŸ™Ó/ñ HØ—
‘
× Ñ  $¨¨d¯k©k×.EÑ.EÐ!FÔG÷Hä�4˜ dÓ+Ð7Ü—‘˜tŸ~™~×2Ñ2Ó3ñ LØ—‘×$Ñ$ d¨D°$·+±+×2IÑ2IÐ%JÔK÷Lð Lð 8÷Hð Hú÷Lð Lúó   Á3C9Â<3DÃ9DÄDrz   rÁ   ©r¯   r   rÎ   r   rf   r}   r~   r   r{   ©r€   r�   r‚   r.   ry   rS   r„   r…   s   @r>   rÄ   rÄ   E  ó   ø„ õô÷	Lr?   rÄ   c                  ó\   ‡ — e Zd Zdˆ fd„Zd„ Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Zˆ xZS )	ÚTFLayoutLMAttentionc                ól   •— t        ‰| �  di |¤Ž t        |d¬«      | _        t	        |d¬«      | _        y )Nr;   ©r)   Úoutputr,   )r-   r.   r‡   Úself_attentionrÄ   Údense_outputr:   s      €r>   r.   zTFLayoutLMAttention.__init__e  s1   ø€ Ü‰ÑÑ"˜6Ò"ä5°fÀ6ÔJˆÔÜ0°¸hÔGˆÕr?   c                ó   — t         ‚r{   ©ÚNotImplementedError)r;   Úheadss     r>   Úprune_headszTFLayoutLMAttention.prune_headsk  s   € Ü!Ð!r?   c	           
     óx   — | j                  ||||||||¬«      }	| j                  |	d   ||¬«      }
|
f|	dd  z   }|S )N©r¯   r°   r±   r²   r³   r´   rµ   rf   r   ©r¯   rÎ   rf   r!   )rÝ   rÞ   )r;   rÎ   r°   r±   r²   r³   r´   rµ   rf   Úself_outputsr¾   r¿   s               r>   ry   zTFLayoutLMAttention.calln  so   € ð ×*Ñ*Ø&Ø)ØØ"7Ø#9Ø)Ø/Øð +ó 	
ˆð  ×,Ñ,Ø& q™/¸Èxð -ó 
Ðð $Ð%¨°Q°RÐ(8Ñ8ˆàˆr?   c                óÆ  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   ŒexY w# 1 sw Y   y xY w)NTrÝ   rÞ   )rQ   rR   rL   rM   rÝ   r)   rS   rÞ   rT   s     r>   rS   zTFLayoutLMAttention.build‹  s¾   € Ø�:Š:ØØˆŒ
Ü�4Ð)¨4Ó0Ð<Ü—‘˜t×2Ñ2×7Ñ7Ó8ñ 0Ø×#Ñ#×)Ñ)¨$Ô/÷0ä�4˜¨Ó.Ð:Ü—‘˜t×0Ñ0×5Ñ5Ó6ñ .Ø×!Ñ!×'Ñ'¨Ô-÷.ð .ð ;÷0ð 0ú÷.ð .úó   ÁCÂ%CÃCÃC rz   rÁ   )rÎ   r   r°   r   r±   r   r²   r   r³   r   r´   rÂ   rµ   r}   rf   r}   r~   rÂ   r{   )r€   r�   r‚   r.   rã   ry   rS   r„   r…   s   @r>   rÙ   rÙ   d  su   ø„ õHò"ð ðàðð "ðð ð	ð
  )ðð !*ðð )ðð  ðð ðð 
ó÷:	.r?   rÙ   c                  ó0   ‡ — e Zd Zdˆ fd„Zdd„Zdd„Zˆ xZS )ÚTFLayoutLMIntermediatec                óT  •— t        ‰| �  di |¤Ž t        j                  j	                  |j
                  t        |j                  «      d¬«      | _        t        |j                  t        «      r"t        |j                  «      | _        || _        y |j                  | _        || _        y )NrÇ   r‹   r,   )r-   r.   r   r4   r—   Úintermediate_sizer   r3   rÇ   Ú
isinstanceÚ
hidden_actÚstrr	   Úintermediate_act_fnr/   r:   s      €r>   r.   zTFLayoutLMIntermediate.__init__™  sŒ   ø€ Ü‰ÑÑ"˜6Ò"ä—\‘\×'Ñ'Ø×*Ñ*¼Èv×OgÑOgÓ?hÐovð (ó 
ˆŒ
ô �f×'Ñ'¬Ô-Ü'8¸×9JÑ9JÓ'KˆDÔ$ð ˆ�ð (.×'8Ñ'8ˆDÔ$Øˆ�r?   c                óL   — | j                  |¬«      }| j                  |«      }|S ©Nrc   )rÇ   rñ   ©r;   r¯   s     r>   ry   zTFLayoutLMIntermediate.call¦  s(   € ØŸ
™
¨-˜
Ó8ˆØ×0Ñ0°Ó?ˆàÐr?   c                ó(  — | j                   ry d| _         t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   y xY w©NTrÇ   ©	rQ   rR   rL   rM   rÇ   r)   rS   r/   r0   rT   s     r>   rS   zTFLayoutLMIntermediate.build¬  ó}   € Ø�:Š:ØØˆŒ
Ü�4˜ $Ó'Ð3Ü—‘˜tŸz™zŸ™Ó/ñ HØ—
‘
× Ñ  $¨¨d¯k©k×.EÑ.EÐ!FÔG÷Hð Hð 4÷Hð Húó   Á3BÂBrz   ©r¯   r   r~   r   r{   rÖ   r…   s   @r>   rë   rë   ˜  s   ø„ õó÷Hr?   rë   c                  ó2   ‡ — e Zd Zdˆ fd„Zddd„Zdd„Zˆ xZS )ÚTFLayoutLMOutputc                óx  •— t        ‰| �  di |¤Ž t        j                  j	                  |j
                  t        |j                  «      d¬«      | _        t        j                  j                  |j                  d¬«      | _        t        j                  j                  |j                  ¬«      | _        || _        y rÆ   rÈ   r:   s      €r>   r.   zTFLayoutLMOutput.__init__·  rÉ   r?   c                óz   — | j                  |¬«      }| j                  ||¬«      }| j                  ||z   ¬«      }|S rË   rÌ   rÍ   s       r>   ry   zTFLayoutLMOutput.callÁ  rÏ   r?   c                ó"  — | j                   ry d| _         t        | dd «      �dt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY wrÑ   )rQ   rR   rL   rM   rÇ   r)   rS   r/   rí   r&   r0   rT   s     r>   rS   zTFLayoutLMOutput.buildÈ  sÞ   € Ø�:Š:ØØˆŒ
Ü�4˜ $Ó'Ð3Ü—‘˜tŸz™zŸ™Ó/ñ NØ—
‘
× Ñ  $¨¨d¯k©k×.KÑ.KÐ!LÔM÷Nä�4˜ dÓ+Ð7Ü—‘˜tŸ~™~×2Ñ2Ó3ñ LØ—‘×$Ñ$ d¨D°$·+±+×2IÑ2IÐ%JÔK÷Lð Lð 8÷Nð Nú÷Lð LúrÔ   rz   rÁ   rÕ   r{   rÖ   r…   s   @r>   rü   rü   ¶  r×   r?   rü   c                  óV   ‡ — e Zd Zdˆ fd„Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Zˆ xZS )ÚTFLayoutLMLayerc                óD  •— t        ‰| �  di |¤Ž t        |d¬«      | _        |j                  | _        |j
                  | _        | j
                  r,| j                  st        | › d�«      ‚t        |d¬«      | _        t        |d¬«      | _	        t        |d¬«      | _        y )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_outputr:   s      €r>   r.   zTFLayoutLMLayer.__init__Ö  s�   ø€ Ü‰ÑÑ"˜6Ò"ä,¨V¸+ÔFˆŒØ ×+Ñ+ˆŒØ#)×#=Ñ#=ˆÔ Ø×#Ò#Ø—?’?Ü  D 6Ð)gÐ!hÓiÐiÜ"5°fÐCSÔ"TˆDÔÜ2°6ÀÔOˆÔÜ+¨F¸ÔBˆÕr?   c	           
     óÐ  — |�|d d nd }	| j                  |||d d |	||¬«      }
|
d   }| j                  r|
dd }|
d   }n|
dd  }d }| j                  rV|�Tt        | d«      st        d| › d�«      ‚|�|d	d  nd }| j	                  ||||||||¬«      }|d   }||dd z   }|d   }|z   }| j                  |¬
«      }| j                  |||¬«      }|f|z   }| j                  r|fz   }|S )Nrb   )rÎ   r°   r±   r²   r³   r´   rµ   rf   r   r!   rY   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  )r;   r¯   r°   r±   r²   r³   r´   rµ   rf   Ú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ÐY]Ð Ø!%§¡Ø&Ø)ØØ"&Ø#'Ø3Ø/Øð "0ó 	"
Ðð 2°!Ñ4Ðð �?Š?Ø,¨Q¨rÐ2ˆGØ 6°rÑ :Ñà,¨Q¨RÐ0ˆGà'+Ð$Ø�?Š?Ð4Ð@Ü˜4Ð!1Ô2Ü Ø=¸d¸Vð DDð Dóð ð @NÐ?Y¨°r°sÑ(;Ð_cÐ%Ø&*×&9Ñ&9Ø-Ø-Ø#Ø&;Ø'=Ø8Ø"3Ø!ð ':ó 	'Ð#ð  7°qÑ9ÐØÐ 7¸¸"Ð =Ñ=ˆGð ,CÀ2Ñ+FÐ(Ø 1Ð4PÑ PÐà"×/Ñ/Ð>NÐ/ÓOÐØ×'Ñ'Ø-Ð<LÐW_ð (ó 
ˆð  �/ GÑ+ˆð �?Š?ØÐ!2Ð 4Ñ4ˆGàˆr?   c                ó`  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   �ŒxY w# 1 sw Y   ŒÌxY w# 1 sw Y   Œ~xY w# 1 sw Y   y xY w)NTr  r  r  r  )
rQ   rR   rL   rM   r  r)   rS   r  r  r  rT   s     r>   rS   zTFLayoutLMLayer.build*  sZ  € Ø�:Š:ØØˆŒ
Ü�4˜ dÓ+Ð7Ü—‘˜tŸ~™~×2Ñ2Ó3ñ +Ø—‘×$Ñ$ TÔ*÷+ä�4˜¨Ó.Ð:Ü—‘˜t×0Ñ0×5Ñ5Ó6ñ .Ø×!Ñ!×'Ñ'¨Ô-÷.ä�4˜¨Ó-Ð9Ü—‘˜t×/Ñ/×4Ñ4Ó5ñ -Ø× Ñ ×&Ñ& tÔ,÷-ä�4Ð)¨4Ó0Ð<Ü—‘˜t×2Ñ2×7Ñ7Ó8ñ 0Ø×#Ñ#×)Ñ)¨$Ô/÷0ð 0ð =÷+ñ +ú÷.ð .ú÷-ð -ú÷0ð 0ús0   ÁE?Â%FÃ?FÅF$Å?F	ÆFÆF!Æ$F-rz   rÁ   )r¯   r   r°   r   r±   r   r²   útf.Tensor | Noner³   r  r´   zTuple[tf.Tensor] | Nonerµ   r}   rf   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0r?   r  c                  ób   ‡ — e Zd Zdˆ fd„Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Zˆ xZS )ÚTFLayoutLMEncoderc                ó¨   •— t        ‰| �  di |¤Ž || _        t        |j                  «      D �cg c]  }t        |d|› �¬«      ‘Œ c}| _        y c c}w )Nzlayer_._rÛ   r,   )r-   r.   r/   rj   Únum_hidden_layersr  Úlayer)r;   r/   r<   Úir=   s       €r>   r.   zTFLayoutLMEncoder.__init__>  sH   ø€ Ü‰ÑÑ"˜6Ò"ØˆŒÜLQÐRX×RjÑRjÓLkÖlÀq”o f°X¸a¸S°>ÖBÒlˆ�
ùÒls   ¯Ac                ó¾  — |	rdnd }|rdnd }|r| j                   j                  rdnd }|rdnd }t        | j                  «      D ]h  \  }}|	r||fz   }|�||   nd } |||||   |||||¬«      }|d   }|r	||d   fz  }|sŒ=||d   fz   }| j                   j                  sŒ]|€Œ`||d   fz   }Œj |	r||fz   }|
st	        d„ ||||fD «       «      S t        |||||¬«      S )	Nr,   rå   r   rY   r!   rb   c              3  ó&   K  — | ]	  }|€Œ|–— Œ y ­wr{   r,   )Ú.0Úvs     r>   ú	<genexpr>z)TFLayoutLMEncoder.call.<locals>.<genexpr>u  s   è ø€ ò ØÐghÑgt”ñùs   ‚Š)Úlast_hidden_stateÚpast_key_valuesr¯   Ú
attentionsÚcross_attentions)r/   r  Ú	enumerater  Útupler
   )r;   r¯   r°   r±   r²   r³   r"  Ú	use_cacherµ   Úoutput_hidden_statesÚreturn_dictrf   Úall_hidden_statesÚall_attentionsÚall_cross_attentionsÚnext_decoder_cacher  Úlayer_moduler´   Úlayer_outputss                       r>   ry   zTFLayoutLMEncoder.callC  sV  € ñ #7™B¸DÐÙ0™°dˆÙ%6¸4¿;¹;×;ZÒ;Z™rÐ`dÐá#,™R°$ÐÜ(¨¯©Ó4ò 	V‰OˆAˆ|Ù#Ø$5¸Ð8HÑ$HÐ!à3BÐ3N˜_¨QÒ/ÐTXˆNá(Ø+Ø-Ø# A™,Ø&;Ø'=Ø-Ø"3Ø!ô	ˆMð *¨!Ñ,ˆMáØ" }°RÑ'8Ð&:Ñ:Ð"â Ø!/°=ÀÑ3CÐ2EÑ!E�Ø—;‘;×2Ó2Ð7LÑ7XØ+?À=ÐQRÑCSÐBUÑ+UÑ(ð1	Vñ6  Ø 1°]Ð4DÑ DÐáÜñ Ø)Ð+<¸nÐNbÐcôó ð ô ;Ø+Ø.Ø+Ø%Ø1ô
ð 	
r?   c                óô   — | j                   ry d| _         t        | dd «      �K| j                  D ];  }t        j                  |j
                  «      5  |j                  d «       d d d «       Œ= y y # 1 sw Y   ŒIxY w)NTr  )rQ   rR   r  rL   rM   r)   rS   )r;   rU   r  s      r>   rS   zTFLayoutLMEncoder.build�  sp   € Ø�:Š:ØØˆŒ
Ü�4˜ $Ó'Ð3ØŸ™ò &�Ü—]‘] 5§:¡:Ó.ñ &Ø—K‘K Ô%÷&ð &ñ&ð 4÷&ð &ús   ÁA.Á.A7	rz   rÁ   )r¯   r   r°   r   r±   r   r²   r  r³   r  r"  zTuple[Tuple[tf.Tensor]] | Noner'  úOptional[bool]rµ   r}   r(  r}   r)  r}   rf   r}   r~   zDUnion[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]r{   rÖ   r…   s   @r>   r  r  =  s�   ø„ õmð" ð<
à ð<
ð "ð<
ð ð	<
ð
  0ð<
ð !1ð<
ð 8ð<
ð "ð<
ð  ð<
ð #ð<
ð ð<
ð ð<
ð 
Nó<
÷|&r?   r  c                  ó0   ‡ — e Zd Zdˆ fd„Zdd„Zdd„Zˆ xZS )ÚTFLayoutLMPoolerc                ó¼   •— t        ‰| �  di |¤Ž t        j                  j	                  |j
                  t        |j                  «      dd¬«      | _        || _	        y )NÚtanhrÇ   )rŒ   r�   Ú
activationr)   r,   )
r-   r.   r   r4   r—   r0   r   r3   rÇ   r/   r:   s      €r>   r.   zTFLayoutLMPooler.__init__�  sT   ø€ Ü‰ÑÑ"˜6Ò"ä—\‘\×'Ñ'Ø×$Ñ$Ü.¨v×/GÑ/GÓHØØð	 (ó 
ˆŒ
ð ˆ�r?   c                ó<   — |d d …df   }| j                  |¬«      }|S )Nr   rc   )rÇ   )r;   r¯   Úfirst_token_tensorÚpooled_outputs       r>   ry   zTFLayoutLMPooler.call˜  s*   € ð +ª1¨a¨4Ñ0ÐØŸ
™
Ð*<˜
Ó=ˆàÐr?   c                ó(  — | j                   ry d| _         t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   y xY wrö   r÷   rT   s     r>   rS   zTFLayoutLMPooler.build   rø   rù   rz   rú   r{   rÖ   r…   s   @r>   r3  r3  Œ  s   ø„ õ	ó÷Hr?   r3  c                  ó0   ‡ — e Zd Zdˆ fd„Zdd„Zdd„Zˆ xZS )Ú!TFLayoutLMPredictionHeadTransformc                ó¦  •— t        ‰| �  di |¤Ž t        j                  j	                  |j
                  t        |j                  «      d¬«      | _        t        |j                  t        «      rt        |j                  «      | _        n|j                  | _        t        j                  j                  |j                  d¬«      | _        || _        y )NrÇ   r‹   r&   r'   r,   )r-   r.   r   r4   r—   r0   r   r3   rÇ   rî   rï   rð   r	   Útransform_act_fnr5   r6   r&   r/   r:   s      €r>   r.   z*TFLayoutLMPredictionHeadTransform.__init__«  s¤   ø€ Ü‰ÑÑ"˜6Ò"ä—\‘\×'Ñ'Ø×$Ñ$Ü.¨v×/GÑ/GÓHØð (ó 
ˆŒ
ô �f×'Ñ'¬Ô-Ü$5°f×6GÑ6GÓ$HˆDÕ!à$*×$5Ñ$5ˆDÔ!äŸ™×8Ñ8À×AVÑAVÐ]hÐ8ÓiˆŒØˆ�r?   c                óp   — | j                  |¬«      }| j                  |«      }| j                  |¬«      }|S ró   )rÇ   r>  r&   rô   s     r>   ry   z&TFLayoutLMPredictionHeadTransform.call¼  s8   € ØŸ
™
¨-˜
Ó8ˆØ×-Ñ-¨mÓ<ˆØŸ™¨m˜Ó<ˆàÐr?   c                ó"  — | j                   ry d| _         t        | dd «      �dt        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY wrÑ   rÒ   rT   s     r>   rS   z'TFLayoutLMPredictionHeadTransform.buildÃ  rÓ   rÔ   rz   rú   r{   rÖ   r…   s   @r>   r<  r<  ª  s   ø„ õó"÷	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 )ÚTFLayoutLMLMPredictionHeadc                ó†   •— t        ‰| �  di |¤Ž || _        |j                  | _        t	        |d¬«      | _        || _        y )NÚ	transformrÛ   r,   )r-   r.   r/   r0   r<  rD  Úinput_embeddings©r;   r/   rE  r<   r=   s       €r>   r.   z#TFLayoutLMLMPredictionHead.__init__Ñ  s@   ø€ Ü‰ÑÑ"˜6Ò"àˆŒØ!×-Ñ-ˆÔä:¸6ÈÔTˆŒð !1ˆÕr?   c                óX  — | j                  | j                  j                  fddd¬«      | _        | j                  ry d| _        t        | dd «      �Nt        j                  | j                  j                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   y xY w)NÚzerosTÚbias)rC   rD   Ú	trainabler)   rD  )rN   r/   rO   rI  rQ   rR   rL   rM   rD  r)   rS   rT   s     r>   rS   z TFLayoutLMLMPredictionHead.buildÝ  s‘   € Ø—O‘O¨4¯;©;×+AÑ+AÐ*CÐQXÐdhÐou�OÓvˆŒ	à�:Š:ØØˆŒ
Ü�4˜ dÓ+Ð7Ü—‘˜tŸ~™~×2Ñ2Ó3ñ +Ø—‘×$Ñ$ TÔ*÷+ð +ð 8÷+ð +ús   Á:B Â B)c                ó   — | j                   S r{   )rE  ©r;   s    r>   Úget_output_embeddingsz0TFLayoutLMLMPredictionHead.get_output_embeddingsç  s   € Ø×$Ñ$Ð$r?   c                ó`   — || j                   _        t        |«      d   | j                   _        y ©Nr   )rE  rB   r   rO   ©r;   r\   s     r>   Úset_output_embeddingsz0TFLayoutLMLMPredictionHead.set_output_embeddingsê  s(   € Ø',ˆ×ÑÔ$Ü+5°eÓ+<¸QÑ+?ˆ×ÑÕ(r?   c                ó   — d| j                   iS )NrI  )rI  rL  s    r>   Úget_biasz#TFLayoutLMLMPredictionHead.get_biasî  s   € Ø˜Ÿ	™	Ð"Ð"r?   c                óX   — |d   | _         t        |d   «      d   | j                  _        y )NrI  r   )rI  r   r/   rO   rP  s     r>   Úset_biasz#TFLayoutLMLMPredictionHead.set_biasñ  s'   € Ø˜&‘MˆŒ	Ü!+¨E°&©MÓ!:¸1Ñ!=ˆ�‰Õr?   c                ó–  — | j                  |¬«      }t        |«      d   }t        j                  |d| j                  g¬«      }t        j
                  || j                  j                  d¬«      }t        j                  |d|| j                  j                  g¬«      }t        j                  j                  || j                  ¬«      }|S )Nr
  r!   rY   r›   T)ÚaÚbr¥   )r\   rI  )rD  r   rL   r    r0   rª   rE  rB   r/   rO   ÚnnÚbias_addrI  )r;   r¯   Ú
seq_lengths      r>   ry   zTFLayoutLMLMPredictionHead.callõ  sŸ   € ØŸ™°]˜ÓCˆÜ Ó.¨qÑ1ˆ
ÜŸ
™
¨-ÀÀD×DTÑDTÐ?UÔVˆÜŸ	™	 M°T×5JÑ5J×5QÑ5QÐ_cÔdˆÜŸ
™
¨-ÀÀJÐPT×P[ÑP[×PfÑPfÐ?gÔhˆÜŸ™Ÿ™¨]ÀÇÁ˜ÓKˆàÐr?   ©r/   r"   rE  úkeras.layers.Layerr{   ©r~   r]  ©r\   ztf.Variable)r~   zDict[str, tf.Variable]rú   )r€   r�   r‚   r.   rS   rM  rQ  rS  rU  ry   r„   r…   s   @r>   rB  rB  Ð  s'   ø„ õ
1ó+ó%ó@ó#ó>÷r?   rB  c                  ó0   ‡ — e Zd Zdˆ fd„Zdd„Zdd„Zˆ xZS )ÚTFLayoutLMMLMHeadc                óJ   •— t        ‰| �  di |¤Ž t        ||d¬«      | _        y )NÚpredictionsrÛ   r,   )r-   r.   rB  rc  rF  s       €r>   r.   zTFLayoutLMMLMHead.__init__  s&   ø€ Ü‰ÑÑ"˜6Ò"ä5°fÐ>NÐUbÔcˆÕr?   c                ó*   — | j                  |¬«      }|S )Nr
  )rc  )r;   Úsequence_outputÚprediction_scoress      r>   ry   zTFLayoutLMMLMHead.call  s   € Ø ×,Ñ,¸?Ð,ÓKÐà Ð r?   c                óú   — | j                   ry d| _         t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   y xY w)NTrc  )rQ   rR   rL   rM   rc  r)   rS   rT   s     r>   rS   zTFLayoutLMMLMHead.build  sm   € Ø�:Š:ØØˆŒ
Ü�4˜¨Ó-Ð9Ü—‘˜t×/Ñ/×4Ñ4Ó5ñ -Ø× Ñ ×&Ñ& tÔ,÷-ð -ð :÷-ð -úó   ÁA1Á1A:r\  )re  r   r~   r   r{   rÖ   r…   s   @r>   ra  ra    s   ø„ õdó
!÷
-r?   ra  c                  ó¨   ‡ — 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 )ÚTFLayoutLMMainLayerc                ó²   •— t        ‰| �  di |¤Ž || _        t        |d¬«      | _        t        |d¬«      | _        |rt        |d¬«      | _        y d | _        y )NrF   rÛ   ÚencoderÚpoolerr,   )	r-   r.   r/   r$   rF   r  rl  r3  rm  )r;   r/   Úadd_pooling_layerr<   r=   s       €r>   r.   zTFLayoutLMMainLayer.__init__  sN   ø€ Ü‰ÑÑ"˜6Ò"àˆŒä.¨v¸LÔIˆŒÜ(¨°iÔ@ˆŒÙARÔ& v°HÔ=ˆ�ÐX\ˆ�r?   c                ó   — | j                   S r{   )rF   rL  s    r>   Úget_input_embeddingsz(TFLayoutLMMainLayer.get_input_embeddings"  s   € Ø�‰Ðr?   c                ó`   — || j                   _        t        |«      d   | j                   _        y rO  )rF   rB   r   rO   rP  s     r>   Úset_input_embeddingsz(TFLayoutLMMainLayer.set_input_embeddings%  s$   € Ø!&ˆ�‰ÔÜ%/°Ó%6°qÑ%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à   )r;   Úheads_to_prunes     r>   Ú_prune_headsz TFLayoutLMMainLayer._prune_heads)  s
   € ô
 "Ð!r?   c                óÔ  — |�|�t        d«      ‚|�t        |«      }n|�t        |«      d d }nt        d«      ‚|€t        j                  |d¬«      }|€t        j                  |d¬«      }|€t        j                  |dgz   d¬«      }| j	                  ||||||¬«      }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  }| j                  ||||d d d|
|||¬«      }|d   }| j                  �| 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 timerY   z5You have to specify either input_ids or inputs_embedsr!   rZ   r   ra   )rl   rm   rn   ro   rp   rf   r¦   g      ð?g     ˆÃÀF)r¯   r°   r±   r²   r³   r"  r'  rµ   r(  r)  rf   r
  )r!  Úpooler_outputr¯   r#  r$  )r�   r   rL   rh   rF   r    r«   r§   Úconstantr®   Úsubtractrá   r/   r  rl  rm  r   r¯   r#  r$  )r;   rl   rm   r°   ro   rn   r±   rp   r²   r³   rµ   r(  r)  rf   rU   Úembedding_outputÚextended_attention_maskÚone_cstÚten_thousand_cstÚencoder_outputsre  r9  s                         r>   ry   zTFLayoutLMMainLayer.call0  s   € ð" Ð  ]Ð%>ÜÐcÓdÐdØÐ"Ü$ YÓ/‰KØÐ&Ü$ ]Ó3°C°RÐ8‰KäÐTÓUÐUàÐ!ÜŸW™W¨+¸QÔ?ˆNàÐ!ÜŸW™W¨+¸QÔ?ˆNØˆ<Ü—7‘7 ¨q¨cÑ 1¸Ô;ˆDàŸ?™?ØØØ%Ø)Ø'Øð +ó 
Ðô #%§*¡*¨^¸kÈ!¹nÈaÐQRÐT_Ð`aÑTbÐ=cÓ"dÐô #%§'¡'Ð*AÐIY×I_ÑI_Ô"`ÐÜ—+‘+˜cÐ)9×)?Ñ)?Ô@ˆÜŸ;™; xÐ7G×7MÑ7MÔNÐÜ"$§+¡+¬b¯k©k¸'ÐCZÓ.[Ð]mÓ"nÐð Ð Ü%Ð%à˜ §¡×!>Ñ!>Ñ>ˆIàŸ,™,Ø*Ø2Øà"7Ø#'Ø ØØ/Ø!5Ø#Øð 'ó 
ˆð *¨!Ñ,ˆØFJÇkÁkÐF]˜Ÿ™°/˜ÔBÐcgˆáàØðð    Ð#ñ$ð $ô
 >Ø-Ø'Ø)×7Ñ7Ø&×1Ñ1Ø,×=Ñ=ô
ð 	
r?   c                ó’  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   Œ¿xY w# 1 sw Y   ŒqxY w# 1 sw Y   y xY w)NTrF   rl  rm  )	rQ   rR   rL   rM   rF   r)   rS   rl  rm  rT   s     r>   rS   zTFLayoutLMMainLayer.build–  s  € Ø�:Š:ØØˆŒ
Ü�4˜ tÓ,Ð8Ü—‘˜tŸ™×3Ñ3Ó4ñ ,Ø—‘×%Ñ% dÔ+÷,ä�4˜ DÓ)Ð5Ü—‘˜tŸ|™|×0Ñ0Ó1ñ )Ø—‘×"Ñ" 4Ô(÷)ä�4˜ 4Ó(Ð4Ü—‘˜tŸ{™{×/Ñ/Ó0ñ (Ø—‘×!Ñ! $Ô'÷(ð (ð 5÷,ð ,ú÷)ð )ú÷(ð (ús$   ÁD%Â%D1Ã?D=Ä%D.Ä1D:Ä=E)T)r/   r"   rn  r}   r^  r_  ©NNNNNNNNNNNNF)rl   úTFModelInputType | Nonerm   únp.ndarray | tf.Tensor | Noner°   r‚  ro   r‚  rn   r‚  r±   r‚  rp   r‚  r²   r‚  r³   r‚  rµ   r1  r(  r1  r)  r1  rf   r}   r~   úGUnion[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]r{   )r€   r�   r‚   r"   Úconfig_classr.   rp  rr  ru  r   ry   rS   r„   r…   s   @r>   rj  rj    sû   ø„ à!€Lö]óó:ò"ð ð .2Ø.2Ø8<Ø8<Ø6:Ø37Ø7;Ø?CØ@DØ,0Ø/3Ø&*Øðc
à*ðc
ð ,ðc
ð 6ð	c
ð
 6ðc
ð 4ðc
ð 1ðc
ð 5ðc
ð  =ðc
ð !>ðc
ð *ðc
ð -ðc
ð $ðc
ð ðc
ð 
Qòc
ó ðc
÷J(r?   rj  c                  ó4   ‡ — 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                ón   •— t         ‰| �  }t        j                  dt        j                  d¬«      |d<   |S )N)NNra   rm   )rC   r§   r)   )r-   Úinput_signaturerL   Ú
TensorSpecÚint32)r;   Ú	signaturer=   s     €r>   r‰  z)TFLayoutLMPreTrainedModel.input_signature®  s/   ø€ ä‘GÑ+ˆ	ÜŸM™M°ÄrÇxÁxÐV\Ô]ˆ	�&ÑØÐr?   )
r€   r�   r‚   rƒ   r"   r„  Úbase_model_prefixÚpropertyr‰  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                  óÚ   ‡ — e Zd Zdˆ fd„Ze eej                  d«      «       ee	e
¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       «       «       Zd	d„Zˆ xZS )
ÚTFLayoutLMModelc                óP   •— t        ‰| �  |g|¢­i |¤Ž t        |d¬«      | _        y )Nr‡  rÛ   )r-   r.   rj  r‡  ©r;   r/   rd   r<   r=   s       €r>   r.   zTFLayoutLMModel.__init__  s(   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3ä+¨F¸ÔDˆ�r?   úbatch_size, sequence_length©Úoutput_typer„  c                ó>   — | j                  ||||||||
|||¬«      }|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
        ```©rl   rm   r°   ro   rn   r±   rp   rµ   r(  r)  rf   )r‡  )r;   rl   rm   r°   ro   rn   r±   rp   r²   r³   rµ   r(  r)  rf   r¿   s                  r>   ry   zTFLayoutLMModel.call!  s@   € ðn —-‘-ØØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#Øð  ó 
ˆð ˆr?   c                óú   — | j                   ry d| _         t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   y xY w)NTr‡  )rQ   rR   rL   rM   r‡  r)   rS   rT   s     r>   rS   zTFLayoutLMModel.buildh  si   € Ø�:Š:ØØˆŒ
Ü�4˜ TÓ*Ð6Ü—‘˜tŸ}™}×1Ñ1Ó2ñ *Ø—‘×#Ñ# DÔ)÷*ð *ð 7÷*ð *úrh  rz   r€  )rl   r�  rm   r‚  r°   r‚  ro   r‚  rn   r‚  r±   r‚  rp   r‚  r²   r‚  r³   r‚  rµ   r1  r(  r1  r)  r1  rf   r1  r~   rƒ  r{   )r€   r�   r‚   r.   r   r   ÚLAYOUTLM_INPUTS_DOCSTRINGÚformatr    r   Ú_CONFIG_FOR_DOCry   rS   r„   r…   s   @r>   r�  r�    s  ø„ õ
Eð
 Ù*Ð+D×+KÑ+KÐLiÓ+jÓkÙØBÐQ`ôð
 .2Ø.2Ø8<Ø8<Ø6:Ø37Ø7;Ø?CØ@DØ,0Ø/3Ø&*Ø#(ð@à*ð@ð ,ð@ð 6ð	@ð
 6ð@ð 4ð@ð 1ð@ð 5ð@ð  =ð@ð !>ð@ð *ð@ð -ð@ð $ð@ð !ð@ð 
Qò@óó ló ð
@÷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	j                  d«      «       eee¬«      	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       «       «       Zdd„Zˆ xZS )ÚTFLayoutLMForMaskedLM)rm  úcls.seq_relationshipzcls.predictions.decoder.weightÚ	nsp___clsc                óâ   •— t        ‰| �  |g|¢­i |¤Ž |j                  rt        j	                  d«       t        |dd¬«      | _        t        || j                  j                  d¬«      | _	        y )NzqIf you want to use `TFLayoutLMForMaskedLM` make sure `config.is_decoder=False` for bi-directional self-attention.Tr‡  ©rn  r)   Ú	mlm___cls)rE  r)   )
r-   r.   r™   ÚloggerÚwarningrj  r‡  ra  rF   Úmlmr’  s       €r>   r.   zTFLayoutLMForMaskedLM.__init__{  sb   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3à×ÒÜ�N‰Nð1ôô
 ,¨FÀdÐQ[Ô\ˆŒÜ$ V¸d¿m¹m×>VÑ>VÐ]hÔiˆ�r?   c                ó.   — | j                   j                  S r{   )r¥  rc  rL  s    r>   Úget_lm_headz!TFLayoutLMForMaskedLM.get_lm_head‡  s   € Ø�x‰x×#Ñ#Ð#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¥  rc  rL  s    r>   Úget_prefix_bias_namez*TFLayoutLMForMaskedLM.get_prefix_bias_nameŠ  sG   € Ü�‰ÐeÔgtÔuØ�y‰y˜3‰ §¡§¡Ñ.°Ñ4°t·x±x×7KÑ7K×7PÑ7PÑPÐPr?   r“  r”  c                ó  — | j                  |||||||||	|
|¬«      }|d   }| j                  ||¬«      }|€dn| j                  ||¬«      }|
s|f|dd z   }|�|f|z   S |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   )re  rf   N©Úlabelsr¨   rb   ©Úlossr¨   r¯   r#  )r‡  r¥  Úhf_compute_lossr   r¯   r#  )r;   rl   rm   r°   ro   rn   r±   rp   rµ   r(  r)  r°  rf   r¿   re  rf  r²  rÜ   s                     r>   ry   zTFLayoutLMForMaskedLM.callŽ  sÅ   € ð~ —-‘-ØØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#Øð  ó 
ˆð " !™*ˆØ ŸH™H°_Èx˜HÓXÐØ�~‰t¨4×+?Ñ+?ÀvÐVgÐ+?Ó+hˆáØ'Ð)¨G°A°B¨KÑ7ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEäØØ$Ø!×/Ñ/Ø×)Ñ)ô	
ð 	
r?   c                óÆ  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �Nt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       y y # 1 sw Y   ŒexY w# 1 sw Y   y xY w)NTr‡  r¥  )rQ   rR   rL   rM   r‡  r)   rS   r¥  rT   s     r>   rS   zTFLayoutLMForMaskedLM.buildé  s±   € Ø�:Š:ØØˆŒ
Ü�4˜ TÓ*Ð6Ü—‘˜tŸ}™}×1Ñ1Ó2ñ *Ø—‘×#Ñ# DÔ)÷*ä�4˜ Ó%Ð1Ü—‘˜tŸx™xŸ}™}Ó-ñ %Ø—‘—‘˜tÔ$÷%ð %ð 2÷*ð *ú÷%ð %úré   rz   r^  )r~   rð   ©NNNNNNNNNNNF)rl   r�  rm   r‚  r°   r‚  ro   r‚  rn   r‚  r±   r‚  rp   r‚  rµ   r1  r(  r1  r)  r1  r°  r‚  rf   r1  r~   z)Union[TFMaskedLMOutput, Tuple[tf.Tensor]]r{   )r€   r�   r‚   Ú"_keys_to_ignore_on_load_unexpectedr.   r§  r­  r   r   r™  rš  r    r   r›  ry   rS   r„   r…   s   @r>   r�  r�  q  s  ø„ ò*Ð&õ
jó$óQð Ù*Ð+D×+KÑ+KÐLiÓ+jÓkÙÐ+;È/ÔZð .2Ø.2Ø8<Ø8<Ø6:Ø37Ø7;Ø,0Ø/3Ø&*Ø04Ø#(ðV
à*ðV
ð ,ðV
ð 6ð	V
ð
 6ðV
ð 4ðV
ð 1ðV
ð 5ðV
ð *ðV
ð -ðV
ð $ðV
ð .ðV
ð !ðV
ð 
3ò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                  óâ   ‡ — e Zd Zg d¢ZdgZdˆ fd„Ze eej                  d«      «       e
ee¬«      	 	 	 	 	 	 	 	 	 	 	 	 d		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
d„«       «       «       Zdd„Zˆ xZS )Ú#TFLayoutLMForSequenceClassification)r¢  rŸ  úcls.predictionsrž  r9   c                óf  •— t        ‰| �  |g|¢­i |¤Ž |j                  | _        t        |d¬«      | _        t
        j                  j                  |j                  ¬«      | _	        t
        j                  j                  |j                  t        |j                  «      d¬«      | _        || _        y )Nr‡  rÛ   r*   Ú
classifierr‹   ©r-   r.   Ú
num_labelsrj  r‡  r   r4   r7   r8   r9   r—   r   r3   r»  r/   r’  s       €r>   r.   z,TFLayoutLMForSequenceClassification.__init__  s’   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3à ×+Ñ+ˆŒä+¨F¸ÔDˆŒÜ—|‘|×+Ñ+°×1KÑ1KÐ+ÓLˆŒÜŸ,™,×,Ñ,Ø×#Ñ#Ü.¨v×/GÑ/GÓHØð -ó 
ˆŒð
 ˆ�r?   r“  r”  c                ó,  — | j                  |||||||||	|
|¬«      }|d   }| j                  ||¬«      }| j                  |¬«      }|€dn| j                  ||¬«      }|
s|f|dd z   }|�|f|z   S |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!   re   rc   Nr¯  rb   r±  )r‡  r9   r»  r³  r   r¯   r#  )r;   rl   rm   r°   ro   rn   r±   rp   rµ   r(  r)  r°  rf   r¿   r9  r¨   r²  rÜ   s                     r>   ry   z(TFLayoutLMForSequenceClassification.call  sÓ   € ð~ —-‘-ØØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#Øð  ó 
ˆð   ™
ˆØŸ™¨MÀH˜ÓMˆØ—‘¨�Ó6ˆØ�~‰t¨4×+?Ñ+?ÀvÐV\Ð+?Ó+]ˆáØ�Y ¨¨ Ñ,ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä)ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
r?   c                óô  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY w©NTr‡  r»  ©
rQ   rR   rL   rM   r‡  r)   rS   r»  r/   r0   rT   s     r>   rS   z)TFLayoutLMForSequenceClassification.buildk  óË   € Ø�:Š:ØØˆŒ
Ü�4˜ TÓ*Ð6Ü—‘˜tŸ}™}×1Ñ1Ó2ñ *Ø—‘×#Ñ# DÔ)÷*ä�4˜ tÓ,Ð8Ü—‘˜tŸ™×3Ñ3Ó4ñ MØ—‘×%Ñ% t¨T°4·;±;×3JÑ3JÐ&KÔL÷Mð Mð 9÷*ð *ú÷Mð Múó   ÁC"Â%3C.Ã"C+Ã.C7rz   rµ  )rl   r�  rm   r‚  r°   r‚  ro   r‚  rn   r‚  r±   r‚  rp   r‚  rµ   r1  r(  r1  r)  r1  r°  r‚  rf   r1  r~   z3Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]r{   )r€   r�   r‚   r¶  Ú_keys_to_ignore_on_load_missingr.   r   r   r™  rš  r    r   r›  ry   rS   r„   r…   s   @r>   r¸  r¸  õ  s  ø„ ò *sÐ&Ø'1 lÐ#õð Ù*Ð+D×+KÑ+KÐLiÓ+jÓkÙÐ+EÐTcÔdð .2Ø.2Ø8<Ø8<Ø6:Ø37Ø7;Ø,0Ø/3Ø&*Ø04Ø#(ðW
à*ðW
ð ,ðW
ð 6ð	W
ð
 6ðW
ð 4ðW
ð 1ðW
ð 5ðW
ð *ðW
ð -ðW
ð $ðW
ð .ðW
ð !ðW
ð 
=òW
ó eó ló ðW
÷r	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j                  d«      «       e
ee¬«      	 	 	 	 	 	 	 	 	 	 	 	 d		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
d„«       «       «       Zdd„Zˆ xZS )Ú TFLayoutLMForTokenClassification©rm  r¢  rŸ  r¹  rž  r9   c                óh  •— t        ‰| �  |g|¢­i |¤Ž |j                  | _        t        |dd¬«      | _        t
        j                  j                  |j                  ¬«      | _	        t
        j                  j                  |j                  t        |j                  «      d¬«      | _        || _        y )NTr‡  r¡  r*   r»  r‹   r¼  r’  s       €r>   r.   z)TFLayoutLMForTokenClassification.__init__‰  s•   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3à ×+Ñ+ˆŒä+¨FÀdÐQ[Ô\ˆŒÜ—|‘|×+Ñ+°×1KÑ1KÐ+ÓLˆŒÜŸ,™,×,Ñ,Ø×#Ñ#Ü.¨v×/GÑ/GÓHØð -ó 
ˆŒð
 ˆ�r?   r“  r”  c                ó,  — | j                  |||||||||	|
|¬«      }|d   }| j                  ||¬«      }| j                  |¬«      }|€dn| j                  ||¬«      }|
s|f|dd z   }|�|f|z   S |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   re   rc   Nr¯  rb   r±  )r‡  r9   r»  r³  r   r¯   r#  )r;   rl   rm   r°   ro   rn   r±   rp   rµ   r(  r)  r°  rf   r¿   re  r¨   r²  rÜ   s                     r>   ry   z%TFLayoutLMForTokenClassification.call—  sÓ   € ðz —-‘-ØØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#Øð  ó 
ˆð " !™*ˆØŸ,™,¨oÈ˜,ÓQˆØ—‘¨�Ó8ˆØ�~‰t¨4×+?Ñ+?ÀvÐV\Ð+?Ó+]ˆáØ�Y ¨¨ Ñ,ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä&ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
r?   c                óô  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY wrÀ  rÁ  rT   s     r>   rS   z&TFLayoutLMForTokenClassification.buildñ  rÂ  rÃ  rz   rµ  )rl   r�  rm   r‚  r°   r‚  ro   r‚  rn   r‚  r±   r‚  rp   r‚  rµ   r1  r(  r1  r)  r1  r°  r‚  rf   r1  r~   z0Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]r{   )r€   r�   r‚   r¶  rÄ  r.   r   r   r™  rš  r    r   r›  ry   rS   r„   r…   s   @r>   rÆ  rÆ  w  s  ø„ ò*Ð&ð (2 lÐ#õð Ù*Ð+D×+KÑ+KÐLiÓ+jÓkÙÐ+BÐQ`Ôað .2Ø.2Ø8<Ø8<Ø6:Ø37Ø7;Ø,0Ø/3Ø&*Ø04Ø#(ðU
à*ðU
ð ,ðU
ð 6ð	U
ð
 6ðU
ð 4ðU
ð 1ðU
ð 5ðU
ð *ðU
ð -ðU
ð $ðU
ð .ðU
ð !ðU
ð 
:òU
ó bó ló ðU
÷n	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j                  d«      «       e	e
e¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	d„«       «       «       Zd
d„Zˆ xZS )ÚTFLayoutLMForQuestionAnsweringrÇ  c                ó
  •— t        ‰| �  |g|¢­i |¤Ž |j                  | _        t        |dd¬«      | _        t
        j                  j                  |j                  t        |j                  «      d¬«      | _
        || _        y )NTr‡  r¡  Ú
qa_outputsr‹   )r-   r.   r½  rj  r‡  r   r4   r—   r   r3   rÎ  r/   r’  s       €r>   r.   z'TFLayoutLMForQuestionAnswering.__init__  sv   ø€ Ü‰Ñ˜Ð3 &Ò3¨FÒ3Ø ×+Ñ+ˆŒä+¨FÀdÐQ[Ô\ˆŒÜŸ,™,×,Ñ,Ø×#Ñ#Ü.¨v×/GÑ/GÓHØð -ó 
ˆŒð
 ˆ�r?   r“  r”  c                ó¶  — | j                  |||||||||	|
|¬«      }|d   }| j                  |¬«      }t        j                  |dd¬«      \  }}t        j                  |d¬«      }t        j                  |d¬«      }d}|� |�d	|i}||d
<   | j                  |||f¬«      }|
s||f|dd z   }|�|f|z   S |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", trust_remote_code=True)
        >>> 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   rc   rb   rY   )r\   Únum_or_size_splitsr`   )Úinputr`   NÚstart_positionÚend_positionr¯  )r²  Ústart_logitsÚ
end_logitsr¯   r#  )	r‡  rÎ  rL   ÚsplitÚsqueezer³  r   r¯   r#  )r;   rl   rm   r°   ro   rn   r±   rp   rµ   r(  r)  Ústart_positionsÚend_positionsrf   r¿   re  r¨   rÔ  rÕ  r²  r°  rÜ   s                         r>   ry   z#TFLayoutLMForQuestionAnswering.call  s!  € ðL —-‘-ØØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#Øð  ó 
ˆð " !™*ˆà—‘¨�Ó8ˆÜ#%§8¡8°&ÈQÐUWÔ#XÑ ˆ�jÜ—z‘z¨¸2Ô>ˆÜ—Z‘Z j°rÔ:ˆ
ØˆàÐ&¨=Ð+DØ&¨Ð8ˆFØ%2ˆF�>Ñ"Ø×'Ñ'¨v¸|ÈZÐ>XÐ'ÓYˆDáØ" JÐ/°'¸!¸"°+Ñ=ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä-ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r?   c                óô  — | j                   ry d| _         t        | dd «      �Mt        j                  | j                  j
                  «      5  | j                  j                  d «       d d d «       t        | dd «      �et        j                  | j                  j
                  «      5  | j                  j                  d d | j                  j                  g«       d d d «       y y # 1 sw Y   Œ|xY w# 1 sw Y   y xY w)NTr‡  rÎ  )
rQ   rR   rL   rM   r‡  r)   rS   rÎ  r/   r0   rT   s     r>   rS   z$TFLayoutLMForQuestionAnswering.buildˆ  rÂ  rÃ  rz   r€  )rl   r�  rm   r‚  r°   r‚  ro   r‚  rn   r‚  r±   r‚  rp   r‚  rµ   r1  r(  r1  r)  r1  rØ  r‚  rÙ  r‚  rf   r1  r~   z7Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]r{   )r€   r�   r‚   r¶  r.   r   r   r™  rš  r    r   r›  ry   rS   r„   r…   s   @r>   rÌ  rÌ  ý  s  ø„ ò*Ð&õ
ð Ù*Ð+D×+KÑ+KÐLiÓ+jÓkÙÐ+IÐXgÔhð .2Ø.2Ø8<Ø8<Ø6:Ø37Ø7;Ø,0Ø/3Ø&*Ø9=Ø7;Ø#(ðh
à*ðh
ð ,ðh
ð 6ð	h
ð
 6ðh
ð 4ðh
ð 1ðh
ð 5ðh
ð *ðh
ð -ðh
ð $ðh
ð 7ðh
ð 5ðh
ð !ðh
ð 
Aòh
ó ió ló ðh
÷T	Mr?   rÌ  )r�  r¸  rÆ  rÌ  rj  r�  r†  )Irƒ   Ú
__future__r   r”   rª  Útypingr   r   r   r   ÚnumpyÚnpÚ
tensorflowrL   Ú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›  r4   ÚLayerr$   r‡   rÄ   rÙ   rë   rü   r  r  r3  r<  rB  ra  rj  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˜eŸl™l×0Ñ0ô AHôJL˜5Ÿ<™<×-Ñ-ô Lô>0.˜%Ÿ,™,×,Ñ,ô 0.ôhH˜UŸ\™\×/Ñ/ô Hô<L�u—|‘|×)Ñ)ô Lô>d0�e—l‘l×(Ñ(ô d0ôPK&˜Ÿ™×*Ñ*ô K&ô^H�u—|‘|×)Ñ)ô Hô<"L¨¯©×(:Ñ(:ô "LôL- §¡×!3Ñ!3ô -ôb-˜Ÿ™×*Ñ*ô -ð( ôL(˜%Ÿ,™,×,Ñ,ó L(ó ðL(ô^Ð 1ô ð (Ð ðT5Ð ñp ØhØóôS*Ð/ó S*ó	ðS*ñl ÐRÐTlÓmô@%Ð5Ð7Só @%ó nð@%ñF ðð óôxMÐ*CÐEaó xMóðxMñv ðð óô|MÐ'@ÐB[ó |Móð|Mñ~ ðð
 óôLMÐ%>Ð@Wó LMóðLMò^�r?   