



from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
    from transformers import (
        TFBertModel,
        TFCLIPVisionModel,
        TFDeiTModel,
        TFRobertaModel,
        TFVisionTextDualEncoderModel,
        TFViTModel,
        VisionTextDualEncoderConfig,
    )

if is_vision_available():
    from PIL import Image

    from transformers import VisionTextDualEncoderProcessor
def 		UpperCamelCase	(   snake_case__       :    int	)	->     List[str]:
   if isinstance(A__       ,     collections.abc.Iterable	):
      return x
   return (x, x)



@require_tf
class 				lowerCAmelCase_		:
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		int:
          pass
       def 					snake_case_       (			self       )     ->		Tuple:
          pass
       def 					snake_case_       (			self       )     ->		Optional[int]:
          pass
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=None,			**SCREAMING_SNAKE_CASE_       )     ->		Any:
          UpperCamelCase    :  List[str]							=	VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase_,			lowerCAmelCase_       )

          UpperCamelCase    :  Optional[Any]							=	TFVisionTextDualEncoderModel(lowerCAmelCase_       )

          UpperCamelCase    :  Tuple							=	model(input_ids=lowerCAmelCase_,			pixel_values=lowerCAmelCase_,			attention_mask=lowerCAmelCase_       )

          self.assertEqual(output['text_embeds'].shape,			(input_ids.shape[0], config.projection_dim)       )
          self.assertEqual(output['image_embeds'].shape,			(pixel_values.shape[0], config.projection_dim)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=None,			**SCREAMING_SNAKE_CASE_       )     ->		str:
          UpperCamelCase    :  List[str]							=	self.get_vision_text_model(lowerCAmelCase_,			lowerCAmelCase_       )
          UpperCamelCase    :  Union[str, Any]							=	TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase_,			text_model=lowerCAmelCase_       )

          UpperCamelCase    :  Dict							=	model(input_ids=lowerCAmelCase_,			pixel_values=lowerCAmelCase_,			attention_mask=lowerCAmelCase_       )

          self.assertEqual(output['text_embeds'].shape,			(input_ids.shape[0], model.config.projection_dim)       )
          self.assertEqual(output['image_embeds'].shape,			(pixel_values.shape[0], model.config.projection_dim)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=None,			**SCREAMING_SNAKE_CASE_       )     ->		Optional[Any]:
          UpperCamelCase    :  Tuple							=	self.get_vision_text_model(lowerCAmelCase_,			lowerCAmelCase_       )
          UpperCamelCase    :  Optional[Any]							=	{"vision_model": vision_model, "text_model": text_model}
          UpperCamelCase    :  Optional[int]							=	TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase_       )

          UpperCamelCase    :  Optional[Any]							=	model(input_ids=lowerCAmelCase_,			pixel_values=lowerCAmelCase_,			attention_mask=lowerCAmelCase_       )

          self.assertEqual(output['text_embeds'].shape,			(input_ids.shape[0], model.config.projection_dim)       )
          self.assertEqual(output['image_embeds'].shape,			(pixel_values.shape[0], model.config.projection_dim)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=None,			**SCREAMING_SNAKE_CASE_       )     ->		Union[str, Any]:
          UpperCamelCase    :  List[Any]							=	self.get_vision_text_model(lowerCAmelCase_,			lowerCAmelCase_       )
          UpperCamelCase    :  Optional[int]							=	TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase_,			text_model=lowerCAmelCase_       )

          UpperCamelCase    :  Optional[Any]							=	model(input_ids=lowerCAmelCase_,			pixel_values=lowerCAmelCase_,			attention_mask=lowerCAmelCase_       )
          UpperCamelCase    :  Any							=	output[0].numpy()

          with tempfile.TemporaryDirectory() as tmpdirname:
             model.save_pretrained(lowerCAmelCase_       )
             UpperCamelCase    :  Tuple							=	TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_       )

             UpperCamelCase    :  List[Any]							=	model(input_ids=lowerCAmelCase_,			pixel_values=lowerCAmelCase_,			attention_mask=lowerCAmelCase_       )
             UpperCamelCase    :  List[str]							=	after_output[0].numpy()
             UpperCamelCase    :  Any							=	np.amax(np.abs(out_a - out_a       )       )
             self.assertLessEqual(lowerCAmelCase_,			1e-5       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=None,			**SCREAMING_SNAKE_CASE_       )     ->		Union[str, Any]:
          UpperCamelCase    :  Dict							=	self.get_vision_text_model(lowerCAmelCase_,			lowerCAmelCase_       )
          UpperCamelCase    :  Tuple							=	TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase_,			text_model=lowerCAmelCase_       )

          UpperCamelCase    :  List[str]							=	model(
              input_ids=lowerCAmelCase_,			pixel_values=lowerCAmelCase_,			attention_mask=lowerCAmelCase_,			output_attentions=lowerCAmelCase_       )

          UpperCamelCase    :  str							=	output.vision_model_output.attentions
          self.assertEqual(len(lowerCAmelCase_       ),			vision_config.num_hidden_layers       )

          # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
          UpperCamelCase    :  Dict							=	to_atuple(vision_model.config.image_size       )
          UpperCamelCase    :  Optional[Any]							=	to_atuple(vision_model.config.patch_size       )
          UpperCamelCase    :  Union[str, Any]							=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
          UpperCamelCase    :  Dict							=	num_patches + 1
          self.assertEqual(vision_attentions[0].shape[-3:],			(vision_config.num_attention_heads, seq_len, seq_len)       )

          UpperCamelCase    :  Tuple							=	output.text_model_output.attentions
          self.assertEqual(len(lowerCAmelCase_       ),			text_config.num_hidden_layers       )

          self.assertEqual(
              text_attentions[0].shape[-3:],			(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]),			)
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		str:
          UpperCamelCase    :  Dict							=	np.abs((a - b)       ).max()
          self.assertLessEqual(lowerCAmelCase_,			lowerCAmelCase_,			F"""Difference between torch and flax is {diff} (>= {tol})."""       )
       def 					snake_case_       (			self       )     ->		List[str]:
          UpperCamelCase    :  List[Any]							=	self.prepare_config_and_inputs()
          self.check_vision_text_dual_encoder_model(**lowerCAmelCase_       )
       def 					snake_case_       (			self       )     ->		List[str]:
          UpperCamelCase    :  int							=	self.prepare_config_and_inputs()
          self.check_model_from_pretrained_configs(**lowerCAmelCase_       )
       def 					snake_case_       (			self       )     ->		List[str]:
          UpperCamelCase    :  Tuple							=	self.prepare_config_and_inputs()
          self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase_       )
       def 					snake_case_       (			self       )     ->		Tuple:
          UpperCamelCase    :  str							=	self.prepare_config_and_inputs()
          self.check_save_load(**lowerCAmelCase_       )
       def 					snake_case_       (			self       )     ->		List[str]:
          UpperCamelCase    :  Tuple							=	self.prepare_config_and_inputs()
          self.check_vision_text_output_attention(**lowerCAmelCase_       )






       @slow
       def 					snake_case_       (			self       )     ->		List[Any]:
          UpperCamelCase    :  Tuple							=	self.get_pretrained_model_and_inputs()

          UpperCamelCase    :  str							=	model_a(**lowerCAmelCase_       )
          UpperCamelCase    :  Optional[Any]							=	outputs[0].numpy()

          with tempfile.TemporaryDirectory() as tmp_dirname:
             model_a.save_pretrained(lowerCAmelCase_       )
             UpperCamelCase    :  Optional[int]							=	TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase_       )

             UpperCamelCase    :  str							=	model_a(**lowerCAmelCase_       )
             UpperCamelCase    :  str							=	after_outputs[0].numpy()
             UpperCamelCase    :  Optional[int]							=	np.amax(np.abs(out_a - out_a       )       )
             self.assertLessEqual(lowerCAmelCase_,			1e-5       )






