


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




      '''simple docstring'''


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




      def      _snake_case	(				self:       Dict				):
       __lowerCamelCase		:							Dict							=						ids_tensor([self.batch_size, self.seq_length]						,      self.vocab_size				)

       __lowerCamelCase		:							Dict							=						None
       if self.use_input_mask:
        __lowerCamelCase		:							List[str]							=						random_attention_mask([self.batch_size, self.seq_length]				)

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

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

       __lowerCamelCase		:							Dict							=						self.get_config()

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




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




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




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



@require_torch
class        A_					(       __UpperCamelCase					,      __UpperCamelCase					,      unittest.TestCase    ):




      '''simple docstring'''


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

      __snake_case					       =   False
      __snake_case					       =   False  # why?
      __snake_case					       =   False
      __snake_case					       =   False




      def      _snake_case	(				self:       Optional[Any]				):
       __lowerCamelCase		:							Union[str, Any]							=						DPRModelTester(self				)
       __lowerCamelCase		:							List[Any]							=						ConfigTester(self						,      config_class=a						,      hidden_size=37				)




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




      def      _snake_case	(				self:       Dict				):
       __lowerCamelCase		:							Optional[int]							=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_context_encoder(*a				)




      def      _snake_case	(				self:       int				):
       __lowerCamelCase		:							Tuple							=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_question_encoder(*a				)




      def      _snake_case	(				self:       Union[str, Any]				):
       __lowerCamelCase		:							Any							=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_reader(*a				)




      def      _snake_case	(				self:       List[str]				):
       __lowerCamelCase		:							Optional[int]							=						self.model_tester.prepare_config_and_inputs()[0]

       __lowerCamelCase		:							Dict							=						DPRQuestionEncoder(config=a				)
       model.to(a				)
       model.eval()

       with tempfile.TemporaryDirectory() as tmp_dirname:
        model.save_pretrained(a				)
        __lowerCamelCase		:							Optional[Any]							=						DPRQuestionEncoder.from_pretrained(a						,      projection_dim=512				)

       self.assertIsNotNone(a				)




      @slow
      def      _snake_case	(				self:       Any				):
       for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCamelCase		:							Optional[Any]							=						DPRContextEncoder.from_pretrained(a				)
        self.assertIsNotNone(a				)

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

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

       for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCamelCase		:							List[str]							=						DPRReader.from_pretrained(a				)
        self.assertIsNotNone(a				)



