


from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import numpy
				import tensorflow as tf

				from transformers import (
				    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    BertConfig,
				    DPRConfig,
				    TFDPRContextEncoder,
				    TFDPRQuestionEncoder,
				    TFDPRReader,
				)




class        lowerCamelCase      :


	def __init__(						self							,    lowercase__							,    lowercase__=1_3							,    lowercase__=7							,    lowercase__=True							,    lowercase__=True							,    lowercase__=True							,    lowercase__=True							,    lowercase__=9_9							,    lowercase__=3_2							,    lowercase__=2							,    lowercase__=4							,    lowercase__=3_7							,    lowercase__="gelu"							,    lowercase__=0.1							,    lowercase__=0.1							,    lowercase__=5_1_2							,    lowercase__=1_6							,    lowercase__=2							,    lowercase__=0.0_2							,    lowercase__=3							,    lowercase__=4							,    lowercase__=None							,    lowercase__=0							,    ):
							__UpperCAmelCase :       Any  						=  parent
							__UpperCAmelCase :       Optional[int]  						=  batch_size
							__UpperCAmelCase :       List[str]  						=  seq_length
							__UpperCAmelCase :       List[str]  						=  is_training
							__UpperCAmelCase :       Optional[Any]  						=  use_input_mask
							__UpperCAmelCase :       int  						=  use_token_type_ids
							__UpperCAmelCase :       Union[str, Any]  						=  use_labels
							__UpperCAmelCase :       List[Any]  						=  vocab_size
							__UpperCAmelCase :       Any  						=  hidden_size
							__UpperCAmelCase :       int  						=  num_hidden_layers
							__UpperCAmelCase :       Any  						=  num_attention_heads
							__UpperCAmelCase :       List[Any]  						=  intermediate_size
							__UpperCAmelCase :       List[str]  						=  hidden_act
							__UpperCAmelCase :       Optional[Any]  						=  hidden_dropout_prob
							__UpperCAmelCase :       Tuple  						=  attention_probs_dropout_prob
							__UpperCAmelCase :       Dict  						=  max_position_embeddings
							__UpperCAmelCase :       str  						=  type_vocab_size
							__UpperCAmelCase :       List[str]  						=  type_sequence_label_size
							__UpperCAmelCase :       List[Any]  						=  initializer_range
							__UpperCAmelCase :       Tuple  						=  num_labels
							__UpperCAmelCase :       int  						=  num_choices
							__UpperCAmelCase :       Optional[Any]  						=  scope
							__UpperCAmelCase :       List[str]  						=  projection_dim


	def    A(						self):
							__UpperCAmelCase :       Tuple  						=  ids_tensor([self.batch_size, self.seq_length]							,    self.vocab_size)

							__UpperCAmelCase :       Tuple  						=  None
							if self.use_input_mask:
													# follow test_modeling_tf_ctrl.py
													__UpperCAmelCase :       Optional[int]  						=  random_attention_mask([self.batch_size, self.seq_length])

							__UpperCAmelCase :       Any  						=  None
							if self.use_token_type_ids:
													__UpperCAmelCase :       Union[str, Any]  						=  ids_tensor([self.batch_size, self.seq_length]							,    self.type_vocab_size)

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

							__UpperCAmelCase :       Dict  						=  BertConfig(
							    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							,    is_decoder=lowercase__							,    initializer_range=self.initializer_range							,    )
							__UpperCAmelCase :       int  						=  DPRConfig(projection_dim=self.projection_dim							,    **config.to_dict())

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


	def    A(						self							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__):
							__UpperCAmelCase :       int  						=  TFDPRContextEncoder(config=lowercase__)
							__UpperCAmelCase :       Any  						=  model(lowercase__							,    attention_mask=lowercase__							,    token_type_ids=lowercase__)
							__UpperCAmelCase :       Any  						=  model(lowercase__							,    token_type_ids=lowercase__)
							__UpperCAmelCase :       Union[str, Any]  						=  model(lowercase__)
							self.parent.assertEqual(result.pooler_output.shape							,    (self.batch_size, self.projection_dim or self.hidden_size))


	def    A(						self							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__):
							__UpperCAmelCase :       List[str]  						=  TFDPRQuestionEncoder(config=lowercase__)
							__UpperCAmelCase :       int  						=  model(lowercase__							,    attention_mask=lowercase__							,    token_type_ids=lowercase__)
							__UpperCAmelCase :       str  						=  model(lowercase__							,    token_type_ids=lowercase__)
							__UpperCAmelCase :       Optional[Any]  						=  model(lowercase__)
							self.parent.assertEqual(result.pooler_output.shape							,    (self.batch_size, self.projection_dim or self.hidden_size))


	def    A(						self							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__							,    lowercase__):
							__UpperCAmelCase :       List[Any]  						=  TFDPRReader(config=lowercase__)
							__UpperCAmelCase :       List[str]  						=  model(lowercase__							,    attention_mask=lowercase__)

							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))
							self.parent.assertEqual(result.relevance_logits.shape							,    (self.batch_size,))


	def    A(						self):
							__UpperCAmelCase :       Tuple  						=  self.prepare_config_and_inputs()
							(
							    (
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			
							) :       List[Any]  						=  config_and_inputs
							__UpperCAmelCase :       List[str]  						=  {'''input_ids''': input_ids}
							return config, inputs_dict






@require_tf
class        lowerCamelCase      (     _UpperCamelCase       ,	_UpperCamelCase       ,	unittest.TestCase   ):
	_lowerCAmelCase    :		Dict        =	(
	    (
	        TFDPRContextEncoder,
	        TFDPRQuestionEncoder,
	        TFDPRReader,
	    )
	    if is_tf_available()
	    else ()
	)
	_lowerCAmelCase    :		Tuple        =	{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

	_lowerCAmelCase    :		List[str]        =	False
	_lowerCAmelCase    :		List[Any]        =	False
	_lowerCAmelCase    :		List[str]        =	False
	_lowerCAmelCase    :		Dict        =	False
	_lowerCAmelCase    :		Tuple        =	False


	def    A(						self):
							__UpperCAmelCase :       int  						=  TFDPRModelTester(self)
							__UpperCAmelCase :       Optional[Any]  						=  ConfigTester(self							,    config_class=lowercase__							,    hidden_size=3_7)


	def    A(						self):
							self.config_tester.run_common_tests()


	def    A(						self):
							__UpperCAmelCase :       List[Any]  						=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_context_encoder(*lowercase__)


	def    A(						self):
							__UpperCAmelCase :       Dict  						=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_question_encoder(*lowercase__)


	def    A(						self):
							__UpperCAmelCase :       Optional[int]  						=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_reader(*lowercase__)


	@slow
	def    A(						self):
							for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase :       Tuple  						=  TFDPRContextEncoder.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)

							for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase :       Optional[int]  						=  TFDPRContextEncoder.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)

							for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase :       Optional[Any]  						=  TFDPRQuestionEncoder.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)

							for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase :       List[Any]  						=  TFDPRReader.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)