@require_tf
class 				lowerCAmelCase_		(  __A       ,    unittest.TestCase							):
       def 					snake_case_       (			self       )     ->		int:
          UpperCamelCase    :  List[str]							=	TFVisionTextDualEncoderModel.from_vision_text_pretrained(
              'hf-internal-testing/tiny-random-vit',			'hf-internal-testing/tiny-random-bert'       )
          UpperCamelCase    :  Optional[int]							=	13
          UpperCamelCase    :  Optional[Any]							=	floats_tensor(
              [
                  batch_size,
                  model.vision_model.config.num_channels,
                  model.vision_model.config.image_size,
                  model.vision_model.config.image_size,
              ]       )
          UpperCamelCase    :  int							=	ids_tensor([batch_size, 4],			model.text_model.config.vocab_size       )
          UpperCamelCase    :  Tuple							=	random_attention_mask([batch_size, 4]       )
          UpperCamelCase    :  Optional[int]							=	{"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		List[Any]:
          UpperCamelCase    :  int							=	TFViTModel(lowerCAmelCase_,			name='vision_model'       )
          UpperCamelCase    :  Optional[Any]							=	TFBertModel(lowerCAmelCase_,			name='text_model'       )
          return vision_model, text_model






       def 					snake_case_       (			self       )     ->		Optional[int]:
          UpperCamelCase    :  List[Any]							=	TFViTModelTester(self       )
          UpperCamelCase    :  List[Any]							=	TFBertModelTester(self       )
          UpperCamelCase    :  Optional[int]							=	vit_model_tester.prepare_config_and_inputs()
          UpperCamelCase    :  Any							=	bert_model_tester.prepare_config_and_inputs()

          UpperCamelCase    :  Tuple							=	vision_config_and_inputs

          (
              UpperCamelCase	
          )    :  List[Any]							=	text_config_and_inputs

          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": input_mask,
              "input_ids": input_ids,
              "text_token_type_ids": token_type_ids,
              "text_sequence_labels": sequence_labels,
              "text_token_labels": token_labels,
              "text_choice_labels": choice_labels,
          }






