
'''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 			lowerCAmelCase__ :



  '''simple docstring'''






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

  def 		_lowerCamelCase						(self	:			Optional[int]     ) ->							List[str]:
        snake_case_    							=				ids_tensor([self.batch_size, self.seq_length]						,			self.vocab_size     )

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

        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_    							=				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 		_lowerCamelCase						(self	:			Optional[int]						,			lowerCAmelCase_	:			List[Any]						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			Dict						,			lowerCAmelCase_	:			str						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			Union[str, Any]     ) ->							int:
        snake_case_    							=				TFDebertaModel(config=lowerCAmelCase_     )
        snake_case_    							=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

        snake_case_    							=				[input_ids, input_mask]
        snake_case_    							=				model(lowerCAmelCase_     )

        snake_case_    							=				model(lowerCAmelCase_     )

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

  def 		_lowerCamelCase						(self	:			str						,			lowerCAmelCase_	:			Any						,			lowerCAmelCase_	:			Tuple						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			str						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			Optional[int]						,			lowerCAmelCase_	:			List[str]     ) ->							Optional[Any]:
        snake_case_    							=				TFDebertaForMaskedLM(config=lowerCAmelCase_     )
        snake_case_    							=				{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        snake_case_    							=				model(lowerCAmelCase_     )
        self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.vocab_size)     )

  def 		_lowerCamelCase						(self	:			Tuple						,			lowerCAmelCase_	:			Tuple						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			Optional[int]						,			lowerCAmelCase_	:			str						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			List[Any]						,			lowerCAmelCase_	:			int     ) ->							Optional[Any]:
        snake_case_    							=				self.num_labels
        snake_case_    							=				TFDebertaForSequenceClassification(config=lowerCAmelCase_     )
        snake_case_    							=				{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

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

  def 		_lowerCamelCase						(self	:			Tuple						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			Dict						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			int						,			lowerCAmelCase_	:			Dict						,			lowerCAmelCase_	:			Optional[int]     ) ->							Union[str, Any]:
        snake_case_    							=				self.num_labels
        snake_case_    							=				TFDebertaForTokenClassification(config=lowerCAmelCase_     )
        snake_case_    							=				{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        snake_case_    							=				model(lowerCAmelCase_     )
        self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.num_labels)     )

  def 		_lowerCamelCase						(self	:			int						,			lowerCAmelCase_	:			Any						,			lowerCAmelCase_	:			Any						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			Tuple						,			lowerCAmelCase_	:			str						,			lowerCAmelCase_	:			Optional[int]						,			lowerCAmelCase_	:			Optional[Any]     ) ->							Optional[int]:
        snake_case_    							=				TFDebertaForQuestionAnswering(config=lowerCAmelCase_     )
        snake_case_    							=				{
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        snake_case_    							=				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 		_lowerCamelCase						(self	:			int     ) ->							Any:
        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_tf
class 			lowerCAmelCase__ (							UpperCamelCase       ,				UpperCamelCase       ,				unittest.TestCase							):



  '''simple docstring'''






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

  _UpperCamelCase							=			False
  _UpperCamelCase							=			False

  def 		_lowerCamelCase						(self	:			Optional[int]     ) ->							Optional[int]:
        snake_case_    							=				TFDebertaModelTester(self     )
        snake_case_    							=				ConfigTester(self						,			config_class=lowerCAmelCase_						,			hidden_size=37     )

  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							List[str]:
        self.config_tester.run_common_tests()

  def 		_lowerCamelCase						(self	:			int     ) ->							List[Any]:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Optional[int]     ) ->							List[Any]:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			int     ) ->							Tuple:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			str     ) ->							List[Any]:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Tuple     ) ->							List[str]:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_     )


  @slow
  def 		_lowerCamelCase						(self	:			List[str]     ) ->							Optional[int]:
        snake_case_    							=				TFDebertaModel.from_pretrained("kamalkraj/deberta-base"     )
        self.assertIsNotNone(lowerCAmelCase_     )



@require_tf
class 			lowerCAmelCase__ (							unittest.TestCase							):



  '''simple docstring'''






  @unittest.skip(reason="Model not available yet"     )
  def 		_lowerCamelCase						(self	:			List[Any]     ) ->							Optional[Any]:
        pass


  @slow
  def 		_lowerCamelCase						(self	:			Union[str, Any]     ) ->							int:
        snake_case_    							=				TFDebertaModel.from_pretrained("kamalkraj/deberta-base"     )
        snake_case_    							=				tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]     )
        snake_case_    							=				tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
        snake_case_    							=				model(lowerCAmelCase_						,			attention_mask=lowerCAmelCase_     )[0]

        snake_case_    							=				tf.constant(
            [
                [
                    [-0.59_855_896, -0.80_552_566, -0.8_462_135],
                    [1.4_484_025, -0.93_483_794, -0.80_593_085],
                    [0.3_122_741, 0.00_316_059, -1.4_131_377],
                ]
            ]     )
        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 EfficientNetImageProcessor
class  _SCREAMING_SNAKE_CASE				(		unittest.TestCase						):


			'''simple docstring'''





			def __init__( self				:    List[str]			,   lowercase				:    Optional[Any]			,   lowercase				:    int=1_3			,   lowercase				:    List[Any]=3			,   lowercase				:    int=1_8			,   lowercase				:    List[Any]=3_0			,   lowercase				:    Optional[int]=4_0_0			,   lowercase				:    Any=True			,   lowercase				:    int=None			,   lowercase				:    Any=True			,   lowercase				:    Optional[int]=[0.5, 0.5, 0.5]			,   lowercase				:    List[Any]=[0.5, 0.5, 0.5]			,   )				->     List[Any]:

									'''simple docstring'''







									UpperCamelCase__       =			size if size is not None else {"""height""": 1_8, """width""": 1_8}
									UpperCamelCase__       =			parent
									UpperCamelCase__       =			batch_size
									UpperCamelCase__       =			num_channels
									UpperCamelCase__       =			image_size
									UpperCamelCase__       =			min_resolution
									UpperCamelCase__       =			max_resolution
									UpperCamelCase__       =			do_resize
									UpperCamelCase__       =			size
									UpperCamelCase__       =			do_normalize
									UpperCamelCase__       =			image_mean
									UpperCamelCase__       =			image_std







			def 					A   ( self				:    Optional[Any]      )				->     Any:

									'''simple docstring'''







									return {
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_normalize": self.do_normalize,
									    "do_resize": self.do_resize,
									    "size": self.size,
									}
@require_torch
@require_vision
class  _SCREAMING_SNAKE_CASE				(		SCREAMING_SNAKE_CASE							,unittest.TestCase						):


			'''simple docstring'''





			__a				:				List[Any]         =       EfficientNetImageProcessor if is_vision_available() else None






			def 					A   ( self				:    Optional[Any]      )				->     str:

									'''simple docstring'''







									UpperCamelCase__       =			EfficientNetImageProcessorTester(self      )






			@property
			def 					A   ( self				:    Optional[int]      )				->     Optional[Any]:

									'''simple docstring'''







									return self.image_processor_tester.prepare_image_processor_dict()






			def 					A   ( self				:    Dict      )				->     str:

									'''simple docstring'''







									UpperCamelCase__       =			self.image_processing_class(**self.image_processor_dict      )
									self.assertTrue(hasattr(lowercase			,   """image_mean"""      )      )
									self.assertTrue(hasattr(lowercase			,   """image_std"""      )      )
									self.assertTrue(hasattr(lowercase			,   """do_normalize"""      )      )
									self.assertTrue(hasattr(lowercase			,   """do_resize"""      )      )
									self.assertTrue(hasattr(lowercase			,   """size"""      )      )






			def 					A   ( self				:    List[str]      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__       =			self.image_processing_class.from_dict(self.image_processor_dict      )
									self.assertEqual(image_processor.size			,   {"""height""": 1_8, """width""": 1_8}      )

									UpperCamelCase__       =			self.image_processing_class.from_dict(self.image_processor_dict			,   size=4_2      )
									self.assertEqual(image_processor.size			,   {"""height""": 4_2, """width""": 4_2}      )






			def 					A   ( self				:    Union[str, Any]      )				->     str:

									'''simple docstring'''







									UpperCamelCase__       =			self.image_processing_class(**self.image_processor_dict      )
									# create random PIL images
									UpperCamelCase__       =			prepare_image_inputs(self.image_processor_tester			,   equal_resolution=lowercase      )
									for image in image_inputs:
															self.assertIsInstance(lowercase			,   Image.Image      )

									# Test not batched input
									UpperCamelCase__       =			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.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )			,   )

									# Test batched
									UpperCamelCase__       =			image_processing(lowercase			,   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.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )			,   )






			def 					A   ( self				:    Dict      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__       =			self.image_processing_class(**self.image_processor_dict      )
									# create random numpy tensors
									UpperCamelCase__       =			prepare_image_inputs(self.image_processor_tester			,   equal_resolution=lowercase			,   numpify=lowercase      )
									for image in image_inputs:
															self.assertIsInstance(lowercase			,   np.ndarray      )

									# Test not batched input
									UpperCamelCase__       =			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.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )			,   )

									# Test batched
									UpperCamelCase__       =			image_processing(lowercase			,   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.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )			,   )







			def 					A   ( self				:    Optional[int]      )				->     List[str]:

									'''simple docstring'''







									UpperCamelCase__       =			self.image_processing_class(**self.image_processor_dict      )
									# create random PyTorch tensors
									UpperCamelCase__       =			prepare_image_inputs(self.image_processor_tester			,   equal_resolution=lowercase			,   torchify=lowercase      )
									for image in image_inputs:
															self.assertIsInstance(lowercase			,   torch.Tensor      )

									# Test not batched input
									UpperCamelCase__       =			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.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )			,   )

									# Test batched
									UpperCamelCase__       =			image_processing(lowercase			,   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.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )			,   )
