



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







							'''simple docstring'''






							def __init__(							self					,					a_					,					a_=13					,					a_=7					,					a_=False					,					a_=True					,					a_=True					,					a_=True					,					a_=99					,					a_=32					,					a_=5					,					a_=4					,					a_=37					,					a_="gelu"					,					a_=0.1					,					a_=0.1					,					a_=512					,					a_=16					,					a_=2					,					a_=0.02					,					a_=3					,					a_=4					,					a_=None					,					a_=0					,					):
											a__			 =						parent
											a__			 =						batch_size
											a__			 =						seq_length
											a__			 =						is_training
											a__			 =						use_input_mask
											a__			 =						use_token_type_ids
											a__			 =						use_labels
											a__			 =						vocab_size
											a__			 =						hidden_size
											a__			 =						num_hidden_layers
											a__			 =						num_attention_heads
											a__			 =						intermediate_size
											a__			 =						hidden_act
											a__			 =						hidden_dropout_prob
											a__			 =						attention_probs_dropout_prob
											a__			 =						max_position_embeddings
											a__			 =						type_vocab_size
											a__			 =						type_sequence_label_size
											a__			 =						initializer_range
											a__			 =						num_labels
											a__			 =						num_choices
											a__			 =						scope
											a__			 =						projection_dim


							def _a (							self						):
											a__			 =						ids_tensor([self.batch_size, self.seq_length]					,					self.vocab_size						)

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

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

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

											a__			 =						self.get_config()

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


							def _a (							self						):
											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 _a (							self					,					a_					,					a_					,					a_					,					a_					,					a_					,					a_					,					a_						):
											a__			 =						DPRContextEncoder(config=a_						)
											model.to(a_						)
											model.eval()
											a__			 =						model(a_					,					attention_mask=a_					,					token_type_ids=a_						)
											a__			 =						model(a_					,					token_type_ids=a_						)
											a__			 =						model(a_						)
											self.parent.assertEqual(result.pooler_output.shape					,					(self.batch_size, self.projection_dim or self.hidden_size)						)


							def _a (							self					,					a_					,					a_					,					a_					,					a_					,					a_					,					a_					,					a_						):
											a__			 =						DPRQuestionEncoder(config=a_						)
											model.to(a_						)
											model.eval()
											a__			 =						model(a_					,					attention_mask=a_					,					token_type_ids=a_						)
											a__			 =						model(a_					,					token_type_ids=a_						)
											a__			 =						model(a_						)
											self.parent.assertEqual(result.pooler_output.shape					,					(self.batch_size, self.projection_dim or self.hidden_size)						)


							def _a (							self					,					a_					,					a_					,					a_					,					a_					,					a_					,					a_					,					a_						):
											a__			 =						DPRReader(config=a_						)
											model.to(a_						)
											model.eval()
											a__			 =						model(
											    a_					,					attention_mask=a_					,					)

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




