




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



 '''simple docstring'''


 def __init__(							self   :    Dict		,				A_   :    Optional[Any]		,				A_   :    Optional[int]=13		,				A_   :    List[Any]=7		,				A_   :    Dict=True		,				A_   :    Any=True		,				A_   :    int=True		,				A_   :    int=True		,				A_   :    Any=99		,				A_   :    str=32		,				A_   :    str=2		,				A_   :    Optional[Any]=4		,				A_   :    Optional[int]=37		,				A_   :    Union[str, Any]="gelu"		,				A_   :    Dict=0.1		,				A_   :    Optional[Any]=0.1		,				A_   :    Optional[Any]=512		,				A_   :    List[str]=16		,				A_   :    Optional[int]=2		,				A_   :    Optional[Any]=0.02		,				A_   :    List[Any]=3		,				A_   :    List[Any]=4		,				A_   :    Optional[Any]=None		,				A_   :    Any=0		,				)  ->      Dict:

       """simple docstring"""






       lowerCamelCase_							=       parent
       lowerCamelCase_							=       batch_size
       lowerCamelCase_							=       seq_length
       lowerCamelCase_							=       is_training
       lowerCamelCase_							=       use_input_mask
       lowerCamelCase_							=       use_token_type_ids
       lowerCamelCase_							=       use_labels
       lowerCamelCase_							=       vocab_size
       lowerCamelCase_							=       hidden_size
       lowerCamelCase_							=       num_hidden_layers
       lowerCamelCase_							=       num_attention_heads
       lowerCamelCase_							=       intermediate_size
       lowerCamelCase_							=       hidden_act
       lowerCamelCase_							=       hidden_dropout_prob
       lowerCamelCase_							=       attention_probs_dropout_prob
       lowerCamelCase_							=       max_position_embeddings
       lowerCamelCase_							=       type_vocab_size
       lowerCamelCase_							=       type_sequence_label_size
       lowerCamelCase_							=       initializer_range
       lowerCamelCase_							=       num_labels
       lowerCamelCase_							=       num_choices
       lowerCamelCase_							=       scope
       lowerCamelCase_							=       projection_dim

 def 							a__					(							self   :    Any    )  ->      int:

       """simple docstring"""






       lowerCamelCase_							=       ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size    )

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

       lowerCamelCase_							=       None
       if self.use_token_type_ids:
             lowerCamelCase_							=       ids_tensor([self.batch_size, self.seq_length]		,				self.type_vocab_size    )

       lowerCamelCase_							=       None
       lowerCamelCase_							=       None
       lowerCamelCase_							=       None
       if self.use_labels:
             lowerCamelCase_							=       ids_tensor([self.batch_size]		,				self.type_sequence_label_size    )
             lowerCamelCase_							=       ids_tensor([self.batch_size, self.seq_length]		,				self.num_labels    )
             lowerCamelCase_							=       ids_tensor([self.batch_size]		,				self.num_choices    )

       lowerCamelCase_							=       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		,				)
       lowerCamelCase_							=       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   :    Dict		,				A_   :    Optional[int]		,				A_   :    Optional[Any]		,				A_   :    Optional[int]		,				A_   :    Optional[Any]		,				A_   :    Any		,				A_   :    Any		,				A_   :    Optional[int]    )  ->      str:

       """simple docstring"""






       lowerCamelCase_							=       TFDPRContextEncoder(config=A_    )
       lowerCamelCase_							=       model(A_		,				attention_mask=A_		,				token_type_ids=A_    )
       lowerCamelCase_							=       model(A_		,				token_type_ids=A_    )
       lowerCamelCase_							=       model(A_    )
       self.parent.assertEqual(result.pooler_output.shape		,				(self.batch_size, self.projection_dim or self.hidden_size)    )

 def 							a__					(							self   :    Any		,				A_   :    List[str]		,				A_   :    int		,				A_   :    List[str]		,				A_   :    List[Any]		,				A_   :    str		,				A_   :    Tuple		,				A_   :    List[Any]    )  ->      Dict:

       """simple docstring"""






       lowerCamelCase_							=       TFDPRQuestionEncoder(config=A_    )
       lowerCamelCase_							=       model(A_		,				attention_mask=A_		,				token_type_ids=A_    )
       lowerCamelCase_							=       model(A_		,				token_type_ids=A_    )
       lowerCamelCase_							=       model(A_    )
       self.parent.assertEqual(result.pooler_output.shape		,				(self.batch_size, self.projection_dim or self.hidden_size)    )

 def 							a__					(							self   :    Optional[Any]		,				A_   :    List[str]		,				A_   :    List[Any]		,				A_   :    Optional[int]		,				A_   :    Optional[int]		,				A_   :    str		,				A_   :    Union[str, Any]		,				A_   :    int    )  ->      Tuple:

       """simple docstring"""






       lowerCamelCase_							=       TFDPRReader(config=A_    )
       lowerCamelCase_							=       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 							a__					(							self   :    str    )  ->      Tuple:

       """simple docstring"""






       lowerCamelCase_							=       self.prepare_config_and_inputs()
       (
           (
           lowerCamelCase_
       )	,							(
           lowerCamelCase_
       )	,							(
           lowerCamelCase_
       )	,							(
           lowerCamelCase_
       )	,							(
           lowerCamelCase_
       )	,							(
           lowerCamelCase_
       )	,							(
           lowerCamelCase_
       )	,							
       )							=       config_and_inputs
       lowerCamelCase_							=       {'input_ids': input_ids}
       return config, inputs_dict







