


import copy
import os
from typing import Union

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


A      :    Any		=					logging.get_logger(__name__)

A      :    Optional[Any]		=					{
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}







class 			_lowercase (							lowercase__):


       """simple docstring"""




       A__											=			"pix2struct_text_model"
       A__											=			["past_key_values"]
       A__											=			{
           "hidden_size": "hidden_size",
           "num_attention_heads": "num_heads",
           "num_hidden_layers": "num_layers",
       }





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


             '''simple docstring'''



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

             lowerCamelCase__		:				Tuple    =  eos_token_id
             lowerCamelCase__		:				Optional[Any]    =  decoder_start_token_id

             # for backwards compatibility
             lowerCamelCase__		:				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 						lowerCAmelCase     (   cls							:			Dict					,      __lowerCamelCase							:			Union[str, os.PathLike]					,      **__lowerCamelCase							:			Dict       ):


             '''simple docstring'''



             cls._set_token_in_kwargs(__lowerCamelCase       )

             lowerCamelCase__    ,   lowerCamelCase__		:				List[str]    =  cls.get_config_dict(__lowerCamelCase					,      **__lowerCamelCase       )

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







class 			_lowercase (							lowercase__):


       """simple docstring"""




       A__											=			"pix2struct_vision_model"





       def __init__(   self							:			List[str]					,      __lowerCamelCase							:			Union[str, Any]=768					,      __lowerCamelCase							:			List[Any]=768					,      __lowerCamelCase							:			Union[str, Any]=2048					,      __lowerCamelCase							:			Union[str, Any]=64					,      __lowerCamelCase							:			str=12					,      __lowerCamelCase							:			Optional[Any]=12					,      __lowerCamelCase							:			Optional[int]="gelu_new"					,      __lowerCamelCase							:			int=1E-6					,      __lowerCamelCase							:			str=0.0					,      __lowerCamelCase							:			str=0.0					,      __lowerCamelCase							:			Optional[Any]=1E-1_0					,      __lowerCamelCase							:			List[str]=1.0					,      __lowerCamelCase							:			Optional[int]=4096					,      __lowerCamelCase							:			int=32					,      __lowerCamelCase							:			Union[str, Any]=128					,      **__lowerCamelCase							:			Optional[int]					,      ):


             '''simple docstring'''



             super().__init__(**__lowerCamelCase       )

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





       @classmethod
       def 						lowerCAmelCase     (   cls							:			Optional[Any]					,      __lowerCamelCase							:			Union[str, os.PathLike]					,      **__lowerCamelCase							:			Optional[int]       ):


             '''simple docstring'''



             cls._set_token_in_kwargs(__lowerCamelCase       )

             lowerCamelCase__    ,   lowerCamelCase__		:				List[str]    =  cls.get_config_dict(__lowerCamelCase					,      **__lowerCamelCase       )

             # get the vision config dict if we are loading from Pix2StructConfig
             if config_dict.get("model_type"       ) == "pix2struct":
                   lowerCamelCase__		:				Optional[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 			_lowercase (							lowercase__):


       """simple docstring"""




       A__											=			"pix2struct"
       A__											=			True





       def __init__(   self							:			Union[str, Any]					,      __lowerCamelCase							:			List[Any]=None					,      __lowerCamelCase							:			Optional[int]=None					,      __lowerCamelCase							:			str=1.0					,      __lowerCamelCase							:			Optional[Any]=0.0_2					,      __lowerCamelCase							:			Dict=False					,      __lowerCamelCase							:			List[Any]=False					,      __lowerCamelCase							:			Dict=True					,      **__lowerCamelCase							:			List[str]					,      ):


             '''simple docstring'''



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

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

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

             lowerCamelCase__		:				List[str]    =  PixaStructTextConfig(**__lowerCamelCase       )
             lowerCamelCase__		:				int    =  PixaStructVisionConfig(**__lowerCamelCase       )

             lowerCamelCase__		:				Optional[int]    =  self.text_config.decoder_start_token_id
             lowerCamelCase__		:				Dict    =  self.text_config.pad_token_id
             lowerCamelCase__		:				Tuple    =  self.text_config.eos_token_id

             lowerCamelCase__		:				Optional[int]    =  initializer_factor
             lowerCamelCase__		:				int    =  initializer_range

             lowerCamelCase__		:				int    =  self.initializer_range
             lowerCamelCase__		:				Union[str, Any]    =  self.initializer_range

             lowerCamelCase__		:				Any    =  is_vqa





       @classmethod
       def 						lowerCAmelCase     (   cls							:			Dict					,      __lowerCamelCase							:			PixaStructTextConfig					,      __lowerCamelCase							:			PixaStructVisionConfig					,      **__lowerCamelCase							:			int       ):


             '''simple docstring'''



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





       def 						lowerCAmelCase     (   self							:			Dict       ):


             '''simple docstring'''



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






def       lowercase_		(		_A			:    int    ,					_A			:    int						):

      """simple docstring"""




      while a != 0:
            lowerCamelCase__    ,   lowerCamelCase__		:				Optional[Any]    =  b % a, a
      return b


def       lowercase_		(		_A			:    int    ,					_A			:    int						):

      """simple docstring"""




      if gcd(_A    ,					_A						) != 1:
            lowerCamelCase__		:				List[str]    =  F"mod inverse of {a!r} and {m!r} does not exist"
            raise ValueError(_A						)
      lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				Tuple    =  1, 0, a
      lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				Optional[int]    =  0, 1, m
      while va != 0:
            lowerCamelCase__		:				Tuple    =  ua // va
            lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				str    =  (ua - q * va), (ua - q * va), (ua - q * va), va, va, va
      return ua % m


