"""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


SCREAMING_SNAKE_CASE__      						=  logging.get_logger(__name__)

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



class   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    List[str]			  =						"""blip_2_vision_model"""

				def __init__(				self		,    lowerCAmelCase=14_08		,    lowerCAmelCase=61_44		,    lowerCAmelCase=39		,    lowerCAmelCase=16		,    lowerCAmelCase=2_24		,    lowerCAmelCase=14		,    lowerCAmelCase="gelu"		,    lowerCAmelCase=0.0_00_01		,    lowerCAmelCase=0.0		,    lowerCAmelCase=1E-10		,    lowerCAmelCase=True		,    **lowerCAmelCase		,    ):






										"""simple docstring"""

										super().__init__(**lowerCAmelCase    )

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

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






										"""simple docstring"""

										cls._set_token_in_kwargs(lowerCAmelCase    )

										snake_case					,snake_case								=			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":
																snake_case								=			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   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    str			  =						"""blip_2_qformer"""

				def __init__(				self		,    lowerCAmelCase=3_05_22		,    lowerCAmelCase=7_68		,    lowerCAmelCase=12		,    lowerCAmelCase=12		,    lowerCAmelCase=30_72		,    lowerCAmelCase="gelu"		,    lowerCAmelCase=0.1		,    lowerCAmelCase=0.1		,    lowerCAmelCase=5_12		,    lowerCAmelCase=0.02		,    lowerCAmelCase=1E-12		,    lowerCAmelCase=0		,    lowerCAmelCase="absolute"		,    lowerCAmelCase=2		,    lowerCAmelCase=14_08		,    **lowerCAmelCase		,    ):






										"""simple docstring"""

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

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

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






										"""simple docstring"""

										cls._set_token_in_kwargs(lowerCAmelCase    )

										snake_case					,snake_case								=			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":
																snake_case								=			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   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    Union[str, Any]			  =						"""blip-2"""
				_lowerCAmelCase     :    Optional[Any]			  =						True

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






										"""simple docstring"""

										super().__init__(**lowerCAmelCase    )

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

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

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

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

										snake_case								=			self.text_config.tie_word_embeddings
										snake_case								=			self.text_config.is_encoder_decoder

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

				@classmethod
				def 		snake_case (				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 		snake_case (				self    ):






										"""simple docstring"""

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



"""simple docstring"""
import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)



class   lowerCAmelCase_    (			unittest.TestCase					):



				"""simple docstring"""

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			['a', 'b', 'c']

										# Defaults to last layer if both are None
										snake_case					,snake_case								=			get_aligned_output_features_output_indices(lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase    )
										self.assertEqual(lowerCAmelCase		,    ['c']    )
										self.assertEqual(lowerCAmelCase		,    [2]    )

										# Out indices set to match out features
										snake_case					,snake_case								=			get_aligned_output_features_output_indices(['a', 'c']		,    lowerCAmelCase		,    lowerCAmelCase    )
										self.assertEqual(lowerCAmelCase		,    ['a', 'c']    )
										self.assertEqual(lowerCAmelCase		,    [0, 2]    )

										# Out features set to match out indices
										snake_case					,snake_case								=			get_aligned_output_features_output_indices(lowerCAmelCase		,    [0, 2]		,    lowerCAmelCase    )
										self.assertEqual(lowerCAmelCase		,    ['a', 'c']    )
										self.assertEqual(lowerCAmelCase		,    [0, 2]    )

										# Out features selected from negative indices
										snake_case					,snake_case								=			get_aligned_output_features_output_indices(lowerCAmelCase		,    [-3, -1]		,    lowerCAmelCase    )
										self.assertEqual(lowerCAmelCase		,    ['a', 'c']    )
										self.assertEqual(lowerCAmelCase		,    [-3, -1]    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(['a', 'b']		,    (0, 1)		,    lowerCAmelCase    )

										# Out features must be a list
										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(('a', 'b')		,    (0, 1)		,    ['a', 'b']    )

										# Out features must be a subset of stage names
										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(['a', 'b']		,    (0, 1)		,    ['a']    )

										# Out indices must be a list or tuple
										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(lowerCAmelCase		,    0		,    ['a', 'b']    )

										# Out indices must be a subset of stage names
										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(lowerCAmelCase		,    (0, 1)		,    ['a']    )

										# Out features and out indices must be the same length
										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(['a', 'b']		,    (0,)		,    ['a', 'b', 'c']    )

										# Out features should match out indices
										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(['a', 'b']		,    (0, 2)		,    ['a', 'b', 'c']    )

										# Out features and out indices should be in order
										with self.assertRaises(lowerCAmelCase    ):
																verify_out_features_out_indices(['b', 'a']		,    (0, 1)		,    ['a', 'b']    )

										# Check passes with valid inputs
										verify_out_features_out_indices(['a', 'b', 'd']		,    (0, 1, -1)		,    ['a', 'b', 'c', 'd']    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			BackboneMixin()

										snake_case								=			['a', 'b', 'c']
										snake_case								=			['a', 'c']
										snake_case								=			[0, 2]

										# Check that the output features and indices are set correctly
										self.assertEqual(backbone.out_features		,    ['a', 'c']    )
										self.assertEqual(backbone.out_indices		,    [0, 2]    )

										# Check out features and indices are updated correctly
										snake_case								=			['a', 'b']
										self.assertEqual(backbone.out_features		,    ['a', 'b']    )
										self.assertEqual(backbone.out_indices		,    [0, 1]    )

										snake_case								=			[-3, -1]
										self.assertEqual(backbone.out_features		,    ['a', 'c']    )
										self.assertEqual(backbone.out_indices		,    [-3, -1]    )


