


import os
import tempfile
from pathlib import Path
from unittest import TestCase

import pyarrow as pa
import pytest

from datasets.arrow_dataset import Dataset
from datasets.arrow_reader import ArrowReader, BaseReader, FileInstructions, ReadInstruction, make_file_instructions
from datasets.info import DatasetInfo
from datasets.splits import NamedSplit, Split, SplitDict, SplitInfo

from .utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases



class 							a       (      _UpperCAmelCase ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]=False				):
        __lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase:					Union[str, Any]	 =						(
            filename_skip_take["""filename"""],
            filename_skip_take["""skip"""] if """skip""" in filename_skip_take else None,
            filename_skip_take["""take"""] if """take""" in filename_skip_take else None,
        )
        open(os.path.join(SCREAMING_SNAKE_CASE_				)     ,						"""wb"""				).close()
        __lowerCamelCase:					List[str]	 =						pa.Table.from_pydict({"""filename""": [Path(SCREAMING_SNAKE_CASE_				).name] * 100}				)
        if take == -1:
           __lowerCamelCase:					Tuple	 =						len(SCREAMING_SNAKE_CASE_				) - skip
        if skip is not None and take is not None:
           __lowerCamelCase:					List[str]	 =						pa_table.slice(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)
        return pa_table



