


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     :    Union[str, Any]	,							lowerCamelCase_     :    Dict	,							lowerCamelCase_     :    Optional[int]=13	,							lowerCamelCase_     :    Dict=7	,							lowerCamelCase_     :    Optional[Any]=False	,							lowerCamelCase_     :    Union[str, Any]=True	,							lowerCamelCase_     :    str=True	,							lowerCamelCase_     :    Optional[Any]=True	,							lowerCamelCase_     :    Union[str, Any]=99	,							lowerCamelCase_     :    Optional[int]=32	,							lowerCamelCase_     :    Dict=5	,							lowerCamelCase_     :    int=4	,							lowerCamelCase_     :    int=37	,							lowerCamelCase_     :    Union[str, Any]="gelu"	,							lowerCamelCase_     :    Dict=0.1	,							lowerCamelCase_     :    Tuple=0.1	,							lowerCamelCase_     :    List[Any]=512	,							lowerCamelCase_     :    Optional[Any]=16	,							lowerCamelCase_     :    Tuple=2	,							lowerCamelCase_     :    Dict=0.02	,							lowerCamelCase_     :    Optional[int]=3	,							lowerCamelCase_     :    Tuple=4	,							lowerCamelCase_     :    Tuple=None	,							lowerCamelCase_     :    Optional[Any]=0	,							):
								_lowerCAmelCase  						=parent
								_lowerCAmelCase  						=batch_size
								_lowerCAmelCase  						=seq_length
								_lowerCAmelCase  						=is_training
								_lowerCAmelCase  						=use_input_mask
								_lowerCAmelCase  						=use_token_type_ids
								_lowerCAmelCase  						=use_labels
								_lowerCAmelCase  						=vocab_size
								_lowerCAmelCase  						=hidden_size
								_lowerCAmelCase  						=num_hidden_layers
								_lowerCAmelCase  						=num_attention_heads
								_lowerCAmelCase  						=intermediate_size
								_lowerCAmelCase  						=hidden_act
								_lowerCAmelCase  						=hidden_dropout_prob
								_lowerCAmelCase  						=attention_probs_dropout_prob
								_lowerCAmelCase  						=max_position_embeddings
								_lowerCAmelCase  						=type_vocab_size
								_lowerCAmelCase  						=type_sequence_label_size
								_lowerCAmelCase  						=initializer_range
								_lowerCAmelCase  						=num_labels
								_lowerCAmelCase  						=num_choices
								_lowerCAmelCase  						=scope
								_lowerCAmelCase  						=projection_dim






				def        lowerCAmelCase__			(				self     :    Any					):
								_lowerCAmelCase  						=ids_tensor([self.batch_size, self.seq_length]	,							self.vocab_size					)

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

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

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

								_lowerCAmelCase  						=self.get_config()

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






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






				def        lowerCAmelCase__			(				self     :    Optional[Any]	,							lowerCamelCase_     :    str	,							lowerCamelCase_     :    Union[str, Any]	,							lowerCamelCase_     :    Optional[Any]	,							lowerCamelCase_     :    int	,							lowerCamelCase_     :    Union[str, Any]	,							lowerCamelCase_     :    Optional[int]	,							lowerCamelCase_     :    Any					):
								_lowerCAmelCase  						=DPRQuestionEncoder(config=lowerCamelCase_					)
								model.to(lowerCamelCase_					)
								model.eval()
								_lowerCAmelCase  						=model(lowerCamelCase_	,							attention_mask=lowerCamelCase_	,							token_type_ids=lowerCamelCase_					)
								_lowerCAmelCase  						=model(lowerCamelCase_	,							token_type_ids=lowerCamelCase_					)
								_lowerCAmelCase  						=model(lowerCamelCase_					)
								self.parent.assertEqual(result.pooler_output.shape	,							(self.batch_size, self.projection_dim or self.hidden_size)					)






				def        lowerCAmelCase__			(				self     :    Dict	,							lowerCamelCase_     :    Tuple	,							lowerCamelCase_     :    Optional[Any]	,							lowerCamelCase_     :    Any	,							lowerCamelCase_     :    List[Any]	,							lowerCamelCase_     :    Tuple	,							lowerCamelCase_     :    List[str]	,							lowerCamelCase_     :    List[str]					):
								_lowerCAmelCase  						=DPRReader(config=lowerCamelCase_					)
								model.to(lowerCamelCase_					)
								model.eval()
								_lowerCAmelCase  						=model(
								    lowerCamelCase_	,							attention_mask=lowerCamelCase_	,							)

								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     :    Dict					):
								_lowerCAmelCase  						=self.prepare_config_and_inputs()
								(
								    (
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							(
								    _lowerCAmelCase
								)						,							
								)  						=config_and_inputs
								_lowerCAmelCase  						={"""input_ids""": input_ids}
								return config, inputs_dict


@require_torch
class        __lowerCamelCase      (				lowerCamelCase_				,		lowerCamelCase_				,		unittest.TestCase ):


				"""simple docstring"""







				a_:		Tuple					      =	(
				    (
				        DPRContextEncoder,
				        DPRQuestionEncoder,
				        DPRReader,
				    )
				    if is_torch_available()
				    else ()
				)
				a_:		Tuple					      =	{"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

				a_:		Union[str, Any]					      =	False
				a_:		str					      =	False  # why?
				a_:		Dict					      =	False
				a_:		Dict					      =	False






				def        lowerCAmelCase__			(				self     :    Dict					):
								_lowerCAmelCase  						=DPRModelTester(self					)
								_lowerCAmelCase  						=ConfigTester(self	,							config_class=lowerCamelCase_	,							hidden_size=37					)






				def        lowerCAmelCase__			(				self     :    Tuple					):
								self.config_tester.run_common_tests()






				def        lowerCAmelCase__			(				self     :    int					):
								_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_context_encoder(*lowerCamelCase_					)






				def        lowerCAmelCase__			(				self     :    int					):
								_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_question_encoder(*lowerCamelCase_					)






				def        lowerCAmelCase__			(				self     :    Dict					):
								_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_reader(*lowerCamelCase_					)






				def        lowerCAmelCase__			(				self     :    Optional[int]					):
								_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs()[0]

								_lowerCAmelCase  						=DPRQuestionEncoder(config=lowerCamelCase_					)
								model.to(lowerCamelCase_					)
								model.eval()

								with tempfile.TemporaryDirectory() as tmp_dirname:
												model.save_pretrained(lowerCamelCase_					)
												_lowerCAmelCase  						=DPRQuestionEncoder.from_pretrained(lowerCamelCase_	,							projection_dim=512					)

								self.assertIsNotNone(lowerCamelCase_					)






				@slow
				def        lowerCAmelCase__			(				self     :    Optional[int]					):
								for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_lowerCAmelCase  						=DPRContextEncoder.from_pretrained(lowerCamelCase_					)
												self.assertIsNotNone(lowerCamelCase_					)

								for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_lowerCAmelCase  						=DPRContextEncoder.from_pretrained(lowerCamelCase_					)
												self.assertIsNotNone(lowerCamelCase_					)

								for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_lowerCAmelCase  						=DPRQuestionEncoder.from_pretrained(lowerCamelCase_					)
												self.assertIsNotNone(lowerCamelCase_					)

								for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_lowerCAmelCase  						=DPRReader.from_pretrained(lowerCamelCase_					)
												self.assertIsNotNone(lowerCamelCase_					)


@require_torch
class        __lowerCamelCase      (				unittest.TestCase ):


				"""simple docstring"""







				@slow
				def        lowerCAmelCase__			(				self     :    int					):
								_lowerCAmelCase  						=DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""	,							return_dict=lowerCamelCase_					)
								model.to(lowerCamelCase_					)

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






				@slow
				def        lowerCAmelCase__			(				self     :    Union[str, Any]					):
								_lowerCAmelCase  						=DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""					)
								_lowerCAmelCase  						=DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""					)
								model.to(lowerCamelCase_					)

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

								_lowerCAmelCase  						=model(**lowerCamelCase_					)

								# compare the actual values for a slice.
								_lowerCAmelCase  						=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=lowerCamelCase_	,							)

								_lowerCAmelCase  						=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=lowerCamelCase_	,							)
								self.assertTrue(torch.allclose(outputs.start_logits[:, :10]	,							lowerCamelCase_	,							atol=1e-4					)					)
								self.assertTrue(torch.allclose(outputs.end_logits[:, :10]	,							lowerCamelCase_	,							atol=1e-4					)					)





__SCREAMING_SNAKE_CASE	:							Optional[Any]           =  '''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

__SCREAMING_SNAKE_CASE	:							Optional[int]           =  [{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
__SCREAMING_SNAKE_CASE	:							Optional[int]           =  {
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}

