




"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    TFDebertaForMaskedLM,
		    TFDebertaForQuestionAnswering,
		    TFDebertaForSequenceClassification,
		    TFDebertaForTokenClassification,
		    TFDebertaModel,
		)






class  SCREAMING_SNAKE_CASE__   :






						"""simple docstring"""




						def __init__(							self			:     Union[str, Any]  ,  lowerCAmelCase			:     List[str]  ,  lowerCAmelCase			:     str=13  ,  lowerCAmelCase			:     List[str]=7  ,  lowerCAmelCase			:     Union[str, Any]=True  ,  lowerCAmelCase			:     Optional[int]=True  ,  lowerCAmelCase			:     Optional[Any]=True  ,  lowerCAmelCase			:     Any=True  ,  lowerCAmelCase			:     List[Any]=99  ,  lowerCAmelCase			:     Optional[Any]=32  ,  lowerCAmelCase			:     Tuple=2  ,  lowerCAmelCase			:     int=4  ,  lowerCAmelCase			:     Optional[int]=37  ,  lowerCAmelCase			:     Dict="gelu"  ,  lowerCAmelCase			:     str=0.1  ,  lowerCAmelCase			:     Union[str, Any]=0.1  ,  lowerCAmelCase			:     List[str]=512  ,  lowerCAmelCase			:     Tuple=16  ,  lowerCAmelCase			:     int=2  ,  lowerCAmelCase			:     Dict=0.02  ,  lowerCAmelCase			:     Union[str, Any]=3  ,  lowerCAmelCase			:     Optional[Any]=4  ,  lowerCAmelCase			:     Any=None  ,  ):
										lowerCAmelCase		=			parent
										lowerCAmelCase		=			13
										lowerCAmelCase		=			7
										lowerCAmelCase		=			True
										lowerCAmelCase		=			True
										lowerCAmelCase		=			True
										lowerCAmelCase		=			True
										lowerCAmelCase		=			99
										lowerCAmelCase		=			32
										lowerCAmelCase		=			2
										lowerCAmelCase		=			4
										lowerCAmelCase		=			37
										lowerCAmelCase		=			"""gelu"""
										lowerCAmelCase		=			0.1
										lowerCAmelCase		=			0.1
										lowerCAmelCase		=			512
										lowerCAmelCase		=			16
										lowerCAmelCase		=			False
										lowerCAmelCase		=			-1
										lowerCAmelCase		=			True
										lowerCAmelCase		=			2
										lowerCAmelCase		=			0.02
										lowerCAmelCase		=			3
										lowerCAmelCase		=			4
										lowerCAmelCase		=			None


						def 					__lowercase	(							self			:     Optional[int]   ):
										lowerCAmelCase		=			ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size   )

										lowerCAmelCase		=			None
										if self.use_input_mask:
														lowerCAmelCase		=			random_attention_mask([self.batch_size, self.seq_length]   )

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

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

										lowerCAmelCase		=			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  ,  relative_attention=self.relative_attention  ,  max_relative_positions=self.max_relative_positions  ,  position_biased_input=self.position_biased_input  ,  initializer_range=self.initializer_range  ,  return_dict=lowerCAmelCase  ,  )

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


						def 					__lowercase	(							self			:     Tuple  ,  lowerCAmelCase			:     Tuple  ,  lowerCAmelCase			:     Any  ,  lowerCAmelCase			:     int  ,  lowerCAmelCase			:     Optional[Any]  ,  lowerCAmelCase			:     Dict  ,  lowerCAmelCase			:     List[str]  ,  lowerCAmelCase			:     Optional[int]   ):
										lowerCAmelCase		=			TFDebertaModel(config=lowerCAmelCase   )
										lowerCAmelCase		=			{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

										lowerCAmelCase		=			[input_ids, input_mask]
										lowerCAmelCase		=			model(lowerCAmelCase   )

										lowerCAmelCase		=			model(lowerCAmelCase   )

										self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)   )


						def 					__lowercase	(							self			:     Optional[int]  ,  lowerCAmelCase			:     Dict  ,  lowerCAmelCase			:     Optional[Any]  ,  lowerCAmelCase			:     Optional[int]  ,  lowerCAmelCase			:     Optional[Any]  ,  lowerCAmelCase			:     List[Any]  ,  lowerCAmelCase			:     Tuple  ,  lowerCAmelCase			:     Any   ):
										lowerCAmelCase		=			TFDebertaForMaskedLM(config=lowerCAmelCase   )
										lowerCAmelCase		=			{
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}
										lowerCAmelCase		=			model(lowerCAmelCase   )
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)   )


						def 					__lowercase	(							self			:     str  ,  lowerCAmelCase			:     str  ,  lowerCAmelCase			:     int  ,  lowerCAmelCase			:     str  ,  lowerCAmelCase			:     int  ,  lowerCAmelCase			:     List[str]  ,  lowerCAmelCase			:     List[str]  ,  lowerCAmelCase			:     List[str]   ):
										lowerCAmelCase		=			self.num_labels
										lowerCAmelCase		=			TFDebertaForSequenceClassification(config=lowerCAmelCase   )
										lowerCAmelCase		=			{
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}

										lowerCAmelCase		=			model(lowerCAmelCase   )
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_labels)   )


						def 					__lowercase	(							self			:     str  ,  lowerCAmelCase			:     Optional[Any]  ,  lowerCAmelCase			:     Optional[int]  ,  lowerCAmelCase			:     List[str]  ,  lowerCAmelCase			:     Any  ,  lowerCAmelCase			:     List[Any]  ,  lowerCAmelCase			:     Union[str, Any]  ,  lowerCAmelCase			:     Dict   ):
										lowerCAmelCase		=			self.num_labels
										lowerCAmelCase		=			TFDebertaForTokenClassification(config=lowerCAmelCase   )
										lowerCAmelCase		=			{
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}
										lowerCAmelCase		=			model(lowerCAmelCase   )
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.num_labels)   )


						def 					__lowercase	(							self			:     Optional[int]  ,  lowerCAmelCase			:     List[Any]  ,  lowerCAmelCase			:     List[Any]  ,  lowerCAmelCase			:     str  ,  lowerCAmelCase			:     Optional[int]  ,  lowerCAmelCase			:     str  ,  lowerCAmelCase			:     Optional[int]  ,  lowerCAmelCase			:     Dict   ):
										lowerCAmelCase		=			TFDebertaForQuestionAnswering(config=lowerCAmelCase   )
										lowerCAmelCase		=			{
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}

										lowerCAmelCase		=			model(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 					__lowercase	(							self			:     Union[str, Any]   ):
										lowerCAmelCase		=			self.prepare_config_and_inputs()
										(
										    (
										    lowerCAmelCase  
										)	,  (
										    lowerCAmelCase  
										)	,  (
										    lowerCAmelCase  
										)	,  (
										    lowerCAmelCase  
										)	,  (
										    lowerCAmelCase  
										)	,  (
										    lowerCAmelCase  
										)	,  (
										    lowerCAmelCase  
										)	,    
										)		=			config_and_inputs
										lowerCAmelCase		=			{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
										return config, inputs_dict






@require_tf
class  SCREAMING_SNAKE_CASE__   (   _a  ,			_a  ,			unittest.TestCase   ):






						"""simple docstring"""




						_a        =  (
						    (
						        TFDebertaModel,
						        TFDebertaForMaskedLM,
						        TFDebertaForQuestionAnswering,
						        TFDebertaForSequenceClassification,
						        TFDebertaForTokenClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						_a        =  (
						    {
						        'feature-extraction': TFDebertaModel,
						        'fill-mask': TFDebertaForMaskedLM,
						        'question-answering': TFDebertaForQuestionAnswering,
						        'text-classification': TFDebertaForSequenceClassification,
						        'token-classification': TFDebertaForTokenClassification,
						        'zero-shot': TFDebertaForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)

						_a        =  False
						_a        =  False


						def 					__lowercase	(							self			:     Optional[int]   ):
										lowerCAmelCase		=			TFDebertaModelTester(self   )
										lowerCAmelCase		=			ConfigTester(self  ,  config_class=lowerCAmelCase  ,  hidden_size=37   )


						def 					__lowercase	(							self			:     str   ):
										self.config_tester.run_common_tests()


						def 					__lowercase	(							self			:     Optional[Any]   ):
										lowerCAmelCase		=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase   )


						def 					__lowercase	(							self			:     Any   ):
										lowerCAmelCase		=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase   )


						def 					__lowercase	(							self			:     str   ):
										lowerCAmelCase		=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase   )


						def 					__lowercase	(							self			:     List[str]   ):
										lowerCAmelCase		=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase   )


						def 					__lowercase	(							self			:     Optional[int]   ):
										lowerCAmelCase		=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase   )




						@slow
						def 					__lowercase	(							self			:     Optional[Any]   ):
										lowerCAmelCase		=			TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""   )
										self.assertIsNotNone(lowerCAmelCase   )






