





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 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						"""blip_2_vision_model"""





			def __init__(    self       ,  __lowerCAmelCase=1_4_0_8       ,  __lowerCAmelCase=6_1_4_4       ,  __lowerCAmelCase=3_9       ,  __lowerCAmelCase=1_6       ,  __lowerCAmelCase=2_2_4       ,  __lowerCAmelCase=1_4       ,  __lowerCAmelCase="gelu"       ,  __lowerCAmelCase=0.0_0001       ,  __lowerCAmelCase=0.0       ,  __lowerCAmelCase=1E-10       ,  __lowerCAmelCase=True       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







				super().__init__(**__lowerCAmelCase							)

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





			@classmethod
			def 		__lowerCamelCase				(    cls       ,  __lowerCAmelCase       ,  **__lowerCAmelCase							):



				'''simple docstring'''







				cls._set_token_in_kwargs(__lowerCAmelCase							)

				lowerCamelCase__				,	lowerCamelCase__								=		cls.get_config_dict(__lowerCAmelCase       ,  **__lowerCAmelCase							)

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

			'''simple docstring'''







			lowerCAmelCase_							     =						"""blip_2_qformer"""





			def __init__(    self       ,  __lowerCAmelCase=3_0_5_2_2       ,  __lowerCAmelCase=7_6_8       ,  __lowerCAmelCase=1_2       ,  __lowerCAmelCase=1_2       ,  __lowerCAmelCase=3_0_7_2       ,  __lowerCAmelCase="gelu"       ,  __lowerCAmelCase=0.1       ,  __lowerCAmelCase=0.1       ,  __lowerCAmelCase=5_1_2       ,  __lowerCAmelCase=0.02       ,  __lowerCAmelCase=1E-12       ,  __lowerCAmelCase=0       ,  __lowerCAmelCase="absolute"       ,  __lowerCAmelCase=2       ,  __lowerCAmelCase=1_4_0_8       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







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

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





			@classmethod
			def 		__lowerCamelCase				(    cls       ,  __lowerCAmelCase       ,  **__lowerCAmelCase							):



				'''simple docstring'''







				cls._set_token_in_kwargs(__lowerCAmelCase							)

				lowerCamelCase__				,	lowerCamelCase__								=		cls.get_config_dict(__lowerCAmelCase       ,  **__lowerCAmelCase							)

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






class 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						"""blip-2"""
			lowerCAmelCase_							     =						True





			def __init__(    self       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase=3_2       ,  **__lowerCAmelCase							):



				'''simple docstring'''







				super().__init__(**__lowerCAmelCase							)

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

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

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

				lowerCamelCase__								=		BlipaVisionConfig(**__lowerCAmelCase							)
				lowerCamelCase__								=		BlipaQFormerConfig(**__lowerCAmelCase							)
				lowerCamelCase__								=		text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
				lowerCamelCase__								=		CONFIG_MAPPING[text_model_type](**__lowerCAmelCase							)

				lowerCamelCase__								=		self.text_config.tie_word_embeddings
				lowerCamelCase__								=		self.text_config.is_encoder_decoder

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





			@classmethod
			def 		__lowerCamelCase				(    cls       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







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





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







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










import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows





def lowerCAmelCase__(__snake_case							)				->      int:  # picklable for multiprocessing







	'''simple docstring'''







	return i + 1





@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def lowerCAmelCase__()				->      Any:







	'''simple docstring'''







	with parallel_backend('''spark'''							):
		assert ParallelBackendConfig.backend_name == "spark"

	lowerCamelCase__								=		[1, 2, 3]
	with pytest.raises(__snake_case							):
		with parallel_backend('''unsupported backend'''							):
			map_nested(__snake_case       ,__snake_case       ,num_proc=2							)

	with pytest.raises(__snake_case							):
		with parallel_backend('''unsupported backend'''							):
			map_nested(__snake_case       ,__snake_case       ,num_proc=-1							)





@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize('''num_proc'''       ,[2, -1]							)
def lowerCAmelCase__(__snake_case							)				->      Tuple:







	'''simple docstring'''







	lowerCamelCase__								=		[1, 2]
	lowerCamelCase__								=		{'''a''': 1, '''b''': 2}
	lowerCamelCase__								=		{'''a''': [1, 2], '''b''': [3, 4]}
	lowerCamelCase__								=		{'''a''': {'''1''': 1}, '''b''': 2}
	lowerCamelCase__								=		{'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4}
	lowerCamelCase__								=		[2, 3]
	lowerCamelCase__								=		{'''a''': 2, '''b''': 3}
	lowerCamelCase__								=		{'''a''': [2, 3], '''b''': [4, 5]}
	lowerCamelCase__								=		{'''a''': {'''1''': 2}, '''b''': 3}
	lowerCamelCase__								=		{'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5}

	with parallel_backend('''spark'''							):
		assert map_nested(__snake_case       ,__snake_case       ,num_proc=__snake_case							) == expected_map_nested_sa
		assert map_nested(__snake_case       ,__snake_case       ,num_proc=__snake_case							) == expected_map_nested_sa
		assert map_nested(__snake_case       ,__snake_case       ,num_proc=__snake_case							) == expected_map_nested_sa
		assert map_nested(__snake_case       ,__snake_case       ,num_proc=__snake_case							) == expected_map_nested_sa
		assert map_nested(__snake_case       ,__snake_case       ,num_proc=__snake_case							) == expected_map_nested_sa



