






"""simple docstring"""


import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
       import torch
       from torch import nn

       from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
       from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import DPTImageProcessor


class    snake_case						:


   '''simple docstring'''

   def __init__(      self					:	int,			_lowerCamelCase					:	Union[str, Any],			_lowerCamelCase					:	Dict=2,			_lowerCamelCase					:	List[str]=32,			_lowerCamelCase					:	Optional[Any]=16,			_lowerCamelCase					:	Optional[Any]=3,			_lowerCamelCase					:	str=True,			_lowerCamelCase					:	Any=True,			_lowerCamelCase					:	Optional[Any]=32,			_lowerCamelCase					:	Any=4,			_lowerCamelCase					:	str=[0, 1, 2, 3],			_lowerCamelCase					:	Any=4,			_lowerCamelCase					:	Dict=37,			_lowerCamelCase					:	Any="gelu",			_lowerCamelCase					:	List[Any]=0.1,			_lowerCamelCase					:	Optional[int]=0.1,			_lowerCamelCase					:	Union[str, Any]=0.02,			_lowerCamelCase					:	str=3,			_lowerCamelCase					:	Tuple=False,			_lowerCamelCase					:	int=None,			):

     '''simple docstring'''
     __A		     =					parent
     __A		     =					batch_size
     __A		     =					image_size
     __A		     =					patch_size
     __A		     =					num_channels
     __A		     =					is_training
     __A		     =					use_labels
     __A		     =					hidden_size
     __A		     =					num_hidden_layers
     __A		     =					backbone_out_indices
     __A		     =					num_attention_heads
     __A		     =					intermediate_size
     __A		     =					hidden_act
     __A		     =					hidden_dropout_prob
     __A		     =					attention_probs_dropout_prob
     __A		     =					initializer_range
     __A		     =					num_labels
     __A		     =					scope
     __A		     =					is_hybrid
     # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
     __A		     =					(image_size // patch_size) ** 2
     __A		     =					num_patches + 1


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict    ):

     '''simple docstring'''
     __A		     =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

     __A		     =					None
     if self.use_labels:
       __A		     =					ids_tensor([self.batch_size, self.image_size, self.image_size],			self.num_labels    )

     __A		     =					self.get_config()

     return config, pixel_values, labels


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[Any]    ):

     '''simple docstring'''
     return DPTConfig(
         image_size=self.image_size,			patch_size=self.patch_size,			num_channels=self.num_channels,			hidden_size=self.hidden_size,			num_hidden_layers=self.num_hidden_layers,			backbone_out_indices=self.backbone_out_indices,			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,			is_decoder=_lowerCamelCase,			initializer_range=self.initializer_range,			is_hybrid=self.is_hybrid,			)


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[Any],			_lowerCamelCase					:	Dict,			_lowerCamelCase					:	List[str],			_lowerCamelCase					:	Any    ):

     '''simple docstring'''
     __A		     =					DPTModel(config=_lowerCamelCase    )
     model.to(_lowerCamelCase    )
     model.eval()
     __A		     =					model(_lowerCamelCase    )
     self.parent.assertEqual(result.last_hidden_state.shape,			(self.batch_size, self.seq_length, self.hidden_size)    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict,			_lowerCamelCase					:	int,			_lowerCamelCase					:	int,			_lowerCamelCase					:	List[str]    ):

     '''simple docstring'''
     __A		     =					self.num_labels
     __A		     =					DPTForDepthEstimation(_lowerCamelCase    )
     model.to(_lowerCamelCase    )
     model.eval()
     __A		     =					model(_lowerCamelCase    )
     self.parent.assertEqual(result.predicted_depth.shape,			(self.batch_size, self.image_size, self.image_size)    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Tuple,			_lowerCamelCase					:	Optional[Any],			_lowerCamelCase					:	Any,			_lowerCamelCase					:	List[Any]    ):

     '''simple docstring'''
     __A		     =					self.num_labels
     __A		     =					DPTForSemanticSegmentation(_lowerCamelCase    )
     model.to(_lowerCamelCase    )
     model.eval()
     __A		     =					model(_lowerCamelCase,			labels=_lowerCamelCase    )
     self.parent.assertEqual(
         result.logits.shape,			(self.batch_size, self.num_labels, self.image_size, self.image_size)    )






   def 							_SCREAMING_SNAKE_CASE							(      self					:	int    ):

     '''simple docstring'''
     __A		     =					self.prepare_config_and_inputs()
     __A     ,       __A     ,       __A		     =					config_and_inputs
     __A		     =					{'''pixel_values''': pixel_values}
     return config, inputs_dict





