


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


__UpperCamelCase     		=							logging.get_logger(__name__)

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







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       Any 			=		'blip_2_vision_model'




			def __init__(				self   ,    snake_case=1408   ,    snake_case=6144   ,    snake_case=39   ,    snake_case=16   ,    snake_case=224   ,    snake_case=14   ,    snake_case="gelu"   ,    snake_case=0.00001   ,    snake_case=0.0   ,    snake_case=1E-1_0   ,    snake_case=True   ,    **snake_case   ,    ):

						'''simple docstring'''


						super().__init__(**snake_case  )

						UpperCamelCase__       =							hidden_size
						UpperCamelCase__       =							intermediate_size
						UpperCamelCase__       =							num_hidden_layers
						UpperCamelCase__       =							num_attention_heads
						UpperCamelCase__       =							patch_size
						UpperCamelCase__       =							image_size
						UpperCamelCase__       =							initializer_range
						UpperCamelCase__       =							attention_dropout
						UpperCamelCase__       =							layer_norm_eps
						UpperCamelCase__       =							hidden_act
						UpperCamelCase__       =							qkv_bias







			@classmethod
			def 					snake_case__					(				cls   ,    snake_case   ,    **snake_case  ):

						'''simple docstring'''


						cls._set_token_in_kwargs(snake_case  )

						UpperCamelCase__,   UpperCamelCase__       =							cls.get_config_dict(snake_case   ,    **snake_case  )

						# get the vision config dict if we are loading from Blip2Config
						if config_dict.get("model_type"  ) == "blip-2":
									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(snake_case   ,    **snake_case  )







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       List[Any] 			=		'blip_2_qformer'




			def __init__(				self   ,    snake_case=30522   ,    snake_case=768   ,    snake_case=12   ,    snake_case=12   ,    snake_case=3072   ,    snake_case="gelu"   ,    snake_case=0.1   ,    snake_case=0.1   ,    snake_case=512   ,    snake_case=0.02   ,    snake_case=1E-1_2   ,    snake_case=0   ,    snake_case="absolute"   ,    snake_case=2   ,    snake_case=1408   ,    **snake_case   ,    ):

						'''simple docstring'''


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

						UpperCamelCase__       =							vocab_size
						UpperCamelCase__       =							hidden_size
						UpperCamelCase__       =							num_hidden_layers
						UpperCamelCase__       =							num_attention_heads
						UpperCamelCase__       =							hidden_act
						UpperCamelCase__       =							intermediate_size
						UpperCamelCase__       =							hidden_dropout_prob
						UpperCamelCase__       =							attention_probs_dropout_prob
						UpperCamelCase__       =							max_position_embeddings
						UpperCamelCase__       =							initializer_range
						UpperCamelCase__       =							layer_norm_eps
						UpperCamelCase__       =							position_embedding_type
						UpperCamelCase__       =							cross_attention_frequency
						UpperCamelCase__       =							encoder_hidden_size







			@classmethod
			def 					snake_case__					(				cls   ,    snake_case   ,    **snake_case  ):

						'''simple docstring'''


						cls._set_token_in_kwargs(snake_case  )

						UpperCamelCase__,   UpperCamelCase__       =							cls.get_config_dict(snake_case   ,    **snake_case  )

						# get the qformer config dict if we are loading from Blip2Config
						if config_dict.get("model_type"  ) == "blip-2":
									UpperCamelCase__       =							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(snake_case   ,    **snake_case  )







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       Optional[Any] 			=		'blip-2'
			_UpperCamelCase		:       Optional[Any] 			=		True




			def __init__(				self   ,    snake_case=None   ,    snake_case=None   ,    snake_case=None   ,    snake_case=32   ,    **snake_case  ):

						'''simple docstring'''


						super().__init__(**snake_case  )

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

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

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

						UpperCamelCase__       =							BlipaVisionConfig(**snake_case  )
						UpperCamelCase__       =							BlipaQFormerConfig(**snake_case  )
						UpperCamelCase__       =							text_config["model_type"] if "model_type" in text_config else "opt"
						UpperCamelCase__       =							CONFIG_MAPPING[text_model_type](**snake_case  )

						UpperCamelCase__       =							self.text_config.tie_word_embeddings
						UpperCamelCase__       =							self.text_config.is_encoder_decoder

						UpperCamelCase__       =							num_query_tokens
						UpperCamelCase__       =							self.vision_config.hidden_size
						UpperCamelCase__       =							self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
						UpperCamelCase__       =							1.0
						UpperCamelCase__       =							0.02




			@classmethod
			def 					snake_case__					(				cls   ,    snake_case   ,    snake_case   ,    snake_case   ,    **snake_case   ,    ):

						'''simple docstring'''


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







			def 					snake_case__					(				self  ):

						'''simple docstring'''


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






from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			def __init__(				self   ,    snake_case   ,    snake_case = None   ,    snake_case = None   ,    snake_case = False   ,    snake_case = False   ,    snake_case = None   ,    snake_case = None   ,    **snake_case   ,    ):

						'''simple docstring'''


						super().__init__(
						    features=snake_case   ,    cache_dir=snake_case   ,    keep_in_memory=snake_case   ,    streaming=snake_case   ,    num_proc=snake_case   ,    **snake_case   ,    )
						UpperCamelCase__       =							Generator(
						    cache_dir=snake_case   ,    features=snake_case   ,    generator=snake_case   ,    gen_kwargs=snake_case   ,    **snake_case   ,    )







			def 					snake_case__					(				self  ):

						'''simple docstring'''


						if self.streaming:
									UpperCamelCase__       =							self.builder.as_streaming_dataset(split="train"  )
						# Build regular (map-style) dataset
						else:
									UpperCamelCase__       =							None
									UpperCamelCase__       =							None
									UpperCamelCase__       =							None
									UpperCamelCase__       =							None

									self.builder.download_and_prepare(
									    download_config=snake_case   ,    download_mode=snake_case   ,    verification_mode=snake_case   ,    base_path=snake_case   ,    num_proc=self.num_proc   ,    )
									UpperCamelCase__       =							self.builder.as_dataset(
									    split="train"   ,    verification_mode=snake_case   ,    in_memory=self.keep_in_memory  )
						return dataset


