


import inspect
import unittest

from transformers 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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import MODEL_MAPPING, GLPNConfig, GLPNForDepthEstimation, GLPNModel
   from transformers.models.glpn.modeling_glpn import GLPN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import GLPNImageProcessor



class 							a       (      _UpperCAmelCase ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					int	 =						self.config_class(**self.inputs_dict				)
        self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_     ,						"""hidden_sizes"""				)				)
        self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_     ,						"""num_attention_heads"""				)				)
        self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_     ,						"""num_encoder_blocks"""				)				)



class 							a       :





     def __init__(     self       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=13     ,						SCREAMING_SNAKE_CASE_       :					Dict=64     ,						SCREAMING_SNAKE_CASE_       :					List[str]=3     ,						SCREAMING_SNAKE_CASE_       :					Any=4     ,						SCREAMING_SNAKE_CASE_       :					List[str]=[2, 2, 2, 2]     ,						SCREAMING_SNAKE_CASE_       :					str=[8, 4, 2, 1]     ,						SCREAMING_SNAKE_CASE_       :					List[Any]=[16, 32, 64, 128]     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]=[1, 4, 8, 16]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]=[1, 2, 4, 8]     ,						SCREAMING_SNAKE_CASE_       :					List[str]=True     ,						SCREAMING_SNAKE_CASE_       :					Any=True     ,						SCREAMING_SNAKE_CASE_       :					Dict="gelu"     ,						SCREAMING_SNAKE_CASE_       :					str=0.1     ,						SCREAMING_SNAKE_CASE_       :					Dict=0.1     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=0.02     ,						SCREAMING_SNAKE_CASE_       :					int=16     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=3     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]=None     ,						):
        __lowerCamelCase:					Optional[int]	 =						parent
        __lowerCamelCase:					Union[str, Any]	 =						batch_size
        __lowerCamelCase:					Dict	 =						image_size
        __lowerCamelCase:					Optional[int]	 =						num_channels
        __lowerCamelCase:					int	 =						num_encoder_blocks
        __lowerCamelCase:					Tuple	 =						sr_ratios
        __lowerCamelCase:					Union[str, Any]	 =						depths
        __lowerCamelCase:					Union[str, Any]	 =						hidden_sizes
        __lowerCamelCase:					int	 =						downsampling_rates
        __lowerCamelCase:					Dict	 =						num_attention_heads
        __lowerCamelCase:					Union[str, Any]	 =						is_training
        __lowerCamelCase:					Optional[int]	 =						use_labels
        __lowerCamelCase:					Tuple	 =						hidden_act
        __lowerCamelCase:					Tuple	 =						hidden_dropout_prob
        __lowerCamelCase:					int	 =						attention_probs_dropout_prob
        __lowerCamelCase:					List[str]	 =						initializer_range
        __lowerCamelCase:					Dict	 =						decoder_hidden_size
        __lowerCamelCase:					int	 =						num_labels
        __lowerCamelCase:					Optional[int]	 =						scope





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]				):
        __lowerCamelCase:					str	 =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

        __lowerCamelCase:					int	 =						None
        if self.use_labels:
           __lowerCamelCase:					List[str]	 =						ids_tensor([self.batch_size, self.image_size, self.image_size]     ,						self.num_labels				)

        __lowerCamelCase:					Tuple	 =						self.get_config()
        return config, pixel_values, labels





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str				):
        return GLPNConfig(
            image_size=self.image_size     ,						num_channels=self.num_channels     ,						num_encoder_blocks=self.num_encoder_blocks     ,						depths=self.depths     ,						hidden_sizes=self.hidden_sizes     ,						num_attention_heads=self.num_attention_heads     ,						hidden_act=self.hidden_act     ,						hidden_dropout_prob=self.hidden_dropout_prob     ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,						initializer_range=self.initializer_range     ,						decoder_hidden_size=self.decoder_hidden_size     ,						)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					List[str]				):
        __lowerCamelCase:					Optional[int]	 =						GLPNModel(config=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)
        model.eval()
        __lowerCamelCase:					Tuple	 =						model(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Optional[int]	 =						self.image_size // (self.downsampling_rates[-1] * 2)
        self.parent.assertEqual(
            result.last_hidden_state.shape     ,						(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Any     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]				):
        __lowerCamelCase:					Optional[int]	 =						self.num_labels
        __lowerCamelCase:					List[Any]	 =						GLPNForDepthEstimation(SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)
        model.eval()
        __lowerCamelCase:					Tuple	 =						model(SCREAMING_SNAKE_CASE_				)
        self.parent.assertEqual(result.predicted_depth.shape     ,						(self.batch_size, self.image_size, self.image_size)				)
        __lowerCamelCase:					List[Any]	 =						model(SCREAMING_SNAKE_CASE_     ,						labels=SCREAMING_SNAKE_CASE_				)
        self.parent.assertEqual(result.predicted_depth.shape     ,						(self.batch_size, self.image_size, self.image_size)				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        __lowerCamelCase:					List[Any]	 =						self.prepare_config_and_inputs()
        __lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase:					List[Any]	 =						config_and_inputs
        __lowerCamelCase:					List[Any]	 =						{"""pixel_values""": pixel_values}
        return config, inputs_dict



@require_torch
class 							a       (      _UpperCAmelCase  ,_UpperCAmelCase  ,unittest.TestCase ):
     UpperCAmelCase__			:	Tuple       =						(GLPNModel, GLPNForDepthEstimation) if is_torch_available() else ()
     UpperCAmelCase__			:	Any       =						(
         {"depth-estimation": GLPNForDepthEstimation, "feature-extraction": GLPNModel} if is_torch_available() else {}
     )

     UpperCAmelCase__			:	Any       =						False
     UpperCAmelCase__			:	List[Any]       =						False
     UpperCAmelCase__			:	Optional[int]       =						False





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Tuple				):
        __lowerCamelCase:					Dict	 =						GLPNModelTester(self				)
        __lowerCamelCase:					Union[str, Any]	 =						GLPNConfigTester(self     ,						config_class=SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        self.config_tester.run_common_tests()





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					Dict	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_				)





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





     @unittest.skip("""GLPN does not use inputs_embeds"""				)
     def 			SCREAMING_SNAKE_CASE__							(     self       :					str				):
        pass





     @unittest.skip("""GLPN does not have get_input_embeddings method and get_output_embeddings methods"""				)
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[int]				):
        pass





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any				):
        __lowerCamelCase ,  __lowerCamelCase:					Optional[int]	 =						self.model_tester.prepare_config_and_inputs_for_common()

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

           __lowerCamelCase:					Optional[Any]	 =						["""pixel_values"""]
           self.assertListEqual(arg_names[:1]     ,						SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any				):
        __lowerCamelCase ,  __lowerCamelCase:					Optional[int]	 =						self.model_tester.prepare_config_and_inputs_for_common()
        __lowerCamelCase:					Optional[int]	 =						True

        for model_class in self.all_model_classes:
           __lowerCamelCase:					str	 =						True
           __lowerCamelCase:					Optional[int]	 =						False
           __lowerCamelCase:					Dict	 =						True
           __lowerCamelCase:					int	 =						model_class(SCREAMING_SNAKE_CASE_				)
           model.to(SCREAMING_SNAKE_CASE_				)
           model.eval()
           with torch.no_grad():
              __lowerCamelCase:					Union[str, Any]	 =						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)				)
           __lowerCamelCase:					List[str]	 =						outputs.attentions

           __lowerCamelCase:					Dict	 =						sum(self.model_tester.depths				)
           self.assertEqual(len(SCREAMING_SNAKE_CASE_				)     ,						SCREAMING_SNAKE_CASE_				)

           # check that output_attentions also work using config
           del inputs_dict["output_attentions"]
           __lowerCamelCase:					Optional[int]	 =						True
           __lowerCamelCase:					Union[str, Any]	 =						model_class(SCREAMING_SNAKE_CASE_				)
           model.to(SCREAMING_SNAKE_CASE_				)
           model.eval()
           with torch.no_grad():
              __lowerCamelCase:					Dict	 =						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)				)
           __lowerCamelCase:					List[Any]	 =						outputs.attentions

           self.assertEqual(len(SCREAMING_SNAKE_CASE_				)     ,						SCREAMING_SNAKE_CASE_				)

           # verify the first attentions (first block, first layer)
           __lowerCamelCase:					int	 =						(self.model_tester.image_size // 4) ** 2
           __lowerCamelCase:					str	 =						(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
           self.assertListEqual(
               list(attentions[0].shape[-3:]				)     ,						[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]     ,						)

           # verify the last attentions (last block, last layer)
           __lowerCamelCase:					Dict	 =						(self.model_tester.image_size // 32) ** 2
           __lowerCamelCase:					Optional[int]	 =						(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
           self.assertListEqual(
               list(attentions[-1].shape[-3:]				)     ,						[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]     ,						)
           __lowerCamelCase:					Optional[int]	 =						len(SCREAMING_SNAKE_CASE_				)

           # Check attention is always last and order is fine
           __lowerCamelCase:					Any	 =						True
           __lowerCamelCase:					List[str]	 =						True
           __lowerCamelCase:					Any	 =						model_class(SCREAMING_SNAKE_CASE_				)
           model.to(SCREAMING_SNAKE_CASE_				)
           model.eval()
           with torch.no_grad():
              __lowerCamelCase:					Optional[Any]	 =						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)				)

           self.assertEqual(out_len + 1     ,						len(SCREAMING_SNAKE_CASE_				)				)

           __lowerCamelCase:					Tuple	 =						outputs.attentions

           self.assertEqual(len(SCREAMING_SNAKE_CASE_				)     ,						SCREAMING_SNAKE_CASE_				)
           # verify the first attentions (first block, first layer)
           __lowerCamelCase:					Any	 =						(self.model_tester.image_size // 4) ** 2
           __lowerCamelCase:					Dict	 =						(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
           self.assertListEqual(
               list(self_attentions[0].shape[-3:]				)     ,						[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]     ,						)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str				):
        def check_hidden_states_output(SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Any     ,						SCREAMING_SNAKE_CASE_       :					Dict				):
           __lowerCamelCase:					Optional[int]	 =						model_class(SCREAMING_SNAKE_CASE_				)
           model.to(SCREAMING_SNAKE_CASE_				)
           model.eval()

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

           __lowerCamelCase:					Union[str, Any]	 =						outputs.hidden_states

           __lowerCamelCase:					Any	 =						self.model_tester.num_encoder_blocks
           self.assertEqual(len(SCREAMING_SNAKE_CASE_				)     ,						SCREAMING_SNAKE_CASE_				)

           # verify the first hidden states (first block)
           self.assertListEqual(
               list(hidden_states[0].shape[-3:]				)     ,						[
                   self.model_tester.hidden_sizes[0],
                   self.model_tester.image_size // 4,
                   self.model_tester.image_size // 4,
               ]     ,						)

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

        for model_class in self.all_model_classes:
           __lowerCamelCase:					List[str]	 =						True
           check_hidden_states_output(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)

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

           check_hidden_states_output(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict				):
        if not self.model_tester.is_training:
           return

        __lowerCamelCase ,  __lowerCamelCase:					Union[str, Any]	 =						self.model_tester.prepare_config_and_inputs_for_common()
        __lowerCamelCase:					Dict	 =						True

        for model_class in self.all_model_classes:
           if model_class in get_values(SCREAMING_SNAKE_CASE_				):
              continue
           # TODO: remove the following 3 lines once we have a MODEL_FOR_DEPTH_ESTIMATION_MAPPING
           # this can then be incorporated into _prepare_for_class in test_modeling_common.py
           if model_class.__name__ == "GLPNForDepthEstimation":
              __lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase:					Optional[int]	 =						inputs_dict["""pixel_values"""].shape
              __lowerCamelCase:					Dict	 =						torch.zeros(
                  [self.model_tester.batch_size, height, width]     ,						device=SCREAMING_SNAKE_CASE_				).long()
           __lowerCamelCase:					Dict	 =						model_class(SCREAMING_SNAKE_CASE_				)
           model.to(SCREAMING_SNAKE_CASE_				)
           model.train()
           __lowerCamelCase:					List[str]	 =						self._prepare_for_class(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						return_labels=SCREAMING_SNAKE_CASE_				)
           __lowerCamelCase:					int	 =						model(**SCREAMING_SNAKE_CASE_				).loss
           loss.backward()







     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]				):
        for model_name in GLPN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Union[str, Any]	 =						GLPNModel.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)



