
'''simple docstring'''

import copy
import os
from typing import Union

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


snake_case_			=     logging.get_logger(__name__)

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


class 	a__     (				UpperCAmelCase__	):
	__magic_name__		:    Optional[int]	    =     "pix2struct_text_model"
	__magic_name__		:    Tuple	    =     ["past_key_values"]
	__magic_name__		:    Optional[int]	    =     {
	    "hidden_size": "hidden_size",
	    "num_attention_heads": "num_heads",
	    "num_hidden_layers": "num_layers",
	}


	def __init__(self : Optional[int],					__UpperCAmelCase : int=50244,					__UpperCAmelCase : str=768,					__UpperCAmelCase : List[Any]=64,					__UpperCAmelCase : Union[str, Any]=2048,					__UpperCAmelCase : Any=12,					__UpperCAmelCase : Any=12,					__UpperCAmelCase : Optional[int]=32,					__UpperCAmelCase : str=128,					__UpperCAmelCase : int=0.1,					__UpperCAmelCase : int=1e-6,					__UpperCAmelCase : str=1.0,					__UpperCAmelCase : Optional[int]="gelu_new",					__UpperCAmelCase : List[str]=0,					__UpperCAmelCase : Union[str, Any]=False,					__UpperCAmelCase : Optional[int]=0,					__UpperCAmelCase : List[Any]=1,					__UpperCAmelCase : Dict=False,					__UpperCAmelCase : int=True,					**__UpperCAmelCase : List[str],					)     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[Any]      					= vocab_size
					SCREAMING_SNAKE_CASE  :						str      					= hidden_size
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= d_kv
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= d_ff
					SCREAMING_SNAKE_CASE  :						str      					= num_layers
					SCREAMING_SNAKE_CASE  :						Tuple      					= num_heads
					SCREAMING_SNAKE_CASE  :						Dict      					= relative_attention_num_buckets
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= relative_attention_max_distance
					SCREAMING_SNAKE_CASE  :						Dict      					= dropout_rate
					SCREAMING_SNAKE_CASE  :						Dict      					= layer_norm_epsilon
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= initializer_factor
					SCREAMING_SNAKE_CASE  :						Tuple      					= use_cache

					SCREAMING_SNAKE_CASE  :						List[str]      					= eos_token_id
					SCREAMING_SNAKE_CASE  :						Any      					= decoder_start_token_id

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

					"""simple docstring"""

					cls._set_token_in_kwargs(__lowerCAmelCase      )

					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						List[Any]      					= cls.get_config_dict(__lowerCAmelCase,					**__lowerCAmelCase      )

					# get the text config dict if we are loading from Pix2StructConfig
					if config_dict.get('''model_type'''      ) == "pix2struct":
									SCREAMING_SNAKE_CASE  :						List[str]      					= 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 	a__     (				UpperCAmelCase__	):
	__magic_name__		:    Dict	    =     "pix2struct_vision_model"


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

					"""simple docstring"""

					super().__init__(**__lowerCAmelCase      )

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






	@classmethod
	def 				lowercase__  (cls : List[str],					__UpperCAmelCase : Union[str, os.PathLike],					**__UpperCAmelCase : str      )     ->			"PretrainedConfig":

					"""simple docstring"""

					cls._set_token_in_kwargs(__lowerCAmelCase      )

					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						Dict      					= cls.get_config_dict(__lowerCAmelCase,					**__lowerCAmelCase      )

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