@require_tf
class  SCREAMING_SNAKE_CASE__   (   unittest.TestCase   ):






						"""simple docstring"""




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




						@slow
						def 					__lowercase	(							self			:     Union[str, Any]   ):
										lowerCAmelCase		=			TFDebertaModel.from_pretrained("""kamalkraj/deberta-base"""   )
										lowerCAmelCase		=			tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]   )
										lowerCAmelCase		=			tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
										lowerCAmelCase		=			model(lowerCAmelCase  ,  attention_mask=lowerCAmelCase   )[0]

										lowerCAmelCase		=			tf.constant(
										    [
										        [
										            [-0.5985_5896, -0.8055_2566, -0.846_2135],
										            [1.448_4025, -0.9348_3794, -0.8059_3085],
										            [0.312_2741, 0.0031_6059, -1.413_1377],
										        ]
										    ]   )
										tf.debugging.assert_near(output[:, 1:4, 1:4]  ,  lowerCAmelCase  ,  atol=1e-4   )











"""simple docstring"""







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import OwlViTImageProcessor






class  SCREAMING_SNAKE_CASE__   (   unittest.TestCase   ):


						def __init__(							self			:     int  ,  lowerCAmelCase			:     Optional[int]  ,  lowerCAmelCase			:     Optional[Any]=7  ,  lowerCAmelCase			:     str=3  ,  lowerCAmelCase			:     str=18  ,  lowerCAmelCase			:     Tuple=30  ,  lowerCAmelCase			:     Union[str, Any]=400  ,  lowerCAmelCase			:     List[str]=True  ,  lowerCAmelCase			:     List[str]=None  ,  lowerCAmelCase			:     int=True  ,  lowerCAmelCase			:     int=None  ,  lowerCAmelCase			:     Optional[int]=True  ,  lowerCAmelCase			:     Dict=[0.4814_5466, 0.457_8275, 0.4082_1073]  ,  lowerCAmelCase			:     str=[0.2686_2954, 0.2613_0258, 0.2757_7711]  ,  lowerCAmelCase			:     Any=True  ,  ):
										lowerCAmelCase		=			parent
										lowerCAmelCase		=			batch_size
										lowerCAmelCase		=			num_channels
										lowerCAmelCase		=			image_size
										lowerCAmelCase		=			min_resolution
										lowerCAmelCase		=			max_resolution
										lowerCAmelCase		=			do_resize
										lowerCAmelCase		=			size if size is not None else {"""height""": 18, """width""": 18}
										lowerCAmelCase		=			do_center_crop
										lowerCAmelCase		=			crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
										lowerCAmelCase		=			do_normalize
										lowerCAmelCase		=			image_mean
										lowerCAmelCase		=			image_std
										lowerCAmelCase		=			do_convert_rgb




						def 					__lowercase	(							self			:     Tuple   ):
										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_center_crop": self.do_center_crop,
										    "crop_size": self.crop_size,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_convert_rgb": self.do_convert_rgb,
										}






