





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






				'''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       ,					):
											"""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        lowercase__   (				self    ):
											"""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        lowercase__   (				self    ):
											"""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        lowercase__   (				self       ,					_a    ):
											"""simple docstring"""
											self.parent.assertListEqual(list(result.loss.size()    )       ,					[]    )






				def        lowercase__   (				self       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a    ):
											"""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        lowercase__   (				self       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a    ):
											"""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        lowercase__   (				self       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a    ):
											"""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        lowercase__   (				self       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a    ):
											"""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        lowercase__   (				self       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a    ):
											"""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        lowercase__   (				self       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a       ,					_a    ):
											"""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        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							self.prepare_config_and_inputs()
											(
											    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       _UpperCamelCase      (			SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    unittest.TestCase			):






				'''simple docstring'''





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

				SCREAMING_SNAKE_CASE:Optional[Any]         =	True
				SCREAMING_SNAKE_CASE:Optional[int]         =	False
				SCREAMING_SNAKE_CASE:Any         =	False
				SCREAMING_SNAKE_CASE:Any         =	False
				SCREAMING_SNAKE_CASE:Optional[Any]         =	False






				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							DebertaVaModelTester(self    )
											a__            =							ConfigTester(self       ,					config_class=_a       ,					hidden_size=37    )






				def        lowercase__   (				self    ):
											"""simple docstring"""
											self.config_tester.run_common_tests()






				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_model(*_a    )






				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_sequence_classification(*_a    )






				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_masked_lm(*_a    )






				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_question_answering(*_a    )






				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_token_classification(*_a    )






				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_multiple_choice(*_a    )






				@slow
				def        lowercase__   (				self    ):
											"""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       _UpperCamelCase      (			unittest.TestCase			):






				'''simple docstring'''





				@unittest.skip(reason='Model not available yet'    )
				def        lowercase__   (				self    ):
											"""simple docstring"""
											pass






				@slow
				def        lowercase__   (				self    ):
											"""simple docstring"""
											a__            =							DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'    )

											a__            =							torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 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]}'''    )










_lowerCamelCase    :		Optional[Any]		      =    256
# Modulus to hash a string
_lowerCamelCase    :		Optional[Any]		      =    1_000_003

def        __a			(   __lowerCAmelCase      ,	__lowerCAmelCase   )     ->     bool:
					SCREAMING_SNAKE_CASE							:     Tuple						=				len(__lowerCAmelCase   )
					SCREAMING_SNAKE_CASE							:     Tuple						=				len(__lowerCAmelCase   )
					if p_len > t_len:
										return False

					SCREAMING_SNAKE_CASE							:     Dict						=				0
					SCREAMING_SNAKE_CASE							:     Tuple						=				0
					SCREAMING_SNAKE_CASE							:     Optional[Any]						=				1

					# Calculating the hash of pattern and substring of text
					for i in range(__lowerCAmelCase   ):
										SCREAMING_SNAKE_CASE							:     Tuple						=				(ord(pattern[i]   ) + p_hash * alphabet_size) % modulus
										SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				(ord(text[i]   ) + text_hash * alphabet_size) % modulus
										if i == p_len - 1:
															continue
										SCREAMING_SNAKE_CASE							:     Optional[Any]						=				(modulus_power * alphabet_size) % modulus

					for i in range(0      ,	t_len - p_len + 1   ):
										if text_hash == p_hash and text[i : i + p_len] == pattern:
															return True
										if i == t_len - p_len:
															continue
										# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
										SCREAMING_SNAKE_CASE							:     int						=				(
										    (text_hash - ord(text[i]   ) * modulus_power) * alphabet_size
										    + ord(text[i + p_len]   )
										) % modulus
					return False

def        __a			(   )     ->     None:
					SCREAMING_SNAKE_CASE							:     Any						=				'abc1abc12'
					SCREAMING_SNAKE_CASE							:     str						=				'alskfjaldsabc1abc1abc12k23adsfabcabc'
					SCREAMING_SNAKE_CASE							:     List[str]						=				'alskfjaldsk23adsfabcabc'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   ) and not rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 2)
					SCREAMING_SNAKE_CASE							:     List[str]						=				'ABABX'
					SCREAMING_SNAKE_CASE							:     int						=				'ABABZABABYABABX'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 3)
					SCREAMING_SNAKE_CASE							:     int						=				'AAAB'
					SCREAMING_SNAKE_CASE							:     Tuple						=				'ABAAAAAB'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 4)
					SCREAMING_SNAKE_CASE							:     Tuple						=				'abcdabcy'
					SCREAMING_SNAKE_CASE							:     Optional[Any]						=				'abcxabcdabxabcdabcdabcy'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )

					# Test 5)
					SCREAMING_SNAKE_CASE							:     List[Any]						=				'Lü'
					SCREAMING_SNAKE_CASE							:     Optional[Any]						=				'Lüsai'
					assert rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )
					SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				'Lue'
					assert not rabin_karp(__lowerCAmelCase      ,	__lowerCAmelCase   )
					print('Success.'   )


if __name__ == "__main__":
			test_rabin_karp()