




import copy
import os
from typing import Union

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


SCREAMING_SNAKE_CASE__       :      List[str]  =   logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__       :      Dict  =   {
    'google/pix2struct-textcaps-base': (
        'https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json'
    ),
}




class 		UpperCamelCase__	(__SCREAMING_SNAKE_CASE     ):

    '''simple docstring'''



    lowerCamelCase_      :	int	     =      """pix2struct_text_model"""
    lowerCamelCase_      :	Any	     =      ["""past_key_values"""]
    lowerCamelCase_      :	str	     =      {
        """hidden_size""": """hidden_size""",
        """num_attention_heads""": """num_heads""",
        """num_hidden_layers""": """num_layers""",
    }







    def __init__(    self     ,  UpperCamelCase__=5_0244     ,  UpperCamelCase__=768     ,  UpperCamelCase__=64     ,  UpperCamelCase__=2048     ,  UpperCamelCase__=12     ,  UpperCamelCase__=12     ,  UpperCamelCase__=32     ,  UpperCamelCase__=128     ,  UpperCamelCase__=0.1     ,  UpperCamelCase__=1e-6     ,  UpperCamelCase__=1.0     ,  UpperCamelCase__="gelu_new"     ,  UpperCamelCase__=0     ,  UpperCamelCase__=False     ,  UpperCamelCase__=0     ,  UpperCamelCase__=1     ,  UpperCamelCase__=False     ,  UpperCamelCase__=True     ,  **UpperCamelCase__     ,  )     ->      Optional[Any]:
           lowerCamelCase   :      Any			 =					vocab_size
           lowerCamelCase   :      Union[str, Any]			 =					hidden_size
           lowerCamelCase   :      Tuple			 =					d_kv
           lowerCamelCase   :      Optional[int]			 =					d_ff
           lowerCamelCase   :      Any			 =					num_layers
           lowerCamelCase   :      int			 =					num_heads
           lowerCamelCase   :      Any			 =					relative_attention_num_buckets
           lowerCamelCase   :      List[str]			 =					relative_attention_max_distance
           lowerCamelCase   :      Optional[int]			 =					dropout_rate
           lowerCamelCase   :      Optional[int]			 =					layer_norm_epsilon
           lowerCamelCase   :      Dict			 =					initializer_factor
           lowerCamelCase   :      List[Any]			 =					use_cache

           lowerCamelCase   :      Union[str, Any]			 =					eos_token_id
           lowerCamelCase   :      List[Any]			 =					decoder_start_token_id

           # for backwards compatibility
           lowerCamelCase   :      int			 =					dense_act_fn

           super().__init__(
               pad_token_id=UpperCAmelCase_     ,  eos_token_id=UpperCAmelCase_     ,  decoder_start_token_id=UpperCAmelCase_     ,  tie_word_embeddings=UpperCAmelCase_     ,  is_decoder=UpperCAmelCase_     ,  **UpperCAmelCase_     ,  )







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      "PretrainedConfig":
           cls._set_token_in_kwargs(UpperCAmelCase_     )

           lowerCamelCase   :      List[Any]			 =					cls.get_config_dict(UpperCAmelCase_     ,  **UpperCAmelCase_     )

           # get the text config dict if we are loading from Pix2StructConfig
           if config_dict.get("model_type"     ) == "pix2struct":
                  lowerCamelCase   :      Optional[Any]			 =					config_dict["text_config"]

           if "model_type" in config_dict and hasattr(cls     ,  "model_type"     ) and config_dict["model_type"] != cls.model_type:
                  logger.warning(
                      F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
                      F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''     )

           return cls.from_dict(UpperCAmelCase_     ,  **UpperCAmelCase_     )




class 		UpperCamelCase__	(__SCREAMING_SNAKE_CASE     ):

    '''simple docstring'''



    lowerCamelCase_      :	List[Any]	     =      """pix2struct_vision_model"""







    def __init__(    self     ,  UpperCamelCase__=768     ,  UpperCamelCase__=768     ,  UpperCamelCase__=2048     ,  UpperCamelCase__=64     ,  UpperCamelCase__=12     ,  UpperCamelCase__=12     ,  UpperCamelCase__="gelu_new"     ,  UpperCamelCase__=1e-6     ,  UpperCamelCase__=0.0     ,  UpperCamelCase__=0.0     ,  UpperCamelCase__=1e-10     ,  UpperCamelCase__=1.0     ,  UpperCamelCase__=4096     ,  UpperCamelCase__=32     ,  UpperCamelCase__=128     ,  **UpperCamelCase__     ,  )     ->      Optional[Any]:
           super().__init__(**UpperCAmelCase_     )

           lowerCamelCase   :      str			 =					hidden_size
           lowerCamelCase   :      Union[str, Any]			 =					patch_embed_hidden_size
           lowerCamelCase   :      str			 =					d_ff
           lowerCamelCase   :      Any			 =					dropout_rate
           lowerCamelCase   :      int			 =					num_hidden_layers
           lowerCamelCase   :      int			 =					num_attention_heads
           lowerCamelCase   :      str			 =					initializer_range
           lowerCamelCase   :      Any			 =					initializer_factor
           lowerCamelCase   :      Union[str, Any]			 =					attention_dropout
           lowerCamelCase   :      Tuple			 =					layer_norm_eps
           lowerCamelCase   :      int			 =					dense_act_fn
           lowerCamelCase   :      Tuple			 =					seq_len
           lowerCamelCase   :      Optional[Any]			 =					relative_attention_num_buckets
           lowerCamelCase   :      int			 =					relative_attention_max_distance
           lowerCamelCase   :      Dict			 =					d_kv







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      "PretrainedConfig":
           cls._set_token_in_kwargs(UpperCAmelCase_     )

           lowerCamelCase   :      str			 =					cls.get_config_dict(UpperCAmelCase_     ,  **UpperCAmelCase_     )

           # get the vision config dict if we are loading from Pix2StructConfig
           if config_dict.get("model_type"     ) == "pix2struct":
                  lowerCamelCase   :      Tuple			 =					config_dict["vision_config"]

           if "model_type" in config_dict and hasattr(cls     ,  "model_type"     ) and config_dict["model_type"] != cls.model_type:
                  logger.warning(
                      F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
                      F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''     )

           return cls.from_dict(UpperCAmelCase_     ,  **UpperCAmelCase_     )




