


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




     '''simple docstring'''


     def __init__(						self			,						__lowerCamelCase			,						__lowerCamelCase=1_3			,						__lowerCamelCase=7			,						__lowerCamelCase=False			,						__lowerCamelCase=True			,						__lowerCamelCase=True			,						__lowerCamelCase=True			,						__lowerCamelCase=9_9			,						__lowerCamelCase=3_2			,						__lowerCamelCase=5			,						__lowerCamelCase=4			,						__lowerCamelCase=3_7			,						__lowerCamelCase="gelu"			,						__lowerCamelCase=0.1			,						__lowerCamelCase=0.1			,						__lowerCamelCase=5_1_2			,						__lowerCamelCase=1_6			,						__lowerCamelCase=2			,						__lowerCamelCase=0.0_2			,						__lowerCamelCase=3			,						__lowerCamelCase=4			,						__lowerCamelCase=None			,						__lowerCamelCase=0			,						)      ->    str:
            _A				:	Dict       =			parent
            _A				:	str       =			batch_size
            _A				:	Tuple       =			seq_length
            _A				:	List[Any]       =			is_training
            _A				:	int       =			use_input_mask
            _A				:	List[Any]       =			use_token_type_ids
            _A				:	Dict       =			use_labels
            _A				:	Tuple       =			vocab_size
            _A				:	int       =			hidden_size
            _A				:	List[Any]       =			num_hidden_layers
            _A				:	Tuple       =			num_attention_heads
            _A				:	Union[str, Any]       =			intermediate_size
            _A				:	Optional[int]       =			hidden_act
            _A				:	Tuple       =			hidden_dropout_prob
            _A				:	int       =			attention_probs_dropout_prob
            _A				:	Union[str, Any]       =			max_position_embeddings
            _A				:	Dict       =			type_vocab_size
            _A				:	Dict       =			type_sequence_label_size
            _A				:	Any       =			initializer_range
            _A				:	Optional[Any]       =			num_labels
            _A				:	Any       =			num_choices
            _A				:	List[str]       =			scope
            _A				:	int       =			projection_dim


     def 			_lowerCamelCase			(						self)      ->    List[str]:
            _A				:	Any       =			ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size)

            _A				:	int       =			None
            if self.use_input_mask:
                   _A				:	int       =			random_attention_mask([self.batch_size, self.seq_length])

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

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

            _A				:	Optional[int]       =			self.get_config()

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


     def 			_lowerCamelCase			(						self)      ->    Union[str, 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 			_lowerCamelCase			(						self			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)      ->    Any:
            _A				:	List[str]       =			DPRContextEncoder(config=__SCREAMING_SNAKE_CASE)
            model.to(__SCREAMING_SNAKE_CASE)
            model.eval()
            _A				:	Optional[Any]       =			model(__SCREAMING_SNAKE_CASE			,						attention_mask=__SCREAMING_SNAKE_CASE			,						token_type_ids=__SCREAMING_SNAKE_CASE)
            _A				:	Union[str, Any]       =			model(__SCREAMING_SNAKE_CASE			,						token_type_ids=__SCREAMING_SNAKE_CASE)
            _A				:	List[Any]       =			model(__SCREAMING_SNAKE_CASE)
            self.parent.assertEqual(result.pooler_output.shape			,						(self.batch_size, self.projection_dim or self.hidden_size))


     def 			_lowerCamelCase			(						self			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)      ->    Any:
            _A				:	Optional[int]       =			DPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE)
            model.to(__SCREAMING_SNAKE_CASE)
            model.eval()
            _A				:	Optional[int]       =			model(__SCREAMING_SNAKE_CASE			,						attention_mask=__SCREAMING_SNAKE_CASE			,						token_type_ids=__SCREAMING_SNAKE_CASE)
            _A				:	Tuple       =			model(__SCREAMING_SNAKE_CASE			,						token_type_ids=__SCREAMING_SNAKE_CASE)
            _A				:	Tuple       =			model(__SCREAMING_SNAKE_CASE)
            self.parent.assertEqual(result.pooler_output.shape			,						(self.batch_size, self.projection_dim or self.hidden_size))


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