@require_torch
class    snake_case						(				_lowerCAmelCase  ,      _lowerCAmelCase  ,      unittest.TestCase		):


   '''simple docstring'''

   A_		:      Tuple							=					(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
   A_		:      str							=					(
       {
           "depth-estimation": DPTForDepthEstimation,
           "feature-extraction": DPTModel,
           "image-segmentation": DPTForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   A_		:      Optional[Any]							=					False
   A_		:      str							=					False
   A_		:      Tuple							=					False


   def 							_SCREAMING_SNAKE_CASE							(      self					:	str    ):

     '''simple docstring'''
     __A		     =					DPTModelTester(self    )
     __A		     =					ConfigTester(self,			config_class=_lowerCamelCase,			has_text_modality=_lowerCamelCase,			hidden_size=37    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[str]    ):

     '''simple docstring'''
     self.config_tester.run_common_tests()


   @unittest.skip(reason='''DPT does not use inputs_embeds'''    )
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Union[str, Any]    ):

     '''simple docstring'''
     pass


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[str]    ):

     '''simple docstring'''
     __A     ,       __A		     =					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       __A		     =					model_class(_lowerCamelCase    )
       self.assertIsInstance(model.get_input_embeddings(),			(nn.Module)    )
       __A		     =					model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(_lowerCamelCase,			nn.Linear    )    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[Any]    ):

     '''simple docstring'''
     __A     ,       __A		     =					self.model_tester.prepare_config_and_inputs_for_common()

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

       __A		     =					['''pixel_values''']
       self.assertListEqual(arg_names[:1],			_lowerCamelCase    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[Any]    ):

     '''simple docstring'''
     __A		     =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCamelCase    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[int]    ):

     '''simple docstring'''
     __A		     =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_depth_estimation(*_lowerCamelCase    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	str    ):

     '''simple docstring'''
     __A		     =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCamelCase    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Tuple    ):

     '''simple docstring'''
     for model_class in self.all_model_classes:
       if model_class.__name__ == "DPTForDepthEstimation":
         continue

       __A     ,       __A		     =					self.model_tester.prepare_config_and_inputs_for_common()
       __A		     =					True

       if model_class in get_values(_lowerCamelCase    ):
         continue

       __A		     =					model_class(_lowerCamelCase    )
       model.to(_lowerCamelCase    )
       model.train()
       __A		     =					self._prepare_for_class(_lowerCamelCase,			_lowerCamelCase,			return_labels=_lowerCamelCase    )
       __A		     =					model(**_lowerCamelCase    ).loss
       loss.backward()


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[Any]    ):

     '''simple docstring'''
     for model_class in self.all_model_classes:
       if model_class.__name__ == "DPTForDepthEstimation":
         continue

       __A     ,       __A		     =					self.model_tester.prepare_config_and_inputs_for_common()
       __A		     =					False
       __A		     =					True

       if model_class in get_values(_lowerCamelCase    ) or not model_class.supports_gradient_checkpointing:
         continue
       __A		     =					model_class(_lowerCamelCase    )
       model.to(_lowerCamelCase    )
       model.gradient_checkpointing_enable()
       model.train()
       __A		     =					self._prepare_for_class(_lowerCamelCase,			_lowerCamelCase,			return_labels=_lowerCamelCase    )
       __A		     =					model(**_lowerCamelCase    ).loss
       loss.backward()


   def 							_SCREAMING_SNAKE_CASE							(      self					:	str    ):

     '''simple docstring'''
     __A     ,       __A		     =					self.model_tester.prepare_config_and_inputs_for_common()

     __A		     =					_config_zero_init(_lowerCamelCase    )
     for model_class in self.all_model_classes:
       __A		     =					model_class(config=_lowerCamelCase    )
       # Skip the check for the backbone
       __A		     =					[]
       for name, module in model.named_modules():
         if module.__class__.__name__ == "DPTViTHybridEmbeddings":
           __A		     =					[f'{name}.{key}' for key in module.state_dict().keys()]
           break

       for name, param in model.named_parameters():
         if param.requires_grad:
           if name in backbone_params:
             continue
           self.assertIn(
               ((param.data.mean() * 1e9).round() / 1e9).item(),			[0.0, 1.0],			msg=f'Parameter {name} of model {model_class} seems not properly initialized',			)


   @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''    )
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Any    ):

     '''simple docstring'''
     pass






   @slow
   def 							_SCREAMING_SNAKE_CASE							(      self					:	int    ):

     '''simple docstring'''
     for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __A		     =					DPTModel.from_pretrained(_lowerCamelCase    )
       self.assertIsNotNone(_lowerCamelCase    )