@require_tf
class 				lowerCAmelCase_		(  __A       ,    unittest.TestCase							):
       def 					snake_case_       (			self       )     ->		Optional[int]:
          # DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's
          # just reinitialize it.
          UpperCamelCase    :  Tuple							=	TFVisionTextDualEncoderModel.from_vision_text_pretrained(
              'Rocketknight1/tiny-random-deit-tf',			'hf-internal-testing/tiny-random-roberta'       )
          UpperCamelCase    :  Any							=	13
          UpperCamelCase    :  List[str]							=	floats_tensor(
              [
                  batch_size,
                  model.vision_model.config.num_channels,
                  model.vision_model.config.image_size,
                  model.vision_model.config.image_size,
              ]       )
          UpperCamelCase    :  Optional[Any]							=	ids_tensor([batch_size, 4],			model.text_model.config.vocab_size       )
          UpperCamelCase    :  Union[str, Any]							=	random_attention_mask([batch_size, 4]       )
          UpperCamelCase    :  List[str]							=	{"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=None,			**SCREAMING_SNAKE_CASE_       )     ->		Union[str, Any]:
          UpperCamelCase    :  Dict							=	self.get_vision_text_model(lowerCAmelCase_,			lowerCAmelCase_       )
          UpperCamelCase    :  Union[str, Any]							=	TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase_,			text_model=lowerCAmelCase_       )

          UpperCamelCase    :  List[Any]							=	model(
              input_ids=lowerCAmelCase_,			pixel_values=lowerCAmelCase_,			attention_mask=lowerCAmelCase_,			output_attentions=lowerCAmelCase_       )

          UpperCamelCase    :  Tuple							=	output.vision_model_output.attentions
          self.assertEqual(len(lowerCAmelCase_       ),			vision_config.num_hidden_layers       )

          # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
          UpperCamelCase    :  List[Any]							=	to_atuple(vision_model.config.image_size       )
          UpperCamelCase    :  Union[str, Any]							=	to_atuple(vision_model.config.patch_size       )
          UpperCamelCase    :  Optional[int]							=	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
          UpperCamelCase    :  str							=	num_patches + 2
          self.assertEqual(vision_attentions[0].shape[-3:],			(vision_config.num_attention_heads, seq_len, seq_len)       )

          UpperCamelCase    :  List[str]							=	output.text_model_output.attentions
          self.assertEqual(len(lowerCAmelCase_       ),			text_config.num_hidden_layers       )

          self.assertEqual(
              text_attentions[0].shape[-3:],			(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]),			)
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Union[str, Any]:
          UpperCamelCase    :  Optional[Any]							=	TFDeiTModel(lowerCAmelCase_,			name='vision_model'       )
          UpperCamelCase    :  str							=	TFRobertaModel(lowerCAmelCase_,			name='text_model'       )
          return vision_model, text_model






       def 					snake_case_       (			self       )     ->		str:
          UpperCamelCase    :  Dict							=	TFDeiTModelTester(self       )
          UpperCamelCase    :  Optional[Any]							=	TFRobertaModelTester(self       )
          UpperCamelCase    :  Union[str, Any]							=	vit_model_tester.prepare_config_and_inputs()
          UpperCamelCase    :  Tuple							=	bert_model_tester.prepare_config_and_inputs()

          UpperCamelCase    :  Dict							=	vision_config_and_inputs

          (
              UpperCamelCase	
          )    :  Optional[int]							=	text_config_and_inputs

          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": input_mask,
              "input_ids": input_ids,
              "text_token_type_ids": token_type_ids,
              "text_sequence_labels": sequence_labels,
              "text_token_labels": token_labels,
              "text_choice_labels": choice_labels,
          }






