
    sg                        d Z ddlZddlmZmZmZ ddlZddlZddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZmZ ddl m!Z!  e       r
ddl"Z"ddl#m$Z$  ejJ                  e&      Z'dZ(dZ) G d dejT                        Z+ G d dejT                        Z, G d dejT                        Z- G d dejT                        Z. G d dejT                        Z/ G d dejT                        Z0 G d dejT                        Z1d7dZ2 G d  d!ejT                        Z3 G d" d#e      Z4d8d$Z5 G d% d&ejT                        Z6d'Z7d(Z8 G d) d*ejT                        Z9 ed+e7       G d, d-e4             Z: ed.e7       G d/ d0e4             Z; ed1e7       G d2 d3e4             Z< ed4e7       G d5 d6e4             Z=y)9zPyTorch LayoutLMv2 model.    N)OptionalTupleUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)BaseModelOutputBaseModelOutputWithPoolingQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel)apply_chunking_to_forward)add_start_docstrings%add_start_docstrings_to_model_forwardis_detectron2_availableloggingreplace_return_docstringsrequires_backends   )LayoutLMv2Config)META_ARCH_REGISTRYz!microsoft/layoutlmv2-base-uncasedr   c                   (     e Zd ZdZ fdZd Z xZS )LayoutLMv2EmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                    t         t        |           t        j                  |j
                  |j                  |j                        | _        t        j                  |j                  |j                        | _
        t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j$                  |j                        | _        t        j(                  |j                  |j*                        | _        t        j,                  |j.                        | _        | j3                  dt5        j6                  |j                        j9                  d      d       y )N)padding_idxepsposition_ids)r   F
persistent)superr   __init__r   	Embedding
vocab_sizehidden_sizepad_token_idword_embeddingsmax_position_embeddingsposition_embeddingsmax_2d_position_embeddingscoordinate_sizex_position_embeddingsy_position_embeddings
shape_sizeh_position_embeddingsw_position_embeddingstype_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsDropouthidden_dropout_probdropoutregister_buffertorcharangeexpandselfconfig	__class__s     e/var/www/html/venv/lib/python3.12/site-packages/transformers/models/layoutlmv2/modeling_layoutlmv2.pyr'   zLayoutLMv2Embeddings.__init__<   sb   "D24!||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c %'\\&2S2SU[UkUk%l"%'\\&2S2SU[UkUk%l"%'\\&2S2SU[UfUf%g"%'\\&2S2SU[UfUf%g"%'\\&2H2H&J\J\%]"f&8&8f>S>STzz&"<"<=ELL)G)GHOOPWXej 	 	
    c                    	 | j                  |d d d d df         }| j                  |d d d d df         }| j                  |d d d d df         }| j                  |d d d d df         }| j                  |d d d d df   |d d d d df   z
        }| j	                  |d d d d df   |d d d d df   z
        }t        j                  ||||||gd      }	|	S # t        $ r}t        d      |d }~ww xY w)Nr   r      r
   z;The `bbox` coordinate values should be within 0-1000 range.r#   dim)r1   r2   
IndexErrorr4   r5   r>   cat)
rB   bboxleft_position_embeddingsupper_position_embeddingsright_position_embeddingslower_position_embeddingser4   r5   spatial_position_embeddingss
             rE   !_calc_spatial_position_embeddingsz6LayoutLMv2Embeddings._calc_spatial_position_embeddingsN   s(   	c'+'A'A$q!Qw-'P$(,(B(B41a=(Q%(,(B(B41a=(Q%(,(B(B41a=(Q% !% : :41a=4PQSTVWPW=;X Y $ : :41a=4PQSTVWPW=;X Y&+ii()))%% 
'
# +*#  	cZ[abb	cs   A,C 	C7&C22C7)__name__
__module____qualname____doc__r'   rT   __classcell__rD   s   @rE   r   r   9   s    Q
$+rF   r   c                   <     e Zd Z fdZd Zd Z	 	 	 	 	 ddZ xZS )LayoutLMv2SelfAttentionc                    t         |           |j                  |j                  z  dk7  r2t	        |d      s&t        d|j                   d|j                   d      |j                  | _        |j                  | _        t        |j                  |j                  z        | _        | j                  | j                  z  | _	        |j                  | _
        |j                  | _        |j                  rt        j                  |j                  d| j                  z  d      | _        t        j                  t!        j"                  d	d	| j                              | _        t        j                  t!        j"                  d	d	| j                              | _        nt        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j.                  |j0                        | _        y )
Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()r
   Fbiasr   )r&   r'   r*   num_attention_headshasattr
ValueErrorfast_qkvintattention_head_sizeall_head_sizehas_relative_attention_biashas_spatial_attention_biasr   Linear
qkv_linear	Parameterr>   zerosq_biasv_biasquerykeyvaluer:   attention_probs_dropout_probr<   rA   s     rE   r'   z LayoutLMv2SelfAttention.__init__i   s    : ::a?PVXhHi#F$6$6#7 8 445Q8  #)#=#= #&v'9'9F<V<V'V#W !558P8PP+1+M+M(*0*K*K'?? ii(:(:A@R@R<RY^_DO,,u{{1a9K9K'LMDK,,u{{1a9K9K'LMDK6#5#5t7I7IJDJyy!3!3T5G5GHDH6#5#5t7I7IJDJzz&"E"EFrF   c                     |j                         d d | j                  | j                  fz   } |j                  | }|j	                  dddd      S )Nr#   r   rH   r   r
   )sizerb   rg   viewpermute)rB   xnew_x_shapes      rE   transpose_for_scoresz,LayoutLMv2SelfAttention.transpose_for_scores   sN    ffhsmt'?'?AYAY&ZZAFFK yyAq!$$rF   c                    | j                   r| j                  |      }t        j                  |dd      \  }}}|j	                         | j
                  j	                         k(  r|| j
                  z   }|| j                  z   }nd|j	                         dz
  z  dz   }| | j
                  j                  | z   }| | j                  j                  | z   }n3| j                  |      }| j                  |      }| j                  |      }|||fS )Nr
   r#   rI   )r   r   )r#   )re   rl   r>   chunk
ndimensionro   rp   rw   rq   rr   rs   )rB   hidden_statesqkvqkv_szs          rE   compute_qkvz#LayoutLMv2SelfAttention.compute_qkv   s    ==//-0Ckk#qb1GAq!||~!7!7!99OOallnq01E9(((#..(((#..

