


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





				'''simple docstring'''

				def __init__(			self							: Optional[int]						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: int=13						,    UpperCAmelCase_							: int=7						,    UpperCAmelCase_							: Optional[Any]=True						,    UpperCAmelCase_							: Union[str, Any]=True						,    UpperCAmelCase_							: int=True						,    UpperCAmelCase_							: str=True						,    UpperCAmelCase_							: Optional[Any]=99						,    UpperCAmelCase_							: Optional[Any]=32						,    UpperCAmelCase_							: Optional[Any]=2						,    UpperCAmelCase_							: List[Any]=4						,    UpperCAmelCase_							: Optional[Any]=37						,    UpperCAmelCase_							: Union[str, Any]="gelu"						,    UpperCAmelCase_							: Optional[Any]=0.1						,    UpperCAmelCase_							: List[Any]=0.1						,    UpperCAmelCase_							: Dict=512						,    UpperCAmelCase_							: Optional[Any]=16						,    UpperCAmelCase_							: Optional[int]=2						,    UpperCAmelCase_							: int=0.02						,    UpperCAmelCase_							: Any=3						,    UpperCAmelCase_							: int=4						,    UpperCAmelCase_							: Any=None						,    UpperCAmelCase_							: int=0						,    )   ->int:



											"""simple docstring"""



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



				def 						lowerCAmelCase    (			self							: Optional[Any]    )   ->List[str]:



											"""simple docstring"""



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

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

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

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

											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=__lowerCAmelCase						,    initializer_range=self.initializer_range						,    )
											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 						lowerCAmelCase    (			self							: Tuple						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: Optional[int]						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: List[Any]    )   ->Dict:



											"""simple docstring"""



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



				def 						lowerCAmelCase    (			self							: Any						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: Dict						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: Union[str, Any]    )   ->Tuple:



											"""simple docstring"""



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



				def 						lowerCAmelCase    (			self							: List[str]						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: Union[str, Any]						,    UpperCAmelCase_							: Optional[int]						,    UpperCAmelCase_							: int						,    UpperCAmelCase_							: Dict						,    UpperCAmelCase_							: Optional[Any]    )   ->Any:



											"""simple docstring"""



											snake_case_          =						TFDPRReader(config=__lowerCAmelCase    )
											snake_case_          =						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 						lowerCAmelCase    (			self							: List[Any]    )   ->Any:



											"""simple docstring"""



											snake_case_          =						self.prepare_config_and_inputs()
											(
											    (
											    snake_case_       
											)    ,  (
											    snake_case_       
											)    ,  (
											    snake_case_       
											)    ,  (
											    snake_case_       
											)    ,  (
											    snake_case_       
											)    ,  (
											    snake_case_       
											)    ,  (
											    snake_case_       
											)    ,         
											)          =						config_and_inputs
											snake_case_          =						{"""input_ids""": input_ids}
											return config, inputs_dict





