


"""simple docstring"""


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 				_SCREAMING_SNAKE_CASE				:
   """simple docstring"""



   def __init__(						self ,     lowerCamelCase__ ,     lowerCamelCase__=13 ,     lowerCamelCase__=7 ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__=99 ,     lowerCamelCase__=32 ,     lowerCamelCase__=2 ,     lowerCamelCase__=4 ,     lowerCamelCase__=37 ,     lowerCamelCase__="gelu" ,     lowerCamelCase__=0.1 ,     lowerCamelCase__=0.1 ,     lowerCamelCase__=512 ,     lowerCamelCase__=16 ,     lowerCamelCase__=2 ,     lowerCamelCase__=0.02 ,     lowerCamelCase__=3 ,     lowerCamelCase__=4 ,     lowerCamelCase__=None ,     lowerCamelCase__=0 ,     )      ->							List[str]:
    lowercase__       :       Optional[Any]				=						parent
    lowercase__       :       Any				=						batch_size
    lowercase__       :       int				=						seq_length
    lowercase__       :       List[Any]				=						is_training
    lowercase__       :       Optional[int]				=						use_input_mask
    lowercase__       :       Optional[int]				=						use_token_type_ids
    lowercase__       :       Tuple				=						use_labels
    lowercase__       :       Tuple				=						vocab_size
    lowercase__       :       Dict				=						hidden_size
    lowercase__       :       Any				=						num_hidden_layers
    lowercase__       :       Any				=						num_attention_heads
    lowercase__       :       Tuple				=						intermediate_size
    lowercase__       :       Dict				=						hidden_act
    lowercase__       :       List[Any]				=						hidden_dropout_prob
    lowercase__       :       List[str]				=						attention_probs_dropout_prob
    lowercase__       :       str				=						max_position_embeddings
    lowercase__       :       Dict				=						type_vocab_size
    lowercase__       :       int				=						type_sequence_label_size
    lowercase__       :       Any				=						initializer_range
    lowercase__       :       Tuple				=						num_labels
    lowercase__       :       int				=						num_choices
    lowercase__       :       List[Any]				=						scope
    lowercase__       :       str				=						projection_dim


   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    lowercase__       :       Dict				=						ids_tensor([self.batch_size, self.seq_length] ,     self.vocab_size						)

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

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

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

    lowercase__       :       List[str]				=						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=lowerCamelCase__ ,     initializer_range=self.initializer_range ,     )
    lowercase__       :       Union[str, Any]				=						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 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							int:
    lowercase__       :       Tuple				=						TFDPRContextEncoder(config=lowerCamelCase__						)
    lowercase__       :       Any				=						model(lowerCamelCase__ ,     attention_mask=lowerCamelCase__ ,     token_type_ids=lowerCamelCase__						)
    lowercase__       :       int				=						model(lowerCamelCase__ ,     token_type_ids=lowerCamelCase__						)
    lowercase__       :       Optional[int]				=						model(lowerCamelCase__						)
    self.parent.assertEqual(result.pooler_output.shape ,     (self.batch_size, self.projection_dim or self.hidden_size)						)


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							str:
    lowercase__       :       str				=						TFDPRQuestionEncoder(config=lowerCamelCase__						)
    lowercase__       :       List[Any]				=						model(lowerCamelCase__ ,     attention_mask=lowerCamelCase__ ,     token_type_ids=lowerCamelCase__						)
    lowercase__       :       List[Any]				=						model(lowerCamelCase__ ,     token_type_ids=lowerCamelCase__						)
    lowercase__       :       Union[str, Any]				=						model(lowerCamelCase__						)
    self.parent.assertEqual(result.pooler_output.shape ,     (self.batch_size, self.projection_dim or self.hidden_size)						)


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							List[Any]:
    lowercase__       :       int				=						TFDPRReader(config=lowerCamelCase__						)
    lowercase__       :       Optional[Any]				=						model(lowerCamelCase__ ,     attention_mask=lowerCamelCase__						)

    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 					UpperCAmelCase__(						self						)      ->							Dict:
    lowercase__       :       str				=						self.prepare_config_and_inputs()
    (
        (
        lowercase__
    )	,       (
        lowercase__
    )	,       (
        lowercase__
    )	,       (
        lowercase__
    )	,       (
        lowercase__
    )	,       (
        lowercase__
    )	,       (
        lowercase__
    )	,       
    )       :       Dict				=						config_and_inputs
    lowercase__       :       List[Any]				=						{"""input_ids""": input_ids}
    return config, inputs_dict