=)A'A

=)A!QwrF   c                    | j                  |      \  }}}	| j                  |      }
| j                  |      }| j                  |	      }|
t        j                  | j                        z  }
t        j                  |
|j                  dd            }| j                  r||z  }| j                  r||z  }|j                         j                  |j                  t
        j                        t        j                  |j                        j                         }t"        j$                  j'                  |dt
        j(                        j+                  |      }| j-                  |      }|||z  }t        j                  ||      }|j/                  dddd      j1                         }|j3                         d d | j4                  fz   } |j6                  | }|r||f}|S |f}|S )Nr#   )rJ   dtyper   rH   r   r
   )r   r{   mathsqrtrg   r>   matmul	transposeri   rj   floatmasked_fill_toboolfinfor   minr   
functionalsoftmaxfloat32type_asr<   rx   
contiguousrv   rh   rw   )rB   r   attention_mask	head_maskoutput_attentionsrel_pos
rel_2d_posr   r   r   query_layer	key_layervalue_layerattention_scoresattention_probscontext_layernew_context_layer_shapeoutputss                     rE   forwardzLayoutLMv2SelfAttention.forward   s    ""=11a //2--a0	//2!DIId.F.F$GG <<Y5H5HR5PQ++'**
*+113@@ejj)5;;7G7M7M+N+R+R
 --//0@bPUP]P]/^ffgrs ,,7  -	9O_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S***,CD6G=/2 O\M]rF   NNFNN)rU   rV   rW   r'   r{   r   r   rY   rZ   s   @rE   r\   r\   h   s)    G4%
( )rF   r\   c                   0     e Zd Z fdZ	 	 	 	 	 ddZ xZS )LayoutLMv2Attentionc                 b    t         |           t        |      | _        t	        |      | _        y N)r&   r'   r\   rB   LayoutLMv2SelfOutputoutputrA   s     rE   r'   zLayoutLMv2Attention.__init__   s&    +F3	*62rF   c                 p    | j                  ||||||      }| j                  |d   |      }|f|dd  z   }	|	S )Nr   r   r   r   )rB   r   )
rB   r   r   r   r   r   r   self_outputsattention_outputr   s
             rE   r   zLayoutLMv2Attention.forward   sY     yy! ! 
  ;;|AF#%QR(88rF   r   rU   rV   rW   r'   r   rY   rZ   s   @rE   r   r      s    3 rF   r   c                   $     e Zd Z fdZd Z xZS )r   c                 (   t         |           t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                        | _
        y Nr    )r&   r'   r   rk   r*   denser8   r9   r:   r;   r<   rA   s     rE   r'   zLayoutLMv2SelfOutput.__init__   s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rF   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S r   r   r<   r8   rB   r   input_tensors      rE   r   zLayoutLMv2SelfOutput.forward   7    

=1]3}|'CDrF   r   rZ   s   @rE   r   r      s    >rF   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )LayoutLMv2Intermediatec                    t         |           t        j                  |j                  |j
                        | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y r   )r&   r'   r   rk   r*   intermediate_sizer   
isinstance
hidden_actstrr   intermediate_act_fnrA   s     rE   r'   zLayoutLMv2Intermediate.__init__   s]    YYv1163K3KL
f''-'-f.?.?'@D$'-'8'8D$rF   r   returnc                 J    | j                  |      }| j                  |      }|S r   )r   r   )rB   r   s     rE   r   zLayoutLMv2Intermediate.forward   s&    

=100?rF   rU   rV   rW   r'   r>   Tensorr   rY   rZ   s   @rE   r   r      s#    9U\\ ell rF   r   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )LayoutLMv2Outputc                 (   t         |           t        j                  |j                  |j
                        | _        t        j                  |j
                  |j                        | _        t        j                  |j                        | _        y r   )r&   r'   r   rk   r   r*   r   r8   r9   r:   r;   r<   rA   s     rE   r'   zLayoutLMv2Output.__init__  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=rF   r   r   r   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S r   r   r   s      rE   r   zLayoutLMv2Output.forward  r   rF   r   rZ   s   @rE   r   r      s1    >U\\  RWR^R^ rF   r   c                   6     e Zd Z fdZ	 	 	 	 	 ddZd Z xZS )LayoutLMv2Layerc                     t         |           |j                  | _        d| _        t	        |      | _        t        |      | _        t        |      | _	        y )Nr   )
r&   r'   chunk_size_feed_forwardseq_len_dimr   	attentionr   intermediater   r   rA   s     rE   r'   zLayoutLMv2Layer.__init__  sI    '-'E'E$,V426:&v.rF   c                     | j                  ||||||      }|d   }|dd  }	t        | j                  | j                  | j                  |      }
|
f|	z   }	|	S )N)r   r   r   r   r   )r   r   feed_forward_chunkr   r   )rB   r   r   r   r   r   r   self_attention_outputsr   r   layer_outputs              rE   r   zLayoutLMv2Layer.forward  s|     "&/! "0 "
 2!4(,0##T%A%A4CSCSUe
  /G+rF   c                 L    | j                  |      }| j                  ||      }|S r   )r   r   )rB   r   intermediate_outputr   s       rE   r   z"LayoutLMv2Layer.feed_forward_chunk3  s,    "//0@A{{#68HIrF   r   )rU   rV   rW   r'   r   r   rY   rZ   s   @rE   r   r     s#    / 8rF   r   c                 6   d}|r4|dz  }|| dkD  j                         |z  z  }t        j                  |       }n*t        j                  |  t        j                  |             }|dz  }||k  }|t        j
                  |j                         |z        t        j
                  ||z        z  ||z
  z  j                  t        j                         z   }t        j                  |t        j                  ||dz
              }|t        j                  |||      z  }|S )a  
    Adapted from Mesh Tensorflow:
    https://github.com/tensorflow/mesh/blob/0cb87fe07da627bf0b7e60475d59f95ed6b5be3d/mesh_tensorflow/transformer/transformer_layers.py#L593
    Translate relative position to a bucket number for relative attention. The relative position is defined as
    memory_position - query_position, i.e. the distance in tokens from the attending position to the attended-to
    position. If bidirectional=False, then positive relative positions are invalid. We use smaller buckets for small
    absolute relative_position and larger buckets for larger absolute relative_positions. All relative positions
    >=max_distance map to the same bucket. All relative positions <=-max_distance map to the same bucket. This should
    allow for more graceful generalization to longer sequences than the model has been trained on.

    Args:
        relative_position: an int32 Tensor
        bidirectional: a boolean - whether the attention is bidirectional
        num_buckets: an integer
        max_distance: an integer

    Returns:
        a Tensor with the same shape as relative_position, containing int32 values in the range [0, num_buckets)
    r   rH   r   )longr>   absmax
