






"""simple docstring"""
import copy
import os
from typing import Union

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


lowerCamelCase_							      = logging.get_logger(__name__)

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







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''pix2struct_text_model'''
	__magic_name__       =							['''past_key_values''']
	__magic_name__       =							{
	    '''hidden_size''': '''hidden_size''',
	    '''num_attention_heads''': '''num_heads''',
	    '''num_hidden_layers''': '''num_layers''',
	}





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

				UpperCAmelCase_	:					Tuple     	=   eos_token_id
				UpperCAmelCase_	:					List[str]     	=   decoder_start_token_id

				# for backwards compatibility
				UpperCAmelCase_	:					Any     	=   dense_act_fn

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





	@classmethod
	def 	_SCREAMING_SNAKE_CASE ( cls   :					str      ,      lowerCAmelCase_   :					Union[str, os.PathLike]      ,      **lowerCAmelCase_   :					int		)		->     "PretrainedConfig":
				cls._set_token_in_kwargs(lowerCAmelCase_		)

				UpperCAmelCase_	,      UpperCAmelCase_	:					Dict     	=   cls.get_config_dict(lowerCAmelCase_      ,      **lowerCAmelCase_		)

				# get the text config dict if we are loading from Pix2StructConfig
				if config_dict.get("model_type"		) == "pix2struct":
							UpperCAmelCase_	:					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(lowerCAmelCase_      ,      **lowerCAmelCase_		)







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''pix2struct_vision_model'''





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

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





	@classmethod
	def 	_SCREAMING_SNAKE_CASE ( cls   :					str      ,      lowerCAmelCase_   :					Union[str, os.PathLike]      ,      **lowerCAmelCase_   :					Optional[Any]		)		->     "PretrainedConfig":
				cls._set_token_in_kwargs(lowerCAmelCase_		)

				UpperCAmelCase_	,      UpperCAmelCase_	:					Tuple     	=   cls.get_config_dict(lowerCAmelCase_      ,      **lowerCAmelCase_		)

				# get the vision config dict if we are loading from Pix2StructConfig
				if config_dict.get("model_type"		) == "pix2struct":
							UpperCAmelCase_	:					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(lowerCAmelCase_      ,      **lowerCAmelCase_		)







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''pix2struct'''
	__magic_name__       =							True





	def __init__( self   :					str      ,      lowerCAmelCase_   :					Dict=None      ,      lowerCAmelCase_   :					Optional[int]=None      ,      lowerCAmelCase_   :					Any=1.0      ,      lowerCAmelCase_   :					Tuple=0.0_2      ,      lowerCAmelCase_   :					Tuple=False      ,      lowerCAmelCase_   :					List[str]=False      ,      lowerCAmelCase_   :					str=True      ,      **lowerCAmelCase_   :					int      ,      )		->     Any:
				super().__init__(tie_word_embeddings=lowerCAmelCase_      ,      is_encoder_decoder=lowerCAmelCase_      ,      **lowerCAmelCase_		)

				if text_config is None:
							UpperCAmelCase_	:					Union[str, Any]     	=   {}
							logger.info("text_config is None. Initializing the Pix2StructTextConfig with default values."		)

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

				UpperCAmelCase_	:					str     	=   PixaStructTextConfig(**lowerCAmelCase_		)
				UpperCAmelCase_	:					Any     	=   PixaStructVisionConfig(**lowerCAmelCase_		)

				UpperCAmelCase_	:					List[str]     	=   self.text_config.decoder_start_token_id
				UpperCAmelCase_	:					Tuple     	=   self.text_config.pad_token_id
				UpperCAmelCase_	:					List[str]     	=   self.text_config.eos_token_id

				UpperCAmelCase_	:					Optional[Any]     	=   initializer_factor
				UpperCAmelCase_	:					Tuple     	=   initializer_range

				UpperCAmelCase_	:					List[str]     	=   self.initializer_range
				UpperCAmelCase_	:					Tuple     	=   self.initializer_range

				UpperCAmelCase_	:					Union[str, Any]     	=   is_vqa





	@classmethod
	def 	_SCREAMING_SNAKE_CASE ( cls   :					Tuple      ,      lowerCAmelCase_   :					PixaStructTextConfig      ,      lowerCAmelCase_   :					PixaStructVisionConfig      ,      **lowerCAmelCase_   :					Union[str, Any]		)		->     int:
				return cls(text_config=text_config.to_dict()      ,      vision_config=vision_config.to_dict()      ,      **lowerCAmelCase_		)





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











"""simple docstring"""
import os
from typing import Dict, List, Tuple, TypeVar, Union


lowerCamelCase_							      = TypeVar('''T''')

lowerCamelCase_							      = Union[List[T], Tuple[T, ...]]
lowerCamelCase_							      = Union[T, List[T], Dict[str, T]]
lowerCamelCase_							      = Union[str, bytes, os.PathLike]