@require_tf
class        __A       (_a  ,						_a  ,						unittest.TestCase):





				'''simple docstring'''

				__lowercase:						List[str]							 =      (
				    (
				        TFDPRContextEncoder,
				        TFDPRQuestionEncoder,
				        TFDPRReader,
				    )
				    if is_tf_available()
				    else ()
				)
				__lowercase:						Optional[Any]							 =      {"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

				__lowercase:						str							 =      False
				__lowercase:						Optional[int]							 =      False
				__lowercase:						Any							 =      False
				__lowercase:						Optional[int]							 =      False
				__lowercase:						List[Any]							 =      False



				def 						lowerCAmelCase    (			self							: Dict    )   ->Tuple:



											"""simple docstring"""



											snake_case_          =						TFDPRModelTester(self    )
											snake_case_          =						ConfigTester(self						,    config_class=__lowerCAmelCase						,    hidden_size=37    )



				def 						lowerCAmelCase    (			self							: Optional[Any]    )   ->int:



											"""simple docstring"""



											self.config_tester.run_common_tests()



				def 						lowerCAmelCase    (			self							: Dict    )   ->int:



											"""simple docstring"""



											snake_case_          =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_dpr_context_encoder(*__lowerCAmelCase    )



				def 						lowerCAmelCase    (			self							: Optional[int]    )   ->Tuple:



											"""simple docstring"""



											snake_case_          =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_dpr_question_encoder(*__lowerCAmelCase    )



				def 						lowerCAmelCase    (			self							: Optional[Any]    )   ->Dict:



											"""simple docstring"""



											snake_case_          =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_dpr_reader(*__lowerCAmelCase    )



				@slow
				def 						lowerCAmelCase    (			self							: str    )   ->Optional[int]:



											"""simple docstring"""



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

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

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

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





@require_tf
class        __A       (unittest.TestCase):





				'''simple docstring'''

				@slow
				def 						lowerCAmelCase    (			self							: List[Any]    )   ->Any:



											"""simple docstring"""



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

											snake_case_          =						tf.constant(
											    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]    )  # [CLS] hello, is my dog cute? [SEP]
											snake_case_          =						model(__lowerCAmelCase    )[0]  # embedding shape = (1, 768)
											# compare the actual values for a slice.
											snake_case_          =						tf.constant(
											    [
											        [
											            0.03_236_253,
											            0.12_753_335,
											            0.16_818_509,
											            0.00_279_786,
											            0.3_896_933,
											            0.24_264_945,
											            0.2_178_971,
											            -0.02_335_227,
											            -0.08_481_959,
											            -0.14_324_117,
											        ]
											    ]    )
											self.assertTrue(numpy.allclose(output[:, :10].numpy()						,    expected_slice.numpy()						,    atol=1E-4    )    )







"""simple docstring"""
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__SCREAMING_SNAKE_CASE							:      Dict						=      logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE							:      Tuple						=      {
    'google/mobilenet_v1_1.0_224': 'https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json',
    'google/mobilenet_v1_0.75_192': 'https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}





class        __A       (snake_case__):





							'''simple docstring'''

							__lowercase:						List[Any]							 =      """mobilenet_v1"""



							def __init__(			self							: Union[str, Any]						,    UpperCAmelCase_							: str=3						,    UpperCAmelCase_							: List[Any]=224						,    UpperCAmelCase_							: List[Any]=1.0						,    UpperCAmelCase_							: Any=8						,    UpperCAmelCase_							: int="relu6"						,    UpperCAmelCase_							: str=True						,    UpperCAmelCase_							: List[str]=0.999						,    UpperCAmelCase_							: Optional[Any]=0.02						,    UpperCAmelCase_							: List[Any]=0.001						,    **UpperCAmelCase_							: Any						,    )   ->Union[str, Any]:



														"""simple docstring"""



														super().__init__(**UpperCAmelCase_    )

														if depth_multiplier <= 0:
																					raise ValueError("""depth_multiplier must be greater than zero."""    )

														snake_case_          =						num_channels
														snake_case_          =						image_size
														snake_case_          =						depth_multiplier
														snake_case_          =						min_depth
														snake_case_          =						hidden_act
														snake_case_          =						tf_padding
														snake_case_          =						classifier_dropout_prob
														snake_case_          =						initializer_range
														snake_case_          =						layer_norm_eps





class        __A       (snake_case__):





							'''simple docstring'''

							__lowercase:						int							 =      version.parse("""1.11""")



							@property
							def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->Mapping[str, Mapping[int, str]]:



														"""simple docstring"""



														return OrderedDict([("""pixel_values""", {0: """batch"""})]    )



							@property
							def 						lowerCAmelCase    (			self							: int    )   ->Mapping[str, Mapping[int, str]]:



														"""simple docstring"""



														if self.task == "image-classification":
																					return OrderedDict([("""logits""", {0: """batch"""})]    )
														else:
																					return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]    )



							@property
							def 						lowerCAmelCase    (			self							: int    )   ->float:



														"""simple docstring"""



														return 1E-4