zeros_likelogr   r   r   r   	full_likewhere)	relative_positionbidirectionalnum_bucketsmax_distanceretn	max_exactis_smallval_if_larges	            rE   relative_position_bucketr   9  s   * C!A%++-;;II'(II((%*:*:;L*MN q I9}H 		!'')i'(488L94L+MMQ\_hQhibnL 99\5??<WX+YZL5;;xL11CJrF   c                   @     e Zd Z fdZd Zd Z	 	 	 	 	 	 	 ddZ xZS )LayoutLMv2Encoderc                    t         |           || _        t        j                  t        |j                        D cg c]  }t        |       c}      | _        |j                  | _	        |j                  | _
        | j                  rS|j                  | _        |j                  | _        t        j                  | j                  |j                  d      | _        | j                  r|j                   | _        |j"                  | _        t        j                  | j"                  |j                  d      | _        t        j                  | j"                  |j                  d      | _        d| _        y c c}w )NFr`   )r&   r'   rC   r   
ModuleListrangenum_hidden_layersr   layerri   rj   rel_pos_binsmax_rel_posrk   rb   rel_pos_biasmax_rel_2d_posrel_2d_pos_binsrel_pos_x_biasrel_pos_y_biasgradient_checkpointing)rB   rC   _rD   s      rE   r'   zLayoutLMv2Encoder.__init__f  s   ]]U6KcKcEd#eOF$;#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   E4c                 t   |j                  d      |j                  d      z
  }t        || j                  | j                        }t	        j
                         5  | j                  j                  j                         |   j                  dddd      }d d d        |j                         }|S # 1 sw Y   xY w)Nr   r#   r   r   r   r
   r   rH   )	unsqueezer   r   r   r>   no_gradr   weighttrx   r   )rB   r"   rel_pos_matr   s       rE   !_calculate_1d_position_embeddingsz3LayoutLMv2Encoder._calculate_1d_position_embeddings{  s    ",,R0<3I3I"3MM*))))
 ]]_ 	P''..0027;CCAq!QOG	P$$&	P 	Ps   :B..B7c                    |d d d d df   }|d d d d df   }|j                  d      |j                  d      z
  }|j                  d      |j                  d      z
  }t        || j                  | j                        }t        || j                  | j                        }t	        j
                         5  | j                  j                  j                         |   j                  dddd      }| j                  j                  j                         |   j                  dddd      }d d d        |j                         }|j                         }||z   }|S # 1 sw Y   0xY w)Nr   r
   r   r#   r   r   rH   )r   r   r   r   r>   r   r   r   r   rx   r   r   )	rB   rM   position_coord_xposition_coord_yrel_pos_x_2d_matrel_pos_y_2d_mat	rel_pos_x	rel_pos_yr   s	            rE   !_calculate_2d_position_embeddingsz3LayoutLMv2Encoder._calculate_2d_position_embeddings  s^   1a=1a=+55b9<L<V<VWY<ZZ+55b9<L<V<VWY<ZZ,,,,,
	
 -,,,,
	 ]]_ 	V++22446yAII!QPQSTUI++22446yAII!QPQSTUI	V ((*	((*	*
	V 	Vs   7A3EE"c	           
          |rdnd }	|rdnd }
| j                   r| j                  |      nd }| j                  r| j                  |      nd }t	        | j
                        D ]p  \  }}|r|	|fz   }	|||   nd }| j                  r/| j                  r#| j                  |j                  ||||||      }n |||||||      }|d   }|sh|
|d   fz   }
r |r|	|fz   }	|st        d ||	|
fD              S t        ||	|
      S )N r   r   r   c              3   $   K   | ]  }|| 
 y wr   r  ).0r   s     rE   	<genexpr>z,LayoutLMv2Encoder.forward.<locals>.<genexpr>  s      
 = s   )last_hidden_stater   
attentions)ri   r  rj   r	  	enumerater   r   training_gradient_checkpointing_func__call__tupler   )rB   r   r   r   r   output_hidden_statesreturn_dictrM   r"   all_hidden_statesall_self_attentionsr   r   ilayer_modulelayer_head_masklayer_outputss                    rE   r   zLayoutLMv2Encoder.forward  sb    #7BD$5b4JNJjJj$88FptEIEdEdT;;DAjn
(4 	POA|#$58H$H!.7.CilO**t}} $ A A ))!"#%#) !B ! !-!"#%#)! *!,M &9]1=M<O&O#9	P<   1]4D D  "%'   ++*
 	
rF   )NNFFTNN)rU   rV   rW   r'   r  r	  r   rY   rZ   s   @rE   r   r   e  s/    ,* < "@
rF   r   c                       e Zd ZdZeZdZd Zy)LayoutLMv2PreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    
layoutlmv2c                    t        |t        j                        rm|j                  j                  j                  d| j                  j                         |j                  %|j                  j                  j                          yyt        |t        j                        rz|j                  j                  j                  d| j                  j                         |j                  2|j                  j                  |j                     j                          yyt        |t        j                        rJ|j                  j                  j                          |j                  j                  j                  d       yt        |t              rIt        |d      r<|j                   j                  j                  d| j                  j                         yyy)zInitialize the weightsg        )meanstdN      ?visual_segment_embedding)r   r   rk   r   datanormal_rC   initializer_rangera   zero_r(   r   r8   fill_LayoutLMv2Modelrc   r%  )rB   modules     rE   _init_weightsz'LayoutLMv2PreTrainedModel._init_weights  sI   fbii( MM&&CT[[5R5R&S{{&  &&( '-MM&&CT[[5R5R&S!!-""6#5#56<<> .-KK""$MM$$S)0v9://44<<#4;;KhKh<i ; 1rF   N)rU   rV   rW   rX   r   config_classbase_model_prefixr-  r  rF   rE   r  r    s    
 $L$jrF   r  c                 j   t        | t        j                  j                  j                  j
                        r*t        j                  j                  j                  | |      S | }t        | t        j                  j                        rt        j                  j                  | j                  | j                  dd|      }t        j                  j                  | j                        |_        t        j                  j                  | j                        |_        | j                   |_        | j"                  |_        t        j$                  dt        j&                  | j                   j(                        |_        | j-                         D ]!  \  }}|j/                  |t1        ||             # ~ |S )NT)num_featuresr!   affinetrack_running_statsprocess_groupr   r   device)r   r>   r   modules	batchnorm
