


import copy
import os
from typing import Union

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


_UpperCAmelCase			:		Tuple   				=    logging.get_logger(__name__)

_UpperCAmelCase			:		str   				=    {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}
class  lowerCAmelCase_       (				snake_case__					):
							UpperCamelCase_		:List[Any]		      =    'pix2struct_text_model'
							UpperCamelCase_		:Dict		      =    ['past_key_values']
							UpperCamelCase_		:Dict		      =    {
							    'hidden_size': 'hidden_size',
							    'num_attention_heads': 'num_heads',
							    'num_hidden_layers': 'num_layers',
							}




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

											lowerCAmelCase__      			=						eos_token_id
											lowerCAmelCase__      			=						decoder_start_token_id

											# for backwards compatibility
											lowerCAmelCase__      			=						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     :   int ,							SCREAMING_SNAKE_CASE_     :   Union[str, os.PathLike] ,							**SCREAMING_SNAKE_CASE_     :   Any	):
											cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__			,		lowerCAmelCase__      			=						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":
															lowerCAmelCase__      			=						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_       (				snake_case__					):
							UpperCamelCase_		:Tuple		      =    'pix2struct_vision_model'




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

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




							@classmethod
							def 						__snake_case					(   cls     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Union[str, os.PathLike] ,							**SCREAMING_SNAKE_CASE_     :   Dict	):
											cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__			,		lowerCAmelCase__      			=						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":
															lowerCAmelCase__      			=						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_       (				snake_case__					):
							UpperCamelCase_		:int		      =    'pix2struct'
							UpperCamelCase_		:str		      =    True




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

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

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

											lowerCAmelCase__      			=						PixaStructTextConfig(**SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						PixaStructVisionConfig(**SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						self.text_config.decoder_start_token_id
											lowerCAmelCase__      			=						self.text_config.pad_token_id
											lowerCAmelCase__      			=						self.text_config.eos_token_id

											lowerCAmelCase__      			=						initializer_factor
											lowerCAmelCase__      			=						initializer_range

											lowerCAmelCase__      			=						self.initializer_range
											lowerCAmelCase__      			=						self.initializer_range

											lowerCAmelCase__      			=						is_vqa




							@classmethod
							def 						__snake_case					(   cls     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   PixaStructTextConfig ,							SCREAMING_SNAKE_CASE_     :   PixaStructVisionConfig ,							**SCREAMING_SNAKE_CASE_     :   Dict	):
											return cls(text_config=text_config.to_dict() ,							vision_config=vision_config.to_dict() ,							**SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						copy.deepcopy(self.__dict__	)
											lowerCAmelCase__      			=						self.text_config.to_dict()
											lowerCAmelCase__      			=						self.vision_config.to_dict()
											lowerCAmelCase__      			=						self.__class__.model_type
											return output













import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


_UpperCAmelCase			:		List[Any]   				=    logging.getLogger()
@unittest.skip('Temporarily disable the doc tests.'					)
@require_torch
@require_tf
@slow
class  lowerCAmelCase_       (				unittest.TestCase					):




							def 						__snake_case					(   self     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Path ,							SCREAMING_SNAKE_CASE_     :   Union[str, None] = None ,							SCREAMING_SNAKE_CASE_     :   Union[List[str], None] = None ,							SCREAMING_SNAKE_CASE_     :   Union[str, List[str], None] = None ,							SCREAMING_SNAKE_CASE_     :   bool = True ,							):
											lowerCAmelCase__      			=						[file for file in os.listdir(SCREAMING_SNAKE_CASE_	) if os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)	)]

											if identifier is not None:
															lowerCAmelCase__      			=						[file for file in files if identifier in file]

											if n_identifier is not None:
															if isinstance(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	):
																			for n_ in n_identifier:
																							lowerCAmelCase__      			=						[file for file in files if n_ not in file]
															else:
																			lowerCAmelCase__      			=						[file for file in files if n_identifier not in file]

											lowerCAmelCase__      			=						ignore_files or []
											ignore_files.append('''__init__.py'''	)
											lowerCAmelCase__      			=						[file for file in files if file not in ignore_files]

											for file in files:
															# Open all files
															print('''Testing''' ,							SCREAMING_SNAKE_CASE_	)

															if only_modules:
																			lowerCAmelCase__      			=						file.split('''.'''	)[0]
																			try:
																							lowerCAmelCase__      			=						getattr(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
																							lowerCAmelCase__      			=						doctest.DocTestSuite(SCREAMING_SNAKE_CASE_	)
																							lowerCAmelCase__      			=						unittest.TextTestRunner().run(SCREAMING_SNAKE_CASE_	)
																							self.assertIs(len(result.failures	) ,							0	)
																			except AttributeError:
																							logger.info(f'{module_identifier} is not a module.'	)
															else:
																			lowerCAmelCase__      			=						doctest.testfile(str('''..''' / directory / file	) ,							optionflags=doctest.ELLIPSIS	)
																			self.assertIs(result.failed ,							0	)




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__      			=						Path('''src/transformers'''	)
											lowerCAmelCase__      			=						'''modeling'''
											lowerCAmelCase__      			=						[
											    '''modeling_ctrl.py''',
											    '''modeling_tf_ctrl.py''',
											]
											self.analyze_directory(SCREAMING_SNAKE_CASE_ ,							identifier=SCREAMING_SNAKE_CASE_ ,							ignore_files=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						Path('''src/transformers'''	)
											lowerCAmelCase__      			=						'''tokenization'''
											self.analyze_directory(SCREAMING_SNAKE_CASE_ ,							identifier=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						Path('''src/transformers'''	)
											lowerCAmelCase__      			=						'''configuration'''
											self.analyze_directory(SCREAMING_SNAKE_CASE_ ,							identifier=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__      			=						Path('''src/transformers'''	)
											lowerCAmelCase__      			=						['''configuration''', '''modeling''', '''tokenization''']
											self.analyze_directory(SCREAMING_SNAKE_CASE_ ,							n_identifier=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						Path('''docs/source'''	)
											lowerCAmelCase__      			=						['''favicon.ico''']
											self.analyze_directory(SCREAMING_SNAKE_CASE_ ,							ignore_files=SCREAMING_SNAKE_CASE_ ,							only_modules=SCREAMING_SNAKE_CASE_	)





