





"""simple docstring"""
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  _UpperCAmelCase       :


					def __init__( self      :List[Any]					,	__UpperCamelCase      :Optional[int]					,	__UpperCamelCase      :Optional[int]=13					,	__UpperCamelCase      :Optional[Any]=7					,	__UpperCamelCase      :int=False					,	__UpperCamelCase      :Any=True					,	__UpperCamelCase      :List[str]=True					,	__UpperCamelCase      :Union[str, Any]=True					,	__UpperCamelCase      :int=99					,	__UpperCamelCase      :Optional[Any]=32					,	__UpperCamelCase      :List[Any]=5					,	__UpperCamelCase      :Any=4					,	__UpperCamelCase      :Dict=37					,	__UpperCamelCase      :Any="gelu"					,	__UpperCamelCase      :int=0.1					,	__UpperCamelCase      :int=0.1					,	__UpperCamelCase      :Dict=5_12					,	__UpperCamelCase      :List[Any]=16					,	__UpperCamelCase      :Optional[Any]=2					,	__UpperCamelCase      :Tuple=0.02					,	__UpperCamelCase      :Any=3					,	__UpperCamelCase      :int=4					,	__UpperCamelCase      :str=None					,	__UpperCamelCase      :str=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      lowerCamelCase	( self      :List[Any]		):
												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      lowerCamelCase	( self      :Optional[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      :Union[str, Any]					,	__UpperCamelCase      :Dict					,	__UpperCamelCase      :Dict					,	__UpperCamelCase      :Any					,	__UpperCamelCase      :Optional[int]					,	__UpperCamelCase      :Dict					,	__UpperCamelCase      :Optional[int]					,	__UpperCamelCase      :List[Any]		):
												A		=  DPRContextEncoder(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase					,	attention_mask=__UpperCamelCase					,	token_type_ids=__UpperCamelCase		)
												A		=  model(__UpperCamelCase					,	token_type_ids=__UpperCamelCase		)
												A		=  model(__UpperCamelCase		)
												self.parent.assertEqual(result.pooler_output.shape					,	(self.batch_size, self.projection_dim or self.hidden_size)		)


					def      lowerCamelCase	( self      :str					,	__UpperCamelCase      :Union[str, Any]					,	__UpperCamelCase      :int					,	__UpperCamelCase      :Union[str, Any]					,	__UpperCamelCase      :int					,	__UpperCamelCase      :List[str]					,	__UpperCamelCase      :List[str]					,	__UpperCamelCase      :Any		):
												A		=  DPRQuestionEncoder(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase					,	attention_mask=__UpperCamelCase					,	token_type_ids=__UpperCamelCase		)
												A		=  model(__UpperCamelCase					,	token_type_ids=__UpperCamelCase		)
												A		=  model(__UpperCamelCase		)
												self.parent.assertEqual(result.pooler_output.shape					,	(self.batch_size, self.projection_dim or self.hidden_size)		)


					def      lowerCamelCase	( self      :Optional[Any]					,	__UpperCamelCase      :Union[str, Any]					,	__UpperCamelCase      :int					,	__UpperCamelCase      :Union[str, Any]					,	__UpperCamelCase      :Tuple					,	__UpperCamelCase      :str					,	__UpperCamelCase      :Union[str, Any]					,	__UpperCamelCase      :Optional[Any]		):
												A		=  DPRReader(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(
												    __UpperCamelCase					,	attention_mask=__UpperCamelCase					,	)

												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      :Union[str, Any]		):
												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  _UpperCAmelCase       (     lowercase_					,							lowercase_					,							unittest.TestCase			):
					UpperCamelCase								=							(
					    (
					        DPRContextEncoder,
					        DPRQuestionEncoder,
					        DPRReader,
					    )
					    if is_torch_available()
					    else ()
					)
					UpperCamelCase								=							{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

					UpperCamelCase								=							False
					UpperCamelCase								=							False  # why?
					UpperCamelCase								=							False
					UpperCamelCase								=							False


					def      lowerCamelCase	( self      :Optional[int]		):
												A		=  DPRModelTester(self		)
												A		=  ConfigTester(self					,	config_class=__UpperCamelCase					,	hidden_size=37		)


					def      lowerCamelCase	( self      :List[Any]		):
												self.config_tester.run_common_tests()


					def      lowerCamelCase	( self      :int		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_context_encoder(*__UpperCamelCase		)


					def      lowerCamelCase	( self      :Tuple		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_question_encoder(*__UpperCamelCase		)


					def      lowerCamelCase	( self      :Any		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_reader(*__UpperCamelCase		)


					def      lowerCamelCase	( self      :Tuple		):
												A		=  self.model_tester.prepare_config_and_inputs()[0]

												A		=  DPRQuestionEncoder(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()

												with tempfile.TemporaryDirectory() as tmp_dirname:
																			model.save_pretrained(__UpperCamelCase		)
																			A		=  DPRQuestionEncoder.from_pretrained(__UpperCamelCase					,	projection_dim=5_12		)

												self.assertIsNotNone(__UpperCamelCase		)


					@slow
					def      lowerCamelCase	( self      :Dict		):
												for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			A		=  DPRContextEncoder.from_pretrained(__UpperCamelCase		)
																			self.assertIsNotNone(__UpperCamelCase		)

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

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

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







@require_torch
class  _UpperCAmelCase       (     unittest.TestCase			):


					@slow
					def      lowerCamelCase	( self      :Optional[int]		):
												A		=  DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"					,	return_dict=__UpperCamelCase		)
												model.to(__UpperCamelCase		)

												A		=  torch.tensor(
												    [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]					,	dtype=torch.long					,	device=__UpperCamelCase		)  # [CLS] hello, is my dog cute? [SEP]
												A		=  model(__UpperCamelCase		)[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=__UpperCamelCase					,	)
												self.assertTrue(torch.allclose(output[:, :10]					,	__UpperCamelCase					,	atol=1e-4		)		)


					@slow
					def      lowerCamelCase	( self      :List[Any]		):
												A		=  DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"		)
												A		=  DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"		)
												model.to(__UpperCamelCase		)

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

												A		=  model(**__UpperCamelCase		)

												# 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=__UpperCamelCase					,	)

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









"""simple docstring"""
import argparse
import os
import sys
import urllib.request
import zipfile


_snake_case					:		Any							=			['CoLA', 'SST', 'MRPC', 'QQP', 'STS', 'MNLI', 'SNLI', 'QNLI', 'RTE', 'WNLI', 'diagnostic']
_snake_case					:		List[str]							=			{
    'CoLA': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FCoLA.zip?alt=media&token=46d5e637-3411-4188-bc44-5809b5bfb5f4',
    'SST': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FSST-2.zip?alt=media&token=aabc5f6b-e466-44a2-b9b4-cf6337f84ac8',
    'MRPC': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2Fmrpc_dev_ids.tsv?alt=media&token=ec5c0836-31d5-48f4-b431-7480817f1adc',
    'QQP': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FQQP.zip?alt=media&token=700c6acf-160d-4d89-81d1-de4191d02cb5',
    'STS': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FSTS-B.zip?alt=media&token=bddb94a7-8706-4e0d-a694-1109e12273b5',
    'MNLI': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FMNLI.zip?alt=media&token=50329ea1-e339-40e2-809c-10c40afff3ce',
    'SNLI': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FSNLI.zip?alt=media&token=4afcfbb2-ff0c-4b2d-a09a-dbf07926f4df',
    'QNLI': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FQNLIv2.zip?alt=media&token=6fdcf570-0fc5-4631-8456-9505272d1601',
    'RTE': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FRTE.zip?alt=media&token=5efa7e85-a0bb-4f19-8ea2-9e1840f077fb',
    'WNLI': 'https://firebasestorage.googleapis.com/v0/b/mtl-sentence-representations.appspot.com/o/data%2FWNLI.zip?alt=media&token=068ad0a0-ded7-4bd7-99a5-5e00222e0faf',
    'diagnostic': 'https://storage.googleapis.com/mtl-sentence-representations.appspot.com/tsvsWithoutLabels%2FAX.tsv?GoogleAccessId=firebase-adminsdk-0khhl@mtl-sentence-representations.iam.gserviceaccount.com&Expires=2498860800&Signature=DuQ2CSPt2Yfre0C%2BiISrVYrIFaZH1Lc7hBVZDD4ZyR7fZYOMNOUGpi8QxBmTNOrNPjR3z1cggo7WXFfrgECP6FBJSsURv8Ybrue8Ypt%2FTPxbuJ0Xc2FhDi%2BarnecCBFO77RSbfuz%2Bs95hRrYhTnByqu3U%2FYZPaj3tZt5QdfpH2IUROY8LiBXoXS46LE%2FgOQc%2FKN%2BA9SoscRDYsnxHfG0IjXGwHN%2Bf88q6hOmAxeNPx6moDulUF6XMUAaXCSFU%2BnRO2RDL9CapWxj%2BDl7syNyHhB7987hZ80B%2FwFkQ3MEs8auvt5XW1%2Bd4aCU7ytgM69r8JDCwibfhZxpaa4gd50QXQ%3D%3D',
}

_snake_case					:		List[str]							=			'https://dl.fbaipublicfiles.com/senteval/senteval_data/msr_paraphrase_train.txt'
_snake_case					:		List[str]							=			'https://dl.fbaipublicfiles.com/senteval/senteval_data/msr_paraphrase_test.txt'






def  A__    (      UpperCamelCase ,  UpperCamelCase	):
							print(F"Downloading and extracting {task}..."	)
							A		=  F"{task}.zip"
							urllib.request.urlretrieve(TASKaPATH[task] ,  UpperCamelCase	)
							with zipfile.ZipFile(UpperCamelCase	) as zip_ref:
														zip_ref.extractall(UpperCamelCase	)
							os.remove(UpperCamelCase	)
							print("\tCompleted!"	)






def  A__    (      UpperCamelCase ,  UpperCamelCase	):
							print("Processing MRPC..."	)
							A		=  os.path.join(UpperCamelCase ,  "MRPC"	)
							if not os.path.isdir(UpperCamelCase	):
														os.mkdir(UpperCamelCase	)
							if path_to_data:
														A		=  os.path.join(UpperCamelCase ,  "msr_paraphrase_train.txt"	)
														A		=  os.path.join(UpperCamelCase ,  "msr_paraphrase_test.txt"	)
							else:
														print("Local MRPC data not specified, downloading data from %s" % MRPC_TRAIN	)
														A		=  os.path.join(UpperCamelCase ,  "msr_paraphrase_train.txt"	)
														A		=  os.path.join(UpperCamelCase ,  "msr_paraphrase_test.txt"	)
														urllib.request.urlretrieve(UpperCamelCase ,  UpperCamelCase	)
														urllib.request.urlretrieve(UpperCamelCase ,  UpperCamelCase	)
							if not os.path.isfile(UpperCamelCase	):
														raise ValueError(F"Train data not found at {mrpc_train_file}"	)
							if not os.path.isfile(UpperCamelCase	):
														raise ValueError(F"Test data not found at {mrpc_test_file}"	)
							urllib.request.urlretrieve(TASKaPATH["MRPC"] ,  os.path.join(UpperCamelCase ,  "dev_ids.tsv"	)	)

							A		=  []
							with open(os.path.join(UpperCamelCase ,  "dev_ids.tsv"	) ,  encoding="utf8"	) as ids_fh:
														for row in ids_fh:
																					dev_ids.append(row.strip().split("\t"	)	)

							with open(UpperCamelCase ,  encoding="utf8"	) as data_fh, open(
							    os.path.join(UpperCamelCase ,  "train.tsv"	) ,  "w" ,  encoding="utf8"	) as train_fh, open(os.path.join(UpperCamelCase ,  "dev.tsv"	) ,  "w" ,  encoding="utf8"	) as dev_fh:
														A		=  data_fh.readline()
														train_fh.write(UpperCamelCase	)
														dev_fh.write(UpperCamelCase	)
														for row in data_fh:
																					A,     A,     A,     A,     A		=  row.strip().split("\t"	)
																					if [ida, ida] in dev_ids:
																												dev_fh.write("%s\t%s\t%s\t%s\t%s\n" % (label, ida, ida, sa, sa)	)
																					else:
																												train_fh.write("%s\t%s\t%s\t%s\t%s\n" % (label, ida, ida, sa, sa)	)

							with open(UpperCamelCase ,  encoding="utf8"	) as data_fh, open(
							    os.path.join(UpperCamelCase ,  "test.tsv"	) ,  "w" ,  encoding="utf8"	) as test_fh:
														A		=  data_fh.readline()
														test_fh.write("index\t#1 ID\t#2 ID\t#1 String\t#2 String\n"	)
														for idx, row in enumerate(UpperCamelCase	):
																					A,     A,     A,     A,     A		=  row.strip().split("\t"	)
																					test_fh.write("%d\t%s\t%s\t%s\t%s\n" % (idx, ida, ida, sa, sa)	)
							print("\tCompleted!"	)






def  A__    (      UpperCamelCase	):
							print("Downloading and extracting diagnostic..."	)
							if not os.path.isdir(os.path.join(UpperCamelCase ,  "diagnostic"	)	):
														os.mkdir(os.path.join(UpperCamelCase ,  "diagnostic"	)	)
							A		=  os.path.join(UpperCamelCase ,  "diagnostic" ,  "diagnostic.tsv"	)
							urllib.request.urlretrieve(TASKaPATH["diagnostic"] ,  UpperCamelCase	)
							print("\tCompleted!"	)
							return






def  A__    (      UpperCamelCase	):
							A		=  task_names.split(","	)
							if "all" in task_names:
														A		=  TASKS
							else:
														A		=  []
														for task_name in task_names:
																					if task_name not in TASKS:
																												raise ValueError(F"Task {task_name} not found!"	)
																					tasks.append(UpperCamelCase	)
							return tasks






def  A__    (      UpperCamelCase	):
							A		=  argparse.ArgumentParser()
							parser.add_argument("--data_dir" ,  help="directory to save data to" ,  type=UpperCamelCase ,  default="glue_data"	)
							parser.add_argument(
							    "--tasks" ,  help="tasks to download data for as a comma separated string" ,  type=UpperCamelCase ,  default="all"	)
							parser.add_argument(
							    "--path_to_mrpc" ,  help="path to directory containing extracted MRPC data, msr_paraphrase_train.txt and msr_paraphrase_text.txt" ,  type=UpperCamelCase ,  default="" ,  )
							A		=  parser.parse_args(UpperCamelCase	)

							if not os.path.isdir(args.data_dir	):
														os.mkdir(args.data_dir	)
							A		=  get_tasks(args.tasks	)

							for task in tasks:
														if task == "MRPC":
																					format_mrpc(args.data_dir ,  args.path_to_mrpc	)
														elif task == "diagnostic":
																					download_diagnostic(args.data_dir	)
														else:
																					download_and_extract(UpperCamelCase ,  args.data_dir	)


if __name__ == "__main__":
		sys.exit(main(sys.argv[1:]))