_BatchNormSyncBatchNormconvert_sync_batchnorm
detectron2layersFrozenBatchNorm2dr1  r!   rm   r   ra   running_meanrunning_vartensorr   r6  num_batches_trackednamed_children
add_modulemy_convert_sync_batchnorm)r,  r4  module_outputnamechilds        rE   rE  rE    sE   &%((**44??@zz''>>v}UUM&*++==>..,,

 $' / 
  %xx11&--@"XX//<%+%8%8"$*$6$6!,1LL%**U[UhUhUoUo,p),,. Xe  '@'VWXrF   c                   *     e Zd Z fdZd Zd Z xZS )LayoutLMv2VisualBackbonec           	      H   t         |           |j                         | _        | j                  j                  j
                  } t        j                  |      | j                        }t        |j                  t        j                  j                  j                        sJ |j                  | _	        t        | j                  j                  j                        t        | j                  j                  j                        k(  sJ t        | j                  j                  j                        }| j!                  dt#        j$                  | j                  j                  j                        j'                  |dd      d       | j!                  dt#        j$                  | j                  j                  j                        j'                  |dd      d       d| _        t#        j*                         rt,        j/                  d       d}| j                  j1                         | j(                     j2                  }t5        j6                  t9        j:                  t9        j:                  |d	   |z        |j<                  d	   z        t9        j:                  t9        j:                  |d   |z        |j<                  d   z        f      | _        n't5        j@                  |j<                  d d
       | _        t        |j<                        d
k(  rJ|j<                  jC                  | j                  j1                         | j(                     jD                         | j                  j1                         | j(                     jD                  |j<                  d
   k(  sJ y )N
pixel_meanr   Fr$   	pixel_stdp2z0using `AvgPool2d` instead of `AdaptiveAvgPool2d`)   rO  r   rH   )#r&   r'   get_detectron2_configcfgMODELMETA_ARCHITECTUREr   getr   backboner<  modelingFPNlen
PIXEL_MEAN	PIXEL_STDr=   r>   r   rw   out_feature_key$are_deterministic_algorithms_enabledloggerwarningoutput_shapestrider   	AvgPool2dr   ceilimage_feature_pool_shapepoolAdaptiveAvgPool2dappendchannels)rB   rC   	meta_archmodelnum_channelsinput_shapebackbone_striderD   s          rE   r'   z!LayoutLMv2VisualBackbone.__init__  s   //1HHNN44	1"&&y1$((;%..**=*=*F*F*J*JKKK488>>,,-TXX^^5M5M1NNNN488>>445LL22388q!L 	 	

 	dhhnn&>&>?DD\STVWXej 	 	
  $557NNMN$K"mm88:4;O;OPWWOIIdiiA(HIFLkLklmLnnoIIdiiA(HIFLkLklmLnnoDI ,,V-L-LRa-PQDIv../14++224==3M3M3OPTPdPd3e3n3no}}))+D,@,@AJJfNmNmnoNpppprF   c                 >   t        j                  |      r|n|j                  | j                  z
  | j                  z  }| j                  |      }|| j                     }| j                  |      j                  d      j                  dd      j                         }|S )NrH   )	start_dimr   )r>   	is_tensorrA  rL  rM  rU  r[  rd  flattenr   r   )rB   imagesimages_inputfeaturess       rE   r   z LayoutLMv2VisualBackbone.forwardB  s    #(??6#:QUQ`Q``dhdrdrr==.D00199X&...;EEaKVVXrF   c           
         t         j                  j                         r?t         j                  j                         r!t         j                  j	                         dkD  st        d      t         j                  j	                         }t         j                  j                         }t         j                  j                         }||z  dk(  st        d      t        ||z        D cg c]   }t        t        ||z  |dz   |z              " }}t        ||z        D cg c]%  }t         j                  j                  ||         ' }}||z  }t        | j                  ||         | _        y c c}w c c}w )Nr#   z/Make sure torch.distributed is set up properly.r   zGMake sure the number of processes can be divided by the number of nodesr   )ranks)r4  )r>   distributedis_availableis_initializedget_rankRuntimeErrorcudadevice_countget_world_sizer   list	new_grouprE  rU  )rB   	self_rank	node_size
world_sizer  node_global_rankssync_bn_groups	node_ranks           rE   synchronize_batch_normz/LayoutLMv2VisualBackbone.synchronize_batch_normI  sE   **,!!002!!**,r1PQQ%%..0	JJ++-	&&557
Y&!+hiiV[\fjs\sVtuQRT%IA7J"KLuuMRS]ajSjMk
HIE''.?.B'C
 
 *	1$--~^gOhi v
s   &%E.*E3)rU   rV   rW   r'   r   r  rY   rZ   s   @rE   rJ  rJ    s    !qFjrF   rJ  aM  
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`LayoutLMv2Config`]): 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        input_ids (`torch.LongTensor` of shape `{0}`):
            Indices of input sequence tokens in the vocabulary.

            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 `({0}, 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.

        image (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` or `detectron.structures.ImageList` whose `tensors` is of shape `(batch_size, num_channels, height, width)`):
            Batch of document images.

        attention_mask (`torch.FloatTensor` of shape `{0}`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

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

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

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

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

            [What are position IDs?](../glossary#position-ids)
        head_mask (`torch.FloatTensor` 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 (`torch.FloatTensor` 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.
c                   $     e Zd Z fdZd Z xZS )LayoutLMv2Poolerc                     t         |           t        j                  |j                  |j                        | _        t        j                         | _        y r   )r&   r'   r   rk   r*   r   Tanh
activationrA   s     rE   r'   zLayoutLMv2Pooler.__init__  s9    YYv1163E3EF
'')rF   c                 \    |d d df   }| j                  |      }| j                  |      }|S )Nr   )r   r  )rB   r   first_token_tensorpooled_outputs       rE   r   zLayoutLMv2Pooler.forward  s6     +1a40

