




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

					def __init__(    self			,	__lowercase			,	__lowercase=13			,	__lowercase=7			,	__lowercase=False			,	__lowercase=True			,	__lowercase=True			,	__lowercase=True			,	__lowercase=99			,	__lowercase=32			,	__lowercase=5			,	__lowercase=4			,	__lowercase=37			,	__lowercase="gelu"			,	__lowercase=0.1			,	__lowercase=0.1			,	__lowercase=512			,	__lowercase=16			,	__lowercase=2			,	__lowercase=0.02			,	__lowercase=3			,	__lowercase=4			,	__lowercase=None			,	__lowercase=0			,	):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[int]							       =							parent
						lowerCamelCase_							:		Dict							       =							batch_size
						lowerCamelCase_							:		Any							       =							seq_length
						lowerCamelCase_							:		Dict							       =							is_training
						lowerCamelCase_							:		Tuple							       =							use_input_mask
						lowerCamelCase_							:		str							       =							use_token_type_ids
						lowerCamelCase_							:		List[Any]							       =							use_labels
						lowerCamelCase_							:		Union[str, Any]							       =							vocab_size
						lowerCamelCase_							:		int							       =							hidden_size
						lowerCamelCase_							:		Dict							       =							num_hidden_layers
						lowerCamelCase_							:		List[str]							       =							num_attention_heads
						lowerCamelCase_							:		Tuple							       =							intermediate_size
						lowerCamelCase_							:		int							       =							hidden_act
						lowerCamelCase_							:		List[Any]							       =							hidden_dropout_prob
						lowerCamelCase_							:		int							       =							attention_probs_dropout_prob
						lowerCamelCase_							:		int							       =							max_position_embeddings
						lowerCamelCase_							:		Union[str, Any]							       =							type_vocab_size
						lowerCamelCase_							:		Optional[Any]							       =							type_sequence_label_size
						lowerCamelCase_							:		Dict							       =							initializer_range
						lowerCamelCase_							:		int							       =							num_labels
						lowerCamelCase_							:		str							       =							num_choices
						lowerCamelCase_							:		int							       =							scope
						lowerCamelCase_							:		Any							       =							projection_dim

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		int							       =							ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size )

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

						lowerCamelCase_							:		List[str]							       =							None
						if self.use_token_type_ids:
							lowerCamelCase_							:		Optional[Any]							       =							ids_tensor([self.batch_size, self.seq_length]			,	self.type_vocab_size )

						lowerCamelCase_							:		Dict							       =							None
						lowerCamelCase_							:		Tuple							       =							None
						lowerCamelCase_							:		Dict							       =							None
						if self.use_labels:
							lowerCamelCase_							:		str							       =							ids_tensor([self.batch_size]			,	self.type_sequence_label_size )
							lowerCamelCase_							:		Optional[int]							       =							ids_tensor([self.batch_size, self.seq_length]			,	self.num_labels )
							lowerCamelCase_							:		int							       =							ids_tensor([self.batch_size]			,	self.num_choices )

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

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

					def      lowercase_							(    self ):







						'''simple docstring'''




						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      lowercase_							(    self			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase ):







						'''simple docstring'''




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

					def      lowercase_							(    self			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase ):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							       =							DPRQuestionEncoder(config=__lowercase )
						model.to(__lowercase )
						model.eval()
						lowerCamelCase_							:		Optional[Any]							       =							model(__lowercase			,	attention_mask=__lowercase			,	token_type_ids=__lowercase )
						lowerCamelCase_							:		List[Any]							       =							model(__lowercase			,	token_type_ids=__lowercase )
						lowerCamelCase_							:		Optional[Any]							       =							model(__lowercase )
						self.parent.assertEqual(result.pooler_output.shape			,	(self.batch_size, self.projection_dim or self.hidden_size) )

					def      lowercase_							(    self			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase ):







						'''simple docstring'''




						lowerCamelCase_							:		Any							       =							DPRReader(config=__lowercase )
						model.to(__lowercase )
						model.eval()
						lowerCamelCase_							:		str							       =							model(
						    __lowercase			,	attention_mask=__lowercase			,	)

						self.parent.assertEqual(result.start_logits.shape			,	(self.batch_size, self.seq_length) )
						self.parent.assertEqual(result.end_logits.shape			,	(self.batch_size, self.seq_length) )
						self.parent.assertEqual(result.relevance_logits.shape			,	(self.batch_size,) )





					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							       =							self.prepare_config_and_inputs()
						(
						    (
						    lowerCamelCase_
						)       ,      (
						    lowerCamelCase_
						)       ,      (
						    lowerCamelCase_
						)       ,      (
						    lowerCamelCase_
						)       ,      (
						    lowerCamelCase_
						)       ,      (
						    lowerCamelCase_
						)       ,      (
						    lowerCamelCase_
						)       ,      
						)							:		Tuple							       =							config_and_inputs
						lowerCamelCase_							:		Dict							       =							{'input_ids': input_ids}
						return config, inputs_dict


