

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
			from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import MobileViTImageProcessor






class _UpperCamelCase				(				__A ):
			'''simple docstring'''







			def 				__UpperCamelCase			(      self				:			List[str]						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							self.config_class(**self.inputs_dict						)
				self.parent.assertTrue(hasattr(a   ,   "hidden_sizes"						)						)
				self.parent.assertTrue(hasattr(a   ,   "neck_hidden_sizes"						)						)
				self.parent.assertTrue(hasattr(a   ,   "num_attention_heads"						)						)






class _UpperCamelCase				:
			'''simple docstring'''







			def __init__(      self				:			int   ,   a				:			str   ,   a				:			Union[str, Any]=13   ,   a				:			Dict=32   ,   a				:			Optional[Any]=2   ,   a				:			str=3   ,   a				:			Optional[Any]=640   ,   a				:			List[str]=4   ,   a				:			Optional[int]="silu"   ,   a				:			Optional[int]=3   ,   a				:			str=32   ,   a				:			List[Any]=0.1   ,   a				:			Optional[Any]=0.1   ,   a				:			Optional[int]=0.1   ,   a				:			Any=0.02   ,   a				:			int=True   ,   a				:			Dict=True   ,   a				:			Dict=10   ,   a				:			Optional[int]=None   ,   )				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							parent
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							batch_size
				SCREAMING_SNAKE_CASE :   Any					  =							image_size
				SCREAMING_SNAKE_CASE :   Dict					  =							patch_size
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							num_channels
				SCREAMING_SNAKE_CASE :   int					  =							last_hidden_size
				SCREAMING_SNAKE_CASE :   str					  =							num_attention_heads
				SCREAMING_SNAKE_CASE :   Tuple					  =							hidden_act
				SCREAMING_SNAKE_CASE :   Tuple					  =							conv_kernel_size
				SCREAMING_SNAKE_CASE :   List[Any]					  =							output_stride
				SCREAMING_SNAKE_CASE :   List[str]					  =							hidden_dropout_prob
				SCREAMING_SNAKE_CASE :   List[str]					  =							attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE :   List[Any]					  =							classifier_dropout_prob
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							use_labels
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							is_training
				SCREAMING_SNAKE_CASE :   Any					  =							num_labels
				SCREAMING_SNAKE_CASE :   Dict					  =							initializer_range
				SCREAMING_SNAKE_CASE :   List[Any]					  =							scope
			def 				__UpperCamelCase			(      self				:			Dict						)				->							Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

				SCREAMING_SNAKE_CASE :   Any					  =							None
				SCREAMING_SNAKE_CASE :   Any					  =							None
				if self.use_labels:
					SCREAMING_SNAKE_CASE :   Dict					  =							ids_tensor([self.batch_size]   ,   self.num_labels						)
					SCREAMING_SNAKE_CASE :   Optional[int]					  =							ids_tensor([self.batch_size, self.image_size, self.image_size]   ,   self.num_labels						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							self.get_config()

				return config, pixel_values, labels, pixel_labels
			def 				__UpperCamelCase			(      self				:			Optional[Any]						)				->							Optional[Any]:

				"""simple docstring"""

				return MobileViTConfig(
				    image_size=self.image_size   ,   patch_size=self.patch_size   ,   num_channels=self.num_channels   ,   num_attention_heads=self.num_attention_heads   ,   hidden_act=self.hidden_act   ,   conv_kernel_size=self.conv_kernel_size   ,   output_stride=self.output_stride   ,   hidden_dropout_prob=self.hidden_dropout_prob   ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,   classifier_dropout_prob=self.classifier_dropout_prob   ,   initializer_range=self.initializer_range   ,   )
			def 				__UpperCamelCase			(      self				:			Optional[Any]   ,   a				:			Optional[Any]   ,   a				:			Optional[int]   ,   a				:			Optional[int]   ,   a				:			Tuple						)				->							Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							MobileViTModel(config=a						)
				model.to(a						)
				model.eval()
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							model(a						)
				self.parent.assertEqual(
				    result.last_hidden_state.shape   ,   (
				        self.batch_size,
				        self.last_hidden_size,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )   ,   )
			def 				__UpperCamelCase			(      self				:			Any   ,   a				:			Tuple   ,   a				:			Any   ,   a				:			List[Any]   ,   a				:			Union[str, Any]						)				->							Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   str					  =							self.num_labels
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							MobileViTForImageClassification(a						)
				model.to(a						)
				model.eval()
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							model(a   ,   labels=a						)
				self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.num_labels)						)
			def 				__UpperCamelCase			(      self				:			Optional[int]   ,   a				:			List[str]   ,   a				:			Tuple   ,   a				:			Optional[int]   ,   a				:			Dict						)				->							int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.num_labels
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							MobileViTForSemanticSegmentation(a						)
				model.to(a						)
				model.eval()
				SCREAMING_SNAKE_CASE :   Tuple					  =							model(a						)
				self.parent.assertEqual(
				    result.logits.shape   ,   (
				        self.batch_size,
				        self.num_labels,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )   ,   )
				SCREAMING_SNAKE_CASE :   Tuple					  =							model(a   ,   labels=a						)
				self.parent.assertEqual(
				    result.logits.shape   ,   (
				        self.batch_size,
				        self.num_labels,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )   ,   )






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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   List[str]					  =							config_and_inputs
				SCREAMING_SNAKE_CASE :   Any					  =							{"pixel_values": pixel_values}
				return config, inputs_dict






