

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



    def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__=13		,				UpperCamelCase__=7		,				UpperCamelCase__=True		,				UpperCamelCase__=True		,				UpperCamelCase__=True		,				UpperCamelCase__=True		,				UpperCamelCase__=99		,				UpperCamelCase__=32		,				UpperCamelCase__=5		,				UpperCamelCase__=4		,				UpperCamelCase__=37		,				UpperCamelCase__="gelu"		,				UpperCamelCase__=0.1		,				UpperCamelCase__=0.1		,				UpperCamelCase__=512		,				UpperCamelCase__=16		,				UpperCamelCase__=2		,				UpperCamelCase__=0.02		,				UpperCamelCase__=False		,				UpperCamelCase__=True		,				UpperCamelCase__="None"		,				UpperCamelCase__=3		,				UpperCamelCase__=4		,				UpperCamelCase__=None		,				)						->      Union[str, 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 					snake_case_			(					self       )						->      List[Any]:





           '''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 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           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 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           A_						=      self.get_config()
           A_						=      300
           return config



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      Optional[Any]:





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



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      List[Any]:





           '''simple docstring'''
           A_						=      DebertaModel(config=UpperCamelCase__       )
           model.to(UpperCamelCase__       )
           model.eval()
           A_						=      model(UpperCamelCase__		,				attention_mask=UpperCamelCase__		,				token_type_ids=UpperCamelCase__       )[0]
           A_						=      model(UpperCamelCase__		,				token_type_ids=UpperCamelCase__       )[0]
           A_						=      model(UpperCamelCase__       )[0]

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



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Optional[int]:





           '''simple docstring'''
           A_						=      DebertaForMaskedLM(config=UpperCamelCase__       )
           model.to(UpperCamelCase__       )
           model.eval()
           A_						=      model(UpperCamelCase__		,				attention_mask=UpperCamelCase__		,				token_type_ids=UpperCamelCase__		,				labels=UpperCamelCase__       )

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



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





           '''simple docstring'''
           A_						=      self.num_labels
           A_						=      DebertaForSequenceClassification(UpperCamelCase__       )
           model.to(UpperCamelCase__       )
           model.eval()
           A_						=      model(UpperCamelCase__		,				attention_mask=UpperCamelCase__		,				token_type_ids=UpperCamelCase__		,				labels=UpperCamelCase__       )
           self.parent.assertListEqual(list(result.logits.size()       )		,				[self.batch_size, self.num_labels]       )
           self.check_loss_output(UpperCamelCase__       )



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      int:





           '''simple docstring'''
           A_						=      self.num_labels
           A_						=      DebertaForTokenClassification(config=UpperCamelCase__       )
           model.to(UpperCamelCase__       )
           model.eval()
           A_						=      model(UpperCamelCase__		,				attention_mask=UpperCamelCase__		,				token_type_ids=UpperCamelCase__		,				labels=UpperCamelCase__       )
           self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.num_labels)       )



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      List[str]:





           '''simple docstring'''
           A_						=      DebertaForQuestionAnswering(config=UpperCamelCase__       )
           model.to(UpperCamelCase__       )
           model.eval()
           A_						=      model(
               UpperCamelCase__		,				attention_mask=UpperCamelCase__		,				token_type_ids=UpperCamelCase__		,				start_positions=UpperCamelCase__		,				end_positions=UpperCamelCase__		,				)
           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 					snake_case_			(					self       )						->      int:





           '''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__      (      _snake_case						,						_snake_case						,						unittest.TestCase			):
    lowercase						=		(
        (
            DebertaModel,
            DebertaForMaskedLM,
            DebertaForSequenceClassification,
            DebertaForTokenClassification,
            DebertaForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    lowercase						=		(
        {
            "feature-extraction": DebertaModel,
            "fill-mask": DebertaForMaskedLM,
            "question-answering": DebertaForQuestionAnswering,
            "text-classification": DebertaForSequenceClassification,
            "token-classification": DebertaForTokenClassification,
            "zero-shot": DebertaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )

    lowercase						=		True
    lowercase						=		False
    lowercase						=		False
    lowercase						=		False
    lowercase						=		False



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           A_						=      DebertaModelTester(self       )
           A_						=      ConfigTester(self		,				config_class=UpperCamelCase__		,				hidden_size=37       )



    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           self.config_tester.run_common_tests()



    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_model(*UpperCamelCase__       )



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





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCamelCase__       )



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





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCamelCase__       )



    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_question_answering(*UpperCamelCase__       )



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_deberta_for_token_classification(*UpperCamelCase__       )







    @slow
    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A_						=      DebertaModel.from_pretrained(UpperCamelCase__       )
                  self.assertIsNotNone(UpperCamelCase__       )


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



    @unittest.skip(reason="""Model not available yet"""       )
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           pass







    @slow
    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           A_						=      DebertaModel.from_pretrained("""microsoft/deberta-base"""       )

           A_						=      torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]       )
           A_						=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
           with torch.no_grad():
                  A_						=      model(UpperCamelCase__		,				attention_mask=UpperCamelCase__       )[0]
           # compare the actual values for a slice.
           A_						=      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]		,				UpperCamelCase__		,				atol=1e-4       )		,				f'''{output[:, 1:4, 1:4]}'''       )