#566rF   r   rZ   s   @rE   r  r    s    $
rF   r  zdThe bare LayoutLMv2 Model transformer outputting raw hidden-states without any specific head on top.c                       e Zd Z fdZd Zd ZddZd Zd ZddZ	 e
ej                  d             eee	      	 	 	 	 	 	 	 	 	 	 	 dd
eej$                     deej$                     deej&                     deej&                     deej$                     deej$                     deej&                     deej&                     dee   dee   dee   deeef   fd              Z xZS )r+  c                    t        | d       t        | 	  |       || _        |j                  | _        t        |      | _        t        |      | _        t        j                  |j                  d   |j                        | _        | j                  rEt        j                  t        j                  d|j                        j                   d         | _        t        j$                  |j                  |j&                        | _        t        j*                  |j,                        | _        t1        |      | _        t5        |      | _        | j9                          y )Nr<  r#   r   r   r    )r   r&   r'   rC   has_visual_segment_embeddingr   
embeddingsrJ  visualr   rk   rc  r*   visual_projrm   r(   r   r%  r8   r9   visual_LayerNormr:   r;   visual_dropoutr   encoderr  pooler	post_initrA   s     rE   r'   zLayoutLMv2Model.__init__  s    $- ,2,O,O).v6.v699V%D%DR%H&J\J\],,,.LLaI[I[9\9c9cde9f,gD) "V-?-?VEZEZ [ jj)C)CD(0&v. 	rF   c                 .    | j                   j                  S r   r  r,   rB   s    rE   get_input_embeddingsz$LayoutLMv2Model.get_input_embeddings  s    ...rF   c                 &    || j                   _        y r   r  )rB   rs   s     rE   set_input_embeddingsz$LayoutLMv2Model.set_input_embeddings  s    */'rF   c                    ||j                         }n|j                         d d }|d   }|Pt        j                  |t        j                  |j                        }|j                  d      j                  |      }|t        j                  |      }|| j                  j                  |      }| j                  j                  |      }| j                  j                  |      }	| j                  j                  |      }
||z   |	z   |
z   }| j                  j                  |      }| j                  j                  |      }|S )Nr#   r   r5  r   )rv   r>   r?   r   r6  r   	expand_asr   r  r,   r.   rT   r7   r8   r<   )rB   	input_idsrM   r"   token_type_idsinputs_embedsrk  
seq_lengthr.   rS   r7   r  s               rE   _calc_text_embeddingsz%LayoutLMv2Model._calc_text_embeddings  s$    #..*K',,.s3K ^
 <<
%**YM]M]^L'11!4>>yIL!"--i8N  OO;;IFM"ooAA,O&*oo&W&WX\&]# $ E En U"%88;VVYnn
__..z:
__,,Z8
rF   c                 <   | j                  | j                  |            }| j                  j                  |      }| j                  j	                  |      }||z   |z   }| j
                  r|| j                  z  }| j                  |      }| j                  |      }|S r   )	r  r  r  r.   rT   r  r%  r  r  )rB   imagerM   r"   visual_embeddingsr.   rS   r  s           rE   _calc_img_embeddingsz$LayoutLMv2Model._calc_img_embeddings  s     ,,T[[-?@"ooAA,O&*oo&W&WX\&]#&)<<?ZZ
,,$777J**:6
((4
rF   c           	      &   t        j                  t        j                  dd|d   dz   z  d||j                        | j                  j
                  d   d      }t        j                  t        j                  dd| j                  j
                  d   dz   z  d||j                        | j                  j
                  d   d      }t        j                  |d d j                  |d   d      |d d j                  |d   d      j                  dd      |dd  j                  |d   d      |dd  j                  |d   d      j                  dd      gd      j                  d|j                  d            }|j                  |d   dd      }|S )	Nr   i  r   )r6  r   floor)rounding_moder#   rI   )r>   divr?   r   rC   rc  stackrepeatr   rw   rv   )rB   rc  rM   r6  final_shapevisual_bbox_xvisual_bbox_yvisual_bboxs           rE   _calc_visual_bboxz!LayoutLMv2Model._calc_visual_bbox  s   		LL03a78jj KK003!

 		LL<<Q?!CDjj KK003!

 kkcr"))*B1*EqIcr"))*B1*EqISSTUWXYab!(()A!)DaHab!(()A!)DaHRRSTVWX	 
 $r499R=
! 	 "((QA>rF   c                     ||t        d      ||j                         S ||j                         d d S t        d      )NDYou cannot specify both input_ids and inputs_embeds at the same timer#   5You have to specify either input_ids or inputs_embeds)rd   rv   )rB   r  r  s      rE   _get_input_shapez LayoutLMv2Model._get_input_shape  sT     ]%>cdd">>##& %%',,TUUrF   z(batch_size, sequence_length)output_typer.  r  rM   r  r   r  r"   r   r  r   r  r  r   c           
      N	   |	|	n| j                   j                  }	|
|
n| j                   j                  }
||n| j                   j                  }| j	                  ||      }||j
                  n|j
                  }t        |      }| j                   j                  d   | j                   j                  d   z  |d<   t        j                  |      }t        | j	                  ||            }|dxx   |d   z  cc<   t        j                  |      }| j                  | j                   j                  |||      }t        j                  ||gd      }|t        j                  ||      }t        j                  ||      }t        j                  ||gd      }|&t        j                  |t        j                  |      }|5|d   }| j                  j                   ddd|f   }|j#                  |      }t        j$                  d|d   t        j                  |      j'                  |d   d      }t        j                  ||gd      }|<t        j                  t)        t        |      dgz         t        j                  |      }| j+                  |||||      }| j-                  |||	      }t        j                  ||gd      }|j/                  d      j/                  d
      }|j1                  | j2                        }d|z
  t        j4                  | j2                        j6                  z  }||j9                         dk(  rh|j/                  d      j/                  d      j/                  d      j/                  d      }|j#                  | j                   j:                  dddd      }nB|j9                         d
