
    g3                         d 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 ddlmZ dd	lmZ erdd
lmZ ddlmZ  ej        e          Z G d de          Z G d de          ZdS )zLayoutLMv3 model configuration    )OrderedDict)TYPE_CHECKINGAnyMappingOptional)version   )PretrainedConfig)
OnnxConfig) compute_effective_axis_dimension)logging)ProcessorMixin)
TensorTypec                   d     e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z xZS )LayoutLMv3Configa  
    This is the configuration class to store the configuration of a [`LayoutLMv3Model`]. It is used to instantiate an
    LayoutLMv3 model according to the specified arguments, defining the model architecture. Instantiating a
    configuration with the defaults will yield a similar configuration to that of the LayoutLMv3
    [microsoft/layoutlmv3-base](https://huggingface.co/microsoft/layoutlmv3-base) architecture.

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.

    Args:
        vocab_size (`int`, *optional*, defaults to 50265):
            Vocabulary size of the LayoutLMv3 model. Defines the number of different tokens that can be represented by
            the `inputs_ids` passed when calling [`LayoutLMv3Model`].
        hidden_size (`int`, *optional*, defaults to 768):
            Dimension of the encoder layers and the pooler layer.
        num_hidden_layers (`int`, *optional*, defaults to 12):
            Number of hidden layers in the Transformer encoder.
        num_attention_heads (`int`, *optional*, defaults to 12):
            Number of attention heads for each attention layer in the Transformer encoder.
        intermediate_size (`int`, *optional*, defaults to 3072):
            Dimension of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
            `"relu"`, `"selu"` and `"gelu_new"` are supported.
        hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
        attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
            The dropout ratio for the attention probabilities.
        max_position_embeddings (`int`, *optional*, defaults to 512):
            The maximum sequence length that this model might ever be used with. Typically set this to something large
            just in case (e.g., 512 or 1024 or 2048).
        type_vocab_size (`int`, *optional*, defaults to 2):
            The vocabulary size of the `token_type_ids` passed when calling [`LayoutLMv3Model`].
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        layer_norm_eps (`float`, *optional*, defaults to 1e-5):
            The epsilon used by the layer normalization layers.
        max_2d_position_embeddings (`int`, *optional*, defaults to 1024):
            The maximum value that the 2D position embedding might ever be used with. Typically set this to something
            large just in case (e.g., 1024).
        coordinate_size (`int`, *optional*, defaults to `128`):
            Dimension of the coordinate embeddings.
        shape_size (`int`, *optional*, defaults to `128`):
            Dimension of the width and height embeddings.
        has_relative_attention_bias (`bool`, *optional*, defaults to `True`):
            Whether or not to use a relative attention bias in the self-attention mechanism.
        rel_pos_bins (`int`, *optional*, defaults to 32):
            The number of relative position bins to be used in the self-attention mechanism.
        max_rel_pos (`int`, *optional*, defaults to 128):
            The maximum number of relative positions to be used in the self-attention mechanism.
        max_rel_2d_pos (`int`, *optional*, defaults to 256):
            The maximum number of relative 2D positions in the self-attention mechanism.
        rel_2d_pos_bins (`int`, *optional*, defaults to 64):
            The number of 2D relative position bins in the self-attention mechanism.
        has_spatial_attention_bias (`bool`, *optional*, defaults to `True`):
            Whether or not to use a spatial attention bias in the self-attention mechanism.
        visual_embed (`bool`, *optional*, defaults to `True`):
            Whether or not to add patch embeddings.
        input_size (`int`, *optional*, defaults to `224`):
            The size (resolution) of the images.
        num_channels (`int`, *optional*, defaults to `3`):
            The number of channels of the images.
        patch_size (`int`, *optional*, defaults to `16`)
            The size (resolution) of the patches.
        classifier_dropout (`float`, *optional*):
            The dropout ratio for the classification head.

    Example:

    ```python
    >>> from transformers import LayoutLMv3Config, LayoutLMv3Model

    >>> # Initializing a LayoutLMv3 microsoft/layoutlmv3-base style configuration
    >>> configuration = LayoutLMv3Config()

    >>> # Initializing a model (with random weights) from the microsoft/layoutlmv3-base style configuration
    >>> model = LayoutLMv3Model(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```
layoutlmv3Y           gelu皙?      {Gz?h㈵>   r         T    @         r	      Nc                 ,    t                      j        d|||||||||	|
|||||d| || _        || _        || _        || _        || _        || _        || _        || _	        || _
        || _        || _        || _        || _        || _        || _        d S )N)