@require_torch
class 					lowerCAmelCase__	(    __lowerCAmelCase					,   __lowerCAmelCase					,   unittest.TestCase):




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE        =			(
         (
             DPRContextEncoder,
             DPRQuestionEncoder,
             DPRReader,
         )
         if is_torch_available()
         else ()
     )
     __SCREAMING_SNAKE_CASE        =			{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

     __SCREAMING_SNAKE_CASE        =			False
     __SCREAMING_SNAKE_CASE        =			False  # why?
     __SCREAMING_SNAKE_CASE        =			False
     __SCREAMING_SNAKE_CASE        =			False


     def 			_lowerCamelCase			(						self)      ->    Tuple:
            _A				:	Tuple       =			DPRModelTester(self)
            _A				:	Optional[int]       =			ConfigTester(self			,						config_class=__SCREAMING_SNAKE_CASE			,						hidden_size=3_7)


     def 			_lowerCamelCase			(						self)      ->    Union[str, Any]:
            self.config_tester.run_common_tests()


     def 			_lowerCamelCase			(						self)      ->    Union[str, Any]:
            _A				:	int       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_context_encoder(*__SCREAMING_SNAKE_CASE)


     def 			_lowerCamelCase			(						self)      ->    str:
            _A				:	Tuple       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_question_encoder(*__SCREAMING_SNAKE_CASE)


     def 			_lowerCamelCase			(						self)      ->    Union[str, Any]:
            _A				:	Optional[int]       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_reader(*__SCREAMING_SNAKE_CASE)


     def 			_lowerCamelCase			(						self)      ->    Any:
            _A				:	List[Any]       =			self.model_tester.prepare_config_and_inputs()[0]

            _A				:	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)
                   _A				:	Optional[int]       =			DPRQuestionEncoder.from_pretrained(__SCREAMING_SNAKE_CASE			,						projection_dim=5_1_2)

            self.assertIsNotNone(__SCREAMING_SNAKE_CASE)


     @slow
     def 			_lowerCamelCase			(						self)      ->    Tuple:




            for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A				:	int       =			DPRContextEncoder.from_pretrained(__SCREAMING_SNAKE_CASE)
                   self.assertIsNotNone(__SCREAMING_SNAKE_CASE)

            for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A				:	List[Any]       =			DPRContextEncoder.from_pretrained(__SCREAMING_SNAKE_CASE)
                   self.assertIsNotNone(__SCREAMING_SNAKE_CASE)

            for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A				:	str       =			DPRQuestionEncoder.from_pretrained(__SCREAMING_SNAKE_CASE)
                   self.assertIsNotNone(__SCREAMING_SNAKE_CASE)

            for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A				:	Tuple       =			DPRReader.from_pretrained(__SCREAMING_SNAKE_CASE)
                   self.assertIsNotNone(__SCREAMING_SNAKE_CASE)







@require_torch
class 					lowerCAmelCase__	(    unittest.TestCase):




     '''simple docstring'''


     @slow
     def 			_lowerCamelCase			(						self)      ->    List[Any]:
            _A				:	Union[str, Any]       =			DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"			,						return_dict=__SCREAMING_SNAKE_CASE)
            model.to(__SCREAMING_SNAKE_CASE)

            _A				:	List[str]       =			torch.tensor(
                [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]			,						dtype=torch.long			,						device=__SCREAMING_SNAKE_CASE)  # [CLS] hello, is my dog cute? [SEP]
            _A				:	Optional[Any]       =			model(__SCREAMING_SNAKE_CASE)[0]  # embedding shape = (1, 768)
            # compare the actual values for a slice.
            _A				:	Tuple       =			torch.tensor(
                [
                    [
                        0.0_3_2_3_6_2_5_3,
                        0.1_2_7_5_3_3_3_5,
                        0.1_6_8_1_8_5_0_9,
                        0.0_0_2_7_9_7_8_6,
                        0.3_8_9_6_9_3_3,
                        0.2_4_2_6_4_9_4_5,
                        0.2_1_7_8_9_7_1,
                        -0.0_2_3_3_5_2_2_7,
                        -0.0_8_4_8_1_9_5_9,
                        -0.1_4_3_2_4_1_1_7,
                    ]
                ]			,						dtype=torch.float			,						device=__SCREAMING_SNAKE_CASE			,						)
            self.assertTrue(torch.allclose(output[:, :1_0]			,						__SCREAMING_SNAKE_CASE			,						atol=1e-4))


     @slow
     def 			_lowerCamelCase			(						self)      ->    Any:
            _A				:	List[Any]       =			DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base")
            _A				:	Tuple       =			DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base")
            model.to(__SCREAMING_SNAKE_CASE)

            _A				:	Tuple       =			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)

            _A				:	Union[str, Any]       =			model(**__SCREAMING_SNAKE_CASE)

            # compare the actual values for a slice.
            _A				:	str       =			torch.tensor(
                [[-1_0.3_0_0_5, -1_0.7_7_6_5, -1_1.4_8_7_2, -1_1.6_8_4_1, -1_1.9_3_1_2, -1_0.3_0_0_2, -9.8_5_4_4, -1_1.7_3_7_8, -1_2.0_8_2_1, -1_0.2_9_7_5]]			,						dtype=torch.float			,						device=__SCREAMING_SNAKE_CASE			,						)

            _A				:	List[str]       =			torch.tensor(
                [[-1_1.0_6_8_4, -1_1.7_0_4_1, -1_1.5_3_9_7, -1_0.3_4_6_5, -1_0.8_7_9_1, -6.8_4_4_3, -1_1.9_9_5_9, -1_1.0_3_6_4, -1_0.0_0_9_6, -6.8_4_0_5]]			,						dtype=torch.float			,						device=__SCREAMING_SNAKE_CASE			,						)
            self.assertTrue(torch.allclose(outputs.start_logits[:, :1_0]			,						__SCREAMING_SNAKE_CASE			,						atol=1e-4))
            self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]			,						__SCREAMING_SNAKE_CASE			,						atol=1e-4))




import warnings

from .generation import FlaxGenerationMixin







class 					lowerCAmelCase__	(    a):




     '''simple docstring'''
     # warning at import time
     warnings.warn(
         "Importing `FlaxGenerationMixin` from `src/transformers/generation_flax_utils.py` is deprecated and will "
         "be removed in Transformers v5. Import as `from transformers import FlaxGenerationMixin` instead."					,   a					,   )
