
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__							:




	'''simple docstring'''




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







	def    _lowerCamelCase				(					self		:int						)		-> Any:
								__UpperCamelCase	:   Optional[Any]			   =					ids_tensor([self.batch_size, self.seq_length]     ,   self.vocab_size						)

								__UpperCamelCase	:   List[str]			   =					None
								if self.use_input_mask:
															# follow test_modeling_tf_ctrl.py
															__UpperCamelCase	:   Optional[Any]			   =					random_attention_mask([self.batch_size, self.seq_length]						)

								__UpperCamelCase	:   List[Any]			   =					None
								if self.use_token_type_ids:
															__UpperCamelCase	:   int			   =					ids_tensor([self.batch_size, self.seq_length]     ,   self.type_vocab_size						)

								__UpperCamelCase	:   List[str]			   =					None
								__UpperCamelCase	:   List[Any]			   =					None
								__UpperCamelCase	:   List[str]			   =					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	:   List[str]			   =					ids_tensor([self.batch_size]     ,   self.num_choices						)

								__UpperCamelCase	:   Union[str, Any]			   =					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=a     ,   initializer_range=self.initializer_range     ,   )
								__UpperCamelCase	:   Any			   =					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    _lowerCamelCase				(					self		:List[Any]     ,   a		:List[Any]     ,   a		:List[str]     ,   a		:Any     ,   a		:int     ,   a		:List[Any]     ,   a		:Optional[int]     ,   a		:Optional[int]						)		-> int:
								__UpperCamelCase	:   str			   =					TFDPRContextEncoder(config=a						)
								__UpperCamelCase	:   int			   =					model(a     ,   attention_mask=a     ,   token_type_ids=a						)
								__UpperCamelCase	:   Union[str, Any]			   =					model(a     ,   token_type_ids=a						)
								__UpperCamelCase	:   Optional[int]			   =					model(a						)
								self.parent.assertEqual(result.pooler_output.shape     ,   (self.batch_size, self.projection_dim or self.hidden_size)						)







	def    _lowerCamelCase				(					self		:Optional[int]     ,   a		:Optional[Any]     ,   a		:Dict     ,   a		:Any     ,   a		:Tuple     ,   a		:Optional[Any]     ,   a		:Any     ,   a		:List[Any]						)		-> Optional[Any]:
								__UpperCamelCase	:   Tuple			   =					TFDPRQuestionEncoder(config=a						)
								__UpperCamelCase	:   Any			   =					model(a     ,   attention_mask=a     ,   token_type_ids=a						)
								__UpperCamelCase	:   List[str]			   =					model(a     ,   token_type_ids=a						)
								__UpperCamelCase	:   Optional[Any]			   =					model(a						)
								self.parent.assertEqual(result.pooler_output.shape     ,   (self.batch_size, self.projection_dim or self.hidden_size)						)







	def    _lowerCamelCase				(					self		:Dict     ,   a		:Optional[int]     ,   a		:Optional[int]     ,   a		:List[Any]     ,   a		:Any     ,   a		:Union[str, Any]     ,   a		:List[str]     ,   a		:Optional[int]						)		-> List[str]:
								__UpperCamelCase	:   Any			   =					TFDPRReader(config=a						)
								__UpperCamelCase	:   str			   =					model(a     ,   attention_mask=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)						)
								self.parent.assertEqual(result.relevance_logits.shape     ,   (self.batch_size,)						)







	def    _lowerCamelCase				(					self		:Optional[Any]						)		-> Optional[Any]:
								__UpperCamelCase	:   int			   =					self.prepare_config_and_inputs()
								(
								    (
								    __UpperCamelCase
								)   ,						(
								    __UpperCamelCase
								)   ,						(
								    __UpperCamelCase
								)   ,						(
								    __UpperCamelCase
								)   ,						(
								    __UpperCamelCase
								)   ,						(
								    __UpperCamelCase
								)   ,						(
								    __UpperCamelCase
								)   ,						
								)	:   Dict			   =					config_and_inputs
								__UpperCamelCase	:   List[str]			   =					{"input_ids": input_ids}
								return config, inputs_dict




