






'''simple docstring'''







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






			def __init__( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any=13  ,						__SCREAMING_SNAKE_CASE      :			int=7  ,						__SCREAMING_SNAKE_CASE      :			Any=True  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=True  ,						__SCREAMING_SNAKE_CASE      :			Any=True  ,						__SCREAMING_SNAKE_CASE      :			Any=True  ,						__SCREAMING_SNAKE_CASE      :			Tuple=99  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=32  ,						__SCREAMING_SNAKE_CASE      :			Any=2  ,						__SCREAMING_SNAKE_CASE      :			str=4  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=37  ,						__SCREAMING_SNAKE_CASE      :			int="gelu"  ,						__SCREAMING_SNAKE_CASE      :			List[str]=0.1  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=0.1  ,						__SCREAMING_SNAKE_CASE      :			str=512  ,						__SCREAMING_SNAKE_CASE      :			str=16  ,						__SCREAMING_SNAKE_CASE      :			Any=2  ,						__SCREAMING_SNAKE_CASE      :			int=0.02  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]=3  ,						__SCREAMING_SNAKE_CASE      :			Tuple=4  ,						__SCREAMING_SNAKE_CASE      :			str=None  ,						__SCREAMING_SNAKE_CASE      :			str=0  ,						)			->			List[Any]:
							a_		:  Dict											=      parent
							a_		:  str											=      batch_size
							a_		:  str											=      seq_length
							a_		:  List[str]											=      is_training
							a_		:  Optional[Any]											=      use_input_mask
							a_		:  Optional[Any]											=      use_token_type_ids
							a_		:  List[Any]											=      use_labels
							a_		:  Any											=      vocab_size
							a_		:  List[Any]											=      hidden_size
							a_		:  Dict											=      num_hidden_layers
							a_		:  Union[str, Any]											=      num_attention_heads
							a_		:  Optional[int]											=      intermediate_size
							a_		:  str											=      hidden_act
							a_		:  str											=      hidden_dropout_prob
							a_		:  Dict											=      attention_probs_dropout_prob
							a_		:  str											=      max_position_embeddings
							a_		:  Union[str, Any]											=      type_vocab_size
							a_		:  List[str]											=      type_sequence_label_size
							a_		:  Optional[int]											=      initializer_range
							a_		:  Any											=      num_labels
							a_		:  int											=      num_choices
							a_		:  List[Any]											=      scope
							a_		:  Optional[int]											=      projection_dim






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Optional[int]:
							a_		:  Union[str, Any]											=      ids_tensor([self.batch_size, self.seq_length]  ,						self.vocab_size					)

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

							a_		:  Dict											=      None
							if self.use_token_type_ids:
											a_		:  List[str]											=      ids_tensor([self.batch_size, self.seq_length]  ,						self.type_vocab_size					)

							a_		:  Tuple											=      None
							a_		:  Optional[Any]											=      None
							a_		:  Optional[Any]											=      None
							if self.use_labels:
											a_		:  Optional[Any]											=      ids_tensor([self.batch_size]  ,						self.type_sequence_label_size					)
											a_		:  int											=      ids_tensor([self.batch_size, self.seq_length]  ,						self.num_labels					)
											a_		:  List[Any]											=      ids_tensor([self.batch_size]  ,						self.num_choices					)

							a_		:  Tuple											=      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  ,						)
							a_		:  Union[str, 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 			SCREAMING_SNAKE_CASE      ( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]					)			->			str:
							a_		:  Tuple											=      TFDPRContextEncoder(config=__A					)
							a_		:  Any											=      model(__A  ,						attention_mask=__A  ,						token_type_ids=__A					)
							a_		:  Dict											=      model(__A  ,						token_type_ids=__A					)
							a_		:  Any											=      model(__A					)
							self.parent.assertEqual(result.pooler_output.shape  ,						(self.batch_size, self.projection_dim or self.hidden_size)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			Tuple					)			->			Union[str, Any]:
							a_		:  Any											=      TFDPRQuestionEncoder(config=__A					)
							a_		:  Optional[Any]											=      model(__A  ,						attention_mask=__A  ,						token_type_ids=__A					)
							a_		:  Tuple											=      model(__A  ,						token_type_ids=__A					)
							a_		:  List[Any]											=      model(__A					)
							self.parent.assertEqual(result.pooler_output.shape  ,						(self.batch_size, self.projection_dim or self.hidden_size)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any					)			->			Optional[int]:
							a_		:  Optional[int]											=      TFDPRReader(config=__A					)
							a_		:  int											=      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 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Dict:
							a_		:  Tuple											=      self.prepare_config_and_inputs()
							(
							    a_		
							)		:  Optional[int]											=      config_and_inputs
							a_		:  Optional[Any]											=      {"input_ids": input_ids}
							return config, inputs_dict





