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



						'''simple docstring'''


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


						def     _SCREAMING_SNAKE_CASE   (			self  :   List[Any]   )  -> Tuple:
							A     = ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size   )

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

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

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

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


						def     _SCREAMING_SNAKE_CASE   (			self  :   Union[str, Any]	,A_  :   List[Any]	,A_  :   Optional[Any]	,A_  :   Tuple	,A_  :   Union[str, Any]	,A_  :   Union[str, Any]	,A_  :   Optional[Any]	,A_  :   Tuple   )  -> List[str]:
							A     = TFDPRQuestionEncoder(config=A_   )
							A     = model(A_	,attention_mask=A_	,token_type_ids=A_   )
							A     = model(A_	,token_type_ids=A_   )
							A     = model(A_   )
							self.parent.assertEqual(result.pooler_output.shape	,(self.batch_size, self.projection_dim or self.hidden_size)   )


						def     _SCREAMING_SNAKE_CASE   (			self  :   Any	,A_  :   Optional[Any]	,A_  :   str	,A_  :   str	,A_  :   Optional[int]	,A_  :   str	,A_  :   str	,A_  :   List[Any]   )  -> List[str]:
							A     = TFDPRReader(config=A_   )
							A     = model(A_	,attention_mask=A_   )

							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     _SCREAMING_SNAKE_CASE   (			self  :   Dict   )  -> Union[str, Any]:
							A     = self.prepare_config_and_inputs()
							(
							    (
							    A
							) ,   (
							    A
							) ,   (
							    A
							) ,   (
							    A
							) ,   (
							    A
							) ,   (
							    A
							) ,   (
							    A
							) ,   
							)     = config_and_inputs
							A     = {'input_ids': input_ids}
							return config, inputs_dict







@require_tf
class 					lowerCAmelCase_					(   _lowercase		,       _lowercase		,       unittest.TestCase     ):



						'''simple docstring'''


						_lowerCamelCase:     Any   =     (
						    (
						        TFDPRContextEncoder,
						        TFDPRQuestionEncoder,
						        TFDPRReader,
						    )
						    if is_tf_available()
						    else ()
						)
						_lowerCamelCase:     Any   =     {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

						_lowerCamelCase:     Tuple   =     False
						_lowerCamelCase:     str   =     False
						_lowerCamelCase:     int   =     False
						_lowerCamelCase:     List[Any]   =     False
						_lowerCamelCase:     Union[str, Any]   =     False


						def     _SCREAMING_SNAKE_CASE   (			self  :   Dict   )  -> Dict:
							A     = TFDPRModelTester(self   )
							A     = ConfigTester(self	,config_class=A_	,hidden_size=37   )


						def     _SCREAMING_SNAKE_CASE   (			self  :   int   )  -> Any:
							self.config_tester.run_common_tests()


						def     _SCREAMING_SNAKE_CASE   (			self  :   List[Any]   )  -> List[Any]:
							A     = self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_context_encoder(*A_   )


						def     _SCREAMING_SNAKE_CASE   (			self  :   Dict   )  -> Optional[int]:
							A     = self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_question_encoder(*A_   )


						def     _SCREAMING_SNAKE_CASE   (			self  :   Optional[Any]   )  -> int:
							A     = self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_dpr_reader(*A_   )




						@slow
						def     _SCREAMING_SNAKE_CASE   (			self  :   Dict   )  -> int:
							for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								A     = TFDPRContextEncoder.from_pretrained(A_   )
								self.assertIsNotNone(A_   )

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

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

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







@require_tf
class 					lowerCAmelCase_					(   unittest.TestCase     ):



						'''simple docstring'''


						@slow
						def     _SCREAMING_SNAKE_CASE   (			self  :   Optional[Any]   )  -> Any:
							A     = TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'   )

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



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE						:str			=  {'''configuration_unispeech''': ['''UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''UniSpeechConfig''']}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   SCREAMING_SNAKE_CASE						:List[str]			=  [
       '''UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''UniSpeechForCTC''',
       '''UniSpeechForPreTraining''',
       '''UniSpeechForSequenceClassification''',
       '''UniSpeechModel''',
       '''UniSpeechPreTrainedModel''',
   ]

if TYPE_CHECKING:
   from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_unispeech import (
          UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST,
          UniSpeechForCTC,
          UniSpeechForPreTraining,
          UniSpeechForSequenceClassification,
          UniSpeechModel,
          UniSpeechPreTrainedModel,
      )

else:
   import sys

   SCREAMING_SNAKE_CASE						:List[str]			=  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




