



'''simple docstring'''





import unittest

from transformers import DebertaVaConfig, 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 (
      DebertaVaForMaskedLM,
      DebertaVaForMultipleChoice,
      DebertaVaForQuestionAnswering,
      DebertaVaForSequenceClassification,
      DebertaVaForTokenClassification,
      DebertaVaModel,
  )
  from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST






class __A    (						A		):



 '''simple docstring'''







 def __init__(self				,     A				,     A=13				,     A=7				,     A=True				,     A=True				,     A=True				,     A=True				,     A=99				,     A=32				,     A=5				,     A=4				,     A=37				,     A="gelu"				,     A=0.1				,     A=0.1				,     A=512				,     A=16				,     A=2				,     A=0.02				,     A=False				,     A=True				,     A="None"				,     A=3				,     A=4				,     A=None				,     )					->  Optional[Any]:


      """simple docstring"""



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



 def       a__						(self )					->  Dict:


      """simple docstring"""



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

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

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

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

      _a											=						self.get_config()

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



 def       a__						(self )					->  Any:


      """simple docstring"""



      return DebertaVaConfig(
          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       a__						(self				,     A )					->  Any:


      """simple docstring"""



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



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  int:


      """simple docstring"""



      _a											=						DebertaVaModel(config=A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     token_type_ids=A )[0]
      _a											=						model(A				,     token_type_ids=A )[0]
      _a											=						model(A )[0]

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



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  Any:


      """simple docstring"""



      _a											=						DebertaVaForMaskedLM(config=A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     token_type_ids=A				,     labels=A )

      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size) )



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  List[str]:


      """simple docstring"""



      _a											=						self.num_labels
      _a											=						DebertaVaForSequenceClassification(A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     token_type_ids=A				,     labels=A )
      self.parent.assertListEqual(list(result.logits.size() )				,     [self.batch_size, self.num_labels] )
      self.check_loss_output(A )



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


      """simple docstring"""



      _a											=						self.num_labels
      _a											=						DebertaVaForTokenClassification(config=A )
      model.to(A )
      model.eval()
      _a											=						model(A				,     attention_mask=A				,     token_type_ids=A				,     labels=A )
      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels) )



 def       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  int:


      """simple docstring"""



      _a											=						DebertaVaForQuestionAnswering(config=A )
      model.to(A )
      model.eval()
      _a											=						model(
          A				,     attention_mask=A				,     token_type_ids=A				,     start_positions=A				,     end_positions=A				,     )
      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       a__						(self				,     A				,     A				,     A				,     A				,     A				,     A				,     A )					->  Optional[int]:


      """simple docstring"""



      _a											=						DebertaVaForMultipleChoice(config=A )
      model.to(A )
      model.eval()
      _a											=						input_ids.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
      _a											=						token_type_ids.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
      _a											=						input_mask.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
      _a											=						model(
          A				,     attention_mask=A				,     token_type_ids=A				,     labels=A				,     )
      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices) )



 def       a__						(self )					->  Any:


      """simple docstring"""



      _a											=						self.prepare_config_and_inputs()
      (
          (
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					(
          _a
      )						,					
      )											=						config_and_inputs
      _a											=						{'''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'''







 __lowerCamelCase					: Union[str, Any]    = (
     (
         DebertaVaModel,
         DebertaVaForMaskedLM,
         DebertaVaForSequenceClassification,
         DebertaVaForTokenClassification,
         DebertaVaForQuestionAnswering,
         DebertaVaForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 __lowerCamelCase					: Tuple    = (
     {
         'feature-extraction': DebertaVaModel,
         'fill-mask': DebertaVaForMaskedLM,
         'question-answering': DebertaVaForQuestionAnswering,
         'text-classification': DebertaVaForSequenceClassification,
         'token-classification': DebertaVaForTokenClassification,
         'zero-shot': DebertaVaForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )

 __lowerCamelCase					: List[str]    = True
 __lowerCamelCase					: str    = False
 __lowerCamelCase					: Dict    = False
 __lowerCamelCase					: List[str]    = False
 __lowerCamelCase					: Optional[Any]    = False



 def       a__						(self )					->  Tuple:


      """simple docstring"""



      _a											=						DebertaVaModelTester(self )
      _a											=						ConfigTester(self				,     config_class=A				,     hidden_size=37 )



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


      """simple docstring"""



      self.config_tester.run_common_tests()



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_model(*A )



 def       a__						(self )					->  Tuple:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_sequence_classification(*A )



 def       a__						(self )					->  Optional[int]:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_masked_lm(*A )



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_question_answering(*A )



 def       a__						(self )					->  Tuple:


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_token_classification(*A )



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


      """simple docstring"""



      _a											=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_multiple_choice(*A )



 @slow
 def       a__						(self )					->  Optional[int]:


      """simple docstring"""



      for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _a											=						DebertaVaModel.from_pretrained(A )
           self.assertIsNotNone(A )






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



 '''simple docstring'''







 @unittest.skip(reason='''Model not available yet''' )
 def       a__						(self )					->  Union[str, Any]:


      """simple docstring"""



      pass



 @slow
 def       a__						(self )					->  Optional[Any]:


      """simple docstring"""



      _a											=						DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge''' )

      _a											=						torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]] )
      _a											=						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
      with torch.no_grad():
           _a											=						model(A				,     attention_mask=A )[0]
      # compare the actual values for a slice.
      _a											=						torch.tensor(
          [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]] )
      self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,     A				,     atol=1E-4 )				,     f'''{output[:, 1:4, 1:4]}''' )








'''simple docstring'''





import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse("9.1.0"):
  lowercase_	 =						{
      "linear": PIL.Image.Resampling.BILINEAR,
      "bilinear": PIL.Image.Resampling.BILINEAR,
      "bicubic": PIL.Image.Resampling.BICUBIC,
      "lanczos": PIL.Image.Resampling.LANCZOS,
      "nearest": PIL.Image.Resampling.NEAREST,
  }
else:
  lowercase_	 =						{
      "linear": PIL.Image.LINEAR,
      "bilinear": PIL.Image.BILINEAR,
      "bicubic": PIL.Image.BICUBIC,
      "lanczos": PIL.Image.LANCZOS,
      "nearest": PIL.Image.NEAREST,
  }




def lowerCAmelCase						(__A):



     """simple docstring"""



     _a											=						(images / 2 + 0.5).clamp(0						,			1)
     _a											=						images.cpu().permute(0						,			2						,			3						,			1).float().numpy()
     _a											=						numpy_to_pil(__A)
     return images




def lowerCAmelCase						(__A):



     """simple docstring"""



     if images.ndim == 3:
          _a											=						images[None, ...]
     _a											=						(images * 255).round().astype('''uint8''')
     if images.shape[-1] == 1:
          # special case for grayscale (single channel) images
          _a											=						[Image.fromarray(image.squeeze()						,			mode='''L''') for image in images]
     else:
          _a											=						[Image.fromarray(__A) for image in images]

     return pil_images