@require_torch
class 	__snake_case (				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,unittest.TestCase):







							'''simple docstring'''






							UpperCamelCase__			: List[str]             =   (
							    (
							        DPRContextEncoder,
							        DPRQuestionEncoder,
							        DPRReader,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCamelCase__			: Any             =   {"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

							UpperCamelCase__			: Dict             =   False
							UpperCamelCase__			: Optional[int]             =   False  # why?
							UpperCamelCase__			: Optional[int]             =   False
							UpperCamelCase__			: Optional[int]             =   False


							def _a (							self						):
											a__			 =						DPRModelTester(self						)
											a__			 =						ConfigTester(self					,					config_class=a_					,					hidden_size=37						)


							def _a (							self						):
											self.config_tester.run_common_tests()


							def _a (							self						):
											a__			 =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_context_encoder(*a_						)


							def _a (							self						):
											a__			 =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_question_encoder(*a_						)


							def _a (							self						):
											a__			 =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_reader(*a_						)


							def _a (							self						):
											a__			 =						self.model_tester.prepare_config_and_inputs()[0]

											a__			 =						DPRQuestionEncoder(config=a_						)
											model.to(a_						)
											model.eval()

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

											self.assertIsNotNone(a_						)







							@slow
							def _a (							self						):
											for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															a__			 =						DPRContextEncoder.from_pretrained(a_						)
															self.assertIsNotNone(a_						)

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

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

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




@require_torch
class 	__snake_case (				unittest.TestCase):







							'''simple docstring'''






							@slow
							def _a (							self						):
											a__			 =						DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""					,					return_dict=a_						)
											model.to(a_						)

											a__			 =						torch.tensor(
											    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]					,					dtype=torch.long					,					device=a_						)  # [CLS] hello, is my dog cute? [SEP]
											a__			 =						model(a_						)[0]  # embedding shape = (1, 768)
											# compare the actual values for a slice.
											a__			 =						torch.tensor(
											    [
											        [
											            0.03_236_253,
											            0.12_753_335,
											            0.16_818_509,
											            0.00_279_786,
											            0.3_896_933,
											            0.24_264_945,
											            0.2_178_971,
											            -0.02_335_227,
											            -0.08_481_959,
											            -0.14_324_117,
											        ]
											    ]					,					dtype=torch.float					,					device=a_					,					)
											self.assertTrue(torch.allclose(output[:, :10]					,					a_					,					atol=1E-4						)						)







							@slow
							def _a (							self						):
											a__			 =						DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
											a__			 =						DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
											model.to(a_						)

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

											a__			 =						model(**a_						)

											# compare the actual values for a slice.
											a__			 =						torch.tensor(
											    [[-10.3_005, -10.7_765, -11.4_872, -11.6_841, -11.9_312, -10.3_002, -9.8_544, -11.7_378, -12.0_821, -10.2_975]]					,					dtype=torch.float					,					device=a_					,					)

											a__			 =						torch.tensor(
											    [[-11.0_684, -11.7_041, -11.5_397, -10.3_465, -10.8_791, -6.8_443, -11.9_959, -11.0_364, -10.0_096, -6.8_405]]					,					dtype=torch.float					,					device=a_					,					)
											self.assertTrue(torch.allclose(outputs.start_logits[:, :10]					,					a_					,					atol=1E-4						)						)
											self.assertTrue(torch.allclose(outputs.end_logits[:, :10]					,					a_					,					atol=1E-4						)						)






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast

@require_vision
class 	__snake_case (				unittest.TestCase):







							'''simple docstring'''






							def _a (							self						):
											a__			 =						tempfile.mkdtemp()

											a__			 =						CLIPImageProcessor()
											a__			 =						BertTokenizer.from_pretrained(
											    """hf-internal-testing/tiny-random-BertModel"""					,					model_input_names=["""input_ids""", """attention_mask"""]						)

											a__			 =						GitProcessor(a_					,					a_						)

											processor.save_pretrained(self.tmpdirname						)


							def _a (							self					,					**a_						):
											return AutoProcessor.from_pretrained(self.tmpdirname					,					**a_						).tokenizer


							def _a (							self					,					**a_						):
											return AutoProcessor.from_pretrained(self.tmpdirname					,					**a_						).image_processor


							def _a (							self						):
											shutil.rmtree(self.tmpdirname						)


							def _a (							self						):
											a__			 =						[np.random.randint(255					,					size=(3, 30, 400)					,					dtype=np.uinta						)]

											a__			 =						[Image.fromarray(np.moveaxis(a_					,					0					,					-1						)						) for x in image_inputs]

											return image_inputs


							def _a (							self						):
											a__			 =						GitProcessor(tokenizer=self.get_tokenizer()					,					image_processor=self.get_image_processor()						)
											processor.save_pretrained(self.tmpdirname						)

											a__			 =						self.get_tokenizer(bos_token="""(BOS)"""					,					eos_token="""(EOS)"""						)
											a__			 =						self.get_image_processor(do_normalize=a_					,					padding_value=1.0						)

											a__			 =						GitProcessor.from_pretrained(
											    self.tmpdirname					,					bos_token="""(BOS)"""					,					eos_token="""(EOS)"""					,					do_normalize=a_					,					padding_value=1.0						)

											self.assertEqual(processor.tokenizer.get_vocab()					,					tokenizer_add_kwargs.get_vocab()						)
											self.assertIsInstance(processor.tokenizer					,					a_						)

											self.assertEqual(processor.image_processor.to_json_string()					,					image_processor_add_kwargs.to_json_string()						)
											self.assertIsInstance(processor.image_processor					,					a_						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						self.prepare_image_inputs()

											a__			 =						image_processor(a_					,					return_tensors="""np"""						)
											a__			 =						processor(images=a_					,					return_tensors="""np"""						)

											for key in input_feat_extract.keys():
															self.assertAlmostEqual(input_feat_extract[key].sum()					,					input_processor[key].sum()					,					delta=1E-2						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""

											a__			 =						processor(text=a_						)

											a__			 =						tokenizer(a_					,					return_token_type_ids=a_						)

											for key in encoded_tok.keys():
															self.assertListEqual(encoded_tok[key]					,					encoded_processor[key]						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""
											a__			 =						self.prepare_image_inputs()

											a__			 =						processor(text=a_					,					images=a_						)

											self.assertListEqual(list(inputs.keys()						)					,					["""input_ids""", """attention_mask""", """pixel_values"""]						)

											# test if it raises when no input is passed
											with pytest.raises(a_						):
															processor()


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											a__			 =						processor.batch_decode(a_						)
											a__			 =						tokenizer.batch_decode(a_						)

											self.assertListEqual(a_					,					a_						)







							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""
											a__			 =						self.prepare_image_inputs()

											a__			 =						processor(text=a_					,					images=a_						)

											# For now the processor supports only ['input_ids', 'attention_mask', 'pixel_values']
											self.assertListEqual(list(inputs.keys()						)					,					["""input_ids""", """attention_mask""", """pixel_values"""]						)

