


'''simple docstring'''






import copy
import re
class 						__UpperCamelCase	:
							lowercase			:		List[str]   		=  'hp'
							lowercase			:		str   		=  {}
							lowercase			:		List[Any]   		=  None





							@classmethod
							def 		a__   (  cls		:int  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:Optional[Any]			):
									snake_case_		:							Optional[Any]			       =						prefix
									snake_case_		:							Union[str, Any]			       =						defaults
									cls.build_naming_info()





							@staticmethod
							def 		a__   (  _UpperCamelCase		:int  ,_UpperCamelCase		:List[str]			):
									if len(_UpperCamelCase			) == 0:
											return ""
									snake_case_		:							Optional[int]			       =						None
									if any(char.isdigit() for char in word			):
											raise Exception(F'''Parameters should not contain numbers: \'{word}\' contains a number'''			)
									if word in info["short_word"]:
											return info["short_word"][word]
									for prefix_len in range(1  ,len(_UpperCamelCase			) + 1			):
											snake_case_		:							Union[str, Any]			       =						word[:prefix_len]
											if prefix in info["reverse_short_word"]:
													continue
											else:
													snake_case_		:							Optional[Any]			       =						prefix
													break

									if short_word is None:
											# Paranoid fallback
											def int_to_alphabetic(_UpperCamelCase		:str			):
													snake_case_		:							List[Any]			       =						""""""
													while integer != 0:
															snake_case_		:							Any			       =						chr(ord("""A"""			) + integer % 1_0			) + s
															integer //= 1_0
													return s

											snake_case_		:							List[str]			       =						0
											while True:
													snake_case_		:							List[Any]			       =						word + """#""" + int_to_alphabetic(_UpperCamelCase			)
													if sword in info["reverse_short_word"]:
															continue
													else:
															snake_case_		:							Optional[int]			       =						sword
															break

									snake_case_		:							Union[str, Any]			       =						short_word
									snake_case_		:							int			       =						word
									return short_word





							@staticmethod
							def 		a__   (  _UpperCamelCase		:str  ,_UpperCamelCase		:Union[str, Any]			):
									snake_case_		:							Optional[Any]			       =						param_name.split("""_"""			)

									snake_case_		:							Any			       =						[TrialShortNamer.shortname_for_word(_UpperCamelCase  ,_UpperCamelCase			) for word in words]

									# We try to create a separatorless short name, but if there is a collision we have to fallback
									# to a separated short name
									snake_case_		:							Optional[int]			       =						["""""", """_"""]

									for separator in separators:
											snake_case_		:							List[Any]			       =						separator.join(_UpperCamelCase			)
											if shortname not in info["reverse_short_param"]:
													snake_case_		:							Any			       =						shortname
													snake_case_		:							List[str]			       =						param_name
													return shortname

									return param_name





							@staticmethod
							def 		a__   (  _UpperCamelCase		:int  ,_UpperCamelCase		:Optional[int]			):
									snake_case_		:							Tuple			       =						TrialShortNamer.shortname_for_key(_UpperCamelCase  ,_UpperCamelCase			)
									snake_case_		:							List[Any]			       =						short_name
									snake_case_		:							List[str]			       =						param_name





							@classmethod
							def 		a__   (  cls		:str			):
									if cls.NAMING_INFO is not None:
											return

									snake_case_		:							Optional[Any]			       =						{
									    """short_word""": {},
									    """reverse_short_word""": {},
									    """short_param""": {},
									    """reverse_short_param""": {},
									}

									snake_case_		:							Union[str, Any]			       =						list(cls.DEFAULTS.keys()			)

									for k in field_keys:
											cls.add_new_param_name(_UpperCamelCase  ,_UpperCamelCase			)

									snake_case_		:							str			       =						info





							@classmethod
							def 		a__   (  cls		:Optional[int]  ,_UpperCamelCase		:Optional[Any]			):
									cls.build_naming_info()
									assert cls.PREFIX is not None
									snake_case_		:							List[str]			       =						[copy.copy(cls.PREFIX			)]

									for k, v in params.items():
											if k not in cls.DEFAULTS:
													raise Exception(F'''You should provide a default value for the param name {k} with value {v}'''			)
											if v == cls.DEFAULTS[k]:
													# The default value is not added to the name
													continue

											snake_case_		:							Tuple			       =						cls.NAMING_INFO["""short_param"""][k]

											if isinstance(_UpperCamelCase  ,_UpperCamelCase			):
													snake_case_		:							Dict			       =						1 if v else 0

											snake_case_		:							Dict			       =						"""""" if isinstance(_UpperCamelCase  ,(int, float)			) else """-"""
											snake_case_		:							int			       =						F'''{key}{sep}{v}'''
											name.append(_UpperCamelCase			)

									return "_".join(_UpperCamelCase			)





							@classmethod
							def 		a__   (  cls		:int  ,_UpperCamelCase		:Dict			):
									snake_case_		:							str			       =						repr[len(cls.PREFIX			) + 1 :]
									if repr == "":
											snake_case_		:							Optional[Any]			       =						[]
									else:
											snake_case_		:							Union[str, Any]			       =						repr.split("""_"""			)

									snake_case_		:							Optional[int]			       =						{}

									for value in values:
											if "-" in value:
													snake_case_       ,      snake_case_		:							int			       =						value.split("""-"""			)
											else:
													snake_case_		:							Any			       =						re.sub("""[0-9.]"""  ,""""""  ,_UpperCamelCase			)
													snake_case_		:							List[str]			       =						float(re.sub("""[^0-9.]"""  ,""""""  ,_UpperCamelCase			)			)

											snake_case_		:							Dict			       =						cls.NAMING_INFO["""reverse_short_param"""][p_k]

											snake_case_		:							Any			       =						p_v

									for k in cls.DEFAULTS:
											if k not in parameters:
													snake_case_		:							List[str]			       =						cls.DEFAULTS[k]

									return parameters



