






"""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  lowercase_     :






      def __init__(     self  ,  a_  ,  a_=1_3  ,  a_=7  ,  a_=False  ,  a_=True  ,  a_=True  ,  a_=True  ,  a_=9_9  ,  a_=3_2  ,  a_=5  ,  a_=4  ,  a_=3_7  ,  a_="gelu"  ,  a_=0.1  ,  a_=0.1  ,  a_=5_1_2  ,  a_=1_6  ,  a_=2  ,  a_=0.02  ,  a_=3  ,  a_=4  ,  a_=None  ,  a_=0  ,  )						->List[Any]:







        '''simple docstring'''


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







        '''simple docstring'''


        _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[Any]:







        '''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       lowerCamelCase__     (     self  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_						)						->str:







        '''simple docstring'''


        _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  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_						)						->Optional[int]:







        '''simple docstring'''


        _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  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_						)						->str:







        '''simple docstring'''


        _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						)						->List[Any]:







        '''simple docstring'''


        _a					      =						self.prepare_config_and_inputs()
        (
            _a   
        )					      =						config_and_inputs
        _a					      =						{"input_ids": input_ids}
        return config, inputs_dict







@require_torch
class  lowercase_     (SCREAMING_SNAKE_CASE__,     SCREAMING_SNAKE_CASE__,     unittest.TestCase  ):
      A__						:		List[str]							      =					(
          (
              DPRContextEncoder,
              DPRQuestionEncoder,
              DPRReader,
          )
          if is_torch_available()
          else ()
      )
      A__						:		Dict							      =					{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

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






      def       lowerCamelCase__     (     self						)						->Union[str, Any]:







        '''simple docstring'''


        _a					      =						DPRModelTester(self						)
        _a					      =						ConfigTester(self  ,  config_class=_UpperCAmelCase  ,  hidden_size=3_7						)






      def       lowerCamelCase__     (     self						)						->List[str]:







        '''simple docstring'''


        self.config_tester.run_common_tests()






      def       lowerCamelCase__     (     self						)						->Optional[int]:







        '''simple docstring'''


        _a					      =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_context_encoder(*_UpperCAmelCase						)






      def       lowerCamelCase__     (     self						)						->str:







        '''simple docstring'''


        _a					      =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_question_encoder(*_UpperCAmelCase						)






      def       lowerCamelCase__     (     self						)						->Any:







        '''simple docstring'''


        _a					      =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_reader(*_UpperCAmelCase						)






      def       lowerCamelCase__     (     self						)						->Optional[Any]:







        '''simple docstring'''


        _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_1_2						)

        self.assertIsNotNone(_UpperCAmelCase						)






      @slow
      def       lowerCamelCase__     (     self						)						->Optional[Any]:







        '''simple docstring'''


        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  lowercase_     (unittest.TestCase  ):






      @slow
      def       lowerCamelCase__     (     self						)						->Dict:







        '''simple docstring'''


        _a					      =						DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"  ,  return_dict=_UpperCAmelCase						)
        model.to(_UpperCAmelCase						)

        _a					      =						torch.tensor(
            [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]  ,  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[:, :1_0]  ,  _UpperCAmelCase  ,  atol=1E-4						)						)






      @slow
      def       lowerCamelCase__     (     self						)						->List[str]:







        '''simple docstring'''


        _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[:, :1_0]  ,  _UpperCAmelCase  ,  atol=1E-4						)						)
        self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]  ,  _UpperCAmelCase  ,  atol=1E-4						)						)








'''simple docstring'''






import os
import unittest

from transformers.models.bertweet.tokenization_bertweet import VOCAB_FILES_NAMES, BertweetTokenizer

from ...test_tokenization_common import TokenizerTesterMixin



class    A						(					SCREAMING_SNAKE_CASE__     ,				unittest.TestCase						):




				'''simple docstring'''





				A       =							BertweetTokenizer
				A       =							False
				def 			a_			(self     )      ->			Any:
							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							__UpperCamelCase :     str										=  ["I", "m", "V@@", "R@@", "r", "e@@"]
							__UpperCamelCase :     int										=  dict(zip(_UpperCAmelCase					,    range(len(_UpperCAmelCase     )     )     )     )
							__UpperCamelCase :     List[Any]										=  ["#version: 0.2", "a m</w>"]
							__UpperCamelCase :     str										=  {"unk_token": "<unk>"}

							__UpperCamelCase :     Any										=  os.path.join(self.tmpdirname					,    VOCAB_FILES_NAMES["vocab_file"]     )
							__UpperCamelCase :     Tuple										=  os.path.join(self.tmpdirname					,    VOCAB_FILES_NAMES["merges_file"]     )
							with open(self.vocab_file					,    "w"					,    encoding="utf-8"     ) as fp:
										for token in vocab_tokens:
													fp.write(f"{token} {vocab_tokens[token]}\n"     )
							with open(self.merges_file					,    "w"					,    encoding="utf-8"     ) as fp:
										fp.write("\n".join(_UpperCAmelCase     )     )
				def 			a_			(self					,    **_UpperCAmelCase     )      ->			str:
							kwargs.update(self.special_tokens_map     )
							return BertweetTokenizer.from_pretrained(self.tmpdirname					,    **_UpperCAmelCase     )
				def 			a_			(self					,    _UpperCAmelCase     )      ->			Dict:
							__UpperCamelCase :     Dict										=  "I am VinAI Research"
							__UpperCamelCase :     Any										=  "I <unk> m V<unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>"
							return input_text, output_text





				def 			a_			(self     )      ->			str:
							__UpperCamelCase :     Tuple										=  BertweetTokenizer(self.vocab_file					,    self.merges_file					,    **self.special_tokens_map     )
							__UpperCamelCase :     Union[str, Any]										=  "I am VinAI Research"
							__UpperCamelCase :     List[str]										=  "I a@@ m V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h".split()
							__UpperCamelCase :     Optional[int]										=  tokenizer.tokenize(_UpperCAmelCase     )
							self.assertListEqual(_UpperCAmelCase					,    _UpperCAmelCase     )

							__UpperCamelCase :     Optional[Any]										=  tokens + [tokenizer.unk_token]

							__UpperCamelCase :     Any										=  [4, 3, 5, 6, 3, 3, 3, 4, 7, 9, 3, 9, 3, 3, 3, 3, 3]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase     )					,    _UpperCAmelCase     )



