
"""simple docstring"""





import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    DebertaForMaskedLM,
							    DebertaForQuestionAnswering,
							    DebertaForSequenceClassification,
							    DebertaForTokenClassification,
							    DebertaModel,
							)
							from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		def __init__(     self							,  lowerCAmelCase__							,  lowerCAmelCase__=1_3							,  lowerCAmelCase__=7							,  lowerCAmelCase__=True							,  lowerCAmelCase__=True							,  lowerCAmelCase__=True							,  lowerCAmelCase__=True							,  lowerCAmelCase__=9_9							,  lowerCAmelCase__=3_2							,  lowerCAmelCase__=5							,  lowerCAmelCase__=4							,  lowerCAmelCase__=3_7							,  lowerCAmelCase__="gelu"							,  lowerCAmelCase__=0.1							,  lowerCAmelCase__=0.1							,  lowerCAmelCase__=5_1_2							,  lowerCAmelCase__=1_6							,  lowerCAmelCase__=2							,  lowerCAmelCase__=0.02							,  lowerCAmelCase__=False							,  lowerCAmelCase__=True							,  lowerCAmelCase__="None"							,  lowerCAmelCase__=3							,  lowerCAmelCase__=4							,  lowerCAmelCase__=None							,  ):
				__SCREAMING_SNAKE_CASE            = parent
				__SCREAMING_SNAKE_CASE            = batch_size
				__SCREAMING_SNAKE_CASE            = seq_length
				__SCREAMING_SNAKE_CASE            = is_training
				__SCREAMING_SNAKE_CASE            = use_input_mask
				__SCREAMING_SNAKE_CASE            = use_token_type_ids
				__SCREAMING_SNAKE_CASE            = use_labels
				__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            = intermediate_size
				__SCREAMING_SNAKE_CASE            = hidden_act
				__SCREAMING_SNAKE_CASE            = hidden_dropout_prob
				__SCREAMING_SNAKE_CASE            = attention_probs_dropout_prob
				__SCREAMING_SNAKE_CASE            = max_position_embeddings
				__SCREAMING_SNAKE_CASE            = type_vocab_size
				__SCREAMING_SNAKE_CASE            = type_sequence_label_size
				__SCREAMING_SNAKE_CASE            = initializer_range
				__SCREAMING_SNAKE_CASE            = num_labels
				__SCREAMING_SNAKE_CASE            = num_choices
				__SCREAMING_SNAKE_CASE            = relative_attention
				__SCREAMING_SNAKE_CASE            = position_biased_input
				__SCREAMING_SNAKE_CASE            = pos_att_type
				__SCREAMING_SNAKE_CASE            = scope




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size)

				__SCREAMING_SNAKE_CASE            = None
				if self.use_input_mask:
						__SCREAMING_SNAKE_CASE            = ids_tensor([self.batch_size, self.seq_length]							,  vocab_size=2)

				__SCREAMING_SNAKE_CASE            = None
				if self.use_token_type_ids:
						__SCREAMING_SNAKE_CASE            = ids_tensor([self.batch_size, self.seq_length]							,  self.type_vocab_size)

				__SCREAMING_SNAKE_CASE            = None
				__SCREAMING_SNAKE_CASE            = None
				__SCREAMING_SNAKE_CASE            = None
				if self.use_labels:
						__SCREAMING_SNAKE_CASE            = ids_tensor([self.batch_size]							,  self.type_sequence_label_size)
						__SCREAMING_SNAKE_CASE            = ids_tensor([self.batch_size, self.seq_length]							,  self.num_labels)
						__SCREAMING_SNAKE_CASE            = ids_tensor([self.batch_size]							,  self.num_choices)

				__SCREAMING_SNAKE_CASE            = self.get_config()

				return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




		def 	snake_case_     (     self):
				return DebertaConfig(
				    vocab_size=self.vocab_size							,  hidden_size=self.hidden_size							,  num_hidden_layers=self.num_hidden_layers							,  num_attention_heads=self.num_attention_heads							,  intermediate_size=self.intermediate_size							,  hidden_act=self.hidden_act							,  hidden_dropout_prob=self.hidden_dropout_prob							,  attention_probs_dropout_prob=self.attention_probs_dropout_prob							,  max_position_embeddings=self.max_position_embeddings							,  type_vocab_size=self.type_vocab_size							,  initializer_range=self.initializer_range							,  relative_attention=self.relative_attention							,  position_biased_input=self.position_biased_input							,  pos_att_type=self.pos_att_type							,  )




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.get_config()
				__SCREAMING_SNAKE_CASE            = 3_0_0
				return config




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				self.parent.assertListEqual(list(result.loss.size())							,  [])




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = DebertaModel(config=lowerCAmelCase__)
				model.to(lowerCAmelCase__)
				model.eval()
				__SCREAMING_SNAKE_CASE            = model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__)[0]
				__SCREAMING_SNAKE_CASE            = model(lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__)[0]
				__SCREAMING_SNAKE_CASE            = model(lowerCAmelCase__)[0]

				self.parent.assertListEqual(list(sequence_output.size())							,  [self.batch_size, self.seq_length, self.hidden_size])




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = DebertaForMaskedLM(config=lowerCAmelCase__)
				model.to(lowerCAmelCase__)
				model.eval()
				__SCREAMING_SNAKE_CASE            = model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__							,  labels=lowerCAmelCase__)

				self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.seq_length, self.vocab_size))




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = self.num_labels
				__SCREAMING_SNAKE_CASE            = DebertaForSequenceClassification(lowerCAmelCase__)
				model.to(lowerCAmelCase__)
				model.eval()
				__SCREAMING_SNAKE_CASE            = model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__							,  labels=lowerCAmelCase__)
				self.parent.assertListEqual(list(result.logits.size())							,  [self.batch_size, self.num_labels])
				self.check_loss_output(lowerCAmelCase__)




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = self.num_labels
				__SCREAMING_SNAKE_CASE            = DebertaForTokenClassification(config=lowerCAmelCase__)
				model.to(lowerCAmelCase__)
				model.eval()
				__SCREAMING_SNAKE_CASE            = model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__							,  labels=lowerCAmelCase__)
				self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.seq_length, self.num_labels))




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = DebertaForQuestionAnswering(config=lowerCAmelCase__)
				model.to(lowerCAmelCase__)
				model.eval()
				__SCREAMING_SNAKE_CASE            = model(
				    lowerCAmelCase__							,  attention_mask=lowerCAmelCase__							,  token_type_ids=lowerCAmelCase__							,  start_positions=lowerCAmelCase__							,  end_positions=lowerCAmelCase__							,  )
				self.parent.assertEqual(result.start_logits.shape							,  (self.batch_size, self.seq_length))
				self.parent.assertEqual(result.end_logits.shape							,  (self.batch_size, self.seq_length))




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.prepare_config_and_inputs()
				(
				    (
				    __SCREAMING_SNAKE_CASE
				)       ,(
				    __SCREAMING_SNAKE_CASE
				)       ,(
				    __SCREAMING_SNAKE_CASE
				)       ,(
				    __SCREAMING_SNAKE_CASE
				)       ,(
				    __SCREAMING_SNAKE_CASE
				)       ,(
				    __SCREAMING_SNAKE_CASE
				)       ,(
				    __SCREAMING_SNAKE_CASE
				)       ,
				)            = config_and_inputs
				__SCREAMING_SNAKE_CASE            = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
				return config, inputs_dict