@require_torch
class _UpperCamelCase				(				__A				,						__A				,						unittest.TestCase ):
			'''simple docstring'''







			lowerCamelCase__							     =(
			    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__							     =(
			    {
			        'feature-extraction': MobileViTModel,
			        'image-classification': MobileViTForImageClassification,
			        'image-segmentation': MobileViTForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			lowerCamelCase__							     =False
			lowerCamelCase__							     =False
			lowerCamelCase__							     =False
			lowerCamelCase__							     =False
			def 				__UpperCamelCase			(      self				:			str						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							MobileViTModelTester(self						)
				SCREAMING_SNAKE_CASE :   Tuple					  =							MobileViTConfigTester(self   ,   config_class=a   ,   has_text_modality=a						)
			def 				__UpperCamelCase			(      self				:			Tuple						)				->							Dict:

				"""simple docstring"""

				self.config_tester.run_common_tests()
			@unittest.skip(reason="MobileViT does not use inputs_embeds"						)
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							int:

				"""simple docstring"""

				pass
			@unittest.skip(reason="MobileViT does not support input and output embeddings"						)
			def 				__UpperCamelCase			(      self				:			Any						)				->							Tuple:

				"""simple docstring"""

				pass
			@unittest.skip(reason="MobileViT does not output attentions"						)
			def 				__UpperCamelCase			(      self				:			Union[str, Any]						)				->							str:

				"""simple docstring"""

				pass
			def 				__UpperCamelCase			(      self				:			Any						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   Dict					  =							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE :   Tuple					  =							model_class(a						)
					SCREAMING_SNAKE_CASE :   str					  =							inspect.signature(model.forward						)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					SCREAMING_SNAKE_CASE :   Dict					  =							[*signature.parameters.keys()]

					SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							["pixel_values"]
					self.assertListEqual(arg_names[:1]   ,   a						)
			@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
			def 				__UpperCamelCase			(      self				:			Optional[Any]						)				->							str:

				"""simple docstring"""

				pass
			def 				__UpperCamelCase			(      self				:			Tuple						)				->							List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Tuple					  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*a						)
			def 				__UpperCamelCase			(      self				:			str						)				->							Tuple:

				"""simple docstring"""

				def check_hidden_states_output(a				:			Tuple   ,   a				:			Tuple   ,   a				:			Optional[Any]						):
					SCREAMING_SNAKE_CASE :   str					  =							model_class(a						)
					model.to(a						)
					model.eval()

					with torch.no_grad():
						SCREAMING_SNAKE_CASE :   List[Any]					  =							model(**self._prepare_for_class(a   ,   a						)						)

					SCREAMING_SNAKE_CASE :   List[str]					  =							outputs.hidden_states

					SCREAMING_SNAKE_CASE :   Optional[Any]					  =							5
					self.assertEqual(len(a						)   ,   a						)

					# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
					# with the width and height being successively divided by 2.
					SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							2
					for i in range(len(a						)						):
						self.assertListEqual(
						    list(hidden_states[i].shape[-2:]						)   ,   [self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]   ,   )
						divisor *= 2

					self.assertEqual(self.model_tester.output_stride   ,   divisor // 2						)

				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   Tuple					  =							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE :   Dict					  =							True
					check_hidden_states_output(a   ,   a   ,   a						)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					SCREAMING_SNAKE_CASE :   List[Any]					  =							True

					check_hidden_states_output(a   ,   a   ,   a						)
			def 				__UpperCamelCase			(      self				:			Tuple						)				->							int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*a						)
			def 				__UpperCamelCase			(      self				:			str						)				->							Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*a						)






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

				"""simple docstring"""

				for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE :   List[Any]					  =							MobileViTModel.from_pretrained(a						)
					self.assertIsNotNone(a						)

def 			lowerCamelCase__			(       ):
	SCREAMING_SNAKE_CASE :   Tuple					  =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
	return image






@require_torch
@require_vision
class _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			@cached_property
			def 				__UpperCamelCase			(      self				:			str						)				->							List[Any]:

				"""simple docstring"""

				return MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"						) if is_vision_available() else None
			@slow
			def 				__UpperCamelCase			(      self				:			Dict						)				->							Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							MobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"						).to(a						)

				SCREAMING_SNAKE_CASE :   Dict					  =							self.default_image_processor
				SCREAMING_SNAKE_CASE :   List[Any]					  =							prepare_img()
				SCREAMING_SNAKE_CASE :   str					  =							image_processor(images=a   ,   return_tensors="pt"						).to(a						)

				# forward pass
				with torch.no_grad():
					SCREAMING_SNAKE_CASE :   List[str]					  =							model(**a						)

				# verify the logits
				SCREAMING_SNAKE_CASE :   Tuple					  =							torch.Size((1, 1000)						)
				self.assertEqual(outputs.logits.shape   ,   a						)

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							torch.tensor([-1.9364, -1.2327, -0.4653]						).to(a						)

				self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,   a   ,   atol=1e-4						)						)
			@slow
			def 				__UpperCamelCase			(      self				:			int						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   List[Any]					  =							MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"						)
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							model.to(a						)

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"						)

				SCREAMING_SNAKE_CASE :   Tuple					  =							prepare_img()
				SCREAMING_SNAKE_CASE :   Tuple					  =							image_processor(images=a   ,   return_tensors="pt"						).to(a						)

				# forward pass
				with torch.no_grad():
					SCREAMING_SNAKE_CASE :   Optional[int]					  =							model(**a						)
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							outputs.logits

				# verify the logits
				SCREAMING_SNAKE_CASE :   List[Any]					  =							torch.Size((1, 21, 32, 32)						)
				self.assertEqual(logits.shape   ,   a						)

				SCREAMING_SNAKE_CASE :   Any					  =							torch.tensor(
				    [
				        [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
				        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
				        [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
				    ]   ,   device=a   ,   )

				self.assertTrue(torch.allclose(logits[0, :3, :3, :3]   ,   a   ,   atol=1e-4						)						)






			@slow
			def 				__UpperCamelCase			(      self				:			Dict						)				->							List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"						)
				SCREAMING_SNAKE_CASE :   List[str]					  =							model.to(a						)

				SCREAMING_SNAKE_CASE :   Dict					  =							MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"						)

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							prepare_img()
				SCREAMING_SNAKE_CASE :   List[str]					  =							image_processor(images=a   ,   return_tensors="pt"						).to(a						)

				# forward pass
				with torch.no_grad():
					SCREAMING_SNAKE_CASE :   Optional[int]					  =							model(**a						)

				SCREAMING_SNAKE_CASE :   int					  =							outputs.logits.detach().cpu()

				SCREAMING_SNAKE_CASE :   int					  =							image_processor.post_process_semantic_segmentation(outputs=a   ,   target_sizes=[(50, 60)]						)
				SCREAMING_SNAKE_CASE :   List[str]					  =							torch.Size((50, 60)						)
				self.assertEqual(segmentation[0].shape   ,   a						)

				SCREAMING_SNAKE_CASE :   int					  =							image_processor.post_process_semantic_segmentation(outputs=a						)
				SCREAMING_SNAKE_CASE :   Tuple					  =							torch.Size((32, 32)						)
				self.assertEqual(segmentation[0].shape   ,   a						)





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


				def __init__(						self       :   Any			,							lowercase_       :   int			,							lowercase_       :   str=13			,							lowercase_       :   Union[str, Any]=7			,							lowercase_       :   Any=True			,							lowercase_       :   List[str]=True			,							lowercase_       :   Tuple=True			,							lowercase_       :   List[Any]=True			,							lowercase_       :   str=99			,							lowercase_       :   int=32			,							lowercase_       :   int=2			,							lowercase_       :   Any=4			,							lowercase_       :   Union[str, Any]=37			,							lowercase_       :   Optional[int]="gelu"			,							lowercase_       :   Dict=0.1			,							lowercase_       :   Optional[Any]=0.1			,							lowercase_       :   Union[str, Any]=512			,							lowercase_       :   Dict=16			,							lowercase_       :   int=2			,							lowercase_       :   Any=0.02			,							lowercase_       :   int=3			,							lowercase_       :   Any=4			,							lowercase_       :   List[Any]=None			,							lowercase_       :   List[str]=0			,							):
									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     A_					(						self       :   int   ):
									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=lowercase_			,							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     A_					(						self       :   Dict			,							lowercase_       :   Optional[Any]			,							lowercase_       :   int			,							lowercase_       :   Optional[int]			,							lowercase_       :   int			,							lowercase_       :   int			,							lowercase_       :   Union[str, Any]			,							lowercase_       :   str   ):
									snake_case_		     =							TFDPRContextEncoder(config=lowercase_   )
									snake_case_		     =							model(lowercase_			,							attention_mask=lowercase_			,							token_type_ids=lowercase_   )
									snake_case_		     =							model(lowercase_			,							token_type_ids=lowercase_   )
									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[Any]			,							lowercase_       :   List[str]			,							lowercase_       :   Union[str, Any]			,							lowercase_       :   Dict			,							lowercase_       :   Dict			,							lowercase_       :   Dict			,							lowercase_       :   Any   ):
									snake_case_		     =							TFDPRQuestionEncoder(config=lowercase_   )
									snake_case_		     =							model(lowercase_			,							attention_mask=lowercase_			,							token_type_ids=lowercase_   )
									snake_case_		     =							model(lowercase_			,							token_type_ids=lowercase_   )
									snake_case_		     =							model(lowercase_   )
									self.parent.assertEqual(result.pooler_output.shape			,							(self.batch_size, self.projection_dim or self.hidden_size)   )


				def     A_					(						self       :   str			,							lowercase_       :   Tuple			,							lowercase_       :   Tuple			,							lowercase_       :   Tuple			,							lowercase_       :   Tuple			,							lowercase_       :   Tuple			,							lowercase_       :   Dict			,							lowercase_       :   Dict   ):
									snake_case_		     =							TFDPRReader(config=lowercase_   )
									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       :   Union[str, Any]   ):
									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							(  _lowerCamelCase  ,	_lowerCamelCase  ,	unittest.TestCase	):
				snake_case_           =						(
				    (
				        TFDPRContextEncoder,
				        TFDPRQuestionEncoder,
				        TFDPRReader,
				    )
				    if is_tf_available()
				    else ()
				)
				snake_case_           =						{"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

				snake_case_           =						False
				snake_case_           =						False
				snake_case_           =						False
				snake_case_           =						False
				snake_case_           =						False


				def     A_					(						self       :   List[Any]   ):
									snake_case_		     =							TFDPRModelTester(self   )
									snake_case_		     =							ConfigTester(self			,							config_class=lowercase_			,							hidden_size=37   )


				def     A_					(						self       :   Any   ):
									self.config_tester.run_common_tests()


				def     A_					(						self       :   int   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_context_encoder(*lowercase_   )


				def     A_					(						self       :   List[Any]   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_question_encoder(*lowercase_   )


				def     A_					(						self       :   Any   ):
									snake_case_		     =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_reader(*lowercase_   )


				@slow
				def     A_					(						self       :   int   ):
									for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														snake_case_		     =							TFDPRContextEncoder.from_pretrained(lowercase_   )
														self.assertIsNotNone(lowercase_   )

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

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

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







@require_tf
class 		a							(  unittest.TestCase	):


				@slow
				def     A_					(						self       :   Dict   ):
									snake_case_		     =							TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''   )

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

