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




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



     """simple docstring"""






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




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






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

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

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

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

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



     """simple docstring"""






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




   def      __a						(   self      :    Dict	,     _lowercase      :    Dict	,     _lowercase      :    Any	,     _lowercase      :    List[str]	,     _lowercase      :    str	,     _lowercase      :    Tuple	,     _lowercase      :    List[Any]	,     _lowercase      :    List[Any]				):



     """simple docstring"""






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




   def      __a						(   self      :    Any	,     _lowercase      :    Optional[int]	,     _lowercase      :    Any	,     _lowercase      :    Dict	,     _lowercase      :    str	,     _lowercase      :    Optional[Any]	,     _lowercase      :    Any	,     _lowercase      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFDPRReader(config=_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     attention_mask=_lowercase				)

     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"""






     SCREAMING_SNAKE_CASE__      							=				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__
     )							,					
     )      							=				config_and_inputs
     SCREAMING_SNAKE_CASE__      							=				{"""input_ids""": input_ids}
     return config, inputs_dict







@require_tf
class __snake_case    (						lowerCamelCase_       ,		lowerCamelCase_       ,		unittest.TestCase		):
   lowerCAmelCase_														=							(
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   lowerCAmelCase_														=							{"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFDPRModelTester(self				)
     SCREAMING_SNAKE_CASE__      							=				ConfigTester(self	,     config_class=_lowercase	,     hidden_size=37				)




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






     self.config_tester.run_common_tests()




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_dpr_context_encoder(*_lowercase				)




   def      __a						(   self      :    int				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_dpr_question_encoder(*_lowercase				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_dpr_reader(*_lowercase				)




   @slow
   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






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

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

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

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







@require_tf
class __snake_case    (						unittest.TestCase		):




   @slow
   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""				)

     SCREAMING_SNAKE_CASE__      							=				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]
     SCREAMING_SNAKE_CASE__      							=				model(_lowercase				)[0]  # embedding shape = (1, 768)
     # compare the actual values for a slice.
     SCREAMING_SNAKE_CASE__      							=				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[:, :10].numpy()	,     expected_slice.numpy()	,     atol=1E-4				)				)

import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)







class __snake_case    (						unittest.TestCase		):




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     self.assertEqual(x.component(0				)	,     1				)
     self.assertEqual(x.component(2				)	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Vector()




   def      __a						(   self      :    int				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([0, 0, 0, 0, 0, 1]				)
     self.assertEqual(str(_lowercase				)	,     """(0,0,0,0,0,1)"""				)




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3, 4]				)
     self.assertEqual(len(_lowercase				)	,     4				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3, 4, 5]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, -1, 1, -1, 2, -3, 4, -5]				)
     self.assertAlmostEqual(x.euclidean_length()	,     2.2_36	,     3				)
     self.assertAlmostEqual(y.euclidean_length()	,     7.4_16	,     3				)
     self.assertEqual(z.euclidean_length()	,     0				)
     self.assertAlmostEqual(w.euclidean_length()	,     7.6_16	,     3				)




   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 1, 1]				)
     self.assertEqual((x + y).component(0				)	,     2				)
     self.assertEqual((x + y).component(1				)	,     3				)
     self.assertEqual((x + y).component(2				)	,     4				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 1, 1]				)
     self.assertEqual((x - y).component(0				)	,     0				)
     self.assertEqual((x - y).component(1				)	,     1				)
     self.assertEqual((x - y).component(2				)	,     2				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([2, -1, 4]				)  # for test of dot product
     SCREAMING_SNAKE_CASE__      							=				Vector([1, -2, -1]				)
     self.assertEqual(str(x * 3.0				)	,     """(3.0,6.0,9.0)"""				)
     self.assertEqual((a * b)	,     0				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     self.assertEqual(str(zero_vector(10				)				).count("""0"""				)	,     10				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     self.assertEqual(str(unit_basis_vector(3	,     1				)				)	,     """(0,1,0)"""				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 1]				)
     self.assertEqual(str(axpy(2	,     _lowercase	,     _lowercase				)				)	,     """(3,4,7)"""				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 0, 0, 0, 0]				)
     SCREAMING_SNAKE_CASE__      							=				x.copy()
     self.assertEqual(str(_lowercase				)	,     str(_lowercase				)				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 0]				)
     x.change_component(0	,     0				)
     x.change_component(1	,     1				)
     self.assertEqual(str(_lowercase				)	,     """(0,1,0)"""				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n"""	,     str(_lowercase				)				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
     for x in range(a.height()				):
       for y in range(a.width()				):
         self.assertEqual(minors[x][y]	,     a.minor(_lowercase	,     _lowercase				)				)




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
     for x in range(a.height()				):
       for y in range(a.width()				):
         self.assertEqual(cofactors[x][y]	,     a.cofactor(_lowercase	,     _lowercase				)				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual(-5	,     a.determinant()				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     self.assertEqual("""(14,32,50)"""	,     str(a * x				)				)
     self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n"""	,     str(a * 2				)				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     a.change_component(0	,     2	,     5				)
     self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n"""	,     str(_lowercase				)				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual(7	,     a.component(2	,     1				)	,     0.01				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]	,     3	,     3				)
     self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n"""	,     str(a + b				)				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]	,     3	,     3				)
     self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n"""	,     str(a - b				)				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     self.assertEqual(
         """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n"""	,     str(square_zero_matrix(5				)				)	,     )


if __name__ == "__main__":
   unittest.main()
