

'''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     :		List[str]		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		Optional[int]=13		,					UpperCAmelCase__     :		Any=7		,					UpperCAmelCase__     :		Any=True		,					UpperCAmelCase__     :		List[Any]=True		,					UpperCAmelCase__     :		int=True		,					UpperCAmelCase__     :		str=True		,					UpperCAmelCase__     :		Optional[Any]=99		,					UpperCAmelCase__     :		Optional[Any]=32		,					UpperCAmelCase__     :		Optional[Any]=2		,					UpperCAmelCase__     :		Any=4		,					UpperCAmelCase__     :		Any=37		,					UpperCAmelCase__     :		str="gelu"		,					UpperCAmelCase__     :		Union[str, Any]=0.1		,					UpperCAmelCase__     :		List[Any]=0.1		,					UpperCAmelCase__     :		Any=512		,					UpperCAmelCase__     :		str=16		,					UpperCAmelCase__     :		Dict=2		,					UpperCAmelCase__     :		Tuple=0.02		,					UpperCAmelCase__     :		Optional[int]=3		,					UpperCAmelCase__     :		List[str]=4		,					UpperCAmelCase__     :		Optional[Any]=None		,					UpperCAmelCase__     :		Tuple=0		,					):
										'''simple docstring'''
										lowercase :							Optional[Any]    		=parent
										lowercase :							Dict    		=batch_size
										lowercase :							List[Any]    		=seq_length
										lowercase :							Optional[Any]    		=is_training
										lowercase :							Tuple    		=use_input_mask
										lowercase :							Dict    		=use_token_type_ids
										lowercase :							Any    		=use_labels
										lowercase :							List[Any]    		=vocab_size
										lowercase :							int    		=hidden_size
										lowercase :							List[Any]    		=num_hidden_layers
										lowercase :							Dict    		=num_attention_heads
										lowercase :							Optional[Any]    		=intermediate_size
										lowercase :							str    		=hidden_act
										lowercase :							Optional[Any]    		=hidden_dropout_prob
										lowercase :							Any    		=attention_probs_dropout_prob
										lowercase :							List[Any]    		=max_position_embeddings
										lowercase :							Dict    		=type_vocab_size
										lowercase :							List[str]    		=type_sequence_label_size
										lowercase :							Union[str, Any]    		=initializer_range
										lowercase :							Tuple    		=num_labels
										lowercase :							Any    		=num_choices
										lowercase :							Dict    		=scope
										lowercase :							Any    		=projection_dim







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							Dict    		=ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size   )

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

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

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

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







				def  lowerCamelCase_	(       self     :		Union[str, Any]		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		Union[str, Any]		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		str		,					UpperCAmelCase__     :		Optional[int]   ):
										'''simple docstring'''
										lowercase :							str    		=TFDPRQuestionEncoder(config=UpperCAmelCase__   )
										lowercase :							Dict    		=model(UpperCAmelCase__		,					attention_mask=UpperCAmelCase__		,					token_type_ids=UpperCAmelCase__   )
										lowercase :							Optional[Any]    		=model(UpperCAmelCase__		,					token_type_ids=UpperCAmelCase__   )
										lowercase :							Any    		=model(UpperCAmelCase__   )
										self.parent.assertEqual(result.pooler_output.shape		,					(self.batch_size, self.projection_dim or self.hidden_size)   )







				def  lowerCamelCase_	(       self     :		List[str]		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		Optional[int]		,					UpperCAmelCase__     :		int   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=TFDPRReader(config=UpperCAmelCase__   )
										lowercase :							Tuple    		=model(UpperCAmelCase__		,					attention_mask=UpperCAmelCase__   )

										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     :		Any   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.prepare_config_and_inputs()
										(
										    (
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	
										) :							Dict    		=config_and_inputs
										lowercase :							Optional[Any]    		={'''input_ids''': input_ids}
										return config, inputs_dict





@require_tf
class 		__SCREAMING_SNAKE_CASE							(			lowercase__						,							lowercase__						,							unittest.TestCase      ):
				lowerCamelCase_		       =    (
				    (
				        TFDPRContextEncoder,
				        TFDPRQuestionEncoder,
				        TFDPRReader,
				    )
				    if is_tf_available()
				    else ()
				)
				lowerCamelCase_		       =    {'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False







				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										lowercase :							int    		=TFDPRModelTester(self   )
										lowercase :							Optional[Any]    		=ConfigTester(self		,					config_class=UpperCAmelCase__		,					hidden_size=37   )







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										self.config_tester.run_common_tests()







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							int    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_context_encoder(*UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										lowercase :							str    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_question_encoder(*UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							int    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_reader(*UpperCAmelCase__   )







				@slow
				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase :							Optional[int]    		=TFDPRContextEncoder.from_pretrained(UpperCAmelCase__   )
																self.assertIsNotNone(UpperCAmelCase__   )

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

										for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase :							Tuple    		=TFDPRQuestionEncoder.from_pretrained(UpperCAmelCase__   )
																self.assertIsNotNone(UpperCAmelCase__   )

										for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase :							List[str]    		=TFDPRReader.from_pretrained(UpperCAmelCase__   )
																self.assertIsNotNone(UpperCAmelCase__   )





@require_tf
class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):







				@slow
				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							str    		=TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''   )

										lowercase :							Any    		=tf.constant(
										    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]   )  # [CLS] hello, is my dog cute? [SEP]
										lowercase :							int    		=model(UpperCAmelCase__   )[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										lowercase :							Any    		=tf.constant(
										    [
										        [
										            0.03_23_62_53,
										            0.12_75_33_35,
										            0.16_81_85_09,
										            0.00_27_97_86,
										            0.3_89_69_33,
										            0.24_26_49_45,
										            0.2_17_89_71,
										            -0.02_33_52_27,
										            -0.08_48_19_59,
										            -0.14_32_41_17,
										        ]
										    ]   )
										self.assertTrue(numpy.allclose(output[:, :10].numpy()		,					expected_slice.numpy()		,					atol=1E-4   )   )









'''simple docstring'''
def       _lowercase							(					UpperCamelCase__							:       dict      ):
 __A	:		Dict	  =      set()
 # edges = list of graph's edges
 __A	:		Any	  =      get_edges(UpperCamelCase__      )

 # While there are still elements in edges list, take an arbitrary edge
 # (from_node, to_node) and add his extremity to chosen_vertices and then
 # remove all arcs adjacent to the from_node and to_node
 while edges:
  __A  ,__A	:		List[Any]	  =      edges.pop()
  chosen_vertices.add(UpperCamelCase__      )
  chosen_vertices.add(UpperCamelCase__      )
  for edge in edges.copy():
   if from_node in edge or to_node in edge:
    edges.discard(UpperCamelCase__      )
 return chosen_vertices


def       _lowercase							(					UpperCamelCase__							:       dict      ):
 __A	:		Any	  =      set()
 for from_node, to_nodes in graph.items():
  for to_node in to_nodes:
   edges.add((from_node, to_node)      )
 return edges


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]}
 # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")