@require_tf
class  lowerCamelCase__							(				__lowercase     ,						__lowercase     ,						unittest.TestCase):




	'''simple docstring'''




	_A								=     (
	    (
	        TFDPRContextEncoder,
	        TFDPRQuestionEncoder,
	        TFDPRReader,
	    )
	    if is_tf_available()
	    else ()
	)
	_A								=     {'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

	_A								=     False
	_A								=     False
	_A								=     False
	_A								=     False
	_A								=     False







	def    _lowerCamelCase				(					self		:Any						)		-> str:
								__UpperCamelCase	:   List[Any]			   =					TFDPRModelTester(self						)
								__UpperCamelCase	:   List[str]			   =					ConfigTester(self     ,   config_class=a     ,   hidden_size=3_7						)







	def    _lowerCamelCase				(					self		:List[Any]						)		-> List[str]:
								self.config_tester.run_common_tests()







	def    _lowerCamelCase				(					self		:int						)		-> int:
								__UpperCamelCase	:   int			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_context_encoder(*a						)







	def    _lowerCamelCase				(					self		:str						)		-> Union[str, Any]:
								__UpperCamelCase	:   int			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_question_encoder(*a						)







	def    _lowerCamelCase				(					self		:Optional[int]						)		-> str:
								__UpperCamelCase	:   Tuple			   =					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_reader(*a						)







	@slow
	def    _lowerCamelCase				(					self		:Optional[Any]						)		-> Union[str, Any]:
								for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__UpperCamelCase	:   Dict			   =					TFDPRContextEncoder.from_pretrained(a						)
															self.assertIsNotNone(a						)

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

								for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__UpperCamelCase	:   List[str]			   =					TFDPRQuestionEncoder.from_pretrained(a						)
															self.assertIsNotNone(a						)

								for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__UpperCamelCase	:   Optional[Any]			   =					TFDPRReader.from_pretrained(a						)
															self.assertIsNotNone(a						)




@require_tf
class  lowerCamelCase__							(				unittest.TestCase):




	'''simple docstring'''




	@slow
	def    _lowerCamelCase				(					self		:Any						)		-> Any:
								__UpperCamelCase	:   int			   =					TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"						)

								__UpperCamelCase	:   Optional[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	:   Any			   =					model(a						)[0]  # embedding shape = (1, 768)
								# compare the actual values for a slice.
								__UpperCamelCase	:   str			   =					tf.constant(
								    [
								        [
								            0.03236253,
								            0.12753335,
								            0.16818509,
								            0.00279786,
								            0.3896933,
								            0.24264945,
								            0.2178971,
								            -0.02335227,
								            -0.08481959,
								            -0.14324117,
								        ]
								    ]						)
								self.assertTrue(numpy.allclose(output[:, :1_0].numpy()     ,   expected_slice.numpy()     ,   atol=1E-4						)						)

import math




def 	_SCREAMING_SNAKE_CASE					(		)					->    None:






							'''simple docstring'''





							__UpperCamelCase	:   List[Any]			   =					input("Enter message: ")
							__UpperCamelCase	:   Optional[int]			   =					int(input(F'Enter key [2-{len(_lowerCamelCase) - 1}]: '))
							__UpperCamelCase	:   str			   =					input("Encryption/Decryption [e/d]: ")

							if mode.lower().startswith("e"):
														__UpperCamelCase	:   List[str]			   =					encrypt_message(_lowerCamelCase						,  _lowerCamelCase)
							elif mode.lower().startswith("d"):
														__UpperCamelCase	:   Dict			   =					decrypt_message(_lowerCamelCase						,  _lowerCamelCase)

							# Append pipe symbol (vertical bar) to identify spaces at the end.
							print(F'Output:\n{text + "|"}')




def 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			int						,  _lowerCamelCase :			str)					->    str:






							'''simple docstring'''





							__UpperCamelCase	:   Optional[Any]			   =					[""] * key
							for col in range(_lowerCamelCase):
														__UpperCamelCase	:   Any			   =					col
														while pointer < len(_lowerCamelCase):
																					cipher_text[col] += message[pointer]
																					pointer += key
							return "".join(_lowerCamelCase)




def 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			int						,  _lowerCamelCase :			str)					->    str:






							'''simple docstring'''





							__UpperCamelCase	:   Any			   =					math.ceil(len(_lowerCamelCase) / key)
							__UpperCamelCase	:   Any			   =					key
							__UpperCamelCase	:   str			   =					(num_cols * num_rows) - len(_lowerCamelCase)
							__UpperCamelCase	:   Union[str, Any]			   =					[""] * num_cols
							__UpperCamelCase	:   Dict			   =					0
							__UpperCamelCase	:   int			   =					0

							for symbol in message:
														plain_text[col] += symbol
														col += 1

														if (
														    (col == num_cols)
														    or (col == num_cols - 1)
														    and (row >= num_rows - num_shaded_boxes)
														):
																					__UpperCamelCase	:   List[Any]			   =					0
																					row += 1

							return "".join(_lowerCamelCase)


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		main()