

"""simple docstring"""





import copy
import os
from typing import Union

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


_a							=							logging.get_logger(__name__)

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




class  _lowerCAmelCase  (     lowercase       ):







   """simple docstring"""







   __UpperCAmelCase     : Optional[Any]       		=     "pix2struct_text_model"
   __UpperCAmelCase     : Any       		=     ["past_key_values"]
   __UpperCAmelCase     : List[Any]       		=     {
       "hidden_size": "hidden_size",
       "num_attention_heads": "num_heads",
       "num_hidden_layers": "num_layers",
   }


   def __init__(      self							:      List[str], UpperCAmelCase__							:      Union[str, Any]=5_0_2_4_4, UpperCAmelCase__							:      str=7_6_8, UpperCAmelCase__							:      Union[str, Any]=6_4, UpperCAmelCase__							:      Dict=2_0_4_8, UpperCAmelCase__							:      Optional[int]=1_2, UpperCAmelCase__							:      Optional[Any]=1_2, UpperCAmelCase__							:      Dict=3_2, UpperCAmelCase__							:      List[str]=1_2_8, UpperCAmelCase__							:      Optional[Any]=0.1, UpperCAmelCase__							:      Any=1E-6, UpperCAmelCase__							:      List[Any]=1.0, UpperCAmelCase__							:      int="gelu_new", UpperCAmelCase__							:      Optional[Any]=0, UpperCAmelCase__							:      Dict=False, UpperCAmelCase__							:      Tuple=0, UpperCAmelCase__							:      Tuple=1, UpperCAmelCase__							:      Tuple=False, UpperCAmelCase__							:      Optional[Any]=True, **UpperCAmelCase__							:      Optional[int], ):
      __lowercase								=						vocab_size
      __lowercase								=						hidden_size
      __lowercase								=						d_kv
      __lowercase								=						d_ff
      __lowercase								=						num_layers
      __lowercase								=						num_heads
      __lowercase								=						relative_attention_num_buckets
      __lowercase								=						relative_attention_max_distance
      __lowercase								=						dropout_rate
      __lowercase								=						layer_norm_epsilon
      __lowercase								=						initializer_factor
      __lowercase								=						use_cache

      __lowercase								=						eos_token_id
      __lowercase								=						decoder_start_token_id

      # for backwards compatibility
      __lowercase								=						dense_act_fn

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



   @classmethod
   def  _lowercase			(      cls							:      Tuple, UpperCAmelCase__							:      Union[str, os.PathLike], **UpperCAmelCase__							:      Optional[int]     ):
      cls._set_token_in_kwargs(UpperCAmelCase__     )

      __lowercase		,__lowercase								=						cls.get_config_dict(UpperCAmelCase__, **UpperCAmelCase__     )

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







class  _lowerCAmelCase  (     lowercase       ):







   """simple docstring"""







   __UpperCAmelCase     : Optional[int]       		=     "pix2struct_vision_model"


   def __init__(      self							:      Optional[Any], UpperCAmelCase__							:      Optional[int]=7_6_8, UpperCAmelCase__							:      Tuple=7_6_8, UpperCAmelCase__							:      List[str]=2_0_4_8, UpperCAmelCase__							:      Any=6_4, UpperCAmelCase__							:      str=1_2, UpperCAmelCase__							:      Dict=1_2, UpperCAmelCase__							:      Union[str, Any]="gelu_new", UpperCAmelCase__							:      Optional[Any]=1E-6, UpperCAmelCase__							:      Dict=0.0, UpperCAmelCase__							:      Union[str, Any]=0.0, UpperCAmelCase__							:      Optional[Any]=1E-10, UpperCAmelCase__							:      List[str]=1.0, UpperCAmelCase__							:      Tuple=4_0_9_6, UpperCAmelCase__							:      str=3_2, UpperCAmelCase__							:      List[Any]=1_2_8, **UpperCAmelCase__							:      str, ):
      super().__init__(**UpperCAmelCase__     )

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



   @classmethod
   def  _lowercase			(      cls							:      List[Any], UpperCAmelCase__							:      Union[str, os.PathLike], **UpperCAmelCase__							:      int     ):
      cls._set_token_in_kwargs(UpperCAmelCase__     )

      __lowercase		,__lowercase								=						cls.get_config_dict(UpperCAmelCase__, **UpperCAmelCase__     )

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







class  _lowerCAmelCase  (     lowercase       ):







   """simple docstring"""







   __UpperCAmelCase     : int       		=     "pix2struct"
   __UpperCAmelCase     : int       		=     True


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

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

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

      __lowercase								=						PixaStructTextConfig(**UpperCAmelCase__     )
      __lowercase								=						PixaStructVisionConfig(**UpperCAmelCase__     )

      __lowercase								=						self.text_config.decoder_start_token_id
      __lowercase								=						self.text_config.pad_token_id
      __lowercase								=						self.text_config.eos_token_id

      __lowercase								=						initializer_factor
      __lowercase								=						initializer_range

      __lowercase								=						self.initializer_range
      __lowercase								=						self.initializer_range

      __lowercase								=						is_vqa


   @classmethod
   def  _lowercase			(      cls							:      Union[str, Any], UpperCAmelCase__							:      PixaStructTextConfig, UpperCAmelCase__							:      PixaStructVisionConfig, **UpperCAmelCase__							:      List[str]     ):
      return cls(text_config=text_config.to_dict(), vision_config=vision_config.to_dict(), **UpperCAmelCase__     )



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







"""simple docstring"""





from ...utils import is_note_seq_available, is_transformers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


try:
   if not (is_transformers_available() and is_torch_available()):
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
   from .notes_encoder import SpectrogramNotesEncoder
   from .continous_encoder import SpectrogramContEncoder
   from .pipeline_spectrogram_diffusion import (
       SpectrogramContEncoder,
       SpectrogramDiffusionPipeline,
       TaFilmDecoder,
   )

try:
   if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   from ...utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
   from .midi_utils import MidiProcessor




