




import copy
import os
from typing import Union

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


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

_lowerCamelCase      :						List[str]							=  {
    """google/pix2struct-textcaps-base""": (
        """https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"""
    ),
}


class 							UpperCamelCase_       (					UpperCAmelCase__	):

    '''simple docstring'''






    UpperCAmelCase__				    = '''pix2struct_text_model'''
    UpperCAmelCase__				    = ['''past_key_values''']
    UpperCAmelCase__				    = {
        '''hidden_size''': '''hidden_size''',
        '''num_attention_heads''': '''num_heads''',
        '''num_hidden_layers''': '''num_layers''',
    }


    def __init__(     self :      Dict   ,					UpperCAmelCase__ :      Any=50_244   ,					UpperCAmelCase__ :      Tuple=768   ,					UpperCAmelCase__ :      Union[str, Any]=64   ,					UpperCAmelCase__ :      Union[str, Any]=2_048   ,					UpperCAmelCase__ :      List[str]=12   ,					UpperCAmelCase__ :      Any=12   ,					UpperCAmelCase__ :      int=32   ,					UpperCAmelCase__ :      Optional[int]=128   ,					UpperCAmelCase__ :      str=0.1   ,					UpperCAmelCase__ :      List[str]=1e-6   ,					UpperCAmelCase__ :      Dict=1.0   ,					UpperCAmelCase__ :      Any="gelu_new"   ,					UpperCAmelCase__ :      Dict=0   ,					UpperCAmelCase__ :      Union[str, Any]=False   ,					UpperCAmelCase__ :      int=0   ,					UpperCAmelCase__ :      List[str]=1   ,					UpperCAmelCase__ :      str=False   ,					UpperCAmelCase__ :      Any=True   ,					**UpperCAmelCase__ :      Union[str, Any]   ,					)		->Union[str, Any]:





           '''simple docstring'''

           A__ 					=       vocab_size
           A__ 					=       hidden_size
           A__ 					=       d_kv
           A__ 					=       d_ff
           A__ 					=       num_layers
           A__ 					=       num_heads
           A__ 					=       relative_attention_num_buckets
           A__ 					=       relative_attention_max_distance
           A__ 					=       dropout_rate
           A__ 					=       layer_norm_epsilon
           A__ 					=       initializer_factor
           A__ 					=       use_cache

           A__ 					=       eos_token_id
           A__ 					=       decoder_start_token_id

           # for backwards compatibility
           A__ 					=       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 			SCREAMING_SNAKE_CASE				(     cls :      Optional[Any]   ,					UpperCAmelCase__ :      Union[str, os.PathLike]   ,					**UpperCAmelCase__ :      Any)		->"PretrainedConfig":





           '''simple docstring'''

           cls._set_token_in_kwargs(UpperCAmelCase__)

           A__					,				A__ 					=       cls.get_config_dict(UpperCAmelCase__   ,					**UpperCAmelCase__)

           # get the text config dict if we are loading from Pix2StructConfig
           if config_dict.get('''model_type''') == "pix2struct":
                  A__ 					=       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_       (					UpperCAmelCase__	):

    '''simple docstring'''






    UpperCAmelCase__				    = '''pix2struct_vision_model'''


    def __init__(     self :      Optional[Any]   ,					UpperCAmelCase__ :      Any=768   ,					UpperCAmelCase__ :      int=768   ,					UpperCAmelCase__ :      int=2_048   ,					UpperCAmelCase__ :      int=64   ,					UpperCAmelCase__ :      Optional[Any]=12   ,					UpperCAmelCase__ :      int=12   ,					UpperCAmelCase__ :      List[Any]="gelu_new"   ,					UpperCAmelCase__ :      Union[str, Any]=1e-6   ,					UpperCAmelCase__ :      Dict=0.0   ,					UpperCAmelCase__ :      List[Any]=0.0   ,					UpperCAmelCase__ :      Any=1e-10   ,					UpperCAmelCase__ :      Any=1.0   ,					UpperCAmelCase__ :      Optional[int]=4_096   ,					UpperCAmelCase__ :      Dict=32   ,					UpperCAmelCase__ :      int=128   ,					**UpperCAmelCase__ :      Dict   ,					)		->Optional[Any]:





           '''simple docstring'''

           super().__init__(**UpperCAmelCase__)

           A__ 					=       hidden_size
           A__ 					=       patch_embed_hidden_size
           A__ 					=       d_ff
           A__ 					=       dropout_rate
           A__ 					=       num_hidden_layers
           A__ 					=       num_attention_heads
           A__ 					=       initializer_range
           A__ 					=       initializer_factor
           A__ 					=       attention_dropout
           A__ 					=       layer_norm_eps
           A__ 					=       dense_act_fn
           A__ 					=       seq_len
           A__ 					=       relative_attention_num_buckets
           A__ 					=       relative_attention_max_distance
           A__ 					=       d_kv





    @classmethod
    def 			SCREAMING_SNAKE_CASE				(     cls :      Tuple   ,					UpperCAmelCase__ :      Union[str, os.PathLike]   ,					**UpperCAmelCase__ :      Tuple)		->"PretrainedConfig":





           '''simple docstring'''

           cls._set_token_in_kwargs(UpperCAmelCase__)

           A__					,				A__ 					=       cls.get_config_dict(UpperCAmelCase__   ,					**UpperCAmelCase__)

           # get the vision config dict if we are loading from Pix2StructConfig
           if config_dict.get('''model_type''') == "pix2struct":
                  A__ 					=       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_       (					UpperCAmelCase__	):

    '''simple docstring'''






    UpperCAmelCase__				    = '''pix2struct'''
    UpperCAmelCase__				    = True


    def __init__(     self :      List[str]   ,					UpperCAmelCase__ :      Union[str, Any]=None   ,					UpperCAmelCase__ :      Tuple=None   ,					UpperCAmelCase__ :      str=1.0   ,					UpperCAmelCase__ :      Optional[int]=0.02   ,					UpperCAmelCase__ :      List[Any]=False   ,					UpperCAmelCase__ :      List[Any]=False   ,					UpperCAmelCase__ :      Dict=True   ,					**UpperCAmelCase__ :      List[Any]   ,					)		->Union[str, Any]:





           '''simple docstring'''

           super().__init__(tie_word_embeddings=UpperCAmelCase__   ,					is_encoder_decoder=UpperCAmelCase__   ,					**UpperCAmelCase__)

           if text_config is None:
                  A__ 					=       {}
                  logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.''')

           if vision_config is None:
                  A__ 					=       {}
                  logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.''')

           A__ 					=       PixaStructTextConfig(**UpperCAmelCase__)
           A__ 					=       PixaStructVisionConfig(**UpperCAmelCase__)

           A__ 					=       self.text_config.decoder_start_token_id
           A__ 					=       self.text_config.pad_token_id
           A__ 					=       self.text_config.eos_token_id

           A__ 					=       initializer_factor
           A__ 					=       initializer_range

           A__ 					=       self.initializer_range
           A__ 					=       self.initializer_range

           A__ 					=       is_vqa


    @classmethod
    def 			SCREAMING_SNAKE_CASE				(     cls :      Optional[Any]   ,					UpperCAmelCase__ :      PixaStructTextConfig   ,					UpperCAmelCase__ :      PixaStructVisionConfig   ,					**UpperCAmelCase__ :      Dict)		->Tuple:





           '''simple docstring'''

           return cls(text_config=text_config.to_dict()   ,					vision_config=vision_config.to_dict()   ,					**UpperCAmelCase__)





    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->List[str]:





           '''simple docstring'''

           A__ 					=       copy.deepcopy(self.__dict__)
           A__ 					=       self.text_config.to_dict()
           A__ 					=       self.vision_config.to_dict()
           A__ 					=       self.__class__.model_type
           return output









