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


							def __init__(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_=13    ,   lowerCAmelCase_=7    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=99    ,   lowerCAmelCase_=32    ,   lowerCAmelCase_=5    ,   lowerCAmelCase_=4    ,   lowerCAmelCase_=37    ,   lowerCAmelCase_="gelu"    ,   lowerCAmelCase_=0.1    ,   lowerCAmelCase_=0.1    ,   lowerCAmelCase_=5_12    ,   lowerCAmelCase_=16    ,   lowerCAmelCase_=2    ,   lowerCAmelCase_=0.02    ,   lowerCAmelCase_=False    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_="None"    ,   lowerCAmelCase_=3    ,   lowerCAmelCase_=4    ,   lowerCAmelCase_=None    ,   ):





								"""simple docstring"""






								_snake_case   						=		parent
								_snake_case   						=		batch_size
								_snake_case   						=		seq_length
								_snake_case   						=		is_training
								_snake_case   						=		use_input_mask
								_snake_case   						=		use_token_type_ids
								_snake_case   						=		use_labels
								_snake_case   						=		vocab_size
								_snake_case   						=		hidden_size
								_snake_case   						=		num_hidden_layers
								_snake_case   						=		num_attention_heads
								_snake_case   						=		intermediate_size
								_snake_case   						=		hidden_act
								_snake_case   						=		hidden_dropout_prob
								_snake_case   						=		attention_probs_dropout_prob
								_snake_case   						=		max_position_embeddings
								_snake_case   						=		type_vocab_size
								_snake_case   						=		type_sequence_label_size
								_snake_case   						=		initializer_range
								_snake_case   						=		num_labels
								_snake_case   						=		num_choices
								_snake_case   						=		relative_attention
								_snake_case   						=		position_biased_input
								_snake_case   						=		pos_att_type
								_snake_case   						=		scope


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size  )

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

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

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

								_snake_case   						=		self.get_config()

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


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								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  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_config()
								_snake_case   						=		3_00
								return config


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								self.parent.assertListEqual(list(result.loss.size()  )    ,   []  )


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		DebertaModel(config=lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()
								_snake_case   						=		model(lowerCAmelCase_    ,   attention_mask=lowerCAmelCase_    ,   token_type_ids=lowerCAmelCase_  )[0]
								_snake_case   						=		model(lowerCAmelCase_    ,   token_type_ids=lowerCAmelCase_  )[0]
								_snake_case   						=		model(lowerCAmelCase_  )[0]

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


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		DebertaForMaskedLM(config=lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()
								_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  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		self.num_labels
								_snake_case   						=		DebertaForSequenceClassification(lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()
								_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  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		self.num_labels
								_snake_case   						=		DebertaForTokenClassification(config=lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()
								_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  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		DebertaForQuestionAnswering(config=lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()
								_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  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.prepare_config_and_inputs()
								(
								    (
								    _snake_case					
								)						, (
								    _snake_case					
								)						, (
								    _snake_case					
								)						, (
								    _snake_case					
								)						, (
								    _snake_case					
								)						, (
								    _snake_case					
								)						, (
								    _snake_case					
								)						, 					
								)   						=		config_and_inputs
								_snake_case   						=		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
								return config, inputs_dict


@require_torch
class 						__UpperCAmelCase      (					__lowerCamelCase						,   __lowerCamelCase						,   unittest.TestCase						):
							__lowercase      		=       (
							    (
							        DebertaModel,
							        DebertaForMaskedLM,
							        DebertaForSequenceClassification,
							        DebertaForTokenClassification,
							        DebertaForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							__lowercase      		=       (
							    {
							        """feature-extraction""": DebertaModel,
							        """fill-mask""": DebertaForMaskedLM,
							        """question-answering""": DebertaForQuestionAnswering,
							        """text-classification""": DebertaForSequenceClassification,
							        """token-classification""": DebertaForTokenClassification,
							        """zero-shot""": DebertaForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

							__lowercase      		=       True
							__lowercase      		=       False
							__lowercase      		=       False
							__lowercase      		=       False
							__lowercase      		=       False


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		DebertaModelTester(self  )
								_snake_case   						=		ConfigTester(self    ,   config_class=lowerCAmelCase_    ,   hidden_size=37  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								self.config_tester.run_common_tests()


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase_  )




							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_snake_case   						=		DebertaModel.from_pretrained(lowerCAmelCase_  )
									self.assertIsNotNone(lowerCAmelCase_  )


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


							@unittest.skip(reason='Model not available yet'  )
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								pass




							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		DebertaModel.from_pretrained('microsoft/deberta-base'  )

								_snake_case   						=		torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]  )
								_snake_case   						=		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
								with torch.no_grad():
									_snake_case   						=		model(lowerCAmelCase_    ,   attention_mask=lowerCAmelCase_  )[0]
								# compare the actual values for a slice.
								_snake_case   						=		torch.tensor(
								    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]  )
								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