class 							a       (      _UpperCAmelCase ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int				):
        __lowerCamelCase:					List[str]	 =						"""my_name"""
        __lowerCamelCase:					str	 =						SplitInfo(name="""train"""     ,						num_examples=100				)
        __lowerCamelCase:					Optional[int]	 =						SplitInfo(name="""test"""     ,						num_examples=100				)
        __lowerCamelCase:					Tuple	 =						[train_info, test_info]
        __lowerCamelCase:					Union[str, Any]	 =						SplitDict()
        split_dict.add(SCREAMING_SNAKE_CASE_				)
        split_dict.add(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Dict	 =						DatasetInfo(splits=SCREAMING_SNAKE_CASE_				)

        with tempfile.TemporaryDirectory() as tmp_dir:
           __lowerCamelCase:					Union[str, Any]	 =						ReaderTest(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)

           __lowerCamelCase:					int	 =						"""test[:33%]"""
           __lowerCamelCase:					int	 =						Dataset(**reader.read(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)				)
           self.assertEqual(dset["""filename"""][0]     ,						F'{name}-test'				)
           self.assertEqual(dset.num_rows     ,						33				)
           self.assertEqual(dset.num_columns     ,						1				)

           __lowerCamelCase:					Dict	 =						["""train""", """test[:33%]"""]
           __lowerCamelCase:					List[str]	 =						[Split.TRAIN, ReadInstruction.from_spec("""test[:33%]"""				)]
           for instructions in [instructionsa, instructionsa]:
              __lowerCamelCase:					Tuple	 =						[reader.read(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				) for instr in instructions]
              __lowerCamelCase ,  __lowerCamelCase:					List[str]	 =						(Dataset(**SCREAMING_SNAKE_CASE_				) for dataset_kwargs in datasets_kwargs)
              self.assertEqual(train_dset["""filename"""][0]     ,						F'{name}-train'				)
              self.assertEqual(train_dset.num_rows     ,						100				)
              self.assertEqual(train_dset.num_columns     ,						1				)
              self.assertIsInstance(train_dset.split     ,						SCREAMING_SNAKE_CASE_				)
              self.assertEqual(str(train_dset.split				)     ,						"""train"""				)
              self.assertEqual(test_dset["""filename"""][0]     ,						F'{name}-test'				)
              self.assertEqual(test_dset.num_rows     ,						33				)
              self.assertEqual(test_dset.num_columns     ,						1				)
              self.assertIsInstance(test_dset.split     ,						SCREAMING_SNAKE_CASE_				)
              self.assertEqual(str(test_dset.split				)     ,						"""test[:33%]"""				)
              del train_dset, test_dset





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[int]				):
        __lowerCamelCase:					Dict	 =						"""my_name"""
        __lowerCamelCase:					List[str]	 =						SplitInfo(name="""train"""     ,						num_examples=1000     ,						shard_lengths=[100] * 10				)
        __lowerCamelCase:					Optional[int]	 =						[train_info]
        __lowerCamelCase:					Optional[Any]	 =						SplitDict()
        split_dict.add(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Any	 =						DatasetInfo(splits=SCREAMING_SNAKE_CASE_				)

        with tempfile.TemporaryDirectory() as tmp_dir:
           __lowerCamelCase:					List[Any]	 =						ReaderTest(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)

           __lowerCamelCase:					Dict	 =						"""train[:33%]"""
           __lowerCamelCase:					List[Any]	 =						Dataset(**reader.read(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)				)
           self.assertEqual(dset["""filename"""][0]     ,						F'{name}-train-00000-of-00010'				)
           self.assertEqual(dset["""filename"""][-1]     ,						F'{name}-train-00003-of-00010'				)
           self.assertEqual(dset.num_rows     ,						330				)
           self.assertEqual(dset.num_columns     ,						1				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any				):
        __lowerCamelCase:					List[str]	 =						SplitInfo(name="""train"""     ,						num_examples=100				)
        __lowerCamelCase:					List[Any]	 =						SplitInfo(name="""test"""     ,						num_examples=100				)
        __lowerCamelCase:					List[Any]	 =						SplitDict()
        split_dict.add(SCREAMING_SNAKE_CASE_				)
        split_dict.add(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Any	 =						DatasetInfo(splits=SCREAMING_SNAKE_CASE_				)

        with tempfile.TemporaryDirectory() as tmp_dir:
           __lowerCamelCase:					int	 =						ReaderTest(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)

           __lowerCamelCase:					List[Any]	 =						[
               {"""filename""": os.path.join(SCREAMING_SNAKE_CASE_     ,						"""train"""				)},
               {"""filename""": os.path.join(SCREAMING_SNAKE_CASE_     ,						"""test"""				), """skip""": 10, """take""": 10},
           ]
           __lowerCamelCase:					Union[str, Any]	 =						Dataset(**reader.read_files(SCREAMING_SNAKE_CASE_     ,						original_instructions="""train+test[10:20]"""				)				)
           self.assertEqual(dset.num_rows     ,						110				)
           self.assertEqual(dset.num_columns     ,						1				)
           del dset



@pytest.mark.parametrize("""in_memory"""    ,   [False, True]			)
def      __lowerCAmelCase    (				snake_case						:     int    ,   snake_case						:     int    ,   snake_case						:     Optional[int]			)							->				List[Any]:
   __lowerCamelCase:					Optional[Any]	 =						arrow_file
   with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
      __lowerCamelCase:					Optional[Any]	 =						ArrowReader.read_table(snake_case    ,   in_memory=snake_case			)
   assert table.shape == dataset.data.shape
   assert set(table.column_names			) == set(dataset.data.column_names			)
   assert dict(table.to_pydict()			) == dict(dataset.data.to_pydict()			)  # to_pydict returns OrderedDict



@pytest.mark.parametrize("""in_memory"""    ,   [False, True]			)
def      __lowerCAmelCase    (				snake_case						:     Any    ,   snake_case						:     Tuple    ,   snake_case						:     int			)							->				Dict:
   __lowerCamelCase:					Dict	 =						arrow_file
   __lowerCamelCase:					Optional[int]	 =						ArrowReader(""""""    ,   snake_case			)
   with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
      __lowerCamelCase:					Optional[Any]	 =						reader.read_files([{"""filename""": filename}]    ,   in_memory=snake_case			)
   assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
   __lowerCamelCase:					str	 =						dataset_kwargs["""arrow_table"""]
   assert table.shape == dataset.data.shape
   assert set(table.column_names			) == set(dataset.data.column_names			)
   assert dict(table.to_pydict()			) == dict(dataset.data.to_pydict()			)  # to_pydict returns OrderedDict



def      __lowerCAmelCase    (				)							->				Optional[int]:
   assert ReadInstruction("""train"""    ,   to=10    ,   unit="""abs"""			).to_spec() == "train[:10]"
   assert ReadInstruction("""train"""    ,   from_=-80    ,   to=10    ,   unit="""%"""			).to_spec() == "train[-80%:10%]"

   __lowerCamelCase:					str	 =						"""train+test"""
   assert ReadInstruction.from_spec(snake_case			).to_spec() == spec_train_test

   __lowerCamelCase:					List[Any]	 =						"""train[2:10]"""
   assert ReadInstruction.from_spec(snake_case			).to_spec() == spec_train_abs

   __lowerCamelCase:					Optional[int]	 =						"""train[15%:-20%]"""
   assert ReadInstruction.from_spec(snake_case			).to_spec() == spec_train_pct

   __lowerCamelCase:					List[Any]	 =						"""train[:10%](closest)"""
   assert ReadInstruction.from_spec(snake_case			).to_spec() == "train[:10%]"

   __lowerCamelCase:					Any	 =						"""train[:10%](pct1_dropremainder)"""
   assert ReadInstruction.from_spec(snake_case			).to_spec() == spec_train_pct_rounding

   __lowerCamelCase:					List[str]	 =						"""train[:10%](pct1_dropremainder)+test[-10%:](pct1_dropremainder)"""
   assert ReadInstruction.from_spec(snake_case			).to_spec() == spec_train_test_pct_rounding



def      __lowerCAmelCase    (				)							->				List[str]:
   __lowerCamelCase:					Union[str, Any]	 =						"""dummy"""
   __lowerCamelCase:					Optional[int]	 =						[SplitInfo(name="""train"""    ,   num_examples=100			)]
   __lowerCamelCase:					int	 =						"""train[:33%]"""
   __lowerCamelCase:					Optional[int]	 =						"""arrow"""
   __lowerCamelCase:					Dict	 =						"""prefix"""

   __lowerCamelCase:					List[str]	 =						make_file_instructions(snake_case    ,   snake_case    ,   snake_case    ,   snake_case    ,   snake_case			)
   assert isinstance(snake_case    ,   snake_case			)
   assert file_instructions.num_examples == 33
   assert file_instructions.file_instructions == [
       {"filename": os.path.join(snake_case    ,   f'{name}-train.arrow'			), "skip": 0, "take": 33}
   ]

   __lowerCamelCase:					str	 =						[SplitInfo(name="""train"""    ,   num_examples=100    ,   shard_lengths=[10] * 10			)]
   __lowerCamelCase:					Optional[int]	 =						make_file_instructions(snake_case    ,   snake_case    ,   snake_case    ,   snake_case    ,   snake_case			)
   assert isinstance(snake_case    ,   snake_case			)
   assert file_instructions.num_examples == 33
   assert file_instructions.file_instructions == [
       {"filename": os.path.join(snake_case    ,   f'{name}-train-00000-of-00010.arrow'			), "skip": 0, "take": -1},
       {"filename": os.path.join(snake_case    ,   f'{name}-train-00001-of-00010.arrow'			), "skip": 0, "take": -1},
       {"filename": os.path.join(snake_case    ,   f'{name}-train-00002-of-00010.arrow'			), "skip": 0, "take": -1},
       {"filename": os.path.join(snake_case    ,   f'{name}-train-00003-of-00010.arrow'			), "skip": 0, "take": 3},
   ]







@pytest.mark.parametrize("""name, expected_exception"""    ,   [(None, TypeError), ("""""", ValueError)]			)
def      __lowerCAmelCase    (				snake_case						:     Tuple    ,   snake_case						:     List[Any]			)							->				int:
   __lowerCamelCase:					Optional[int]	 =						[SplitInfo(name="""train"""    ,   num_examples=100			)]
   __lowerCamelCase:					Union[str, Any]	 =						"""train"""
   __lowerCamelCase:					str	 =						"""arrow"""
   __lowerCamelCase:					Union[str, Any]	 =						"""prefix_path"""
   with pytest.raises(snake_case			):
      __lowerCamelCase:					List[Any]	 =						make_file_instructions(snake_case    ,   snake_case    ,   snake_case    ,   snake_case    ,   snake_case			)






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





     def __init__(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					List[str]=13     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]=7     ,						SCREAMING_SNAKE_CASE_       :					List[str]=False     ,						SCREAMING_SNAKE_CASE_       :					List[Any]=True     ,						SCREAMING_SNAKE_CASE_       :					str=True     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=True     ,						SCREAMING_SNAKE_CASE_       :					Any=99     ,						SCREAMING_SNAKE_CASE_       :					Dict=32     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]=5     ,						SCREAMING_SNAKE_CASE_       :					List[Any]=4     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=37     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]="gelu"     ,						SCREAMING_SNAKE_CASE_       :					Any=0.1     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=0.1     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]=512     ,						SCREAMING_SNAKE_CASE_       :					Any=16     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=2     ,						SCREAMING_SNAKE_CASE_       :					str=0.02     ,						SCREAMING_SNAKE_CASE_       :					Tuple=3     ,						SCREAMING_SNAKE_CASE_       :					Any=4     ,						SCREAMING_SNAKE_CASE_       :					Tuple=None     ,						SCREAMING_SNAKE_CASE_       :					str=0     ,						):
        __lowerCamelCase:					Optional[Any]	 =						parent
        __lowerCamelCase:					Union[str, Any]	 =						batch_size
        __lowerCamelCase:					str	 =						seq_length
        __lowerCamelCase:					str	 =						is_training
        __lowerCamelCase:					Optional[Any]	 =						use_input_mask
        __lowerCamelCase:					Union[str, Any]	 =						use_token_type_ids
        __lowerCamelCase:					Any	 =						use_labels
        __lowerCamelCase:					str	 =						vocab_size
        __lowerCamelCase:					Tuple	 =						hidden_size
        __lowerCamelCase:					str	 =						num_hidden_layers
        __lowerCamelCase:					Dict	 =						num_attention_heads
        __lowerCamelCase:					int	 =						intermediate_size
        __lowerCamelCase:					Any	 =						hidden_act
        __lowerCamelCase:					Dict	 =						hidden_dropout_prob
        __lowerCamelCase:					str	 =						attention_probs_dropout_prob
        __lowerCamelCase:					Optional[Any]	 =						max_position_embeddings
        __lowerCamelCase:					Optional[Any]	 =						type_vocab_size
        __lowerCamelCase:					Optional[int]	 =						type_sequence_label_size
        __lowerCamelCase:					List[str]	 =						initializer_range
        __lowerCamelCase:					Tuple	 =						num_labels
        __lowerCamelCase:					str	 =						num_choices
        __lowerCamelCase:					Union[str, Any]	 =						scope
        __lowerCamelCase:					Dict	 =						projection_dim





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					int	 =						ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size				)

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

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

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

        __lowerCamelCase:					int	 =						self.get_config()

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





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





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]     ,						SCREAMING_SNAKE_CASE_       :					int     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					Tuple				):
        __lowerCamelCase:					List[Any]	 =						DPRQuestionEncoder(config=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)
        model.eval()
        __lowerCamelCase:					int	 =						model(SCREAMING_SNAKE_CASE_     ,						attention_mask=SCREAMING_SNAKE_CASE_     ,						token_type_ids=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Optional[int]	 =						model(SCREAMING_SNAKE_CASE_     ,						token_type_ids=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Union[str, Any]	 =						model(SCREAMING_SNAKE_CASE_				)
        self.parent.assertEqual(result.pooler_output.shape     ,						(self.batch_size, self.projection_dim or self.hidden_size)				)





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



@require_torch
class 							a       (      _UpperCAmelCase  ,_UpperCAmelCase  ,unittest.TestCase ):
     UpperCAmelCase__			:	Dict       =						(
         (
             DPRContextEncoder,
             DPRQuestionEncoder,
             DPRReader,
         )
         if is_torch_available()
         else ()
     )
     UpperCAmelCase__			:	List[str]       =						{"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}

     UpperCAmelCase__			:	str       =						False
     UpperCAmelCase__			:	Optional[Any]       =						False  # why?
     UpperCAmelCase__			:	List[Any]       =						False
     UpperCAmelCase__			:	int       =						False





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]				):
        __lowerCamelCase:					str	 =						DPRModelTester(self				)
        __lowerCamelCase:					Dict	 =						ConfigTester(self     ,						config_class=SCREAMING_SNAKE_CASE_     ,						hidden_size=37				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any				):
        self.config_tester.run_common_tests()





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]				):
        __lowerCamelCase:					Optional[Any]	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_context_encoder(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int				):
        __lowerCamelCase:					Any	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_question_encoder(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict				):
        __lowerCamelCase:					str	 =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_reader(*SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					str	 =						self.model_tester.prepare_config_and_inputs()[0]

        __lowerCamelCase:					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_				)
           __lowerCamelCase:					List[Any]	 =						DPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE_     ,						projection_dim=512				)

        self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)







     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Dict				):
        for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Dict	 =						DPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

        for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Any	 =						DPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

        for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Tuple	 =						DPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

        for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __lowerCamelCase:					Optional[int]	 =						DPRReader.from_pretrained(SCREAMING_SNAKE_CASE_				)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)



@require_torch
class 							a       (      unittest.TestCase ):





     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        __lowerCamelCase:					Union[str, Any]	 =						DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""     ,						return_dict=SCREAMING_SNAKE_CASE_				)
        model.to(SCREAMING_SNAKE_CASE_				)

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







     @slow
     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]				):
        __lowerCamelCase:					Dict	 =						DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""				)
        __lowerCamelCase:					Optional[Any]	 =						DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""				)
        model.to(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					int	 =						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_				)

        __lowerCamelCase:					Optional[Any]	 =						model(**SCREAMING_SNAKE_CASE_				)

        # compare the actual values for a slice.
        __lowerCamelCase:					Optional[Any]	 =						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=SCREAMING_SNAKE_CASE_     ,						)

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