def    lowerCAmelCase							(       ):




  """simple docstring"""







  __A		     =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
  return image





@require_torch
@require_vision
@slow
class    snake_case						(				unittest.TestCase		):


   '''simple docstring'''

   def 							_SCREAMING_SNAKE_CASE							(      self					:	Union[str, Any]    ):

     '''simple docstring'''
     __A		     =					DPTImageProcessor.from_pretrained('''Intel/dpt-large'''    )
     __A		     =					DPTForDepthEstimation.from_pretrained('''Intel/dpt-large'''    ).to(_lowerCamelCase    )

     __A		     =					prepare_img()
     __A		     =					image_processor(images=_lowerCamelCase,			return_tensors='''pt'''    ).to(_lowerCamelCase    )

     # forward pass
     with torch.no_grad():
       __A		     =					model(**_lowerCamelCase    )
       __A		     =					outputs.predicted_depth

     # verify the predicted depth
     __A		     =					torch.Size((1, 3_84, 3_84)    )
     self.assertEqual(predicted_depth.shape,			_lowerCamelCase    )

     __A		     =					torch.tensor(
         [[6.31_99, 6.36_29, 6.41_48], [6.38_50, 6.36_15, 6.41_66], [6.35_19, 6.31_76, 6.35_75]]    ).to(_lowerCamelCase    )

     self.assertTrue(torch.allclose(outputs.predicted_depth[0, :3, :3],			_lowerCamelCase,			atol=1e-4    )    )


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict    ):

     '''simple docstring'''
     __A		     =					DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''    )
     __A		     =					DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''    ).to(_lowerCamelCase    )

     __A		     =					prepare_img()
     __A		     =					image_processor(images=_lowerCamelCase,			return_tensors='''pt'''    ).to(_lowerCamelCase    )

     # forward pass
     with torch.no_grad():
       __A		     =					model(**_lowerCamelCase    )

     # verify the logits
     __A		     =					torch.Size((1, 1_50, 4_80, 4_80)    )
     self.assertEqual(outputs.logits.shape,			_lowerCamelCase    )

     __A		     =					torch.tensor(
         [[4.04_80, 4.24_20, 4.43_60], [4.31_24, 4.56_93, 4.82_61], [4.57_68, 4.89_65, 5.21_63]]    ).to(_lowerCamelCase    )

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






   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[str]    ):

     '''simple docstring'''
     __A		     =					DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''    )
     __A		     =					DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''    ).to(_lowerCamelCase    )

     __A		     =					prepare_img()
     __A		     =					image_processor(images=_lowerCamelCase,			return_tensors='''pt'''    ).to(_lowerCamelCase    )

     # forward pass
     with torch.no_grad():
       __A		     =					model(**_lowerCamelCase    )

     __A		     =					outputs.logits.detach().cpu()

     __A		     =					image_processor.post_process_semantic_segmentation(outputs=_lowerCamelCase,			target_sizes=[(5_00, 3_00)]    )
     __A		     =					torch.Size((5_00, 3_00)    )
     self.assertEqual(segmentation[0].shape,			_lowerCamelCase    )

     __A		     =					image_processor.post_process_semantic_segmentation(outputs=_lowerCamelCase    )
     __A		     =					torch.Size((4_80, 4_80)    )
     self.assertEqual(segmentation[0].shape,			_lowerCamelCase    )