'''simple docstring'''







import numpy as np
from cva import COLOR_BGR2GRAY, cvtColor, imread
from numpy import array, uinta
from PIL import Image

from digital_image_processing import change_contrast as cc
from digital_image_processing import convert_to_negative as cn
from digital_image_processing import sepia as sp
from digital_image_processing.dithering import burkes as bs
from digital_image_processing.edge_detection import canny
from digital_image_processing.filters import convolve as conv
from digital_image_processing.filters import gaussian_filter as gg
from digital_image_processing.filters import local_binary_pattern as lbp
from digital_image_processing.filters import median_filter as med
from digital_image_processing.filters import sobel_filter as sob
from digital_image_processing.resize import resize as rs

__lowerCamelCase		       =    imread(r'''digital_image_processing/image_data/lena_small.jpg''')
__lowerCamelCase		       =    cvtColor(img, COLOR_BGR2GRAY)
def       UpperCAmelCase__    (      )			->			Dict:
       A_						=      cn.convert_to_negative(UpperCAmelCase__				)
       # assert negative_img array for at least one True
       assert negative_img.any()
def       UpperCAmelCase__    (      )			->			List[Any]:
       with Image.open("""digital_image_processing/image_data/lena_small.jpg"""				) as img:
              # Work around assertion for response
              assert str(cc.change_contrast(UpperCAmelCase__,       1_10				)				).startswith(
                  """<PIL.Image.Image image mode=RGB size=100x100 at"""				)
def       UpperCAmelCase__    (      )			->			str:
       A_						=      canny.gen_gaussian_kernel(9,       sigma=1.4				)
       # Assert ambiguous array
       assert resp.all()
def       UpperCAmelCase__    (      )			->			Union[str, Any]:
       A_						=      imread("""digital_image_processing/image_data/lena_small.jpg""",       0				)
       # assert ambiguous array for all == True
       assert canny_img.all()
       A_						=      canny.canny(UpperCAmelCase__				)
       # assert canny array for at least one True
       assert canny_array.any()
def       UpperCAmelCase__    (      )			->			Dict:
       assert gg.gaussian_filter(UpperCAmelCase__,       5,       sigma=0.9				).all()
def       UpperCAmelCase__    (      )			->			int:
       # laplace diagonals
       A_						=      array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]]				)
       A_						=      conv.img_convolve(UpperCAmelCase__,       UpperCAmelCase__				).astype(UpperCAmelCase__				)
       assert res.any()
def       UpperCAmelCase__    (      )			->			List[Any]:
       assert med.median_filter(UpperCAmelCase__,       3				).any()
def       UpperCAmelCase__    (      )			->			List[Any]:
       A_					,			A_						=      sob.sobel_filter(UpperCAmelCase__				)
       assert grad.any() and theta.any()
def       UpperCAmelCase__    (      )			->			List[str]:
       A_						=      sp.make_sepia(UpperCAmelCase__,       20				)
       assert sepia.all()
def       UpperCAmelCase__    (      UpperCAmelCase__ = "digital_image_processing/image_data/lena_small.jpg"				)			->			List[Any]:
       A_						=      bs.Burkes(imread(UpperCAmelCase__,       1				),       1_20				)
       burkes.process()
       assert burkes.output_img.any()
def       UpperCAmelCase__    (      UpperCAmelCase__ = "digital_image_processing/image_data/lena_small.jpg",       )			->			Optional[int]:
       A_						=      rs.NearestNeighbour(imread(UpperCAmelCase__,       1				),       4_00,       2_00				)
       nn.process()
       assert nn.output.any()







def       UpperCAmelCase__    (      )			->			Optional[int]:
       A_						=      """digital_image_processing/image_data/lena.jpg"""

       # Reading the image and converting it to grayscale.
       A_						=      imread(UpperCAmelCase__,       0				)

       # Test for get_neighbors_pixel function() return not None
       A_						=      0
       A_						=      0
       A_						=      image[x_coordinate][y_coordinate]

       A_						=      lbp.get_neighbors_pixel(
           UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)

       assert neighbors_pixels is not None

       # Test for local_binary_pattern function()
       # Create a numpy array as the same height and width of read image
       A_						=      np.zeros((image.shape[0], image.shape[1])				)

       # Iterating through the image and calculating the local binary pattern value
       # for each pixel.
       for i in range(0,       image.shape[0]				):
              for j in range(0,       image.shape[1]				):
                     A_						=      lbp.local_binary_value(UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)

       assert lbp_image.any()
