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



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


          '''simple docstring'''


          snake_case			:  Any     							= parent
          snake_case			:  List[Any]     							= batch_size
          snake_case			:  Dict     							= seq_length
          snake_case			:  List[str]     							= is_training
          snake_case			:  Any     							= use_input_mask
          snake_case			:  Tuple     							= use_token_type_ids
          snake_case			:  int     							= use_labels
          snake_case			:  Optional[Any]     							= vocab_size
          snake_case			:  Optional[Any]     							= hidden_size
          snake_case			:  Optional[Any]     							= num_hidden_layers
          snake_case			:  List[str]     							= num_attention_heads
          snake_case			:  str     							= intermediate_size
          snake_case			:  Union[str, Any]     							= hidden_act
          snake_case			:  int     							= hidden_dropout_prob
          snake_case			:  Optional[int]     							= attention_probs_dropout_prob
          snake_case			:  str     							= max_position_embeddings
          snake_case			:  Union[str, Any]     							= type_vocab_size
          snake_case			:  Optional[Any]     							= type_sequence_label_size
          snake_case			:  List[str]     							= initializer_range
          snake_case			:  str     							= num_labels
          snake_case			:  Optional[Any]     							= num_choices
          snake_case			:  List[Any]     							= scope
          snake_case			:  Optional[Any]     							= projection_dim



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


          '''simple docstring'''


          snake_case			:  Optional[Any]     							= ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size					)

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

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

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

          snake_case			:  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=snake_case__       ,		initializer_range=self.initializer_range       ,		)
          snake_case			:  List[str]     							= 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						:   int       ,		snake_case__						:   Dict       ,		snake_case__						:   Any       ,		snake_case__						:   Any       ,		snake_case__						:   Tuple       ,		snake_case__						:   str       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   List[Any]					)						->      int:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= TFDPRContextEncoder(config=snake_case__					)
          snake_case			:  Union[str, Any]     							= model(snake_case__       ,		attention_mask=snake_case__       ,		token_type_ids=snake_case__					)
          snake_case			:  int     							= model(snake_case__       ,		token_type_ids=snake_case__					)
          snake_case			:  List[Any]     							= model(snake_case__					)
          self.parent.assertEqual(result.pooler_output.shape       ,		(self.batch_size, self.projection_dim or self.hidden_size)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple       ,		snake_case__						:   str       ,		snake_case__						:   Tuple       ,		snake_case__						:   List[str]       ,		snake_case__						:   Tuple       ,		snake_case__						:   Optional[int]       ,		snake_case__						:   Any       ,		snake_case__						:   List[Any]					)						->      Tuple:


          '''simple docstring'''


          snake_case			:  int     							= TFDPRQuestionEncoder(config=snake_case__					)
          snake_case			:  Union[str, Any]     							= model(snake_case__       ,		attention_mask=snake_case__       ,		token_type_ids=snake_case__					)
          snake_case			:  Tuple     							= model(snake_case__       ,		token_type_ids=snake_case__					)
          snake_case			:  str     							= model(snake_case__					)
          self.parent.assertEqual(result.pooler_output.shape       ,		(self.batch_size, self.projection_dim or self.hidden_size)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   str       ,		snake_case__						:   Tuple       ,		snake_case__						:   List[str]       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   str       ,		snake_case__						:   Any       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   Union[str, Any]					)						->      Optional[Any]:


          '''simple docstring'''


          snake_case			:  Any     							= TFDPRReader(config=snake_case__					)
          snake_case			:  Any     							= model(snake_case__       ,		attention_mask=snake_case__					)

          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						:   Dict					)						->      Any:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= self.prepare_config_and_inputs()
          (
              (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   
          )			:  Any     							= config_and_inputs
          snake_case			:  Tuple     							= {"input_ids": input_ids}
          return config, inputs_dict






@require_tf
class     UpperCAmelCase						(		A_			,A_			,unittest.TestCase					):
   A__   :						List[Any]			=	(
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   A__   :						Union[str, Any]			=	{"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

   A__   :						str			=	False
   A__   :						List[str]			=	False
   A__   :						Union[str, Any]			=	False
   A__   :						str			=	False
   A__   :						Optional[int]			=	False



   def 							_SCREAMING_SNAKE_CASE  (self						:   List[Any]					)						->      int:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= TFDPRModelTester(self					)
          snake_case			:  Optional[Any]     							= ConfigTester(self       ,		config_class=snake_case__       ,		hidden_size=37					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   str					)						->      str:


          '''simple docstring'''


          self.config_tester.run_common_tests()



   def 							_SCREAMING_SNAKE_CASE  (self						:   Optional[Any]					)						->      str:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_context_encoder(*snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Any					)						->      List[str]:


          '''simple docstring'''


          snake_case			:  int     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_question_encoder(*snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Optional[Any]					)						->      List[Any]:


          '''simple docstring'''


          snake_case			:  List[Any]     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_reader(*snake_case__					)



   @slow
   def 							_SCREAMING_SNAKE_CASE  (self						:   List[str]					)						->      Optional[Any]:


          '''simple docstring'''


          for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case			:  Any     							= TFDPRContextEncoder.from_pretrained(snake_case__					)
                 self.assertIsNotNone(snake_case__					)

          for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case			:  Tuple     							= TFDPRContextEncoder.from_pretrained(snake_case__					)
                 self.assertIsNotNone(snake_case__					)

          for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case			:  Union[str, Any]     							= TFDPRQuestionEncoder.from_pretrained(snake_case__					)
                 self.assertIsNotNone(snake_case__					)

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






@require_tf
class     UpperCAmelCase						(		unittest.TestCase					):



   @slow
   def 							_SCREAMING_SNAKE_CASE  (self						:   List[str]					)						->      int:


          '''simple docstring'''


          snake_case			:  Dict     							= TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"					)

          snake_case			:  int     							= tf.constant(
              [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]					)  # [CLS] hello, is my dog cute? [SEP]
          snake_case			:  str     							= model(snake_case__					)[0]  # embedding shape = (1, 768)
          # compare the actual values for a slice.
          snake_case			:  Dict     							= 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					)					)







def      UpperCamelCase   (       __lowerCamelCase	:					str = "The quick brown fox jumps over the lazy dog"							,   ):
       snake_case			:  Dict     							= set()

       # Replace all the whitespace in our sentence
       snake_case			:  List[Any]     							= input_str.replace(" "							,   ""       )
       for alpha in input_str:
              if "a" <= alpha.lower() <= "z":
                     frequency.add(alpha.lower()       )
       return len(__lowerCamelCase       ) == 26


def      UpperCamelCase   (       __lowerCamelCase	:					str = "The quick brown fox jumps over the lazy dog"							,   ):
       snake_case			:  Optional[int]     							= [False] * 26
       for char in input_str:
              if char.islower():
                     snake_case			:  Dict     							= True
              elif char.isupper():
                     snake_case			:  Optional[Any]     							= True
       return all(__lowerCamelCase       )


def      UpperCamelCase   (       __lowerCamelCase	:					str = "The quick brown fox jumps over the lazy dog"							,   ):
       return len({char for char in input_str.lower() if char.isalpha()}       ) == 26


def      UpperCamelCase   (       ):
       from timeit import timeit

       snake_case			:  Tuple     							= "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest"
       print(timeit("is_pangram()"							,   setup=__lowerCamelCase       )       )
       print(timeit("is_pangram_faster()"							,   setup=__lowerCamelCase       )       )
       print(timeit("is_pangram_fastest()"							,   setup=__lowerCamelCase       )       )
       # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
       # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   benchmark()





