

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    : Tuple				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Optional[Any]=1_3				,				UpperCAmelCase__    : Optional[Any]=7				,				UpperCAmelCase__    : Tuple=True				,				UpperCAmelCase__    : Optional[Any]=True				,				UpperCAmelCase__    : Optional[int]=True				,				UpperCAmelCase__    : List[Any]=True				,				UpperCAmelCase__    : Tuple=9_9				,				UpperCAmelCase__    : List[Any]=3_2				,				UpperCAmelCase__    : Any=2				,				UpperCAmelCase__    : int=4				,				UpperCAmelCase__    : Optional[Any]=3_7				,				UpperCAmelCase__    : Dict="gelu"				,				UpperCAmelCase__    : Tuple=0.1				,				UpperCAmelCase__    : Optional[int]=0.1				,				UpperCAmelCase__    : List[Any]=5_1_2				,				UpperCAmelCase__    : Any=1_6				,				UpperCAmelCase__    : List[str]=2				,				UpperCAmelCase__    : Union[str, Any]=0.02				,				UpperCAmelCase__    : Tuple=3				,				UpperCAmelCase__    : Union[str, Any]=4				,				UpperCAmelCase__    : Union[str, Any]=None				,				UpperCAmelCase__    : Union[str, Any]=0				,				)					->Tuple:
        """simple docstring"""







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


  def  _lowercase      (			self    : Dict		)					->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Tuple       =   ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size		)

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

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

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

        SCREAMING_SNAKE_CASE :      Union[str, 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=UpperCAmelCase__				,				initializer_range=self.initializer_range				,				)
        SCREAMING_SNAKE_CASE :      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  _lowercase      (			self    : Dict				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : Dict				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Union[str, Any]		)					->str:
        """simple docstring"""







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


  def  _lowercase      (			self    : Any				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Any		)					->List[str]:
        """simple docstring"""







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


  def  _lowercase      (			self    : Dict				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : Dict				,				UpperCAmelCase__    : Dict				,				UpperCAmelCase__    : List[Any]		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   TFDPRReader(config=UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      str       =   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  _lowercase      (			self    : List[str]		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE
        )     ,	(
            SCREAMING_SNAKE_CASE
        )     ,	(
            SCREAMING_SNAKE_CASE
        )     ,	(
            SCREAMING_SNAKE_CASE
        )     ,	(
            SCREAMING_SNAKE_CASE
        )     ,	(
            SCREAMING_SNAKE_CASE
        )     ,	(
            SCREAMING_SNAKE_CASE
        )     ,	
        ) :      str       =   config_and_inputs
        SCREAMING_SNAKE_CASE :      List[str]       =   {"""input_ids""": input_ids}
        return config, inputs_dict







@require_tf
class        a__       (   UpperCAmelCase	,			UpperCAmelCase	,			unittest.TestCase       ):







  """simple docstring"""




  UpperCAmelCase__      : int			       =(
      (
          TFDPRContextEncoder,
          TFDPRQuestionEncoder,
          TFDPRReader,
      )
      if is_tf_available()
      else ()
  )
  UpperCAmelCase__      : Tuple			       ={"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

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


  def  _lowercase      (			self    : Dict		)					->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Tuple       =   TFDPRModelTester(self		)
        SCREAMING_SNAKE_CASE :      Dict       =   ConfigTester(self				,				config_class=UpperCAmelCase__				,				hidden_size=3_7		)


  def  _lowercase      (			self    : str		)					->Tuple:
        """simple docstring"""







        self.config_tester.run_common_tests()


  def  _lowercase      (			self    : Dict		)					->List[Any]:
        """simple docstring"""







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


  def  _lowercase      (			self    : Optional[int]		)					->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[Any]       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_question_encoder(*UpperCAmelCase__		)


  def  _lowercase      (			self    : int		)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Dict       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_reader(*UpperCAmelCase__		)




  @slow
  def  _lowercase      (			self    : List[Any]		)					->Union[str, Any]:
        """simple docstring"""







        for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE :      str       =   TFDPRContextEncoder.from_pretrained(UpperCAmelCase__		)
              self.assertIsNotNone(UpperCAmelCase__		)

        for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE :      List[Any]       =   TFDPRContextEncoder.from_pretrained(UpperCAmelCase__		)
              self.assertIsNotNone(UpperCAmelCase__		)

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

        for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE :      Optional[Any]       =   TFDPRReader.from_pretrained(UpperCAmelCase__		)
              self.assertIsNotNone(UpperCAmelCase__		)







@require_tf
class        a__       (   unittest.TestCase       ):







  """simple docstring"""




  @slow
  def  _lowercase      (			self    : List[str]		)					->Optional[Any]:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE :      str       =   tf.constant(
            [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]		)  # [CLS] hello, is my dog cute? [SEP]
        SCREAMING_SNAKE_CASE :      Tuple       =   model(UpperCAmelCase__		)[0]  # embedding shape = (1, 768)
        # compare the actual values for a slice.
        SCREAMING_SNAKE_CASE :      Optional[int]       =   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[:, :1_0].numpy()				,				expected_slice.numpy()				,				atol=1e-4		)		)







from math import ceil






def 					__lowercase      (     _A = 1001			)     ->						int:
      SCREAMING_SNAKE_CASE :      Union[str, Any]       =   1

      for i in range(1						,    int(ceil(n / 2.0			)			)			):
            SCREAMING_SNAKE_CASE :      Dict       =   2 * i + 1
            SCREAMING_SNAKE_CASE :      Optional[int]       =   2 * i
            SCREAMING_SNAKE_CASE :      str       =   total + 4 * odd**2 - 6 * even

      return total


if __name__ == "__main__":
  import sys

  if len(sys.argv) == 1:
    print(solution())
  else:
    try:
      UpperCAmelCase__ :     str				    =    int(sys.argv[1])
      print(solution(n))
    except ValueError:
      print("""Invalid entry - please enter a number""")




