



"""simple docstring"""







from collections import OrderedDict
from typing import Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...feature_extraction_utils import FeatureExtractionMixin
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import TensorType, logging


lowerCAmelCase_   						=   logging.get_logger(__name__)

lowerCAmelCase_   						=   {
    'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json',
    # See all Perceiver models at https://huggingface.co/models?filter=perceiver
}
class        __A       (	A_    ):







		'''simple docstring'''





		lowerCAmelCase			:					Union[str, Any]   	=				"perceiver"







		def __init__(	self						:  Optional[int]			,_snake_case						:  Optional[Any]=256			,_snake_case						:  List[str]=1_280			,_snake_case						:  List[Any]=768			,_snake_case						:  Dict=1			,_snake_case						:  Optional[Any]=26			,_snake_case						:  Any=8			,_snake_case						:  List[str]=8			,_snake_case						:  int=None			,_snake_case						:  Tuple=None			,_snake_case						:  Any="kv"			,_snake_case						:  List[Any]=1			,_snake_case						:  int=1			,_snake_case						:  List[str]="gelu"			,_snake_case						:  Any=0.1			,_snake_case						:  Dict=0.02			,_snake_case						:  Optional[int]=1e-12			,_snake_case						:  Dict=True			,_snake_case						:  Optional[int]=262			,_snake_case						:  List[str]=2_048			,_snake_case						:  Optional[int]=56			,_snake_case						:  str=[368, 496]			,_snake_case						:  List[Any]=16			,_snake_case						:  Union[str, Any]=1_920			,_snake_case						:  Optional[int]=16			,_snake_case						:  str=[1, 16, 224, 224]			,**_snake_case						:  List[Any]			,)					->		Optional[Any]:






				"""simple docstring"""



				super().__init__(**_SCREAMING_SNAKE_CASE						)

				lowercase__   :    str				=			num_latents
				lowercase__   :    int				=			d_latents
				lowercase__   :    Any				=			d_model
				lowercase__   :    List[str]				=			num_blocks
				lowercase__   :    Tuple				=			num_self_attends_per_block
				lowercase__   :    List[Any]				=			num_self_attention_heads
				lowercase__   :    List[Any]				=			num_cross_attention_heads
				lowercase__   :    List[str]				=			qk_channels
				lowercase__   :    Optional[Any]				=			v_channels
				lowercase__   :    int				=			cross_attention_shape_for_attention
				lowercase__   :    List[Any]				=			self_attention_widening_factor
				lowercase__   :    Any				=			cross_attention_widening_factor
				lowercase__   :    Optional[int]				=			hidden_act
				lowercase__   :    Tuple				=			attention_probs_dropout_prob
				lowercase__   :    List[str]				=			initializer_range
				lowercase__   :    List[Any]				=			layer_norm_eps
				lowercase__   :    Optional[int]				=			use_query_residual
				# masked language modeling attributes
				lowercase__   :    List[str]				=			vocab_size
				lowercase__   :    List[str]				=			max_position_embeddings
				# image classification attributes
				lowercase__   :    Any				=			image_size
				# flow attributes
				lowercase__   :    List[Any]				=			train_size
				# multimodal autoencoding attributes
				lowercase__   :    Optional[int]				=			num_frames
				lowercase__   :    Any				=			audio_samples_per_frame
				lowercase__   :    Dict				=			samples_per_patch
				lowercase__   :    str				=			output_shape