k(  r/|j/                  d      j/                  d      j/                  d      }|j1                  t=        | j?                               j2                        }ndg| j                   j:                  z  }| jA                  ||||||	|
|      }|d   }| jC                  |      }|s
||f|dd z   S tE        |||jF                  |jH                        S )a  
        Return:

        Examples:

        ```python
        >>> from transformers import AutoProcessor, LayoutLMv2Model, set_seed
        >>> from PIL import Image
        >>> import torch
        >>> from datasets import load_dataset

        >>> set_seed(0)

        >>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased")
        >>> model = LayoutLMv2Model.from_pretrained("microsoft/layoutlmv2-base-uncased")


        >>> dataset = load_dataset("hf-internal-testing/fixtures_docvqa", trust_remote_code=True)
        >>> image_path = dataset["test"][0]["file"]
        >>> image = Image.open(image_path).convert("RGB")

        >>> encoding = processor(image, return_tensors="pt")

        >>> outputs = model(**encoding)
        >>> last_hidden_states = outputs.last_hidden_state

        >>> last_hidden_states.shape
        torch.Size([1, 342, 768])
        ```
        Nr   r   rI   )r6  r5     )r  rM   r  r"   r  r  rM   r"   rH   )r   r$  r#   )rM   r"   r   r   r  r  )r  pooler_outputr   r  )%rC   r   r  use_return_dictr  r6  r~  rc  r>   Sizer  rL   onesrn   r   r  r"   r@   r?   r  r  r  r  r   r   r   r   r   rJ   r   next
parametersr  r  r   r   r  )rB   r  rM   r  r   r  r"   r   r  r   r  r  rk  r6  visual_shaper  r  
final_bboxvisual_attention_maskfinal_attention_maskr  visual_position_idsfinal_position_idstext_layout_emb
visual_emb	final_embextended_attention_maskencoder_outputssequence_outputr  s                                 rE   r   zLayoutLMv2Model.forward(  sQ   \ 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]++I}E%.%:!!@T@TK(++>>qADKKDhDhijDkkQzz,/400MJKA,q/)jj-,,T[[-Q-QSWY_almYYk2:
!"ZZFCN %

< G$yy.:O)PVWX!"[[EJJvVN$QJ??77;J;GL'..{;L#ll1l1oUZZX^_ffNA
 #YY6I'JPQR<;;uT+%6!%<=UZZX^_D44)%' 5 
 .., / 


 II
;C	"6"@"@"C"M"Ma"P"9"<"<4::"<"N#&)@#@EKKPTPZPZD[D_D_"_ }}!#%//2<<Q?II"MWWXZ[	%,,T[[-J-JBPRTVXZ[	A%%//2<<R@JJ2N	!40A+B+H+HII!>!>>I,,#+/!5# ' 	
 *!,O4#]3oab6III)-')77&11	
 	
rF   r   )NN)NNNNNNNNNNN)rU   rV   rW   r'   r  r  r  r  r  r  r   LAYOUTLMV2_INPUTS_DOCSTRINGformatr   r   _CONFIG_FOR_DOCr   r>   
LongTensorFloatTensorr   r   r   r   r   rY   rZ   s   @rE   r+  r+    su   
(/02	#JV ++F+M+MNm+no?Y 15+/-16:59371559,0/3&*I
E,,-I
 u''(I
 ))*	I

 !!2!23I
 !!1!12I
 u//0I
 E--.I
   1 12I
 $D>I
 'tnI
 d^I
 
u00	1I
 Z pI
rF   r+  ax  
    LayoutLMv2 Model with a sequence classification head on top (a linear layer on top of the concatenation of the
    final hidden state of the [CLS] token, average-pooled initial visual embeddings and average-pooled final visual
    embeddings, e.g. for document image classification tasks such as the
    [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip/) dataset.
    c                       e Zd Z fdZd Z eej                  d             ee	e
      	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     d	eej                     d
eej                     deej                     deej                     deej                     dee   dee   dee   deee	f   fd              Z xZS )#LayoutLMv2ForSequenceClassificationc                 2   t         |   |       |j                  | _        t        |      | _        t        j                  |j                        | _        t        j                  |j                  dz  |j                        | _        | j                          y )Nr
   r&   r'   
num_labelsr+  r   r   r:   r;   r<   rk   r*   
classifierr  rA   s     rE   r'   z,LayoutLMv2ForSequenceClassification.__init__  sn      ++)&1zz&"<"<=))F$6$6$:F<M<MN 	rF   c                 B    | j                   j                  j                  S r   r   r  r,   r  s    rE   r  z8LayoutLMv2ForSequenceClassification.get_input_embeddings      ))999rF   batch_size, sequence_lengthr  r  rM   r  r   r  r"   r   r  labelsr   r  r  r   c                 `   ||n| j                   j                  }||t        d      |#| j                  ||       |j	                         }n!||j	                         dd }nt        d      ||j
                  n|j
                  }t        |      }| j                   j                  d   | j                   j                  d   z  |d<   t        j                  |      }t        |      }|dxx   |d   z  cc<   t        j                  |      }| j                  j                  | j                   j                  |||      }t        j                  d|d   t        j                  |      j                  |d   d      }| j                  j                  |||      }| j                  |||||||||
||	      }||j	                         }n|j	                         dd }|d   }|d   ddd|f   |d   dd|df   }}|dddddf   }|j!                  d
      }|j!                  d
      }t        j"                  |||gd
      }| j%                  |      }| j'                  |      }d}|	| j                   j(                  | j*                  dk(  rd| j                   _        nl| j*                  dkD  rL|	j,                  t        j                  k(  s|	j,                  t        j.                  k(  rd| j                   _        nd| j                   _        | j                   j(                  dk(  rIt1               }| j*                  dk(  r& ||j3                         |	j3                               }n |||	      }n| j                   j(                  dk(  r=t5               } ||j7                  d| j*                        |	j7                  d            }n,| j                   j(                  dk(  rt9               } |||	      }|s|f|dd z   }||f|z   S |S t;        |||j<                  |j>                        S )a  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Returns:

        Example:

        ```python
        >>> from transformers import AutoProcessor, LayoutLMv2ForSequenceClassification, set_seed
        >>> from PIL import Image
        >>> import torch
        >>> from datasets import load_dataset

        >>> set_seed(0)

        >>> dataset = load_dataset("aharley/rvl_cdip", split="train", streaming=True, trust_remote_code=True)
        >>> data = next(iter(dataset))
        >>> image = data["image"].convert("RGB")

        >>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased")
        >>> model = LayoutLMv2ForSequenceClassification.from_pretrained(
        ...     "microsoft/layoutlmv2-base-uncased", num_labels=dataset.info.features["label"].num_classes
        ... )

        >>> encoding = processor(image, return_tensors="pt")
        >>> sequence_label = torch.tensor([data["label"]])

        >>> outputs = model(**encoding, labels=sequence_label)

        >>> loss, logits = outputs.loss, outputs.logits
        >>> predicted_idx = logits.argmax(dim=-1).item()
        >>> predicted_answer = dataset.info.features["label"].names[4]
        >>> predicted_idx, predicted_answer  # results are not good without further fine-tuning
        (7, 'advertisement')
        ```
        Nr  r#   r  r   r   r5  r  r  rM   r  r   r  r"   r   r  r   r  r  rI   