'''simple docstring'''






import copy
import os
from typing import Union

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


__A	:	str     =     logging.get_logger(__name__)

__A	:	Dict     =     {
    'google/pix2struct-textcaps-base': (
        'https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json'
    ),
}
class 						__UpperCamelCase	(						lowercase__							):
							lowercase			:		Tuple   		=  'pix2struct_text_model'
							lowercase			:		int   		=  ['past_key_values']
							lowercase			:		int   		=  {
							    'hidden_size': 'hidden_size',
							    'num_attention_heads': 'num_heads',
							    'num_hidden_layers': 'num_layers',
							}





							def __init__(  self		:int  ,_UpperCamelCase		:List[str]=5_0_2_4_4  ,_UpperCamelCase		:Optional[Any]=7_6_8  ,_UpperCamelCase		:Dict=6_4  ,_UpperCamelCase		:Dict=2_0_4_8  ,_UpperCamelCase		:Optional[int]=1_2  ,_UpperCamelCase		:Union[str, Any]=1_2  ,_UpperCamelCase		:List[str]=3_2  ,_UpperCamelCase		:Union[str, Any]=1_2_8  ,_UpperCamelCase		:Tuple=0.1  ,_UpperCamelCase		:List[str]=1E-6  ,_UpperCamelCase		:List[Any]=1.0  ,_UpperCamelCase		:Optional[int]="gelu_new"  ,_UpperCamelCase		:Dict=0  ,_UpperCamelCase		:List[str]=False  ,_UpperCamelCase		:Optional[int]=0  ,_UpperCamelCase		:Dict=1  ,_UpperCamelCase		:List[Any]=False  ,_UpperCamelCase		:Tuple=True  ,**_UpperCamelCase		:List[Any]  ,):
									snake_case_		:							List[str]			       =						vocab_size
									snake_case_		:							Any			       =						hidden_size
									snake_case_		:							Any			       =						d_kv
									snake_case_		:							List[Any]			       =						d_ff
									snake_case_		:							Union[str, Any]			       =						num_layers
									snake_case_		:							Union[str, Any]			       =						num_heads
									snake_case_		:							str			       =						relative_attention_num_buckets
									snake_case_		:							Optional[int]			       =						relative_attention_max_distance
									snake_case_		:							Tuple			       =						dropout_rate
									snake_case_		:							Tuple			       =						layer_norm_epsilon
									snake_case_		:							Any			       =						initializer_factor
									snake_case_		:							List[Any]			       =						use_cache

									snake_case_		:							Optional[int]			       =						eos_token_id
									snake_case_		:							List[Any]			       =						decoder_start_token_id

									# for backwards compatibility
									snake_case_		:							List[str]			       =						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 		a__   (  cls		:Optional[int]  ,_UpperCamelCase		:Union[str, os.PathLike]  ,**_UpperCamelCase		:Optional[int]			):
									cls._set_token_in_kwargs(_UpperCamelCase			)

									snake_case_       ,      snake_case_		:							Optional[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":
											snake_case_		:							Optional[int]			       =						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	(						lowercase__							):
							lowercase			:		Dict   		=  'pix2struct_vision_model'





							def __init__(  self		:List[str]  ,_UpperCamelCase		:Any=7_6_8  ,_UpperCamelCase		:List[str]=7_6_8  ,_UpperCamelCase		:List[Any]=2_0_4_8  ,_UpperCamelCase		:Union[str, Any]=6_4  ,_UpperCamelCase		:int=1_2  ,_UpperCamelCase		:int=1_2  ,_UpperCamelCase		:Any="gelu_new"  ,_UpperCamelCase		:Optional[int]=1E-6  ,_UpperCamelCase		:List[Any]=0.0  ,_UpperCamelCase		:Union[str, Any]=0.0  ,_UpperCamelCase		:int=1E-1_0  ,_UpperCamelCase		:str=1.0  ,_UpperCamelCase		:Optional[int]=4_0_9_6  ,_UpperCamelCase		:str=3_2  ,_UpperCamelCase		:Union[str, Any]=1_2_8  ,**_UpperCamelCase		:Union[str, Any]  ,):
									super().__init__(**_UpperCamelCase			)

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





							@classmethod
							def 		a__   (  cls		:Optional[Any]  ,_UpperCamelCase		:Union[str, os.PathLike]  ,**_UpperCamelCase		:List[str]			):
									cls._set_token_in_kwargs(_UpperCamelCase			)

									snake_case_       ,      snake_case_		:							Tuple			       =						cls.get_config_dict(_UpperCamelCase  ,**_UpperCamelCase			)

									# get the vision config dict if we are loading from Pix2StructConfig
									if config_dict.get("""model_type"""			) == "pix2struct":
											snake_case_		:							Dict			       =						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	(						lowercase__							):
							lowercase			:		Union[str, Any]   		=  'pix2struct'
							lowercase			:		int   		=  True





							def __init__(  self		:int  ,_UpperCamelCase		:Tuple=None  ,_UpperCamelCase		:Optional[int]=None  ,_UpperCamelCase		:Dict=1.0  ,_UpperCamelCase		:Optional[int]=0.02  ,_UpperCamelCase		:Tuple=False  ,_UpperCamelCase		:List[str]=False  ,_UpperCamelCase		:Dict=True  ,**_UpperCamelCase		:Union[str, Any]  ,):
									super().__init__(tie_word_embeddings=_UpperCamelCase  ,is_encoder_decoder=_UpperCamelCase  ,**_UpperCamelCase			)

									if text_config is None:
											snake_case_		:							Optional[int]			       =						{}
											logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values."""			)

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

									snake_case_		:							Optional[int]			       =						PixaStructTextConfig(**_UpperCamelCase			)
									snake_case_		:							List[str]			       =						PixaStructVisionConfig(**_UpperCamelCase			)

									snake_case_		:							Any			       =						self.text_config.decoder_start_token_id
									snake_case_		:							List[Any]			       =						self.text_config.pad_token_id
									snake_case_		:							Optional[int]			       =						self.text_config.eos_token_id

									snake_case_		:							Optional[int]			       =						initializer_factor
									snake_case_		:							List[str]			       =						initializer_range

									snake_case_		:							Union[str, Any]			       =						self.initializer_range
									snake_case_		:							str			       =						self.initializer_range

									snake_case_		:							Dict			       =						is_vqa





							@classmethod
							def 		a__   (  cls		:Any  ,_UpperCamelCase		:PixaStructTextConfig  ,_UpperCamelCase		:PixaStructVisionConfig  ,**_UpperCamelCase		:Any			):
									return cls(text_config=text_config.to_dict()  ,vision_config=vision_config.to_dict()  ,**_UpperCamelCase			)





							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							int			       =						copy.deepcopy(self.__dict__			)
									snake_case_		:							str			       =						self.text_config.to_dict()
									snake_case_		:							Tuple			       =						self.vision_config.to_dict()
									snake_case_		:							Any			       =						self.__class__.model_type
									return output