
    cCiY+                      S r SSKJr  SSKrSSKrSSKrSSKJr  SSK	J
r
JrJrJr  SSKJrJrJrJrJrJrJrJr  SSKJr  SS	KJrJrJr  S
SKJr  Sr/ SQ/ SQ/r / SQ/ SQ/ SQ// SQ/ SQ/ SQ//r!Sr" " S S\RF                  RH                  5      r% " S S\RF                  RH                  5      r& " S S\RF                  RH                  5      r' " S S\RF                  RH                  5      r( " S S\RF                  RH                  5      r) " S  S!\RF                  RH                  5      r* " S" S#\RF                  RH                  5      r+ " S$ S%\RF                  RH                  5      r, " S& S'\RF                  RH                  5      r-\ " S( S)\RF                  RH                  5      5       r. " S* S+\5      r/S,r0S-r1\" S.\05       " S/ S0\/5      5       r2 " S1 S2\RF                  RH                  5      r3\" S3\05       " S4 S5\/\5      5       r4\" S6\05       " S7 S8\/\5      5       r5\" S9\05       " S: S;\/\5      5       r6/ S<Qr7g)=zTF 2.0 LayoutLMv3 model.    )annotationsN   )get_tf_activation)TFBaseModelOutputTFQuestionAnsweringModelOutputTFSequenceClassifierOutputTFTokenClassifierOutput)TFPreTrainedModelTFQuestionAnsweringLossTFSequenceClassificationLossTFTokenClassificationLossget_initializerkeraskeras_serializableunpack_inputs)check_embeddings_within_bounds)add_start_docstrings%add_start_docstrings_to_model_forwardreplace_return_docstrings   )LayoutLMv3Configr   )      r   )r      r   )r   r   r      )   r   r      )	   
         )            )            )            g    חc                  D   ^  \ rS rSrSrSU 4S jjrSS jrS	S jrSrU =r	$ )
TFLayoutLMv3PatchEmbeddings>   z$LayoutLMv3 image (patch) embeddings.c                  > [         TU ]  " S	0 UD6  [        UR                  [        R
                  R                  5      (       a  UR                  OUR                  UR                  4n[        R                  R                  UR                  UUSSS[        UR                  5      SS9U l        UR                  U l
        UR                  S-  US   US   -  -  U l        Xl        g )
Nvalidchannels_lastTproj)filterskernel_sizestridespaddingdata_formatuse_biaskernel_initializernamer   r   r    )super__init__
isinstance
patch_sizecollectionsabcIterabler   layersConv2Dhidden_sizer   initializer_ranger4   
input_sizenum_patchesconfig)selfrK   kwargspatch_sizes	__class__s       o/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.pyr?   $TFLayoutLMv3PatchEmbeddings.__init__A   s    "6" &++[__-E-EFF ##V%6%67 	
 LL''&&#'.v/G/GH ( 	
	 "--"--q0k!n{ST~6UV    c                    [         R                  " U/ SQS9nU R                  U5      n[         R                  " USU R                  U R
                  45      nU$ )N)r   r   r   r   perm)tf	transposer4   reshaperJ   rG   )rL   pixel_values