@require_torch
class     __SCREAMING_SNAKE_CASE							(		lowerCamelCase       ,					lowerCamelCase       ,					unittest.TestCase  ):
					_lowerCamelCase		  =			(
					    (
					        DPRContextEncoder,
					        DPRQuestionEncoder,
					        DPRReader,
					    )
					    if is_torch_available()
					    else ()
					)
					_lowerCamelCase		  =			{"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

					_lowerCamelCase		  =			False
					_lowerCamelCase		  =			False  # why?
					_lowerCamelCase		  =			False
					_lowerCamelCase		  =			False

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		int							       =							DPRModelTester(self )
						lowerCamelCase_							:		List[Any]							       =							ConfigTester(self			,	config_class=__lowercase			,	hidden_size=37 )

					def      lowercase_							(    self ):







						'''simple docstring'''




						self.config_tester.run_common_tests()

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_context_encoder(*__lowercase )

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[Any]							       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_question_encoder(*__lowercase )

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		str							       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_reader(*__lowercase )

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							       =							self.model_tester.prepare_config_and_inputs()[0]

						lowerCamelCase_							:		Dict							       =							DPRQuestionEncoder(config=__lowercase )
						model.to(__lowercase )
						model.eval()

						with tempfile.TemporaryDirectory() as tmp_dirname:
							model.save_pretrained(__lowercase )
							lowerCamelCase_							:		Union[str, Any]							       =							DPRQuestionEncoder.from_pretrained(__lowercase			,	projection_dim=512 )

						self.assertIsNotNone(__lowercase )





					@slow
					def      lowercase_							(    self ):







						'''simple docstring'''




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

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

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

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


@require_torch
class     __SCREAMING_SNAKE_CASE							(		unittest.TestCase  ):

					@slow
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							       =							DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'			,	return_dict=__lowercase )
						model.to(__lowercase )

						lowerCamelCase_							:		Any							       =							torch.tensor(
						    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]			,	dtype=torch.long			,	device=__lowercase )  # [CLS] hello, is my dog cute? [SEP]
						lowerCamelCase_							:		List[Any]							       =							model(__lowercase )[0]  # embedding shape = (1, 768)
						# compare the actual values for a slice.
						lowerCamelCase_							:		Any							       =							torch.tensor(
						    [
						        [
						            0.03_23_62_53,
						            0.12_75_33_35,
						            0.16_81_85_09,
						            0.00_27_97_86,
						            0.3_89_69_33,
						            0.24_26_49_45,
						            0.2_17_89_71,
						            -0.02_33_52_27,
						            -0.08_48_19_59,
						            -0.14_32_41_17,
						        ]
						    ]			,	dtype=torch.float			,	device=__lowercase			,	)
						self.assertTrue(torch.allclose(output[:, :10]			,	__lowercase			,	atol=1E-4 ) )





					@slow
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[Any]							       =							DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base' )
						lowerCamelCase_							:		List[str]							       =							DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base' )
						model.to(__lowercase )

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

						lowerCamelCase_							:		Dict							       =							model(**__lowercase )

						# compare the actual values for a slice.
						lowerCamelCase_							:		Dict							       =							torch.tensor(
						    [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]			,	dtype=torch.float			,	device=__lowercase			,	)

						lowerCamelCase_							:		List[str]							       =							torch.tensor(
						    [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]			,	dtype=torch.float			,	device=__lowercase			,	)
						self.assertTrue(torch.allclose(outputs.start_logits[:, :10]			,	__lowercase			,	atol=1E-4 ) )
						self.assertTrue(torch.allclose(outputs.end_logits[:, :10]			,	__lowercase			,	atol=1E-4 ) )






