
    g                        d Z ddlZddlZddlmZmZmZ ddlZddlm	Z	 ddl
m	c mZ ddl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 ddl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#  ej$        e%          Z&dZ'dZ(dZ)dZ* G d de	j+                  Z, G d de	j+                  Z- G d de          Z. G d de	j+                  Z/ G d de	j+                  Z0 G d de	j+                  Z1 G d de	j+                  Z2 G d d e	j+                  Z3 G d! d"e	j+                  Z4 G d# d$e	j+                  Z5 ed%e(           G d& d'e.                      Z6 G d( d)e	j+                  Z7 ed*e(           G d+ d,e.                      Z8 ed-e(           G d. d/e.                      Z9 ed0e(           G d1 d2e.                      Z:dS )3zPyTorch LayoutLMv3 model.    N)OptionalTupleUnion)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)BaseModelOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel)apply_chunking_to_forward)add_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstrings	torch_int   )LayoutLMv3Configr   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 ([`LayoutLMv3Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~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.

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

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

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

        attention_mask (`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**.

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

            [What are attention masks?](../glossary#attention-mask)
        token_type_ids (`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.

            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 `({0})`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

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

            [What are position IDs?](../glossary#position-ids)
        head_mask (`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 `({0}, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert *input_ids* indices into associated vectors than the
            model's internal embedding lookup matrix.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
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.

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

        attention_mask (`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 `({0}, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert *input_ids* indices into associated vectors than the
            model's internal embedding lookup matrix.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
c                   *     e Zd ZdZ fdZddZ xZS )LayoutLMv3PatchEmbeddingszLayoutLMv3 image (patch) embeddings. This class also automatically interpolates the position embeddings for varying
    image sizes.c                    t                                                       t          |j        t          j        j                  r|j        n|j        |j        f}t          |j        t          j        j                  r|j        n|j        |j        f}|d         |d         z  |d         |d         z  f| _        t          j
        |j        |j        ||          | _        d S )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       n/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/layoutlmv3/modeling_layoutlmv3.pyr   z"LayoutLMv3PatchEmbeddings.__init__   s     &+[_-EFF8F#V%67 	 &+[_-EFF8F#V%67 	
 'qMZ]:JqMZXY]<Z[If163ES]fpqqq			    Nc                    |                      |          }|~|                    d| j        d         | j        d         d          }|                    dddd          }|j        d         |j        d         }}t          j        |||fd          }||z   }|                    d                              dd          }|S )Nr   r   r	      bicubic)sizemode)	r*   viewr%   permuteshapeFinterpolateflatten	transpose)r+   pixel_valuesposition_embedding
embeddingspatch_heightpatch_widths         r/   forwardz!LayoutLMv3PatchEmbeddings.forward   s    YY|,,
)!3!8!8D<LQ<OQUQabcQdfh!i!i!3!;!;Aq!Q!G!G(2(8(;Z=Ma=P+L!"/AWbHcjs!t!t!t#&88J''**44Q::
r0   N__name__
__module____qualname____doc__r   rC   __classcell__r.   s   @r/   r   r      s[         r r r r r        r0   r   c                   F     e Zd ZdZ fdZd Zd Zd Z	 	 	 	 	 ddZ xZ	S )	LayoutLMv3TextEmbeddingszm
    LayoutLMv3 text embeddings. Same as `RobertaEmbeddings` but with added spatial (layout) embeddings.
    c                 ~   t                                                       t          j        |j        |j        |j                  | _        t          j        |j        |j                  | _	        t          j
        |j        |j                  | _
        t          j        |j                  | _        |                     dt!          j        |j                                      d          d           |j        | _        t          j        |j        |j        | j                  | _        t          j        |j        |j                  | _        t          j        |j        |j                  | _        t          j        |j        |j                  | _        t          j        |j        |j                  | _        d S )N)padding_idxepsposition_ids)r   r2   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expandrO   position_embeddingsmax_2d_position_embeddingscoordinate_sizex_position_embeddingsy_position_embeddings
shape_sizeh_position_embeddingsw_position_embeddingsr+   r,   r.   s     r/   r   z!LayoutLMv3TextEmbeddings.__init__   ss   !|F,=v?Q_e_rsss%'\&2H&J\%]%]"f&8f>STTTz&"<== 	EL)GHHOOPWXXej 	 	
 	
 	
 ".#%<*F,>DL\$
 $
 $
  &(\&2SU[Uk%l%l"%'\&2SU[Uk%l%l"%'\&2SU[Uf%g%g"%'\&2SU[Uf%g%g"""r0   c           	         	 |                      |d d d d df                   }|                     |d d d d df                   }|                      |d d d d df                   }|                     |d d d d df                   }n"# t          $ r}t          d          |d }~ww xY w|                     t	          j        |d d d d df         |d d d d df         z
  dd                    }|                     t	          j        |d d d d df         |d d d d df         z
  dd                    }t	          j        ||||||gd          }	|	S )	Nr   r   r3   r	   z;The `bbox` coordinate values should be within 0-1000 range.i  r2   dim)rg   rh   
IndexErrorrj   r`   cliprk   cat)
r+   bboxleft_position_embeddingsupper_position_embeddingsright_position_embeddingslower_position_embeddingserj   rk   spatial_position_embeddingss
             r/   %calculate_spatial_position_embeddingsz>LayoutLMv3TextEmbeddings.calculate_spatial_position_embeddings  s   	c'+'A'A$qqq!!!Qw-'P'P$(,(B(B4111a=(Q(Q%(,(B(B4111a=(Q(Q%(,(B(B4111a=(Q(Q%% 	c 	c 	cZ[[abb	c !% : :5:d111aaaQR7mVZ[\[\[\^_^_^_ab[bVcFcefhl;m;m n n $ : :5:d111aaaQR7mVZ[\[\[\^_^_^_ab[bVcFcefhl;m;m n n ',i()))%% 
'
 
'
 
'
# +*s   BB 
B*B%%B*c                     |                     |                                          }t          j        |d                              |          |z  }|                                |z   S )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   rn   )neintr`   cumsumtype_aslong)r+   	input_idsrO   maskincremental_indicess        r/   "create_position_ids_from_input_idsz;LayoutLMv3TextEmbeddings.create_position_ids_from_input_ids!  s`     ||K((,,..$|Da888@@FF$N"''))K77r0   c                    |                                 dd         }|d         }t          j        | j        dz   || j        z   dz   t          j        |j                  }|                    d                              |          S )z
        We are provided embeddings directly. We cannot infer which are padded so just generate sequential position ids.
        Nr2   r   dtypedevicer   )r5   r`   ra   rO   r   r   	unsqueezerc   )r+   inputs_embedsinput_shapesequence_lengthrR   s        r/   &create_position_ids_from_inputs_embedsz?LayoutLMv3TextEmbeddings.create_position_ids_from_inputs_embeds+  s     $((**3B3/%a.|q /D4D"Dq"HPUPZcpcw
 
 
 %%a((//<<<r0   Nc                 ~   |K|4|                      || j                                      |j                  }n|                     |          }||                                }n|                                d d         }|+t          j        |t          j        | j	        j                  }|| 
                    |          }|                     |          }||z   }|                     |          }	||	z  }|                     |          }
||
z   }|                     |          }|                     |          }|S )Nr2   r   )r   rO   tor   r   r5   r`   zerosr   rR   rW   rY   rd   rz   rZ   r^   )r+   r   rs   token_type_idsrR   r   r   rY   r@   rd   ry   s              r/   rC   z LayoutLMv3TextEmbeddings.forward7  sJ    $#FFyRVRbccff$     $JJ=YY #..**KK',,..ss3K!"[EJtO`OghhhN  00;;M $ : :> J J"%::
"66|DD))
&*&P&PQU&V&V#"==
^^J//
\\*--
r0   )NNNNN)
rF   rG   rH   rI   r   rz   r   r   rC   rJ   rK   s   @r/   rM   rM      s         h h h h h.+ + +48 8 8
= 
= 
= ' ' ' ' ' ' ' 'r0   rM   c                        e Zd ZdZeZdZd ZdS )LayoutLMv3PreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    
layoutlmv3c                    t          |t          j        t          j        f          rT|j        j                            d| j        j                   |j	         |j	        j        
                                 dS dS t          |t          j                  r_|j        j                            d| j        j                   |j        +|j        j        |j                 
                                 dS dS t          |t          j                  r?|j	        j        
                                 |j        j                            d           dS dS )zInitialize the weights        )meanstdNg      ?)r   r&   Linearr'   weightdatanormal_r,   initializer_rangebiaszero_rT   rO   rZ   fill_)r+   modules     r/   _init_weightsz'LayoutLMv3PreTrainedModel._init_weightsj  s0   fry")455 	* M&&CT[5R&SSS{& &&((((( '&-- 	*M&&CT[5R&SSS!-"6#56<<>>>>> .--- 	*K""$$$M$$S)))))	* 	*r0   N)rF   rG   rH   rI   r   config_classbase_model_prefixr    r0   r/   r   r   a  s:         
 $L$* * * * *r0   r   c                   >     e Zd Z fdZd ZddZ	 	 	 	 	 d	dZ xZS )
LayoutLMv3SelfAttentionc                    t                                                       |j        |j        z  dk    r0t	          |d          s t          d|j         d|j         d          |j        | _        t          |j        |j        z            | _        | j        | j        z  | _        t          j
        |j        | j                  | _        t          j
        |j        | j                  | _        t          j
        |j        | j                  | _        t          j        |j                  | _        |j        | _        |j        | _        d S )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&   r   querykeyvaluer\   attention_probs_dropout_probr^   has_relative_attention_biashas_spatial_attention_biasrl   s     r/   r   z LayoutLMv3SelfAttention.__init__|  s1    ::a??PVXhHiHi?8F$6 8 8 48 8 8  
 $*#= #&v'9F<V'V#W#W !58PPYv143EFF
9V/1CDDYv143EFF
z&"EFF+1+M(*0*K'''r0   c                     |                                 d d         | j        | j        fz   } |j        | }|                    dddd          S )Nr2   r   r3   r   r	   )r5   r   r   r7   r8   )r+   xnew_x_shapes      r/   transpose_for_scoresz,LayoutLMv3SelfAttention.transpose_for_scores  sM    ffhhssmt'?AY&ZZAFK yyAq!$$$r0       c                     ||z  }|                     d                              d          }||z
  |z  } t          j        d          |          S )a  
        https://arxiv.org/abs/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.
        r2   rn   )amaxr   r&   Softmax)r+   attention_scoresalphascaled_attention_scores	max_valuenew_attention_scoress         r/   cogview_attentionz)LayoutLMv3SelfAttention.cogview_attention  sa     #3U":+00b0::DDRHH	 7) CuL!rzb!!!"6777r0   NFc                    |                      |          }|                     |                     |                    }|                     |                     |                    }	|                     |          }
t	          j        |
t          j        | j                  z  |	                    dd                    }| j
        r*| j        r#|||z   t          j        | j                  z  z  }n&| j
        r||t          j        | j                  z  z  }|||z   }|                     |          }|                     |          }|||z  }t	          j        ||	          }|                    dddd                                          }|                                d d         | j        fz   } |j        | }|r||fn|f}|S )Nr2   r   r3   r   r	   )r   r   r   r   r`   matmulmathsqrtr   r=   r   r   r   r^   r8   
contiguousr5   r   r7   )r+   hidden_statesattention_mask	head_maskoutput_attentionsrel_pos
rel_2d_posmixed_query_layer	key_layervalue_layerquery_layerr   attention_probscontext_layernew_context_layer_shapeoutputss                   r/   rC   zLayoutLMv3SelfAttention.forward  s    !JJ}55--dhh}.E.EFF	//

=0I0IJJ//0ABB
 !<di@X6Y6Y(Y[d[n[noqsu[v[vww+ 	N0O 	N:!54C[9\9\ \\- 	N$)D4L*M*M MM%/.@ 001ABB ,,77  -	9O_kBB%--aAq99DDFF"/"4"4"6"6ss";t?Q>S"S**,CD6G]=/22mM]r0   )r   NNFNN)rF   rG   rH   r   r   r   rC   rJ   rK   s   @r/   r   r   {  s        L L L L L(% % %

8 
8 
8 
8 1 1 1 1 1 1 1 1r0   r   c                   P     e Zd Z fdZdej        dej        dej        fdZ xZS )LayoutLMv3SelfOutputc                    t                                                       t          j        |j        |j                  | _        t          j        |j        |j                  | _        t          j        |j	                  | _
        d S NrP   )r   r   r&   r   r)   denserZ   r[   r\   r]   r^   rl   s     r/   r   zLayoutLMv3SelfOutput.__init__  sf    Yv163EFF
f&8f>STTTz&"<==r0   r   input_tensorreturnc                     |                      |          }|                     |          }|                     ||z             }|S rD   r   r^   rZ   r+   r   r   s      r/   rC   zLayoutLMv3SelfOutput.forward  @    

=11]33}|'CDDr0   rF   rG   rH   r   r`   TensorrC   rJ   rK   s   @r/   r   r     i        > > > > >U\  RWR^        r0   r   c                   0     e Zd Z fdZ	 	 	 	 	 ddZ xZS )LayoutLMv3Attentionc                     t                                                       t          |          | _        t	          |          | _        d S rD   )r   r   r   r+   r   outputrl   s     r/   r   zLayoutLMv3Attention.__init__  s;    +F33	*622r0   NFc                     |                      ||||||          }|                     |d         |          }|f|dd          z   }	|	S )Nr   r   r   r   )r+   r   )
r+   r   r   r   r   r   r   self_outputsattention_outputr   s
             r/   rC   zLayoutLMv3Attention.forward  sc     yy! ! 
 
  ;;|AFF#%QRR(88r0   r   )rF   rG   rH   r   rC   rJ   rK   s   @r/   r   r     s]        3 3 3 3 3        r0   r   c                   6     e Zd Z fdZ	 	 	 	 	 ddZd Z xZS )LayoutLMv3Layerc                     t                                                       |j        | _        d| _        t	          |          | _        t          |          | _        t          |          | _	        d S Nr   )
r   r   chunk_size_feed_forwardseq_len_dimr   	attentionLayoutLMv3IntermediateintermediateLayoutLMv3Outputr   rl   s     r/   r   zLayoutLMv3Layer.__init__  s^    '-'E$,V4426::&v..r0   NFc                     |                      ||||||          }|d         }|dd          }	t          | j        | j        | j        |          }
|
f|	z   }	|	S )N)r   r   r   r   r   )r   r   feed_forward_chunkr   r   )r+   r   r   r   r   r   r   self_attention_outputsr   r   layer_outputs              r/   rC   zLayoutLMv3Layer.forward  s     "&/! "0 "
 "
 2!4(,0#T%A4CSUe
 
  /G+r0   c                 \    |                      |          }|                     ||          }|S rD   )r   r   )r+   r   intermediate_outputr   s       r/   r   z"LayoutLMv3Layer.feed_forward_chunk'  s2    "//0@AA{{#68HIIr0   r   )rF   rG   rH   r   rC   r   rJ   rK   s   @r/   r   r     sl        / / / / /    8      r0   r   c                   L     e Zd Z fdZddZd Zd Z	 	 	 	 	 	 	 	 	 dd
Z xZS )LayoutLMv3Encoderc                 h   t                                                       | _        t          j        fdt          j                  D                       | _        d| _        j	        | _	        j
        | _
        | j	        r>j        | _        j        | _        t          j        | j        j        d          | _        | j
        rfj        | _        j        | _        t          j        | j        j        d          | _        t          j        | j        j        d          | _        d S d S )Nc                 .    g | ]}t                    S r   )r   ).0_r,   s     r/   
<listcomp>z.LayoutLMv3Encoder.__init__.<locals>.<listcomp>1  s!    #e#e#eOF$;$;#e#e#er0   F)r   )r   r   r,   r&   
ModuleListrangenum_hidden_layers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_biasrl   s    `r/   r   zLayoutLMv3Encoder.__init__.  s&   ]#e#e#e#eU6KcEdEd#e#e#eff
&+#+1+M(*0*K'+ 	e & 3D%1D "	$*;V=W^c d d dD* 	j"("7D#)#9D "$)D,@&B\ch"i"i"iD"$)D,@&B\ch"i"i"iD		j 	jr0   Tr      c                 :   d}|r8|dz  }||dk                                     |z  z  }t          j        |          }n(t          j        | t          j        |                    }|dz  }||k     }|t          j        |                                |z            t          j        ||z            z  ||z
  z                      t          j                   z   }	t          j	        |	t          j
        |	|dz
                      }	|t          j        |||	          z  }|S )Nr   r3   r   )r   r`   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
             r/   relative_position_bucketz*LayoutLMv3Encoder.relative_position_bucketB  s    	SAK%)//11K??C	+,,AA	,,e.>?P.Q.QRRA  1$	y= !Iaggii)+,,txy8P/Q/QQU`clUlm
"UZ.. yu|[[\_/]/]^^u{8Q555
r0   c                    |                     d          |                     d          z
  }|                     || j        | j                  }t	          j                    5  | j        j                                        |         	                    dddd          }d d d            n# 1 swxY w Y   |
                                }|S )Nr   r2   r  r  r   r	   r   r3   )r   r%  r  r  r`   no_gradr  r   tr8   r   )r+   rR   rel_pos_matr   s       r/   _cal_1d_pos_embz!LayoutLMv3Encoder._cal_1d_pos_embY  s   ",,R00<3I3I"3M3MM//)) 0 
 
 ]__ 	P 	P'.00227;CCAq!QOOG	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P$$&&s   !;B((B,/B,c                    |d d d d df         }|d d d d df         }|                     d          |                     d          z
  }|                     d          |                     d          z
  }|                     || j        | j                  }|                     || j        | j                  }t	          j                    5  | j        j                                        |         	                    dddd          }| j
        j                                        |         	                    dddd          }d d d            n# 1 swxY w Y   |                                }|                                }||z   }|S )Nr   r	   r   r2   r'  r   r3   )r   r%  r  r  r`   r(  r  r   r)  r8   r  r   )	r+   rs   position_coord_xposition_coord_yrel_pos_x_2d_matrel_pos_y_2d_mat	rel_pos_x	rel_pos_yr   s	            r/   _cal_2d_pos_embz!LayoutLMv3Encoder._cal_2d_pos_embj  s   111a=111a=+55b99<L<V<VWY<Z<ZZ+55b99<L<V<VWY<Z<ZZ11,, 2 
 
	
 11,, 2 
 
	 ]__ 	V 	V+24466yAII!QPQSTUUI+24466yAII!QPQSTUUI	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V ((**	((**	*
s   A5EEENFc           
      
   |rdnd }|rdnd }| j         r|                     |          nd }| j        r|                     |          nd }t	          | j                  D ]o\  }}|r||fz   }|||         nd }| j        r(| j        r!|                     |j	        ||||||          }n |||||||          }|d         }|r||d         fz   }p|r||fz   }|st          d |||fD                       S t          |||          S )Nr   r   r   r   c              3      K   | ]}||V  	d S rD   r   )r  vs     r/   	<genexpr>z,LayoutLMv3Encoder.forward.<locals>.<genexpr>  s4        
 =  !=== r0   last_hidden_stater   
attentions)r   r+  r   r3  	enumerater	  r
  training_gradient_checkpointing_func__call__tupler   )r+   r   rs   r   r   r   output_hidden_statesreturn_dictrR   rA   rB   all_hidden_statesall_self_attentionsr   r   ilayer_modulelayer_head_masklayer_outputss                      r/   rC   zLayoutLMv3Encoder.forward  s    #7@BBD$5?bb48<8Xb$&&|444^b373R\T))$///X\
(44 	P 	POA|# I$58H$H!.7.CillO* t}  $ A A )!"#%! ! !-!"#%#)! ! ! *!,M  P&9]1=M<O&O# 	E 1]4D D 		   "%'      ++*
 
 
 	
r0   )Tr   r  )	NNNFFTNNN)	rF   rG   rH   r   r%  r+  r3  rC   rJ   rK   s   @r/   r   r   -  s        j j j j j(   .  "  < "B
 B
 B
 B
 B
 B
 B
 B
r0   r   c                   B     e Zd Z fdZdej        dej        fdZ xZS )r   c                    t                                                       t          j        |j        |j                  | _        t          |j        t                    rt          |j                 | _        d S |j        | _        d S rD   )r   r   r&   r   r)   intermediate_sizer   r   
hidden_actstrr
   intermediate_act_fnrl   s     r/   r   zLayoutLMv3Intermediate.__init__  sn    Yv163KLL
f'-- 	9'-f.?'@D$$$'-'8D$$$r0   r   r   c                 Z    |                      |          }|                     |          }|S rD   )r   rM  )r+   r   s     r/   rC   zLayoutLMv3Intermediate.forward  s,    

=1100??r0   r   rK   s   @r/   r   r     s^        9 9 9 9 9U\ el        r0   r   c                   P     e Zd Z fdZdej        dej        dej        fdZ xZS )r   c                    t                                                       t          j        |j        |j                  | _        t          j        |j        |j                  | _        t          j	        |j
                  | _        d S r   )r   r   r&   r   rJ  r)   r   rZ   r[   r\   r]   r^   rl   s     r/   r   zLayoutLMv3Output.__init__  sf    Yv79KLL
f&8f>STTTz&"<==r0   r   r   r   c                     |                      |          }|                     |          }|                     ||z             }|S rD   r   r   s      r/   rC   zLayoutLMv3Output.forward  r   r0   r   rK   s   @r/   r   r     r   r0   r   zdThe bare LayoutLMv3 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 ZddZd Zd	 Z	 e
e                    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 )LayoutLMv3Modelc                 |   t                                          |           || _        |j        rt	          |          | _        |j        rGt          |          | _        t          |j
        |j        z            }t          j        t          j        dd|j                            | _        t          j        t          j        d||z  dz   |j                            | _        t          j        d          | _        t          j        |j        |j                  | _        t          j        |j                  | _        | j        j        s| j        j        r|                     ||f           t          j        |j        d          | _        t;          |          | _        |                                  d S )Nr   r   )prP   )r-   gư>) r   r   r,   
text_embedrM   r@   visual_embedr   patch_embedr}   r    r$   r&   	Parameterr`   r   r)   	cls_token	pos_embedr\   pos_droprZ   r[   r]   r^   r   r   init_visual_bboxnormr   encoderinit_weights)r+   r,   r5   r.   s      r/   r   zLayoutLMv3Model.__init__  sv       	?6v>>DO 	C  9@@Dv(6+<<==D\%+aF<N*O*OPPDN\%+aq&J\*]*]^^DNJ---DM\&*<&BWXXXDN:f&@AADL{6 ?$+:` ?%%$%>>>V%7TBBBDI(00r0   c                     | j         j        S rD   r@   rW   )r+   s    r/   get_input_embeddingsz$LayoutLMv3Model.get_input_embeddings  s    ..r0   c                     || j         _        d S rD   rb  )r+   r   s     r/   set_input_embeddingsz$LayoutLMv3Model.set_input_embeddings  s    */'''r0   c                     |                                 D ]/\  }}| j        j        |         j                            |           0dS )z
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
        class PreTrainedModel
        N)itemsr_  r	  r   prune_heads)r+   heads_to_pruner	  headss       r/   _prune_headszLayoutLMv3Model._prune_heads  sU    
 +0022 	C 	CLE5Lu%/;;EBBBB	C 	Cr0      rm    c           	      .   t          j        t          j        d||d         dz   z  |          |d         d          }t          j        t          j        d||d         dz   z  |          |d         d          }t          j        |dd                             |d         d          |dd                             |d         d                              dd          |dd                             |d         d          |dd                             |d         d                              dd          gd                              dd          }t          j        dd|dz
  |dz
  gg          }t          j        ||gd          | _	        dS )	zJ
        Create the bounding boxes for the visual (patch) tokens.
        r   r   trunc)rounding_modeNr2   rn      )
r`   divra   stackrepeatr=   r7   tensorrr   visual_bbox)r+   r-   max_lenvisual_bbox_xvisual_bbox_yrw  cls_token_boxs          r/   r]  z LayoutLMv3Model.init_visual_bbox  s    	LGz!}q'897CCZPQ]bi
 
 
 	LGz!}q'897CCZPQ]bi
 
 
 kcrc"))*Q-;;crc"))*Q-;;EEaKKabb!((A::abb!((A::DDQJJ	 
 
 
 $r1++ 	 ueWq['A+&N%OPP 9m[%AqIIIr0   c                     | j                             |dd          }|                    |                              |          }|S r   )rw  ru  r   type)r+   r   r   
batch_sizerw  s        r/   calculate_visual_bboxz%LayoutLMv3Model.calculate_visual_bbox0  sA    &--j!Q??!nnV,,11%88r0   c                 >   |                      |          }|                                \  }}}| j                            |dd          }t	          j        ||fd          }| j        
|| j        z   }|                     |          }|                     |          }|S )Nr2   r   rn   )	rX  r5   rZ  rc   r`   rr   r[  r\  r^  )r+   r>   r@   r~  seq_lenr  
cls_tokenss          r/   forward_imagezLayoutLMv3Model.forward_image5  s    %%l33
 ",!2!2
GQ^**:r2>>
Y
J7Q???
 >%#dn4J]]:..
YYz**
r0   z!batch_size, token_sequence_lengthoutput_typer   Nr   rs   r   r   rR   r   r   r>   r   r@  rA  r   c                    |	|	n| j         j        }	|
|
n| j         j        }
||n| j         j        }|!|                                }|\  }}|j        }nS|)|                                dd         }|\  }}|j        }n(|t          |          }|j        }nt          d          |||t          j	        ||f|          }|!t          j
        |t          j        |          }|?t          j
        t          t          |          dgz             t          j        |          }|                     |||||          }dx}}dx}}| t          |j        d         | j         j        z            t          |j        d	         | j         j        z            }}|                     |          }t          j	        ||j        d
         ft          j        |          }|t          j        ||gd
          }n|}| j         j        s| j         j        r| j         j        r?|                     |t          j        |          }|t          j        ||gd
          }n|}t          j        d|j        d
         t          j        |                              |d
          }||^t          j        d|d
         |                              d          }|                    |          }t          j        ||gd
          }n|}||t          j        ||gd
          }n|}|                     |          }|                     |          }ng| j         j        s| j         j        rO| j         j        r|}| j         j        r5| j        j        ddd|d
         f         }|                    |          }|}|                     |d||j                   }| !                    || j         j"                  }| #                    ||||||	|
|||
  
        }|d         }|s|f|d
d         z   S tI          ||j%        |j&                  S )a  
        Returns:

        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", trust_remote_code=True)
        >>> 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
        ```Nr2   zEYou have to specify either input_ids or inputs_embeds or pixel_values)r   r   rr  )r   rs   rR   r   r   r3   r	   r   rn   )r   r~  r   )r   )	rs   rR   r   r   r   r@  rA  rA   rB   r8  )'r,   r   r@  use_return_dictr5   r   lenr   r`   onesr   r   r?  listr@   r   r9   r$   r  rr   r   r   r  ra   ru  r   rc   rZ   r^   rR   	expand_asget_extended_attention_maskr   get_head_maskr  r_  r   r   r:  )r+   r   rs   r   r   rR   r   r   r>   r   r@  rA  r   r~  
seq_lengthr   embedding_output
final_bboxfinal_position_idsrA   rB   visual_embeddingsvisual_attention_maskrw  visual_position_idsextended_attention_maskencoder_outputssequence_outputs                               r/   rC   zLayoutLMv3Model.forwardF  s   R 2C1N--TXT_Tq$8$D  $+Jj 	 &1%<kk$+B] #..**K%0"J
%FF&',,..ss3K%0"J
")FF%\**J!(FFdeee M$=%!&j*-Ev!V!V!V%!&[
SY!Z!Z!Z|{5k):):aS)@#A#A\bccc##)-+  /     +/.
'%)){#,,Q/$+2HHII,,Q/$+2HHII &L !% 2 2< @ @$)J.4Q78
SY% % %! )!&N<Q+RXY!Z!Z!Z!6{6 =$+:` =;9 1"&"<"<V5:bl"<"m"mK'%*Yk/B%J%J%J

%0
&+l(.q1F' ' '&Q'' $ (M,E#(<;q>&#Q#Q#Q#[#[\]#^#^L#/#6#6{#C#CL).LBU3V\])^)^)^&&)<&$(A#(9.>@Q-RXY#Z#Z#Z  #4 #~~.>??#||,<==[4 	28^ 	2{5 "!
{6 2#;AAA?OQ?O<OP+55i@@%1"040P0PD&0@0F 1Q 1
 1
 &&y$+2OPP	,,+2/!5#%# ' 
 
 *!, 	<#%(;;;-)7&1
 
 
 	
r0   )rl  rn  )NNNNNNNNNNN)rF   rG   rH   r   rc  re  rk  r]  r  r  r   !LAYOUTLMV3_MODEL_INPUTS_DOCSTRINGformatr   r   _CONFIG_FOR_DOCr   r`   
LongTensorFloatTensorboolr   r   rC   rJ   rK   s   @r/   rS  rS    s       
    :/ / /0 0 0C C CJ J J J.  
  " +*)001TUU  ?YYY 15+/6:5937155948,0/3&*[
 [
E,-[
 u'([
 !!23	[

 !!12[
 u/0[
 E-.[
   12[
 u01[
 $D>[
 'tn[
 d^[
 
uo%	&[
 [
 [
 ZY [
 [
 [
 [
 [
r0   rS  c                   *     e Zd ZdZd fd	Zd Z xZS )LayoutLMv3ClassificationHeadz\
    Head for sentence-level classification tasks. Reference: RobertaClassificationHead
    Fc                    t                                                       || _        |r(t          j        |j        dz  |j                  | _        n$t          j        |j        |j                  | _        |j        |j        n|j        }t          j	        |          | _
        t          j        |j        |j                  | _        d S )Nr	   )r   r   pool_featurer&   r   r)   r   classifier_dropoutr]   r\   r^   
num_labelsout_proj)r+   r,   r  r  r.   s       r/   r   z%LayoutLMv3ClassificationHead.__init__  s    ( 	K6#5#96;MNNDJJ6#5v7IJJDJ)/)B)NF%%TZTn 	 z"455	&"4f6GHHr0   c                     |                      |          }|                     |          }t          j        |          }|                      |          }|                     |          }|S rD   )r^   r   r`   tanhr  )r+   r   s     r/   rC   z$LayoutLMv3ClassificationHead.forward  sR    LLOOJJqMMJqMMLLOOMM!r0   )FrE   rK   s   @r/   r  r    s\         I I I I I I      r0   r  a  
    LayoutLMv3 Model with a token classification head on top (a linear layer on top of the final hidden states) e.g.
    for sequence labeling (information extraction) tasks such as [FUNSD](https://guillaumejaume.github.io/FUNSD/),
    [SROIE](https://rrc.cvc.uab.es/?ch=13), [CORD](https://github.com/clovaai/cord) and
    [Kleister-NDA](https://github.com/applicaai/kleister-nda).
    c                       e Zd Z fdZ ee                    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j                 deeef         fd                        Z xZS ) LayoutLMv3ForTokenClassificationc                 z   t                                          |           |j        | _        t          |          | _        t          j        |j                  | _        |j        dk     r%t          j	        |j
        |j                  | _        nt          |d          | _        |                                  d S )N
   Fr  )r   r   r  rS  r   r&   r\   r]   r^   r   r)   
classifierr  r`  rl   s     r/   r   z)LayoutLMv3ForTokenClassification.__init__  s        +)&11z&"<==r!! i(:F<MNNDOO:6PUVVVDOr0   batch_size, sequence_lengthr  Nr   rs   r   r   rR   r   r   labelsr   r@  rA  r>   r   c                 V   ||n| j         j        }|                     ||||||||	|
||          }||                                }n|                                dd         }|d         }|d         ddd|f         }|                     |          }|                     |          }d}|Ft                      } ||                    d| j                  |                    d                    }|s|f|dd         z   }||f|z   n|S t          |||j
        |j                  S )aj  
        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:

        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", trust_remote_code=True)
        >>> 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
        ```N)
rs   r   r   rR   r   r   r   r@  rA  r>   r2   r   r   losslogitsr   r:  )r,   r  r   r5   r^   r  r   r7   r  r   r   r:  )r+   r   rs   r   r   rR   r   r   r  r   r@  rA  r>   r   r   r  r  r  r  loss_fctr   s                        r/   rC   z(LayoutLMv3ForTokenClassification.forward  sg   ^ &1%<kk$+B]//))%'/!5#% " 
 
  #..**KK',,..ss3K ^
!!*QQQ^4,,7711'))H8FKKDO<<fkk"ooNND 	FY,F)-)9TGf$$vE$!/)	
 
 
 	
r0   NNNNNNNNNNNN)rF   rG   rH   r   r   &LAYOUTLMV3_DOWNSTREAM_INPUTS_DOCSTRINGr  r   r   r  r   r`   r  r  r  r   r   rC   rJ   rK   s   @r/   r  r    s            +*.556STT  +@___ 15+/6:59371559-1,0/3&*37S
 S
E,-S
 u'(S
 !!23	S

 !!12S
 u/0S
 E-.S
   12S
 )*S
 $D>S
 'tnS
 d^S
 u/0S
 
u++	,S
 S
 S
 `_ S
 S
 S
 S
 S
r0   r  a  
    LayoutLMv3 Model with a span classification head on top for extractive question-answering tasks such as
    [DocVQA](https://rrc.cvc.uab.es/?ch=17) (a linear layer on top of the text part of the hidden-states output to
    compute `span start logits` and `span end logits`).
    c            !           e Zd Z fdZ ee                    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j                 de
ej                 deeef         fd                        Z xZS )LayoutLMv3ForQuestionAnsweringc                     t                                          |           |j        | _        t          |          | _        t          |d          | _        |                                  d S NFr  )r   r   r  rS  r   r  
qa_outputsr`  rl   s     r/   r   z'LayoutLMv3ForQuestionAnswering.__init__}  s`        +)&116vERRRr0   r  r  Nr   r   r   rR   r   r   start_positionsend_positionsr   r@  rA  rs   r>   r   c                    ||n| j         j        }|                     |||||||	|
|||          }|d         }|                     |          }|                    dd          \  }}|                    d                                          }|                    d                                          }d}||t          |                                          dk    r|                    d          }t          |                                          dk    r|                    d          }|                    d          }|	                    d|          }|	                    d|          }t          |          } |||          } |||          }||z   dz  }|s||f|dd         z   }||f|z   n|S t          ||||j        |j        	          S )
aZ  
        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:

        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", trust_remote_code=True)
        >>> 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
        ```N
r   r   rR   r   r   r   r@  rA  rs   r>   r   r   r2   rn   )ignore_indexr3   )r  start_logits
end_logitsr   r:  )r,   r  r   r  splitsqueezer   r  r5   clampr   r   r   r:  )r+   r   r   r   rR   r   r   r  r  r   r@  rA  rs   r>   r   r  r  r  r  
total_lossignored_indexr  
start_lossend_lossr   s                            r/   rC   z&LayoutLMv3ForQuestionAnswering.forward  s   v &1%<kk$+B]//))%'/!5#% " 
 
 "!*11#)<<r<#:#: j#++B//::<<''++6688

&=+D?''))**Q.."1"9"9""="==%%''((1,, - 5 5b 9 9(--a00M-33A}EEO)//=AAM']CCCH!,@@Jx
M::H$x/14J 	R"J/'!""+=F/9/EZMF**6Q+%!!/)
 
 
 	
r0   )NNNNNNNNNNNNN)rF   rG   rH   r   r   r  r  r   r   r  r   r`   r  r  r  r   r   rC   rJ   rK   s   @r/   r  r  t  s            +*.556STT  +GVefff 156:593715596:48,0/3&*+/37i
 i
E,-i
 !!23i
 !!12	i

 u/0i
 E-.i
   12i
 "%"23i
   01i
 $D>i
 'tni
 d^i
 u'(i
 u/0i
 
u22	3i
 i
 i
 gf i
 i
 i
 i
 i
r0   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                       e Zd Z fdZ ee                    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         de
e         de
e         de
ej                 de
ej                 deeef         fd                        Z xZS )#LayoutLMv3ForSequenceClassificationc                     t                                          |           |j        | _        || _        t	          |          | _        t          |d          | _        |                                  d S r  )	r   r   r  r,   rS  r   r  r  r`  rl   s     r/   r   z,LayoutLMv3ForSequenceClassification.__init__  sg        +)&116vERRRr0   r  r  Nr   r   r   rR   r   r   r  r   r@  rA  rs   r>   r   c                    |
|
n| j         j        }
|                     ||||||||	|
||          }|d         dddddf         }|                     |          }d}|Z| j         j        f| j        dk    rd| j         _        nN| j        dk    r7|j        t          j        k    s|j        t          j	        k    rd| j         _        nd| j         _        | j         j        dk    rWt                      }| j        dk    r1 ||                                |                                          }n |||          }n| j         j        dk    rGt                      } ||                    d| j                  |                    d                    }n*| j         j        dk    rt                      } |||          }|
s|f|dd         z   }||f|z   n|S t          |||j        |j        	          S )
a  
        Returns:

        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", trust_remote_code=True)
        >>> 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
        ```Nr  r   r   
regressionsingle_label_classificationmulti_label_classificationr2   r  )r,   r  r   r  problem_typer  r   r`   r   r}   r   r  r   r7   r   r   r   r:  )r+   r   r   r   rR   r   r   r  r   r@  rA  rs   r>   r   r  r  r  r  r   s                      r/   rC   z+LayoutLMv3ForSequenceClassification.forward  s%   Z &1%<kk$+B]//))%'/!5#% " 
 
 "!*QQQ111W-11{'/?a''/;DK,,_q((flej.H.HFL\a\eLeLe/LDK,,/KDK,{'<77"99?a''#8FNN$4$4fnn6F6FGGDD#8FF33DD)-JJJ+--xB @ @&++b//RR)-III,..x// 	FY,F)-)9TGf$$vE'!/)	
 
 
 	
r0   r  )rF   rG   rH   r   r   r  r  r   r   r  r   r`   r  r  r  r   r   rC   rJ   rK   s   @r/   r  r    s            +*.556STT  +CRabbb 156:59371559-1,0/3&*+/37\
 \
E,-\
 !!23\
 !!12	\

 u/0\
 E-.\
   12\
 )*\
 $D>\
 'tn\
 d^\
 u'(\
 u/0\
 
u..	/\
 \
 \
 cb \
 \
 \
 \
 \
r0   r  );rI   r!   r   typingr   r   r   r`   torch.nnr&   torch.nn.functional
functionalr:   torch.utils.checkpointr   r   r   activationsr
   modeling_outputsr   r   r   r   modeling_utilsr   pytorch_utilsr   utilsr   r   r   r   r   configuration_layoutlmv3r   
get_loggerrF   loggerr  LAYOUTLMV3_START_DOCSTRINGr  r  Moduler   rM   r   r   r   r   r   r   r   r   rS  r  r  r  r  r   r0   r/   <module>r     s           ) ) ) ) ) ) ) ) ) )                     A A A A A A A A A A ! ! ! ! ! !            . - - - - - 6 6 6 6 6 6              7 6 6 6 6 6 
	H	%	%$	 J% !X;* &|         	      Fs s s s sry s s sl* * * * * * * *4W W W W Wbi W W Wv    29       ")   :( ( ( ( (bi ( ( (VZ
 Z
 Z
 Z
 Z
	 Z
 Z
 Z
|    RY        ry    j x
 x
 x
 x
 x
/ x
 x
	 x
v    29   6   e
 e
 e
 e
 e
'@ e
 e
 e
P 
  w
 w
 w
 w
 w
%> w
 w
 w
t 
  j
 j
 j
 j
 j
*C j
 j
 j
 j
 j
r0   