





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        __SCREAMING_SNAKE_CASE					(     _a       ):
							snake_case     :      Optional[int]						     =       """pix2struct_text_model"""
							snake_case     :      Optional[Any]						     =       ["""past_key_values"""]
							snake_case     :      List[str]						     =       {
							    """hidden_size""": """hidden_size""",
							    """num_attention_heads""": """num_heads""",
							    """num_hidden_layers""": """num_layers""",
							}






							def __init__(						self  , __lowerCAmelCase=50244  , __lowerCAmelCase=768  , __lowerCAmelCase=64  , __lowerCAmelCase=2048  , __lowerCAmelCase=12  , __lowerCAmelCase=12  , __lowerCAmelCase=32  , __lowerCAmelCase=128  , __lowerCAmelCase=0.1  , __lowerCAmelCase=1E-6  , __lowerCAmelCase=1.0  , __lowerCAmelCase="gelu_new"  , __lowerCAmelCase=0  , __lowerCAmelCase=False  , __lowerCAmelCase=0  , __lowerCAmelCase=1  , __lowerCAmelCase=False  , __lowerCAmelCase=True  , **__lowerCAmelCase  , ):
														UpperCamelCase__				     =			vocab_size
														UpperCamelCase__				     =			hidden_size
														UpperCamelCase__				     =			d_kv
														UpperCamelCase__				     =			d_ff
														UpperCamelCase__				     =			num_layers
														UpperCamelCase__				     =			num_heads
														UpperCamelCase__				     =			relative_attention_num_buckets
														UpperCamelCase__				     =			relative_attention_max_distance
														UpperCamelCase__				     =			dropout_rate
														UpperCamelCase__				     =			layer_norm_epsilon
														UpperCamelCase__				     =			initializer_factor
														UpperCamelCase__				     =			use_cache

														UpperCamelCase__				     =			eos_token_id
														UpperCamelCase__				     =			decoder_start_token_id

														# for backwards compatibility
														UpperCamelCase__				     =			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 				_lowerCamelCase		(						cls  , __lowerCAmelCase  , **__lowerCAmelCase  ):
														cls._set_token_in_kwargs(__lowerCAmelCase  )

														UpperCamelCase__     ,     UpperCamelCase__				     =			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__				     =			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        __SCREAMING_SNAKE_CASE					(     _a       ):
							snake_case     :      Union[str, Any]						     =       """pix2struct_vision_model"""






							def __init__(						self  , __lowerCAmelCase=768  , __lowerCAmelCase=768  , __lowerCAmelCase=2048  , __lowerCAmelCase=64  , __lowerCAmelCase=12  , __lowerCAmelCase=12  , __lowerCAmelCase="gelu_new"  , __lowerCAmelCase=1E-6  , __lowerCAmelCase=0.0  , __lowerCAmelCase=0.0  , __lowerCAmelCase=1E-10  , __lowerCAmelCase=1.0  , __lowerCAmelCase=4096  , __lowerCAmelCase=32  , __lowerCAmelCase=128  , **__lowerCAmelCase  , ):
														super().__init__(**__lowerCAmelCase  )

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







							@classmethod
							def 				_lowerCamelCase		(						cls  , __lowerCAmelCase  , **__lowerCAmelCase  ):
														cls._set_token_in_kwargs(__lowerCAmelCase  )

														UpperCamelCase__     ,     UpperCamelCase__				     =			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__				     =			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        __SCREAMING_SNAKE_CASE					(     _a       ):
							snake_case     :      Optional[Any]						     =       """pix2struct"""
							snake_case     :      Optional[Any]						     =       True






							def __init__(						self  , __lowerCAmelCase=None  , __lowerCAmelCase=None  , __lowerCAmelCase=1.0  , __lowerCAmelCase=0.02  , __lowerCAmelCase=False  , __lowerCAmelCase=False  , __lowerCAmelCase=True  , **__lowerCAmelCase  , ):
														super().__init__(tie_word_embeddings=__lowerCAmelCase  , is_encoder_decoder=__lowerCAmelCase  , **__lowerCAmelCase  )

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

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

														UpperCamelCase__				     =			PixaStructTextConfig(**__lowerCAmelCase  )
														UpperCamelCase__				     =			PixaStructVisionConfig(**__lowerCAmelCase  )

														UpperCamelCase__				     =			self.text_config.decoder_start_token_id
														UpperCamelCase__				     =			self.text_config.pad_token_id
														UpperCamelCase__				     =			self.text_config.eos_token_id

														UpperCamelCase__				     =			initializer_factor
														UpperCamelCase__				     =			initializer_range

														UpperCamelCase__				     =			self.initializer_range
														UpperCamelCase__				     =			self.initializer_range

														UpperCamelCase__				     =			is_vqa






							@classmethod
							def 				_lowerCamelCase		(						cls  , __lowerCAmelCase  , __lowerCAmelCase  , **__lowerCAmelCase  ):
														return cls(text_config=text_config.to_dict()  , vision_config=vision_config.to_dict()  , **__lowerCAmelCase  )







							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__				     =			copy.deepcopy(self.__dict__  )
														UpperCamelCase__				     =			self.text_config.to_dict()
														UpperCamelCase__				     =			self.vision_config.to_dict()
														UpperCamelCase__				     =			self.__class__.model_type
														return output











import inspect
import unittest

class        __SCREAMING_SNAKE_CASE					(     unittest.TestCase       ):






							def 				_lowerCamelCase		(						self  ):
														try:
																					import diffusers  # noqa: F401
														except ImportError:
																					assert False







							def 				_lowerCamelCase		(						self  ):
														import diffusers
														from diffusers.dependency_versions_table import deps

														UpperCamelCase__				     =			inspect.getmembers(__lowerCAmelCase  , inspect.isclass  )

														for cls_name, cls_module in all_classes:
																					if "dummy_" in cls_module.__module__:
																												for backend in cls_module._backends:
																																			if backend == "k_diffusion":
																																										UpperCamelCase__				     =			"""k-diffusion"""
																																			elif backend == "invisible_watermark":
																																										UpperCamelCase__				     =			"""invisible-watermark"""
																																			assert backend in deps, f"""{backend} is not in the deps table!"""