def      __lowerCAmelCase    (				)							->				str:
   __lowerCamelCase:					str	 =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
   return image







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





     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict				):
        __lowerCamelCase:					List[str]	 =						GLPNImageProcessor.from_pretrained(GLPN_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)
        __lowerCamelCase:					Optional[int]	 =						GLPNForDepthEstimation.from_pretrained(GLPN_PRETRAINED_MODEL_ARCHIVE_LIST[0]				).to(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					List[str]	 =						prepare_img()
        __lowerCamelCase:					List[str]	 =						image_processor(images=SCREAMING_SNAKE_CASE_     ,						return_tensors="""pt"""				).to(SCREAMING_SNAKE_CASE_				)

        # forward pass
        with torch.no_grad():
           __lowerCamelCase:					Optional[Any]	 =						model(**SCREAMING_SNAKE_CASE_				)

        # verify the predicted depth
        __lowerCamelCase:					Dict	 =						torch.Size([1, 480, 640]				)
        self.assertEqual(outputs.predicted_depth.shape     ,						SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					List[Any]	 =						torch.tensor(
            [[3.4291, 2.7865, 2.5151], [3.2841, 2.7021, 2.3502], [3.1147, 2.4625, 2.2481]]				).to(SCREAMING_SNAKE_CASE_				)

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






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				)				)