@require_tf
class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
			snake_case__  =				(
			    (
			        TFDPRContextEncoder,
			        TFDPRQuestionEncoder,
			        TFDPRReader,
			    )
			    if is_tf_available()
			    else ()
			)
			snake_case__  =				{"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

			snake_case__  =				False
			snake_case__  =				False
			snake_case__  =				False
			snake_case__  =				False
			snake_case__  =				False






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			List[str]:
							a_		:  Dict											=      TFDPRModelTester(self					)
							a_		:  Any											=      ConfigTester(self  ,						config_class=__A  ,						hidden_size=37					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[Any]:
							self.config_tester.run_common_tests()






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Tuple:
							a_		:  Any											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_context_encoder(*__A					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Any:
							a_		:  Dict											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_question_encoder(*__A					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Dict:
							a_		:  Union[str, Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_reader(*__A					)







			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			int:
							for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_		:  str											=      TFDPRContextEncoder.from_pretrained(__A					)
											self.assertIsNotNone(__A					)

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

							for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_		:  str											=      TFDPRQuestionEncoder.from_pretrained(__A					)
											self.assertIsNotNone(__A					)

							for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a_		:  Tuple											=      TFDPRReader.from_pretrained(__A					)
											self.assertIsNotNone(__A					)





@require_tf
class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			List[str]:
							a_		:  int											=      TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''					)

							a_		:  Optional[int]											=      tf.constant(
							    [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]					)  # [CLS] hello, is my dog cute? [SEP]
							a_		:  Optional[Any]											=      model(__A					)[0]  # embedding shape = (1, 768)
							# compare the actual values for a slice.
							a_		:  Tuple											=      tf.constant(
							    [
							        [
							            0.0323_6253,
							            0.1275_3335,
							            0.1681_8509,
							            0.0027_9786,
							            0.389_6933,
							            0.2426_4945,
							            0.217_8971,
							            -0.0233_5227,
							            -0.0848_1959,
							            -0.1432_4117,
							        ]
							    ]					)
							self.assertTrue(numpy.allclose(output[:, :10].numpy()  ,						expected_slice.numpy()  ,						atol=1e-4					)					)












'''simple docstring'''







from __future__ import annotations





def 		_UpperCAmelCase   (							__A							:   tuple[int, int]						,		__A							:   int      ):
				a_				,			a_		:  List[str]											=      position
				a_		:  Optional[int]											=      [
				    (y + 1, x + 2),
				    (y - 1, x + 2),
				    (y + 1, x - 2),
				    (y - 1, x - 2),
				    (y + 2, x + 1),
				    (y + 2, x - 1),
				    (y - 2, x + 1),
				    (y - 2, x - 1),
				]
				a_		:  Any											=      []

				for position in positions:
								a_				,			a_		:  Dict											=      position
								if 0 <= y_test < n and 0 <= x_test < n:
												permissible_positions.append(__A      )

				return permissible_positions





def 		_UpperCAmelCase   (							__A							:   list[list[int]]      ):
				return not any(elem == 0 for row in board for elem in row      )





def 		_UpperCAmelCase   (							__A							:   list[list[int]]						,		__A							:   tuple[int, int]						,		__A							:   int      ):

				if is_complete(__A      ):
								return True

				for position in get_valid_pos(__A						,		len(__A      )      ):
								a_				,			a_		:  Dict											=      position

								if board[y][x] == 0:
												a_		:  Optional[Any]											=      curr + 1
												if open_knight_tour_helper(__A						,		__A						,		curr + 1      ):
																return True
												a_		:  Tuple											=      0

				return False





def 		_UpperCAmelCase   (							__A							:   int      ):
				a_		:  List[str]											=      [[0 for i in range(__A      )] for j in range(__A      )]

				for i in range(__A      ):
								for j in range(__A      ):
												a_		:  Optional[Any]											=      1
												if open_knight_tour_helper(__A						,		(i, j)						,		1      ):
																return board
												a_		:  Union[str, Any]											=      0

				a_		:  Dict											=      f'Open Kight Tour cannot be performed on a board of size {n}'
				raise ValueError(__A      )


if __name__ == "__main__":
		import doctest

		doctest.testmod()