class 	a__     (				UpperCAmelCase__	):
	__magic_name__		:    Dict	    =     "pix2struct"
	__magic_name__		:    str	    =     True


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

					"""simple docstring"""

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

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

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

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= PixaStructTextConfig(**__lowerCAmelCase      )
					SCREAMING_SNAKE_CASE  :						List[Any]      					= PixaStructVisionConfig(**__lowerCAmelCase      )

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.text_config.decoder_start_token_id
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.text_config.pad_token_id
					SCREAMING_SNAKE_CASE  :						Any      					= self.text_config.eos_token_id

					SCREAMING_SNAKE_CASE  :						List[Any]      					= initializer_factor
					SCREAMING_SNAKE_CASE  :						int      					= initializer_range

					SCREAMING_SNAKE_CASE  :						List[str]      					= self.initializer_range
					SCREAMING_SNAKE_CASE  :						str      					= self.initializer_range

					SCREAMING_SNAKE_CASE  :						str      					= is_vqa


	@classmethod
	def 				lowercase__  (cls : Optional[Any],					__UpperCAmelCase : PixaStructTextConfig,					__UpperCAmelCase : PixaStructVisionConfig,					**__UpperCAmelCase : List[Any]      )     ->			Optional[Any]:

					"""simple docstring"""

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






	def 				lowercase__  (self : Optional[int]      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= copy.deepcopy(self.__dict__      )
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= self.text_config.to_dict()
					SCREAMING_SNAKE_CASE  :						List[Any]      					= self.vision_config.to_dict()
					SCREAMING_SNAKE_CASE  :						int      					= self.__class__.model_type
					return output









'''simple docstring'''

import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate






def       __lowercase  (_SCREAMING_SNAKE_CASE      :List[Any]       ):
				return x + 2




class 	a__     (				unittest.TestCase	):


	def 				lowercase__  (self : List[str]      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= '''x = 3'''
					SCREAMING_SNAKE_CASE  :						Dict      					= {}
					SCREAMING_SNAKE_CASE  :						Any      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3}      )

					SCREAMING_SNAKE_CASE  :						str      					= '''x = y'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''y''': 5}
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 5, '''y''': 5}      )


	def 				lowercase__  (self : Optional[int]      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						int      					= '''y = add_two(x)'''
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )

					# Won't work without the tool
					with CaptureStdout() as out:
									SCREAMING_SNAKE_CASE  :						Tuple      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result is None
					assert "tried to execute add_two" in out.out


	def 				lowercase__  (self : Any      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''x = 3'''
					SCREAMING_SNAKE_CASE  :						Any      					= {}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3}      )


	def 				lowercase__  (self : Optional[int]      )     ->			Optional[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= '''test_dict = {\'x\': x, \'y\': add_two(x)}'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						List[Any]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}      )


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= '''x = 3\ny = 5'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {}
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 5}      )


	def 				lowercase__  (self : Tuple      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''text = f\'This is x: {x}.\''''
					SCREAMING_SNAKE_CASE  :						List[str]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Dict      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == "This is x: 3."
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''text''': '''This is x: 3.'''}      )


	def 				lowercase__  (self : int      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= '''if x <= 3:\n    y = 2\nelse:\n    y = 5'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						List[str]      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 2
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 2}      )

					SCREAMING_SNAKE_CASE  :						Any      					= {'''x''': 8}
					SCREAMING_SNAKE_CASE  :						int      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					# evaluate returns the value of the last assignment.
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 8, '''y''': 5}      )


	def 				lowercase__  (self : Any      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''test_list = [x, add_two(x)]'''
					SCREAMING_SNAKE_CASE  :						List[str]      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Tuple      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					self.assertListEqual(__UpperCAmelCase,					[3, 5]      )
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_list''': [3, 5]}      )


	def 				lowercase__  (self : int      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Tuple      					= '''y = x'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						str      					= evaluate(__UpperCAmelCase,					{},					state=__UpperCAmelCase      )
					assert result == 3
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''y''': 3}      )


	def 				lowercase__  (self : Optional[Any]      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= '''test_list = [x, add_two(x)]\ntest_list[1]'''
					SCREAMING_SNAKE_CASE  :						int      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_list''': [3, 5]}      )

					SCREAMING_SNAKE_CASE  :						Dict      					= '''test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']'''
					SCREAMING_SNAKE_CASE  :						Tuple      					= {'''x''': 3}
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= evaluate(__UpperCAmelCase,					{'''add_two''': add_two},					state=__UpperCAmelCase      )
					assert result == 5
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}      )






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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= '''x = 0\nfor i in range(3):\n    x = i'''
					SCREAMING_SNAKE_CASE  :						List[Any]      					= {}
					SCREAMING_SNAKE_CASE  :						List[str]      					= evaluate(__UpperCAmelCase,					{'''range''': range},					state=__UpperCAmelCase      )
					assert result == 2
					self.assertDictEqual(__UpperCAmelCase,					{'''x''': 2, '''i''': 2}      )