@require_tf
class        lowerCamelCase      (     unittest.TestCase   ):


	@slow
	def    A(						self):
							__UpperCAmelCase :       Any  						=  TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base''')

							__UpperCAmelCase :       List[Any]  						=  tf.constant(
							    [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]])  # [CLS] hello, is my dog cute? [SEP]
							__UpperCAmelCase :       Dict  						=  model(lowercase__)[0]  # embedding shape = (1, 768)
							# compare the actual values for a slice.
							__UpperCAmelCase :       Dict  						=  tf.constant(
							    [
							        [
							            0.0_3_2_3_6_2_5_3,
							            0.1_2_7_5_3_3_3_5,
							            0.1_6_8_1_8_5_0_9,
							            0.0_0_2_7_9_7_8_6,
							            0.3_8_9_6_9_3_3,
							            0.2_4_2_6_4_9_4_5,
							            0.2_1_7_8_9_7_1,
							            -0.0_2_3_3_5_2_2_7,
							            -0.0_8_4_8_1_9_5_9,
							            -0.1_4_3_2_4_1_1_7,
							        ]
							    ])
							self.assertTrue(numpy.allclose(output[:, :1_0].numpy()							,    expected_slice.numpy()							,    atol=1e-4))





lowerCAmelCase							=						256
# Modulus to hash a string
lowerCAmelCase							=						1_000_003




def 						__SCREAMING_SNAKE_CASE       (					lowercase_			,					lowercase_			)		->    bool:




						'''simple docstring'''







						__UpperCAmelCase :       List[str]  						=  len(lowercase_			)
						__UpperCAmelCase :       Tuple  						=  len(lowercase_			)
						if p_len > t_len:
												return False

						__UpperCAmelCase :       Any  						=  0
						__UpperCAmelCase :       List[Any]  						=  0
						__UpperCAmelCase :       List[Any]  						=  1

						# Calculating the hash of pattern and substring of text
						for i in range(lowercase_			):
												__UpperCAmelCase :       List[str]  						=  (ord(pattern[i]			) + p_hash * alphabet_size) % modulus
												__UpperCAmelCase :       List[Any]  						=  (ord(text[i]			) + text_hash * alphabet_size) % modulus
												if i == p_len - 1:
																		continue
												__UpperCAmelCase :       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
												__UpperCAmelCase :       int  						=  (
												    (text_hash - ord(text[i]			) * modulus_power) * alphabet_size
												    + ord(text[i + p_len]			)
												) % modulus
						return False




def 						__SCREAMING_SNAKE_CASE       (					)		->    None:




						'''simple docstring'''







						__UpperCAmelCase :       Optional[int]  						=  '''abc1abc12'''
						__UpperCAmelCase :       List[str]  						=  '''alskfjaldsabc1abc1abc12k23adsfabcabc'''
						__UpperCAmelCase :       Any  						=  '''alskfjaldsk23adsfabcabc'''
						assert rabin_karp(lowercase_			,					lowercase_			) and not rabin_karp(lowercase_			,					lowercase_			)

						# Test 2)
						__UpperCAmelCase :       Union[str, Any]  						=  '''ABABX'''
						__UpperCAmelCase :       List[Any]  						=  '''ABABZABABYABABX'''
						assert rabin_karp(lowercase_			,					lowercase_			)

						# Test 3)
						__UpperCAmelCase :       str  						=  '''AAAB'''
						__UpperCAmelCase :       List[Any]  						=  '''ABAAAAAB'''
						assert rabin_karp(lowercase_			,					lowercase_			)

						# Test 4)
						__UpperCAmelCase :       Optional[Any]  						=  '''abcdabcy'''
						__UpperCAmelCase :       Any  						=  '''abcxabcdabxabcdabcdabcy'''
						assert rabin_karp(lowercase_			,					lowercase_			)

						# Test 5)
						__UpperCAmelCase :       Any  						=  '''Lü'''
						__UpperCAmelCase :       Optional[int]  						=  '''Lüsai'''
						assert rabin_karp(lowercase_			,					lowercase_			)
						__UpperCAmelCase :       List[Any]  						=  '''Lue'''
						assert not rabin_karp(lowercase_			,					lowercase_			)
						print('''Success.'''			)


if __name__ == "__main__":
				test_rabin_karp()