import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
   import torch

   from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
   from transformers.models.dpr.modeling_dpr import (
       DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
   )



class 							a       :





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





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					int	 =						ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size				)

        __lowerCamelCase:					List[Any]	 =						None
        if self.use_input_mask:
           __lowerCamelCase:					Optional[int]	 =						random_attention_mask([self.batch_size, self.seq_length]				)

        __lowerCamelCase:					int	 =						None
        if self.use_token_type_ids:
           __lowerCamelCase:					Union[str, Any]	 =						ids_tensor([self.batch_size, self.seq_length]     ,						self.type_vocab_size				)

        __lowerCamelCase:					Optional[int]	 =						None
        __lowerCamelCase:					int	 =						None
        __lowerCamelCase:					List[Any]	 =						None
        if self.use_labels:
           __lowerCamelCase:					List[str]	 =						ids_tensor([self.batch_size]     ,						self.type_sequence_label_size				)
           __lowerCamelCase:					str	 =						ids_tensor([self.batch_size, self.seq_length]     ,						self.num_labels				)
           __lowerCamelCase:					Optional[int]	 =						ids_tensor([self.batch_size]     ,						self.num_choices				)

        __lowerCamelCase:					int	 =						self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any				):
        return DPRConfig(
            projection_dim=self.projection_dim     ,						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     ,						initializer_range=self.initializer_range     ,						)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					int     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					List[str]				):
        __lowerCamelCase:					Union[str, Any]	 =						DPRContextEncoder(config=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)
        model.eval()
        __lowerCamelCase:					int	 =						model(SCREAMING_SNAKE_CASE_     ,						attention_mask=SCREAMING_SNAKE_CASE_     ,						token_type_ids=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Optional[Any]	 =						model(SCREAMING_SNAKE_CASE_     ,						token_type_ids=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Any	 =						model(SCREAMING_SNAKE_CASE_				)
        self.parent.assertEqual(result.pooler_output.shape     ,						(self.batch_size, self.projection_dim or self.hidden_size)				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]     ,						SCREAMING_SNAKE_CASE_       :					int     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					Tuple				):
        __lowerCamelCase:					List[Any]	 =						DPRQuestionEncoder(config=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)
        model.eval()
        __lowerCamelCase:					int	 =						model(SCREAMING_SNAKE_CASE_     ,						attention_mask=SCREAMING_SNAKE_CASE_     ,						token_type_ids=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Optional[int]	 =						model(SCREAMING_SNAKE_CASE_     ,						token_type_ids=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Union[str, Any]	 =						model(SCREAMING_SNAKE_CASE_				)
        self.parent.assertEqual(result.pooler_output.shape     ,						(self.batch_size, self.projection_dim or self.hidden_size)				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]				):
        __lowerCamelCase:					Optional[Any]	 =						DPRReader(config=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)
        model.eval()
        __lowerCamelCase:					Any	 =						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 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					Any	 =						self.prepare_config_and_inputs()
        (
            (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  (
            __lowerCamelCase
        ) ,  
        ):					Dict	 =						config_and_inputs
        __lowerCamelCase:					List[str]	 =						{"""input_ids""": input_ids}
        return config, inputs_dict



@require_torch
class 							a       (      _UpperCAmelCase  ,_UpperCAmelCase  ,unittest.TestCase ):
     UpperCAmelCase__			:	Dict       =						(
         (
             DPRContextEncoder,
             DPRQuestionEncoder,
             DPRReader,
         )
         if is_torch_available()
         else ()
     )
     UpperCAmelCase__			:	List[str]       =						{"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}

     UpperCAmelCase__			:	str       =						False
     UpperCAmelCase__			:	Optional[Any]       =						False  # why?
     UpperCAmelCase__			:	List[Any]       =						False
     UpperCAmelCase__			:	int       =						False





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]				):
        __lowerCamelCase:					str	 =						DPRModelTester(self				)
        __lowerCamelCase:					Dict	 =						ConfigTester(self     ,						config_class=SCREAMING_SNAKE_CASE_     ,						hidden_size=37				)





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





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]				):
        __lowerCamelCase:					Optional[Any]	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_context_encoder(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int				):
        __lowerCamelCase:					Any	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_question_encoder(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict				):
        __lowerCamelCase:					str	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_reader(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					str	 =						self.model_tester.prepare_config_and_inputs()[0]

        __lowerCamelCase:					Optional[int]	 =						DPRQuestionEncoder(config=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)
        model.eval()

        with tempfile.TemporaryDirectory() as tmp_dirname:
           model.save_pretrained(SCREAMING_SNAKE_CASE_				)
           __lowerCamelCase:					List[Any]	 =						DPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE_     ,						projection_dim=512				)

        self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)







     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict				):
        for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Dict	 =						DPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

        for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Any	 =						DPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

        for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Tuple	 =						DPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

        for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Optional[int]	 =						DPRReader.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)