vocab_sizehidden_sizenum_hidden_layersnum_attention_headsintermediate_size
hidden_acthidden_dropout_probattention_probs_dropout_probmax_position_embeddingstype_vocab_sizeinitializer_rangelayer_norm_epspad_token_idbos_token_ideos_token_id )super__init__max_2d_position_embeddingscoordinate_size
shape_sizehas_relative_attention_biasrel_pos_binsmax_rel_poshas_spatial_attention_biasrel_2d_pos_binsmax_rel_2d_pos
text_embedvisual_embed
input_sizenum_channels
patch_sizeclassifier_dropout)!selfr&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r8   r9   r:   r;   r<   r=   r?   r@   r>   rA   rB   rC   rD   rE   rF   kwargs	__class__s!                                   s/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/layoutlmv3/configuration_layoutlmv3.pyr7   zLayoutLMv3Config.__init__y   s    D 	 	
!#/ 3/! 3)E$;+/)%%%	
 	
  !	
 	
 	
$ +E'.$+F((&*D'.,$($($"4    )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Tr    r   r!   r"   TTTr#   r	   r$   N)__name__
__module____qualname____doc__
model_typer7   __classcell__)rI   s   @rJ   r   r   $   s        P Pd J %( ##'$(#'?B5 B5 B5 B5 B5 B5 B5 B5 B5 B5rK   r   c                       e Zd Z ej        d          Zedeeee	ef         f         fd            Z
edefd            Zede	fd            Z	 	 	 	 	 	 	 dddde	de	deded         de	de	de	deeef         fdZdS )LayoutLMv3OnnxConfigz1.12returnc                     | j         dv r)t          ddddfddddfddddfddd	d
ddfg          S t          ddddfddddfddddfddd	dfg          S )N)zquestion-answeringzsequence-classification	input_idsbatchsequence)r   r   attention_maskbboxpixel_valuesrD   heightwidth)r   r   r   r	   )taskr   rG   s    rJ   inputszLayoutLMv3OnnxConfig.inputs   s     9III g*"="=>%7z'B'BCZ889#^U\%]%]^	    g*"="=>Z889%7z'B'BC#^%D%DE	  rK   c                     dS )Nr   r5   r_   s    rJ   atol_for_validationz(LayoutLMv3OnnxConfig.atol_for_validation   s    trK   c                     dS )Nr   r5   r_   s    rJ   default_onnx_opsetz'LayoutLMv3OnnxConfig.default_onnx_opset   s    rrK   FNr	   (   	processorr   
batch_size
seq_lengthis_pair	frameworkr   rD   image_widthimage_heightc	                    t          |j        dd           t          |t          j        d          }|j                            |          }	t          |t          j        |	          }d                    |j        j	        g          |z  gg|z  }
g dgg|z  }| 
                    ||||          }t           |||
||                    }|S )a  
        Generate inputs to provide to the ONNX exporter for the specific framework

        Args:
            processor ([`ProcessorMixin`]):
                The processor associated with this model configuration.
            batch_size (`int`, *optional*, defaults to -1):
                The batch size to export the model for (-1 means dynamic axis).
            seq_length (`int`, *optional*, defaults to -1):
                The sequence length to export the model for (-1 means dynamic axis).
            is_pair (`bool`, *optional*, defaults to `False`):
                Indicate if the input is a pair (sentence 1, sentence 2).
            framework (`TensorType`, *optional*, defaults to `None`):
                The framework (PyTorch or TensorFlow) that the processor will generate tensors for.
            num_channels (`int`, *optional*, defaults to 3):
                The number of channels of the generated images.
            image_width (`int`, *optional*, defaults to 40):
                The width of the generated images.
            image_height (`int`, *optional*, defaults to 40):
                The height of the generated images.

        Returns:
            Mapping[str, Any]: holding the kwargs to provide to the model's forward function
        	apply_ocrFr   )fixed_dimensionnum_token_to_add )0   T   I   r   )textboxesreturn_tensors)setattrimage_processorr   r   default_fixed_batch	tokenizernum_special_tokens_to_adddefault_fixed_sequencejoin	unk_token_generate_dummy_imagesdict)rG   rg   rh   ri   rj   rk   rD   rl   rm   token_to_add
dummy_textdummy_bboxesdummy_imager`   s                 rJ   generate_dummy_inputsz*LayoutLMv3OnnxConfig.generate_dummy_inputs   s	   J 		);>>> 6
(FYZ
 
 

 !*DDWMM5
(I\h
 
 

 xx!4!> ?@@:MNOR\\
 +**+,z9 11*lLZeffI"(	  
 
 rK   )re   re   FNr	   rf   rf   )rL   rM   rN   r   parsetorch_onnx_minimum_versionpropertyr   strintr`   floatrb   rd   boolr   r   r   r5   rK   rJ   rS   rS      sR       !.v!6!6WS#X%6 67    X* U    X C    X ,0C C#C C 	C
 C L)C C C C 
c	C C C C C CrK   rS   N)rO   collectionsr   typingr   r   r   r   	packagingr   configuration_utilsr
   onnxr   
onnx.utilsr   utilsr   processing_utilsr   r   
get_loggerrL   loggerr   rS   r5   rK   rJ   <module>r      sQ   % $ # # # # # # 8 8 8 8 8 8 8 8 8 8 8 8       3 3 3 3 3 3       : : : : : :        $222222###### 
	H	%	%W5 W5 W5 W5 W5' W5 W5 W5td d d d d: d d d d drK   