class        __A       (	A_    ):







		'''simple docstring'''





		@property
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Mapping[str, Mapping[int, str]]:






				"""simple docstring"""



				if self.task == "multiple-choice":
						lowercase__   :    Union[str, Any]				=			{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
				else:
						lowercase__   :    Any				=			{0: '''batch''', 1: '''sequence'''}
				return OrderedDict(
				    [
				        ('''inputs''', dynamic_axis),
				        ('''attention_mask''', dynamic_axis),
				    ]						)







		@property
		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		float:






				"""simple docstring"""



				return 1e-4







		def 					UpperCAmelCase    (	self						:  Dict			,_snake_case						:  int			,_snake_case						:  Optional[int] = -1			,_snake_case						:  List[str] = -1			,_snake_case						:  List[str] = -1			,_snake_case						:  Optional[int] = False			,_snake_case						:  Union[str, Any] = None			,_snake_case						:  Optional[Any] = 3			,_snake_case						:  List[Any] = 40			,_snake_case						:  List[str] = 40			,)					->		Mapping[str, Any]:






				"""simple docstring"""



				if isinstance(_SCREAMING_SNAKE_CASE			,_SCREAMING_SNAKE_CASE						):
						# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
						lowercase__   :    Optional[int]				=			compute_effective_axis_dimension(
						    _SCREAMING_SNAKE_CASE			,fixed_dimension=OnnxConfig.default_fixed_batch			,num_token_to_add=0						)
						# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
						lowercase__   :    str				=			preprocessor.num_special_tokens_to_add(_SCREAMING_SNAKE_CASE						)
						lowercase__   :    str				=			compute_effective_axis_dimension(
						    _SCREAMING_SNAKE_CASE			,fixed_dimension=OnnxConfig.default_fixed_sequence			,num_token_to_add=_SCREAMING_SNAKE_CASE						)
						# Generate dummy inputs according to compute batch and sequence
						lowercase__   :    int				=			[''' '''.join(['''a''']						) * seq_length] * batch_size
						lowercase__   :    Dict				=			dict(preprocessor(_SCREAMING_SNAKE_CASE			,return_tensors=_SCREAMING_SNAKE_CASE						)						)
						lowercase__   :    int				=			inputs.pop('''input_ids'''						)
						return inputs
				elif isinstance(_SCREAMING_SNAKE_CASE			,_SCREAMING_SNAKE_CASE						) and preprocessor.model_input_names[0] == "pixel_values":
						# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
						lowercase__   :    Optional[int]				=			compute_effective_axis_dimension(_SCREAMING_SNAKE_CASE			,fixed_dimension=OnnxConfig.default_fixed_batch						)
						lowercase__   :    Union[str, Any]				=			self._generate_dummy_images(_SCREAMING_SNAKE_CASE			,_SCREAMING_SNAKE_CASE			,_SCREAMING_SNAKE_CASE			,_SCREAMING_SNAKE_CASE						)
						lowercase__   :    Optional[int]				=			dict(preprocessor(images=_SCREAMING_SNAKE_CASE			,return_tensors=_SCREAMING_SNAKE_CASE						)						)
						lowercase__   :    List[Any]				=			inputs.pop('''pixel_values'''						)
						return inputs
				else:
						raise ValueError(
						    '''Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.'''						)




import copy
import os
from typing import Union

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


UpperCamelCase								=	logging.get_logger(__name__)

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






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "pix2struct_text_model"
       snake_case      =    ["past_key_values"]
       snake_case      =    {
           "hidden_size": "hidden_size",
           "num_attention_heads": "num_heads",
           "num_hidden_layers": "num_layers",
       }



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=5_0244				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=64				,       _SCREAMING_SNAKE_CASE=2048				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=32				,       _SCREAMING_SNAKE_CASE=128				,       _SCREAMING_SNAKE_CASE=0.1				,       _SCREAMING_SNAKE_CASE=1e-6				,       _SCREAMING_SNAKE_CASE=1.0				,       _SCREAMING_SNAKE_CASE="gelu_new"				,       _SCREAMING_SNAKE_CASE=0				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=0				,       _SCREAMING_SNAKE_CASE=1				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=True				,       **_SCREAMING_SNAKE_CASE				,       )->int:




         '''simple docstring'''





         A_      :				Optional[int]		     =					vocab_size
         A_      :				Any		     =					hidden_size
         A_      :				Optional[Any]		     =					d_kv
         A_      :				int		     =					d_ff
         A_      :				int		     =					num_layers
         A_      :				Dict		     =					num_heads
         A_      :				Any		     =					relative_attention_num_buckets
         A_      :				int		     =					relative_attention_max_distance
         A_      :				Optional[Any]		     =					dropout_rate
         A_      :				Optional[Any]		     =					layer_norm_epsilon
         A_      :				List[Any]		     =					initializer_factor
         A_      :				Optional[int]		     =					use_cache

         A_      :				Optional[Any]		     =					eos_token_id
         A_      :				List[Any]		     =					decoder_start_token_id

         # for backwards compatibility
         A_      :				int		     =					dense_act_fn

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







       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->"PretrainedConfig":




         '''simple docstring'''





         cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE		)

         A_     ,       A_      :				Union[str, Any]		     =					cls.get_config_dict(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

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






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "pix2struct_vision_model"



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




         '''simple docstring'''





         super().__init__(**_SCREAMING_SNAKE_CASE		)

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







       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->"PretrainedConfig":




         '''simple docstring'''





         cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE		)

         A_     ,       A_      :				str		     =					cls.get_config_dict(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

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






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "pix2struct"
       snake_case      =    True



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=None				,       _SCREAMING_SNAKE_CASE=None				,       _SCREAMING_SNAKE_CASE=1.0				,       _SCREAMING_SNAKE_CASE=0.0_2				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=False				,       _SCREAMING_SNAKE_CASE=True				,       **_SCREAMING_SNAKE_CASE				,       )->Any:




         '''simple docstring'''





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

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

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

         A_      :				int		     =					PixaStructTextConfig(**_SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					PixaStructVisionConfig(**_SCREAMING_SNAKE_CASE		)

         A_      :				Optional[Any]		     =					self.text_config.decoder_start_token_id
         A_      :				Tuple		     =					self.text_config.pad_token_id
         A_      :				Union[str, Any]		     =					self.text_config.eos_token_id

         A_      :				str		     =					initializer_factor
         A_      :				Tuple		     =					initializer_range

         A_      :				List[str]		     =					self.initializer_range
         A_      :				int		     =					self.initializer_range

         A_      :				List[Any]		     =					is_vqa



       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->Union[str, Any]:




         '''simple docstring'''





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







       def        _snake_case       (      self		)->Dict:




         '''simple docstring'''





         A_      :				List[Any]		     =					copy.deepcopy(self.__dict__		)
         A_      :				Dict		     =					self.text_config.to_dict()
         A_      :				int		     =					self.vision_config.to_dict()
         A_      :				List[str]		     =					self.__class__.model_type
         return output




