
    Z jh                     ^   S r SSKrSSKrSSKrSSKJr  SSKJs  Jr  SSKJ	r	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJrJrJrJr  SS	KJr  SS
KJr  SSKJr  SSKJrJ r J!r!J"r"  SSK#J$r$J%r%  SSK&J'r'  SSK(J)r)  \!RT                  " \+5      r, " S S\RZ                  5      r. " S S\RZ                  5      r/ " S S\RZ                  5      r0 " S S\RZ                  5      r1 " S S\RZ                  5      r2 " S S\5      r3 " S S\RZ                  5      r4 " S S \RZ                  5      r5 " S! S"\RZ                  5      r6\ " S# S$\5      5       r7\ " S% S&\75      5       r8 " S' S(\RZ                  5      r9\" S)S*9 " S+ S,\75      5       r:\ " S- S.\75      5       r;\" S/S*9 " S0 S1\75      5       r</ S2Qr=g)3zPyTorch LayoutLMv3 model.    N)BCEWithLogitsLossCrossEntropyLossMSELoss   )initialization)ACT2FN)GradientCheckpointingLayer)BaseModelOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel)Unpack)apply_chunking_to_forward)auto_docstringcan_return_tuplelogging	torch_int)TransformersKwargsmerge_with_config_defaults)capture_outputs   )LayoutLMv3Configc                   6   ^  \ rS rSrSrU 4S jrSS jrSrU =r$ )LayoutLMv3PatchEmbeddings2   zLayoutLMv3 image (patch) embeddings. This class also automatically interpolates the position embeddings for varying
image sizes.c                   > [         TU ]  5         [        UR                  [        R
                  R                  5      (       a  UR                  OUR                  UR                  4n[        UR                  [        R
                  R                  5      (       a  UR                  OUR                  UR                  4nUS   US   -  US   US   -  4U l        [        R                  " UR                  UR                  X3S9U l        g )Nr   r   )kernel_sizestride)super__init__
isinstance
input_sizecollectionsabcIterable
patch_sizepatch_shapennConv2dnum_channelshidden_sizeproj)selfconfig
image_sizer'   	__class__s       ڃ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/layoutlmv3/modeling_layoutlmv3.pyr!   "LayoutLMv3PatchEmbeddings.__init__6   s     &++[__-E-EFF ##V%6%67 	 &++[__-E-EFF ##V%6%67 	
 'qMZ]:JqMZXY]<Z[IIf1163E3ES]q	    c                 d   U R                  U5      nUbz  UR                  SU R                  S   U R                  S   S5      nUR                  SSSS5      nUR                  S   UR                  S   pT[
        R                  " X$U4SS9nX2-   nUR                  S5      R                  SS5      nU$ )Nr   r   r      bicubic)sizemode)	r-   viewr(   permuteshapeFinterpolateflatten	transpose)r.   pixel_valuesposition_embedding
embeddingspatch_heightpatch_widths         r2   forward!LayoutLMv3PatchEmbeddings.forwardF   s    YY|,
)!3!8!8D<L<LQ<OQUQaQabcQdfh!i!3!;!;Aq!Q!G(2(8(8(;Z=M=Ma=P+!"/AWbHcjs!t#8J''*44Q:
r4   )r(   r-   N	__name__
__module____qualname____firstlineno____doc__r!   rG   __static_attributes____classcell__r1   s   @r2   r   r   2   s    r  r4   r   c                   R   ^  \ rS rSrSrU 4S jrS rS rS r     S	S jr	Sr
U =r$ )
LayoutLMv3TextEmbeddingsU   ze
LayoutLMv3 text embeddings. Same as `RobertaEmbeddings` but with added spatial (layout) embeddings.
c                 *  > [         TU ]  5         [        R                  " UR                  UR
                  UR                  S9U l        [        R                  " UR                  UR
                  5      U l	        [        R                  " UR
                  UR                  S9U l
        [        R                  " UR                  5      U l        U R                  S[         R"                  " UR$                  5      R'                  S5      SS9  UR                  U l        [        R                  " UR$                  UR
                  U R(                  S9U l        [        R                  " UR,                  UR.                  5      U l        [        R                  " UR,                  UR.                  5      U l        [        R                  " UR,                  UR4                  5      U l        [        R                  " UR,                  UR4                  5      U l        g )N)padding_idxepsposition_idsr   r6   F
persistent)r    r!   r)   	Embedding
vocab_sizer,   pad_token_idword_embeddingstype_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsDropouthidden_dropout_probdropoutregister_buffertorcharangemax_position_embeddingsexpandrW   position_embeddingsmax_2d_position_embeddingscoordinate_sizex_position_embeddingsy_position_embeddings
shape_sizeh_position_embeddingsw_position_embeddingsr.   r/   r1   s     r2   r!   !LayoutLMv3TextEmbeddings.__init__Z   s}   !||F,=,=v?Q?Q_e_r_rs%'\\&2H2H&J\J\%]"f&8&8f>S>STzz&"<"<= 	ELL)G)GHOOPWXej 	 	
 "..#%<<**F,>,>DL\L\$
  &(\\&2S2SU[UkUk%l"%'\\&2S2SU[UkUk%l"%'\\&2S2SU[UfUf%g"%'\\&2S2SU[UfUf%g"r4   c           	      J    U R                  US S 2S S 2S4   5      nU R                  US S 2S S 2S4   5      nU R                  US S 2S S 2S4   5      nU R                  US S 2S S 2S4   5      nU R                  [        R
                  " US S 2S S 2S4   US S 2S S 2S4   -
  SS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S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)	Nr   r   r7   r   z;The `bbox` coordinate values should be within 0-1000 range.i  r6   dim)rq   rr   
IndexErrorrt   rj   clipru   cat)
r.   bboxleft_position_embeddingsupper_position_embeddingsright_position_embeddingslower_position_embeddingsert   ru   spatial_position_embeddingss
             r2   %calculate_spatial_position_embeddings>LayoutLMv3TextEmbeddings.calculate_spatial_position_embeddingsq   sN   	c'+'A'A$q!Qw-'P$(,(B(B41a=(Q%(,(B(B41a=(Q%(,(B(B41a=(Q% !% : :5::d1aQR7mVZ[\^_ab[bVcFcefhl;m n $ : :5::d1aQR7mVZ[\^_ab[bVcFcefhl;m n ',ii()))%% 
'
# +*%  	cZ[abb	cs   A,D 
D"DD"c                     UR                  U5      R                  5       n[        R                  " USS9R	                  U5      U-  nUR                  5       U-   $ )z
Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding
symbols are ignored. This is modified from fairseq's `utils.make_positions`.
r   ry   )neintrj   cumsumtype_aslong)r.   	input_idsrW   maskincremental_indicess        r2   "create_position_ids_from_input_ids;LayoutLMv3TextEmbeddings.create_position_ids_from_input_ids   sP     ||K(,,.$||Da8@@F$N"'')K77r4   c                    UR                  5       SS nUS   n[        R                  " U R                  S-   X0R                  -   S-   [        R                  UR
                  S9nUR                  S5      R                  U5      $ )zq
We are provided embeddings directly. We cannot infer which are padded so just generate sequential position ids.
Nr6   r   dtypedevicer   )r9   rj   rk   rW   r   r   	unsqueezerm   )r.   inputs_embedsinput_shapesequence_lengthrZ   s        r2   &create_position_ids_from_inputs_embeds?LayoutLMv3TextEmbeddings.create_position_ids_from_inputs_embeds   s~     $((*3B/%a.||q /4D4D"Dq"HPUPZPZcpcwcw
 %%a(//<<r4   c                 L   UcI  Ub5  U R                  XR                  5      R                  UR                  5      nOU R	                  U5      nUb  UR                  5       nOUR                  5       S S nUc8  [        R                  " U[        R                  U R                  R                  S9nUc  U R                  U5      nU R                  U5      nXW-   nU R                  U5      n	X-  nU R                  U5      n
X-   nU R                  U5      nU R                  U5      nU$ )Nr6   r   )r   rW   tor   r   r9   rj   zerosr   rZ   ra   rc   rn   r   rd   rh   )r.   r   r~   token_type_idsrZ   r   r   rc   rD   rn   r   s              r2   rG    LayoutLMv3TextEmbeddings.forward   s    $#FFyRbRbcff$$   $JJ=Y #..*K',,.s3K!"[[EJJtO`O`OgOghN  00;M $ : :> J":
"66|D)
&*&P&PQU&V#=
^^J/
\\*-
r4   )
rd   rh   rt   rW   rn   rc   ru   ra   rq   rr   NNNNN)rK   rL   rM   rN   rO   r!   r   r   r   rG   rP   rQ   rR   s   @r2   rT   rT   U   s8    h.+48
= ' 'r4   rT   c                   P   ^  \ rS rSrU 4S jrSS jr   SS\\   4S jjrSr	U =r
$ )	LayoutLMv3SelfAttention   c                 $  > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eUR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  5      U l        UR"                  U l        UR$                  U l        g )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ())r    r!   r,   num_attention_headshasattr
ValueErrorr   attention_head_sizeall_head_sizer)   Linearquerykeyvaluerf   attention_probs_dropout_probrh   has_relative_attention_biashas_spatial_attention_biasrv   s     r2   r!    LayoutLMv3SelfAttention.__init__   s;    : ::a?PVXhHiHi#F$6$6#7 8 445Q8 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PPYYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF+1+M+M(*0*K*K'r4   c                     X-  nUR                  SS9R                  S5      nX4-
  U-  n[        R                  " SS9" U5      $ )a  
https://huggingface.co/papers/2105.13290 Section 2.4 Stabilization of training: Precision Bottleneck Relaxation
(PB-Relax). A replacement of the original nn.Softmax(dim=-1)(attention_scores). Seems the new attention_probs
will result in a slower speed and a little bias. Can use torch.allclose(standard_attention_probs,
cogview_attention_probs, atol=1e-08) for comparison. The smaller atol (e.g., 1e-08), the better.
r6   ry   )amaxr   r)   Softmax)r.   attention_scoresalphascaled_attention_scores	max_valuenew_attention_scoress         r2   cogview_attention)LayoutLMv3SelfAttention.cogview_attention   sN     #3":+00b0:DDRH	 7 CuLzzb!"677r4   kwargsc                    UR                   S   nU R                  U5      R                  USU R                  U R                  5      R                  SS5      nU R                  U5      R                  USU R                  U R                  5      R                  SS5      nU R                  U5      R                  USU R                  U R                  5      R                  SS5      n	[        R                  " U[        R                  " U R                  5      -  UR                  SS5      5      n
U R                  (       a:  U R                  (       a)  XU-   [        R                  " U R                  5      -  -  n
O6U R                  (       a%  X[        R                  " U R                  5      -  -  n
Ub  X-   n
U R                  U
5      nU R                  U5      n[        R                  " X5      nUR!                  SSSS5      R#                  5       nUR%                  5       S S U R&                  4-   nUR                  " U6 nX4$ )Nr   r6   r   r7   r   )r=   r   r;   r   r   rA   r   r   rj   matmulmathsqrtr   r   r   rh   r<   
contiguousr9   r   )r.   hidden_statesattention_maskrel_pos
rel_2d_posr   
batch_sizequery_layer	key_layervalue_layerr   attention_probscontext_layernew_context_layer_shapes                 r2   rG   LayoutLMv3SelfAttention.forward   s    #((+
JJ}%T*b$":":D<T<TUYq!_ 	 HH]#T*b$":":D<T<TUYq!_ 	 JJ}%T*b$":":D<T<TUYq!_ 	 !<<dii@X@X6Y(Y[d[n[noqsu[vw++0O0O:!54C[C[9\ \\--$))D4L4L*M MM%/@ 001AB ,,7_B%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**,CD--r4   )	r   r   rh   r   r   r   r   r   r   )    NNN)rK   rL   rM   rN   r!   r   r   r   rG   rP   rQ   rR   s   @r2   r   r      s3    L(
8 5. +,5. 5.r4   r   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )LayoutLMv3SelfOutputi%  c                 (  > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " UR                  UR                  S9U l        [        R                  " UR                  5      U l
        g NrX   )r    r!   r)   r   r,   denserd   re   rf   rg   rh   rv   s     r2   r!   LayoutLMv3SelfOutput.__init__&  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=r4   r   input_tensorreturnc                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ rI   r   rh   rd   r.   r   r   s      r2   rG   LayoutLMv3SelfOutput.forward,  5    

=1]3}'CDr4   rd   r   rh   
rK   rL   rM   rN   r!   rj   TensorrG   rP   rQ   rR   s   @r2   r   r   %  6    >U\\  RWR^R^  r4   r   c                   F   ^  \ rS rSrU 4S jr   SS\\   4S jjrSrU =r	$ )LayoutLMv3Attentioni4  c                 b   > [         TU ]  5         [        U5      U l        [	        U5      U l        g rI   )r    r!   r   r.   r   outputrv   s     r2   r!   LayoutLMv3Attention.__init__5  s&    +F3	*62r4   r   c                 ^    UnU R                   " UU4UUS.UD6u  pxU R                  Xv5      nU$ Nr   r   )r.   r   )	r.   r   r   r   r   r   residualattention_output_s	            r2   rG   LayoutLMv3Attention.forward:  sP     !"ii
 !	

 
  ;;'7Br4   )r   r.   r   )
rK   rL   rM   rN   r!   r   r   rG   rP   rQ   rR   s   @r2   r   r   4  s-    3   +,   r4   r   c                   N   ^  \ rS rSrU 4S jr    SS\\   4S jjrS rSr	U =r
$ )LayoutLMv3LayeriO  c                    > [         TU ]  5         UR                  U l        SU l        [	        U5      U l        [        U5      U l        [        U5      U l	        g Nr   )
r    r!   chunk_size_feed_forwardseq_len_dimr   	attentionLayoutLMv3IntermediateintermediateLayoutLMv3Outputr   rv   s     r2   r!   LayoutLMv3Layer.__init__P  sI    '-'E'E$,V426:&v.r4   r   c                     U R                  UUUUS9n[        U R                  U R                  U R                  U5      nU$ r   )r   r   feed_forward_chunkr   r   )	r.   r   r   output_attentionsr   r   r   r   layer_outputs	            r2   rG   LayoutLMv3Layer.forwardX  sT      >>!	 * 
 1##T%A%A4CSCSUe
 r4   c                 J    U R                  U5      nU R                  X!5      nU$ rI   )r   r   )r.   r   intermediate_outputr   s       r2   r   "LayoutLMv3Layer.feed_forward_chunkn  s)    "//0@A{{#6Ir4   )r   r   r   r   r   )NFNN)rK   rL   rM   rN   r!   r   r   rG   r   rP   rQ   rR   s   @r2   r   r   O  s5    /  +,, r4   r   c                   `   ^  \ rS rSrU 4S jrS	S jrS rS r     S
S\\	   4S jjr
SrU =r$ )LayoutLMv3Encoderit  c                   > [         TU ]  5         Xl        [        R                  " [        UR                  5       Vs/ s H  n[        U5      PM     sn5      U l        SU l	        UR                  U l
        UR                  U l        U R                  (       aQ  UR                  U l        UR                  U l        [        R                  " U R                  UR                  SS9U l        U R                  (       a  UR"                  U l        UR$                  U l        [        R                  " U R$                  UR                  SS9U l        [        R                  " U R$                  UR                  SS9U l        g g s  snf )NF)bias)r    r!   r/   r)   
ModuleListrangenum_hidden_layersr   layergradient_checkpointingr   r   rel_pos_binsmax_rel_posr   r   rel_pos_biasmax_rel_2d_posrel_2d_pos_binsrel_pos_x_biasrel_pos_y_bias)r.   r/   r   r1   s      r2   r!   LayoutLMv3Encoder.__init__u  s#   ]]U6KcKcEd#eEdOF$;Ed#ef
&+#+1+M+M(*0*K*K'++ & 3 3D%11D "		$*;*;V=W=W^c dD**"("7"7D#)#9#9D "$))D,@,@&B\B\ch"iD"$))D,@,@&B\B\ch"iD	 + $fs   E<c                 D   SnU(       a4  US-  nXQS:  R                  5       U-  -  n[        R                  " U5      nO,[        R                  " U* [        R                  " U5      5      nUS-  nXg:  nU[        R
                  " UR                  5       U-  5      [        R
                  " XG-  5      -  X7-
  -  R                  [        R                   5      -   n	[        R                  " U	[        R                  " XS-
  5      5      n	U[        R                  " XU	5      -  nU$ )Nr   r7   r   )r   rj   absmax
zeros_likelogfloatr   r   min	full_likewhere)
r.   relative_positionbidirectionalnum_bucketsmax_distanceretn	max_exactis_smallval_if_larges
             r2   relative_position_bucket*LayoutLMv3Encoder.relative_position_bucket  s    AK)//1K??C		+,A		,,e.>.>?P.QRA  1$	= !IIaggi)+,txx8P/QQU`Ulm
"UZZ. yyu|[\_/]^u{{855
r4   c                    UR                  S5      UR                  S5      -
  nU R                  UU R                  U R                  S9n[        R
                  " 5          U R                  R                  R                  5       U   R                  SSSS5      nS S S 5        UR                  5       nU$ ! , (       d  f       N = f)Nr   r6   r   r!  r   r   r   r7   )r   r'  r  r  rj   no_gradr  weighttr<   r   )r.   rZ   rel_pos_matr   s       r2   _cal_1d_pos_emb!LayoutLMv3Encoder._cal_1d_pos_emb  s    ",,R0<3I3I"3MM//)))) 0 
 ]]_''..0027;CCAq!QOG $$& _s   :B22
C c                    US S 2S S 2S4   nUS S 2S S 2S4   nUR                  S5      UR                  S5      -
  nUR                  S5      UR                  S5      -
  nU R                  UU R                  U R                  S9nU R                  UU R                  U R                  S9n[        R
                  " 5          U R                  R                  R                  5       U   R                  SSSS5      nU R                  R                  R                  5       U   R                  SSSS5      nS S S 5        UR                  5       nUR                  5       nXg-   nU$ ! , (       d  f       N4= f)Nr   r   r   r6   r*  r   r7   )r   r'  r  r  rj   r+  r  r,  r-  r<   r  r   )	r.   r~   position_coord_xposition_coord_yrel_pos_x_2d_matrel_pos_y_2d_mat	rel_pos_x	rel_pos_yr   s	            r2   _cal_2d_pos_emb!LayoutLMv3Encoder._cal_2d_pos_emb  sf   1a=1a=+55b9<L<V<VWY<ZZ+55b9<L<V<VWY<ZZ11,,,, 2 
	
 11,,,, 2 
	 ]]_++22446yAII!QPQSTUI++22446yAII!QPQSTUI  ((*	((*	*
 _s   >A3E
E-r   c                     U R                   (       a  U R                  U5      OS nU R                  (       a  U R                  U5      OS n	U R                   H  n
U
" UU4UU	S.UD6nM     [        US9$ )Nr   last_hidden_state)r   r/  r   r8  r  r
   )r.   r   r~   r   rZ   rE   rF   r   r   r   layer_modules              r2   rG   LayoutLMv3Encoder.forward  s|     9=8X8X$&&|4^b373R3RT))$/X\
 JJL(  %	
 M ' ??r4   )r/   r  r   r   r  r  r  r  r  r  r  r  )Tr      r   )rK   rL   rM   rN   r!   r'  r/  r8  r   r   rG   rP   rQ   rR   s   @r2   r  r  t  sF    j(."< @ +,@ @r4   r  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )r   i  c                   > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        UR                  [        5      (       a  [        UR                     U l        g UR                  U l        g rI   )r    r!   r)   r   r,   intermediate_sizer   r"   
hidden_actstrr   intermediate_act_fnrv   s     r2   r!   LayoutLMv3Intermediate.__init__  s`    YYv1163K3KL
f''--'-f.?.?'@D$'-'8'8D$r4   r   r   c                 J    U R                  U5      nU R                  U5      nU$ rI   r   rE  )r.   r   s     r2   rG   LayoutLMv3Intermediate.forward  s&    

=100?r4   rH  r   rR   s   @r2   r   r     s(    9U\\ ell  r4   r   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )r   i  c                 (  > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        R                  " UR
                  UR                  S9U l        [        R                  " UR                  5      U l        g r   )r    r!   r)   r   rB  r,   r   rd   re   rf   rg   rh   rv   s     r2   r!   LayoutLMv3Output.__init__  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=r4   r   r   r   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ rI   r   r   s      r2   rG   LayoutLMv3Output.forward  r   r4   r   r   rR   s   @r2   r   r     r   r4   r   c                   n   ^  \ rS rSr% \\S'   SrSr\\	S.r
\R                  " 5       U 4S j5       rSrU =r$ )LayoutLMv3PreTrainedModeli  r/   
layoutlmv3)imagetext)r   
attentionsc                   > [         TU ]  U5        [        U[        5      (       a  U R                  R
                  (       a@  [        R                  " UR                  5        [        R                  " UR                  5        [        US5      (       aE  [        R                  " UR                  UR                  UR                  UR                  4S95        gg[        U[        5      (       a\  [        R                  " UR                   ["        R$                  " UR                   R&                  S   5      R)                  S5      5        gg)zInitialize the weightsvisual_bboxr0   r6   r[   N)r    _init_weightsr"   LayoutLMv3Modelr/   visual_embedinitzeros_	cls_token	pos_embedr   copy_rV  create_visual_bboxr9   rT   rZ   rj   rk   r=   rm   )r.   moduler1   s     r2   rX  'LayoutLMv3PreTrainedModel._init_weights  s     	f%fo..{{''F,,-F,,-v}--

6--v/H/HU[U`U`bhbmbmTn/H/op . 899JJv**ELL9L9L9R9RSU9V,W,^,^_f,gh :r4    )rK   rL   rM   rN   r   __annotations__base_model_prefixinput_modalitiesr   r   _can_record_outputsrj   r+  rX  rP   rQ   rR   s   @r2   rP  rP    s;    $(,;Kbc
]]_
i 
ir4   rP  c                   j  ^  \ rS rSrU 4S jrS rS rSS jrS rS r	\
\\       SS	\R                  S-  S
\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\\   S\\-  4S jj5       5       5       rSrU =r$ )rY  i  c                 X  > [         TU ]  U5        Xl        UR                  (       a  [	        U5      U l        UR                  (       Ga  [        U5      U l        [        UR                  UR                  -  5      U l        [        R                  " [        R                   " SSUR"                  5      5      U l        [        R                  " [        R                   " SU R                  U R                  -  S-   UR"                  5      5      U l        [        R(                  " SS9U l        [        R,                  " UR"                  UR.                  S9U l        [        R(                  " UR0                  5      U l        U R                  R4                  (       d  U R                  R6                  (       a4  U R9                  SU R;                  U R                  U R                  4S9SS9  [        R,                  " UR"                  S	S9U l        [?        U5      U l         U RC                  5         g )
Nr   g        )prX   rV  rW  Fr\   gư>)"r    r!   r/   
text_embedrT   rD   rZ  r   patch_embedr   r#   r'   r9   r)   	Parameterrj   r   r,   r]  r^  rf   pos_droprd   re   rg   rh   r   r   ri   r`  normr  encoder	post_initrv   s     r2   r!   LayoutLMv3Model.__init__  s    6v>DO  9@DF--0A0AABDI\\%++aF<N<N*OPDN\\%++aTYY9NQR9RTZTfTf*ghDNJJ-DM\\&*<*<&BWBWXDN::f&@&@ADL{{66$++:`:`$$!4#:#:tyyRVR[R[F\#:#]jo %  V%7%7TBDI(0r4   c                 .    U R                   R                  $ rI   rD   ra   r.   s    r2   get_input_embeddings$LayoutLMv3Model.get_input_embeddings:  s    ...r4   c                 $    XR                   l        g rI   rt  r.   r   s     r2   set_input_embeddings$LayoutLMv3Model.set_input_embeddings=  s    */'r4   c           	         [         R                  " [         R                  " SX!S   S-   -  U5      US   SS9n[         R                  " [         R                  " SX!S   S-   -  U5      US   SS9n[         R                  " USS R	                  US   S5      USS R	                  US   S5      R                  SS5      USS R	                  US   S5      USS R	                  US   S5      R                  SS5      /SS9R                  SS5      n[         R                  " SSUS-
  US-
  //5      n[         R                  " Xe/SS9$ )	z:
Create the bounding boxes for the visual (patch) tokens.
r   r   trunc)rounding_modeNr6   ry      )	rj   divrk   stackrepeatrA   r;   tensorr}   )r.   r0   max_lenvisual_bbox_xvisual_bbox_yrV  cls_token_boxs          r2   r`  "LayoutLMv3Model.create_visual_bbox@  sV    		LLG!}q'897CZPQ]bi
 		LLG!}q'897CZPQ]bi
 kkcr"))*Q-;cr"))*Q-;EEaKab!((A:ab!((A:DDQJ	 
 $r1+ 	 ueWq['A+&N%OPyy-51==r4   c                     U R                   R                  USS5      nUR                  U5      R                  U5      nU$ r   )rV  r  r   type)r.   r   r   r   rV  s        r2   calculate_visual_bbox%LayoutLMv3Model.calculate_visual_bboxW  s;    &&--j!Q?!nnV,11%8r4   c                 .   U R                  U5      nUR                  5       u  p4nU R                  R                  USS5      n[        R
                  " Xb4SS9nU R                  b  X R                  -   nU R                  U5      nU R                  U5      nU$ )Nr6   r   ry   )	rl  r9   r]  rm   rj   r}   r^  rn  ro  )r.   rB   rD   r   seq_lenr   
cls_tokenss          r2   forward_imageLayoutLMv3Model.forward_image\  s    %%l3
 ",!2
Q^^**:r2>
YY
7Q?
 >>%#nn4J]]:.
YYz*
r4   Nr   r~   r   r   rZ   r   rB   r   r   c           	      T   Ub!  UR                  5       n	U	u  pUR                  nOMUb$  UR                  5       SS n	U	u  pUR                  nO&Ub  [        U5      n
UR                  nO[        S5      eUc  Ub  Uc  [        R
                  " U
W4US9nUc$  [        R                  " W	[        R                  US9nUc:  [        R                  " [        [        W	5      S/-   5      [        R                  US9nU R                  UUUUUS9nS=pS=nnUGb*  [        UR                  S   U R                  R                  -  5      [        UR                  S	   U R                  R                  -  5      nnU R                  U5      n[        R
                  " U
UR                  S
   4[        R                  US9nUb  [        R                   " UU/S
S9nOUnU R                  R"                  (       d  U R                  R$                  (       a  U R                  R$                  (       a<  U R'                  U[        R                  U
S9nUb  [        R                   " UU/S
S9nOUn[        R(                  " SUR                  S
   [        R                  US9R+                  U
S
5      nUc  UbQ  [        R(                  " SW	S
   US9R-                  S5      nUR/                  U	5      n[        R                   " UU/S
S9nOUnUc  Ub  [        R                   " WU/S
S9nOUnU R1                  U5      nU R3                  U5      nOU R                  R"                  (       d  U R                  R$                  (       am  U R                  R$                  (       a  UnU R                  R"                  (       a5  U R                  R4                  SS2SW	S
   24   nUR7                  U5      nUnU R9                  USWR:                  S9nU R<                  " U4UUUUUS.UD6nUR>                  n[A        US9$ )a$  
input_ids (`torch.LongTensor` of shape `(batch_size, token_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 (`torch.LongTensor` of shape `(batch_size, token_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`.
token_type_ids (`torch.LongTensor` of shape `(batch_size, token_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 (`torch.LongTensor` of shape `(batch_size, token_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)
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, token_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.

Examples:

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

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = AutoModel.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="pt")

>>> outputs = model(**encoding)
>>> last_hidden_states = outputs.last_hidden_state
```Nr6   zEYou have to specify either input_ids or inputs_embeds or pixel_values)r   r   r  )r   r~   rZ   r   r   r7   r   r   ry   )r   r   r   )r   )r~   rZ   r   rE   rF   r;  )!r9   r   lenr   rj   onesr   r   tuplelistrD   r   r=   r/   r'   r  r}   r   r   r  rk   r  r   rm   rd   rh   rZ   	expand_asget_extended_attention_maskr   rp  r<  r
   )r.   r   r~   r   r   rZ   r   rB   r   r   r   
seq_lengthr   embedding_output
final_bboxfinal_position_idsrE   rF   visual_embeddingsvisual_attention_maskrV  visual_position_idsextended_attention_maskencoder_outputssequence_outputs                            r2   rG   LayoutLMv3Model.forwardm  s   Z  #..*K%0"J%%F&',,.s3K%0"J"))F%\*J!((Fdee M$=%!&j*-Ev!V%!&[

SY!Z|{{5k):aS)@#A\bc##)-+  /   +/.
%)){#,,,Q/$++2H2HHI,,,Q/$++2H2HHI &L !% 2 2< @$)JJ.44Q78

SY%! )!&N<Q+RXY!Z!6{{66$++:`:`;;99"&"<"<V5::bl"<"mK'%*YYk/B%J
%0
&+ll(..q1F'&Q' $ (M,E#(<<;q>&#Q#[#[\]#^L#/#6#6{#CL).LBU3V\])^&)<&$(A#(99.>@Q-RXY#Z #4 #~~.>?#||,<=[[448^8^{{55!
{{66#;;A?OQ?O<OP+55i@%1"040P0PD(8(>(> 1Q 1
 ,,
+2%#
 
 *;;-
 	
r4   )rd   r]  r/   rh   rD   rp  ro  rl  rn  r^  r9   ))   r  i  )NNNNNNN)rK   rL   rM   rN   r!   rv  rz  r`  r  r  r   r   r   rj   
LongTensorFloatTensorr   r   r  r
   rG   rP   rQ   rR   s   @r2   rY  rY    s   >/0>.
"   .2(,3726042615k
##d*k
 %k
 ))D0	k

 ((4/k
 &&-k
 ((4/k
 ''$.k
 +,k
 
	 k
    k
r4   rY  c                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )LayoutLMv3ClassificationHeadi  zT
Head for sentence-level classification tasks. Reference: RobertaClassificationHead
c                   > [         TU ]  5         X l        U(       a4  [        R                  " UR
                  S-  UR
                  5      U l        O0[        R                  " UR
                  UR
                  5      U l        UR                  b  UR                  OUR                  n[        R                  " U5      U l
        [        R                  " UR
                  UR                  5      U l        g )Nr   )r    r!   pool_featurer)   r   r,   r   classifier_dropoutrg   rf   rh   
num_labelsout_proj)r.   r/   r  r  r1   s       r2   r!   %LayoutLMv3ClassificationHead.__init__#  s    (6#5#5#96;M;MNDJ6#5#5v7I7IJDJ)/)B)B)NF%%TZTnTn 	 zz"45		&"4"4f6G6GHr4   c                     U R                  U5      nU R                  U5      n[        R                  " U5      nU R                  U5      nU R	                  U5      nU$ rI   )rh   r   rj   tanhr  )r.   xs     r2   rG   $LayoutLMv3ClassificationHead.forward0  sI    LLOJJqMJJqMLLOMM!r4   )r   rh   r  r  )FrJ   rR   s   @r2   r  r    s    I r4   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).
    )custom_introc                   j  ^  \ rS rSrU 4S jrS rS r\\        SS\	R                  S-  S\	R                  S-  S\	R                  S-  S	\	R                  S-  S
\	R                  S-  S\	R                  S-  S\	R                  S-  S\	R                  S-  S\\   S\\-  4S jj5       5       rSrU =r$ ) LayoutLMv3ForTokenClassificationi9  c                 p  > [         TU ]  U5        UR                  U l        [        U5      U l        [
        R                  " UR                  5      U l        UR                  S:  a1  [
        R                  " UR                  UR                  5      U l        O[        USS9U l        U R                  5         g )N
   Fr  )r    r!   r  rY  rQ  r)   rf   rg   rh   r   r,   
classifierr  rq  rv   s     r2   r!   )LayoutLMv3ForTokenClassification.__init__B  s      ++)&1zz&"<"<=r! ii(:(:F<M<MNDO:6PUVDOr4   c                 6    U R                   R                  5       $ rI   rQ  rv  ru  s    r2   rv  5LayoutLMv3ForTokenClassification.get_input_embeddingsO      3355r4   c                 :    U R                   R                  U5        g rI   rQ  rz  ry  s     r2   rz  5LayoutLMv3ForTokenClassification.set_input_embeddingsR      ,,U3r4   Nr   r~   r   r   rZ   r   labelsrB   r   r   c	           
         U R                   " U4UUUUUUS.U	D6n
Ub  UR                  5       nOUR                  5       SS nUS   nU
S   SS2SU24   nU R                  U5      nU R                  U5      nSnUb<  [	        5       nU" UR                  SU R                  5      UR                  S5      5      n[        UUU
R                  U
R                  S9$ )aa  
bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
    Bounding boxes of each input sequence tokens. Selected in the range `[0,
    config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
    format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
    y1) represents the position of the lower right corner.
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.

Examples:

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

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = AutoModelForTokenClassification.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="pt")

>>> outputs = model(**encoding)
>>> loss = outputs.loss
>>> logits = outputs.logits
```)r~   r   r   rZ   r   rB   Nr6   r   r   losslogitsr   rT  )
rQ  r9   rh   r  r   r;   r  r   r   rT  )r.   r   r~   r   r   rZ   r   r  rB   r   outputsr   r  r  r  r  loss_fcts                    r2   rG   (LayoutLMv3ForTokenClassification.forwardU  s    Z //	
))%'%	
 	
  #..*K',,.s3K ^
!!*Q^4,,71')HFKKDOO<fkk"oND$!//))	
 	
r4   )r  rh   rQ  r  NNNNNNNN)rK   rL   rM   rN   r!   rv  rz  r   r   rj   r  r  r   r   r  r   rG   rP   rQ   rR   s   @r2   r  r  9  s   64  .2(,37260426*.04J
##d*J
 %J
 ))D0	J

 ((4/J
 &&-J
 ((4/J
   4'J
 &&-J
 +,J
 
&	&J
  J
r4   r  c                     ^  \ rS rSrU 4S jrS rS r\\         SS\	R                  S-  S\	R                  S-  S\	R                  S-  S	\	R                  S-  S
\	R                  S-  S\	R                  S-  S\	R                  S-  S\	R                  S-  S\	R                  S-  S\\   S\\-  4S jj5       5       rSrU =r$ )LayoutLMv3ForQuestionAnsweringi  c                    > [         TU ]  U5        UR                  U l        [        U5      U l        [        USS9U l        U R                  5         g NFr  )r    r!   r  rY  rQ  r  
qa_outputsrq  rv   s     r2   r!   'LayoutLMv3ForQuestionAnswering.__init__  sA      ++)&16vERr4   c                 6    U R                   R                  5       $ rI   r  ru  s    r2   rv  3LayoutLMv3ForQuestionAnswering.get_input_embeddings  r  r4   c                 :    U R                   R                  U5        g rI   r  ry  s     r2   rz  3LayoutLMv3ForQuestionAnswering.set_input_embeddings  r  r4   Nr   r   r   rZ   r   start_positionsend_positionsr~   rB   r   r   c
           
         U R                   " U4UUUUUU	S.U
D6nUS   nU R                  U5      nUR                  SSS9u  pUR                  S5      R	                  5       nUR                  S5      R	                  5       nSnUb  Ub  [        UR                  5       5      S:  a  UR                  S5      n[        UR                  5       5      S:  a  UR                  S5      nUR                  S5      nUR                  SU5      nUR                  SU5      n[        US9nU" X5      nU" X5      nUU-   S-  n[        UUUUR                  UR                  S	9$ )
aG  
bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
    Bounding boxes of each input sequence tokens. Selected in the range `[0,
    config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
    format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
    y1) represents the position of the lower right corner.

Examples:

```python
>>> from transformers import AutoProcessor, AutoModelForQuestionAnswering
>>> from datasets import load_dataset
>>> import torch

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = AutoModelForQuestionAnswering.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="pt")
>>> start_positions = torch.tensor([1])
>>> end_positions = torch.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
```r   r   rZ   r   r~   rB   r   r   r6   ry   N)ignore_indexr7   )r  start_logits
end_logitsr   rT  )rQ  r  splitsqueezer   r  r9   clampr   r   r   rT  )r.   r   r   r   rZ   r   r  r  r~   rB   r   r  r  r  r  r  
total_lossignored_indexr  
start_lossend_losss                        r2   rG   &LayoutLMv3ForQuestionAnswering.forward  s|   ` $(??	$
))%'%	$
 	$
 "!*1#)<<r<#: #++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
:H$x/14J+%!!//))
 	
r4   )rQ  r  r  )	NNNNNNNNN)rK   rL   rM   rN   r!   rv  rz  r   r   rj   r  r  r   r   r  r   rG   rP   rQ   rR   s   @r2   r  r    s3   64  .2372604263715(,04W
##d*W
 ))D0W
 ((4/	W

 &&-W
 ((4/W
 ))D0W
 ''$.W
 %W
 &&-W
 +,W
 
-	-W
  W
r4   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                   j  ^  \ rS rSrU 4S jrS rS r\\        SS\	R                  S-  S\	R                  S-  S\	R                  S-  S	\	R                  S-  S
\	R                  S-  S\	R                  S-  S\	R                  S-  S\	R                  S-  S\\   S\\-  4S jj5       5       rSrU =r$ )#LayoutLMv3ForSequenceClassificationi  c                    > [         TU ]  U5        UR                  U l        Xl        [	        U5      U l        [        USS9U l        U R                  5         g r  )	r    r!   r  r/   rY  rQ  r  r  rq  rv   s     r2   r!   ,LayoutLMv3ForSequenceClassification.__init__  sF      ++)&16vERr4   c                 6    U R                   R                  5       $ rI   r  ru  s    r2   rv  8LayoutLMv3ForSequenceClassification.get_input_embeddings"  r  r4   c                 :    U R                   R                  U5        g rI   r  ry  s     r2   rz  8LayoutLMv3ForSequenceClassification.set_input_embeddings%  r  r4   Nr   r   r   rZ   r   r  r~   rB   r   r   c	           
         U R                   " U4UUUUUUS.U	D6n
U
S   SS2SSS24   nU R                  U5      nSnUGb  U R                  R                  c  U R                  S:X  a  SU R                  l        OoU R                  S:  aN  UR
                  [        R                  :X  d  UR
                  [        R                  :X  a  SU R                  l        OSU R                  l        U R                  R                  S:X  aI  [        5       nU R                  S:X  a&  U" UR                  5       UR                  5       5      nOU" X5      nOU R                  R                  S:X  a=  [        5       nU" UR                  SU R                  5      UR                  S5      5      nO,U R                  R                  S:X  a  [        5       nU" X5      n[        UUU
R                  U
R                   S	9$ )
a  
bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*):
    Bounding boxes of each input sequence tokens. Selected in the range `[0,
    config.max_2d_position_embeddings-1]`. Each bounding box should be a normalized version in (x0, y0, x1, y1)
    format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1,
    y1) represents the position of the lower right corner.

Examples:

```python
>>> from transformers import AutoProcessor, AutoModelForSequenceClassification
>>> from datasets import load_dataset
>>> import torch

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = AutoModelForSequenceClassification.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="pt")
>>> sequence_label = torch.tensor([1])

>>> outputs = model(**encoding, labels=sequence_label)
>>> loss = outputs.loss
>>> logits = outputs.logits
```r  r   Nr   
regressionsingle_label_classificationmulti_label_classificationr6   r  )rQ  r  r/   problem_typer  r   rj   r   r   r   r  r   r;   r   r   r   rT  )r.   r   r   r   rZ   r   r  r~   rB   r   r  r  r  r  r  s                  r2   rG   +LayoutLMv3ForSequenceClassification.forward(  s   X $(??	$
))%'%	$
 	$
 "!*Q1W-1{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#F3D))-JJ+-B @&++b/R))-II,./'!//))	
 	
r4   )r  r/   rQ  r  r  )rK   rL   rM   rN   r!   rv  rz  r   r   rj   r  r  r   r   r  r   rG   rP   rQ   rR   s   @r2   r  r    s   64  .237260426*.(,04T
##d*T
 ))D0T
 ((4/	T

 &&-T
 ((4/T
   4'T
 %T
 &&-T
 +,T
 
)	)T
  T
r4   r  )r  r  r  rY  rP  )>rO   r$   r   rj   torch.nnr)   torch.nn.functional
functionalr>   r   r   r    r   r[  activationsr   modeling_layersr	   modeling_outputsr
   r   r   r   modeling_utilsr   processing_utilsr   pytorch_utilsr   utilsr   r   r   r   utils.genericr   r   utils.output_capturingr   configuration_layoutlmv3r   
get_loggerrK   loggerModuler   rT   r   r   r   r   r  r   r   rP  rY  r  r  r  r  __all__rc  r4   r2   <module>r     s           A A & ! 9  . & 6  L 5 6 
		H	% 		  Fsryy slV.bii V.t299  "))  6"0 "Jn@		 n@dRYY  ryy  i i i( A
/ A
 A
H299 6 `
'@ `
`
F i
%> i
 i
X f
*C f
f
Rr4   