@require_torch
class 						SCREAMING_SNAKE_CASE_ (						__a     ,				__a     ,				unittest.TestCase ):




		"""simple docstring"""





		__lowercase							:  Dict						   =		(
		    (
		        DebertaModel,
		        DebertaForMaskedLM,
		        DebertaForSequenceClassification,
		        DebertaForTokenClassification,
		        DebertaForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		__lowercase							:  Any						   =		(
		    {
		        '''feature-extraction''': DebertaModel,
		        '''fill-mask''': DebertaForMaskedLM,
		        '''question-answering''': DebertaForQuestionAnswering,
		        '''text-classification''': DebertaForSequenceClassification,
		        '''token-classification''': DebertaForTokenClassification,
		        '''zero-shot''': DebertaForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)

		__lowercase							:  int						   =		True
		__lowercase							:  Optional[int]						   =		False
		__lowercase							:  str						   =		False
		__lowercase							:  Tuple						   =		False
		__lowercase							:  Optional[int]						   =		False




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = DebertaModelTester(self)
				__SCREAMING_SNAKE_CASE            = ConfigTester(self							,  config_class=lowerCAmelCase__							,  hidden_size=3_7)




		def 	snake_case_     (     self):
				self.config_tester.run_common_tests()




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_model(*lowerCAmelCase__)




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase__)




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase__)




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase__)




		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase__)




		@slow
		def 	snake_case_     (     self):
				for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__SCREAMING_SNAKE_CASE            = DebertaModel.from_pretrained(lowerCAmelCase__)
						self.assertIsNotNone(lowerCAmelCase__)