@require_torch
class 							a       (      unittest.TestCase ):





     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        __lowerCamelCase:					Union[str, Any]	 =						DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""     ,						return_dict=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					Tuple	 =						torch.tensor(
            [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]     ,						dtype=torch.long     ,						device=SCREAMING_SNAKE_CASE_				)  # [CLS] hello, is my dog cute? [SEP]
        __lowerCamelCase:					Tuple	 =						model(SCREAMING_SNAKE_CASE_				)[0]  # embedding shape = (1, 768)
        # compare the actual values for a slice.
        __lowerCamelCase:					Dict	 =						torch.tensor(
            [
                [
                    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,
                ]
            ]     ,						dtype=torch.float     ,						device=SCREAMING_SNAKE_CASE_     ,						)
        self.assertTrue(torch.allclose(output[:, :10]     ,						SCREAMING_SNAKE_CASE_     ,						atol=1E-4				)				)







     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					Dict	 =						DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""				)
        __lowerCamelCase:					Optional[Any]	 =						DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""				)
        model.to(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					int	 =						tokenizer(
            questions="""What is love ?"""     ,						titles="""Haddaway"""     ,						texts="""What Is Love is a song recorded by the artist Haddaway"""     ,						padding=SCREAMING_SNAKE_CASE_     ,						return_tensors="""pt"""     ,						)
        encoded_inputs.to(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					Optional[Any]	 =						model(**SCREAMING_SNAKE_CASE_				)

        # compare the actual values for a slice.
        __lowerCamelCase:					Optional[Any]	 =						torch.tensor(
            [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]     ,						dtype=torch.float     ,						device=SCREAMING_SNAKE_CASE_     ,						)

        __lowerCamelCase:					Dict	 =						torch.tensor(
            [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]     ,						dtype=torch.float     ,						device=SCREAMING_SNAKE_CASE_     ,						)
        self.assertTrue(torch.allclose(outputs.start_logits[:, :10]     ,						SCREAMING_SNAKE_CASE_     ,						atol=1E-4				)				)
        self.assertTrue(torch.allclose(outputs.end_logits[:, :10]     ,						SCREAMING_SNAKE_CASE_     ,						atol=1E-4				)				)


