


import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     str			= ['image_processor', 'tokenizer']
    A_  :     int			= 'LayoutLMv2ImageProcessor'
    A_  :     str			= ('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast')





    def __init__(	self				:   Union[str, Any]       ,   __snake_case				:   List[Any]=None       ,   __snake_case				:   List[str]=None       ,   **__snake_case				:   Optional[int]	):





       '''simple docstring'''
       if "feature_extractor" in kwargs:
          warnings.warn(
              '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
              ''' instead.'''       ,   __snake_case       ,   )
          UpperCAmelCase_							:      List[Any]						=   kwargs.pop('''feature_extractor'''	)

       UpperCAmelCase_							:      Optional[int]						=   image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
          raise ValueError('''You need to specify an `image_processor`.'''	)
       if tokenizer is None:
          raise ValueError('''You need to specify a `tokenizer`.'''	)

       super().__init__(__snake_case       ,   __snake_case	)





    def __call__(	self				:   List[str]       ,   __snake_case				:   Dict       ,   __snake_case				:   Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None       ,   __snake_case				:   Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None       ,   __snake_case				:   Union[List[List[int]], List[List[List[int]]]] = None       ,   __snake_case				:   Optional[Union[List[int], List[List[int]]]] = None       ,   __snake_case				:   bool = True       ,   __snake_case				:   Union[bool, str, PaddingStrategy] = False       ,   __snake_case				:   Union[bool, str, TruncationStrategy] = None       ,   __snake_case				:   Optional[int] = None       ,   __snake_case				:   int = 0       ,   __snake_case				:   Optional[int] = None       ,   __snake_case				:   Optional[bool] = None       ,   __snake_case				:   Optional[bool] = None       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = True       ,   __snake_case				:   Optional[Union[str, TensorType]] = None       ,   **__snake_case				:   Optional[int]       ,   ):





       '''simple docstring'''
       # verify input
       if self.image_processor.apply_ocr and (boxes is not None):
          raise ValueError(
              '''You cannot provide bounding boxes '''
              '''if you initialized the image processor with apply_ocr set to True.'''	)

       if self.image_processor.apply_ocr and (word_labels is not None):
          raise ValueError(
              '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'''	)

       if return_overflowing_tokens is True and return_offsets_mapping is False:
          raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.'''	)

       # first, apply the image processor
       UpperCAmelCase_							:      Tuple						=   self.image_processor(images=__snake_case       ,   return_tensors=__snake_case	)

       # second, apply the tokenizer
       if text is not None and self.image_processor.apply_ocr and text_pair is None:
          if isinstance(__snake_case       ,   __snake_case	):
             UpperCAmelCase_							:      Optional[Any]						=   [text]  # add batch dimension (as the image processor always adds a batch dimension)
          UpperCAmelCase_							:      Any						=   features['''words''']

       UpperCAmelCase_							:      str						=   self.tokenizer(
           text=text if text is not None else features['''words''']       ,   text_pair=text_pair if text_pair is not None else None       ,   boxes=boxes if boxes is not None else features['''boxes''']       ,   word_labels=__snake_case       ,   add_special_tokens=__snake_case       ,   padding=__snake_case       ,   truncation=__snake_case       ,   max_length=__snake_case       ,   stride=__snake_case       ,   pad_to_multiple_of=__snake_case       ,   return_token_type_ids=__snake_case       ,   return_attention_mask=__snake_case       ,   return_overflowing_tokens=__snake_case       ,   return_special_tokens_mask=__snake_case       ,   return_offsets_mapping=__snake_case       ,   return_length=__snake_case       ,   verbose=__snake_case       ,   return_tensors=__snake_case       ,   **__snake_case       ,   )

       # add pixel values
       UpperCAmelCase_							:      List[str]						=   features.pop('''pixel_values'''	)
       if return_overflowing_tokens is True:
          UpperCAmelCase_							:      Optional[int]						=   self.get_overflowing_images(__snake_case       ,   encoded_inputs['''overflow_to_sample_mapping''']	)
       UpperCAmelCase_							:      List[Any]						=   images

       return encoded_inputs





    def 					_lowerCamelCase (	self				:   Union[str, Any]       ,   __snake_case				:   str       ,   __snake_case				:   List[Any]	):





       '''simple docstring'''
       # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
       UpperCAmelCase_							:      List[str]						=   []
       for sample_idx in overflow_to_sample_mapping:
          images_with_overflow.append(images[sample_idx]	)

       if len(__snake_case	) != len(__snake_case	):
          raise ValueError(
              '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
              f''' {len(__snake_case	)} and {len(__snake_case	)}'''	)

       return images_with_overflow





    def 					_lowerCamelCase (	self				:   List[Any]       ,   *__snake_case				:   Optional[int]       ,   **__snake_case				:   List[Any]	):





       '''simple docstring'''
       return self.tokenizer.batch_decode(*__snake_case       ,   **__snake_case	)





    def 					_lowerCamelCase (	self				:   str       ,   *__snake_case				:   Optional[Any]       ,   **__snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       return self.tokenizer.decode(*__snake_case       ,   **__snake_case	)





    @property
    def 					_lowerCamelCase (	self				:   Optional[Any]	):





       '''simple docstring'''
       return ["input_ids", "bbox", "attention_mask", "image"]





    @property
    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       warnings.warn(
           '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''       ,   __snake_case       ,   )
       return self.image_processor_class





    @property
    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       warnings.warn(
           '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''       ,   __snake_case       ,   )
       return self.image_processor



import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCamelCase				:  Union[str, Any]      					=							logging.get_logger(__name__)

__UpperCamelCase				:  str      					=							{
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     List[Any]			= 'unispeech-sat'





    def __init__(	self				:   int       ,   __snake_case				:   Optional[int]=32       ,   __snake_case				:   Dict=768       ,   __snake_case				:   Optional[Any]=12       ,   __snake_case				:   Optional[int]=12       ,   __snake_case				:   Dict=3_072       ,   __snake_case				:   List[str]="gelu"       ,   __snake_case				:   Any=0.1       ,   __snake_case				:   Tuple=0.1       ,   __snake_case				:   Optional[int]=0.1       ,   __snake_case				:   Tuple=0.0       ,   __snake_case				:   List[Any]=0.0       ,   __snake_case				:   Tuple=0.1       ,   __snake_case				:   Union[str, Any]=0.1       ,   __snake_case				:   Dict=0.02       ,   __snake_case				:   Optional[Any]=1E-5       ,   __snake_case				:   Optional[int]="group"       ,   __snake_case				:   str="gelu"       ,   __snake_case				:   Union[str, Any]=(512, 512, 512, 512, 512, 512, 512)       ,   __snake_case				:   str=(5, 2, 2, 2, 2, 2, 2)       ,   __snake_case				:   Tuple=(10, 3, 3, 3, 3, 2, 2)       ,   __snake_case				:   int=False       ,   __snake_case				:   Optional[int]=128       ,   __snake_case				:   Any=16       ,   __snake_case				:   Union[str, Any]=False       ,   __snake_case				:   Union[str, Any]=True       ,   __snake_case				:   List[Any]=0.05       ,   __snake_case				:   Dict=10       ,   __snake_case				:   int=2       ,   __snake_case				:   Optional[Any]=0.0       ,   __snake_case				:   Optional[int]=10       ,   __snake_case				:   List[Any]=0       ,   __snake_case				:   Optional[int]=320       ,   __snake_case				:   int=2       ,   __snake_case				:   Any=0.1       ,   __snake_case				:   Optional[int]=100       ,   __snake_case				:   Tuple=256       ,   __snake_case				:   List[str]=256       ,   __snake_case				:   List[Any]=0.1       ,   __snake_case				:   Tuple="mean"       ,   __snake_case				:   List[Any]=False       ,   __snake_case				:   List[str]=False       ,   __snake_case				:   Optional[Any]=256       ,   __snake_case				:   Tuple=(512, 512, 512, 512, 1_500)       ,   __snake_case				:   Optional[int]=(5, 3, 3, 1, 1)       ,   __snake_case				:   Any=(1, 2, 3, 1, 1)       ,   __snake_case				:   int=512       ,   __snake_case				:   Optional[int]=0       ,   __snake_case				:   Dict=1       ,   __snake_case				:   Tuple=2       ,   __snake_case				:   Union[str, Any]=504       ,   **__snake_case				:   List[str]       ,   ):





       '''simple docstring'''
       super().__init__(**__snake_case       ,   pad_token_id=__snake_case       ,   bos_token_id=__snake_case       ,   eos_token_id=__snake_case	)
       UpperCAmelCase_							:      Union[str, Any]						=   hidden_size
       UpperCAmelCase_							:      int						=   feat_extract_norm
       UpperCAmelCase_							:      Dict						=   feat_extract_activation
       UpperCAmelCase_							:      Union[str, Any]						=   list(__snake_case	)
       UpperCAmelCase_							:      List[str]						=   list(__snake_case	)
       UpperCAmelCase_							:      Any						=   list(__snake_case	)
       UpperCAmelCase_							:      Any						=   conv_bias
       UpperCAmelCase_							:      List[str]						=   num_conv_pos_embeddings
       UpperCAmelCase_							:      Dict						=   num_conv_pos_embedding_groups
       UpperCAmelCase_							:      Optional[int]						=   len(self.conv_dim	)
       UpperCAmelCase_							:      List[str]						=   num_hidden_layers
       UpperCAmelCase_							:      Dict						=   intermediate_size
       UpperCAmelCase_							:      Union[str, Any]						=   hidden_act
       UpperCAmelCase_							:      str						=   num_attention_heads
       UpperCAmelCase_							:      Union[str, Any]						=   hidden_dropout
       UpperCAmelCase_							:      List[str]						=   attention_dropout
       UpperCAmelCase_							:      Optional[Any]						=   activation_dropout
       UpperCAmelCase_							:      Dict						=   feat_proj_dropout
       UpperCAmelCase_							:      Optional[Any]						=   final_dropout
       UpperCAmelCase_							:      List[Any]						=   layerdrop
       UpperCAmelCase_							:      int						=   layer_norm_eps
       UpperCAmelCase_							:      int						=   initializer_range
       UpperCAmelCase_							:      List[Any]						=   vocab_size
       UpperCAmelCase_							:      int						=   num_clusters
       UpperCAmelCase_							:      int						=   do_stable_layer_norm
       UpperCAmelCase_							:      Any						=   use_weighted_layer_sum

       if (
           (len(self.conv_stride	) != self.num_feat_extract_layers)
           or (len(self.conv_kernel	) != self.num_feat_extract_layers)
           or (len(self.conv_dim	) != self.num_feat_extract_layers)
       ):
          raise ValueError(
              '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
              ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
              f''' {len(self.conv_dim	)}`, `len(config.conv_stride) = {len(self.conv_stride	)}`,'''
              f''' `len(config.conv_kernel) = {len(self.conv_kernel	)}`.'''	)

       # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
       UpperCAmelCase_							:      int						=   apply_spec_augment
       UpperCAmelCase_							:      Optional[Any]						=   mask_time_prob
       UpperCAmelCase_							:      str						=   mask_time_length
       UpperCAmelCase_							:      Any						=   mask_time_min_masks
       UpperCAmelCase_							:      str						=   mask_feature_prob
       UpperCAmelCase_							:      str						=   mask_feature_length
       UpperCAmelCase_							:      Tuple						=   mask_feature_min_masks

       # parameters for pretraining with codevector quantized representations
       UpperCAmelCase_							:      Optional[int]						=   num_codevectors_per_group
       UpperCAmelCase_							:      int						=   num_codevector_groups
       UpperCAmelCase_							:      List[str]						=   contrastive_logits_temperature
       UpperCAmelCase_							:      int						=   feat_quantizer_dropout
       UpperCAmelCase_							:      List[str]						=   num_negatives
       UpperCAmelCase_							:      Any						=   codevector_dim
       UpperCAmelCase_							:      Tuple						=   proj_codevector_dim
       UpperCAmelCase_							:      Union[str, Any]						=   diversity_loss_weight

       # ctc loss
       UpperCAmelCase_							:      Any						=   ctc_loss_reduction
       UpperCAmelCase_							:      Optional[Any]						=   ctc_zero_infinity

       # SequenceClassification-specific parameter. Feel free to ignore for other classes.
       UpperCAmelCase_							:      Optional[int]						=   classifier_proj_size

       # XVector-specific parameters. Feel free to ignore for other classes.
       UpperCAmelCase_							:      Dict						=   list(__snake_case	)
       UpperCAmelCase_							:      Dict						=   list(__snake_case	)
       UpperCAmelCase_							:      Dict						=   list(__snake_case	)
       UpperCAmelCase_							:      Union[str, Any]						=   xvector_output_dim





    @property
    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       return functools.reduce(operator.mul       ,   self.conv_stride       ,   1	)