






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					:   str							,   SCREAMING_SNAKE_CASE					:   List[str]							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=13							,   SCREAMING_SNAKE_CASE					:   int=7							,   SCREAMING_SNAKE_CASE					:   List[str]=True							,   SCREAMING_SNAKE_CASE					:   Tuple=True							,   SCREAMING_SNAKE_CASE					:   int=True							,   SCREAMING_SNAKE_CASE					:   Optional[int]=True							,   SCREAMING_SNAKE_CASE					:   List[Any]=99							,   SCREAMING_SNAKE_CASE					:   Optional[int]=32							,   SCREAMING_SNAKE_CASE					:   Any=2							,   SCREAMING_SNAKE_CASE					:   Any=4							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=37							,   SCREAMING_SNAKE_CASE					:   int="gelu"							,   SCREAMING_SNAKE_CASE					:   int=0.1							,   SCREAMING_SNAKE_CASE					:   Tuple=0.1							,   SCREAMING_SNAKE_CASE					:   int=5_12							,   SCREAMING_SNAKE_CASE					:   List[str]=16							,   SCREAMING_SNAKE_CASE					:   Optional[int]=2							,   SCREAMING_SNAKE_CASE					:   Any=0.0_2							,   SCREAMING_SNAKE_CASE					:   Any=3							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=4							,   SCREAMING_SNAKE_CASE					:   Tuple=None							,   SCREAMING_SNAKE_CASE					:   Optional[int]=0							,   ):


      '''simple docstring'''






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







 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






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

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

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

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

      UpperCamelCase__			:   List[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=SCREAMING_SNAKE_CASE							,   initializer_range=self.initializer_range							,   )
      UpperCamelCase__			:   int       				=  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 					__lowercase					(		self					:   Dict							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]							,   SCREAMING_SNAKE_CASE					:   Tuple							,   SCREAMING_SNAKE_CASE					:   str							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]							,   SCREAMING_SNAKE_CASE					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   List[str]							,   SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Union[str, Any]       				=  TFDPRContextEncoder(config=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Tuple       				=  model(SCREAMING_SNAKE_CASE							,   attention_mask=SCREAMING_SNAKE_CASE							,   token_type_ids=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Optional[Any]       				=  model(SCREAMING_SNAKE_CASE							,   token_type_ids=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Union[str, Any]       				=  model(SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(result.pooler_output.shape							,   (self.batch_size, self.projection_dim or self.hidden_size)						)







 def 					__lowercase					(		self					:   Tuple							,   SCREAMING_SNAKE_CASE					:   int							,   SCREAMING_SNAKE_CASE					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Optional[Any]							,   SCREAMING_SNAKE_CASE					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Optional[Any]						):


      '''simple docstring'''






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







 def 					__lowercase					(		self					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   int							,   SCREAMING_SNAKE_CASE					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]							,   SCREAMING_SNAKE_CASE					:   List[str]							,   SCREAMING_SNAKE_CASE					:   Any							,   SCREAMING_SNAKE_CASE					:   Tuple							,   SCREAMING_SNAKE_CASE					:   str						):


      '''simple docstring'''






      UpperCamelCase__			:   List[str]       				=  TFDPRReader(config=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   str       				=  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 					__lowercase					(		self					:   Optional[int]						):


      '''simple docstring'''






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







@require_tf
class 			__a  (    A__       ,   A__       ,   unittest.TestCase       ):
 _lowerCAmelCase       :       Dict        = (
     (
         TFDPRContextEncoder,
         TFDPRQuestionEncoder,
         TFDPRReader,
     )
     if is_tf_available()
     else ()
 )
 _lowerCAmelCase       :       List[Any]        = {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

 _lowerCAmelCase       :       List[str]        = False
 _lowerCAmelCase       :       Union[str, Any]        = False
 _lowerCAmelCase       :       List[str]        = False
 _lowerCAmelCase       :       Optional[Any]        = False
 _lowerCAmelCase       :       List[Any]        = False







 def 					__lowercase					(		self					:   Dict						):


      '''simple docstring'''






      UpperCamelCase__			:   Union[str, Any]       				=  TFDPRModelTester(self						)
      UpperCamelCase__			:   List[str]       				=  ConfigTester(self							,   config_class=SCREAMING_SNAKE_CASE							,   hidden_size=37						)







 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      self.config_tester.run_common_tests()







 def 					__lowercase					(		self					:   int						):


      '''simple docstring'''






      UpperCamelCase__			:   Tuple       				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_context_encoder(*SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   str						):


      '''simple docstring'''






      UpperCamelCase__			:   List[str]       				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_question_encoder(*SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[Any]       				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_reader(*SCREAMING_SNAKE_CASE						)







 @slow
 def 					__lowercase					(		self					:   Optional[int]						):


      '''simple docstring'''






      for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__			:   int       				=  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__			:   str       				=  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__			:   List[str]       				=  TFDPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE						)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE						)

      for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__			:   Union[str, Any]       				=  TFDPRReader.from_pretrained(SCREAMING_SNAKE_CASE						)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE						)







@require_tf
class 			__a  (    unittest.TestCase       ):







 @slow
 def 					__lowercase					(		self					:   Union[str, Any]						):


      '''simple docstring'''






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

      UpperCamelCase__			:   str       				=  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]
      UpperCamelCase__			:   Tuple       				=  model(SCREAMING_SNAKE_CASE						)[0]  # embedding shape = (1, 768)
      # compare the actual values for a slice.
      UpperCamelCase__			:   Optional[Any]       				=  tf.constant(
          [
              [
                  0.0_3_2_3_6_2_5_3,
                  0.1_2_7_5_3_3_3_5,
                  0.1_6_8_1_8_5_0_9,
                  0.0_0_2_7_9_7_8_6,
                  0.3_8_9_6_9_3_3,
                  0.2_4_2_6_4_9_4_5,
                  0.2_1_7_8_9_7_1,
                  -0.0_2_3_3_5_2_2_7,
                  -0.0_8_4_8_1_9_5_9,
                  -0.1_4_3_2_4_1_1_7,
              ]
          ]						)
      self.assertTrue(numpy.allclose(output[:, :10].numpy()							,   expected_slice.numpy()							,   atol=1e-4						)						)







# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.:
#   python ./utils/get_modified_files.py utils src tests examples
#
# it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered
# since the output of this script is fed into Makefile commands it doesn't print a newline after the results

import re
import subprocess
import sys


lowerCamelCase	:							Optional[Any]				=subprocess.check_output('''git merge-base main HEAD'''.split()).decode('''utf-8''')
lowerCamelCase	:							str				=subprocess.check_output(F"""git diff --name-only {fork_point_sha}""".split()).decode('''utf-8''').split()

lowerCamelCase	:							List[Any]				='''|'''.join(sys.argv[1:])
lowerCamelCase	:							str				=re.compile(RF"""^({joined_dirs}).*?\.py$""")

lowerCamelCase	:							Optional[int]				=[x for x in modified_files if regex.match(x)]
print(''' '''.join(relevant_modified_files), end='''''')