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   :






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





       '''simple docstring'''





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






  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





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

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

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

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

       UpperCamelCase__										=      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=SCREAMING_SNAKE_CASE_   ,				initializer_range=self.initializer_range   ,				)
       UpperCamelCase__										=      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   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	)		->      Optional[Any]:





       '''simple docstring'''





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






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	)		->      Tuple:





       '''simple docstring'''





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






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





       '''simple docstring'''





       UpperCamelCase__										=      TFDPRReader(config=SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      model(SCREAMING_SNAKE_CASE_   ,				attention_mask=SCREAMING_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 		_a					(self	)		->      Tuple:





       '''simple docstring'''





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



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

  SCREAMING_SNAKE_CASE_	:	Dict						=False
  SCREAMING_SNAKE_CASE_	:	Optional[int]						=False
  SCREAMING_SNAKE_CASE_	:	Any						=False
  SCREAMING_SNAKE_CASE_	:	Tuple						=False
  SCREAMING_SNAKE_CASE_	:	Union[str, Any]						=False






  def 		_a					(self	)		->      List[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      TFDPRModelTester(self	)
       UpperCamelCase__										=      ConfigTester(self   ,				config_class=SCREAMING_SNAKE_CASE_   ,				hidden_size=37	)






  def 		_a					(self	)		->      Any:





       '''simple docstring'''





       self.config_tester.run_common_tests()






  def 		_a					(self	)		->      Tuple:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*SCREAMING_SNAKE_CASE_	)






  def 		_a					(self	)		->      int:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*SCREAMING_SNAKE_CASE_	)






  def 		_a					(self	)		->      List[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*SCREAMING_SNAKE_CASE_	)






  @slow
  def 		_a					(self	)		->      Tuple:





       '''simple docstring'''





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

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

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

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



@require_tf
class     _A   (    unittest.TestCase							):






  @slow
  def 		_a					(self	)		->      Any:





       '''simple docstring'''





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

       UpperCamelCase__										=      tf.constant(
           [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]	)  # [CLS] hello, is my dog cute? [SEP]
       UpperCamelCase__										=      model(SCREAMING_SNAKE_CASE_	)[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       UpperCamelCase__										=      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	)	)




from datetime import datetime

import matplotlib.pyplot as plt
import torch







def  __UpperCamelCase    (						A							):
     for param in module.parameters():
          UpperCamelCase__										=      False







def  __UpperCamelCase    (						):
     UpperCamelCase__										=      '''cuda''' if torch.cuda.is_available() else '''cpu'''
     if torch.backends.mps.is_available() and torch.backends.mps.is_built():
          UpperCamelCase__										=      '''mps'''
     if device == "mps":
          print(
              '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch'''
              ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues'''
              ''' with generations.'''							)
     return device







def  __UpperCamelCase    (						A							):
     UpperCamelCase__										=      plt.imshow(A							)
     fig.axes.get_xaxis().set_visible(A							)
     fig.axes.get_yaxis().set_visible(A							)
     plt.show()







def  __UpperCamelCase    (						):
     UpperCamelCase__										=      datetime.now()
     UpperCamelCase__										=      current_time.strftime('''%H:%M:%S'''							)
     return timestamp