embeddingss      rP   call TFLayoutLMv3PatchEmbeddings.callV   sK     ||L|DYY|,
ZZ
R1A1A4CSCS,TU
rR   c                B   U R                   (       a  g SU l         [        U SS 5      bg  [        R                  " U R                  R
                  5         U R                  R                  S S S U R                  R                  /5        S S S 5        g g ! , (       d  f       g = f)NTr4   )	builtgetattrrW   
name_scoper4   r<   buildrK   num_channelsrL   input_shapes     rP   rb   !TFLayoutLMv3PatchEmbeddings.build_   sm    ::
4&2tyy~~.		tT4;;3K3K LM /. 3..s   4B
B)r_   rK   rG   rJ   r4   rK   r   rZ   	tf.Tensorreturnri   N
__name__
__module____qualname____firstlineno____doc__r?   r\   rb   __static_attributes____classcell__rO   s   @rP   r/   r/   >   s    .*N NrR   r/   c                     ^  \ rS rSrSrSU 4S jjrSS jrSS jrSS jrSS jr	      S             SS jjr
SS	 jrS
rU =r$ )TFLayoutLMv3TextEmbeddingsh   ze
LayoutLMv3 text embeddings. Same as `RobertaEmbeddings` but with added spatial (layout) embeddings.
c                T  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  UR                  [        UR                  5      SS9U l	        [        R                  R	                  UR                  UR                  [        UR                  5      SS9U l        [        R                  R                  UR                  SS9U l        [        R                  R                  UR                   5      U l        UR$                  U l        [        R                  R	                  UR(                  UR                  [        UR                  5      SS9U l        [        R                  R	                  UR,                  UR.                  [        UR                  5      SS9U l        [        R                  R	                  UR,                  UR.                  [        UR                  5      SS9U l        [        R                  R	                  UR,                  UR4                  [        UR                  5      S	S9U l        [        R                  R	                  UR,                  UR4                  [        UR                  5      S
S9U l        UR,                  U l        Xl        g )Nword_embeddings)embeddings_initializerr<   token_type_embeddings	LayerNormepsilonr<   position_embeddingsx_position_embeddingsy_position_embeddingsh_position_embeddingsw_position_embeddingsr=   )r>   r?   r   rE   	Embedding
vocab_sizerG   r   rH   ry   type_vocab_sizer{   LayerNormalizationlayer_norm_epsr|   Dropouthidden_dropout_probdropoutpad_token_idpadding_token_indexmax_position_embeddingsr   max_2d_position_embeddingscoordinate_sizer   r   
shape_sizer   r   max_2d_positionsrK   rL   rK   rM   rO   s      rP   r?   #TFLayoutLMv3TextEmbeddings.__init__m   s9   "6"$||55#263K3K#L"	  6  
 &+\\%;%;""#263K3K#L(	 &< &
" 88AVAV]h8i||++F,F,FG#)#6#6 #(<<#9#9**#263K3K#L&	 $: $
  &+\\%;%;--""#263K3K#L(	 &< &
" &+\\%;%;--""#263K3K#L(	 &< &
" &+\\%;%;--#263K3K#L(	 &< &
" &+\\%;%;--#263K3K#L(	 &< &
" !' A ArR   c           	         US S 2S S 2S4   nUS S 2S S 2S4   nUS S 2S S 2S4   nUS S 2S S 2S4   n U R                  U5      nU R                  U5      nU R                  U5      n	U R                  U5      n
U R                  S-
  nU R	                  [
        R                  " US S 2S S 2S4   US S 2S S 2S4   -
  SU5      5      nU R                  [
        R                  " US S 2S S 2S4   US S 2S S 2S4   -
  SU5      5      n[
        R                  " UUU	U
UU/SS	9nU$ ! [          a  n[        S5      UeS nAff = f! [          a  n[        SU R                   S35      UeS nAff = f)
Nr   r   r   r   z9Bounding box is not of shape (batch_size, seq_length, 4).z0The `bbox` coordinate values should be within 0-z range.rV   axis)	
IndexErrorr   r   r   r   rW   clip_by_valuer   concat)rL   bboxleft_position_idsupper_position_idsright_position_idslower_position_ids	exceptionleft_position_embeddingsupper_position_embeddingsright_position_embeddingslower_position_embeddingsmax_position_idr   r   spatial_position_embeddingss                  rP   %calculate_spatial_position_embeddings@TFLayoutLMv3TextEmbeddings.calculate_spatial_position_embeddings   s   	i $Q1W!%aAg!%aAg!%aAg	'+'A'ABS'T$(,(B(BCU(V%(,(B(BCU(V%(,(B(BCU(V% //!3 $ : :T!Q']T!Q']:AO!
 !% : :T!Q']T!Q']:AO!

 ')ii()))%% 
'
# +*C  	iXY_hh	i  	B4CXCXBYY`a	s/   0D AD= 
D:)D55D:=
E&E!!E&c                0   [         R                  " U5      nUS   nU R                  S-   nU R                  U-   S-   n[         R                  " XE[         R                  S9nUS   n[         R
                  " USU45      n[         R                  " XgS45      nU$ )zr
We are provided embeddings directly. We cannot infer which are padded, so just generate sequential position
ids.
r   dtyper   )rW   shaper   rangeint32rY   tile)rL   inputs_embdsre   sequence_lengthstart_index	end_indexposition_ids
batch_sizes           rP   &create_position_ids_from_inputs_embedsATFLayoutLMv3TextEmbeddings.create_position_ids_from_inputs_embeds   s    
 hh|,%a...2,,>B	xxbhhG ^
zz,O0DEww|!_=rR   c                    [         R                  " [         R                  " XR                  5      UR                  5      n[         R
                  " USS9U-  nX0R                  -   nU$ )zm
Replace non-padding symbols with their position numbers. Position numbers begin at padding_token_index + 1.
r   r   )rW   cast	not_equalr   r   cumsum)rL   	input_idsmaskr   s       rP   "create_position_ids_from_input_ids=TFLayoutLMv3TextEmbeddings.create_position_ids_from_input_ids   sP     wwr||I/G/GH)//ZyyA.5#&>&>>rR   c                L    Uc  U R                  U5      $ U R                  U5      $ rk   )r   r   )rL   r   inputs_embedss      rP   create_position_ids.TFLayoutLMv3TextEmbeddings.create_position_ids   s*    >>}MM::9EErR   c                   Uc  U R                  X5      nUb  [        R                  " U5      nO[        R                  " U5      S S nUc  [        R                  " XtR                  S9nUc0  [        XR                  R                  5        U R                  U5      nU R                  U5      nXX-   n	U R                  U5      n
X-  n	U R                  U5      nX-  n	U R                  U	5      n	U R                  XS9n	U	$ )NrV   r   training)r   rW   r   zerosr   r   ry   	input_dimr{   r   r   r|   r   )rL   r   r   token_type_idsr   r   r   re   r{   r[   r   r   s               rP   r\   TFLayoutLMv3TextEmbeddings.call   s     33IML ((9-K((=1#26K!XXk9K9KLN *96J6J6T6TU 00;M $ : :> J":
"66|D)
&*&P&PQU&V#1
^^J/
\\*\@
rR   c                B   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U S	S 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       GN= f! , (       d  f       GNb= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNm= f! , (       d  f       GN#= f! , (       d  f       N= f! , (       d  f       g = f)
NTry   r{   r|   r   r   r   r   r   )r_   r`   rW   ra   ry   r<   rb   r{   r|   rK   rG   r   r   r   r   r   rd   s     rP   rb    TFLayoutLMv3TextEmbeddings.build  s   ::
4*D1=t33889$$**40 :40$7Ct99>>?**006 @4d+7t~~223$$dD$++2I2I%JK 44.5At77<<=((..t4 >40$7Ct99>>?**006 @40$7Ct99>>?**006 @40$7Ct99>>?**006 @40$7Ct99>>?**006 @? D) :9 @? 43 >= @? @? @? @?s`   L.L%
3L7=M	M5M-M?-N
L"%
L47
M	
M
M*-
M<?
N
N)r|   r_   rK   r   r   r   r   r   r{   r   ry   r   r   rg   )r   ri   rj   ri   )r   ri   rj   ri   )r   ri   rj   ri   )r   ri   r   ri   rj   ri   )NNNNNF)r   tf.Tensor | Noner   r   r   r   r   r   r   r   r   boolrj   ri   rk   )rm   rn   ro   rp   rq   r?   r   r   r   r   r\   rb   rr   rs   rt   s   @rP   rv   rv   h   s    0d'+RF '+!%+/)-*.### # )	#
 '# (# # 
#J7 7rR   rv   c                     ^  \ rS rSrSU 4S jjrS	S jrS
SS jjr   S               SS jjrSS jrSr	U =r
$ )TFLayoutLMv3SelfAttentioni)  c                  > [         TU ]  " S	0 UD6  UR                  UR                  -  S:w  a&  [	        SUR                   SUR                   S35      eUR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l        [        R                  " U R                  5      U l
        [        R                  R                  U R                  [        UR                  5      SS9U l        [        R                  R                  U R                  [        UR                  5      SS9U l        [        R                  R                  U R                  [        UR                  5      SS9U l        [        R                  R'                  UR(                  5      U l        UR,                  U l        UR.                  U l        Xl        g )
Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()queryr;   r<   keyvaluer=   )r>   r?   rG   num_attention_heads
ValueErrorintattention_head_sizeall_head_sizemathsqrtattention_score_normaliserr   rE   Denser   rH   r   r   r   r   attention_probs_dropout_probr   has_relative_attention_biashas_spatial_attention_biasrK   r   s      rP   r?   "TFLayoutLMv3SelfAttention.__init__*  s   "6" : ::a?#F$6$6#7 8 445Q8 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PP*.))D4L4L*M'\\''.v/G/GH ( 


 <<%%.v/G/GH & 

 \\''.v/G/GH ( 

 ||++F,O,OP+1+M+M(*0*K*K'rR   c                    [         R                  " U5      nUS   US   U R                  U R                  4n[         R                  " X5      n[         R
                  " U/ SQS9$ )Nr   r   r   r   r   r   rT   )rW   r   r   r   rY   rX   )rL   xr   	new_shapes       rP   transpose_for_scores.TFLayoutLMv3SelfAttention.transpose_for_scoresL  sW    !H!H$$$$	
	 JJq$||AL11rR   c                    X-  n[         R                  " [         R                  " USS9SS9nX4-
  U-  n[         R                  R	                  USS9$ )a  
https://huggingface.co/papers/2105.13290 Section 2.4 Stabilization of training: Precision Bottleneck Relaxation
(PB-Relax). A replacement of the original keras.layers.Softmax(axis=-1)(attention_scores). Seems the new
attention_probs will result in a slower speed and a little bias. Can use
tf.debugging.assert_near(standard_attention_probs, cogview_attention_probs, atol=1e-08) for comparison. The
smaller atol (e.g., 1e-08), the better.
rV   r   )rW   expand_dims
reduce_maxr   softmax)rL   attention_scoresalphascaled_attention_scores	max_valuenew_attention_scoress         rP   cogview_attention+TFLayoutLMv3SelfAttention.cogview_attentionW  sP     #3":NN2==1Hr#RY[\	 7 CuLww3"==rR   c                B   U R                  U R                  U5      5      nU R                  U R                  U5      5      n	U R                  U R                  U5      5      n
XR                  -  n[
        R                  " U/ SQS9n[
        R                  " X5      nU R                  (       a&  U R                  (       a  XU-   U R                  -  -  nO"U R                  (       a  XU R                  -  -  nUb  X-  nU R                  U5      nU R                  XS9nUb  X-  n[
        R                  " X5      n[
        R                  " U/ SQS9n[
        R                  " U5      n[
        R                  " UUS   US   U R                  45      nU(       a  X4nU$ U4nU$ )N)r   r   r   r   rT   r   r   r   r   )r   r   r   r   r   rW   rX   matmulr   r   r   r   r   rY   r   )rL   hidden_statesattention_mask	head_maskoutput_attentionsrel_pos
rel_2d_posr   	key_layervalue_layerquery_layernormalised_query_layertransposed_key_layerr   attention_probscontext_layerr   outputss                     rP   r\   TFLayoutLMv3SelfAttention.calld  s    --dhh}.EF	//

=0IJ//

=0IJ "-/N/N!N!||L 
 99%;R++0O0O:!59X9X XX--$*I*I II%. 001AB,,,J  -9O		/?
 '

E!HeAh0B0BC
 7H=2 O\M]rR   c                P   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)NTr   r   r   )r_   r`   rW   ra   r   r<   rb   rK   rG   r   r   rd   s     rP   rb   TFLayoutLMv3SelfAttention.build  s   ::
4$'3tzz/

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

  $dkk.E.E!FG 0/ 4 0/ .- 0/s$   3E53F83F5
F
F
F%)r   r   r   r_   rK   r   r   r   r   r   r   r   rg   )r   ri   )    )r   ri   r   zfloat | intNNFr   ri   r   r   r   r   r   r   r   r   r   r   r   r   rj   z.tuple[tf.Tensor] | tuple[tf.Tensor, tf.Tensor]rk   )rm   rn   ro   rp   r?   r   r   r\   rb   rr   rs   rt   s   @rP   r   r   )  s~     D	2>& %)'+3 3 )3 $	3
  3 "3 %3 3 
83jH HrR   r   c                  D   ^  \ rS rSrSU 4S jjrSSS jjrS	S jrSrU =r$ )
TFLayoutLMv3SelfOutputi  c                f  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SS9U l        [        R                  R                  UR                  SS9U l        [        R                  R                  UR                  S9U l        Xl        g Ndenseunitsr;   r<   r|   r}   )rater=   r>   r?   r   rE   r   rG   r   rH   r  r   r   r|   r   r   r   rK   r   s      rP   r?   TFLayoutLMv3SelfOutput.__init__      "6"\\''$$IaIa9bip ( 

 88AVAV]h8i||++1K1K+LrR   c                d    U R                  US9nU R                  XS9nU R                  X-   S9nU$ Ninputs)r  r   r  r   r|   rL   r   input_tensorr   s       rP   r\   TFLayoutLMv3SelfOutput.call  ;    

-
8MMm.JKrR   c                H   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = fNTr  r|   )
r_   r`   rW   ra   r  r<   rb   rK   rG   r|   rd   s     rP   rb   TFLayoutLMv3SelfOutput.build  s    ::
4$'3tzz/

  $dkk.E.E!FG 04d+7t~~223$$dD$++2I2I%JK 43 8 0/ 43   3D3D
D
D!r|   r_   rK   r  r   rg   Fr   ri   r  ri   r   r   rj   ri   rk   	rm   rn   ro   rp   r?   r\   rb   rr   rs   rt   s   @rP   r  r        	L 	LrR   r  c                  h   ^  \ rS rSrSU 4S jjr   S               SS jjrS	S jrSrU =r$ )
TFLayoutLMv3Attentioni  c                b   > [         TU ]  " S0 UD6  [        USS9U l        [	        USS9U l        g )NrL   r<   outputr=   )r>   r?   r   self_attentionr  self_outputr   s      rP   r?   TFLayoutLMv3Attention.__init__  s1    "6"7VL1&xHrR   c           
     h    U R                  UUUUUUUS9nU R                  US   XS9n	U	4USS  -   n
U
$ )Nr   r   r   )r-  r.  )rL   r   r   r   r   r   r   r   self_outputsattention_outputr  s              rP   r\   TFLayoutLMv3Attention.call  sc     ** + 
  ++LO]+^#%QR(88rR   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       Nl= f! , (       d  f       g = f)NTr-  r.  )r_   r`   rW   ra   r-  r<   rb   r.  rd   s     rP   rb   TFLayoutLMv3Attention.build  s    ::
4)40<t22778##))$/ 94-9t//445  &&t, 65 : 98 65   C.C%
C"%
C3)r_   r-  r.  rg   r	  r
  rk   r&  rt   s   @rP   r)  r)    sq    I %)'+  ) $	
   " %  
8.	- 	-rR   r)  c                  @   ^  \ rS rSrSU 4S jjrSS jrSS jrSrU =r$ )	TFLayoutLMv3Intermediatei  c                J  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SS9U l        [        UR                  [        5      (       a  [        UR                  5      U l        OUR                  U l        Xl        g )Nr  r  r=   )r>   r?   r   rE   r   intermediate_sizer   rH   r  r@   
hidden_actstrr   intermediate_act_fnrK   r   s      rP   r?   !TFLayoutLMv3Intermediate.__init__  s    "6"\\''**vOgOg?hov ( 

 f''--'89J9J'KD$'-'8'8D$rR   c                F    U R                  US9nU R                  U5      nU$ )Nr  )r  r=  )rL   r   s     rP   r\   TFLayoutLMv3Intermediate.call  s(    

-
800?rR   c                @   U R                   (       a  g SU l         [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       g = f)NTr  )	r_   r`   rW   ra   r  r<   rb   rK   rG   rd   s     rP   rb   TFLayoutLMv3Intermediate.build  sm    ::
4$'3tzz/

  $dkk.E.E!FG 0/ 4//s   3B
B)r_   rK   r  r=  rg   )r   ri   rj   ri   rk   r&  rt   s   @rP   r8  r8    s    H HrR   r8  c                  D   ^  \ rS rSrSU 4S jjrSSS jjrS	S jrSrU =r$ )
TFLayoutLMv3Outputi  c                f  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  [        UR                  5      SS9U l        [        R                  R                  UR                  SS9U l        [        R                  R                  UR                  S9U l        Xl        g r  r  r   s      rP   r?   TFLayoutLMv3Output.__init__  r  rR   c                d    U R                  US9nU R                  XS9nU R                  X-   S9nU$ r  r  r  s       rP   r\   TFLayoutLMv3Output.call  r  rR   c                H   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = fr   )r_   r`   rW   ra   r  r<   rb   rK   r:  r|   rG   rd   s     rP   rb   TFLayoutLMv3Output.build!  s    ::
4$'3tzz/

  $dkk.K.K!LM 04d+7t~~223$$dD$++2I2I%JK 43 8 0/ 43r"  r#  rg   r$  r%  rk   r&  rt   s   @rP   rD  rD    r'  rR   rD  c                  h   ^  \ rS rSrSU 4S jjr   S               SS jjrS	S jrSrU =r$ )
TFLayoutLMv3Layeri-  c                   > [         TU ]  " S0 UD6  [        USS9U l        [	        USS9U l        [        USS9U l        g )N	attentionr+  intermediater,  r=   )r>   r?   r)  rN  r8  rO  rD  bert_outputr   s      rP   r?   TFLayoutLMv3Layer.__init__.  s?    "6".vKH4V.Q-f8DrR   c           
         U R                  UUUUUUUS9nUS   n	USS  n
U R                  U	5      nU R                  XUS9nU4U
-   n
U
$ )N)r   r   r   r   r   r   r   )rN  rO  rP  )rL   r   r   r   r   r   r   r   self_attention_outputsr2  r  intermediate_outputlayer_outputs                rP   r\   TFLayoutLMv3Layer.call4  s     "&/! "0 "
 2!4(,"//0@A''(;X`'a/G+rR   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       N}= f! , (       d  f       g = f)NTrN  rO  rP  )	r_   r`   rW   ra   rN  r<   rb   rO  rP  rd   s     rP   rb   TFLayoutLMv3Layer.buildN  s    ::
4d+7t~~223$$T* 44.:t00556!!''- 74-9t//445  &&t, 65 : 43 76 65s$   D0.E
E0
D>
E
E )rN  rP  r_   rO  rg   r	  r
  rk   r&  rt   s   @rP   rL  rL  -  sq    E %)'+  ) $	
   " %  
84- -rR   rL  c                     ^  \ rS rSrS
U 4S jjrSS jr        SS jrSS jrSS jr        S                   SS jjr	SS jr
S	rU =r$ )TFLayoutLMv3Encoderi]  c                F  > [         TU ]  " S0 UD6  Xl        [        UR                  5       Vs/ s H  n[        USU 3S9PM     snU l        UR                  U l        UR                  U l        U R                  (       ad  UR                  U l	        UR                  U l
        [        R                  R                  UR                  [        UR                   5      SSS9U l        U R                  (       a  UR$                  U l        UR&                  U l        [        R                  R                  UR                  [        UR                   5      SSS9U l        [        R                  R                  UR                  [        UR                   5      SSS9U l        g g s  snf )	Nzlayer.r+  Frel_pos_bias)r  r;   r:   r<   rel_pos_x_biasrel_pos_y_biasr=   )r>   r?   rK   r   num_hidden_layersrL  layerr   r   rel_pos_binsmax_rel_posr   rE   r   r   r   rH   r\  max_rel_2d_posrel_2d_pos_binsr]  r^  )rL   rK   rM   irO   s       rP   r?   TFLayoutLMv3Encoder.__init__^  sj   "6"LQRXRjRjLklLkq'vaS\BLkl
+1+M+M(*0*K*K'++ & 3 3D%11D % 2 200#263K3K#L#	 !3 !D **"("7"7D#)#9#9D "',,"4"400#263K3K#L%	 #5 #D #(,,"4"400#263K3K#L%	 #5 #D + ms   Fc                   US-  n[         R                  " U5      nUS-  nXE:  n[         R                  R                  [         R                  " U[         R
                  5      U-  5      n[        R                  " X5-  5      nXx-  X%-
  -  n	XY-   n
[         R                  " XR                  5      n
[         R                  " XS-
  5      n
[         R                  " US:  UR                  5      U-  [         R                  " XdU
5      -   $ )Nr   r   r   )	rW   absr   logr   float32r   minimumwhere)rL   relative_positionsnum_bucketsmax_distancebucketsmax_exact_bucketsis_smallbuckets_log_ratiodistance_log_ratiobuckets_big_offsetbuckets_bigs              rP   relative_position_bucket,TFLayoutLMv3Encoder.relative_position_bucket  s     "Q&&&+, (1,. GGKK(DGX(XY!XXl&FG2k6UV 	 (<ggk==9jjAo>*Q.>LPRPXPX{Q
 
 	
rR   c                <   [         R                  " USS9[         R                  " USS9-
  nU R                  XSU5      n[         R                  " XcU R                  S9nU" U5      n[         R
                  " U/ SQ5      n[         R                  " XR                  S9nU$ )Nr   rV   )depthr   )r   r   r   r   r   )rW   r   rw  one_hotcompute_dtyperX   r   )	rL   dense_layerr   rn  ro  rel_pos_matrixr   rel_pos_one_hot	embeddings	            rP   _cal_pos_emb TFLayoutLMv3Encoder._cal_pos_emb  s     2>P\ceAff//\Z**WtGYGYZ0	LLL9	GGI-?-?@	rR   c                d    U R                  U R                  XR                  U R                  5      $ rk   )r  r\  ra  rb  )rL   r   s     rP   _cal_1d_pos_emb#TFLayoutLMv3Encoder._cal_1d_pos_emb  s*      !2!2LBSBSUYUeUeffrR   c                   US S 2S S 2S4   nUS S 2S S 2S4   nU R                  U R                  UU R                  U R                  5      nU R                  U R                  UU R                  U R                  5      nXE-   nU$ )Nr   r   )r  r]  rd  rc  r^  )rL   r   position_coord_xposition_coord_y	rel_pos_x	rel_pos_yr   s          rP   _cal_2d_pos_emb#TFLayoutLMv3Encoder._cal_2d_pos_emb  s    1a=1a=%%  	
	 %%  	
	 *
rR   c
                   U(       a  SOS n
U(       a  SOS nU R                   (       a  U R                  U5      OS nU R                  (       a  U R                  U5      OS n[	        U R
                  5       H=  u  pU(       a  X4-   n
Ub  XN   OS nU" UUUUUUU	S9nUS   nU(       d  M4  UUS   4-   nM?     U(       a  X4-   n
U(       a  [        UU
US9$ [        S XU4 5       5      $ )Nr=   )r   r   r   r   r   last_hidden_stater   
attentionsc              3  .   #    U  H  oc  M  Uv   M     g 7frk   r=   ).0r   s     rP   	<genexpr>+TFLayoutLMv3Encoder.call.<locals>.<genexpr>  s      #Z%#Zs   	)r   r  r   r  	enumerater`  r   tuple)rL   r   r   r   r   r   output_hidden_statesreturn_dictr   r   all_hidden_statesall_self_attentionsr   r   re  layer_modulelayer_head_masklayer_outputss                     rP   r\   TFLayoutLMv3Encoder.call  s    #7BD$5b48<8X8X$&&|4^b373R3RT))$/X\
(4OA#$58H$H!.7.CilO(!%!M *!,M  &9]1=M<O&O#%  5(   14D D$"//.   $1FY#Z  rR   c                   U R                   (       a  g SU l         [        U SS 5      b[  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  /5        S S S 5        [        U SS 5      b[  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  /5        S S S 5        [        U SS 5      b[  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  /5        S S S 5        [        U SS 5      bN  U R                   H=  n[        R                  " UR
                  5         UR                  S 5        S S S 5        M?     g g ! , (       d  f       GN>= f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       M  = f)NTr\  r]  r^  r`  )r_   r`   rW   ra   r\  r<   rb   ra  r]  rd  r^  r`  )rL   re   r`  s      rP   rb   TFLayoutLMv3Encoder.build  sf   ::
4.:t00556!!''tT5F5F(GH 74)40<t22778##))4t7K7K*LM 94)40<t22778##))4t7K7K*LM 94$'3]]5::.KK% /. $ 4 76 98 98 /.s0   )F2;)G$)GG&2
G
G
G#&
G5	)r_   rK   r   r   r`  rc  rb  rd  r\  ra  r]  r^  rg   )rm  ri   rn  r   ro  r   )r~  zkeras.layers.Denser   ri   rn  r   ro  r   )r   ri   )r   ri   )NNNFFTNF)r   ri   r   r   r   r   r   r   r   r   r  r   r  r   r   r   r   r   rj   kTFBaseModelOutput | tuple[tf.Tensor] | tuple[tf.Tensor, tf.Tensor] | tuple[tf.Tensor, tf.Tensor, tf.Tensor]rk   )rm   rn   ro   rp   r?   rw  r  r  r  r\   rb   rr   rs   rt   s   @rP   rZ  rZ  ]  s     D
2'   	
  g* "&+/&*"'%* )-2 2 2 )	2
 $2  2 #2 2 '2 2 
u2h& &rR   rZ  c                     ^  \ rS rSr\rSU 4S jjrSS jrSS jrSS jr	S r
SSS jjrSS jrSS	 jrSS
 jrSS jr\            S                         SS jj5       rSrU =r$ )TFLayoutLMv3MainLayeri  c                `  > [         TU ]  " S0 UD6  Xl        UR                  (       a  [	        USS9U l        UR                  (       a  [        USS9U l        [        R                  R                  UR                  SS9U l        [        R                  R                  UR                  SS9U l        UR"                  (       d  UR$                  (       a)  UR&                  UR(                  -  nU R+                  X34S9  [        R                  R                  SS	S9U l        [/        US
S9U l        g )Nr[   r+  patch_embedr|   r}   r   )
image_sizegư>normencoderr=   )r>   r?   rK   
text_embedrv   r[   visual_embedr/   r  r   rE   r   r   r|   r   r   r   r   r   rI   rA   init_visual_bboxr  rZ  r  )rL   rK   rM   r  rO   s       rP   r?   TFLayoutLMv3MainLayer.__init__	  s    "6"8lSDO:6VD"\\<<VEZEZal<mDN <<//0J0JQZ/[DL11V5V5V#..&2C2CC
%%*1I%J7767RDI*6	BrR   c                F   U R                   R                  (       a  U R                   R                  U R                   R                  -  nU R	                  SSU R                   R
                  4SS[        R                  SS9U l        U R	                  SX"-  S-   U R                   R
                  4SS[        R                  SS9U l	        U R                  (       a  g SU l
        [        U SS 5      bN  [        R                  " U R                  R                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                   R                  5         U R                   R                  S 5        S S S 5        [        U S	S 5      bN  [        R                  " U R"                  R                  5         U R"                  R                  S 5        S S S 5        [        U S
S 5      be  [        R                  " U R$                  R                  5         U R$                  R                  S S U R                   R
                  /5        S S S 5        [        U SS 5      bN  [        R                  " U R&                  R                  5         U R&                  R                  S 5        S S S 5        [        U SS 5      bf  [        R                  " U R(                  R                  5         U R(                  R                  S S U R                   R
                  /5        S S S 5        g g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNw= f! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       g = f)Nr   r   T	cls_token)r   initializer	trainabler   r<   	pos_embedr  r[   r  r|   r   r  )rK   r  rI   rA   
add_weightrG   rW   rj  r  r  r_   r`   ra   r  r<   rb   r[   r  r|   r   r  )rL   re   r  s      rP   rb   TFLayoutLMv3MainLayer.build  s   ;;##//4;;3I3IIJ!__!T[[445#jj  - DN "__*1A5t{{7N7NO#jj  - DN ::
4D)5t||001""4( 24t,8t334%%d+ 54-9t//445  &&t, 64d+7t~~223$$dD$++2I2I%JK 44D)5t||001""4( 24&2tyy~~.		tT[[-D-D EF /. 3 21 54 65 43 21 /.sH   L95MM-3M/ N<3N9
M
M
M,/
M>
N
N c                .    U R                   R                  $ rk   )r[   ry   )rL   s    rP   get_input_embeddings*TFLayoutLMv3MainLayer.get_input_embeddingsF  s    ...rR   c                8    XR                   R                  l        g rk   )r[   ry   weight)rL   r   s     rP   set_input_embeddings*TFLayoutLMv3MainLayer.set_input_embeddingsI  s    16''.rR   c                    [         e)z
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
class PreTrainedModel
)NotImplementedError)rL   heads_to_prunes     rP   _prune_heads"TFLayoutLMv3MainLayer._prune_headsM  s
    
 "!rR   c                h   Uu  p4[         R                  " SX$S-   -  U5      U-  n[         R                  " USS9n[         R                  " XTS/5      n[         R                  " SX#S-   -  U5      U-  n[         R                  " USS9n[         R                  " USU/5      n[         R                  " US S 2S S24   US S US S 2SS 24   USS  /SS9n[         R
                  " USS/5      n[         R                  " SSUS-
  US-
  //[         R                  S9n[         R                  " X/SS9U l	        g )Nr   r   r   rV   r   r   )
rW   r   r   r   stackrY   constantr   r   visual_bbox)	rL   r  max_lenheightwidthvisual_bbox_xvisual_bbox_yr  cls_token_boxs	            rP   r  &TFLayoutLMv3MainLayer.init_visual_bboxT  s&    #Gqy$97CuL}1=qz:Gz$:GDN}1=6{;hh1crc6"M#2$6ae8Lm\]\^N_`
 jjr1g6aGaK1%E$FbhhW99m%AJrR   c                    [         R                  " U R                  SS9n[         R                  " X1SS/5      n[         R                  " X2S9nU$ )Nr   r   r   r   )rW   r   r  r   r   )rL   r   r   r  s       rP   calculate_visual_bbox+TFLayoutLMv3MainLayer.calculate_visual_bboxk  sA    nnT%5%5A>ggk1+=>ggk7rR   c                (   U R                  U5      n[        R                  " U5      S   n[        R                  " U R                  USS/5      n[        R
                  " XB/SS9n[        U SS 5      b  X R                  -  nU R                  U5      nU$ )Nr   r   r   r  )	r  rW   r   r   r  r   r`   r  r  )rL   rZ   r[   r   
cls_tokenss        rP   embed_image!TFLayoutLMv3MainLayer.embed_imageq  s    %%l3
 XXj)!,
WWT^^j!Q-?@
YY
7a@
 4d+7..(JYYz*
rR   c                T   [        UR                  5      nUS:X  a  [        R                  " USS9nOJUS:X  a+  [        R                  " USS9n[        R                  " USS9nO[	        SUR                   S35      e[        R
                  " X0R                  5      nSU-
  [        -  nU$ )Nr   r   r   r   z&Wrong shape for attention_mask (shape ).g      ?)lenr   rW   r   r   r   r}  LARGE_NEGATIVE)rL   r   n_dimsextended_attention_masks       rP   get_extended_attention_mask1TFLayoutLMv3MainLayer.get_extended_attention_mask  s     ^))* Q;&(nn^!&L#q[ ')nn^!&L#&(nn5LST&U#EnFZFZE[[]^__ #%''*ACUCU"V#&)@#@N"R&&rR   c                   Uc  S /U R                   R                  -  $ [        R                  " U5      nUS:X  a  [        R                  " USS9n[        R                  " USS9n[        R                  " USS9n[        R                  " USS9n[        R
                  " XR                   R                  SSSS/5      nOeUS:X  a@  [        R                  " USS9n[        R                  " USS9n[        R                  " USS9nOUS:w  a  [        SUR                   S35      e[        R                  " U5      S:X  d   S	[        R                  " U5       S
35       e[        R                  " XR                  5      nU$ )Nr   r   r   rV   r   r   z!Wrong shape for head_mask (shape r  zGot head_mask rank of z, but require 5.)
rK   r_  rW   rankr   r   r   r   r   r}  )rL   r   r  s      rP   get_head_mask#TFLayoutLMv3MainLayer.get_head_mask  s@   6DKK9999#Q;yq9Iyq9Iyr:Iyr:IKK991aAFI q[yq9Iyr:Iyr:Iq[@@QQSTUUwwy!Q&e*@AS@TTd(ee&GGI'9'9:	rR   c                   U	b  U	OU R                   R                  n	U
b  U
OU R                   R                  n
Ub  UOU R                   R                  nUb!  [        R
                  " U5      nUS   nUS   nOLUb!  [        R
                  " U5      nUS   nUS   nO(Ub  [        R
                  " U5      S   nO[        S5      eUb  UR                  nO@Ub  UR                  nO0Ub  UR                  nO Ub  UR                  nO[        R                  nUc  Ubc  Uc  [        R                  " UW4US9nUc  [        R                  " UW4US9nUc  [        R                  " UWS4US9nU R                  UUUUUUS9nS nS nUGb  U R                  U5      n[        R                  " U[        R
                  " U5      S   4US9nUc  UnO[        R                  " UU/SS9nU R                   R                  (       a/  U R                  UU5      nUc  UnO[        R                  " UU/SS9nU R                   R                   (       d  U R                   R                  (       a  [        R"                  " S[        R
                  " U5      S   US9n[        R$                  " USS9n[        R&                  " UUS/5      nUc  UbY  [        R$                  " [        R"                  " SWUS9SS9n[        R&                  " X^S/5      n[        R                  " UU/SS9nOUnUc  Uc  UnO[        R                  " WU/SS9nU R)                  U5      nU R+                  UUS9nOU R                   R                   (       d  U R                   R                  (       a{  U R                   R                   (       aC  [        R$                  " [        R"                  " SWUS9SS9n[        R&                  " X^S/5      nUnU R                   R                  (       a  UnU R-                  U5      nU R/                  U5      nU R1                  WUUUUU	U
US	9nUS   nU(       d	  U4USS  -   $ [3        UUR4                  UR6                  S
9$ )Nr   r   zEYou have to specify either input_ids or inputs_embeds or pixel_valuesr   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r  r  r  )rK   r   r  r  rW   r   r   r   r   onesr   r[   r  r   r   r  r   r   r   r   r|   r   r  r  r  r   r   r  )rL   r   r   r   r   r   r   r   rZ   r   r  r  r   re   r   
seq_length	int_dtypeembedding_output
final_bboxfinal_position_idsvisual_embeddingsvisual_attention_maskr  visual_position_idsr  encoder_outputssequence_outputs                              rP   r\   TFLayoutLMv3MainLayer.call  sS   , 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++BYBY ((9-K$QJ$QJ&((=1K$QJ$QJ%,/2Jdee  !I

I'&,,I'&,,II M$=%!#*j)A!S%!#:z*B)!T|xxZ ;9M##)-+!  /   
!# $ 0 0 > %'GGZBS9TUV9W,X`i$j!%!6!#N<Q+RYZ![ {{55"88YO<!,J!#D++>Q!GJ {{66$++:`:`&(hhq"((;L2Ma2PXa&b#&(nn5Hq&Q#&(gg.AJPQ?&S#(M,E#%>>"((1jPY2Zab#cL#%77<a#IL)+LBU3V]^)_&)<&  ]%:#4 #%99.>@Q-RYZ#[ #~~.>?#||,<x|P[[448^8^{{66!~~bhhq*I.V]^_!ww|!_E%1"{{55!
"&"B"B>"R &&y1	,,+2/!5# ' 	
 *!,#%(;;; -)77&11
 	
rR   )r|   r_   r  rK   r   r[   r  r  r  r  r  rg   rk   )rj   zkeras.layers.Layer)r   ztf.Variable)i  )r  ztuple[int, int]r  r   )r   r   r   ztf.DTyperh   )r   ri   rj   ri   )r   r   rj   z"tf.Tensor | list[tf.Tensor | None]NNNNNNNNNNNFr   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   r   r   bool | Noner  r  r  r  r   r   rj   r  )rm   rn   ro   rp   r   config_classr?   rb   r  r  r  r  r  r  r  r  r   r\   rr   rs   rt   s   @rP   r  r    s   #LC*&GP/7"K.'62  '+!%+/+/)-&**.)-)-,0#'[
#[
 [
 )	[

 )[
 '[
 $[
 ([
 '[
 '[
 *[
 ![
 [
 
u[
 [
rR   r  c                  >   ^  \ rS rSrSr\rSr\U 4S j5       r	Sr
U =r$ )TFLayoutLMv3PreTrainedModeliS  zz
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
models.

layoutlmv3c                j   > [         TU ]  n[        R                  " S[        R                  SS9US'   U$ )N)NNr   r   r+  )r>   input_signaturerW   
TensorSpecr   )rL   sigrO   s     rP   r  +TFLayoutLMv3PreTrainedModel.input_signature\  s,    g%mmORXXFKF
rR   r=   )rm   rn   ro   rp   rq   r   r  base_model_prefixpropertyr  rr   rs   rt   s   @rP   r  r  S  s'    
 $L$ rR   r  a	  
    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>

    Parameters:
        config ([`LayoutLMv3Config`]): 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 `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Note that `sequence_length = token_sequence_length + patch_sequence_length + 1` where `1` is for [CLS]
            token. See `pixel_values` for `patch_sequence_length`.

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

            [What are input IDs?](../glossary#input-ids)

        bbox (`Numpy array` or `tf.Tensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
            Bounding boxes of each input sequence tokens. Selected in the range `[0,
            config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
            format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
            y1) represents the position of the lower right corner.

            Note that `sequence_length = token_sequence_length + patch_sequence_length + 1` where `1` is for [CLS]
            token. See `pixel_values` for `patch_sequence_length`.

        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Batch of document images. Each image is divided into patches of shape `(num_channels, config.patch_size,
            config.patch_size)` and the total number of patches (=`patch_sequence_length`) equals to `((height /
            config.patch_size) * (width / config.patch_size))`.

        attention_mask (`tf.Tensor` of shape `(batch_size, sequence_length)`, *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**.

            Note that `sequence_length = token_sequence_length + patch_sequence_length + 1` where `1` is for [CLS]
            token. See `pixel_values` for `patch_sequence_length`.

            [What are attention masks?](../glossary#attention-mask)
        token_type_ids (`Numpy array` or `tf.Tensor` of shape `(batch_size, sequence_length)`, *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.

            Note that `sequence_length = token_sequence_length + patch_sequence_length + 1` where `1` is for [CLS]
            token. See `pixel_values` for `patch_sequence_length`.

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

            Note that `sequence_length = token_sequence_length + patch_sequence_length + 1` where `1` is for [CLS]
            token. See `pixel_values` for `patch_sequence_length`.

            [What are position IDs?](../glossary#position-ids)
        head_mask (`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 `(batch_size, sequence_length, 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.
zdThe bare LayoutLMv3 Model transformer outputting raw hidden-states without any specific head on top.c                     ^  \ rS rSrS/rU 4S jr\\" \5      \	" \
\S9            S                         S	S jj5       5       5       rS
S jrSrU =r$ )TFLayoutLMv3Modeli  r   c                L   > [         TU ]  " U/UQ70 UD6  [        USS9U l        g )Nr  r+  )r>   r?   r  r  )rL   rK   r  rM   rO   s       rP   r?   TFLayoutLMv3Model.__init__  s(    3&3F3/\JrR   output_typer  c                :    U R                  UUUUUUUUU	U
UUS9nU$ )ay  
Returns:

Examples:

```python
>>> from transformers import AutoProcessor, TFAutoModel
>>> from datasets import load_dataset

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = TFAutoModel.from_pretrained("microsoft/layoutlmv3-base")

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

>>> encoding = processor(image, words, boxes=boxes, return_tensors="tf")

>>> outputs = model(**encoding)
>>> last_hidden_states = outputs.last_hidden_state
```)r   r   r   r   r   r   r   rZ   r   r  r  r   )r  )rL   r   r   r   r   r   r   r   rZ   r   r  r  r   r  s                 rP   r\   TFLayoutLMv3Model.call  sC    T //))%'%/!5# " 
 rR   c                   U R                   (       a  g SU l         [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       g = f)NTr  )r_   r`   rW   ra   r  r<   rb   rd   s     rP   rb   TFLayoutLMv3Model.build   s^    ::
4t,8t334%%d+ 54 944s   A88
B)r_   r  r  r  rk   )rm   rn   ro   rp   "_keys_to_ignore_on_load_unexpectedr?   r   r   LAYOUTLMV3_INPUTS_DOCSTRINGr   r   _CONFIG_FOR_DOCr\   rb   rr   rs   rt   s   @rP   r  r    s     +:):&K *+FG+<?[ '+!%+/+/)-&**.)-)-,0#'6#6 6 )	6
 )6 '6 $6 (6 '6 '6 *6 !6 6 
u6 \ H 6p, ,rR   r  c                  H   ^  \ rS rSrSrSU 4S jjrSS	S jjrS
S jrSrU =r	$ )TFLayoutLMv3ClassificationHeadi)  zT
Head for sentence-level classification tasks. Reference: RobertaClassificationHead
c                  > [         TU ]  " S0 UD6  [        R                  R	                  UR
                  S[        UR                  5      SS9U l        UR                  b  UR                  OUR                  n[        R                  R                  USS9U l        [        R                  R	                  UR                  [        UR                  5      SS9U l        Xl        g )	Ntanhr  )
activationr;   r<   r   r+  out_projr   r=   )r>   r?   r   rE   r   rG   r   rH   r  classifier_dropoutr   r   r   
num_labelsr  rK   )rL   rK   rM   r  rO   s       rP   r?   'TFLayoutLMv3ClassificationHead.__init__.  s    "6"\\''.v/G/GH	 ( 

 *0)B)B)NF%%TZTnTn 	 ||++ , 
 **.v/G/GH + 

 rR   c                    U R                  XS9nU R                  U5      nU R                  X2S9nU R                  U5      nU$ )Nr   )r   r  r  )rL   r  r   r  s       rP   r\   #TFLayoutLMv3ClassificationHead.callD  sC    ,,v,9**W%,,w,:--(rR   c                "   U R                   (       a  g SU l         [        U SS 5      be  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)NTr  r   r  )r_   r`   rW   ra   r  r<   rb   rK   rG   r   r  rd   s     rP   rb   $TFLayoutLMv3ClassificationHead.buildK  s
   ::
4$'3tzz/

  $dkk.E.E!FG 04D)5t||001""4( 24T*6t}}112##T41H1H$IJ 32 7 0/ 21 32s$   3EE/!3F 
E,/
E= 
F)r_   rK   r  r   r  rg   r$  )r  ri   r   r   rj   ri   rk   rl   rt   s   @rP   r  r  )  s    ,K KrR   r  a
  
    LayoutLMv3 Model with a sequence classification head on top (a linear layer on top of the final hidden state of the
    [CLS] token) e.g. for document image classification tasks such as the
    [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip/) dataset.
    c                     ^  \ rS rSrS/rSU 4S jjr\\" \5      \	" \
\S9             S	                           S
S jj5       5       5       rSS jrSrU =r$ )%TFLayoutLMv3ForSequenceClassificationiZ  r   c                p   > [         TU ]  " U40 UD6  Xl        [        USS9U l        [        USS9U l        g )Nr  r+  
classifier)r>   r?   rK   r  r  r  r  r   s      rP   r?   .TFLayoutLMv3ForSequenceClassification.__init__f  s6    *6*/\J8lSrR   r  c                L   U
b  U
OU R                   R                  n
U R                  UUUUUUUU	U
UUUS9nUS   SS2SSS24   nU R                  XS9nUc  SOU R	                  UU5      nU
(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )a  
Returns:

Examples:

```python
>>> from transformers import AutoProcessor, TFAutoModelForSequenceClassification
>>> from datasets import load_dataset
>>> import tensorflow as tf

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = TFAutoModelForSequenceClassification.from_pretrained("microsoft/layoutlmv3-base")

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

>>> encoding = processor(image, words, boxes=boxes, return_tensors="tf")
>>> sequence_label = tf.convert_to_tensor([1])

>>> outputs = model(**encoding, labels=sequence_label)
>>> loss = outputs.loss
>>> logits = outputs.logits
```Nr   r   r   r   r   r   r  r  r   rZ   r   r   r   r   losslogitsr   r  )rK   use_return_dictr  r  hf_compute_lossr   r   r  )rL   r   r   r   r   r   r   labelsr   r  r  r   rZ   r   r  r  r  r  r,  s                      rP   r\   *TFLayoutLMv3ForSequenceClassification.calll  s    h &1%<k$++B]B]//))%'/!5#% " 
 "!*Q1W-D~t4+?+?+OY,F)-)9TGf$EvE)!//))	
 	
rR   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       Nl= f! , (       d  f       g = f)NTr  r  )r_   r`   rW   ra   r  r<   rb   r  rd   s     rP   rb   +TFLayoutLMv3ForSequenceClassification.build      ::
4t,8t334%%d+ 54t,8t334%%d+ 54 9 54 54r6  )r_   r  rK   r  rg   NNNNNNNNNNNNF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r   r   rZ   r   r   r  rj   zTFSequenceClassifierOutput | tuple[tf.Tensor] | tuple[tf.Tensor, tf.Tensor] | tuple[tf.Tensor, tf.Tensor, tf.Tensor] | tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor]rk   )rm   rn   ro   rp   r  r?   r   r   r  r   r   r  r\   rb   rr   rs   rt   s   @rP   r  r  Z  s    +:):&T *+FG+ETcd '++/+/)-&**.#')-,0#'!%)- %O
#O
 )O
 )	O

 'O
 $O
 (O
 !O
 'O
 *O
 !O
 O
 'O
 O
 	<!O
 e H O
b	, 	,rR   r  a  
    LayoutLMv3 Model with a token classification head on top (a linear layer on top of the final hidden states) e.g.
    for sequence labeling (information extraction) tasks such as [FUNSD](https://guillaumejaume.github.io/FUNSD/),
    [SROIE](https://rrc.cvc.uab.es/?ch=13), [CORD](https://github.com/clovaai/cord) and
    [Kleister-NDA](https://github.com/applicaai/kleister-nda).
    c                     ^  \ rS rSrS/rSU 4S jjr\\" \5      \	" \
\S9             S	                           S
S jj5       5       5       rSS jrSrU =r$ )"TFLayoutLMv3ForTokenClassificationi  r   c                  > [         TU ]  " U40 UD6  UR                  U l        [        USS9U l        [
        R                  R                  UR                  SS9U l	        UR                  S:  aB  [
        R                  R                  UR                  [        UR                  5      SS9U l        O[        USS9U l        Xl        g )Nr  r+  r   r   r  r   )r>   r?   r  r  r  r   rE   r   r   r   r   r   rH   r  r  rK   r   s      rP   r?   +TFLayoutLMv3ForTokenClassification.__init__  s    *6* ++/\J||++F,F,FY+Wr!#ll00!!#263K3K#L! 1 DO =V,WDOrR   r  c                   Ub  UOU R                   R                  nU R                  UUUUUUUU	U
UUUS9nUb  [        R                  " U5      nO[        R                  " U5      SS nUS   nUS   SS2SU24   nU R                  UUS9nU R                  U5      nUc  SOU R                  UU5      nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )a  
labels (`tf.Tensor` 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
>>> from transformers import AutoProcessor, TFAutoModelForTokenClassification
>>> from datasets import load_dataset

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = TFAutoModelForTokenClassification.from_pretrained("microsoft/layoutlmv3-base", num_labels=7)

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

>>> encoding = processor(image, words, boxes=boxes, word_labels=word_labels, return_tensors="tf")

>>> outputs = model(**encoding)
>>> loss = outputs.loss
>>> logits = outputs.logits
```N)r   r   r   r   r   r   r   r  r  rZ   r   rV   r   r   r   r  )rK   r  r  rW   r   r   r  r  r	   r   r  )rL   r   r   r   r   r   r   r   r   r   r  r  rZ   r   r  re   r  r  r  r  r,  s                        rP   r\   'TFLayoutLMv3ForTokenClassification.call  s%   l &1%<k$++B]B]//))%'/!5#% " 
  ((9-K((=1#26K ^
!!*Q^4,,,J1~t4+?+?+OY,F)-)9TGf$EvE&!//))	
 	
rR   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bf  [        R                  " U R                  R
                  5         U R                  R                  S S U R                  R                  /5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)NTr  r   r  )r_   r`   rW   ra   r  r<   rb   r   r  rK   rG   rd   s     rP   rb   (TFLayoutLMv3ForTokenClassification.buildG  s    ::
4t,8t334%%d+ 54D)5t||001""4( 24t,8t334%%tT4;;3J3J&KL 54 9 54 21 54s$   E.E
3E)
E
E&)
E7)r_   r  rK   r   r  r  rg   r%  )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  rZ   r   r   r  rj   zTFTokenClassifierOutput | tuple[tf.Tensor] | tuple[tf.Tensor, tf.Tensor] | tuple[tf.Tensor, tf.Tensor, tf.Tensor] | tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor]rk   )rm   rn   ro   rp   r  r?   r   r   r  r   r	   r  r\   rb   rr   rs   rt   s   @rP   r'  r'    s    +:):&  *+FG+BQ`a '+!%+/+/)-&**.#')-,0#')- %Y
#Y
 Y
 )	Y

 )Y
 'Y
 $Y
 (Y
 !Y
 'Y
 *Y
 !Y
 'Y
 Y
 	<!Y
 b H Y
vM MrR   r'  a  
    LayoutLMv3 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 text part of the hidden-states output to
    compute `span start logits` and `span end logits`).
    c                     ^  \ rS rSrS/rSU 4S jjr\\" \5      \	" \
\S9              S	                             S
S jj5       5       5       rSS jrSrU =r$ ) TFLayoutLMv3ForQuestionAnsweringiV  r   c                   > [         TU ]  " U40 UD6  UR                  U l        [        USS9U l        [        USS9U l        g )Nr  r+  
qa_outputs)r>   r?   r  r  r  r  r1  r   s      rP   r?   )TFLayoutLMv3ForQuestionAnswering.__init__b  s>    *6* ++/\J8lSrR   r  c                   Ub  UOU R                   R                  nU R                  UUUUUUU	U
UUUUS9nUS   nU R                  UUS9n[        R
                  " USSS9u  nn[        R                  " USS9n[        R                  " USS9nSnUb  Ub  XxS	.nU R                  UUU4S
9nU(       d  UU4USS -   nUb  U4U-   $ U$ [        UUUUR                  UR                  S9$ )ac  
start_positions (`tf.Tensor` 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` 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
>>> from transformers import AutoProcessor, TFAutoModelForQuestionAnswering
>>> from datasets import load_dataset
>>> import tensorflow as tf

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = TFAutoModelForQuestionAnswering.from_pretrained("microsoft/layoutlmv3-base")

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

>>> encoding = processor(image, question, words, boxes=boxes, return_tensors="tf")
>>> start_positions = tf.convert_to_tensor([1])
>>> end_positions = tf.convert_to_tensor([3])

>>> outputs = model(**encoding, start_positions=start_positions, end_positions=end_positions)
>>> loss = outputs.loss
>>> start_scores = outputs.start_logits
>>> end_scores = outputs.end_logits
```Nr  r   r   r   rV   )r   num_or_size_splitsr   )inputr   )start_positionend_position)r  r   )r  start_logits
end_logitsr   r  )rK   r  r  r1  rW   splitsqueezer  r   r   r  )rL   r   r   r   r   r   r   start_positionsend_positionsr   r  r   rZ   r  r   r  r  r  r8  r9  r  r   r,  s                          rP   r\   %TFLayoutLMv3ForQuestionAnswering.callj  s0   B &1%<k$++B]B]//))%'/!5#% " 
 "!*8D#%88&QUW#X jzz2>ZZjr:
&=+D(7WF''j7Q'RD"J/'!"+=F)-)9TGf$EvE-%!!//))
 	
rR   c                   U R                   (       a  g SU l         [        U SS 5      bN  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        [        U SS 5      bO  [        R                  " U R                  R
                  5         U R                  R                  S 5        S S S 5        g g ! , (       d  f       Nl= f! , (       d  f       g = f)NTr  r1  )r_   r`   rW   ra   r  r<   rb   r1  rd   s     rP   rb   &TFLayoutLMv3ForQuestionAnswering.build  r$  r6  )r_   r  r  r1  rg   )NNNNNNNNNNNNNF)r   r   r   r   r   r   r   r   r   r   r   r   r<  r   r=  r   r   r  r  r  r   r   rZ   r   r  r  r   r   rj   zTFQuestionAnsweringModelOutput | tuple[tf.Tensor] | tuple[tf.Tensor, tf.Tensor] | tuple[tf.Tensor, tf.Tensor, tf.Tensor] | tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor]rk   )rm   rn   ro   rp   r  r?   r   r   r  r   r   r  r\   rb   rr   rs   rt   s   @rP   r/  r/  V  s%    +:):&T *+FG+IXgh '++/+/)-&**.,0*.)-,0!%)-#'f
#f
 )f
 )	f

 'f
 $f
 (f
 *f
 (f
 'f
 *f
 f
 'f
 !f
 f
"	<#f
 i H f
P	, 	,rR   r/  )r/  r  r'  r  r  )8rq   
__future__r   rB   r   
tensorflowrW   activations_tfr   modeling_tf_outputsr   r   r   r	   modeling_tf_utilsr
   r   r   r   r   r   r   r   tf_utilsr   utilsr   r   r   configuration_layoutlmv3r   r  _DUMMY_INPUT_IDS_DUMMY_BBOXr  rE   Layerr/   rv   r   r  r)  r8  rD  rL  rZ  r  r  LAYOUTLMV3_START_DOCSTRINGr  r  r  r  r'  r/  __all__r=   rR   rP   <module>rN     s    "    / 	 	 	 7 k k 6 %   <1')9: 'N%,,"4"4 'NT~7!3!3 ~7B|H 2 2 |H@LU\\// L<&-ELL.. &-THu||11 H<L++ L<--** --`e&%,,,, e&P J
ELL.. J
 J
Z
"3  ' RJ Z jI,3 I,	I,X.KU\\%7%7 .Kb 
 g,,GIe g,g,T  ~M)DF_ ~M~MB 
 @,'BD[ @,@,FrR   