'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE									=				logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE									=				{
    "facebook/deit-base-distilled-patch16-224": (
        "https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json"
    ),
    # See all DeiT models at https://huggingface.co/models?filter=deit
}

class  lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase    :				Optional[int]								=       "deit"




 def __init__(						self      ,  _lowerCAmelCase=768      ,  _lowerCAmelCase=12      ,  _lowerCAmelCase=12      ,  _lowerCAmelCase=3072      ,  _lowerCAmelCase="gelu"      ,  _lowerCAmelCase=0.0      ,  _lowerCAmelCase=0.0      ,  _lowerCAmelCase=0.02      ,  _lowerCAmelCase=1E-12      ,  _lowerCAmelCase=224      ,  _lowerCAmelCase=16      ,  _lowerCAmelCase=3      ,  _lowerCAmelCase=True      ,  _lowerCAmelCase=16      ,  **_lowerCAmelCase      ,  )     ->				Dict:
     super().__init__(**_lowerCAmelCase		)

     _lowerCAmelCase						      =   hidden_size
     _lowerCAmelCase						      =   num_hidden_layers
     _lowerCAmelCase						      =   num_attention_heads
     _lowerCAmelCase						      =   intermediate_size
     _lowerCAmelCase						      =   hidden_act
     _lowerCAmelCase						      =   hidden_dropout_prob
     _lowerCAmelCase						      =   attention_probs_dropout_prob
     _lowerCAmelCase						      =   initializer_range
     _lowerCAmelCase						      =   layer_norm_eps
     _lowerCAmelCase						      =   image_size
     _lowerCAmelCase						      =   patch_size
     _lowerCAmelCase						      =   num_channels
     _lowerCAmelCase						      =   qkv_bias
     _lowerCAmelCase						      =   encoder_stride







class  lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase    :				List[str]								=       version.parse("1.11"		)




 @property
 def 				_snake_case (						self		)     ->				Mapping[str, Mapping[int, str]]:
     return OrderedDict(
         [
             ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
         ]		)





 @property
 def 				_snake_case (						self		)     ->				float:
     return 1E-4