regressionsingle_label_classificationmulti_label_classificationrH   losslogitsr   r  ) rC   r  rd   %warn_if_padding_and_no_attention_maskrv   r6  r~  rc  r>   r  r   r  r?   r   r  r  r"  rL   r<   r  problem_typer  r   rf   r	   squeezer   rw   r   r   r   r  )rB   r  rM   r  r   r  r"   r   r  r  r   r  r  rk  r6  r  r  r  r  initial_image_embeddingsr   r  r  final_image_embeddingscls_final_outputpooled_initial_image_embeddingspooled_final_image_embeddingsr  r  loss_fctr   s                                  rE   r   z+LayoutLMv2ForSequenceClassification.forward  s
   r &1%<k$++B]B] ]%>cdd"66y.Q#..*K&',,.s3KTUU%.%:!!@T@TK(++>>qADKKDhDhijDkkQzz,/;'A,q/)jj-oo77KK00$
 $ll1l1oUZZX^_ffNA
 $(??#G#G, $H $
  //))%'/!5# " 
  #..*K',,.s3K ^
29!*Q^2LgVWjYZ\f\gYgNh/*1a73 +C*G*GA*G*N'(>(C(C(C(J%))>@]^de
 ,,71{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#FF3D))-JJ+-B @&++b/R))-II,./Y,F)-)9TGf$EvE'!//))	
 	
rF   NNNNNNNNNNNN)rU   rV   rW   r'   r  r   r  r  r   r   r  r   r>   r  r  r   r   r   r   rY   rZ   s   @rE   r  r    su   : ++F+M+MNk+lm+CRab 15+/-16:59371559-1,0/3&*Z
E,,-Z
 u''(Z
 ))*	Z

 !!2!23Z
 !!1!12Z
 u//0Z
 E--.Z
   1 12Z
 ))*Z
 $D>Z
 'tnZ
 d^Z
 
u..	/Z
 c nZ
rF   r  a  
    LayoutLMv2 Model with a token classification head on top (a linear layer on top of the text part of the 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                       e Zd Z fdZd Z eej                  d             ee	e
      	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     d	eej                     d
eej                     deej                     deej                     deej                     dee   dee   dee   deee	f   fd              Z xZS ) LayoutLMv2ForTokenClassificationc                 ,   t         |   |       |j                  | _        t        |      | _        t        j                  |j                        | _        t        j                  |j                  |j                        | _        | j                          y r   r  rA   s     rE   r'   z)LayoutLMv2ForTokenClassification.__init__v  si      ++)&1zz&"<"<=))F$6$68I8IJ 	rF   c                 B    | j                   j                  j                  S r   r  r  s    rE   r  z5LayoutLMv2ForTokenClassification.get_input_embeddings  r  rF   r  r  r  rM   r  r   r  r"   r   r  r  r   r  r  r   c                    ||n| j                   j                  }| j                  |||||||||
||      }||j                         }n|j                         dd }|d   }|d   ddd|f   }| j	                  |      }| j                  |      }d}|	<t               } ||j                  d| j                        |	j                  d            }|s|f|dd z   }||f|z   S |S t        |||j                  |j                        S )a  
        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]`.

        Returns:

        Example:

        ```python
        >>> from transformers import AutoProcessor, LayoutLMv2ForTokenClassification, set_seed
        >>> from PIL import Image
        >>> from datasets import load_dataset

        >>> set_seed(0)

        >>> datasets = load_dataset("nielsr/funsd", split="test", trust_remote_code=True)
        >>> labels = datasets.features["ner_tags"].feature.names
        >>> id2label = {v: k for v, k in enumerate(labels)}

        >>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr")
        >>> model = LayoutLMv2ForTokenClassification.from_pretrained(
        ...     "microsoft/layoutlmv2-base-uncased", num_labels=len(labels)
        ... )

        >>> data = datasets[0]
        >>> image = Image.open(data["image_path"]).convert("RGB")
        >>> words = data["words"]
        >>> boxes = data["bboxes"]  # make sure to normalize your bounding boxes
        >>> word_labels = data["ner_tags"]
        >>> encoding = processor(
        ...     image,
        ...     words,
        ...     boxes=boxes,
        ...     word_labels=word_labels,
        ...     padding="max_length",
        ...     truncation=True,
        ...     return_tensors="pt",
        ... )

        >>> outputs = model(**encoding)
        >>> logits, loss = outputs.logits, outputs.loss

        >>> predicted_token_class_ids = logits.argmax(-1)
        >>> predicted_tokens_classes = [id2label[t.item()] for t in predicted_token_class_ids[0]]
        >>> predicted_tokens_classes[:5]  # results are not good without further fine-tuning
        ['I-HEADER', 'I-HEADER', 'I-QUESTION', 'I-HEADER', 'I-QUESTION']
        ```
        Nr  r#   r   r   rH   r  )rC   r  r   rv   r<   r  r   rw   r  r   r   r  )rB   r  rM   r  r   r  r"   r   r  r  r   r  r  r   rk  r  r  r  r  r  r   s                        rE   r   z(LayoutLMv2ForTokenClassification.forward  s:   D &1%<k$++B]B]//))%'/!5# " 
  #..*K',,.s3K ^
!!*Q^4,,71')HFKKDOO<fkk"oNDY,F)-)9TGf$EvE$!//))	
 	
rF   r  )rU   rV   rW   r'   r  r   r  r  r   r   r  r   r>   r  r  r   r   r   r   rY   rZ   s   @rE   r  r  l  st   : ++F+M+MNk+lm+@_ 15+/-16:59371559-1,0/3&*h
E,,-h
 u''(h
 ))*	h

 !!2!23h
 !!1!12h
 u//0h
 E--.h
   1 12h
 ))*h
 $D>h
 'tnh
 d^h
 
u++	,h
 ` nh
rF   r  a  
    LayoutLMv2 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            !           e Zd Zd fd	Zd Z eej                  d             ee	e
      	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     deej                     d	eej                     d
eej                     deej                     deej                     deej                     deej                     dee   dee   dee   deee	f   fd              Z xZS )LayoutLMv2ForQuestionAnsweringc                     t         |   |       |j                  | _        ||_        t	        |      | _        t        j                  |j                  |j                        | _	        | j                          y r   )r&   r'   r  r  r+  r   r   rk   r*   
qa_outputsr  )rB   rC   r  rD   s      rE   r'   z'LayoutLMv2ForQuestionAnswering.__init__  s[      ++.J+)&1))F$6$68I8IJ 	rF   c                 B    | j                   j                  j                  S r   r  r  s    rE   r  z3LayoutLMv2ForQuestionAnswering.get_input_embeddings  r  rF   r  r  r  rM   r  r   r  r"   r   r  start_positionsend_positionsr   r  r  r   c                    ||n| j                   j                  }| j                  |||||||||||      }||j                         }n|j                         dd }|d   }|d   ddd|f   }| j	                  |      }|j                  dd      \  }}|j                  d      j                         }|j                  d      j                         }d}|	|
t        |	j                               dkD  r|	j                  d      }	t        |
j                               dkD  r|
j                  d      }
|j                  d      }|	j                  d|      }	|
j                  d|      }
t        |      } |||	      } |||
      }||z   dz  }|s||f|dd z   }||f|z   S |S t        ||||j                  |j                  	      S )
u  
        start_positions (`torch.LongTensor` 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 (`torch.LongTensor` 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:

        Example:

        In this example below, we give the LayoutLMv2 model an image (of texts) and ask it a question. It will give us
        a prediction of what it thinks the answer is (the span of the answer within the texts parsed from the image).

        ```python
        >>> from transformers import AutoProcessor, LayoutLMv2ForQuestionAnswering, set_seed
        >>> import torch
        >>> from PIL import Image
        >>> from datasets import load_dataset

        >>> set_seed(0)
        >>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased")
        >>> model = LayoutLMv2ForQuestionAnswering.from_pretrained("microsoft/layoutlmv2-base-uncased")

        >>> dataset = load_dataset("hf-internal-testing/fixtures_docvqa", trust_remote_code=True)
        >>> image_path = dataset["test"][0]["file"]
        >>> image = Image.open(image_path).convert("RGB")
        >>> question = "When is coffee break?"
        >>> encoding = processor(image, question, return_tensors="pt")

        >>> outputs = model(**encoding)
        >>> predicted_start_idx = outputs.start_logits.argmax(-1).item()
        >>> predicted_end_idx = outputs.end_logits.argmax(-1).item()
        >>> predicted_start_idx, predicted_end_idx
        (30, 191)

        >>> predicted_answer_tokens = encoding.input_ids.squeeze()[predicted_start_idx : predicted_end_idx + 1]
        >>> predicted_answer = processor.tokenizer.decode(predicted_answer_tokens)
        >>> predicted_answer  # results are not good without further fine-tuning
        '44 a. m. to 12 : 25 p. m. 12 : 25 to 12 : 58 p. m. 12 : 58 to 4 : 00 p. m. 2 : 00 to 5 : 00 p. m. coffee break coffee will be served for men and women in the lobby adjacent to exhibit area. please move into exhibit area. ( exhibits open ) trrf general session ( part | ) presiding : lee a. waller trrf vice president “ introductory remarks ” lee a. waller, trrf vice presi - dent individual interviews with trrf public board members and sci - entific advisory council mem - bers conducted by trrf treasurer philip g. kuehn to get answers which the public refrigerated warehousing industry is looking for. plus questions from'
        ```

        ```python
        >>> target_start_index = torch.tensor([7])
        >>> target_end_index = torch.tensor([14])
        >>> outputs = model(**encoding, start_positions=target_start_index, end_positions=target_end_index)
        >>> predicted_answer_span_start = outputs.start_logits.argmax(-1).item()
        >>> predicted_answer_span_end = outputs.end_logits.argmax(-1).item()
        >>> predicted_answer_span_start, predicted_answer_span_end
        (30, 191)
        ```
        Nr  r#   r   r   rI   )ignore_indexrH   )r  start_logits
end_logitsr   r  )rC   r  r   rv   r  splitr  r   rX  clampr   r   r   r  )rB   r  rM   r  r   r  r"   r   r  r  r  r   r  r  r   rk  r  r  r  r  r  
total_lossignored_indexr  
start_lossend_lossr   s                              rE   r   z&LayoutLMv2ForQuestionAnswering.forward  s   T &1%<k$++B]B]//))%'/!5# " 
  #..*K',,.s3K ^
!!*Q^41#)<<r<#: j#++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
M:H$x/14J"J/'!"+=F/9/EZMF*Q6Q+%!!//))
 	
rF   )T)NNNNNNNNNNNNN)rU   rV   rW   r'   r  r   r  r  r   r   r  r   r>   r  r  r   r   r   r   rY   rZ   s   @rE   r  r    s   : ++F+M+MNk+lm+GVef 15+/-16:593715596:48,0/3&*A
E,,-A
 u''(A
 ))*	A

 !!2!23A
 !!1!12A
 u//0A
 E--.A
   1 12A
 "%"2"23A
   0 01A
 $D>A
 'tnA
 d^A
 
u22	3A
 g nA
rF   r  )T       r   )>rX   r   typingr   r   r   r>   torch.utils.checkpointr   torch.nnr   r   r	   activationsr   modeling_outputsr   r   r   r   r   modeling_utilsr   pytorch_utilsr   utilsr   r   r   r   r   r   configuration_layoutlmv2r   r<  detectron2.modelingr   
get_loggerrU   r]  _CHECKPOINT_FOR_DOCr  Moduler   r\   r   r   r   r   r   r   r   r  rE  rJ  LAYOUTLMV2_START_DOCSTRINGr  r  r+  r  r  r  r  rF   rE   <module>r     s      ) )    A A !  . 6  7 6			H	%9 $,+299 ,+^Zbii Zz")) 8299 RYY  ryy (bii (V)XA
		 A
Hj j:0?jryy ?jD	 9 xryy  jy
/ y
	y
x  j
*C j
j
Z  x
'@ x
x
v 
 Q
%> Q
Q
rF   