from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


UpperCamelCase__		=						logging.get_logger(__name__)

UpperCamelCase__		=						{
    'facebook/m2m100_418M': 'https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}


class     __SCREAMING_SNAKE_CASE							(		lowerCamelCase  ):
					_lowerCamelCase		  =			"""m2m_100"""
					_lowerCamelCase		  =			["""past_key_values"""]
					_lowerCamelCase		  =			{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

					def __init__(    self			,	__lowercase=128_112			,	__lowercase=1_024			,	__lowercase=12			,	__lowercase=4_096			,	__lowercase=16			,	__lowercase=12			,	__lowercase=4_096			,	__lowercase=16			,	__lowercase=0.05			,	__lowercase=0.05			,	__lowercase=True			,	__lowercase=True			,	__lowercase="relu"			,	__lowercase=1_024			,	__lowercase=0.1			,	__lowercase=0.1			,	__lowercase=0.0			,	__lowercase=0.02			,	__lowercase=2			,	__lowercase=True			,	__lowercase=1			,	__lowercase=0			,	__lowercase=2			,	**__lowercase			,	):







						'''simple docstring'''




						lowerCamelCase_							:		int							       =							vocab_size
						lowerCamelCase_							:		List[str]							       =							max_position_embeddings
						lowerCamelCase_							:		Tuple							       =							d_model
						lowerCamelCase_							:		Optional[int]							       =							encoder_ffn_dim
						lowerCamelCase_							:		Union[str, Any]							       =							encoder_layers
						lowerCamelCase_							:		Dict							       =							encoder_attention_heads
						lowerCamelCase_							:		Union[str, Any]							       =							decoder_ffn_dim
						lowerCamelCase_							:		List[str]							       =							decoder_layers
						lowerCamelCase_							:		Union[str, Any]							       =							decoder_attention_heads
						lowerCamelCase_							:		Optional[int]							       =							dropout
						lowerCamelCase_							:		str							       =							attention_dropout
						lowerCamelCase_							:		Optional[Any]							       =							activation_dropout
						lowerCamelCase_							:		str							       =							activation_function
						lowerCamelCase_							:		int							       =							init_std
						lowerCamelCase_							:		Optional[int]							       =							encoder_layerdrop
						lowerCamelCase_							:		Tuple							       =							decoder_layerdrop
						lowerCamelCase_							:		Tuple							       =							use_cache
						lowerCamelCase_							:		Any							       =							encoder_layers
						lowerCamelCase_							:		Any							       =							scale_embedding  # scale factor will be sqrt(d_model) if True

						super().__init__(
						    pad_token_id=__lowercase			,	bos_token_id=__lowercase			,	eos_token_id=__lowercase			,	is_encoder_decoder=__lowercase			,	decoder_start_token_id=__lowercase			,	**__lowercase			,	)


class     __SCREAMING_SNAKE_CASE							(		lowerCamelCase  ):

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Any							       =							OrderedDict(
						    [
						        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
						        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
						    ] )

						if self.use_past:
							lowerCamelCase_							:		Union[str, Any]							       =							{0: 'batch'}
							lowerCamelCase_							:		Optional[Any]							       =							{0: 'batch', 1: 'past_decoder_sequence + sequence'}
						else:
							lowerCamelCase_							:		Any							       =							{0: 'batch', 1: 'decoder_sequence'}
							lowerCamelCase_							:		Union[str, Any]							       =							{0: 'batch', 1: 'decoder_sequence'}

						if self.use_past:
							self.fill_with_past_key_values_(__lowercase			,	direction='inputs' )
						return common_inputs

					def      lowercase_							(    self			,	__lowercase			,	__lowercase = -1			,	__lowercase = -1			,	__lowercase = False			,	__lowercase = None			,	):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[int]							       =							compute_effective_axis_dimension(
						    __lowercase			,	fixed_dimension=OnnxConfig.default_fixed_batch			,	num_token_to_add=0 )

						# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
						lowerCamelCase_							:		str							       =							tokenizer.num_special_tokens_to_add(__lowercase )
						lowerCamelCase_							:		Dict							       =							compute_effective_axis_dimension(
						    __lowercase			,	fixed_dimension=OnnxConfig.default_fixed_sequence			,	num_token_to_add=__lowercase )

						# Generate dummy inputs according to compute batch and sequence
						lowerCamelCase_							:		Optional[int]							       =							[' '.join([tokenizer.unk_token] ) * seq_length] * batch_size
						lowerCamelCase_							:		Optional[Any]							       =							dict(tokenizer(__lowercase			,	return_tensors=__lowercase ) )
						return common_inputs

					def      lowercase_							(    self			,	__lowercase			,	__lowercase = -1			,	__lowercase = -1			,	__lowercase = False			,	__lowercase = None			,	):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[Any]							       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    __lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase )

						# Generate decoder inputs
						lowerCamelCase_							:		Dict							       =							seq_length if not self.use_past else 1
						lowerCamelCase_							:		List[str]							       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    __lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase )
						lowerCamelCase_							:		Any							       =							{f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
						lowerCamelCase_							:		str							       =							dict(**__lowercase			,	**__lowercase )

						if self.use_past:
							if not is_torch_available():
								raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' )
							else:
								import torch
							lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							common_inputs['input_ids'].shape
							lowerCamelCase_							:		List[Any]							       =							common_inputs['decoder_input_ids'].shape[1]
							lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							self.num_attention_heads
							lowerCamelCase_							:		Any							       =							(
							    batch,
							    num_encoder_attention_heads,
							    encoder_seq_length,
							    self._config.hidden_size // num_encoder_attention_heads,
							)
							lowerCamelCase_							:		Tuple							       =							decoder_seq_length + 3
							lowerCamelCase_							:		Any							       =							(
							    batch,
							    num_decoder_attention_heads,
							    decoder_past_length,
							    self._config.hidden_size // num_decoder_attention_heads,
							)

							lowerCamelCase_							:		List[Any]							       =							torch.cat(
							    [common_inputs['decoder_attention_mask'], torch.ones(__lowercase			,	__lowercase )]			,	dim=1 )

							lowerCamelCase_							:		Tuple							       =							[]
							# If the number of encoder and decoder layers are present in the model configuration, both are considered
							lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							self.num_layers
							lowerCamelCase_							:		Optional[int]							       =							min(__lowercase			,	__lowercase )
							lowerCamelCase_							:		int							       =							max(__lowercase			,	__lowercase ) - min_num_layers
							lowerCamelCase_							:		Union[str, Any]							       =							'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

							for _ in range(__lowercase ):
								common_inputs["past_key_values"].append(
								    (
								        torch.zeros(__lowercase ),
								        torch.zeros(__lowercase ),
								        torch.zeros(__lowercase ),
								        torch.zeros(__lowercase ),
								    ) )
							# TODO: test this.
							lowerCamelCase_							:		Optional[int]							       =							encoder_shape if remaining_side_name == 'encoder' else decoder_shape
							for _ in range(__lowercase			,	__lowercase ):
								common_inputs["past_key_values"].append((torch.zeros(__lowercase ), torch.zeros(__lowercase )) )
						return common_inputs





					_lowerCamelCase		  =			_generate_dummy_inputs_for_default_and_seqaseq_lm
