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






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





				"""simple docstring"""


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






			def 					UpperCAmelCase							(self  :	Union[str, Any]			)       ->	Optional[int]:





				"""simple docstring"""


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

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

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

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

				lowerCAmelCase												=  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=SCREAMING_SNAKE_CASE_      ,initializer_range=self.initializer_range      ,)
				lowerCAmelCase												=  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  :	int      ,SCREAMING_SNAKE_CASE_  :	int      ,SCREAMING_SNAKE_CASE_  :	Optional[int]      ,SCREAMING_SNAKE_CASE_  :	int      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	List[Any]      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]			)       ->	str:





				"""simple docstring"""


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






			def 					UpperCAmelCase							(self  :	str      ,SCREAMING_SNAKE_CASE_  :	Any      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	List[Any]      ,SCREAMING_SNAKE_CASE_  :	Optional[int]      ,SCREAMING_SNAKE_CASE_  :	Dict      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	int			)       ->	Optional[int]:





				"""simple docstring"""


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






			def 					UpperCAmelCase							(self  :	str      ,SCREAMING_SNAKE_CASE_  :	List[str]      ,SCREAMING_SNAKE_CASE_  :	Tuple      ,SCREAMING_SNAKE_CASE_  :	List[str]      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	Optional[int]      ,SCREAMING_SNAKE_CASE_  :	int      ,SCREAMING_SNAKE_CASE_  :	int			)       ->	Optional[int]:





				"""simple docstring"""


				lowerCAmelCase												=  TFDPRReader(config=SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_      ,attention_mask=SCREAMING_SNAKE_CASE_			)

				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  :	Optional[int]			)       ->	str:





				"""simple docstring"""


				lowerCAmelCase												=  self.prepare_config_and_inputs()
				(
				    (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       
				)												=  config_and_inputs
				lowerCAmelCase												=  {'''input_ids''': input_ids}
				return config, inputs_dict





@require_tf
class 			lowercase					( lowercase__   ,lowercase__   ,unittest.TestCase		):
			lowercase        =     (
			    (
			        TFDPRContextEncoder,
			        TFDPRQuestionEncoder,
			        TFDPRReader,
			    )
			    if is_tf_available()
			    else ()
			)
			lowercase        =     {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

			lowercase        =     False
			lowercase        =     False
			lowercase        =     False
			lowercase        =     False
			lowercase        =     False






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





				"""simple docstring"""


				lowerCAmelCase												=  TFDPRModelTester(self			)
				lowerCAmelCase												=  ConfigTester(self      ,config_class=SCREAMING_SNAKE_CASE_      ,hidden_size=37			)






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





				"""simple docstring"""


				self.config_tester.run_common_tests()






			def 					UpperCAmelCase							(self  :	Optional[int]			)       ->	Dict:





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_context_encoder(*SCREAMING_SNAKE_CASE_			)






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





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_question_encoder(*SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Dict			)       ->	Any:





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_reader(*SCREAMING_SNAKE_CASE_			)






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





				"""simple docstring"""


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

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

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

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





@require_tf
class 			lowercase					( unittest.TestCase		):






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





				"""simple docstring"""


				lowerCAmelCase												=  TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''			)

				lowerCAmelCase												=  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]
				lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			)[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				lowerCAmelCase												=  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			)			)




"""simple docstring"""






import warnings

from ...utils import logging
from .image_processing_mobilevit import MobileViTImageProcessor


UpperCAmelCase        =				logging.get_logger(__name__)



class 			lowercase					( lowercase__		):






			def __init__(self  :	Dict      ,*SCREAMING_SNAKE_CASE_  :	str      ,**SCREAMING_SNAKE_CASE_  :	Optional[Any]			)       ->	None:





				"""simple docstring"""


				warnings.warn(
				    '''The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
				    ''' Please use MobileViTImageProcessor instead.'''      ,SCREAMING_SNAKE_CASE_      ,)
				super().__init__(*SCREAMING_SNAKE_CASE_      ,**SCREAMING_SNAKE_CASE_			)


