
"""simple docstring"""





import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402


__magic_name__       =			get_tests_dir("fixtures/dummy-config.json")
class 						SCREAMING_SNAKE_CASE_ (						unittest.TestCase ):




		"""simple docstring"""





		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = 0




		def 	snake_case_     (     self):
				self.assertIsNotNone(transformers.models.auto.__spec__)
				self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""))




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""bert-base-uncased""")
				self.assertIsInstance(lowerCAmelCase__							,  lowerCAmelCase__)




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained(lowerCAmelCase__)
				self.assertIsInstance(lowerCAmelCase__							,  lowerCAmelCase__)




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained(lowerCAmelCase__)
				self.assertIsInstance(lowerCAmelCase__							,  lowerCAmelCase__)




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = AutoConfig.for_model("""roberta""")
				self.assertIsInstance(lowerCAmelCase__							,  lowerCAmelCase__)




		def 	snake_case_     (     self):
				with tempfile.TemporaryDirectory() as tmp_dir:
						# This model name contains bert and roberta, but roberta ends up being picked.
						__SCREAMING_SNAKE_CASE            = os.path.join(lowerCAmelCase__							,  """fake-roberta""")
						os.makedirs(lowerCAmelCase__							,  exist_ok=lowerCAmelCase__)
						with open(os.path.join(lowerCAmelCase__							,  """config.json""")							,  """w""") as f:
								f.write(json.dumps({}))
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained(lowerCAmelCase__)
						self.assertEqual(type(lowerCAmelCase__)							,  lowerCAmelCase__)




		def 	snake_case_     (     self):
				try:
						AutoConfig.register("""custom"""							,  lowerCAmelCase__)
						# Wrong model type will raise an error
						with self.assertRaises(lowerCAmelCase__):
								AutoConfig.register("""model"""							,  lowerCAmelCase__)
						# Trying to register something existing in the Transformers library will raise an error
						with self.assertRaises(lowerCAmelCase__):
								AutoConfig.register("""bert"""							,  lowerCAmelCase__)

						# Now that the config is registered, it can be used as any other config with the auto-API
						__SCREAMING_SNAKE_CASE            = CustomConfig()
						with tempfile.TemporaryDirectory() as tmp_dir:
								config.save_pretrained(lowerCAmelCase__)
								__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained(lowerCAmelCase__)
								self.assertIsInstance(lowerCAmelCase__							,  lowerCAmelCase__)

				finally:
						if "custom" in CONFIG_MAPPING._extra_content:
								del CONFIG_MAPPING._extra_content["custom"]




		def 	snake_case_     (     self):
				with self.assertRaisesRegex(
				    lowerCAmelCase__							,  """bert-base is not a local folder and is not a valid model identifier"""):
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""bert-base""")




		def 	snake_case_     (     self):
				with self.assertRaisesRegex(
				    lowerCAmelCase__							,  R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""):
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained(lowerCAmelCase__							,  revision="""aaaaaa""")




		def 	snake_case_     (     self):
				with self.assertRaisesRegex(
				    lowerCAmelCase__							,  """hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""							,  ):
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo""")




		def 	snake_case_     (     self):
				# If remote code is not set, we will time out when asking whether to load the model.
				with self.assertRaises(lowerCAmelCase__):
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model""")
				# If remote code is disabled, we can't load this config.
				with self.assertRaises(lowerCAmelCase__):
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""							,  trust_remote_code=lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""							,  trust_remote_code=lowerCAmelCase__)
				self.assertEqual(config.__class__.__name__							,  """NewModelConfig""")

				# Test config can be reloaded.
				with tempfile.TemporaryDirectory() as tmp_dir:
						config.save_pretrained(lowerCAmelCase__)
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained(lowerCAmelCase__							,  trust_remote_code=lowerCAmelCase__)
				self.assertEqual(reloaded_config.__class__.__name__							,  """NewModelConfig""")




		def 	snake_case_     (     self):
				class 						SCREAMING_SNAKE_CASE_ (						__a ):




						"""simple docstring"""





						__lowercase							:  Tuple						   =		'''new-model'''

				try:
						AutoConfig.register("""new-model"""							,  lowerCAmelCase__)
						# If remote code is not set, the default is to use local
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model""")
						self.assertEqual(config.__class__.__name__							,  """NewModelConfigLocal""")

						# If remote code is disabled, we load the local one.
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""							,  trust_remote_code=lowerCAmelCase__)
						self.assertEqual(config.__class__.__name__							,  """NewModelConfigLocal""")

						# If remote is enabled, we load from the Hub
						__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""							,  trust_remote_code=lowerCAmelCase__)
						self.assertEqual(config.__class__.__name__							,  """NewModelConfig""")

				finally:
						if "new-model" in CONFIG_MAPPING._extra_content:
								del CONFIG_MAPPING._extra_content["new-model"]






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


__magic_name__       =			logging.get_logger(__name__)

__magic_name__       =			{
    "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json",
}
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Optional[Any]						   =		'''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_00_01							,  lowerCAmelCase__=0.0							,  lowerCAmelCase__=1E-10							,  lowerCAmelCase__=True							,  **lowerCAmelCase__							,  ):
				super().__init__(**lowerCAmelCase__)

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




		@classmethod
		def 	snake_case_     (     cls							,  lowerCAmelCase__							,  **lowerCAmelCase__):
				cls._set_token_in_kwargs(lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE       ,__SCREAMING_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":
						__SCREAMING_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 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Tuple						   =		'''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__							,  ):
				super().__init__(pad_token_id=lowerCAmelCase__							,  **lowerCAmelCase__)

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




		@classmethod
		def 	snake_case_     (     cls							,  lowerCAmelCase__							,  **lowerCAmelCase__):
				cls._set_token_in_kwargs(lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE       ,__SCREAMING_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":
						__SCREAMING_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 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





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




		def __init__(     self							,  lowerCAmelCase__=None							,  lowerCAmelCase__=None							,  lowerCAmelCase__=None							,  lowerCAmelCase__=3_2							,  **lowerCAmelCase__):
				super().__init__(**lowerCAmelCase__)

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

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

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

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

				__SCREAMING_SNAKE_CASE            = self.text_config.tie_word_embeddings
				__SCREAMING_SNAKE_CASE            = self.text_config.is_encoder_decoder

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




		@classmethod
		def 	snake_case_     (     cls							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  **lowerCAmelCase__							,  ):
				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):
				__SCREAMING_SNAKE_CASE            = copy.deepcopy(self.__dict__)
				__SCREAMING_SNAKE_CASE            = self.vision_config.to_dict()
				__SCREAMING_SNAKE_CASE            = self.qformer_config.to_dict()
				__SCREAMING_SNAKE_CASE            = self.text_config.to_dict()
				__SCREAMING_SNAKE_CASE            = self.__class__.model_type
				return output