@require_torch
@require_sentencepiece
@require_tokenizers
class 						SCREAMING_SNAKE_CASE_ (						unittest.TestCase ):




		"""simple docstring"""





		@unittest.skip(reason="""Model not available yet""")
		def 	snake_case_     (     self):
				pass




		@slow
		def 	snake_case_     (     self):
				__SCREAMING_SNAKE_CASE            = DebertaModel.from_pretrained("""microsoft/deberta-base""")

				__SCREAMING_SNAKE_CASE            = torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]])
				__SCREAMING_SNAKE_CASE            = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
				with torch.no_grad():
						__SCREAMING_SNAKE_CASE            = model(lowerCAmelCase__							,  attention_mask=lowerCAmelCase__)[0]
				# compare the actual values for a slice.
				__SCREAMING_SNAKE_CASE            = torch.tensor(
				    [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]])
				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]							,  lowerCAmelCase__							,  atol=1E-4)							,  f"{output[:, 1:4, 1:4]}")






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        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     Optional[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.00_001		,    snake_case=0.0		,    snake_case=1E-10		,    snake_case=True		,    **snake_case		,    ):
		super().__init__(**snake_case       )

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





	@classmethod
	def 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    **snake_case       ):
		cls._set_token_in_kwargs(snake_case       )

		lowercase  ,					lowercase       =    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":
			lowercase       =    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        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     int							 =       """blip_2_qformer"""





	def __init__(       self		,    snake_case=3_0522		,    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-12		,    snake_case=0		,    snake_case="absolute"		,    snake_case=2		,    snake_case=1408		,    **snake_case		,    ):
		super().__init__(pad_token_id=snake_case		,    **snake_case       )

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





	@classmethod
	def 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    **snake_case       ):
		cls._set_token_in_kwargs(snake_case       )

		lowercase  ,					lowercase       =    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":
			lowercase       =    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        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     List[Any]							 =       """blip-2"""
	_UpperCamelCase     :     str							 =       True





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

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

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

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

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

		lowercase       =    self.text_config.tie_word_embeddings
		lowercase       =    self.text_config.is_encoder_decoder

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





	@classmethod
	def 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    snake_case		,    snake_case		,    **snake_case		,    ):
		return cls(
		    vision_config=vision_config.to_dict()		,    qformer_config=qformer_config.to_dict()		,    text_config=text_config.to_dict()		,    **snake_case		,    )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    copy.deepcopy(self.__dict__       )
		lowercase       =    self.vision_config.to_dict()
		lowercase       =    self.qformer_config.to_dict()
		lowercase       =    self.text_config.to_dict()
		lowercase       =    self.__class__.model_type
		return output