@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE__   (   _a  ,			unittest.TestCase   ):
						_a        =  OwlViTImageProcessor if is_vision_available() else None


						def 					__lowercase	(							self			:     Union[str, Any]   ):
										lowerCAmelCase		=			OwlViTImageProcessingTester(self   )


						@property
						def 					__lowercase	(							self			:     List[Any]   ):
										return self.image_processor_tester.prepare_image_processor_dict()


						def 					__lowercase	(							self			:     List[str]   ):
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_resize"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """size"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_center_crop"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """center_crop"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_normalize"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """image_mean"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """image_std"""   )   )
										self.assertTrue(hasattr(lowerCAmelCase  ,  """do_convert_rgb"""   )   )


						def 					__lowercase	(							self			:     List[Any]   ):
										lowerCAmelCase		=			self.image_processing_class.from_dict(self.image_processor_dict   )
										self.assertEqual(image_processor.size  ,  {"""height""": 18, """width""": 18}   )
										self.assertEqual(image_processor.crop_size  ,  {"""height""": 18, """width""": 18}   )

										lowerCAmelCase		=			self.image_processing_class.from_dict(self.image_processor_dict  ,  size=42  ,  crop_size=84   )
										self.assertEqual(image_processor.size  ,  {"""height""": 42, """width""": 42}   )
										self.assertEqual(image_processor.crop_size  ,  {"""height""": 84, """width""": 84}   )


						def 					__lowercase	(							self			:     str   ):
										# Initialize image_processing
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										# create random PIL images
										lowerCAmelCase		=			prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=lowerCAmelCase   )

										for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase  ,  Image.Image   )

										# Test not batched input
										lowerCAmelCase		=			image_processing(image_inputs[0]  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )

										# Test batched
										lowerCAmelCase		=			image_processing(lowerCAmelCase  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )


						def 					__lowercase	(							self			:     Tuple   ):
										# Initialize image_processing
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										# create random numpy tensors
										lowerCAmelCase		=			prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=lowerCAmelCase  ,  numpify=lowerCAmelCase   )
										for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase  ,  np.ndarray   )

										# Test not batched input
										lowerCAmelCase		=			image_processing(image_inputs[0]  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )

										# Test batched
										lowerCAmelCase		=			image_processing(lowerCAmelCase  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )




						def 					__lowercase	(							self			:     Optional[int]   ):
										# Initialize image_processing
										lowerCAmelCase		=			self.image_processing_class(**self.image_processor_dict   )
										# create random PyTorch tensors
										lowerCAmelCase		=			prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=lowerCAmelCase  ,  torchify=lowerCAmelCase   )
										for image in image_inputs:
														self.assertIsInstance(lowerCAmelCase  ,  torch.Tensor   )

										# Test not batched input
										lowerCAmelCase		=			image_processing(image_inputs[0]  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )

										# Test batched
										lowerCAmelCase		=			image_processing(lowerCAmelCase  ,  return_tensors="""pt"""   ).pixel_values
										self.assertEqual(
										    encoded_images.shape  ,  (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )  ,  )