@require_tf
class     A(		UpperCamelCase				, UpperCamelCase				, unittest.TestCase  ):



 '''simple docstring'''


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

 UpperCamelCase		  =		False
 UpperCamelCase		  =		False
 UpperCamelCase		  =		False
 UpperCamelCase		  =		False
 UpperCamelCase		  =		False

 def 							a__					(							self   :    Dict    )  ->      List[Any]:

       """simple docstring"""






       lowerCamelCase_							=       TFDPRModelTester(self    )
       lowerCamelCase_							=       ConfigTester(self		,				config_class=A_		,				hidden_size=37    )

 def 							a__					(							self   :    Union[str, Any]    )  ->      Tuple:

       """simple docstring"""






       self.config_tester.run_common_tests()

 def 							a__					(							self   :    int    )  ->      List[str]:

       """simple docstring"""






       lowerCamelCase_							=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*A_    )

 def 							a__					(							self   :    Optional[int]    )  ->      Dict:

       """simple docstring"""






       lowerCamelCase_							=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*A_    )

 def 							a__					(							self   :    Union[str, Any]    )  ->      str:

       """simple docstring"""






       lowerCamelCase_							=       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*A_    )





 @slow
 def 							a__					(							self   :    str    )  ->      Union[str, Any]:

       """simple docstring"""






       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase_							=       TFDPRContextEncoder.from_pretrained(A_    )
             self.assertIsNotNone(A_    )

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase_							=       TFDPRContextEncoder.from_pretrained(A_    )
             self.assertIsNotNone(A_    )

       for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase_							=       TFDPRQuestionEncoder.from_pretrained(A_    )
             self.assertIsNotNone(A_    )

       for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase_							=       TFDPRReader.from_pretrained(A_    )
             self.assertIsNotNone(A_    )







@require_tf
class     A(		unittest.TestCase  ):



 '''simple docstring'''


 @slow
 def 							a__					(							self   :    int    )  ->      int:

       """simple docstring"""






       lowerCamelCase_							=       TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'    )

       lowerCamelCase_							=       tf.constant(
           [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]    )  # [CLS] hello, is my dog cute? [SEP]
       lowerCamelCase_							=       model(A_    )[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       lowerCamelCase_							=       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[:, :10].numpy()		,				expected_slice.numpy()		,				atol=1E-4    )    )








'''simple docstring'''





# Algorithm for the pigeonhole sorting



def 			a_	(				_lowerCAmelCase			) -> List[str]:
  __lowerCamelCase			:			int       			=					min(_lowerCAmelCase			)  # min() finds the minimum value
  __lowerCamelCase			:			List[Any]       			=					max(_lowerCAmelCase			)  # max() finds the maximum value

  __lowerCamelCase			:			List[Any]       			=					max_val - min_val + 1  # size is difference of max and min values plus one

  # list of pigeonholes of size equal to the variable size
  __lowerCamelCase			:			List[Any]       			=					[0] * size

  # Populate the pigeonholes.
  for x in a:
    assert isinstance(_lowerCAmelCase			,_lowerCAmelCase			), "integers only please"
    holes[x - min_val] += 1

  # Putting the elements back into the array in an order.
  __lowerCamelCase			:			Tuple       			=					0
  for count in range(_lowerCAmelCase			):
    while holes[count] > 0:
      holes[count] -= 1
      __lowerCamelCase			:			List[Any]       			=					count + min_val
      i += 1

def 			a_	(				) -> str:
  __lowerCamelCase			:			Optional[Any]       			=					[8, 3, 2, 7, 4, 6, 8]
  pigeonhole_sort(_lowerCAmelCase			)
  print('Sorted order is:'			,' '.join(_lowerCAmelCase			)			)


if __name__ == "__main__":
   main()