@require_tf
class 				lowerCAmelCase_		(  __A       ,    unittest.TestCase							):
       def 					snake_case_       (			self       )     ->		Any:
          UpperCamelCase    :  Optional[int]							=	TFVisionTextDualEncoderModel.from_vision_text_pretrained(
              'Rocketknight1/tiny-random-clip-tf',			'hf-internal-testing/tiny-random-bert'       )
          UpperCamelCase    :  List[Any]							=	13
          UpperCamelCase    :  Dict							=	floats_tensor(
              [
                  batch_size,
                  model.vision_model.config.num_channels,
                  model.vision_model.config.image_size,
                  model.vision_model.config.image_size,
              ]       )
          UpperCamelCase    :  Optional[int]							=	ids_tensor([batch_size, 4],			model.text_model.config.vocab_size       )
          UpperCamelCase    :  Optional[Any]							=	random_attention_mask([batch_size, 4]       )
          UpperCamelCase    :  Dict							=	{"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Optional[int]:
          UpperCamelCase    :  Tuple							=	TFCLIPVisionModel(lowerCAmelCase_,			name='vision_model'       )
          UpperCamelCase    :  Optional[int]							=	TFBertModel(lowerCAmelCase_,			name='text_model'       )
          return vision_model, text_model






       def 					snake_case_       (			self       )     ->		Union[str, Any]:
          UpperCamelCase    :  str							=	TFCLIPVisionModelTester(self       )
          UpperCamelCase    :  str							=	TFBertModelTester(self       )
          UpperCamelCase    :  str							=	clip_model_tester.prepare_config_and_inputs()
          UpperCamelCase    :  int							=	bert_model_tester.prepare_config_and_inputs()

          UpperCamelCase    :  List[str]							=	vision_config_and_inputs

          (
              UpperCamelCase	
          )    :  Optional[int]							=	text_config_and_inputs

          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": input_mask,
              "input_ids": input_ids,
              "text_token_type_ids": token_type_ids,
              "text_sequence_labels": sequence_labels,
              "text_token_labels": token_labels,
              "text_choice_labels": choice_labels,
          }






