

"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__A						       =      {
    "configuration_pix2struct": [
        "PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "Pix2StructConfig",
        "Pix2StructTextConfig",
        "Pix2StructVisionConfig",
    ],
    "processing_pix2struct": ["Pix2StructProcessor"],
}

try:
			if not is_vision_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__A						       =      ["Pix2StructImageProcessor"]


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__A						       =      [
			    "PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "Pix2StructPreTrainedModel",
			    "Pix2StructForConditionalGeneration",
			    "Pix2StructVisionModel",
			    "Pix2StructTextModel",
			]

if TYPE_CHECKING:
			from .configuration_pixastruct import (
			    PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP,
			    PixaStructConfig,
			    PixaStructTextConfig,
			    PixaStructVisionConfig,
			)
			from .processing_pixastruct import PixaStructProcessor

			try:
						if not is_vision_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .image_processing_pixastruct import PixaStructImageProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_pixastruct import (
						    PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    PixaStructForConditionalGeneration,
						    PixaStructPreTrainedModel,
						    PixaStructTextModel,
						    PixaStructVisionModel,
						)

else:
			import sys

			__A						       =      _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





"""simple docstring"""


import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


__A						       =      logging.get_logger(__name__)

__A						       =      {
    "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json",
}





class    snake_case     (    __snake_case  ):
				SCREAMING_SNAKE_CASE_       :				Union[str, Any]       				=  """blip_2_vision_model"""

				def __init__(     self							:    Optional[int]					,  UpperCamelCase__							:    Optional[int]=1_4_0_8					,  UpperCamelCase__							:    Tuple=6_1_4_4					,  UpperCamelCase__							:    Tuple=3_9					,  UpperCamelCase__							:    str=1_6					,  UpperCamelCase__							:    Optional[Any]=2_2_4					,  UpperCamelCase__							:    List[str]=1_4					,  UpperCamelCase__							:    Optional[int]="gelu"					,  UpperCamelCase__							:    Optional[int]=0.00001					,  UpperCamelCase__							:    Tuple=0.0					,  UpperCamelCase__							:    Dict=1e-10					,  UpperCamelCase__							:    List[str]=True					,  **UpperCamelCase__							:    List[str]					,  )->       int:



						'''simple docstring'''


						super().__init__(**UpperCamelCase__)

						__lowerCAmelCase:	List[str]   				=      hidden_size
						__lowerCAmelCase:	Tuple   				=      intermediate_size
						__lowerCAmelCase:	str   				=      num_hidden_layers
						__lowerCAmelCase:	Any   				=      num_attention_heads
						__lowerCAmelCase:	Any   				=      patch_size
						__lowerCAmelCase:	Tuple   				=      image_size
						__lowerCAmelCase:	str   				=      initializer_range
						__lowerCAmelCase:	List[str]   				=      attention_dropout
						__lowerCAmelCase:	List[Any]   				=      layer_norm_eps
						__lowerCAmelCase:	Any   				=      hidden_act
						__lowerCAmelCase:	int   				=      qkv_bias




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



						'''simple docstring'''


						cls._set_token_in_kwargs(UpperCamelCase__)

						__lowerCAmelCase				,			__lowerCAmelCase:	List[str]   				=      cls.get_config_dict(UpperCamelCase__					,  **UpperCamelCase__)

						# get the vision config dict if we are loading from Blip2Config
						if config_dict.get("model_type") == "blip-2":
								__lowerCAmelCase:	Tuple   				=      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    snake_case     (    __snake_case  ):
				SCREAMING_SNAKE_CASE_       :				List[str]       				=  """blip_2_qformer"""

				def __init__(     self							:    Optional[int]					,  UpperCamelCase__							:    Dict=3_0_5_2_2					,  UpperCamelCase__							:    Union[str, Any]=7_6_8					,  UpperCamelCase__							:    Union[str, Any]=1_2					,  UpperCamelCase__							:    Union[str, Any]=1_2					,  UpperCamelCase__							:    Optional[int]=3_0_7_2					,  UpperCamelCase__							:    Optional[Any]="gelu"					,  UpperCamelCase__							:    Tuple=0.1					,  UpperCamelCase__							:    Optional[Any]=0.1					,  UpperCamelCase__							:    List[str]=5_1_2					,  UpperCamelCase__							:    List[Any]=0.02					,  UpperCamelCase__							:    Dict=1e-12					,  UpperCamelCase__							:    List[str]=0					,  UpperCamelCase__							:    Optional[int]="absolute"					,  UpperCamelCase__							:    List[Any]=2					,  UpperCamelCase__							:    Optional[Any]=1_4_0_8					,  **UpperCamelCase__							:    List[str]					,  )->       str:



						'''simple docstring'''


						super().__init__(pad_token_id=UpperCamelCase__					,  **UpperCamelCase__)

						__lowerCAmelCase:	Tuple   				=      vocab_size
						__lowerCAmelCase:	str   				=      hidden_size
						__lowerCAmelCase:	Dict   				=      num_hidden_layers
						__lowerCAmelCase:	List[Any]   				=      num_attention_heads
						__lowerCAmelCase:	List[Any]   				=      hidden_act
						__lowerCAmelCase:	Optional[int]   				=      intermediate_size
						__lowerCAmelCase:	str   				=      hidden_dropout_prob
						__lowerCAmelCase:	Optional[int]   				=      attention_probs_dropout_prob
						__lowerCAmelCase:	Optional[int]   				=      max_position_embeddings
						__lowerCAmelCase:	int   				=      initializer_range
						__lowerCAmelCase:	List[str]   				=      layer_norm_eps
						__lowerCAmelCase:	Optional[Any]   				=      position_embedding_type
						__lowerCAmelCase:	Dict   				=      cross_attention_frequency
						__lowerCAmelCase:	List[Any]   				=      encoder_hidden_size




				@classmethod
				def 							lowercase_					(     cls							:    int					,  UpperCamelCase__							:    Union[str, os.PathLike]					,  **UpperCamelCase__							:    Tuple)->       "PretrainedConfig":



						'''simple docstring'''


						cls._set_token_in_kwargs(UpperCamelCase__)

						__lowerCAmelCase				,			__lowerCAmelCase:	Optional[int]   				=      cls.get_config_dict(UpperCamelCase__					,  **UpperCamelCase__)

						# get the qformer config dict if we are loading from Blip2Config
						if config_dict.get("model_type") == "blip-2":
								__lowerCAmelCase:	Any   				=      config_dict["qformer_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    snake_case     (    __snake_case  ):
				SCREAMING_SNAKE_CASE_       :				Dict       				=  """blip-2"""
				SCREAMING_SNAKE_CASE_       :				str       				=  True

				def __init__(     self							:    List[Any]					,  UpperCamelCase__							:    Tuple=None					,  UpperCamelCase__							:    Dict=None					,  UpperCamelCase__							:    Optional[int]=None					,  UpperCamelCase__							:    Union[str, Any]=3_2					,  **UpperCamelCase__							:    Dict)->       List[Any]:



						'''simple docstring'''


						super().__init__(**UpperCamelCase__)

						if vision_config is None:
								__lowerCAmelCase:	Dict   				=      {}
								logger.info("vision_config is None. initializing the Blip2VisionConfig with default values.")

						if qformer_config is None:
								__lowerCAmelCase:	Union[str, Any]   				=      {}
								logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values.")

						if text_config is None:
								__lowerCAmelCase:	Dict   				=      {}
								logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`).")

						__lowerCAmelCase:	List[str]   				=      BlipaVisionConfig(**UpperCamelCase__)
						__lowerCAmelCase:	str   				=      BlipaQFormerConfig(**UpperCamelCase__)
						__lowerCAmelCase:	Optional[int]   				=      text_config["model_type"] if "model_type" in text_config else "opt"
						__lowerCAmelCase:	Optional[int]   				=      CONFIG_MAPPING[text_model_type](**UpperCamelCase__)

						__lowerCAmelCase:	List[Any]   				=      self.text_config.tie_word_embeddings
						__lowerCAmelCase:	List[Any]   				=      self.text_config.is_encoder_decoder

						__lowerCAmelCase:	int   				=      num_query_tokens
						__lowerCAmelCase:	Union[str, Any]   				=      self.vision_config.hidden_size
						__lowerCAmelCase:	Any   				=      self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
						__lowerCAmelCase:	Dict   				=      1.0
						__lowerCAmelCase:	Dict   				=      0.02

				@classmethod
				def 							lowercase_					(     cls							:    str					,  UpperCamelCase__							:    BlipaVisionConfig					,  UpperCamelCase__							:    BlipaQFormerConfig					,  UpperCamelCase__							:    PretrainedConfig					,  **UpperCamelCase__							:    Union[str, Any]					,  )->       Tuple:



						'''simple docstring'''


						return cls(
						    vision_config=vision_config.to_dict()					,  qformer_config=qformer_config.to_dict()					,  text_config=text_config.to_dict()					,  **UpperCamelCase__					,  )




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



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      copy.deepcopy(self.__dict__)
						__lowerCAmelCase:	Optional[int]   				=      self.vision_config.to_dict()
						__lowerCAmelCase:	List[Any]   				=      self.qformer_config.to_dict()
						__lowerCAmelCase:	str   				=      self.text_config.to_dict()
						__lowerCAmelCase:	Tuple   				=      self.__class__.model_type
						return output