@require_torch
class        A_					(       unittest.TestCase    ):




      '''simple docstring'''


      @slow
      def      _snake_case	(				self:       Tuple				):
       __lowerCamelCase		:							Dict							=						DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'						,      return_dict=a				)
       model.to(a				)

       __lowerCamelCase		:							Optional[Any]							=						torch.tensor(
           [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]						,      dtype=torch.long						,      device=a				)  # [CLS] hello, is my dog cute? [SEP]
       __lowerCamelCase		:							Optional[int]							=						model(a				)[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       __lowerCamelCase		:							List[Any]							=						torch.tensor(
           [
               [
                   0.0_3_2_3_6_2_5_3,
                   0.1_2_7_5_3_3_3_5,
                   0.1_6_8_1_8_5_0_9,
                   0.0_0_2_7_9_7_8_6,
                   0.3_8_9_6_9_3_3,
                   0.2_4_2_6_4_9_4_5,
                   0.2_1_7_8_9_7_1,
                   -0.0_2_3_3_5_2_2_7,
                   -0.0_8_4_8_1_9_5_9,
                   -0.1_4_3_2_4_1_1_7,
               ]
           ]						,      dtype=torch.float						,      device=a						,      )
       self.assertTrue(torch.allclose(output[:, :10]						,      a						,      atol=1e-4				)				)




      @slow
      def      _snake_case	(				self:       Any				):
       __lowerCamelCase		:							Tuple							=						DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'				)
       __lowerCamelCase		:							List[str]							=						DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'				)
       model.to(a				)

       __lowerCamelCase		:							Optional[int]							=						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				)

       __lowerCamelCase		:							int							=						model(**a				)

       # compare the actual values for a slice.
       __lowerCamelCase		:							Dict							=						torch.tensor(
           [[-1_0.3_0_0_5, -1_0.7_7_6_5, -1_1.4_8_7_2, -1_1.6_8_4_1, -1_1.9_3_1_2, -1_0.3_0_0_2, -9.8_5_4_4, -1_1.7_3_7_8, -1_2.0_8_2_1, -1_0.2_9_7_5]]						,      dtype=torch.float						,      device=a						,      )

       __lowerCamelCase		:							str							=						torch.tensor(
           [[-1_1.0_6_8_4, -1_1.7_0_4_1, -1_1.5_3_9_7, -1_0.3_4_6_5, -1_0.8_7_9_1, -6.8_4_4_3, -1_1.9_9_5_9, -1_1.0_3_6_4, -1_0.0_0_9_6, -6.8_4_0_5]]						,      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 unittest

from transformers import ErnieMTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


lowercase_           =	get_tests_dir('fixtures/spiece.model')


@require_sentencepiece
@require_tokenizers
class        A_					(       __UpperCamelCase					,      unittest.TestCase    ):




      '''simple docstring'''


      __snake_case					       =   ErnieMTokenizer
      __snake_case					       =   False
      __snake_case					       =   True
      __snake_case					       =   False
      __snake_case					       =   False




      def      _snake_case	(				self:       Optional[int]				):
       super().setUp()

       # We have a SentencePiece fixture for testing
       __lowerCamelCase		:							List[str]							=						ErnieMTokenizer(a						,      unk_token='<unk>'						,      pad_token='<pad>'				)
       tokenizer.save_pretrained(self.tmpdirname				)




      def      _snake_case	(				self:       Dict						,      a:       List[Any]				):
       __lowerCamelCase		:							Optional[Any]							=						'this is a test'
       __lowerCamelCase		:							Optional[int]							=						'this is a test'
       return input_text, output_text




      def      _snake_case	(				self:       Tuple				):
       __lowerCamelCase		:							List[str]							=						'<pad>'
       __lowerCamelCase		:							Union[str, Any]							=						0

       self.assertEqual(self.get_tokenizer()._convert_token_to_id(a				)						,      a				)
       self.assertEqual(self.get_tokenizer()._convert_id_to_token(a				)						,      a				)




      def      _snake_case	(				self:       List[Any]				):
       __lowerCamelCase		:							List[str]							=						list(self.get_tokenizer().get_vocab().keys()				)

       self.assertEqual(vocab_keys[0]						,      '<pad>'				)
       self.assertEqual(vocab_keys[1]						,      '<unk>'				)
       self.assertEqual(vocab_keys[-1]						,      '▁eloquent'				)
       self.assertEqual(len(a				)						,      3_0000				)




      def      _snake_case	(				self:       Tuple				):
       self.assertEqual(self.get_tokenizer().vocab_size						,      3_0000				)




      def      _snake_case	(				self:       int				):
       if not self.test_rust_tokenizer:
        return

       __lowerCamelCase		:							Any							=						self.get_tokenizer()
       __lowerCamelCase		:							Optional[Any]							=						self.get_rust_tokenizer()

       __lowerCamelCase		:							Optional[int]							=						'I was born in 92000, and this is falsé.'

       __lowerCamelCase		:							Optional[Any]							=						tokenizer.tokenize(a				)
       __lowerCamelCase		:							Dict							=						rust_tokenizer.tokenize(a				)
       self.assertListEqual(a						,      a				)

       __lowerCamelCase		:							List[str]							=						tokenizer.encode(a						,      add_special_tokens=a				)
       __lowerCamelCase		:							List[Any]							=						rust_tokenizer.encode(a						,      add_special_tokens=a				)
       self.assertListEqual(a						,      a				)

       __lowerCamelCase		:							str							=						self.get_rust_tokenizer()
       __lowerCamelCase		:							str							=						tokenizer.encode(a				)
       __lowerCamelCase		:							List[str]							=						rust_tokenizer.encode(a				)
       self.assertListEqual(a						,      a				)




      def      _snake_case	(				self:       Optional[Any]				):
       __lowerCamelCase		:							Union[str, Any]							=						ErnieMTokenizer(a						,      do_lower_case=a						,      unk_token='<unk>'						,      pad_token='<pad>'				)

       __lowerCamelCase		:							List[str]							=						tokenizer.tokenize('This is a test'				)
       self.assertListEqual(a						,      ['▁this', '▁is', '▁a', '▁test']				)

       self.assertListEqual(tokenizer.convert_tokens_to_ids(a				)						,      [48, 25, 21, 1289]				)

       __lowerCamelCase		:							Optional[Any]							=						tokenizer.tokenize('I was born in 92000, and this is falsé.'				)
       # ErnieMTokenizer(paddlenlp implementation) outputs '9' instead of '_9' so to mimic that '_9' is changed to '9'
       self.assertListEqual(
           a						,      ['▁i', '▁was', '▁born', '▁in', '9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', 'é', '.']				)
       __lowerCamelCase		:							Any							=						tokenizer.convert_tokens_to_ids(a				)
       self.assertListEqual(a						,      [31, 23, 386, 19, 518, 3050, 15, 17, 48, 25, 8256, 18, 1, 9]				)

       __lowerCamelCase		:							int							=						tokenizer.convert_ids_to_tokens(a				)
       self.assertListEqual(
           a						,      ['▁i', '▁was', '▁born', '▁in', '9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '.']						,      )




      def      _snake_case	(				self:       str				):
       __lowerCamelCase		:							Tuple							=						ErnieMTokenizer(a						,      unk_token='<unk>'						,      pad_token='<pad>'				)

       __lowerCamelCase		:							Tuple							=						tokenizer.encode('sequence builders'				)
       __lowerCamelCase		:							List[Any]							=						tokenizer.encode('multi-sequence build'				)

       __lowerCamelCase		:							Optional[Any]							=						tokenizer.build_inputs_with_special_tokens(a				)
       __lowerCamelCase		:							Tuple							=						tokenizer.build_inputs_with_special_tokens(a						,      a				)

       assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
       assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + [
           tokenizer.sep_token_id
       ] + text_a + [tokenizer.sep_token_id]




      @slow
      def      _snake_case	(				self:       List[Any]				):
       # fmt: off
       __lowerCamelCase		:							int							=						{'input_ids': [[0, 1_1062, 8_2772, 7, 15, 8_2772, 538, 5_1529, 237, 1_7198, 1290, 206, 9, 21_5175, 1314, 136, 1_7198, 1290, 206, 9, 5_6359, 42, 12_2009, 9, 1_6466, 16, 8_7344, 4537, 9, 4717, 7_8381, 6, 15_9958, 7, 15, 2_4480, 618, 4, 527, 2_2693, 9, 304, 4, 2777, 2_4480, 9874, 4, 4_3523, 594, 4, 803, 1_8392, 3_3189, 18, 4, 4_3523, 2_4447, 5, 5, 5, 16, 100, 2_4955, 8_3658, 9626, 14_4057, 15, 839, 2_2335, 16, 136, 2_4955, 8_3658, 8_3479, 15, 3_9102, 724, 16, 678, 645, 6460, 1328, 4589, 42, 12_2009, 11_5774, 23, 3559, 1328, 4_6876, 7, 136, 5_3894, 1940, 4_2227, 4_1159, 1_7721, 823, 425, 4, 2_7512, 9_8722, 206, 136, 5531, 4970, 919, 1_7336, 5, 2], [0, 2_0080, 618, 83, 8_2775, 47, 479, 9, 1517, 73, 5_3894, 333, 8_0581, 11_0117, 1_8811, 5256, 1295, 51, 15_2526, 297, 7986, 390, 12_4416, 538, 3_5431, 214, 98, 1_5044, 2_5737, 136, 7108, 4_3701, 23, 756, 13_5355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 581, 6_3773, 11_9455, 6, 14_7797, 8_8203, 7, 645, 70, 21, 3285, 1_0269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}
       # fmt: on

       self.tokenizer_integration_test_util(
           expected_encoding=a						,      model_name='susnato/ernie-m-base_pytorch'						,      sequences=[
               'Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides '
               'general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet...) for Natural '
               'Language Understanding (NLU) and Natural Language Generation (NLG) with over32+ pretrained '
               'models in100+ languages and deep interoperability between Jax, PyTorch and TensorFlow.',
               'BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly '
               'conditioning on both left and right context in all layers.',
               'The quick brown fox jumps over the lazy dog.',
           ]						,      )