@require_tf
class 				_SCREAMING_SNAKE_CASE				(							__UpperCAmelCase     ,   __UpperCAmelCase     ,   unittest.TestCase  ):
   """simple docstring"""



   _a   :       Optional[int]									=    (
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   _a   :       Union[str, Any]									=    {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

   _a   :       List[str]									=    False
   _a   :       List[Any]									=    False
   _a   :       int									=    False
   _a   :       Optional[Any]									=    False
   _a   :       Dict									=    False


   def 					UpperCAmelCase__(						self						)      ->							List[str]:
    lowercase__       :       str				=						TFDPRModelTester(self						)
    lowercase__       :       Tuple				=						ConfigTester(self ,     config_class=lowerCamelCase__ ,     hidden_size=37						)


   def 					UpperCAmelCase__(						self						)      ->							Tuple:
    self.config_tester.run_common_tests()


   def 					UpperCAmelCase__(						self						)      ->							Optional[Any]:
    lowercase__       :       int				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_dpr_context_encoder(*lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							List[str]:
    lowercase__       :       str				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_dpr_question_encoder(*lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    lowercase__       :       List[Any]				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_dpr_reader(*lowerCamelCase__						)







   @slow
   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowercase__       :       Dict				=						TFDPRContextEncoder.from_pretrained(lowerCamelCase__						)
     self.assertIsNotNone(lowerCamelCase__						)

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

    for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowercase__       :       List[Any]				=						TFDPRQuestionEncoder.from_pretrained(lowerCamelCase__						)
     self.assertIsNotNone(lowerCamelCase__						)

    for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowercase__       :       int				=						TFDPRReader.from_pretrained(lowerCamelCase__						)
     self.assertIsNotNone(lowerCamelCase__						)

@require_tf
class 				_SCREAMING_SNAKE_CASE				(							unittest.TestCase  ):
   """simple docstring"""



   @slow
   def 					UpperCAmelCase__(						self						)      ->							int:
    lowercase__       :       Dict				=						TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""						)

    lowercase__       :       Optional[int]				=						tf.constant(
        [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]						)  # [CLS] hello, is my dog cute? [SEP]
    lowercase__       :       Dict				=						model(lowerCamelCase__						)[0]  # embedding shape = (1, 768)
    # compare the actual values for a slice.
    lowercase__       :       List[str]				=						tf.constant(
        [
            [
                0.0323_6253,
                0.1275_3335,
                0.1681_8509,
                0.0027_9786,
                0.389_6933,
                0.2426_4945,
                0.217_8971,
                -0.0233_5227,
                -0.0848_1959,
                -0.1432_4117,
            ]
        ]						)
    self.assertTrue(numpy.allclose(output[:, :10].numpy() ,     expected_slice.numpy() ,     atol=1E-4						)						)



"""simple docstring"""


from ..utils import DummyObject, requires_backends

class 				_SCREAMING_SNAKE_CASE				(							metaclass=__UpperCAmelCase  ):
   """simple docstring"""



   _a   :       Optional[int]									=    ['''note_seq''']


   def __init__(						self ,     *lowerCamelCase__ ,     **lowerCamelCase__						)      ->							Tuple:
    requires_backends(self ,     ["""note_seq"""]						)


   @classmethod
   def 					UpperCAmelCase__(						cls ,     *lowerCamelCase__ ,     **lowerCamelCase__						)      ->							Tuple:
    requires_backends(cls ,     ["""note_seq"""]						)







   @classmethod
   def 					UpperCAmelCase__(						cls ,     *lowerCamelCase__ ,     **lowerCamelCase__						)      ->							Tuple:
    requires_backends(cls ,     ["""note_seq"""]						)