

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






     '''simple docstring'''



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


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

      UpperCAmelCase	:     Optional[int]						 =			None
      if self.use_input_mask:
       UpperCAmelCase	:     str						 =			ids_tensor([self.batch_size, self.seq_length]					,      vocab_size=2	)

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

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

      UpperCAmelCase	:     Dict						 =			self.get_config()

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


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


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


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

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


     def        UpperCAmelCase_	(		self			:					str					,      lowercase_			:					List[str]					,      lowercase_			:					str					,      lowercase_			:					Dict					,      lowercase_			:					Tuple					,      lowercase_			:					Tuple					,      lowercase_			:					Optional[int]					,      lowercase_			:					int	)						->     Optional[Any]:
      UpperCAmelCase	:     Union[str, Any]						 =			DebertaForMaskedLM(config=_lowerCamelCase	)
      model.to(_lowerCamelCase	)
      model.eval()
      UpperCAmelCase	:     List[str]						 =			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        UpperCAmelCase_	(		self			:					Tuple					,      lowercase_			:					Dict					,      lowercase_			:					int					,      lowercase_			:					Any					,      lowercase_			:					List[Any]					,      lowercase_			:					List[Any]					,      lowercase_			:					List[Any]					,      lowercase_			:					int	)						->     List[Any]:
      UpperCAmelCase	:     Optional[Any]						 =			self.num_labels
      UpperCAmelCase	:     str						 =			DebertaForSequenceClassification(_lowerCamelCase	)
      model.to(_lowerCamelCase	)
      model.eval()
      UpperCAmelCase	:     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        UpperCAmelCase_	(		self			:					Dict					,      lowercase_			:					Dict					,      lowercase_			:					Optional[int]					,      lowercase_			:					Any					,      lowercase_			:					Optional[Any]					,      lowercase_			:					List[Any]					,      lowercase_			:					str					,      lowercase_			:					Optional[int]	)						->     List[Any]:
      UpperCAmelCase	:     List[Any]						 =			self.num_labels
      UpperCAmelCase	:     Optional[int]						 =			DebertaForTokenClassification(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.num_labels)	)


     def        UpperCAmelCase_	(		self			:					Tuple					,      lowercase_			:					int					,      lowercase_			:					List[Any]					,      lowercase_			:					int					,      lowercase_			:					str					,      lowercase_			:					Tuple					,      lowercase_			:					Optional[int]					,      lowercase_			:					str	)						->     Optional[int]:
      UpperCAmelCase	:     str						 =			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        UpperCAmelCase_	(		self			:					Tuple	)						->     Optional[int]:
      UpperCAmelCase	:     Tuple						 =			self.prepare_config_and_inputs()
      (
          (
          UpperCAmelCase 
      ) ,			(
          UpperCAmelCase 
      ) ,			(
          UpperCAmelCase 
      ) ,			(
          UpperCAmelCase 
      ) ,			(
          UpperCAmelCase 
      ) ,			(
          UpperCAmelCase 
      ) ,			(
          UpperCAmelCase 
      ) ,			 
      )	:     List[Any]						 =			config_and_inputs
      UpperCAmelCase	:     Dict						 =			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict




@require_torch
class 	A_			(    A_	,       A_	,       unittest.TestCase							):






     '''simple docstring'''



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

     UpperCAmelCase_						:     Dict  		=  True
     UpperCAmelCase_						:     Optional[Any]  		=  False
     UpperCAmelCase_						:     List[str]  		=  False
     UpperCAmelCase_						:     int  		=  False
     UpperCAmelCase_						:     Optional[int]  		=  False


     def        UpperCAmelCase_	(		self			:					Tuple	)						->     List[Any]:
      UpperCAmelCase	:     List[str]						 =			DebertaModelTester(self	)
      UpperCAmelCase	:     List[Any]						 =			ConfigTester(self					,      config_class=_lowerCamelCase					,      hidden_size=37	)


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


     def        UpperCAmelCase_	(		self			:					Union[str, Any]	)						->     Optional[Any]:
      UpperCAmelCase	:     Dict						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_model(*_lowerCamelCase	)


     def        UpperCAmelCase_	(		self			:					Tuple	)						->     Union[str, Any]:
      UpperCAmelCase	:     Optional[Any]						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCamelCase	)


     def        UpperCAmelCase_	(		self			:					Dict	)						->     Tuple:
      UpperCAmelCase	:     Union[str, Any]						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCamelCase	)


     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     Optional[Any]:
      UpperCAmelCase	:     Optional[int]						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCamelCase	)


     def        UpperCAmelCase_	(		self			:					str	)						->     Any:
      UpperCAmelCase	:     Tuple						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCamelCase	)




     @slow
     def        UpperCAmelCase_	(		self			:					Union[str, Any]	)						->     Optional[int]:
      for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:     Union[str, Any]						 =			DebertaModel.from_pretrained(_lowerCamelCase	)
       self.assertIsNotNone(_lowerCamelCase	)




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






     '''simple docstring'''



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




     @slow
     def        UpperCAmelCase_	(		self			:					int	)						->     str:
      UpperCAmelCase	:     Dict						 =			DebertaModel.from_pretrained('microsoft/deberta-base'	)

      UpperCAmelCase	:     Optional[int]						 =			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	:     str						 =			model(_lowerCamelCase					,      attention_mask=_lowerCamelCase	)[0]
      # compare the actual values for a slice.
      UpperCAmelCase	:     Tuple						 =			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]}"""	)









'''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 PoolFormerImageProcessor




class 	A_			(    unittest.TestCase							):






     '''simple docstring'''



     def __init__(		self			:					Any					,      lowercase_			:					str					,      lowercase_			:					Union[str, Any]=7					,      lowercase_			:					Union[str, Any]=3					,      lowercase_			:					int=30					,      lowercase_			:					Tuple=400					,      lowercase_			:					Tuple=True					,      lowercase_			:					Optional[int]=None					,      lowercase_			:					List[str]=0.9					,      lowercase_			:					Tuple=None					,      lowercase_			:					Union[str, Any]=True					,      lowercase_			:					int=[0.5, 0.5, 0.5]					,      lowercase_			:					List[str]=[0.5, 0.5, 0.5]					,      )						->     Tuple:
      UpperCAmelCase	:     Optional[int]						 =			size if size is not None else {'shortest_edge': 30}
      UpperCAmelCase	:     int						 =			crop_size if crop_size is not None else {'height': 30, 'width': 30}
      UpperCAmelCase	:     Tuple						 =			parent
      UpperCAmelCase	:     Optional[Any]						 =			batch_size
      UpperCAmelCase	:     int						 =			num_channels
      UpperCAmelCase	:     int						 =			min_resolution
      UpperCAmelCase	:     Optional[int]						 =			max_resolution
      UpperCAmelCase	:     str						 =			do_resize_and_center_crop
      UpperCAmelCase	:     int						 =			size
      UpperCAmelCase	:     Dict						 =			crop_pct
      UpperCAmelCase	:     Union[str, Any]						 =			crop_size
      UpperCAmelCase	:     Optional[int]						 =			do_normalize
      UpperCAmelCase	:     Optional[Any]						 =			image_mean
      UpperCAmelCase	:     Optional[Any]						 =			image_std




     def        UpperCAmelCase_	(		self			:					str	)						->     int:
      return {
          "size": self.size,
          "do_resize_and_center_crop": self.do_resize_and_center_crop,
          "crop_pct": self.crop_pct,
          "crop_size": self.crop_size,
          "do_normalize": self.do_normalize,
          "image_mean": self.image_mean,
          "image_std": self.image_std,
      }




@require_torch
@require_vision
class 	A_			(    _snake_case	,       unittest.TestCase							):






     '''simple docstring'''



     UpperCAmelCase_						:     Tuple  		=  PoolFormerImageProcessor if is_vision_available() else None


     def        UpperCAmelCase_	(		self			:					Dict	)						->     str:
      UpperCAmelCase	:     Any						 =			PoolFormerImageProcessingTester(self	)


     @property
     def        UpperCAmelCase_	(		self			:					List[Any]	)						->     Union[str, Any]:
      return self.image_processor_tester.prepare_image_processor_dict()


     def        UpperCAmelCase_	(		self			:					Tuple	)						->     str:
      UpperCAmelCase	:     Any						 =			self.image_processing_class(**self.image_processor_dict	)
      self.assertTrue(hasattr(lowercase_					,      'do_resize_and_center_crop'	)	)
      self.assertTrue(hasattr(lowercase_					,      'size'	)	)
      self.assertTrue(hasattr(lowercase_					,      'crop_pct'	)	)
      self.assertTrue(hasattr(lowercase_					,      'do_normalize'	)	)
      self.assertTrue(hasattr(lowercase_					,      'image_mean'	)	)
      self.assertTrue(hasattr(lowercase_					,      'image_std'	)	)


     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     Tuple:
      UpperCAmelCase	:     List[str]						 =			self.image_processing_class.from_dict(self.image_processor_dict	)
      self.assertEqual(image_processor.size					,      {'shortest_edge': 30}	)
      self.assertEqual(image_processor.crop_size					,      {'height': 30, 'width': 30}	)

      UpperCAmelCase	:     Any						 =			self.image_processing_class.from_dict(self.image_processor_dict					,      size=42					,      crop_size=84	)
      self.assertEqual(image_processor.size					,      {'shortest_edge': 42}	)
      self.assertEqual(image_processor.crop_size					,      {'height': 84, 'width': 84}	)


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


     def        UpperCAmelCase_	(		self			:					List[Any]	)						->     List[str]:
      # Initialize image_processing
      UpperCAmelCase	:     int						 =			self.image_processing_class(**self.image_processor_dict	)
      # create random PIL images
      UpperCAmelCase	:     Any						 =			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	:     Dict						 =			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
      UpperCAmelCase	:     str						 =			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.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )					,      )


     def        UpperCAmelCase_	(		self			:					List[Any]	)						->     Dict:
      # Initialize image_processing
      UpperCAmelCase	:     int						 =			self.image_processing_class(**self.image_processor_dict	)
      # create random numpy tensors
      UpperCAmelCase	:     Dict						 =			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	:     Optional[Any]						 =			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
      UpperCAmelCase	:     Optional[Any]						 =			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.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )					,      )




     def        UpperCAmelCase_	(		self			:					str	)						->     Dict:
      # Initialize image_processing
      UpperCAmelCase	:     Optional[Any]						 =			self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      UpperCAmelCase	:     Tuple						 =			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	:     int						 =			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
      UpperCAmelCase	:     Optional[int]						 =			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.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )					,      )