@require_vision
@require_tf
class 				lowerCAmelCase_		(  unittest.TestCase							):
       @slow
       def 					snake_case_       (			self       )     ->		int:
          UpperCamelCase    :  Any							=	TFVisionTextDualEncoderModel.from_pretrained(
              'clip-italian/clip-italian',			logit_scale_init_value=1.0,			from_pt=lowerCAmelCase_       )
          UpperCamelCase    :  Tuple							=	VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'       )

          UpperCamelCase    :  str							=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
          UpperCamelCase    :  Dict							=	processor(
              text=['una foto di un gatto', 'una foto di un cane'],			images=lowerCAmelCase_,			padding=lowerCAmelCase_,			return_tensors='np'       )

          UpperCamelCase    :  Any							=	model(**lowerCAmelCase_       )

          # verify the logits
          self.assertEqual(outputs.logits_per_image.shape,			(inputs.pixel_values.shape[0], inputs.input_ids.shape[0])       )
          self.assertEqual(
              outputs.logits_per_text.shape,			(inputs.input_ids.shape[0], inputs.pixel_values.shape[0]),			)

          UpperCamelCase    :  List[Any]							=	np.array([[1.2_28_47_27, 0.3_10_41_22]]       )

          self.assertTrue(np.allclose(outputs.logits_per_image.numpy(),			lowerCAmelCase_,			atol=1e-3       )       )











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





	def __init__(      self					: str	,	lowerCAmelCase_					: List[Any]	,	lowerCAmelCase_					: List[Any]=13	,	lowerCAmelCase_					: Tuple=7	,	lowerCAmelCase_					: Dict=True	,	lowerCAmelCase_					: str=True	,	lowerCAmelCase_					: List[str]=True	,	lowerCAmelCase_					: List[str]=True	,	lowerCAmelCase_					: List[str]=99	,	lowerCAmelCase_					: int=32	,	lowerCAmelCase_					: List[str]=5	,	lowerCAmelCase_					: Optional[int]=4	,	lowerCAmelCase_					: str=37	,	lowerCAmelCase_					: List[Any]="gelu"	,	lowerCAmelCase_					: Dict=0.1	,	lowerCAmelCase_					: List[str]=0.1	,	lowerCAmelCase_					: List[Any]=512	,	lowerCAmelCase_					: Optional[int]=16	,	lowerCAmelCase_					: Union[str, Any]=2	,	lowerCAmelCase_					: List[str]=0.0_2	,	lowerCAmelCase_					: List[Any]=False	,	lowerCAmelCase_					: Dict=True	,	lowerCAmelCase_					: Union[str, Any]="None"	,	lowerCAmelCase_					: List[Any]=3	,	lowerCAmelCase_					: Optional[int]=4	,	lowerCAmelCase_					: int=None	,	)						->			Dict:
				UpperCAmelCase_     : Dict			     =					parent
				UpperCAmelCase_     : Union[str, Any]			     =					batch_size
				UpperCAmelCase_     : Optional[Any]			     =					seq_length
				UpperCAmelCase_     : List[Any]			     =					is_training
				UpperCAmelCase_     : Optional[int]			     =					use_input_mask
				UpperCAmelCase_     : int			     =					use_token_type_ids
				UpperCAmelCase_     : Any			     =					use_labels
				UpperCAmelCase_     : Optional[int]			     =					vocab_size
				UpperCAmelCase_     : Any			     =					hidden_size
				UpperCAmelCase_     : Dict			     =					num_hidden_layers
				UpperCAmelCase_     : List[Any]			     =					num_attention_heads
				UpperCAmelCase_     : List[Any]			     =					intermediate_size
				UpperCAmelCase_     : int			     =					hidden_act
				UpperCAmelCase_     : Optional[Any]			     =					hidden_dropout_prob
				UpperCAmelCase_     : Union[str, Any]			     =					attention_probs_dropout_prob
				UpperCAmelCase_     : Any			     =					max_position_embeddings
				UpperCAmelCase_     : Union[str, Any]			     =					type_vocab_size
				UpperCAmelCase_     : Union[str, Any]			     =					type_sequence_label_size
				UpperCAmelCase_     : Tuple			     =					initializer_range
				UpperCAmelCase_     : int			     =					num_labels
				UpperCAmelCase_     : Optional[Any]			     =					num_choices
				UpperCAmelCase_     : List[str]			     =					relative_attention
				UpperCAmelCase_     : List[Any]			     =					position_biased_input
				UpperCAmelCase_     : Dict			     =					pos_att_type
				UpperCAmelCase_     : Optional[Any]			     =					scope





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]						)						->			Dict:
				UpperCAmelCase_     : Any			     =					ids_tensor([self.batch_size, self.seq_length]	,	self.vocab_size						)

				UpperCAmelCase_     : Tuple			     =					None
				if self.use_input_mask:
							UpperCAmelCase_     : List[str]			     =					ids_tensor([self.batch_size, self.seq_length]	,	vocab_size=2						)

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

				UpperCAmelCase_     : Optional[Any]			     =					None
				UpperCAmelCase_     : List[str]			     =					None
				UpperCAmelCase_     : Union[str, Any]			     =					None
				if self.use_labels:
							UpperCAmelCase_     : Dict			     =					ids_tensor([self.batch_size]	,	self.type_sequence_label_size						)
							UpperCAmelCase_     : Union[str, Any]			     =					ids_tensor([self.batch_size, self.seq_length]	,	self.num_labels						)
							UpperCAmelCase_     : Tuple			     =					ids_tensor([self.batch_size]	,	self.num_choices						)

				UpperCAmelCase_     : int			     =					self.get_config()

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





	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Dict:
				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      _SCREAMING_SNAKE_CASE      (      self					: Optional[int]						)						->			Union[str, Any]:
				UpperCAmelCase_     : List[str]			     =					self.get_config()
				UpperCAmelCase_     : int			     =					300
				return config





	def      _SCREAMING_SNAKE_CASE      (      self					: Any	,	lowerCAmelCase_					: int						)						->			List[Any]:
				self.parent.assertListEqual(list(result.loss.size()						)	,	[]						)





	def      _SCREAMING_SNAKE_CASE      (      self					: int	,	lowerCAmelCase_					: List[Any]	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: Any	,	lowerCAmelCase_					: Any	,	lowerCAmelCase_					: List[str]	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: List[Any]						)						->			List[Any]:
				UpperCAmelCase_     : Optional[Any]			     =					DebertaModel(config=lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : Optional[Any]			     =					model(lowerCAmelCase_	,	attention_mask=lowerCAmelCase_	,	token_type_ids=lowerCAmelCase_						)[0]
				UpperCAmelCase_     : Optional[int]			     =					model(lowerCAmelCase_	,	token_type_ids=lowerCAmelCase_						)[0]
				UpperCAmelCase_     : Optional[Any]			     =					model(lowerCAmelCase_						)[0]

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





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[int]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: List[Any]	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: Optional[int]						)						->			List[Any]:
				UpperCAmelCase_     : Union[str, Any]			     =					DebertaForMaskedLM(config=lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : List[Any]			     =					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      _SCREAMING_SNAKE_CASE      (      self					: Any	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: Optional[Any]	,	lowerCAmelCase_					: Any	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Optional[Any]						)						->			Optional[Any]:
				UpperCAmelCase_     : Any			     =					self.num_labels
				UpperCAmelCase_     : List[Any]			     =					DebertaForSequenceClassification(lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : Optional[Any]			     =					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      _SCREAMING_SNAKE_CASE      (      self					: List[Any]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: Optional[Any]	,	lowerCAmelCase_					: Union[str, Any]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Tuple						)						->			str:
				UpperCAmelCase_     : Optional[int]			     =					self.num_labels
				UpperCAmelCase_     : Optional[int]			     =					DebertaForTokenClassification(config=lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : Optional[Any]			     =					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      _SCREAMING_SNAKE_CASE      (      self					: Tuple	,	lowerCAmelCase_					: List[Any]	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: List[Any]	,	lowerCAmelCase_					: Union[str, Any]	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: str						)						->			List[Any]:
				UpperCAmelCase_     : Dict			     =					DebertaForQuestionAnswering(config=lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : Any			     =					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      _SCREAMING_SNAKE_CASE      (      self					: Optional[int]						)						->			Union[str, Any]:
				UpperCAmelCase_     : Union[str, Any]			     =					self.prepare_config_and_inputs()
				(
				    (
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	
				)     : Tuple			     =					config_and_inputs
				UpperCAmelCase_     : List[Any]			     =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
				return config, inputs_dict







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

	__magic_name__	      =				True
	__magic_name__	      =				False
	__magic_name__	      =				False
	__magic_name__	      =				False
	__magic_name__	      =				False





	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			int:
				UpperCAmelCase_     : int			     =					DebertaModelTester(self						)
				UpperCAmelCase_     : Any			     =					ConfigTester(self	,	config_class=lowerCAmelCase_	,	hidden_size=37						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			Optional[Any]:
				self.config_tester.run_common_tests()





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[int]						)						->			Optional[Any]:
				UpperCAmelCase_     : Union[str, Any]			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_model(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]						)						->			Union[str, Any]:
				UpperCAmelCase_     : int			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]						)						->			Optional[int]:
				UpperCAmelCase_     : Any			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			Union[str, Any]:
				UpperCAmelCase_     : Tuple			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]						)						->			Union[str, Any]:
				UpperCAmelCase_     : Any			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase_						)





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[int]						)						->			Tuple:
				for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_     : Optional[int]			     =					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      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			Optional[Any]:
				pass





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			List[Any]:
				UpperCAmelCase_     : Optional[int]			     =					DebertaModel.from_pretrained("microsoft/deberta-base"						)

				UpperCAmelCase_     : List[Any]			     =					torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]						)
				UpperCAmelCase_     : Tuple			     =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
				with torch.no_grad():
							UpperCAmelCase_     : Optional[Any]			     =					model(lowerCAmelCase_	,	attention_mask=lowerCAmelCase_						)[0]
				# compare the actual values for a slice.
				UpperCAmelCase_     : Tuple			     =					torch.tensor(
				    [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]						)
				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,	lowerCAmelCase_	,	atol=1e-4						)	,	f"""{output[:, 1:4, 1:4]}"""						)