class 		UpperCamelCase__	(__SCREAMING_SNAKE_CASE     ):

    '''simple docstring'''



    lowerCamelCase_      :	Optional[int]	     =      """pix2struct"""
    lowerCamelCase_      :	List[str]	     =      True







    def __init__(    self     ,  UpperCamelCase__=None     ,  UpperCamelCase__=None     ,  UpperCamelCase__=1.0     ,  UpperCamelCase__=0.02     ,  UpperCamelCase__=False     ,  UpperCamelCase__=False     ,  UpperCamelCase__=True     ,  **UpperCamelCase__     ,  )     ->      List[str]:
           super().__init__(tie_word_embeddings=UpperCAmelCase_     ,  is_encoder_decoder=UpperCAmelCase_     ,  **UpperCAmelCase_     )

           if text_config is None:
                  lowerCamelCase   :      List[str]			 =					{}
                  logger.info("text_config is None. Initializing the Pix2StructTextConfig with default values."     )

           if vision_config is None:
                  lowerCamelCase   :      str			 =					{}
                  logger.info("vision_config is None. Initializing the Pix2StructVisionConfig with default values."     )

           lowerCamelCase   :      int			 =					PixaStructTextConfig(**UpperCAmelCase_     )
           lowerCamelCase   :      int			 =					PixaStructVisionConfig(**UpperCAmelCase_     )

           lowerCamelCase   :      Union[str, Any]			 =					self.text_config.decoder_start_token_id
           lowerCamelCase   :      Tuple			 =					self.text_config.pad_token_id
           lowerCamelCase   :      List[str]			 =					self.text_config.eos_token_id

           lowerCamelCase   :      Union[str, Any]			 =					initializer_factor
           lowerCamelCase   :      str			 =					initializer_range

           lowerCamelCase   :      Optional[int]			 =					self.initializer_range
           lowerCamelCase   :      Dict			 =					self.initializer_range

           lowerCamelCase   :      str			 =					is_vqa







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      Optional[Any]:
           return cls(text_config=text_config.to_dict()     ,  vision_config=vision_config.to_dict()     ,  **UpperCAmelCase_     )







    def     _lowercase     (    self     )     ->      List[Any]:
           lowerCamelCase   :      int			 =					copy.deepcopy(self.__dict__     )
           lowerCamelCase   :      List[Any]			 =					self.text_config.to_dict()
           lowerCamelCase   :      List[str]			 =					self.vision_config.to_dict()
           lowerCamelCase   :      Dict			 =					self.__class__.model_type
           return output













import functools
import operator

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


__A									=				logging.get_logger(__name__)

__A									=				{
    "microsoft/unispeech-large-1500h-cv": (
        "https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}



class    _SCREAMING_SNAKE_CASE						(	__SCREAMING_SNAKE_CASE    ):







 '''simple docstring'''
 lowercase_		=			"unispeech"

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



   '''simple docstring'''



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

   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
   lowerCamelCase__:      int			  =apply_spec_augment
   lowerCamelCase__:      List[str]			  =mask_time_prob
   lowerCamelCase__:      Union[str, Any]			  =mask_time_length
   lowerCamelCase__:      List[Any]			  =mask_time_min_masks
   lowerCamelCase__:      Any			  =mask_feature_prob
   lowerCamelCase__:      Optional[Any]			  =mask_feature_length
   lowerCamelCase__:      List[str]			  =mask_feature_min_masks

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

   # ctc loss
   lowerCamelCase__:      Any			  =ctc_loss_reduction
   lowerCamelCase__:      Dict			  =ctc_zero_infinity

   # pretraining loss
   lowerCamelCase__:      Dict			  =replace_prob







 @property
 def 							SCREAMING_SNAKE_CASE_					(self				:   List[Any])       ->Optional[Any]:



   '''simple docstring'''



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

