





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 					lowercase__ (			__lowerCamelCase       ):

   '''simple docstring'''


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






       """simple docstring"""
       UpperCamelCase__							:       str      =   parent
       UpperCamelCase__							:       Dict      =   batch_size
       UpperCamelCase__							:       Optional[int]      =   seq_length
       UpperCamelCase__							:       int      =   is_training
       UpperCamelCase__							:       Any      =   use_input_mask
       UpperCamelCase__							:       Dict      =   use_token_type_ids
       UpperCamelCase__							:       Optional[int]      =   use_labels
       UpperCamelCase__							:       Optional[int]      =   vocab_size
       UpperCamelCase__							:       Dict      =   hidden_size
       UpperCamelCase__							:       Optional[int]      =   num_hidden_layers
       UpperCamelCase__							:       List[Any]      =   num_attention_heads
       UpperCamelCase__							:       str      =   intermediate_size
       UpperCamelCase__							:       Dict      =   hidden_act
       UpperCamelCase__							:       Union[str, Any]      =   hidden_dropout_prob
       UpperCamelCase__							:       Optional[Any]      =   attention_probs_dropout_prob
       UpperCamelCase__							:       Optional[int]      =   max_position_embeddings
       UpperCamelCase__							:       Any      =   type_vocab_size
       UpperCamelCase__							:       Union[str, Any]      =   type_sequence_label_size
       UpperCamelCase__							:       List[str]      =   initializer_range
       UpperCamelCase__							:       Dict      =   num_labels
       UpperCamelCase__							:       str      =   num_choices
       UpperCamelCase__							:       List[Any]      =   relative_attention
       UpperCamelCase__							:       Optional[int]      =   position_biased_input
       UpperCamelCase__							:       int      =   pos_att_type
       UpperCamelCase__							:       int      =   scope




   def    UpperCamelCase__						(					self							)				->   str:






       """simple docstring"""
       UpperCamelCase__							:       str      =   ids_tensor([self.batch_size, self.seq_length],	self.vocab_size							)

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

       UpperCamelCase__							:       Optional[int]      =   None
       if self.use_token_type_ids:
           UpperCamelCase__							:       Tuple      =   ids_tensor([self.batch_size, self.seq_length],	self.type_vocab_size							)

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

       UpperCamelCase__							:       Tuple      =   self.get_config()

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




   def    UpperCamelCase__						(					self							)				->   Optional[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    UpperCamelCase__						(					self							)				->   Any:






       """simple docstring"""
       UpperCamelCase__							:       int      =   self.get_config()
       UpperCamelCase__							:       int      =   300
       return config




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






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




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       List[str]      =   DebertaModel(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:       Dict      =   model(__magic_name__,	attention_mask=__magic_name__,	token_type_ids=__magic_name__							)[0]
       UpperCamelCase__							:       List[Any]      =   model(__magic_name__,	token_type_ids=__magic_name__							)[0]
       UpperCamelCase__							:       Tuple      =   model(__magic_name__							)[0]

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




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       List[str]      =   DebertaForMaskedLM(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:       Dict      =   model(__magic_name__,	attention_mask=__magic_name__,	token_type_ids=__magic_name__,	labels=__magic_name__							)

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




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__,	__magic_name__							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   self.num_labels
       UpperCamelCase__							:       str      =   DebertaForSequenceClassification(__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:       List[str]      =   model(__magic_name__,	attention_mask=__magic_name__,	token_type_ids=__magic_name__,	labels=__magic_name__							)
       self.parent.assertListEqual(list(result.logits.size()							),	[self.batch_size, self.num_labels]							)
       self.check_loss_output(__magic_name__							)




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






       """simple docstring"""
       UpperCamelCase__							:       Any      =   self.num_labels
       UpperCamelCase__							:       Tuple      =   DebertaForTokenClassification(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:       List[Any]      =   model(__magic_name__,	attention_mask=__magic_name__,	token_type_ids=__magic_name__,	labels=__magic_name__							)
       self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.seq_length, self.num_labels)							)




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






       """simple docstring"""
       UpperCamelCase__							:       Tuple      =   DebertaForQuestionAnswering(config=__magic_name__							)
       model.to(__magic_name__							)
       model.eval()
       UpperCamelCase__							:       Dict      =   model(
           __magic_name__,	attention_mask=__magic_name__,	token_type_ids=__magic_name__,	start_positions=__magic_name__,	end_positions=__magic_name__,	)
       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							)				->   str:






       """simple docstring"""
       UpperCamelCase__							:       Optional[Any]      =   self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase__
       )     ,(
           UpperCamelCase__
       )     ,(
           UpperCamelCase__
       )     ,(
           UpperCamelCase__
       )     ,(
           UpperCamelCase__
       )     ,(
           UpperCamelCase__
       )     ,(
           UpperCamelCase__
       )     ,
       )							:       Any      =   config_and_inputs
       UpperCamelCase__							:       Union[str, Any]      =   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict






@require_torch
class 					lowercase__ (			__lowerCamelCase    ,       __lowerCamelCase    ,       unittest.TestCase       ):

   '''simple docstring'''


   a		:							List[Any]     	=	(
       (
           DebertaModel,
           DebertaForMaskedLM,
           DebertaForSequenceClassification,
           DebertaForTokenClassification,
           DebertaForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   a		:							Optional[Any]     	=	(
       {
           "feature-extraction": DebertaModel,
           "fill-mask": DebertaForMaskedLM,
           "question-answering": DebertaForQuestionAnswering,
           "text-classification": DebertaForSequenceClassification,
           "token-classification": DebertaForTokenClassification,
           "zero-shot": DebertaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

   a		:							Optional[int]     	=	True
   a		:							Optional[int]     	=	False
   a		:							List[str]     	=	False
   a		:							Any     	=	False
   a		:							str     	=	False




   def    UpperCamelCase__						(					self							)				->   int:






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   DebertaModelTester(self							)
       UpperCamelCase__							:       Optional[int]      =   ConfigTester(self,	config_class=__magic_name__,	hidden_size=37							)




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






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




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






       """simple docstring"""
       UpperCamelCase__							:       Union[str, Any]      =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*__magic_name__							)




   def    UpperCamelCase__						(					self							)				->   int:






       """simple docstring"""
       UpperCamelCase__							:       List[Any]      =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*__magic_name__							)




   def    UpperCamelCase__						(					self							)				->   Any:






       """simple docstring"""
       UpperCamelCase__							:       List[Any]      =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*__magic_name__							)




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






       """simple docstring"""
       UpperCamelCase__							:       Optional[int]      =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*__magic_name__							)




   def    UpperCamelCase__						(					self							)				->   Tuple:






       """simple docstring"""
       UpperCamelCase__							:       int      =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_token_classification(*__magic_name__							)




   @slow
   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__							:       List[str]      =   DebertaModel.from_pretrained(__magic_name__							)
           self.assertIsNotNone(__magic_name__							)






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

   '''simple docstring'''


   @unittest.skip(reason='''Model not available yet'''							)
   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       pass




   @slow
   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       UpperCamelCase__							:       List[str]      =   DebertaModel.from_pretrained('''microsoft/deberta-base'''							)

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








from __future__ import annotations




def 	lowerCAmelCase_					(						__UpperCAmelCase:    str	,     __UpperCAmelCase:    str   )   ->			bool:
    UpperCamelCase__							:       List[str]      =   get_failure_array(__UpperCAmelCase   )

    # 2) Step through text searching for pattern
    UpperCamelCase__     ,UpperCamelCase__							:       Dict      =   0, 0  # index into text, pattern
    while i < len(__UpperCAmelCase   ):
        if pattern[j] == text[i]:
            if j == (len(__UpperCAmelCase   ) - 1):
                return True
            j += 1

        # if this is a prefix in our pattern
        # just go back far enough to continue
        elif j > 0:
            UpperCamelCase__							:       Optional[int]      =   failure[j - 1]
            continue
        i += 1
    return False




def 	lowerCAmelCase_					(						__UpperCAmelCase:    str   )   ->			list[int]:
    UpperCamelCase__							:       Union[str, Any]      =   [0]
    UpperCamelCase__							:       Tuple      =   0
    UpperCamelCase__							:       Tuple      =   1
    while j < len(__UpperCAmelCase   ):
        if pattern[i] == pattern[j]:
            i += 1
        elif i > 0:
            UpperCamelCase__							:       str      =   failure[i - 1]
            continue
        j += 1
        failure.append(__UpperCAmelCase   )
    return failure


if __name__ == "__main__":
  # Test 1)
  UpperCAmelCase_  							=				'abc1abc12'
  UpperCAmelCase_  							=				'alskfjaldsabc1abc1abc12k23adsfabcabc'
  UpperCAmelCase_  							=				'alskfjaldsk23adsfabcabc'
  assert kmp(pattern, texta) and not kmp(pattern, texta)

  # Test 2)
  UpperCAmelCase_  							=				'ABABX'
  UpperCAmelCase_  							=				'ABABZABABYABABX'
  assert kmp(pattern, text)

  # Test 3)
  UpperCAmelCase_  							=				'AAAB'
  UpperCAmelCase_  							=				'ABAAAAAB'
  assert kmp(pattern, text)

  # Test 4)
  UpperCAmelCase_  							=				'abcdabcy'
  UpperCAmelCase_  							=				'abcxabcdabxabcdabcdabcy'
  assert kmp(pattern, text)

  # Test 5)
  UpperCAmelCase_  							=				'aabaabaaa'
  assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]

